Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Project: Sage Days 82
Views: 259

Number theory in Sage

Sage has the capability to work with objects like the integers Z\mathbb{Z} and the rationals Q\mathbb{Q}, as well as objects like elliptic and hyperelliptic curves.

ZZ
Integer Ring
4/2
2
a = 4/2 type(a)
b = ZZ(a)
b
type(b)
ZZ(1/2)
Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-7.3/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 976, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/structure/parent.pyx", line 1107, in sage.structure.parent.Parent.__call__ (/projects/sage/sage-7.3/src/build/cythonized/sage/structure/parent.c:9905) return mor._call_(x) File "sage/rings/rational.pyx", line 3885, in sage.rings.rational.Q_to_Z._call_ (/projects/sage/sage-7.3/src/build/cythonized/sage/rings/rational.c:31439) raise TypeError("no conversion of this rational to integer") TypeError: no conversion of this rational to integer
QQ
QQ?
File: /projects/sage/sage-7.3/local/lib/python2.7/site-packages/sage/rings/rational_field.py Signature : QQ(self, x=0, *args, **kwds) Docstring : The class "RationalField" represents the field QQ of rational numbers. EXAMPLES: sage: a = long(901824309821093821093812093810928309183091832091) sage: b = QQ(a); b 901824309821093821093812093810928309183091832091 sage: QQ(b) 901824309821093821093812093810928309183091832091 sage: QQ(int(93820984323)) 93820984323 sage: QQ(ZZ(901824309821093821093812093810928309183091832091)) 901824309821093821093812093810928309183091832091 sage: QQ('-930482/9320842317') -930482/9320842317 sage: QQ((-930482, 9320842317)) -930482/9320842317 sage: QQ([9320842317]) 9320842317 sage: QQ(pari(39029384023840928309482842098430284398243982394)) 39029384023840928309482842098430284398243982394 sage: QQ('sage') Traceback (most recent call last): ... TypeError: unable to convert 'sage' to a rational sage: QQ(u'-5/7') -5/7 Conversion from the reals to the rationals is done by default using continued fractions. sage: QQ(RR(3929329/32)) 3929329/32 sage: QQ(-RR(3929329/32)) -3929329/32 sage: QQ(RR(1/7)) - 1/7 0 If you specify an optional second base argument, then the string representation of the float is used. sage: QQ(23.2, 2) 6530219459687219/281474976710656 sage: 6530219459687219.0/281474976710656 23.20000000000000 sage: a = 23.2; a 23.2000000000000 sage: QQ(a, 10) 116/5 Here's a nice example involving elliptic curves: sage: E = EllipticCurve('11a') sage: L = E.lseries().at1(300)[0]; L 0.2538418608559106843377589233... sage: O = E.period_lattice().omega(); O 1.26920930427955 sage: t = L/O; t 0.200000000000000 sage: QQ(RealField(45)(t)) 1/5

Besides integers and rationals, one can use real numbers, complex numbers (where capital I denotes the chosen square root of -1), and p-adic numbers.

7 % 3 ## modular reduction 7 // 3 ## integer division 10/6 ## returns a rational number exp(2.7) (2+I)*(3+I) Qp(5,7)(-1)
Here we create the field of 5-adics Q5\mathbb{Q}_5 with 10 digits of precision:
K = Qp(5,10); K
5-adic Field with capped relative precision 10
We can work with its ring of integers, the 5-adic integers (here with 10 digits of precision):
Zp(5,10)
plot(Zp(5,10), color='red', pointsize=10)
a = K(-3)
a^2
4 + 5 + O(5^10)
a = K(4) a.sqrt()
2 + O(5^10)

Here we create the number field: Q(2)\mathbb{Q}(\sqrt{2}):

R.<x> = QQ['x']; R F.<a> = NumberField(x^2 -2); F
Univariate Polynomial Ring in x over Rational Field Number Field in a with defining polynomial x^2 - 2
a^2
2
(a-2)*(a+3)
a - 4

We can work with power series and Laurent series:

S.<t> = QQ[['t']]; S
g = sum(t^i + O(t^10) for i in range(10)); g
g^2
K.<T> = LaurentSeriesRing(QQ); K
f = sum(i*T^i for i in range(-5,5)); f
f^2

Sage has lots of functionality for elliptic curves.

E = EllipticCurve([1,2,3,4,5])
E
plot(E)
E.conductor()
E = EllipticCurve('37a1'); E
E.rank()
E.torsion_subgroup()
E.torsion_points()
EllipticCurve('37.a1')
EllipticCurve('11a1') #Cremona label
EllipticCurve('11.a1') #LMFDB label
E = EllipticCurve_from_j(1728); E
F = GF(5); F EF = E.change_ring(F) EF EF.rational_points()
E.change_ring(GF(2))
for p in prime_range(3,50): if E.is_good(p): print p, E.Np(p)
E = EllipticCurve('37a1') E E23 = E.change_ring(GF(23)) E23 plot(E23,pointsize=30)
Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field Elliptic Curve defined by y^2 + y = x^3 + 22*x over Finite Field of size 23
w= [] for p in prime_range(10,100): if E.is_good(p): w = w+ [plot(E.change_ring(GF(p)),rgbcolor=(random(),random(),random()),size=15)] graphics_array(w, 4,4).show(axes=False)

Sage includes a couple of useful ways to find out more about functions. One of these is tab completion: if you type part of the name of a function and hit Tab, Sage will attempt to complete the name of the function.

char

If more than one completion is possible, Sage will show you all possible completions.

randint

 Introspection is another useful tool: if one evaluates the name of a function followed by a ?, Python will show you a bit of documentation about that function. 

EllipticCurve?

Another useful way to find functions which might already be in Sage is to search the source code for relevant key words:

search_src('coleman')

Some functionality relevant to computing zeta functions of hyperelliptic curves:

R.<x> = QQ['x'] f = x^5 + x +1 fd = factor(f.discriminant()); fd badp = [p[0] for p in fd]; badp H = HyperellipticCurve(f) for p in prime_range(7,100): if p not in badp: H.change_ring(GF(p)).frobenius_polynomial() #more SageMathCloud functionality: latex, real-time chat, collaboration, show settings for quota #https://github.com/sagemathinc/smc/wiki
R.<x> = Qp(5,10)['x'];R for a in range(1,10): print a, (x^2-a).roots()
a = 1 + 3*5 + 4*5^3 + 2*5^4 + 5^5 + 2*5^6 + 3*5^7 + 5^8 + 3*5^9 + O(5^10) algdep(a,2)