CoCalc Public Filesdemo.sagews
Authors: Peter Bruin, Sander Dahmen
Views : 51
Description: SageMath demo
%typeset_mode True

# Basic commands

1 + 1
$\displaystyle 2$
print("hello world")
hello world

# Programming

if 57.is_prime(): print("57 is a prime number") else: print("57 is composite")
57 is composite
for p in primes(25): print([p, p^2 - 1])
[2, 3] [3, 8] [5, 24] [7, 48] [11, 120] [13, 168] [17, 288] [19, 360] [23, 528]

# Mathematical objects

Integers(), str(Integers())
($\displaystyle \Bold{Z}$, Integer Ring)
ZZ
$\displaystyle \Bold{Z}$
Rationals(), str(Rationals())
($\displaystyle \Bold{Q}$, Rational Field)
ComplexField(200), str(ComplexField(200))
($\displaystyle \Bold{C}$, Complex Field with 200 bits of precision)
Zmod(16)
$\displaystyle \ZZ/16\ZZ$
F = FiniteField(3^4, 'g') F, str(F)
($\displaystyle \Bold{F}_{3^{4}}$, Finite Field in g of size 3^4)
F.polynomial()
$\displaystyle g^{4} + 2 g^{3} + 2$
R = PolynomialRing(Integers(), 'x') R R.gen()
$\displaystyle \Bold{Z}[x]$
$\displaystyle x$
ZZ['x']
$\displaystyle \Bold{Z}[x]$
FunctionField(QQ, 'x')
Rational function field in x over Rational Field

# Modular forms

str(SL2Z)
'Modular Group SL(2,Z)'
SL2Z.gens()
([ 0 -1] [ 1 0], [1 1] [0 1])
M4 = ModularForms(SL2Z, 4) M4
Modular Forms space of dimension 1 for Modular Group SL(2,Z) of weight 4 over Rational Field
M4.dimension()
1
M4.q_expansion_basis()
[ 1 + 240*q + 2160*q^2 + 6720*q^3 + 17520*q^4 + 30240*q^5 + O(q^6) ]
f = M4.q_expansion_basis()[0]/240 f
1/240 + q + 9*q^2 + 28*q^3 + 73*q^4 + 126*q^5 + O(q^6)
sum(d^3 for d in 5.divisors())
126
S12 = CuspForms(SL2Z, 12) S12
Cuspidal subspace of dimension 1 of Modular Forms space of dimension 2 for Modular Group SL(2,Z) of weight 12 over Rational Field
S12.q_expansion_basis(prec=20)
[ q - 24*q^2 + 252*q^3 - 1472*q^4 + 4830*q^5 - 6048*q^6 - 16744*q^7 + 84480*q^8 - 113643*q^9 - 115920*q^10 + 534612*q^11 - 370944*q^12 - 577738*q^13 + 401856*q^14 + 1217160*q^15 + 987136*q^16 - 6905934*q^17 + 2727432*q^18 + 10661420*q^19 + O(q^20) ]
EisensteinForms(SL2Z,16).q_expansion_basis()
[ 1 + 16320/3617*q + 534790080/3617*q^2 + 234174178560/3617*q^3 + 17524001357760/3617*q^4 + 498046875016320/3617*q^5 + O(q^6) ]
f, g = ModularForms(Gamma1(4), 2).basis() f, g type(f) f.parent()
(1 + 24*q^2 + 24*q^4 + O(q^6), q + 4*q^3 + 6*q^5 + O(q^6)) <class 'sage.modular.modform.ambient_g1.ModularFormsAmbient_g1_Q_with_category.element_class'> Modular Forms space of dimension 2 for Congruence Subgroup Gamma1(4) of weight 2 over Rational Field
f.q_expansion(20)
1 + 24*q^2 + 24*q^4 + 96*q^6 + 24*q^8 + 144*q^10 + 96*q^12 + 192*q^14 + 24*q^16 + 312*q^18 + O(q^20)
# Create a power series ring R.<q> = ZZ[[]] R
Power Series Ring in q over Integer Ring
theta = 1 + 2*q + 2*q^4 + 2*q^9 + 2*q^16 + O(q^25)
theta^4
1 + 8*q + 24*q^2 + 32*q^3 + 24*q^4 + 48*q^5 + 96*q^6 + 64*q^7 + 24*q^8 + 104*q^9 + 144*q^10 + 96*q^11 + 96*q^12 + 112*q^13 + 192*q^14 + 192*q^15 + 24*q^16 + 144*q^17 + 312*q^18 + 160*q^19 + 144*q^20 + 256*q^21 + 288*q^22 + 192*q^23 + 96*q^24 + O(q^25)
theta^4 == (f + 8*g).q_expansion(25)
True

# Some pictures of fundamental domains

(warning: not of the same type as in the lectures)

SL2Z.farey_symbol().fundamental_domain()
Gamma0(2).farey_symbol().fundamental_domain()
Gamma(3).farey_symbol().fundamental_domain()
Gamma(3).cusps()
[$\displaystyle 0$, $\displaystyle 1$, $\displaystyle 2$, $\displaystyle \infty$]

# Hecke operators

M = ModularForms(SL2Z, 12) M
Modular Forms space of dimension 2 for Modular Group SL(2,Z) of weight 12 over Rational Field
T2 = M.hecke_operator(2) T2 T2.matrix()
$\displaystyle T_{2}$
$\displaystyle \left(\begin{array}{rr} -24 & 0 \\ 0 & 2049 \end{array}\right)$
M = ModularForms(Gamma1(7), 3) M
Modular Forms space of dimension 7 for Congruence Subgroup Gamma1(7) of weight 3 over Rational Field
d3 = M.diamond_bracket_operator(3) d3 d3.matrix()
$\displaystyle \langle 3 \rangle$
$\displaystyle \left(\begin{array}{rrrrrrr} -1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 27 & 126 & 294 & 770 & 2142 & 3528 \\ 0 & \frac{56}{3} & 85 & 200 & 530 & 1445 & 2408 \\ 0 & \frac{11}{3} & 14 & 22 & 66 & 233 & 392 \\ 0 & -1 & -3 & -3 & -11 & -51 & -87 \\ 0 & -1 & -4 & -7 & -20 & -67 & -112 \\ 0 & -\frac{1}{3} & -2 & -6 & -15 & -34 & -56 \end{array}\right)$
d3.matrix()^6
$\displaystyle \left(\begin{array}{rrrrrrr} 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{array}\right)$
T2 = M.hecke_operator(2) T2.matrix()
$\displaystyle \left(\begin{array}{rrrrrrr} -3 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 285 & 0 & 1176 & 560 & 9072 & 7056 \\ 0 & \frac{556}{3} & 0 & 764 & \frac{982}{3} & 5945 & 4543 \\ 0 & \frac{136}{3} & 1 & 184 & \frac{235}{3} & 1448 & 1162 \\ 0 & -12 & 0 & -48 & -15 & -384 & -300 \\ 0 & -12 & 0 & -47 & -19 & -376 & -301 \\ 0 & -\frac{8}{3} & 0 & -12 & -\frac{20}{3} & -88 & -63 \end{array}\right)$
(T2*d3) (T2*d3-d3*T2).matrix()
$\displaystyle \left(\begin{array}{rrrrrrr} 3 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 23 & 294 & 2142 & 3626 & 8190 & 6552 \\ 0 & \frac{56}{3} & 200 & 1441 & 2485 & 5525 & 4618 \\ 0 & \frac{11}{3} & 22 & 233 & 329 & 902 & 409 \\ 0 & -1 & -3 & -51 & -63 & -195 & -39 \\ 0 & -1 & -7 & -67 & -98 & -260 & -139 \\ 0 & -\frac{1}{3} & -6 & -34 & -67 & -130 & -148 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrrrrrr} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{array}\right)$

# Old and new subspaces

%typeset_mode True # Eisenstein subspace and cuspidal subspace M = ModularForms(Gamma1(14), 4) E = M.eisenstein_submodule() S = CuspForms(Gamma1(14), 4) S == M.cuspidal_submodule() M.dimension() (E.dimension(), S.dimension())
$\displaystyle \mathrm{True}$
$\displaystyle 24$
($\displaystyle 12$, $\displaystyle 12$)
# Old and new subspaces Sold = S.old_submodule() Snew = S.new_submodule() (Sold.dimension(), Snew.dimension())
($\displaystyle 6$, $\displaystyle 6$)
# Check consistency with dimensions of spaces of lower level CuspForms(Gamma1(2), 4).dimension() CuspForms(Gamma1(7), 4).dimension()
$\displaystyle 0$
$\displaystyle 3$
S.old_submodule(2)
Modular Forms subspace of dimension 6 of Modular Forms space of dimension 24 for Congruence Subgroup Gamma1(14) of weight 4 over Rational Field
# An example of a non-diagonalisable Hecke operator. This shows that the # decomposition of the new subspace into simultaneous eigenspaces for the # T_m with m coprime to the level does not extend directly to all the T_m. S = CuspForms(Gamma0(16), 4) T2 = S.hecke_matrix(2) T2 T2.jordan_form()
$\displaystyle \left(\begin{array}{rrr} 0 & 0 & 0 \\ 1 & 0 & -4 \\ 0 & 0 & 0 \end{array}\right)$
$\displaystyle \left(\begin{array}{rr|r} 0 & 1 & 0 \\ 0 & 0 & 0 \\ \hline 0 & 0 & 0 \end{array}\right)$

# Newforms

# We can compute the set of newforms (primitive forms) of a given level and weight. S = CuspForms(Gamma1(15), 2) NF = S.newforms() NF Newforms(Gamma1(15), 2) == NF
[$\displaystyle q - q^{2} - q^{3} - q^{4} + q^{5} + O(q^{6})$]
$\displaystyle \mathrm{True}$
# An example with multiple newforms Newforms(Gamma0(26), 2)
[$\displaystyle q - q^{2} + q^{3} + q^{4} - 3q^{5} + O(q^{6})$, $\displaystyle q + q^{2} - 3q^{3} + q^{4} - q^{5} + O(q^{6})$]
# We have to be careful when the newforms don't have rational coefficients Newforms(Gamma1(26), 2)
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1234, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/modular/modform/constructor.py", line 467, in Newforms return CuspForms(group, weight, base_ring).newforms(names) File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/modular/modform/space.py", line 1642, in newforms raise ValueError("Please specify a name to be used when generating names for generators of Hecke eigenvalue fields corresponding to the newforms.") ValueError: Please specify a name to be used when generating names for generators of Hecke eigenvalue fields corresponding to the newforms.
# In this case we have to specify a "names" parameter. NF = Newforms(Gamma1(26), 2, names='a') NF
[$\displaystyle q - q^{2} + q^{3} + q^{4} - 3q^{5} + O(q^{6})$, $\displaystyle q + q^{2} - 3q^{3} + q^{4} - q^{5} + O(q^{6})$, $\displaystyle q + a_{2}q^{2} + \left(-a_{2} - 1\right)q^{4} - q^{5} + O(q^{6})$, $\displaystyle q + a_{3}q^{2} - q^{3} - q^{4} - 3 a_{3}q^{5} + O(q^{6})$]
# Every newform has a character [f.character() for f in NF]
[$\displaystyle \hbox{Dirichlet character modulo } 26 \hbox{ of conductor } 1 \hbox{ mapping } 15 \mapsto 1$, $\displaystyle \hbox{Dirichlet character modulo } 26 \hbox{ of conductor } 1 \hbox{ mapping } 15 \mapsto 1$, $\displaystyle \hbox{Dirichlet character modulo } 26 \hbox{ of conductor } 13 \hbox{ mapping } 15 \mapsto -a_{2} - 1$, $\displaystyle \hbox{Dirichlet character modulo } 26 \hbox{ of conductor } 13 \hbox{ mapping } 15 \mapsto -1$]
# Compare our list of newforms with the dimension of the new subspace: S = CuspForms(Gamma1(26), 2) Sold = S.old_submodule() Snew = S.new_submodule() (Sold.dimension(), Snew.dimension())
($\displaystyle 4$, $\displaystyle 6$)
# Sage only gives 4 newforms, but the new subspace has dimension 6. # This is explained by the fact that two of the forms have larger coefficient fields. # The following computation shows that the coefficient fields of the last two forms # are Q(\sqrt{-3}) and Q(\sqrt{-1}), respectively. [f.base_ring() for f in NF]
[$\displaystyle \Bold{Q}$, $\displaystyle \Bold{Q}$, $\displaystyle \Bold{Q}[a_{2}]/(a_{2}^{2} + a_{2} + 1)$, $\displaystyle \Bold{Q}[a_{3}]/(a_{3}^{2} + 1)$]

# $L$-functions

# Here is a newform of which we are going to compute the L-series. f = Newforms(Gamma1(14), 2)[0]; f
$\displaystyle q - q^{2} - 2q^{3} + q^{4} + O(q^{6})$
Lf = f.lseries() Lf
L-series associated to the cusp form q - q^2 - 2*q^3 + q^4 + O(q^6)
# We can evaluate L-series both inside and outside the # right half-plane where the Dirichlet series converges. Lf(3) Lf(3+2*I) Lf(-2-I)
$\displaystyle 0.826125962101783$
$\displaystyle 0.995825161298581 + 0.180645100106889i$
$\displaystyle 1.25737321267029 - 0.432187040382323i$
# The L-function is holomorphic. Lf.poles
[]
# Like the Riemann zeta function, it has some 'trivial' zeroes. [Lf(s) for s in [-4..-1]]
[$\displaystyle 0.000000000000000$, $\displaystyle 0.000000000000000$, $\displaystyle 0.000000000000000$, $\displaystyle 0.000000000000000$]
# Sign of the functional equation Lf.eps
$\displaystyle 1.00000000000000$
Lf.conductor Lf.weight
$\displaystyle 14$
$\displaystyle 2$
# Completed L-function attached to L(s) def Lambda(L, s): return gamma(s)*L.conductor^(s/2)/(2*pi.n())^s * L(s)
# Check numerically that the completed L-function # satisfies the expected functional equation. s = 1.43250982 + .435873*I Lambda(Lf, s) Lambda(Lf, 2 - s)
$\displaystyle 0.196288571460192 + 0.0108503534364708i$
$\displaystyle 0.196288571460192 + 0.0108503534364707i$
# There is also a quicker (but more obscure) way: Lf.check_functional_equation() # answer should be a small number
$\displaystyle -1.68051336735253 \times 10^{-18}$
# An example with non-rational coefficients g = Newforms(Gamma1(16), 2, names='a')[0] g g.base_ring()
$\displaystyle q + a_{0}q^{2} + \left(-a_{0} - 2\right)q^{3} + \left(-2 a_{0} - 2\right)q^{4} + a_{0}q^{5} + O(q^{6})$
$\displaystyle \Bold{Q}[a_{0}]/(a_{0}^{2} + 2 a_{0} + 2)$
Lg = g.lseries() Lgstar = g.lseries(embedding=1) Lg(2) Lgstar(2) (Lg.eps, Lg.eps.abs()) (Lgstar.eps, Lgstar.eps.abs()) Lg.conductor
$\displaystyle 0.668961812718835 - 0.0905917403031900i$
$\displaystyle 0.668961812718835 + 0.0905917403031900i$
($\displaystyle 0.923879532511287 - 0.382683432365090i$, $\displaystyle 1.00000000000000$)
($\displaystyle 0.923879532511287 + 0.382683432365090i$, $\displaystyle 1.00000000000000$)
$\displaystyle 16$
s = 0.520934 - 2.230498*I Lambda(Lg, 2 - s) Lg.eps * Lambda(Lgstar, s) Lg.check_functional_equation()
$\displaystyle 0.0875461953458519 + 0.0205215759552274i$
$\displaystyle 0.0875461953458519 + 0.0205215759552274i$
$\displaystyle -1.28423747330908 \times 10^{-16} + 6.45303580536216 \times 10^{-17}i$