, MathLab.
np:
import numpy as np
numpy.array. Python, NumPy , .
, , () .
M=np.array(range(0,110,10))
NumPy arange(). numpy.arange() range(), , .
M=np.arange(0,110,10)
numpy.linspace(), , .
M=np.linspace(0,100,11)
: linspace, , , . , . , 0 360 10 36, 37.
numpy
- numpy . .
>>> import numpy as np # numpy np
>>>a = np.arange(24) + 1
>>>a.shape = (4, 6)
>>> print a #
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]
>>> print a[1,2] # 1,2
9
>>> print a[1,:] # 1
[ 7 8 9 10 11 12]
>>> print a [1] # 1
[ 7 8 9 10 11 12]
>>> print a[:,1] # 1
[ 2 8 14 20]
>>> print a[-2,:] #
[13 14 15 16 17 18]
>>> print a[0:2,1:3] # 2x2
[[2 3]
[8 9]]
>>> print a[1,::3] # 1
[ 7 10]
>>> print a[:,::-1] #
[[ 6 5 4 3 2 1]
[12 11 10 9 8 7]
[18 17 16 15 14 13]
[24 23 22 21 20 19]]
( ), . 0 1:
>>> a[1,::3] = np.array([0,0])
>>> print a
[[ 1 2 3 4 5 6]
[ 0 8 9 0 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]
Numpy
numpy select, , . :
numpy.select(condlist, choicelist, default=0)
|
|
condlist , choicelist , default , . :
import numpy as np
x = np.arange(10)
condlist = [x<3, x>5]
choicelist = [x, x**2]
print(np.select(condlist, choicelist))
print(np.select([x<3],[x]))
extract, :
print(np.extract(x<3, x))
numpy
np.savetxt("test.txt", x)
np.savetxt("test2.txt", x, fmt="%2.3f", delimiter=",")
savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ')
y = np.loadtxt("test.txt")
y = np.loadtxt("test2.txt", delimiter=",")
numpy.linalg
A = np.matrix([[1.,2], [3,4], [5,6]])
, , , .
np.rank(A)
, !
A = np.array([[1,2],[3,4]])
b = np.array([10, 20])
x = np.linalg.solve(A,b)
xmax=10
N=101
x=np.linspace(0,xmax,N)
y=sin(x)
y1=(float(N)/xmax)*diff(y)
y2=(float(N)/xmax)**2*diff(y,2)
plot(x,y) plot(x[:-1],y1)
plot(x[:-2],y2)
grid()
- scipy