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

Commit

Permalink
Trac 18157: fix garbage collected objects in doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
videlec committed Apr 13, 2015
1 parent c0f3e47 commit ced7bfc
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 46 deletions.
76 changes: 49 additions & 27 deletions src/sage/structure/coerce.pyx
Expand Up @@ -239,7 +239,8 @@ cdef class CoercionModel_cache_maps(CoercionModel):
sage: from sage.structure.coerce import CoercionModel_cache_maps
sage: cm = CoercionModel_cache_maps(4, .95)
sage: A = cm.get_action(ZZ, NumberField(x^2-2, 'a'), operator.mul)
sage: K = NumberField(x^2-2, 'a')
sage: A = cm.get_action(ZZ, K, operator.mul)
sage: f, g = cm.coercion_maps(QQ, int)
sage: f, g = cm.coercion_maps(ZZ, int)
Expand Down Expand Up @@ -476,7 +477,8 @@ cdef class CoercionModel_cache_maps(CoercionModel):
Result lives in Rational Field
Rational Field
sage: cm.explain(ZZ['x'], QQ)
sage: R = ZZ['x']
sage: cm.explain(R, QQ)
Action discovered.
Right scalar multiplication by Rational Field on Univariate Polynomial Ring in x over Integer Ring
Result lives in Univariate Polynomial Ring in x over Rational Field
Expand All @@ -502,10 +504,11 @@ cdef class CoercionModel_cache_maps(CoercionModel):
Sometimes with non-sage types there is not enough information to deduce
what will actually happen::
sage: cm.explain(RealField(100), float, operator.add)
sage: R100 = RealField(100)
sage: cm.explain(R100, float, operator.add)
Right operand is numeric, will attempt coercion in both directions.
Unknown result parent.
sage: parent(RealField(100)(1) + float(1))
sage: parent(R100(1) + float(1))
<type 'float'>
sage: cm.explain(QQ, float, operator.add)
Right operand is numeric, will attempt coercion in both directions.
Expand All @@ -521,7 +524,9 @@ cdef class CoercionModel_cache_maps(CoercionModel):
Result lives in Rational Field
Rational Field
sage: cm.explain(ZZ['x'], QQ['x'], operator.div)
sage: ZZx = ZZ['x']
sage: QQx = QQ['x']
sage: cm.explain(ZZx, QQx, operator.div)
Coercion on left operand via
Ring morphism:
From: Univariate Polynomial Ring in x over Integer Ring
Expand All @@ -543,7 +548,7 @@ cdef class CoercionModel_cache_maps(CoercionModel):
Result lives in Rational Field
Rational Field
sage: cm.explain(ZZ['x'], ZZ, operator.div)
sage: cm.explain(ZZx, ZZ, operator.div)
Action discovered.
Right inverse action by Rational Field on Univariate Polynomial Ring in x over Integer Ring
with precomposition on right by Natural morphism:
Expand Down Expand Up @@ -583,7 +588,8 @@ cdef class CoercionModel_cache_maps(CoercionModel):
EXAMPLES::
sage: cm = sage.structure.element.get_coercion_model()
sage: steps, res = cm.analyse(GF(7), ZZ)
sage: GF7 = GF(7)
sage: steps, res = cm.analyse(GF7, ZZ)
sage: print steps
['Coercion on right operand via', Natural morphism:
From: Integer Ring
Expand Down Expand Up @@ -691,23 +697,29 @@ cdef class CoercionModel_cache_maps(CoercionModel):
Rational Field
sage: cm.common_parent(ZZ, QQ, RR)
Real Field with 53 bits of precision
sage: cm.common_parent(ZZ[['T']], QQ['T'], RDF)
sage: ZZT = ZZ[['T']]
sage: QQT = QQ['T']
sage: cm.common_parent(ZZT, QQT, RDF)
Power Series Ring in T over Real Double Field
sage: cm.common_parent(4r, 5r)
<type 'int'>
sage: cm.common_parent(int, float, ZZ)
<type 'float'>
sage: cm.common_parent(*[RealField(prec) for prec in [10,20..100]])
sage: real_fields = [RealField(prec) for prec in [10,20..100]]
sage: cm.common_parent(*real_fields)
Real Field with 10 bits of precision
There are some cases where the ordering does matter, but if a parent
can be found it is always the same::
sage: cm.common_parent(QQ['x,y'], QQ['y,z']) == cm.common_parent(QQ['y,z'], QQ['x,y'])
sage: QQxy = QQ['x,y']
sage: QQyz = QQ['y,z']
sage: cm.common_parent(QQxy, QQyz) == cm.common_parent(QQyz, QQxy)
True
sage: cm.common_parent(QQ['x,y'], QQ['y,z'], QQ['z,t'])
sage: QQzt = QQ['z,t']
sage: cm.common_parent(QQxy, QQyz, QQzt)
Multivariate Polynomial Ring in x, y, z, t over Rational Field
sage: cm.common_parent(QQ['x,y'], QQ['z,t'], QQ['y,z'])
sage: cm.common_parent(QQxy, QQzt, QQyz)
Traceback (most recent call last):
...
TypeError: no common canonical parent for objects with parents: 'Multivariate Polynomial Ring in x, y over Rational Field' and 'Multivariate Polynomial Ring in z, t over Rational Field'
Expand Down Expand Up @@ -742,13 +754,17 @@ cdef class CoercionModel_cache_maps(CoercionModel):
Rational Field
sage: cm.division_parent(QQ)
Rational Field
sage: cm.division_parent(ZZ['x'])
sage: ZZx = ZZ['x']
sage: cm.division_parent(ZZx)
Fraction Field of Univariate Polynomial Ring in x over Integer Ring
sage: cm.division_parent(GF(41))
sage: K = GF(41)
sage: cm.division_parent(K)
Finite Field of size 41
sage: cm.division_parent(Integers(100))
sage: Zmod100 = Integers(100)
sage: cm.division_parent(Zmod100)
Ring of integers modulo 100
sage: cm.division_parent(SymmetricGroup(5))
sage: S5 = SymmetricGroup(5)
sage: cm.division_parent(S5)
Symmetric group of order 5! as a permutation group
"""
try:
Expand Down Expand Up @@ -1082,7 +1098,8 @@ cdef class CoercionModel_cache_maps(CoercionModel):
sage: print g
None
sage: f, g = cm.coercion_maps(ZZ['x'], QQ)
sage: ZZx = ZZ['x']
sage: f, g = cm.coercion_maps(ZZx, QQ)
sage: print f
(map internal to coercion system -- copy before use)
Ring morphism:
Expand All @@ -1094,7 +1111,8 @@ cdef class CoercionModel_cache_maps(CoercionModel):
From: Rational Field
To: Univariate Polynomial Ring in x over Rational Field
sage: cm.coercion_maps(QQ, GF(7)) is None
sage: K = GF(7)
sage: cm.coercion_maps(QQ, K) is None
True
Note that to break symmetry, if there is a coercion map in both
Expand Down Expand Up @@ -1248,7 +1266,8 @@ cdef class CoercionModel_cache_maps(CoercionModel):
Otherwise, try and compute an appropriate cover::
sage: cm.discover_coercion(ZZ['x,y'], RDF)
sage: ZZxy = ZZ['x,y']
sage: cm.discover_coercion(ZZxy, RDF)
((map internal to coercion system -- copy before use)
Call morphism:
From: Multivariate Polynomial Ring in x, y over Integer Ring
Expand Down Expand Up @@ -1307,24 +1326,26 @@ cdef class CoercionModel_cache_maps(CoercionModel):
EXAMPLES::
sage: cm = sage.structure.element.get_coercion_model()
sage: cm.get_action(ZZ['x'], ZZ, operator.mul)
sage: ZZx = ZZ['x']
sage: cm.get_action(ZZx, ZZ, operator.mul)
Right scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Integer Ring
sage: cm.get_action(ZZ['x'], ZZ, operator.imul)
sage: cm.get_action(ZZx, ZZ, operator.imul)
Right scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Integer Ring
sage: cm.get_action(ZZ['x'], QQ, operator.mul)
sage: cm.get_action(ZZx, QQ, operator.mul)
Right scalar multiplication by Rational Field on Univariate Polynomial Ring in x over Integer Ring
sage: cm.get_action(QQ['x'], int, operator.mul)
sage: QQx = QQ['x']
sage: cm.get_action(QQx, int, operator.mul)
Right scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Rational Field
with precomposition on right by Native morphism:
From: Set of Python objects of type 'int'
To: Integer Ring
sage: R.<x> = QQ['x']
sage: A = cm.get_action(R, ZZ, operator.div); A
sage: A = cm.get_action(QQx, ZZ, operator.div); A
Right inverse action by Rational Field on Univariate Polynomial Ring in x over Rational Field
with precomposition on right by Natural morphism:
From: Integer Ring
To: Rational Field
sage: x = QQx.gen()
sage: A(x+10, 5)
1/5*x + 2
Expand Down Expand Up @@ -1449,12 +1470,13 @@ cdef class CoercionModel_cache_maps(CoercionModel):
Bug :trac:`17740`::
sage: cm.discover_action(GF(5)['x'], ZZ, operator.div)
sage: R = GF(5)['x']
sage: cm.discover_action(R, ZZ, operator.div)
Right inverse action by Finite Field of size 5 on Univariate Polynomial Ring in x over Finite Field of size 5
with precomposition on right by Natural morphism:
From: Integer Ring
To: Finite Field of size 5
sage: cm.bin_op(GF(5)['x'].gen(), 7, operator.div).parent()
sage: cm.bin_op(R.gen(), 7, operator.div).parent()
Univariate Polynomial Ring in x over Finite Field of size 5
"""
#print "looking", R, <int><void *>R, op, S, <int><void *>S
Expand Down
58 changes: 39 additions & 19 deletions src/sage/structure/coerce_actions.pyx
Expand Up @@ -65,17 +65,19 @@ cdef class GenericAction(Action):
"""
TESTS::
sage: sage.structure.coerce_actions.ActedUponAction(MatrixSpace(ZZ, 2), Cusps, True)
sage: M = MatrixSpace(ZZ,2)
sage: sage.structure.coerce_actions.ActedUponAction(M, Cusps, True)
Left action by Full MatrixSpace of 2 by 2 dense matrices over Integer Ring on Set P^1(QQ) of all cusps
sage: sage.structure.coerce_actions.GenericAction(QQ, Zmod(6), True)
sage: Z6 = Zmod(6)
sage: sage.structure.coerce_actions.GenericAction(QQ, Z6, True)
Traceback (most recent call last):
...
NotImplementedError: Action not implemented.
This will break if we tried to use it::
sage: sage.structure.coerce_actions.GenericAction(QQ, Zmod(6), True, check=False)
sage: sage.structure.coerce_actions.GenericAction(QQ, Z6, True, check=False)
Left action by Rational Field on Ring of integers modulo 6
"""
Expand All @@ -94,10 +96,14 @@ cdef class GenericAction(Action):
EXAMPLES::
sage: A = sage.structure.coerce_actions.ActedUponAction(MatrixSpace(ZZ, 2), Cusps, True)
sage: M = MatrixSpace(ZZ,2)
sage: A = sage.structure.coerce_actions.ActedUponAction(M, Cusps, True)
sage: A.codomain()
Set P^1(QQ) of all cusps
sage: A = sage.structure.coerce_actions.ActOnAction(SymmetricGroup(3), QQ['x,y,z'], False)
sage: S3 = SymmetricGroup(3)
sage: QQxyz = QQ['x,y,z']
sage: A = sage.structure.coerce_actions.ActOnAction(S3, QQxyz, False)
sage: A.codomain()
Multivariate Polynomial Ring in x, y, z over Rational Field
"""
Expand Down Expand Up @@ -139,7 +145,8 @@ cdef class ActedUponAction(GenericAction):
"""
TESTS::
sage: A = sage.structure.coerce_actions.ActedUponAction(MatrixSpace(ZZ, 2), Cusps, True)
sage: M = MatrixSpace(ZZ,2)
sage: A = sage.structure.coerce_actions.ActedUponAction(M, Cusps, True)
sage: A.act(matrix(ZZ, 2, [1,0,2,-1]), Cusp(1,2))
Infinity
sage: A._call_(matrix(ZZ, 2, [1,0,2,-1]), Cusp(1,2))
Expand All @@ -160,13 +167,15 @@ def detect_element_action(Parent X, Y, bint X_on_left, X_el=None, Y_el=None):
EXAMPLES::
sage: from sage.structure.coerce_actions import detect_element_action
sage: detect_element_action(ZZ['x'], ZZ, False)
sage: ZZx = ZZ['x']
sage: M = MatrixSpace(ZZ,2)
sage: detect_element_action(ZZx, ZZ, False)
Left scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Integer Ring
sage: detect_element_action(ZZ['x'], QQ, True)
sage: detect_element_action(ZZx, QQ, True)
Right scalar multiplication by Rational Field on Univariate Polynomial Ring in x over Integer Ring
sage: detect_element_action(Cusps, MatrixSpace(ZZ, 2), False)
sage: detect_element_action(Cusps, M, False)
Left action by Full MatrixSpace of 2 by 2 dense matrices over Integer Ring on Set P^1(QQ) of all cusps
sage: detect_element_action(Cusps, MatrixSpace(ZZ, 2), True),
sage: detect_element_action(Cusps, M, True),
(None,)
sage: detect_element_action(ZZ, QQ, True),
(None,)
Expand Down Expand Up @@ -266,13 +275,17 @@ cdef class ModuleAction(Action):
EXAMPLES::
sage: from sage.structure.coerce_actions import LeftModuleAction
sage: LeftModuleAction(ZZ, ZZ['x'])
sage: ZZx = ZZ['x']
sage: QQx = QQ['x']
sage: QQy = QQ['y']
sage: ZZxy = ZZ['x']['y']
sage: LeftModuleAction(ZZ, ZZx)
Left scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Integer Ring
sage: LeftModuleAction(ZZ, QQ['x'])
sage: LeftModuleAction(ZZ, QQx)
Left scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Rational Field
sage: LeftModuleAction(QQ, ZZ['x'])
sage: LeftModuleAction(QQ, ZZx)
Left scalar multiplication by Rational Field on Univariate Polynomial Ring in x over Integer Ring
sage: LeftModuleAction(QQ, ZZ['x']['y'])
sage: LeftModuleAction(QQ, ZZxy)
Left scalar multiplication by Rational Field on Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Integer Ring
The following tests against a problem that was relevant during work on
Expand Down Expand Up @@ -351,11 +364,15 @@ cdef class ModuleAction(Action):
EXAMPLES::
sage: from sage.structure.coerce_actions import LeftModuleAction, RightModuleAction
sage: A = LeftModuleAction(ZZ, ZZ['x']); A
sage: ZZx = ZZ['x']
sage: A = LeftModuleAction(ZZ, ZZx); A
Left scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Integer Ring
sage: A._repr_name_()
'scalar multiplication'
sage: RightModuleAction(GF(5), GF(5)[['t']])
sage: GF5 = GF(5)
sage: GF5t = GF5[['t']]
sage: RightModuleAction(GF5, GF5t)
Right scalar multiplication by Finite Field of size 5 on Power Series Ring in t over Finite Field of size 5
"""
return "scalar multiplication"
Expand Down Expand Up @@ -587,7 +604,8 @@ cdef class IntegerMulAction(Action):
EXAMPLES::
sage: from sage.structure.coerce_actions import IntegerMulAction
sage: act = IntegerMulAction(ZZ, GF(101))
sage: GF101 = GF(101)
sage: act = IntegerMulAction(ZZ, GF101)
sage: act(3, 9)
27
sage: act(3^689, 9)
Expand All @@ -606,7 +624,8 @@ cdef class IntegerMulAction(Action):
This used to hang before :trac:`17844`::
sage: E = EllipticCurve(GF(5), [4,0])
sage: GF5 = GF(5)
sage: E = EllipticCurve(GF5, [4,0])
sage: P = E.random_element()
sage: (-2^63)*P
(0 : 1 : 0)
Expand Down Expand Up @@ -644,7 +663,8 @@ cdef class IntegerMulAction(Action):
EXAMPLES::
sage: from sage.structure.coerce_actions import IntegerMulAction
sage: IntegerMulAction(ZZ, GF(5))
sage: GF5 = GF(5)
sage: IntegerMulAction(ZZ, GF5)
Left Integer Multiplication by Integer Ring on Finite Field of size 5
"""
return "Integer Multiplication"
Expand Down

0 comments on commit ced7bfc

Please sign in to comment.