memoization

Does Python intern strings?

回眸只為那壹抹淺笑 提交于 2019-11-26 22:58:50
In Java, explicitly declared Strings are interned by the JVM, so that subsequent declarations of the same String results in two pointers to the same String instance, rather than two separate (but identical) Strings. For example: public String baz() { String a = "astring"; return a; } public String bar() { String b = "astring" return b; } public void main() { String a = baz() String b = bar() assert(a == b) // passes } My question is, does CPython (or any other Python runtime) do the same thing for strings? For example, if I have some class: class example(): def __init__(): self._inst =

Numpy Pure Functions for performance, caching

人盡茶涼 提交于 2019-11-26 22:54:12
I'm writing some moderately performance critical code in numpy. This code will be in the inner most loop, of a computation that's run time is measured in hours. A quick calculation suggest that this code will be executed up something like 10^12 times, in some variations of the calculation. So the function is to calculate sigmoid(X) and another to calculate its derivative (gradient). Sigmoid has the property that for y=sigmoid(x), dy/dx= y(1-y) In python for numpy this looks like: sigmoid = vectorize(lambda(x): 1.0/(1.0+exp(-x))) grad_sigmoid = vectorize(lambda (x): sigmoid(x)*(1-sigmoid(x)))

What type to use to store an in-memory mutable data table in Scala?

橙三吉。 提交于 2019-11-26 22:16:27
Each time a function is called, if it's result for a given set of argument values is not yet memoized I'd like to put the result into an in-memory table. One column is meant to store a result, others to store arguments values. How do I best implement this? Arguments are of diverse types, including some enums. In C# I'd generally use DataTable. Is there an equivalent in Scala? Aaron Novstrup You could use a mutable.Map[TupleN[A1, A2, ..., AN], R] , or if memory is a concern, a WeakHashMap [1]. The definitions below (built on the memoization code from michid's blog ) allow you to easily memoize

How can I memoize a class instantiation in Python?

拈花ヽ惹草 提交于 2019-11-26 19:52:14
问题 Ok, here is the real world scenario: I'm writing an application, and I have a class that represents a certain type of files (in my case this is photographs but that detail is irrelevant to the problem). Each instance of the Photograph class should be unique to the photo's filename. The problem is, when a user tells my application to load a file, I need to be able to identify when files are already loaded, and use the existing instance for that filename, rather than create duplicate instances

Scala Memoization: How does this Scala memo work?

筅森魡賤 提交于 2019-11-26 19:12:40
问题 The following code is from Pathikrit's Dynamic Programming repository. I'm mystified by both its beauty and peculiarity. def subsetSum(s: List[Int], t: Int) = { type DP = Memo[(List[Int], Int), (Int, Int), Seq[Seq[Int]]] implicit def encode(key: (List[Int], Int)) = (key._1.length, key._2) lazy val f: DP = Memo { case (Nil, 0) => Seq(Nil) case (Nil, _) => Nil case (a :: as, x) => (f(as, x - a) map {_ :+ a}) ++ f(as, x) } f(s, t) } The type Memo is implemented in another file: case class Memo[I

memoization library for python 2.7

让人想犯罪 __ 提交于 2019-11-26 18:38:29
I see that python 3.2 has memoization as a decorator in functools library. http://docs.python.org/py3k/library/functools.html#functools.lru_cache Unfortunately it is not yet backported to 2.7. Is there any specific reason as why it is not available in 2.7? Is there any 3rd party library providing the same feature or should I write my own? Paolo Moretti Is there any specific reason as why it is not available in 2.7? @Nirk has already provided the reason: unfortunately, the 2.x line only receive bugfixes, and new features are developed for 3.x only. Is there any 3rd party library providing the

Memoizing Coin Change

跟風遠走 提交于 2019-11-26 18:36:16
问题 I want to convert my coin change function to memoized function to do that, I decided to use dictionary so that a key in my dict will be the coin and the value will be a list that contain all the coins that can change the "key" coin. what I did is: def change(a,kinds=(50,20,10,5,1)): if(a==0): return 1 if(a<0 or len(kinds)==0): return 0 return change(a-kinds[0],kinds)+change(a,kinds[1:]) def memoizeChange(f): cache={} def memo(a,kinds=(50,20,10,5,1)): if len(cache)>0 and kinds in cache[a]:

Writing Universal memoization function in C++11

£可爱£侵袭症+ 提交于 2019-11-26 17:15:45
Looking for a way to implement a universal generic memoization function which will take a function and return the memoized version of the same? Looking for something like @memo (from Norving's site)decorator in python. def memo(f): table = {} def fmemo(*args): if args not in table: table[args] = f(*args) return table[args] fmemo.memo = table return fmemo Going more general, is there a way to express generic and reusable decorators in C++, possibly using the new features of C++11? A compact one returning a lambda: template <typename R, typename... Args> std::function<R (Args...)> memo(R (*fn)

Python - anyone have a memoizing decorator that can handle unhashable arguments?

非 Y 不嫁゛ 提交于 2019-11-26 15:46:57
问题 I've been using the following memoizing decorator (from the great book Python Algorithms: Mastering Basic Algorithms in the Python Language ... love it, btw). def memo(func): cache = {} @ wraps(func) def wrap(*args): if args not in cache: cache[args] = func(*args) return cache[args] return wrap The problem with this decorator is that the dictionary-based cache means that all of my arguments must be hashable. Does anyone have an implementation (or a tweak to this one) that allows for

Combine memoization and tail-recursion

送分小仙女□ 提交于 2019-11-26 15:38:11
问题 Is it possible to combine memoization and tail-recursion somehow? I'm learning F# at the moment and understand both concepts but can't seem to combine them. Suppose I have the following memoize function (from Real-World Functional Programming): let memoize f = let cache = new Dictionary<_, _>() (fun x -> match cache.TryGetValue(x) with | true, y -> y | _ -> let v = f(x) cache.Add(x, v) v) and the following factorial function: let rec factorial(x) = if (x = 0) then 1 else x * factorial(x - 1)