Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 223

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

R.<a,b,c> = ZZ[[]] R
Multivariate Power Series Ring in a, b, c over Integer Ring
a
a
a.parent()
Multivariate Power Series Ring in a, b, c over Integer Ring
1.parent()
Integer Ring
x.parent()
Symbolic Ring
1 / (1-a)
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?

11 + 5 / 2015
4434/403
11.parent()
Integer Ring
(5 / 2015).parent()
Rational Field

What is the Goal of Coercions

from SageMath's documentation:

"The primary goal [...] is to be able to transparently do arithmetic, comparisons, etc. between elements of distinct sets."

A short Look behind the Scenes

QQ.coerce_map_from(ZZ) # seeing what's going on
Natural morphism: From: Integer Ring To: Rational Field

An Exercise

P.<t> = ZZ[] P
Univariate Polynomial Ring in t over Integer Ring
z0 = t z0.parent()
Univariate Polynomial Ring in t over Integer Ring
z1 = t / 2 z1.parent()
Univariate Polynomial Ring in t over Rational Field
z2 = t / 1 z2.parent()
Univariate Polynomial Ring in t over Rational Field
z3 = 1 / t z3.parent()
Fraction Field of Univariate Polynomial Ring in t over Integer Ring
(t / (1+I)).parent()
Symbolic Ring
I.parent()
Symbolic Ring
I^2
-1
K = QQ.extension(x^2+1, 'i'); i = K.gen() K
Number Field in i with defining polynomial x^2 + 1
i^2
-1
(t/(1+i)).parent()
Univariate Polynomial Ring in t over Number Field in i with defining polynomial x^2 + 1

Coercions vs. Conversions

ZZ(2/1)
2
ZZ(3/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

RR
Real Field with 53 bits of precision
a = RR(pi) # a conversion occurs here a
3.14159265358979
pi.parent()
Symbolic Ring
R2 = RealField(2) R2
Real Field with 2 bits of precision
b = R2(3) b
3.0
R2(7)
8.0
R2(15)
16.
c = a + b c, c.parent()
(6.0, Real Field with 2 bits of precision)

Comparisions

a == b
True

A more Challenging Example

P # above: P.<t> = ZZ[]
Univariate Polynomial Ring in t over Integer Ring
d = (t^2 + 11*t) + 5/2015 d
t^2 + 11*t + 1/403
Q = d.parent()
P.coerce_map_from(QQ) is None, QQ.coerce_map_from(P) is None
(True, True)

Looking behind the Scene: Now really...

from sage.structure.element import get_coercion_model cm = get_coercion_model() cm # What a strange object!
<sage.structure.coerce.CoercionModel_cache_maps object at 0x7f0c260f6738>
cm.common_parent(P, QQ)
Univariate Polynomial Ring in t over Rational Field
cm.explain(P, QQ)
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

M.<m, n> = ZZ[] M
Multivariate Polynomial Ring in m, n over Integer Ring
alpha = m^2 * n + 42 * n^2 alpha
m^2*n + 42*n^2
N.<n, o> = QQ[] N
Multivariate Polynomial Ring in n, o over Rational Field
beta = n^2 / 3 + o beta
1/3*n^2 + o
gamma = alpha + beta gamma, gamma.parent()
(m^2*n + 127/3*n^2 + o, Multivariate Polynomial Ring in m, n, o over Rational Field)
cm.explain(M, N)
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

M.construction()
(MPoly[m,n], Integer Ring)
N.construction()
(MPoly[n,o], Rational Field)
QQ.construction()
(FractionField, Integer Ring)
cm.common_parent(M, N).construction()
(MPoly[m,n,o], Rational Field)

Properties of Coercions / Axioms

  1. A coercion is defined on all elements of a parent.

  2. Coercions are structure preserving.

  3. There is at most one coercion from one parent to another.

  4. Coercions can be composed.

  5. The identity is a coercion