Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Sage Days 74
Views: 152
Kernel: SageMath 6.10

Sage Days 74

Introduction

Sage comes with a lot of rings and fields that are at the basis of mathematical structures:

(ZZ, QQ, RDF, RR, CDF , CC, AA, GF(5^2), Zp(5), Qp(5), CyclotomicField(), PolynomialRing(QQ, 'x'), PowerSeriesRing(QQ, 'x'), SR)
(Integer Ring, Rational Field, Real Double Field, Real Field with 53 bits of precision, Complex Double Field, Complex Field with 53 bits of precision, Algebraic Real Field, Finite Field in z2 of size 5^2, 5-adic Ring with capped relative precision 20, 5-adic Field with capped relative precision 20, Universal Cyclotomic Field, Univariate Polynomial Ring in x over Rational Field, Power Series Ring in x over Rational Field, Symbolic Ring)

We say that the ring is the parent of the ring elements. In interactive use you use the parent to construct elements:

ZZ(1), ZZ.zero()
(1, 0)
1, 1.parent()
(1, Integer Ring)

How do these work together?

First, explicit conversion:

CDF(1)
1.0
CDF._element_constructor_??

The preparser uses explicit conversion for Python -> Sage types:

preparse('1, 2, 3.0')
"Integer(1), Integer(2), RealNumber('3.0')"

Ok for computer programming, but interactively we want to be able to write

1 + 1/2 # and not QQ(1) + 1/2
3/2
a = ZZ(1) b = QQ(1/2) (a+b).parent()
Rational Field

Coercion

This is called coercion in Sage. It only depends on the operation and the parents of the operands:

cm = get_coercion_model() cm.explain(ZZ, QQ, operator.add)
Coercion on left operand via Natural morphism: From: Integer Ring To: Rational Field Arithmetic performed after coercions. Result lives in Rational Field
Rational Field
QQ.coerce_map_from(ZZ)
Natural morphism: From: Integer Ring To: Rational Field

Even though coercion doesn't depend on the element, they can be used as arguments instead of their parent:

cm.explain(1, oo, operator.add)
Coercion on left operand via Conversion map: From: Integer Ring To: The Infinity Ring Arithmetic performed after coercions. Result lives in The Infinity Ring
The Infinity Ring
InfinityRing(1), InfinityRing(oo)
(A positive finite number, +Infinity)

Further topics

  • Attach

  • Git log

  • Development workflow