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  
    5252        EXAMPLES: 
    5353            sage: g = CachedFunction(number_of_partitions) 
    5454            sage: a = g(5) 
    55             sage: g.cache 
     55            sage: g.get_cache() 
    5656            {((5,), ()): 7} 
    5757            sage: a = g(10^5)  
    5858            sage: a == number_of_partitions(10^5) 
    5959            True 
    6060        """ 
    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] 
    6465        w = self.f(*args, **kwds) 
    65         self.cache[k] = w 
     66        cache[k] = w 
    6667        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()))) 
    67121 
    68122    def __repr__(self): 
    69123        """ 
     
    73127            Cached version of <function number_of_partitions at 0x...> 
    74128        """ 
    75129        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 
    76148 
    77149cached_function = CachedFunction 
    78150 
     
    106178            sage: a = Foo(2) 
    107179            sage: a.f() 
    108180            4 
     181            sage: a.f() is a.f() 
     182            True 
    109183            sage: b = Foo(3) 
    110184            sage: b.f() 
    111185            9 
    112186        """ 
    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
    115189        if cache.has_key(key): 
    116190            return cache[key] 
    117191        else: 
    118192            cache[key] = self.f(self._instance, *args, **kwds) 
    119193            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 
    121216    def __get__(self, inst, cls=None): 
    122217        """ 
    123218        This is needed to allow CachedFunction to decorate 
  • a/sage/rings/polynomial/multi_polynomial_ideal.py

    old new  
    147147from sage.rings.integer import Integer 
    148148from sage.structure.sequence import Sequence 
    149149 
    150 from sage.misc.cachefunc import CachedFunction 
     150from sage.misc.cachefunc import cached_method 
    151151from sage.misc.misc import prod 
    152152from sage.misc.sage_eval import sage_eval 
    153153 
     
    255255            return func(*args, **kwds) 
    256256    wrapper.__doc__=func.__doc__ 
    257257    return wrapper 
    258  
    259 class cached(CachedFunction): 
    260     r"""  
    261     Specialised \code{CachedFunction} for the application in 
    262     multivariate polynomial ideals. 
    263  
    264     AUTHOR: 
    265         -- Martin Albrecht 
    266     """ 
    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 doctest 
    274             sage: I.groebner_basis.cache 
    275             {(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_MPolynomialRing 
    290         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] = w 
    297         return w 
    298  
    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.cache 
    308             {} 
    309             sage: gb = I.groebner_basis() # indirect doctest 
    310             sage: len(I.groebner_basis.cache) 
    311             1 
    312             sage: I.groebner_basis.clear_cache() 
    313             sage: len(I.groebner_basis.cache) 
    314             0 
    315         """ 
    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 r 
    336  
    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             False 
    346             sage: gb = I.groebner_basis() 
    347             sage: I.groebner_basis.is_in_cache() 
    348             True 
    349         """ 
    350         return self.gen_key(*args, **kwds) in self.cache 
    351258 
    352259def is_MPolynomialIdeal(x): 
    353260    r""" 
     
    18411748        return groebner_fan.GroebnerFan(self, is_groebner_basis=is_groebner_basis, 
    18421749                                        symmetry=symmetry, verbose=verbose) 
    18431750 
    1844     @cached 
     1751    @cached_method 
    18451752    def groebner_basis(self, algorithm='', *args, **kwds): 
    18461753        r""" 
    18471754        Return the reduced Groebner basis of this ideal. A Groeber basis