Ticket #1952: trac_1952-2.patch
| File trac_1952-2.patch, 9.0 kB (added by mhansen, 4 months ago) |
|---|
-
a/sage/misc/cachefunc.py
old new 52 52 EXAMPLES: 53 53 sage: g = CachedFunction(number_of_partitions) 54 54 sage: a = g(5) 55 sage: g. cache55 sage: g.get_cache() 56 56 {((5,), ()): 7} 57 57 sage: a = g(10^5) 58 58 sage: a == number_of_partitions(10^5) 59 59 True 60 60 """ 61 k = (args, tuple(kwds)) 62 if self.cache.has_key(k): 63 return self.cache[k] 61 cache = self.get_cache() 62 k = self.get_key(*args, **kwds) 63 if cache.has_key(k): 64 return cache[k] 64 65 w = self.f(*args, **kwds) 65 self.cache[k] = w66 cache[k] = w 66 67 return w 68 69 def get_cache(self): 70 """ 71 Returns the cache dictionary. 72 73 EXAMPLES: 74 sage: g = CachedFunction(number_of_partitions) 75 sage: a = g(5) 76 sage: g.get_cache() 77 {((5,), ()): 7} 78 79 """ 80 return self.cache 81 82 def is_in_cache(self, *args, **kwds): 83 """ 84 EXAMPLES: 85 sage: class Foo: 86 ... def __init__(self, x): 87 ... self._x = x 88 ... @cached_method 89 ... def f(self, z): 90 ... return self._x*z 91 ... 92 sage: a = Foo(2) 93 sage: a.f.is_in_cache(3) 94 False 95 sage: a.f(3) 96 6 97 sage: a.f.is_in_cache(3) 98 True 99 """ 100 cache = self.get_cache() 101 return self.get_key(*args, **kwds) in cache 102 103 def get_key(self, *args, **kwds): 104 """ 105 Returns the key in the cache to be used when args 106 and kwds are passed in as parameters. 107 108 EXAMPLES: 109 sage: class Foo: 110 ... def __init__(self, x): 111 ... self._x = x 112 ... @cached_method 113 ... def f(self): 114 ... return self._x^2 115 ... 116 sage: a = Foo(2) 117 sage: a.f.get_key() 118 ((), ()) 119 """ 120 return (args, tuple(sorted(kwds.items()))) 67 121 68 122 def __repr__(self): 69 123 """ … … 73 127 Cached version of <function number_of_partitions at 0x...> 74 128 """ 75 129 return "Cached version of %s"%self.f 130 131 def clear_cache(self): 132 """ 133 Clear the cache dictionary. 134 135 EXAMPLES: 136 sage: g = CachedFunction(number_of_partitions) 137 sage: a = g(5) 138 sage: g.get_cache() 139 {((5,), ()): 7} 140 sage: g.clear_cache() 141 sage: g.get_cache() 142 {} 143 """ 144 cache = self.get_cache() 145 for key in cache.keys(): 146 del cache[key] 147 76 148 77 149 cached_function = CachedFunction 78 150 … … 106 178 sage: a = Foo(2) 107 179 sage: a.f() 108 180 4 181 sage: a.f() is a.f() 182 True 109 183 sage: b = Foo(3) 110 184 sage: b.f() 111 185 9 112 186 """ 113 cache = self. _instance.__dict__.setdefault(self._cache_name, {})114 key = (args, tuple(kwds))187 cache = self.get_cache() 188 key = self.get_key(*args, **kwds) 115 189 if cache.has_key(key): 116 190 return cache[key] 117 191 else: 118 192 cache[key] = self.f(self._instance, *args, **kwds) 119 193 return cache[key] 120 194 195 def get_cache(self): 196 """ 197 Returns the cache dictionary. 198 199 EXAMPLES: 200 sage: class Foo: 201 ... def __init__(self, x): 202 ... self._x = x 203 ... @cached_method 204 ... def f(self): 205 ... return self._x^2 206 ... 207 sage: a = Foo(2) 208 sage: a.f() 209 4 210 sage: a.f.get_cache() 211 {((), ()): 4} 212 213 """ 214 return self._instance.__dict__.setdefault(self._cache_name, {}) 215 121 216 def __get__(self, inst, cls=None): 122 217 """ 123 218 This is needed to allow CachedFunction to decorate -
a/sage/rings/polynomial/multi_polynomial_ideal.py
old new 147 147 from sage.rings.integer import Integer 148 148 from sage.structure.sequence import Sequence 149 149 150 from sage.misc.cachefunc import CachedFunction150 from sage.misc.cachefunc import cached_method 151 151 from sage.misc.misc import prod 152 152 from sage.misc.sage_eval import sage_eval 153 153 … … 255 255 return func(*args, **kwds) 256 256 wrapper.__doc__=func.__doc__ 257 257 return wrapper 258 259 class cached(CachedFunction):260 r"""261 Specialised \code{CachedFunction} for the application in262 multivariate polynomial ideals.263 264 AUTHOR:265 -- Martin Albrecht266 """267 def __call__(self, *args, **kwds):268 """269 EXAMPLES:270 sage: P = PolynomialRing(GF(32003),4,'x')271 sage: I = sage.rings.ideal.Katsura(P)272 sage: I.groebner_basis.clear_cache()273 sage: gb = I.groebner_basis() # indirect doctest274 sage: I.groebner_basis.cache275 {(Multivariate Polynomial Ring in x0, x1, x2, x3 over Finite Field of size 32003,276 (x0 + 2*x1 + 2*x2 + 2*x3 - 1,277 x1^2 + 2*x0*x2 + 2*x1*x3 - x2,278 2*x0*x1 + 2*x1*x2 + 2*x2*x3 - x1,279 x0^2 + 2*x1^2 + 2*x2^2 + 2*x3^2 - x0),280 (),281 ()):282 [x0 + 2*x1 + 2*x2 + 2*x3 - 1,283 x2^2 + 2*x1*x3 - 13711*x2*x3 - 4568*x3^2 - 4572*x1 + 13715*x2 - 9145*x3,284 x1*x2 - 2*x1*x3 - 9147*x2*x3 - 13719*x3^2 + 2286*x1 + 9144*x2 + 4573*x3,285 x1^2 + 2*x1*x3 + 4573*x2*x3 - 9142*x3^2 - 9144*x1 - 4572*x2 + 13715*x3,286 x2*x3^2 + 3557*x3^3 - 1778*x1*x3 - 3161*x2*x3 + 5926*x3^2 - 10075*x1 - 6124*x2 + 11853*x3, x1*x3^2 - 10668*x3^3 - 3556*x1*x3 - 10075*x2*x3 + 3556*x3^2 - 889*x1 - 11853*x2,287 x3^4 + 12535*x3^3 + 7471*x1*x3 + 6188*x2*x3 + 10117*x3^2 + 10521*x1 + 11393*x2 + 11829*x3]}288 """289 from sage.rings.polynomial.multi_polynomial_ring_generic import is_MPolynomialRing290 k = self.gen_key(*args, **kwds)291 if self.cache.has_key(k) and "nocache" not in kwds:292 return self.cache[k]293 294 w = self.f(self.instance, *args, **kwds)295 if is_MPolynomialRing(self.instance.ring()):296 self.cache[k] = w297 return w298 299 def clear_cache(self):300 """301 Clear the cache.302 303 EXAMPLE:304 sage: P = PolynomialRing(GF(32003),4,'x')305 sage: I = sage.rings.ideal.Katsura(P)306 sage: I.groebner_basis.clear_cache()307 sage: I.groebner_basis.cache308 {}309 sage: gb = I.groebner_basis() # indirect doctest310 sage: len(I.groebner_basis.cache)311 1312 sage: I.groebner_basis.clear_cache()313 sage: len(I.groebner_basis.cache)314 0315 """316 self.cache = {}317 318 def gen_key(self, *args, **kwds):319 """320 Generate a key to the cache.321 322 EXAMPLE:323 sage: P = PolynomialRing(GF(32003),4,'x')324 sage: I = sage.rings.ideal.Katsura(P)325 sage: I.groebner_basis.gen_key()326 (Multivariate Polynomial Ring in x0, x1, x2, x3 over Finite Field of size 32003,327 (x0 + 2*x1 + 2*x2 + 2*x3 - 1,328 x1^2 + 2*x0*x2 + 2*x1*x3 - x2,329 2*x0*x1 + 2*x1*x2 + 2*x2*x3 - x1,330 x0^2 + 2*x1^2 + 2*x2^2 + 2*x3^2 - x0),331 (),332 ())333 """334 r = (self.instance.ring(), tuple(sorted(self.instance.gens())), args, tuple(kwds.iteritems()))335 return r336 337 def is_in_cache(self, *args, **kwds):338 """339 Check whether key is already in cache.340 341 EXAMPLE:342 sage: P = PolynomialRing(GF(32003),3,'x')343 sage: I = sage.rings.ideal.Katsura(P)344 sage: I.groebner_basis.is_in_cache()345 False346 sage: gb = I.groebner_basis()347 sage: I.groebner_basis.is_in_cache()348 True349 """350 return self.gen_key(*args, **kwds) in self.cache351 258 352 259 def is_MPolynomialIdeal(x): 353 260 r""" … … 1841 1748 return groebner_fan.GroebnerFan(self, is_groebner_basis=is_groebner_basis, 1842 1749 symmetry=symmetry, verbose=verbose) 1843 1750 1844 @cached 1751 @cached_method 1845 1752 def groebner_basis(self, algorithm='', *args, **kwds): 1846 1753 r""" 1847 1754 Return the reduced Groebner basis of this ideal. A Groeber basis