Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 798aaf8

Browse files
committed
Implemented _cache_key() for polynomials
This allows caching of polynomials with unhashable coefficients.
1 parent fa16bc7 commit 798aaf8

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/sage/rings/polynomial/polynomial_element.pyx

+36-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ AUTHORS:
1313
1414
- Simon King: Use a faster way of conversion from the base ring.
1515
16-
- Julian Rueth (2012-05-25): Fixed is_squarefree() for imperfect fields.
17-
Fixed division without remainder over QQbar.
16+
- Julian Rueth (2012-05-25,2014-05-09): Fixed is_squarefree() for imperfect
17+
fields, fixed division without remainder over QQbar, added ``_cache_key``
18+
for polynomials with unhashable coefficients
1819
1920
- Simon King (2013-10): Implement copying of :class:`PolynomialBaseringInjection`.
2021
@@ -829,6 +830,39 @@ cdef class Polynomial(CommutativeAlgebraElement):
829830
def __iter__(self):
830831
return iter(self.list())
831832

833+
def _cache_key(self):
834+
"""
835+
Return a key which uniquely identifies this element among all elements
836+
in the parent.
837+
838+
.. SEEALSO::
839+
840+
:meth:`sage.structure.sage_object.SageObject._cache_key`
841+
842+
EXAMPLES:
843+
844+
This enables caching for polynomials with unhashable coefficients such
845+
as `p`-adics::
846+
847+
sage: K.<u> = Qq(4)
848+
sage: R.<x> = K[]
849+
sage: f = x
850+
sage: hash(f)
851+
Traceback (most recent call last):
852+
...
853+
TypeError: unhashable type: 'sage.rings.padics.padic_ZZ_pX_CR_element.pAdicZZpXCRElement'
854+
sage: f._cache_key()
855+
(0, 1 + O(2^20))
856+
857+
sage: @cached_function
858+
....: def foo(t): return t
859+
....:
860+
sage: foo(x)
861+
(1 + O(2^20))*x
862+
863+
"""
864+
return tuple(self)
865+
832866
# you may have to replicate this boilerplate code in derived classes if you override
833867
# __richcmp__. The python documentation at http://docs.python.org/api/type-structs.html
834868
# explains how __richcmp__, __hash__, and __cmp__ are tied together.

0 commit comments

Comments
 (0)