Multiplying Apples and Oranges: Transparent Arithmetic with different Data Types in SageMath
Or: An Introduction to SageMath's Coercion Framework
Daniel Krenn, AAU Klagenfurt, Austria
@Purdue University, 5.11.2015
Multivariate Power Series Ring in a, b, c over Integer Ring
a
Multivariate Power Series Ring in a, b, c over Integer Ring
Integer Ring
Symbolic Ring
1 + a + a^2 + a^3 + a^4 + a^5 + a^6 + a^7 + a^8 + a^9 + a^10 + a^11 + O(a, b, c)^12
What is all this about?
4434/403
Integer Ring
Rational Field
What is the Goal of Coercions
from SageMath's documentation:
A short Look behind the Scenes
Natural morphism:
From: Integer Ring
To: Rational Field
An Exercise
Univariate Polynomial Ring in t over Integer Ring
Univariate Polynomial Ring in t over Integer Ring
Univariate Polynomial Ring in t over Rational Field
Univariate Polynomial Ring in t over Rational Field
Fraction Field of Univariate Polynomial Ring in t over Integer Ring
Symbolic Ring
Symbolic Ring
-1
Number Field in i with defining polynomial x^2 + 1
-1
Univariate Polynomial Ring in t over Number Field in i with defining polynomial x^2 + 1
Coercions vs. Conversions
2
Error in lines 1-1
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/smc_sagews/sage_server.py", line 905, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "sage/structure/parent.pyx", line 1098, in sage.structure.parent.Parent.__call__ (/projects/53b77207-8614-4086-a032-432af4b4cdbd/sage-dev-images/sage-6.10.beta4/src/build/cythonized/sage/structure/parent.c:9820)
return mor._call_(x)
File "sage/rings/rational.pyx", line 3745, in sage.rings.rational.Q_to_Z._call_ (/projects/53b77207-8614-4086-a032-432af4b4cdbd/sage-dev-images/sage-6.10.beta4/src/build/cythonized/sage/rings/rational.c:30593)
raise TypeError, "no conversion of this rational to integer"
TypeError: no conversion of this rational to integer
Another Example: Real Numbers
Real Field with 53 bits of precision
3.14159265358979
Symbolic Ring
Real Field with 2 bits of precision
3.0
8.0
16.
(6.0, Real Field with 2 bits of precision)
Comparisions
True
A more Challenging Example
Univariate Polynomial Ring in t over Integer Ring
t^2 + 11*t + 1/403
(True, True)
Looking behind the Scene: Now really...
<sage.structure.coerce.CoercionModel_cache_maps object at 0x7f0c260f6738>
Univariate Polynomial Ring in t over Rational Field
Action discovered.
Right scalar multiplication by Rational Field on Univariate Polynomial Ring in t over Integer Ring
Result lives in Univariate Polynomial Ring in t over Rational Field
Univariate Polynomial Ring in t over Rational Field
Discovering new Parents
Multivariate Polynomial Ring in m, n over Integer Ring
m^2*n + 42*n^2
Multivariate Polynomial Ring in n, o over Rational Field
1/3*n^2 + o
(m^2*n + 127/3*n^2 + o, Multivariate Polynomial Ring in m, n, o over Rational Field)
Coercion on left operand via
Conversion map:
From: Multivariate Polynomial Ring in m, n over Integer Ring
To: Multivariate Polynomial Ring in m, n, o over Rational Field
Coercion on right operand via
Conversion map:
From: Multivariate Polynomial Ring in n, o over Rational Field
To: Multivariate Polynomial Ring in m, n, o over Rational Field
Arithmetic performed after coercions.
Result lives in Multivariate Polynomial Ring in m, n, o over Rational Field
Multivariate Polynomial Ring in m, n, o over Rational Field
Constructions for Discovering new Parents
(MPoly[m,n], Integer Ring)
(MPoly[n,o], Rational Field)
(FractionField, Integer Ring)
(MPoly[m,n,o], Rational Field)
Properties of Coercions / Axioms
A coercion is defined on all elements of a parent.
Coercions are structure preserving.
There is at most one coercion from one parent to another.
Coercions can be composed.
The identity is a coercion