scipy - Jupyter Notebook
scipy - Jupyter Notebook
engineering. The SciPy library depends on NumPy, which provides convenient and fast N-dimensional array
manipulation. The main reason for building the SciPy library is that, it should work with NumPy arrays
Numpy and scipy both used for the mathematical and numerical analysis Numpy contains array based basic
operation while sciply contains all the numerical values scipy is fully featured version of mathematical &
scientific functions.
In [3]:
In [4]:
In [5]:
#exponential function
a=sp.exp10(3)
print(a)
b=sp.exp2(6)
print(b)
1000.0
64.0
In [6]:
#trignometric functions
c=sp.sindg(90)
print(c)
d=sp.cosdg(90)
print(d)
1.0
-0.0
In [7]:
0.5267500645255109
In [8]:
#permutation fuction
#permutation of N things where K things taken at a time..i.e. K permutation of N
p=sp.perm(10,3,exact=True) #exact true will return int value
print(p)
720
In [9]:
import numpy as np
a=np.array([3,5])
b=np.array([10,12])
x=sp.perm(b,a)
print(x)
[ 720. 95040.]
In [10]:
#combination function
c=sp.comb(10,3,exact=True)
print(c)
120
In [11]:
In [12]:
#single integration
f=lambda x:x**3
i=igt.quad(f,1,2)# quad(function,lower-lim,upper-lim)
print(i)
#final result contains integration result and error in the result
(3.7500000000000004, 4.1633363423443377e-14)
In [13]:
#double intgration
#dblquad(function,outer_low-lim,outer_upr-lim,inner_low-lim:fun1,inner_upr-lim:fun2)
a=igt.dblquad(lambda x,y:x*y,0,0.5,lambda x:0,lambda x:1-2*x)
print(a)
(0.010416666666666668, 4.101620128472366e-16)
In [14]:
#polynomial function
#ax**2+bx+c
from numpy import poly1d
p=poly1d([3,6,8])#it will decide the order of polynomial by the
#number of constant
print(p)
2
3 x + 6 x + 8
In [15]:
p=poly1d([4,5,0,8])
print(p)
3 2
4 x + 5 x + 8
In [16]:
p=poly1d([4,5,7,8],variable='y')
print(p)
3 2
4 y + 5 y + 7 y + 8
In [17]:
2
3 x + 6 x + 8
Out[17]:
array([-1.+1.29099445j, -1.-1.29099445j])
In [18]:
p=poly1d([4,5,7,8])
print(p)
print(p.integ(k=6))#integ will give the polyn after integration and k constant will be a
3 2
4 x + 5 x + 7 x + 8
4 3 2
1 x + 1.667 x + 3.5 x + 8 x + 6
In [19]:
2
12 x + 10 x + 7
In [20]:
#linear algebra
from scipy import linalg as la
In [21]:
A=np.array([[1,2],[3,4]])
print(A)
[[1 2]
[3 4]]
In [22]:
b=la.inv(A)#inverse of a matrix
print(b)
[[-2. 1. ]
[ 1.5 -0.5]]
In [23]:
x=la.det(A)#determinant of matrix
print(x)
-2.0
In [24]:
[-0.37228132+0.j 5.37228132+0.j]
[[-0.82456484 -0.41597356]
[ 0.56576746 -0.90937671]]
In [25]:
B=np.array([[1,2,3],[4,5,6],[7,8,9]])
print(B)
[[1 2 3]
[4 5 6]
[7 8 9]]
In [28]:
#LU decomposition
#permutation matrix
#lower triangular matrix
#upper triangular matrix
P,L,U=la.lu(B)
print(P)
print(L)
print(U)
[[0. 1. 0.]
[0. 0. 1.]
[1. 0. 0.]]
[[1. 0. 0. ]
[0.14285714 1. 0. ]
[0.57142857 0.5 1. ]]
[[ 7.00000000e+00 8.00000000e+00 9.00000000e+00]
[ 0.00000000e+00 8.57142857e-01 1.71428571e+00]
[ 0.00000000e+00 0.00000000e+00 -1.58603289e-16]]
In [29]:
#Polynomial Solution
#3x+y=4
#x+3y=5
coef=np.array([[3,1],[1,3]])#coefficient of equations
const=np.array([4,5])#constant term of equation
result=la.solve(coef,const)
print(result)
[0.875 1.375]
In [30]:
In [31]:
con.find()
Out[31]:
['Angstrom star',
'Avogadro constant',
'Bohr magneton',
'Bohr magneton in Hz/T',
'Bohr magneton in K/T',
'Bohr magneton in eV/T',
'Bohr magneton in inverse meter per tesla',
'Bohr radius',
'Boltzmann constant',
'Boltzmann constant in Hz/K',
'Boltzmann constant in eV/K',
'Boltzmann constant in inverse meter per kelvin',
'Compton wavelength',
'Cu x unit',
'Faraday constant',
'Fermi coupling constant',
'Hartree energy',
In [32]:
con.pi
Out[32]:
3.141592653589793
In [33]:
con.u
Out[33]:
1.6605390666e-27
In [36]:
con.neutron_mass
Out[36]:
1.67492749804e-27
In [37]:
con.value('luminous efficacy')
Out[37]:
683.0
In [39]:
con.value('Planck constant')
Out[39]:
6.62607015e-34
In [40]:
#statistcs module
from scipy.stats.mstats import gmean,hmean
In [41]:
#geomatrical mean
g=gmean([1,5,20])
print(g)
4.641588833612778
In [42]:
#harmonic mean
h=hmean([1,5,20])
print(h)
2.4
scipy.io(input and output package): it provides wide range of functions to work around different file formats
In [43]:
In [52]:
v=np.arange(10)
print(v)
[0 1 2 3 4 5 6 7 8 9]
In [53]:
sio.savemat(r'c:\users\admin\desktop\arry1.mat',{'vector':v})
In [54]:
file=sio.loadmat(r'c:\users\admin\desktop\arry1.mat')
print(file)
{'__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Mon Jul 12
10:50:23 2021', '__version__': '1.0', '__globals__': [], 'vector': array
([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])}
In [ ]: