memoization

Recursion with versus without memoization

北城以北 提交于 2019-12-02 10:40:51
I got homework in school to calculate Catalan number with recursion: 1st without memoization def catalan_rec(n): res = 0 if n == 0: return 1 else: for i in range (n): res += (catalan_rec(i))*(catalan_rec(n-1-i)) return res 2nd with: def catalan_mem(n, memo = None): if memo == None: memo = {0: 1} res = 0 if n not in memo: for i in range (n): res += (catalan_mem(i))*(catalan_mem(n-1-i)) memo[n] = res return memo[n] The weirdest thing happened to me: the memoization takes twice much time! When it should be the other way around! Can someone please explain this to me? This question inspired me to

Implementing Automatic Memoization (returns a closured function) in JavaScript

给你一囗甜甜゛ 提交于 2019-12-02 10:24:32
I've read http://www.sitepoint.com/implementing-memoization-in-javascript/ Automatic Memoization In all of the previous examples, the functions were explicitly modified to add memoization. It is also possible to implement a memoization infrastructure without modifying the functions at all. This is useful because it allows the function logic to be implemented separately from the memoization logic. This is done by creating a utility function which takes a function as input and applies memoization to it. The following memoize() function takes a function, “func”, as input. memoize() returns a new

How to keep the first result of a function from Prolog?

风格不统一 提交于 2019-12-02 07:11:06
问题 I need to write a customized function that will be called many times by other fixed functions. In this function, at the first called time, it will return the total number of lines of a file. The second called time of this function, forward, will return the number of lines in small sections of this file. My question is how I keep the first returned result(total number of lines of a file) and use it for the next called times of my function. I need to write or declare any thing only in this

Haskell: memoization

拥有回忆 提交于 2019-12-02 05:58:48
I'm trying to relearn Haskell, after many years away and forgetting everything, and I find myself still confused my memoization . In particular, I'm trying to write a program to generate the number of derangements D[n] of n objects (permutations with no item in its original place); the numbers D[n] can be defined recursively by D[1]=0 , D[2]=1 , D[n]=(n-1)(D[n-1]+D[n-2]) . So this works: der :: Int -> Integer der n = lder !! n where lder = 1 : 0 : zipWith3 (\n d1 d2 -> n * (d1+d2)) [1..] lder (tail lder) as does this (which is a bit clumsy as it requires three functions): nder :: Int ->

Memoization pascals triangle

 ̄綄美尐妖づ 提交于 2019-12-02 03:46:51
I'm not interested in the actual solution or other methods solving the problem, it's the memoization i need help with :) I need help doing a pascals triangle problem with memoization. I want to get the middle number in the base of the triangle. (Project Euler 15) The first example is not memoized (although the name suggests so) "20 20" not solvable Second attempt is an attempt on doing something similar to: http://www.haskell.org/haskellwiki/Memoization Third is hlints suggestion on no2 if that is more readable for someone. I get this error, but i'm not sure its right even if it would compile.

Containers for different signature functions

白昼怎懂夜的黑 提交于 2019-12-01 20:23:28
I'm trying to programming in C++ a framework where the user can indicates a set of functions inside its program where he wants to apply a memoization strategy. So let's suppose that we have 5 functions in our program f1...f5 and we want to avoid the (expensive) re-computation for the functions f1 and f3 if we already called them with the same input. Notice that each function can have different return and argument types. I found this solution for the problem, but you can use only double and int . MY SOLUTION Ok I wrote this solution for my problem, but I don't know if it's efficient, typesafe

Does Haskell optimizer utilize memoization for repeated function calls in a scope?

二次信任 提交于 2019-12-01 15:21:05
Consider this function: f as = if length as > 100 then length as else 100 Since the function is pure it's obvious that the length will be the same in both calls. My question is does Haskell optimizer turn the code above into equivalent of the following? f as = let l = length as in if l > 100 then l else 100 If it does, then which level setting enables it? If it doesn't, then why? In this scenario a memory waste can't be the reason as explained in this answer , because the introduced variable gets released as soon as the function execution is finished. Please note that this is not a duplicate

Does Haskell optimizer utilize memoization for repeated function calls in a scope?

筅森魡賤 提交于 2019-12-01 14:16:38
问题 Consider this function: f as = if length as > 100 then length as else 100 Since the function is pure it's obvious that the length will be the same in both calls. My question is does Haskell optimizer turn the code above into equivalent of the following? f as = let l = length as in if l > 100 then l else 100 If it does, then which level setting enables it? If it doesn't, then why? In this scenario a memory waste can't be the reason as explained in this answer, because the introduced variable

Why does this memoization implementation work on anonymous functions but not work on declared functions?

限于喜欢 提交于 2019-12-01 10:55:49
I'm trying to use memoization to optimize an explicitly self recursive implementation of the Fibonacci function. The implementation which is fairly standard (a simple and rather naïve implementation though to focus on the actual problem) follows. Function.prototype.memoize = function () { var originalFunction = this, slice = Array.prototype.slice; cache = {}; return function () { var key = slice.call(arguments); if (key in cache) { return cache[key]; } else { return cache[key] = originalFunction.apply(this, key); } }; }; Now, when creating and memoizing a function as follows, this works 1 .

Caching result of pre-computed function in Matlab

怎甘沉沦 提交于 2019-12-01 07:51:32
问题 I have two arrays, x and y . x is the input of the function and y is the function values. For example, x = [ 1 2 3 4 5 6 7 8 9 10] , y = [ 3 6 2 4 1 6 7 0 1 8 ] . Both are the same length. Suppose I have an another array z containing [ 2 3 8 9 10 3] (not the same length as x and y ), Is there any functions that produce the output [6 2 0 1 8 2] (return value at corresponding indices) without using for-loop through each element of array? Thank you so much edit1* How can I do if the numbers in