Numerical Python Book Practice
Numerical Python Book Practice
In [1]: %lsmagic
Out[1]: Available line magics:
%alias %alias_magic %autocall
Available cell magics:
%%! %%HTML %%SVG %%bash
%automagic
%%capture
%autosave
%%debug
%%file
In [3]: %reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? n
Nothing done.
In [5]: data=array([[1,2],
[3,4],
[5,6]])
In [6]: data.shape
Out[6]: (3, 2)
In [7]: X=Y=linspace(1,10)
In [8]: type(X)
Out[8]: numpy.ndarray
1
%bookmark
%%html
%cat
%c
%%javascrip
In [9]: x,y=meshgrid(X,Y)
In [10]: from sympy import *
x=symbols('x')
func=lambda x:x**2 +2*x -3
func(x)
Out[10]: x**2 + 2*x - 3
In [11]: %reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? n
Nothing done.
WARNING: pylab import has clobbered these variables: ['test', 'floor', 'beta', 'sqr
`%matplotlib` prevents importing * from pylab and numpy
In [13]: x,y,z=symbols('x,y,z')
z=(x+y)**2
z
Out[13]:
(x + y)2
In [14]: x=y=linspace(1,3)
X,Y=meshgrid(x,y)
Z=(+Y)**2
Z
Out[14]: array([[ 1.
,
1.
,
[ 1.08329863,
1.08329863,
[ 1.1699292 ,
1.1699292 ,
...,
[ 8.51686797,
8.51686797,
[ 8.75676801,
8.75676801,
[ 9.
,
9.
,
1.
, 1.
, ...,
1.
],
1.08329863, 1.08329863, ...,
1.08329863],
1.1699292 , 1.1699292 , ...,
1.1699292 ],
8.51686797, 8.51686797, ...,
8.51686797],
8.75676801, 8.75676801, ...,
8.75676801],
9.
, 9.
, ...,
9.
]])
2
1.
1.08329863,
1.1699292 ,
8.51686797,
8.75676801,
9.
In [15]: Z[0:2]
Out[15]: array([[ 1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
[ 1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
,
1.
],
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863,
1.08329863]])
In [16]: Z[0:-1]
Out[16]: array([[ 1.
,
1.
,
[ 1.08329863,
1.08329863,
[ 1.1699292 ,
1.1699292 ,
...,
[ 8.28029988,
8.28029988,
[ 8.51686797,
8.51686797,
[ 8.75676801,
8.75676801,
1.
, 1.
, ...,
1.
],
1.08329863, 1.08329863, ...,
1.08329863],
1.1699292 , 1.1699292 , ...,
1.1699292 ],
1.
1.08329863,
1.1699292 ,
8.28029988,
1.25989171,
8.51686797,
8.75676801,
In [17]: Z[3:]
Out[17]: array([[ 1.25989171,
1.25989171,
[ 1.35318617,
1.35318617,
[ 1.44981258,
1.44981258,
...,
[ 8.51686797,
8.51686797,
3
8.51686797, ...,
1.35318617,
1.44981258,
8.51686797,
8.51686797,
[ 8.75676801,
8.75676801,
[ 9.
,
9.
,
8.51686797],
8.75676801, 8.75676801, ...,
8.75676801],
9.
, 9.
, ...,
9.
]])
8.75676801,
9.
In [18]: Z[::-50]
Out[18]: array([[ 9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
0.,
1.,
2.,
3.,
4.,
5.,
10.,
11.,
12.,
13.,
14.,
15.,
20.,
21.,
22.,
23.,
24.,
25.,
30.,
31.,
32.,
33.,
34.,
35.,
40.,
41.,
42.,
43.,
44.,
45.,
4.,
5.])
50.],
51.],
52.],
53.],
54.],
55.]])
In [20]: F[:,0]
Out[20]: array([ 0.,
1.,
2.,
3.,
In [21]: F[:,3]
Out[21]: array([ 30.,
31.,
32.,
33.,
34.,
35.])
10.,
20.,
30.,
40.,
50.])
In [22]: F[0,:]
Out[22]: array([
0.,
In [23]: F[:3,:3]
Out[23]: array([[
[
[
0.,
1.,
2.,
10.,
11.,
12.,
20.],
21.],
22.]])
In [24]: F[:,0]
Out[24]: array([ 0.,
1.,
2.,
3.,
4.,
In [65]: F[1:4,2:4]
Out[65]: array([[ 21.,
[ 22.,
[ 23.,
31.],
32.],
33.]])
4
5.])
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9.,
9., 9.,
9., 9.,
9., 9.,
9.]])
9.,
9.,
9.,
We can access data by issueing n:m which means from element in row and then call ,n:m to limit
it in specific column
In [26]: F[::-1]
Out[26]: array([[
[
[
[
[
[
5.,
4.,
3.,
2.,
1.,
0.,
15.,
14.,
13.,
12.,
11.,
10.,
25.,
24.,
23.,
22.,
21.,
20.,
35.,
34.,
33.,
32.,
31.,
30.,
45.,
44.,
43.,
42.,
41.,
40.,
55.],
54.],
53.],
52.],
51.],
50.]])
5.,
3.,
1.,
15.,
13.,
11.,
25.,
23.,
21.,
35.,
33.,
31.,
45.,
43.,
41.,
55.],
53.],
51.]])
In [27]: F[::-2]
Out[27]: array([[
[
[
In [28]: F[3::2,1::3]
Out[28]: array([[ 13.,
[ 15.,
43.],
45.]])
1
h: 2
r
In [33]: Area_r=Area.subs(h_r)
Area_r
Out[33]:
2r2 +
2
r
In [34]: rsol=solve(Area_r.diff(r))[0]
rsol
Out[34]:
2
23
23
In [35]: _.evalf()
Out[35]:
0.541926070139289
In [36]: Area_r.diff(r,2).subs(r,rsol)
Out[36]:
12
In [37]: Area_r.subs(r,rsol)
Out[37]:
3
3 23
In [38]: _.evalf()
Out[38]:
5.53581044593209
In [39]: func_r=lambdify(r,2*pi*r**2 +2/r)
func_r(r)
Out[39]:
6.28318530717959r2 +
In [68]: plotting.plot(func_r(r),(r,0,2))
<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
2
r
0.0.1
fun:
nfev:
nit:
success:
x:
5.5358104459320856
16
15
True
0.54192607725571351
Newtons method
In [43]: x=symbols('x')
x_k1,x_k,k=symbols('x_k1 x_k k')
f=Function('f')
x_k1=x_k -f(k).diff(k)/f(k).diff(k,2)
x_k1
Out[43]:
xk
0.0.2
d
dk f (k)
d2
f (k)
dk2
i
2x1 + 10x2 10
In [47]: #Hessian
fhess_sym=hessian(f_sym,[x1,x2]).tolist()
#fhess_sym = [[f_sym.diff(x1_, x2_) for x1_ in (x1, x2)] for x2_ in (x1, x
fhess_sym
Out[47]:
hh
12 (x1 1)2 ,
i
2 ,
[2,
i
10]
In [48]: f_lmbda=lambdify([x1,x2],f_sym)
f_lmbda(x1,x2)
Out[48]:
2x1 x2 + (x1 1)4 + 5 (x2 1)2
In [49]: fprime_lmbda=lambdify([x1,x2],fprime_sym)
fprime_lmbda(x1,x2)
Out[49]:
h
i
2x1 + 10x2 10
In [50]: fhess_lambda=lambdify([x1,x2],fhess_sym)
fhess_lambda(x1,x2)
Out[50]:
hh
12 (x1 1)2 ,
i
2 ,
[2,
i
10]
In [54]: x_opt.tolist(),x_opt1.tolist()
Out[54]:
([1.882926129296321,
1.3765852258592641] ,
[1.8829260403989003,
1.3765852205473148])
<IPython.core.display.HTML object>
<IPython.core.display.HTML object>
Out[57]:
x0 x1 x2
In [58]: g=2*(x0*x1+x1*x2+x0*x2)-1
g
Out[58]:
2x0 x1 + 2x0 x2 + 2x1 x2 1
In [59]: L=f+l*g
L
Out[59]:
(2x0 x1 + 2x0 x2 + 2x1 x2 1) + x0 x1 x2
In [60]: grad_L=Matrix([L]).jacobian(x)
#grad_L = [diff(L, x_) for x_ in x]
grad_L
Out[60]:
(2x1 + 2x2 ) + x1 x2 (2x0 + 2x2 ) + x0 x2 (2x0 + 2x1 ) + x0 x1 2x0 x1 + 2x0 x2 + 2x1 x2 1
In [61]: sols=solve(grad_L)
sols
Out[61]:
"(
6
,
:
24
6
x0 :
,
6
6
x1 :
,
6
)
6
x2 :
,
6
6
:
,
24
6
x0 :
,
6
10
6
x1 :
,
6
)#
6
x2 :
6