0% found this document useful (0 votes)
58 views3 pages

CMSM Lecture2 Handouts

This document discusses using SageMath for computational mathematics. It covers: 1. Using Python as an advanced calculator by importing common math functions from the math module like sqrt, sin, gcd. 2. Calculating permutations and combinations using functions from math like perm and comb. 3. Solving quadratic equations by computing the quadratic formula and taking the square root of complex numbers using cmath. 4. Practice exercises including calculating the surface area and volume of a sphere, verifying properties of combinations, and exploring properties of complex numbers.

Uploaded by

Sandeep Kumar
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)
58 views3 pages

CMSM Lecture2 Handouts

This document discusses using SageMath for computational mathematics. It covers: 1. Using Python as an advanced calculator by importing common math functions from the math module like sqrt, sin, gcd. 2. Calculating permutations and combinations using functions from math like perm and comb. 3. Solving quadratic equations by computing the quadratic formula and taking the square root of complex numbers using cmath. 4. Practice exercises including calculating the surface area and volume of a sphere, verifying properties of combinations, and exploring properties of complex numbers.

Uploaded by

Sandeep Kumar
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/ 3

Computational Mathematics with SageMath

Ajit Kumar @ ICT Mumbai


Lecture 2

1 Python as an advanced calculator

[ ]: sqrt(2) # Gives name error.

[ ]: import math

[ ]: math.sqrt(2)

[ ]: math.sin(2.5)

[ ]: math.asin(-0.75)

[ ]: math.gcd(34352, 88262)

[ ]: help(math.log10)

[ ]: dir(math) # To list all the functions/methods inside math module

2 Permutations and combinatotions


[ ]: from math import perm, comb, factorial

[ ]: n = 20
r = 12
perm(n,r)
comb(n,r)

[ ]: perm(n,r)==factorial(n)/factorial(n-r)

[ ]: comb(n,r) == factorial(n)/(factorial(r)*factorial(n-r))

[ ]: from math import *

1
[ ]: sqrt(4)

[ ]: sqrt(-1) # math domain error

[ ]: import cmath

[ ]: dit(cmath)

[ ]: cmath.sqrt(-1)

[ ]: z = 5+3j
cmath.sqrt(z)

3 Solving a quadratic ax2 + bx + c = 0.

[ ]: a, b, c = 3, 5, -7
x1 = (-b+sqrt(b**2-4*a*c))/(2*a)
x2 = (-b-sqrt(b**2-4*a*c))/(2*a)
print(f'The roots are {x1} and {x2}')

[ ]: a = input('Enter the value of a:')

[ ]: a

[ ]: a = float(input('Enter the value of a:'))

[ ]: a

[ ]: a = float(input('Enter the value of a:'))


b = float(input('Enter the value of b:'))
c = float(input('Enter the value of c:'))
x1 = (-b+cmath.sqrt(b**2-4*a*c))/(2*a)
x2 = (-b-cmath.sqrt(b**2-4*a*c))/(2*a)
print(f'The roots are {x1} and {x2}')

[ ]: a = float(input('Enter a:'))
b = float(input('Enter b:'))
c = float(input('Enter c:'))
s = (a+b+c)/(2.0)
A = sqrt(s*(s-a)*(s-b)*(s-c))
print(f'The area is given by {A:.4}')
print(f'The area is given by {A:.4f}')

[ ]: factorial(100)

2
[ ]: factorial(1000)

4 Practice Execrcises
1. Write python codes to input radius of a shpere and print its surface area and volume.
2. Input a positive integer n and intger k <= n. Verify the following properties
i. n Ck = n Cn−k
ii. k × n Ck = n × n−1 Ck−1
iii. n Ck−1 + n Ck = n+1 Ck
3. Input a complex number z and explore some of the properties of z.

[ ]:

You might also like