Python3下NumPy学习笔记(和MATLAB语法进行对比)

NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算。因此,说到numpy数值计算模块,我总是忍不住要把他拿来和MATLAB(ps:作为应用数学专业的学生,我先接触到MATLAB)对比,毕竟矩阵实验室(matrix laboratory)确实也不是浪得虚名,但是其下标从1开始的风格很是让人(特别是习惯索引从0开始的程序猿)诟病,这也使得它更倾向于是一个进行科学计算的工具而非专门的程序语言,所以,如果可能的话,我会尽量把语法方面的用法和MATLAB进行对比。

数组和索引

# Python
>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> a
array([1, 2, 3])
>>> a[0]
1

-------------------------------------------------------------------------------------------
% MATLAB
>> a = [1, 2, 3]

a =

     1     2     3

>> a(1)

ans =

     1

>> 

矩阵和切片

# Python
>>> b = np.array([[1,  2],  [3,  4]])
>>> b
array([[1, 2],
       [3, 4]])

>>> b[0, 1]
2
>>> b[0][1]
2
>>> b[1, :]
array([3, 4])
>>> 

----------------------------------------------------------------------------------------
% MATLAB
>> b = [1, 2; 3, 4]

b =

     1     2
     3     4

>> b(1, 2)

ans =

     2

>> b(2,:)

ans =

     3     4

>> 
# Python
>>> np.e
2.718281828459045
>>> np.pi
3.141592653589793
>>> 
>>> np.eye(5)
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])
>>> np.eye(5).shape
(5, 5)
>>> np.zeros((5, 5))
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
>>> a= np.arange(24)
>>> a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
>>> a.reshape(4,2,3)
array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[ 6,  7,  8],
        [ 9, 10, 11]],

       [[12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23]]])
>>> np.linspace(0, 10, 11)
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
>>> np.logspace(2,4,3)
array([  100.,  1000., 10000.])

----------------------------------------------------------------------------------------
% MATLAB
>> exp(1)

ans =

   2.718281828459046

>> pi

ans =

   3.141592653589793

>> eye(5)

ans =

     1     0     0     0     0
     0     1     0     0     0
     0     0     1     0     0
     0     0     0     1     0
     0     0     0     0     1
>> size(eye(5))

ans =

     5     5
>> zeros(5)

ans =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
>> aa=0:23

aa =

  Columns 1 through 14

     0     1     2     3     4     5     6     7     8     9    10    11    12    13

  Columns 15 through 24

    14    15    16    17    18    19    20    21    22    23

MATLAB是列优先的
>> reshape(aa, 2, 3, 4)

ans(:,:,1) =

     0     2     4
     1     3     5


ans(:,:,2) =

     6     8    10
     7     9    11


ans(:,:,3) =

    12    14    16
    13    15    17


ans(:,:,4) =

    18    20    22
    19    21    23

>> linspace(0, 10, 11)

ans =

     0     1     2     3     4     5     6     7     8     9    10
>> logspace(2,4,3)

ans =

         100        1000       10000

子矩阵抽取 

# Python
>>> np.eye(5)[np.ix_([0,2,4],[1,2])]
array([[0., 0.],
       [0., 1.],
       [0., 0.]])

-------------------------------------------------------------------------------------------
% MATLAB
>> aa=eye(5)

aa =

     1     0     0     0     0
     0     1     0     0     0
     0     0     1     0     0
     0     0     0     1     0
     0     0     0     0     1

>> aa([1,3,5],[2,3])

ans =

     0     0
     0     1
     0     0

数组和矩阵运算 

# Python
>>> a=np.array([1,2,3,4])
>>> b = np.array([10,20,30,40])
>>> 2*b
array([20, 40, 60, 80])
>>> a*b
array([ 10,  40,  90, 160])

>>> a = np.array([[ 0, 0, 0],
           [10,10,10],
           [20,20,20],
           [30,30,30]])
>>> b = np.array([1,2,3])
>>> a+b
array([[ 1,  2,  3],
       [11, 12, 13],
       [21, 22, 23],
       [31, 32, 33]])
>>> b
array([1, 2, 3])
>>> np.tile(b,(5,2))  # 对b进行横向纵向盖瓦片
array([[1, 2, 3, 1, 2, 3],
       [1, 2, 3, 1, 2, 3],
       [1, 2, 3, 1, 2, 3],
       [1, 2, 3, 1, 2, 3],
       [1, 2, 3, 1, 2, 3]])
>>> b
array([[1, 2],
       [3, 4]])
>>> np.tile(b,(5,2))
array([[1, 2, 1, 2],
       [3, 4, 3, 4],
       [1, 2, 1, 2],
       [3, 4, 3, 4],
       [1, 2, 1, 2],
       [3, 4, 3, 4],
       [1, 2, 1, 2],
       [3, 4, 3, 4],
       [1, 2, 1, 2],
       [3, 4, 3, 4]])

----------------------------------------------------------------------------------------
% MATLAB
>> a=[1,2,3,4]

a =

     1     2     3     4

>> b=[10,20,30,40]

b =

    10    20    30    40
>> 2*b

ans =

    20    40    60    80

 
>> a.*b

ans =

    10    40    90   160
>> a

a =

     0    10    20    30

>> b

b =

     1     2     3

>> [x,y]=meshgrid(a,b)

x =

     0    10    20    30
     0    10    20    30
     0    10    20    30


y =

     1     1     1     1
     2     2     2     2
     3     3     3     3

>> (x+y)'

ans =

     1     2     3
    11    12    13
    21    22    23
    31    32    33

数组和矩阵操作

# Python
>>> import numpy as np
>>> np.arange(8)
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> a=np.arange(8)
>>> b=a.reshape(4, 2)
>>> b
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])
>>> a=np.arange(9).reshape(3,3)
>>> for element in a.flat:
	print(element)

	
0
1
2
3
4
5
6
7
8
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> a.T
array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])

----------------------------------------------------------------------------------------
% MATLAB
>> a=0:7

a =

     0     1     2     3     4     5     6     7
% MATLAB默认列优先
>> reshape(a, 2, 4)'

ans =

     0     1
     2     3
     4     5
     6     7
>> a

a =

     0     3     6
     1     4     7
     2     5     8
[n, m] = size(a);

test.m
for i=1:m
    for j=1:n
        disp(a(i,j))
    end
end

>> test
     0

     3

     6

     1

     4

     7

     2

     5

     8
>> a

a =

     0     1     2
     3     4     5
     6     7     8

>> a'

ans =

     0     3     6
     1     4     7
     2     5     8

数组和矩阵连接

# Python
>>> import numpy as np
>>> a = np.array([[1,2],[3,4]])
>>> b = np.array([[5,6],[7,8]])
>>> np.concatenate((a,b),axis=1)
array([[1, 2, 5, 6],
       [3, 4, 7, 8]])
>>> np.concatenate((a,b),axis=0)
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])

>>> np.hstack((a,b))
array([[1, 2, 5, 6],
       [3, 4, 7, 8]])
>>> np.vstack((a,b))
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])

>>> a=np.arange(9)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> np.split(a, 3)
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]
>>> np.split(a, [4,7])
[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]

>>> harr = np.floor(10 * np.random.random((2, 6)))
array([[8., 5., 8., 8., 7., 1.],
       [3., 1., 5., 9., 1., 4.]])
>>> np.hsplit(harr,3)
[array([[8., 5.],
        [3., 1.]]),
 array([[8., 8.],
        [5., 9.]]), 
 array([[7., 1.],
        [1., 4.]])]
>>> a = np.arange(16).reshape(4,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> np.vsplit(a,2)
[array([[0, 1, 2, 3],
        [4, 5, 6, 7]]), 
 array([[ 8,  9, 10, 11],
        [12, 13, 14, 15]])]

>>> a = np.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> np.resize(a,(3, 2))
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> np.resize(a,(3, 3))
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3]])

>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> np.append(a, [7, 8, 9])
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# 当axis为0时,数组是加在下边(列数要相同)。当axis为1时,数组是加在右边(行数要相同)。
# axis=0,就代表沿着行坐标变化的方向进行计算,也就是对每一列进行操作。
# axis=1,就代表沿着列坐标变化的方向进行计算,也就是对每一行进行操作。
>>> np.append(a, [[7, 8, 9]], axis=0)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> np.append(a, [[5,5,5],[7,8,9]],axis = 1)
array([[1, 2, 3, 5, 5, 5],
       [4, 5, 6, 7, 8, 9]])
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> np.delete(a, 1, axis=1)
array([[ 0,  2,  3],
       [ 4,  6,  7],
       [ 8, 10, 11]])
>>> np.delete(a, 1, axis=0)
array([[ 0,  1,  2,  3],
       [ 8,  9, 10, 11]])

----------------------------------------------------------------------------------------
% MATLAB
>> a=[1,2;3,4];
>> b=[5,6;7,8];
>> [a(:,:),b(:,:)]

ans =

     1     2     5     6
     3     4     7     8

>> [a(:,:);b(:,:)]

ans =

     1     2
     3     4
     5     6
     7     8
>> a=[0,1,2,3;4,5,6,7;8,9,10,11]

a =

     0     1     2     3
     4     5     6     7
     8     9    10    11

>> a(:,2)=[]

a =

     0     2     3
     4     6     7
     8    10    11
>> a=[0,1,2,3;4,5,6,7;8,9,10,11]

a =

     0     1     2     3
     4     5     6     7
     8     9    10    11

>> a(2,:)=[]

a =

     0     1     2     3
     8     9    10    11

位操作 

# Python
>>> import numpy as np
>>> a=13
>>> b=17
>>> a & b
1
>>> a | b
29
>>> a ^ b
28
>>> np.bitwise_and(a, b)
1
>>> np.bitwise_or(a, b)
29
>>> np.bitwise_xor(a,b)
28
>>> bin(242)
'0b11110010'
>>> np.left_shift(10, 2)
40
>>> np.right_shift(40, 2)
10
>>> bin(10)
'0b1010'
>>> bin(40)
'0b101000'

----------------------------------------------------------------------------------------
% MATLAB
>> bitand(13, 17)

ans =

     1

>> bitor(13, 17)

ans =

    29
>> bitxor(13, 17)

ans =

    28
>> dec2bin(242)

ans =

11110010
>> bitshift(10,2,8)

ans =

    40
>> bitshift(40,-2,8)

ans =

    10

字符串操作,与Python自带函数对比 

# Python
>>> import numpy as np

>>> "".join(['hello', 'xyz'])
'helloxyz'
>>> np.char.add(['hello'], ['xyz'])
array(['helloxyz'], dtype='<U8')

>>> [''.join(x) for x in zip(['hello', 'hi'],[' abc', ' xyz'])]
['hello abc', 'hi xyz']
>>> np.char.add(['hello', 'hi'],[' abc', ' xyz'])
array(['hello abc', 'hi xyz'], dtype='<U9')

>>> 'Runoob '*3
'Runoob Runoob Runoob '
>>> np.char.multiply('Runoob ',3)
array('Runoob Runoob Runoob ', dtype='<U21')

>>> "Runoob".center(20, '*')
'*******Runoob*******'
>>> np.char.center('Runoob', 20,fillchar = '*')
array('*******Runoob*******', dtype='<U20')

>>> "runoob".capitalize()
'Runoob'
>>> np.char.capitalize('runoob')
array('Runoob', dtype='<U6')

>>> 'i like runoob'.title()
'I Like Runoob'
>>> np.char.title('i like runoob')
array('I Like Runoob', dtype='<U13')

>>> [x.lower() for x in ['RUNOOB','GOOGLE']]
['runoob', 'google']
>>> np.char.lower(['RUNOOB','GOOGLE'])
array(['runoob', 'google'], dtype='<U6')

>>> "RUNOOB".lower()
'runoob'
>>> np.char.lower('RUNOOB')
array('runoob', dtype='<U6')

>>> [x.upper() for x in ['runoob','google']]
['RUNOOB', 'GOOGLE']
>>> np.char.upper(['runoob','google'])
array(['RUNOOB', 'GOOGLE'], dtype='<U6')

>>> 'runoob'.upper()
'RUNOOB'
>>> np.char.upper('runoob')
array('RUNOOB', dtype='<U6')

>>> 'i like runoob?'.split()
['i', 'like', 'runoob?']
>>> np.char.split ('i like runoob?')
array(list(['i', 'like', 'runoob?']), dtype=object)

>>> 'www.runoob.com'.split('.')
['www', 'runoob', 'com']
>>> np.char.split ('www.runoob.com', sep = '.')
array(list(['www', 'runoob', 'com']), dtype=object)

>>> 'i\nlike runoob?'.split()
['i', 'like', 'runoob?']
>>> np.char.splitlines('i\nlike runoob?')
array(list(['i', 'like runoob?']), dtype=object)

>>> 'ashok arunooba'.strip('a')
'shok arunoob'
>>> np.char.strip('ashok arunooba','a')
array('shok arunoob', dtype='<U14')

>>> [x.strip('a') for x in ['arunooba','admin','java']]
['runoob', 'dmin', 'jav']
>>> np.char.strip(['arunooba','admin','java'],'a')
array(['runoob', 'dmin', 'jav'], dtype='<U8')

>>> ":".join('runoob')
'r:u:n:o:o:b'
>>> np.char.join(':','runoob')
array('r:u:n:o:o:b', dtype='<U11')

>>> [x[0].join(x[1]) for x in zip([':','-'],['runoob','google'])]
['r:u:n:o:o:b', 'g-o-o-g-l-e']
>>> np.char.join([':','-'],['runoob','google'])
array(['r:u:n:o:o:b', 'g-o-o-g-l-e'], dtype='<U11')

>>> "i like runoob".replace('oo', 'cc')
'i like runccb'
>>> np.char.replace ('i like runoob', 'oo', 'cc')
array('i like runccb', dtype='<U13')

>>> np.char.encode('runoob', 'cp500')
array(b'\x99\xa4\x95\x96\x96\x82', dtype='|S6')
>>> np.char.decode(b'\x99\xa4\x95\x96\x96\x82','cp500')
array('runoob', dtype='<U6')
>>> 

数学函数 

# Python
>>> import numpy as np
>>> a = np.array([0,30,45,60,90])
>>> np.sin(a*np.pi/180)
array([0.        , 0.5       , 0.70710678, 0.8660254 , 1.        ])
>>> np.cos(a*np.pi/180)
array([1.00000000e+00, 8.66025404e-01, 7.07106781e-01, 5.00000000e-01,
       6.12323400e-17])
>>> np.tan(a*np.pi/180)
array([0.00000000e+00, 5.77350269e-01, 1.00000000e+00, 1.73205081e+00,
       1.63312394e+16])
>>> a = np.array([0,np.pi/6,np.pi/4,np.pi/3,np.pi/2])
>>> np.degrees(a)
array([ 0., 30., 45., 60., 90.])
>>> a = np.array([1.0,5.55,  123,  0.567,  25.532])
>>> np.around(a)
array([  1.,   6., 123.,   1.,  26.])
>>> a = np.array([-1.7,  1.5,  -0.2,  0.6,  10])
>>> np.floor(a)
array([-2.,  1., -1.,  0., 10.])
>>> np.ceil(a)
array([-1.,  2., -0.,  1., 10.])

----------------------------------------------------------------------------------------
% MATLAB
>> a=[0, 30, 45, 60, 90]

a =

     0    30    45    60    90

>> sin(pi.*a/180)

ans =

         0    0.5000    0.7071    0.8660    1.0000

>> cos(pi.*a/180)

ans =

    1.0000    0.8660    0.7071    0.5000    0.0000

>> tan(pi.*a/180)

ans =

   1.0e+16 *

         0    0.0000    0.0000    0.0000    1.6331

>> a = [1.0,5.55,  123,  0.567,  25.532]

a =

    1.0000    5.5500  123.0000    0.5670   25.5320

>> round(a)

ans =

     1     6   123     1    26

>> a = [-1.7,  1.5,  -0.2,  0.6,  10]

a =

   -1.7000    1.5000   -0.2000    0.6000   10.0000

>> floor(a)

ans =

    -2     1    -1     0    10

>> ceil(a)

ans =

    -1     2     0     1    10

算术函数 

# Python
>>> import numpy as np
>>> a = np.arange(9, dtype = np.float_).reshape(3,3)
>>> a
array([[0., 1., 2.],
       [3., 4., 5.],
       [6., 7., 8.]])
>>> b = np.array([10,10,10])
>>> b
array([10, 10, 10])
>>> np.add(a, b)
array([[10., 11., 12.],
       [13., 14., 15.],
       [16., 17., 18.]])
>>> np.subtract(a, b)
array([[-10.,  -9.,  -8.],
       [ -7.,  -6.,  -5.],
       [ -4.,  -3.,  -2.]])
>>> np.multiply(a, b)
array([[ 0., 10., 20.],
       [30., 40., 50.],
       [60., 70., 80.]])
>>> np.divide(a, b)
array([[0. , 0.1, 0.2],
       [0.3, 0.4, 0.5],
       [0.6, 0.7, 0.8]])
>>> a = np.array([0.25,  1.33,  1,  100])
>>> np.reciprocal(a)
array([4.       , 0.7518797, 1.       , 0.01     ])
>>> a = np.array([10,100,1000])
>>> np.power(a, 2)
array([    100,   10000, 1000000], dtype=int32)
>>> b=np.array([1, 2, 3])
>>> a
array([  10,  100, 1000])
>>> np.power(a, b)
array([        10,      10000, 1000000000], dtype=int32)
>>> a = np.array([10,20,30])
>>> b = np.array([3,5,7])
>>> np.mod(a, b)
array([1, 0, 2], dtype=int32)
>>> np.remainder(a, b)
array([1, 0, 2], dtype=int32)
>>> 

----------------------------------------------------------------------------------------
% MATLAB
>> a=reshape(0:8, 3, 3)'

a =

     0     1     2
     3     4     5
     6     7     8

>> b=10*ones(3,3)

b =

    10    10    10
    10    10    10
    10    10    10

>> a+b

ans =

    10    11    12
    13    14    15
    16    17    18

>> a-b

ans =

   -10    -9    -8
    -7    -6    -5
    -4    -3    -2

>> a.*b

ans =

     0    10    20
    30    40    50
    60    70    80

>> a./b

ans =

         0    0.1000    0.2000
    0.3000    0.4000    0.5000
    0.6000    0.7000    0.8000
>> a = [0.25,  1.33,  1,  100]

a =

    0.2500    1.3300    1.0000  100.0000

>> 1./a

ans =

    4.0000    0.7519    1.0000    0.0100

>> a = [10,100,1000]

a =

          10         100        1000

>> a.^2

ans =

         100       10000     1000000
>> a

a =

          10         100        1000

>> b

b =

     1     2     3

>> a.^b

ans =

                        10                     10000                1000000000

>> a=[10,20,30]

a =

    10    20    30

>> b=[3,5,7]

b =

     3     5     7

>> 
>> mod(a, b)

ans =

     1     0     2

统计函数 

# Python
>>> import numpy as np
>>> a = np.array([[3,7,5],[8,4,3],[2,4,9]])
>>> a
array([[3, 7, 5],
       [8, 4, 3],
       [2, 4, 9]])
>>> np.amin(a, 1)
array([3, 3, 2])
>>> np.amin(a, 0)
array([2, 4, 3])
>>> np.amax(a, 1)
array([7, 8, 9])
>>> np.amax(a, 0)
array([8, 7, 9])
>>> np.ptp(a)
7
>>> np.ptp(a, 0)
array([6, 3, 6])
>>> np.ptp(a, 1)
array([4, 5, 7])
>>> a = np.array([[10, 7, 4], [3, 2, 1]])
>>> a
array([[10,  7,  4],
       [ 3,  2,  1]])
>>> np.percentile(a, 50)
3.5
>>> a = np.array([[30,65,70],[80,95,10],[50,90,60]])
>>> np.median(a)
65.0
>>> a = np.array([[1,2,3],[3,4,5],[4,5,6]])
>>> np.mean(a)
3.6666666666666665
>>> a = np.array([1,2,3,4])
>>> np.average(a)
2.5
>>> np.std([1, 2, 3, 4])
1.118033988749895
>>> np.var([1, 2, 3, 4])
1.25

----------------------------------------------------------------------------------------
% MATLAB
>> a

a =

     3     7     5
     8     4     3
     2     4     9

>> min(a, [], 2)

ans =

     3
     3
     2

>> min(a, [], 1)

ans =

     2     4     3

>> max(a, [], 2)

ans =

     7
     8
     9

>> max(a, [], 1)

ans =

     8     7     9
>> max(max(a)) - min(min(a))

ans =

     7
>> max(a, [], 1) - min(a, [], 1)

ans =

     6     3     6

>> max(a, [], 2) - min(a, [], 2)

ans =

     4
     5
     7
>> a = [10, 7, 4, 3, 2, 1]

a =

    10     7     4     3     2     1

>> prctile(a,50)

ans =

    3.5000
>> a = [30,65,70,80,95,10,50,90,60]

a =

    30    65    70    80    95    10    50    90    60

>> median(a)

ans =

    65

>> a = [[1,2,3],[3,4,5],[4,5,6]]

a =

     1     2     3     3     4     5     4     5     6

>> mean(a)

ans =

    3.6667
>> std([1, 2, 3, 4], 1)

ans =

    1.1180

>> var([1, 2, 3, 4], 1)

ans =

    1.2500
>> std([1, 2, 3, 4], 0)

ans =

    1.2910

>> var([1, 2, 3, 4], 0)

ans =

    1.6667

线性代数运算 

# Python
>>> a = np.array([[1,2],[3,4]])
>>> b = np.array([[11,12],[13,14]])
>>> np.dot(a, b)
array([[37, 40],
       [85, 92]])
>>> np.vdot(a, b)
130
>>> np.inner(np.array([1,2,3]),np.array([0,1,0]))
2
>>> a = [[1,0],[0,1]]
>>> b = [[4,1],[2,2]]
>>> a
[[1, 0], [0, 1]]
>>> b
[[4, 1], [2, 2]]
>>> np.matmul(a, b)
array([[4, 1],
       [2, 2]])
>>> np.linalg.det(a)
-2.0000000000000004
>>> x = np.array([[1,2],[3,4]])
>>> np.linalg.inv(x)
array([[-2. ,  1. ],
       [ 1.5, -0.5]])

----------------------------------------------------------------------------------------
% MATLAB
>> a = [1, 2; 3, 4]

a =

     1     2
     3     4

>> b = [11, 12; 13, 14]

b =

    11    12
    13    14

>> a*b

ans =

    37    40
    85    92
>> sum(sum(a.*b))

ans =

   130
>> sum([1, 2, 3].*[0, 1, 0])

ans =

     2
>> a = [1, 0; 0, 1]

a =

     1     0
     0     1

>> b = [4, 1; 2, 2]

b =

     4     1
     2     2

>> a*b

ans =

     4     1
     2     2
>> a = [1, 2; 3, 4]

a =

     1     2
     3     4

>> det(a)

ans =

    -2
>> a

a =

     1     2
     3     4

>> inv(a)

ans =

   -2.0000    1.0000
    1.5000   -0.5000

多项式计算 

# Python
>>> import numpy as np
>>> np.poly1d([1, 2, 3])* np.poly1d([4, 5, 6])
poly1d([ 4, 13, 28, 27, 18])
>>> f = np.poly1d(np.array([1,0,-2,1]))
>>> type(f)
<class 'numpy.poly1d'>
>>> f.deriv()
poly1d([ 3,  0, -2])
>>> f.integ()
poly1d([ 0.25,  0.  , -1.  ,  1.  ,  0.  ])
>>> np.roots(f)
array([-1.61803399,  1.        ,  0.61803399])
>>> x = np.linspace(-np.pi/2,np.pi/2, 1000)
>>> y = np.sin(x)
>>> np.polyfit(x, y, 3)
array([-1.45021094e-01,  0.00000000e+00,  9.88749145e-01,  3.01152295e-17])
>>> np.polyfit(x, y, 4)
array([-2.75722813e-17, -1.45021094e-01,  2.03220330e-16,  9.88749145e-01,
       -3.79170015e-16])
>>> np.polyfit(x, y, 5)
array([ 7.57279944e-03, -2.75699842e-17, -1.65823793e-01,  3.11560173e-17,
        9.99770071e-01, -3.87238941e-18])

-------------------------------------------------------------------------------------------
% MATLAB
>> conv([1, 2, 3], [4, 5, 6])

ans =

     4    13    28    27    18
>> polyder(a)

ans =

     3     0    -2

>> polyint(a)

ans =

    0.2500         0   -1.0000    1.0000         0
>> roots(a)

ans =

   -1.6180
    1.0000
    0.6180
>> x= linspace(-pi/2, pi/2, 1000);
>> y=sin(x);
>> polyfit(x, y, 3)

ans =

   -0.1450   -0.0000    0.9887   -0.0000

>> polyfit(x, y, 4)

ans =

    0.0000   -0.1450   -0.0000    0.9887    0.0000

>> polyfit(x, y, 5)

ans =

    0.0076    0.0000   -0.1658   -0.0000    0.9998    0.0000

猜你喜欢

转载自blog.csdn.net/TomorrowAndTuture/article/details/106249179