0% found this document useful (0 votes)
16 views84 pages

Ilovepdf Merged

This document is a practical record for the Master of Science in Mathematics program at Presidency College, detailing various Sage Math practical exercises conducted during the academic year 2024-2025. It includes a list of programs for solving mathematical problems, plotting graphs, and performing numerical computations. The document also contains a bonafide certificate and a table of contents outlining the practical exercises and their respective pages.

Uploaded by

balajibhuvana004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views84 pages

Ilovepdf Merged

This document is a practical record for the Master of Science in Mathematics program at Presidency College, detailing various Sage Math practical exercises conducted during the academic year 2024-2025. It includes a list of programs for solving mathematical problems, plotting graphs, and performing numerical computations. The document also contains a bonafide certificate and a table of contents outlining the practical exercises and their respective pages.

Uploaded by

balajibhuvana004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

PRESIDENCY COLLEGE (AUTONOMOUS)

CHENNAI – 600 005

MASTER OF SCIENCE
IN

MATHEMATICS

SAGE MATH
(PRACTICALS)

DEPARTMENT OF MATHEMATICS
MARCH 2025
DEPARTMENT OF MATHEMATICS

BONAFIDE CERTIFICATE

Certified that this is a bonafide record of the work done by

____________________________________________of I M.Sc. Mathematics

Reg. No. ______________________ at Presidency College (Autonomous),

Chennai-600 005 during the year 2024-2025.

HEAD OF THE DEPARTMENT FACULTY IN-CHARGE

Submitted for the SAGE MATH Practical Examination Held


on _____________________
at Presidency College (Autonomous), Chennai – 600 005.

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

Program to differentiate the given


2 functions 3

Program to find the laplace transform of


3 the functions 5

Program to find Numerical solution of


4 Differential Equations Using Euler’s 7
Method
Program to plot the 2D graph of the
5 function and latex expression of 9
f(x) = e^x+x
Program to plot the 2D graph of the
6 function and latex expression of 11
f(x) = 1-sin(x)^2
Program to plot the 2D graph of the
7 13
circle with colouring
Program to plot the 2D graph of the
8 15
polygon with its name
Program to plot the 2D graph of the
9 17
tangent function

10 Program to plot the graph of the sphere 19

Program to generate a 3D parametric


11 21
plot
Program to add vertices , edges and find
12 23
the shortest path

13 Program to plot the 2D Peterson Graph 25

Program to find the shortest route from


14 27
vector 0 to vector 3 in Peterson Graph
Program to find the vertex colouring of
15 29
the Peterson Graph
Program to find the Hamiltonian cycle
16 31
for Dodecahedral Graph

17 Program to plot the 2D Wheel graph 33


18 Program to plot the 2D Heawood graph 35

Program to plot all possible graphs


19 37
with vertices 5
Program to plot the 2D Hoffman
20 39
singleton graph
Program to display an 8-dimensional
21 41
cube graph
Program to plot the 2D graph of
22 43
differential functions
Program to solve the quadratic
23 45
equations

24 Program to find the simple interest 47

Program to find the factorial of a given


25 number using conditional statement 49
and inbuilt functions

26 Program to print the tables 51

Program to convert temperature from


27 Fahrenheit to Celsius and from Celsius 53
to Fahrenheit
Program to sort out the list of given
28 55
numbers
Program to determine the number of
29 57
characters in Upper case and lower case
Program to find whether the given
30 59
number is odd or even
Program to find the roots of the
31 61
quadratic equation
Program to simplify and factorize the
32 63
given expression

33 Program to solve sudoku 65

Program to display the matrix and to


34 find eigen value, square of the matrix 67
and transpose of the matrix
Program to find the characteristic
35 polynomial and determinant of the 69
matrix
Program to solve the elementary
36 71
functions part(1)
Program to solve the elementary
37 73
functions part (2)
Program to solve the elementary
38 75
functions part (3)
Sage Quick Reference
Arithmetic 2D graphics
√ -6 -4 -2 2 4 6
I M.Sc. Mathematics - 2024-25 ab = a*b ab = a/b ab = a^b x = sqrt(x) -0.25
-0.5
√ -0.75
-1
n
x = x^(1/n) |x| = abs(x) logb (x) = log(x,b)
23PP2SP01 Xn
Sums: f (i) = sum(f(i) for i in (k..n))
line([(x1 ,y1 ),. . .,(xn ,yn )],options)
Programming with SageMath - Practical i=k
n
Y polygon([(x1 ,y1 ),. . .,(xn ,yn )],options)
Products: f (i) = prod(f(i) for i in (k..n)) circle((x,y),r,options)
i=k
text("txt",(x,y),options)
Evaluate cell: hshift-enteri
Constants and functions options as in plot.options, e.g. thickness=pixel ,
Evaluate cell creating new cell: halt-enteri
Constants: π = pi e = e i = i ∞ = oo rgbcolor=(r,g,b), hue=h where 0 ≤ r, b, g, h ≤ 1
Split cell: hcontrol-;i
φ = golden_ratio γ = euler_gamma show(graphic, options)
Join cells: hcontrol-backspacei
Approximate: pi.n(digits=18) = 3.14159265358979324 use figsize=[w,h] to adjust size
Insert math cell: click blue line between cells
Functions: sin cos tan sec csc cot sinh cosh tanh use aspect_ratio=number to adjust aspect ratio
Insert text/HTML cell: shift-click blue line between cells sech csch coth log ln exp ...
plot(f(x),(x, xmin , xmax ),options)
Delete cell: delete content then backspace Python function: def f(x): return x^2
parametric plot((f(t),g(t)),(t, tmin , tmax ),options)
Command line polar plot(f(t),(t, tmin , tmax ),options)
Interactive functions
comhtabi complete command combine: circle((1,1),1)+line([(0,0),(2,2)])
Put @interact before function (vars determine controls)
*bar *? list command names containing “bar” animate(list of graphics, options).show(delay=20)
@interact
command ?htabi shows documentation def f(n=[0..4], s=(1..5), c=Color("red")):
3D graphics
command ??htabi shows source code var("x");show(plot(sin(n+x^s),-pi,pi,color=c))
a.htabi shows methods for object a (more: dir(a))
a._htabi shows hidden methods for object a Symbolic expressions
search_doc("string or regexp") fulltext search of docs Define new symbolic variables: var("t u v y z")
search_src("string or regexp") search source code Symbolic function: e.g. f (x) = x2 f(x)=x^2
_ is previous output Relations: f==g f<=g f>=g f<g f>g
Solve f = g: solve(f(x)==g(x), x)
Numbers line3d([(x1 ,y1 ,z1 ),. . .,(xn ,yn ,zn )],options)
solve([f(x,y)==0, g(x,y)==0], x,y)
Integers: Z = ZZ e.g. -2 -1 0 1 10^100 sphere((x,y,z),r,options)
factor(...) expand(...) (...).simplify_...
Rationals: Q = QQ e.g. 1/2 1/1000 314/100 -2/1 text3d("txt", (x,y,z), options)
find_root(f(x), a, b) find x ∈ [a, b] s.t. f (x) ≈ 0
Reals: R ≈ RR e.g. .5 0.001 3.14 1.23e10000 tetrahedron((x,y,z),size,options)
Complex: C ≈ CC e.g. CC(1,1) CC(2.5,-3) Calculus cube((x,y,z),size,options)
Double precision: RDF and CDF e.g. CDF(2.1,3) lim f (x) = limit(f(x), x=a) octahedron((x,y,z),size,options)
x→a
Mod n: Z/nZ = Zmod e.g. Mod(2,3) Zmod(3)(2) d
dodecahedron((x,y,z),size,options)
dx (f (x)) = diff(f(x),x)
icosahedron((x,y,z),size,options)
Finite fields: Fq = GF e.g. GF(3)(2) GF(9,"a").0 ∂
Polynomials: R[x, y] e.g. S.<x,y>=QQ[] x+2*y^3 ∂x (f (x, y))= diff(f(x,y),x) plot3d(f(x, y),(x, xb , xe ), (y, yb , ye ),options)
diff = differentiate = derivative parametric plot3d((f,g,h),(t, tb , te ),options)
Series: R[[t]] e.g. S.<t>=QQ[[]] 1/2+2*t+O(t^2) R
f (x)dx = integral(f(x),x) parametric plot3d((f(u, v),g(u, v),h(u, v)),
p-adic numbers: Zp ≈Zp, Qp ≈Qp e.g. 2+3*5+O(5^2) Rb
Algebraic closure: Q = QQbar e.g. QQbar(2^(1/5)) a
f (x)dx = integral(f(x),x,a,b) (u, ub , ue ),(v, vb , ve ),options)
Rb
Interval arithmetic: RIF e.g. sage: RIF((1,1.00001)) a
f (x)dx ≈ numerical_integral(f(x),a,b) options: aspect ratio=[1, 1, 1], color="red"
Number field: R.<x>=QQ[];K.<a>=NumberField(x^3+x+1) Taylor polynomial, deg n about a: taylor(f(x),x,a,n) opacity=0.5, figsize=6, viewer="tachyon"
Crystals: CrystalOfTableaux(["A",3], shape=[3,2])
Discrete math Lattice Polytopes: A=random_matrix(ZZ,3,6,x=7) Numerical mathematics
bxc = floor(x) dxe = ceil(x) L=LatticePolytope(A) L.npoints() L.plot3d() Packages: import numpy, scipy, cvxopt
Remainder of n divided by k = n%k k|n iff n%k==0 Minimization: var("x y z")
x Matrix algebra

n! = factorial(n) m = binomial(x,m)   minimize(x^2+x*y^3+(1-z)^2-1, [1,1,1])
φ(n) = euler phi(n) 1
= vector([1,2])
Strings: e.g. s = "Hello" = "He"+’llo’ 2 Number theory
 
1 2
s[0]="H" s[-1]="o" s[1:3]="el" s[3:]="lo" = matrix(QQ,[[1,2],[3,4]], sparse=False) Primes: prime_range(n,m), is_prime, next_prime
3 4 Factor: factor(n), qsieve(n), ecm.factor(n)
Lists: e.g. [1,"Hello",x] = []+[1,"Hello"]+[x]  
1 2 3 Kronecker symbol: ab = kronecker symbol(a,b)

Tuples: e.g. (1,"Hello",x) (immutable) = matrix(QQ,2,3,[1,2,3, 4,5,6])
4 5 6 Continued fractions: continued_fraction(x)
Sets: e.g. {1, 2, 1, a} = Set([1,2,1,"a"]) (= {1, 2, a})
1 2 Bernoulli numbers: bernoulli(n), bernoulli mod p(p)
List comprehension ≈ set builder notation, e.g. = det(matrix(QQ,[[1,2],[3,4]]))
3 4
{f (x) : x ∈ X, x > 0} = Set([f(x) for x in X if x>0]) Elliptic curves: EllipticCurve([a1 , a2 , a3 , a4 , a6 ])
Av = A*v A−1 = A^-1 At = A.transpose() Dirichlet characters: DirichletGroup(N)
Graph theory Solve Ax = v: A\v or A.solve_right(v) Modular forms: ModularForms(level, weight)
Solve xA = v: A.solve_left(v) Modular symbols: ModularSymbols(level, weight, sign)
Reduced row echelon form: A.echelon_form() Brandt modules: BrandtModule(level, weight)
Rank and nullity: A.rank() A.nullity() Modular abelian varieties: J0(N), J1(N)
Hessenberg form: A.hessenberg_form()
Characteristic polynomial: A.charpoly() Group theory
Graph: G = Graph({0:[1,2,3], 2:[4]}) Eigenvalues: A.eigenvalues() G = PermutationGroup([[(1,2,3),(4,5)],[(3,4)]])
Directed Graph: DiGraph(dictionary) Eigenvectors: A.eigenvectors_right() (also left) SymmetricGroup(n), AlternatingGroup(n)
Graph families: graphs.htabi Gram-Schmidt: A.gram_schmidt() Abelian groups: AbelianGroup([3,15])
Invariants: G.chromatic polynomial(), G.is planar() Visualize: A.plot() Matrix groups: GL, SL, Sp, SU, GU, SO, GO
Paths: G.shortest path() LLL reduction: matrix(ZZ,...).LLL() Functions: G.sylow subgroup(p), G.character table(),
Visualize: G.plot(), G.plot3d() Hermite form: matrix(ZZ,...).hermite_form() G.normal subgroups(), G.cayley graph()
Automorphisms: G.automorphism group(),
G1.is isomorphic(G2), G1.is subgraph(G2) Linear algebra Noncommutative rings
Quaternions: Q.<i,j,k> = QuaternionAlgebra(a,b)
Combinatorics Free algebra: R.<a,b,c> = FreeAlgebra(QQ, 3)

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]

To access the notebook, open this file in a browser:


file:///home/sage/.local/share/jupyter/runtime/nbserver-1900-open.html
Or copy and paste one of these URLs:

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)

In [2]: x, b, c = var(‘x b c’)

solve([x^2 + b*x + c == 0],x)

In [3]: x, y = var(‘x, y’)


solve([x+y==6, x-y==4], x, y)

In [4]: var(‘x y p q’)

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[1]: [x == -2, x == -1]

Out[2]: [x == -1/2*b – ½*sqrt(b^2 – 4*c), x == -1/2*b + ½*sqrt(b^2


– 4*c)]

Out[3]: [[x == 5, y == 1]]

Out[4]: [[p == 1, q == 8, x == -4/3*sqrt(10) – 2/3, y ==


1/6*sqrt(10) – 2/3], [p == 1, q == 8, x == 4/3*sqrt(10) – 2/3, y
==-1/6*sqrt(10) – 2/3]]

Out[5]: cos(u)

Out[6]: 16*x^4*sin(x^2) – 48*x^2*cos(x^2) – 12*sin(x^2)

Page | 2
In [1]: t = var('t')
x = function('x')(t)

DE = diff(x, t) + x - 1
desolve(DE, [x,t])

In [2]: show(desolve(DE, [x,t]))

Page | 3
Output:

Out[1]: (_C + e^t)*e^(-t)

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 [3]: de2 = maxima("diff(y(t),t, 2) + 2*y(t) - 2*x(t)")

lde2 = de2.laplace("t","s"); lde2.sage()

In [4]: show(lde2.sage())

Page | 5
Output:

Out[1]: -1/(s^2 + 1) + 2/(s - 1)^3

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[1]: x |--> x + e^x

Out[2]: x  x  e
x

Out[3]: x \ {\mapsto}\ 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:

Out[1]: x |--> -sin(x)^2 + 1

2
Out[2]: x   sin  x   1

Out[3]: x \ {\mapsto}\ -\sin\left(x\right)^{2} + 1

Out[4]:

Page | 12
In [1]: circle((0,0), 1, rgbcolor=(1,1,0))

In [2]: circle((0,0), 1, rgbcolor=(1,1,0), fill=True)

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))

t = text("hypotrochoid", (5,4), rgbcolor=(1,0,0))


show(p+t)

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)]

show(line(v), xmin=-20, xmax=20)

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),

(y,-2,2), (z,-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)

parametric_plot3d([fx, fy, fz], (u, 0, 2*pi),


(v, 0, 2*pi), frame=False, color="red")

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)

print("Shortest path from 1 to 3:", shortest_path)

Page | 23
Output:

Out[1]: Graph on 3 vertices

Shortest path from 1 to 3: [1, 3]

Page | 24
In [1]: G = graphs.PetersenGraph()
print("diameter =", G.diameter())

print("treewidth =", G.treewidth())


show(G)

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[2]:x |--> 2*x*cos(x^2)*e^(-x) - e^(-x)*sin(x^2) + 3

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,

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: "))

r = float (input ("Enter rate of interest: "))


print ("Simple interest = ", p*n*r/100)

Page | 47
Output:

Out[1]: Enter the principle: 5


Enter number of years: 2

Enter rate of interest: 6


Simple interest = 0.6

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))

In [2]: n = int (input ("Enter a number: "))


i = 1
s = 1

while i<=n:
s = s*i

i = i+1
print ('The factorial of', n, 'numbers is', s)

Page | 49
Output:

Out[1]: Enter a number: 5


Factorial of 0 is 1

Factorial of 1 is 1
Factorial of 2 is 2

Factorial of 3 is 6
Factorial of 4 is 24

Out[2]: Enter a number: 10

The factorial of 10 numbers is 3628800

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:

Out[1]: Which table: 5


How many stages: 10

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:

Out[1]: Enter 1 to convert Fahrenheit to Celsius or Enter 2 to


convert Celsius to Fahrenheit: 1

Enter the value of Fahrenheit: 45


Celsius = 7.222222222222222

Enter 1 to convert Fahrenheit to Celsius or Enter 2 to

convert Celsius to Fahrenheit: 2


Enter the value of Fahrenheit: 45

Fahrenheit = 113.0

Page | 54
In [1]: n = int (input ("Enter the number of terms: "))
i = 0

a = []
while i < n:

m = float (input ("Enter the numbers:"))


a.append(m)

i = i+1
for j in range(n-1):

for k in range (j+1, n):


if a[j] > a[k]:

(a[j], a[k]) =(a[k], a[j])


print ("The sorted list is ")

for i in range(n):
print(a[i])

Page | 55
Output:

Out[1]: Enter the number of terms: 5


Enter the numbers:1

Enter the numbers:2


Enter the numbers:12

Enter the numbers:5


Enter the numbers:8

The sorted list is


1.0

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

print ("Upper case letters: ", up)


print ("Lower case letters: ", low)

Page | 57
Output:

Out[1]: Enter a string:AdRiaN


Upper case letters: 3

Lower case letters: 3

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:

Out[1]: Enter an integer:45


45 is odd

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: "))

b = float (input ("Enter 'b' value: "))


c = float (input ("Enter 'c' value: "))

d = b**2 -4*a*c
if d > 0:

r1 = (-b+math.sqrt(d))/(2*a)
r2 = (-b-math.sqrt(d))/(2*a)

print ("The two roots are", r1,"and", r2)


elif d == 0:

r1 = -b/(2*a)
r2 = r1

print ("The two roots are same", r1,"and", r2)


else:
re1 = -b/(2*a)

im1 = math.sqrt(-d)/(2*a)
re2 = re1

im2 = -im1
print ("Root1 = ", re1,"+i (", im1,")")

print ("Root2 = ", re2,"+i (", im2,")")

Page | 61
Output:

Out[1]: Enter 'a' value: 5


Enter 'b' value: 4

Enter 'c' value: 3


Root1 = -0.4 +i ( 0.6633249580710799 )

Root2 = -0.4 +i ( -0.6633249580710799 )

Page | 62
In [1]: x = var('x')
y = var('y')

expression = x**2 + 2*x*y + y**2


simplified_expression = expression.simplify()

factorized_expression = simplified_expression.factor()
equation = x**2 - 4

solution = solve(equation, x)
print("Original Expression:", expression)

print("Simplified Expression:", simplified_expression)


print("Factorized Expression:", factorized_expression)

print("Solution:", solution)

Page | 63
Output:

Out[1]: Original Expression: x^2 + 2*x*y + y^2


Simplified Expression: x^2 + 2*x*y + y^2

Factorized Expression: (x + y)^2


Solution:

[
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()

In [3]: show(A1^2 + A1.transpose())

Page | 67
Output:

Out[1]:

Out[2]: [5, 0, -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[5]: -((x - 5)*(x - 9) - 48)*(x - 1) + 29*x + 3

Out[6]:    x  5  x  9   48   x  1  29 x  3

Out[7]: [x == -3/2*sqrt(33) + 15/2, x == 3/2*sqrt(33) + 15/2, x


==0]

 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 [2]: [valuation(c,p) for p in prime_range(2,23)]

In [3]: next_prime(2005)

In [4]: previous_prime(2005)

In [5]: divisors(28); sum(divisors(28)); 2*28

In [6]: d,u,v = xgcd(12,15)


d == u*12 + v*15

In [7]: n = 2005
inverse_mod(3,n)

Page | 73
Output:

Out[1]: 15511210043330985984000000

Out[2]: [22, 10, 6, 3, 2, 1, 1, 1]

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy