Ilovepdf Merged
Ilovepdf Merged
MASTER OF SCIENCE
IN
MATHEMATICS
SAGE MATH
(PRACTICALS)
DEPARTMENT OF MATHEMATICS
MARCH 2025
DEPARTMENT OF MATHEMATICS
BONAFIDE CERTIFICATE
Date: Examiners
Place: 1. ___________________
(EXTERNAL)
2. ____________________
(INTERNAL)
TABLE OF CONTENTS
S. Page Faculty
Date Title of the Program
No. No. Signature
Program to solve algebraic equation
1 and differentiate the functions 1
Python modules
import module name
Vector space K n = K^n e.g. QQ^3 RR^2 CC^4
module_name.htabi and help(module_name)
Integer sequences: sloane find(list), sloane.htabi Subspace: span(vectors, field )
Partitions: P=Partitions(n) P.count() E.g., span([[1,2,3], [2,3,5]], QQ) Profiling and debugging
Combinations: C=Combinations(list) C.list() Kernel: A.right_kernel() (also left) time command : show timing information
Cartesian product: CartesianProduct(P,C) Sum and intersection: V + W and V.intersection(W) timeit("command"): accurately time command
Tableau: Tableau([[1,2,3],[4,5]]) Basis: V.basis() t = cputime(); cputime(t): elapsed CPU time
Words: W=Words("abc"); W("aabca") Basis matrix: V.basis_matrix() t = walltime(); walltime(t): elapsed wall time
Posets: Poset([[1,2],[4],[3],[4],[]]) Restrict matrix to subspace: A.restrict(V) %pdb: turn on interactive debugger (command line only)
Root systems: RootSystem(["A",3]) Vector in terms of basis: V.coordinates(vector) %prun command: profile command (command line only)
Sagemath 9.2 notebook Server
[I 23:32:22.537 NotebookApp] Using MathJax: nbextensions/mathjax/MathJax.js
[I 23:32:22.868 NotebookApp] Serving notebooks from local directory: /home/sage
[I 23:32:22.868 NotebookApp] Jupyter Notebook 6.1.1 is running at:
[I 23:32:22.868 NotebookApp]
http://localhost:8888/?token=9576c8b8f2d04eda27a34466bbf355a72b0fbf670aadc069
[I 23:32:22.868 NotebookApp] or
http://127.0.0.1:8888/?token=9576c8b8f2d04eda27a34466bbf355a72b0fbf670aadc069
[I 23:32:22.868 NotebookApp] Use Control-C to stop this server and shut down all
kernels (twice to skip confirmation).
[C 23:32:22.881 NotebookApp]
http://localhost:8888/?token=9576c8b8f2d04eda27a34466bbf355a72b0fbf670aadc069
or
http://127.0.0.1:8888/?token=9576c8b8f2d04eda27a34466bbf355a72b0fbf670aadc069
[I 23:32:27.976 NotebookApp] Kernel started: 63d1eb85-dab8-437c-b0c7-a95b7352c4a0,
name: sagemath
[W 23:32:31.500 NotebookApp] 404 GET /nbextensions/mathjax/fonts/HTML-
CSS/TeX/otf/MathJax_Script-Regular.otf (::1) 6.96ms
referer=http://localhost:8888/notebooks/Untitled2.ipynb
In [1]: x = var(‘x’)
solve(x^2 + 3*x + 2, x)
eq1 = p+q==9
eq2 = q*y+p*x==-6
eq3 = q*y^2+p*x^2==24
solve([eq1,eq2,eq3,p==1],p,q,x,y)
In [5]: u = var(‘u’)
diff(sin(u), u)
In [6]: diff(sin(x^2), x, 4)
Page | 1
Output:
Out[5]: cos(u)
Page | 2
In [1]: t = var('t')
x = function('x')(t)
DE = diff(x, t) + x - 1
desolve(DE, [x,t])
Page | 3
Output:
Out[2]: C e e
t t
Page | 4
In [1]: s = var("s")
t = var("t")
f = t^2*exp(t) - sin(t)
f.laplace(t,s)
In [2]: show(f.laplace(t,s))
In [4]: show(lde2.sage())
Page | 5
Output:
Out[2]:
Out[3]:
s^2*laplace(y(t),t,s)-s*y(0)-2*laplace(x(t),t,s)+2*laplace(y(t),t,s
) - D[0](y)(0)
Out[4]:
Page | 6
In [1]: t,x,y = PolynomialRing(RealField(10),3,"txy").gens()
f = y; g = -x - y * t
eulers_method_2x2(f,g, 0, 1, 0, 1/4, 1)
Page | 7
Output:
Out[1]:
Page | 8
In [1]: x=var(‘x’)
f(x)=e^x+x
print(f)
show(f)
print(latex(f))
plot(f)
Page | 9
Output:
Out[2]: x x e
x
Out[4]:
Page | 10
In [1]: x = var(‘x’)
f(x) = 1 – sin(x)^2
print(f)
show(f)
print(latex(f))
plot(f)
Page | 11
Output:
2
Out[2]: x sin x 1
Out[4]:
Page | 12
In [1]: circle((0,0), 1, rgbcolor=(1,1,0))
Page | 13
Output:
Out[1]:
Out[2]:
Page | 14
In [1]: L = [[6*cos(pi*i/100)+5*cos((6/2)*pi*i/100),
6*sin(pi*i/100)-5*sin((6/2)*pi*i/100)]
for i in range(200)]
p = polygon(L, rgbcolor=(1/8,1/4,1/2))
Page | 15
Output:
Out[1]:
Page | 16
In [1]: v = [(tan(x),x) for x in srange
(-2*float(pi),2*float(pi),0.01)]
Page | 17
Output:
Out[1]:
Page | 18
In [1]: x, y, z = var('x, y, z')
implicit_plot3d(x^2 + y^2 + z^2 - 4, (x,-2, 2),
Page | 19
Output:
Out[1]:
Page | 20
In [1]: u, v = var('u,v')
fx = (3+sin(v)+cos(u))*cos(2*v)
fy = (3+sin(v)+cos(u))*sin(2*v)
fz = sin(u)+2*cos(v)
Page | 21
Output:
Out[1]:
Page | 22
In [1]: graph = Graph()
graph.add_vertex(1)
graph.add_vertex(2)
graph.add_vertex(3)
graph.add_edge(1, 2)
graph.add_edge(2, 3)
graph.add_edge(1, 3)
print(graph)
show(graph)
shortest_path = graph.shortest_path(1, 3)
Page | 23
Output:
Page | 24
In [1]: G = graphs.PetersenGraph()
print("diameter =", G.diameter())
Page | 25
Output:
Out[1]:
Page | 26
In [1]: G = graphs.PetersenGraph()
for P in G.shortest_simple_paths(0, 3):
print(P)
Page | 27
Output:
Out[1]:
[0, 4, 3] [0, 1, 2, 7, 5, 8, 3]
[0, 1, 2, 3] [0, 1, 2, 7, 9, 6, 8, 3]
[0, 5, 8, 3] [0, 5, 8, 6, 9, 7, 2, 3]
[0, 5, 7, 2, 3] [0, 1, 6, 9, 7, 5, 8, 3]
[0, 1, 6, 8, 3] [0, 5, 7, 9, 6, 1, 2, 3]
[0, 1, 6, 9, 4, 3] [0, 1, 6, 8, 5, 7, 2, 3]
[0, 5, 7, 9, 4, 3] [0, 5, 7, 2, 1, 6, 8, 3]
[0, 4, 9, 7, 2, 3] [0, 5, 7, 2, 1, 6, 9, 4, 3]
[0, 4, 9, 6, 8, 3] [0, 1, 6, 8, 5, 7, 9, 4, 3]
[0, 4, 9, 6, 1, 2, 3] [0, 4, 9, 6, 8, 5, 7, 2, 3]
[0, 4, 9, 7, 5, 8, 3] [0, 4, 9, 7, 2, 1, 6, 8, 3]
[0, 5, 7, 9, 6, 8, 3] [0, 1, 2, 7, 5, 8, 6, 9, 4, 3]
[0, 1, 6, 9, 7, 2, 3] [0, 5, 8, 6, 1, 2, 7, 9, 4, 3]
[0, 5, 8, 6, 1, 2, 3] [0, 4, 9, 7, 5, 8, 6, 1, 2, 3]
[0, 5, 8, 6, 9, 4, 3] [0, 4, 9, 6, 1, 2, 7, 5, 8, 3]
[0, 1, 2, 7, 9, 4, 3]
Page | 28
In [1]: G = graphs.PetersenGraph()
C = G.coloring()
print(C)
G.plot(partition=C)
Page | 29
Output:
Out[1]:
Page | 30
In [1]: G = graphs.DodecahedralGraph()
H = G.hamiltonian_cycle()
G.plot(edge_colors={'red': H.edges()})
Page | 31
Output:
Out[1]:
Page | 32
In [1]: G = graphs.WheelGraph(15)
P = G.plot()
P.show()
Page | 33
Output:
Out[1]:
Page | 34
In [1]: G = graphs.HeawoodGraph().copy(sparse=True)
for u, v, l in G.edges(sort=True):
G.set_edge_label(u, v, f'({u},{v})')
G.graphplot(edge_labels=True).show()
Page | 35
Output:
Out[1]:
Page | 36
In [1]: glist= [g for g in graphs(5)]
graphs_list.show_graphs(glist)
Page | 37
Output:
Out[1]:
Page | 38
In [1]: from sage.graphs.graph_plot import GraphPlot
G = graphs.HoffmanSingletonGraph()
T = Graph()
T.add_edges(G.min_spanning_tree(starting_vertex=0))
T.show(layout='tree', tree_root=0)
Page | 39
Output:
Out[1]:
Page | 40
In [1]: C = graphs.CubeGraph(8)
P = C.graphplot(vertex_labels=False, vertex_size=0,
graph_border=True)
P.show()
Page | 41
Output:
Out[1]:
Page | 42
In [1]: f(x)=sin(x^2)*exp(-x)+3*x+1
f(2.1)
In [2]: f.diff()
In [3]: plot(f.diff())
Page | 43
Output:
Out[1]:7.18309969277139
Out[3]:
Page | 44
In [1]: var('a,b,c')
s=solve(a*x^2+b*x+c==0,x)
print(s)
In [2]: show(s)
Page | 45
Output:
Out[1]:[
x == -1/2*(b + sqrt(b^2 - 4*a*c))/a,
Out[2]:
Page | 46
In [1]: p = float (input ("Enter the principle: "))
n = int (input ("Enter number of years: "))
Page | 47
Output:
Page | 48
In [1]: def fact(n):
i = 1
s = 1
while i<=n:
s = s*i
i = i+1
return(s)
n = int (input ("Enter a number: "))
for i in range(n):
print ('Factorial of', i,'is', fact(i))
while i<=n:
s = s*i
i = i+1
print ('The factorial of', n, 'numbers is', s)
Page | 49
Output:
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Page | 50
In [1]: t = int (input ('Which table: '))
n = int (input ('How many stages: '))
for i in range(n):
print (t, '*', i+1,'=', (i+1) *t)
Page | 51
Output:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Page | 52
In [1]: n = int (input ('Enter 1 to convert Fahrenheit to Celsius
or Enter 2 to convert Celsius to Fahrenheit: '))
if n==1:
f = float (input ('Enter the value of Fahrenheit: '))
c = (f-32) *5/9
print ('Celsius =', c)
elif n==2:
c = float (input ('Enter the value of Celsius: '))
f = 9/5*c+32
print ('Fahrenheit = ', f)
else:
print ('You entered an invalid option')
Page | 53
Output:
Fahrenheit = 113.0
Page | 54
In [1]: n = int (input ("Enter the number of terms: "))
i = 0
a = []
while i < n:
i = i+1
for j in range(n-1):
for i in range(n):
print(a[i])
Page | 55
Output:
2.0
5.0
8.0
12.0
Page | 56
In [1]: import string
x = str (input ("Enter a string:"))
n = len(x)
i = 0
up = 0
low = 0
while i < n:
if x[i] == x[i]. upper ():
up = up + 1
if x[i] == x[i]. lower ():
low = low + 1
i = i + 1
Page | 57
Output:
Page | 58
In [1]: n = int (input ("Enter an integer:" ))
r = n % 2
if r == 0:
print (n,"is even")
else:
print (n,"is odd")
if n < 0:
print (n,"is negative")
Page | 59
Output:
Enter an integer:1024
45 is even
Enter an integer:-1024
-1024 is even
-1024 is negative
Page | 60
In [1]: import math
a = float (input ("Enter 'a' value: "))
d = b**2 -4*a*c
if d > 0:
r1 = (-b+math.sqrt(d))/(2*a)
r2 = (-b-math.sqrt(d))/(2*a)
r1 = -b/(2*a)
r2 = r1
im1 = math.sqrt(-d)/(2*a)
re2 = re1
im2 = -im1
print ("Root1 = ", re1,"+i (", im1,")")
Page | 61
Output:
Page | 62
In [1]: x = var('x')
y = var('y')
factorized_expression = simplified_expression.factor()
equation = x**2 - 4
solution = solve(equation, x)
print("Original Expression:", expression)
print("Solution:", solution)
Page | 63
Output:
[
x == -2,
x == 2
]
Page | 64
In [1]:
a=Sudoku('5...8..49...5...3..673....115..........2.8.....
.....187....415..3...2...49..5...3')
print(a)
In [2]: print(next(a.solve()))
Page | 65
Output:
Out[1]:
Out[2]:
Page | 66
In [1]: A1 = Matrix([[1,2,3],[3,2,1],[1,1,1]])
print(A1)
print(latex(A1))
show(A1)
In [2]: A1.eigenvalues()
Page | 67
Output:
Out[1]:
Out[3]:
Page | 68
In [1]: A=matrix([[1,2,3],[4,5,6],[7,8,9]])
print(A)
In [2]: I=matrix.identity(3)
print(I)
In [3]: print(A+5*I)
In [4]: det(A)
In [5]: characteristic_polynomial=det(A-x*I)
print(characteristic_polynomial)
In [6]: show(characteristic_polynomial)
In [7]: solve(characteristic_polynomial,x)
In [8]: show(solve(characteristic_polynomial,x))
Page | 69
Output:
Out[1]:
Out[2]:
Out[3]:
Out[4]: 0
Out[6]: x 5 x 9 48 x 1 29 x 3
3 15 3 15
Out[7]: x 33 , x 33 , x 0
2 2 2 2
Page | 70
In [1]: R = IntegerModRing(97)
a = R(2) / R(3)
In [2]: a.rational_reconstruction()
In [3]: b = R(47)
b^20052005
In [4]: b.modulus()
In [5]: b.is_square()
In [6]: gcd(515,2005)
Page | 71
Output:
Out[1]: 33
Out[2]: 2/3
Out[3]: 50
Out[4]: 97
Out[5]: True
Out[6]: 5
Page | 72
In [1]: c = factorial(25); c
In [3]: next_prime(2005)
In [4]: previous_prime(2005)
In [7]: n = 2005
inverse_mod(3,n)
Page | 73
Output:
Out[1]: 15511210043330985984000000
Out[3]: 2011
Out[4]: 2003
Out[5]: 56
Out[6]: True
Out[7]: 1337
Page | 74
In [1]: n = 2005
euler_phi(n)
In [2]: prime_to_m_part(n, 5)
In [3]: n = 2005
for i in range(1000):
n = 3*odd_part(n) + 1
if odd_part(n)==1:
print(i)
break
Page | 75
Output:
Out[1]: 1600
Out[2]: 401
Out[3]: 38
Page | 76