# Python for math

• Python is used as a teaching tool in mathematics education due to its simplicity and readability. It helps students visualize and understand mathematical concepts through coding.

import math
!pip install vectors
dino_vectors = [(6,4), (3,1), (1,2),
(-1,5), (-2,5), (-3,4), (-4,4),
# insert 16 remaining vectors here
]
from math import sqrtdef length(v):
return sqrt(v[0]**2 + v[1]**2
from math import sin, cos
def to_cartesian(polar_vector):
length, angle = polar_vector[0],
polar_vector[1]
return (length*cos(angle),
length*sin(angle))
from math import pi
angle = 37*pi/180
to_cartesian((5,angle))
(3.993177550236464, 3.0090751157602416)
from math import asin
sin(1)
0.8414709848078965
asin(0.8414709848078965)
1.0
from math import sqrt
asin(3/sqrt(13))
0.9827937232473292
math.asin
from math import acos
acos(-2/sqrt(13))
2.1587989303424644
import matplotlib
import numpy as np
from matplotlib.patches import Polygon
from matplotlib.collections
import PatchCollection
from vectors import *
def plane_equation(p1,p2,p3):
parallel1 = subtract(p2,p1)
parallel2 = subtract(p3,p1)
a,b,c = cross(parallel1, parallel2)
d = dot((a,b,c), p1)
return a,b,c,d
data = np.arange(9).reshape(33)
data
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
np.transpose(data)
array([[0, 3, 6], [1, 4, 7], [2, 5, 8]])
A = np.arange(17).reshape(23)
A
array([[1, 2, 3], [4, 5, 6]])
B = np.arange(17).reshape(32)
B
array([[1, 2], [3, 4], [5, 6]])
np.dot(A, B)
array([[22, 28], [49, 64]])
np.dot(B, A)
array([[ 9, 12, 15], [19, 26, 33], [29, 40, 51]])
# SYMPY
import sympy
sympy.init_printing()
from sympy import I, pi, oo
sympy.symbols("x")
x
import numpy as np
data = np.array([[12], [34], [56]])
type(data)
numpy.ndarray
data
array([[1, 2], [3, 4], [5, 6]])
data.ndim
2
data.shape
(3,2)
data.size
6
data.dtype
dtype('int64')
data.nbytes
48
np.array([123], dtype=np.int)
array([1, 2, 3])
np.array([1,2,3],dtype=np.float)
array([1., 2., 3.])
np.array([1,2,3],dtype=np.complex)
array([1.+0.j, 2.+0.j, 3.+0.j])
data.imag
array([[0, 0], [0, 0], [0, 0]])
#Array Created from Lists and
Other Array- Like Objects,
Onedimention
np.array([1,2,3,4])
array([1, 2, 3, 4])
np.array([[1,2],[3,4]])
array([[1, 2], [3, 4]])
np.zeros((5,3))
array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
np.ones((5,3))
array([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]])
x1=5.4*np.ones(10)
x1
array([5.4, 5.4, 5.4, 5.4,
5.4, 5.4, 5.4, 5.4, 5.4, 5.4])
x1=np.empty(5)
x1
array([0. , 0.25, 0.5 , 0.75, 1. ])
x1.fill(3.0)
x1
array([3., 3., 3., 3., 3.])
x2=np.full(5,3.0)
x2
array([3., 3., 3., 3., 3.])
np.arange(0.0,10,1)
array([0., 1., 2., 3., 4., 5., 6.,
7., 8., 9.])
np.linspace(0,10,11)
array([ 0., 1., 2., 3., 4., 5.,
6., 7., 8., 9., 10.])
np.logspace(0,2,5)
array([ 1. , 3.16227766, 10. , 31.6227766 , 100. ])
x=np.array([-1,0,1])
y=np.array([-2,0,2])
z=np.array([-3,0,3])
x
array([-1, 0, 1])
y
array([-2, 0, 2])
z
array([-3, 0, 3])
X,Y,Z=np.meshgrid(x,y,z)
X,Y,Z
(array([[[-1, -1, -1], [ 0, 0, 0], [ 1, 1, 1]], [[-1, -1, -1], [ 0, 0, 0], [ 1, 1, 1]], [[-1, -1, -1], [ 0, 0, 0], [ 1, 1, 1]]]),
array([[[-2, -2, -2], [-2, -2, -2], [-2, -2, -2]], [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]], [[ 2, 2, 2], [ 2, 2, 2], [ 2, 2, 2]]]),
array([[[-3, 0, 3], [-3, 0, 3], [-3, 0, 3]], [[-3, 0, 3], [-3, 0, 3], [-3, 0, 3]], [[-3, 0, 3], [-3, 0, 3], [-3, 0, 3]]]))
P=(X+Y)**2
P
array([[[9, 9, 9], [4, 4, 4], [1, 1, 1]], [[1, 1, 1], [0, 0, 0], [1, 1, 1]], [[1, 1, 1], [4, 4, 4], [9, 9, 9]]])
X**2
array([[[1, 1, 1], [0, 0, 0], [1, 1, 1]], [[1, 1, 1], [0, 0, 0], [1, 1, 1]], [[1, 1, 1], [0, 0, 0], [1, 1, 1]]])
Y**2
array([[[4, 4, 4], [4, 4, 4], [4, 4, 4]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[4, 4, 4], [4, 4, 4], [4, 4, 4]]])
Z**2
array([[[9, 0, 9], [9, 0, 9], [9, 0, 9]], [[9, 0, 9], [9, 0, 9], [9, 0, 9]], [[9, 0, 9], [9, 0, 9], [9, 0, 9]]])
2*X*Y
array([[[ 4, 4, 4], [ 0, 0, 0], [-4, -4, -4]], [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]], [[-4, -4, -4], [ 0, 0, 0], [ 4, 4, 4]]])
np.empty(3, dtype=np.float)
array([58., 15., 3.])
def f(x):
y = np.ones_like(x)
return y
x=2
x
2
np.eye(3,k=1)
array([[0., 1., 0.], [0., 0., 1.], [0., 0., 0.]])
np.diag(np.arange(0205))
array([[ 0, 0, 0, 0],
[ 0, 5, 0, 0], [ 0, 0, 10, 0], [ 0, 0, 0, 15]])
a = np.arange(011)
a
array([ 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10])
data = np.array([[1,2],[3,4],[5,6]])

### #Trigonometry

x = np.linspace(-1111)
x
array([-1. , -0.8, -0.6, -0.4,
-0.2, 0. , 0.2, 0.4, 0.6, 0.8, 1. ])
y = np.sin(np.pi * x)
y'
array([-1.22464680e-16, -5.87785252e-01, -9.51056516e-01, -9.51056516e-01, -5.87785252e-01, 0.00000000e+00, 5.87785252e-01, 9.51056516e-01, 9.51056516e-01, 5.87785252e-01, 1.22464680e-16])
np.round(y, decimals=4)
array([-0. , -0.5878, -0.9511, -0.9511, -0.5878, 0. , 0.5878, 0.9511, 0.9511, 0.5878, 0. ])
np.add(np.sin(x) ** 2, np.cos(x) ** 2)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.sin(x) ** 2 + np.cos(x) ** 2
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.subtract(np.sin(x)**2,np.cos(x)**2)
array([ 0.41614684, 0.02919952, -0.36235775, -0.69670671, -0.92106099, -1. , -0.92106099, -0.69670671, -0.36235775, 0.02919952, 0.41614684])
data = np.random.normal(size=(15,15))
np.mean(data)
\displaystyle 0.0349074182786913

### import sympy

np.alltrue(np.einsum("mk,kn", A, B) == np.dot(A, B))
True
x = sympy.Symbol("x")
expr = 1 + 2 * x**2 + 3 * x**3
expr
\displaystyle 3 x^{3} + 2 x^{2} + 1