CH1 Numpy

2017-06-07  本文已影响0人  Trancer95

1.7        Getting started with the NumPy library

① Random Array

>>> from numpy import*

>>> random.rand(4,4)

array([[ 0.43831197,  0.35192743,  0.41718421,  0.02885252],

[ 0.38820193,  0.6842994 ,  0.56657079,  0.6378907 ],

[ 0.7795009 ,  0.67264657,  0.84689064,  0.38003772],

[ 0.15846023,  0.42460139,  0.60844758,  0.88809248]])

②Convert  an array to a matrix

>>> randMat = mat(random.rand(4,4))

>>> invRandMat = randMat.I

The ".I" operator  solves the inverse of a matrix.

>>> randMat

matrix([[ 0.96119052,  0.65586165,  0.9050705 ,  0.82003587],

[ 0.09912414,  0.27526886,  0.04278784,  0.53352162],

[ 0.43153359,  0.89609418,  0.70573325,  0.4582519 ],

[ 0.10391469,  0.92706015,  0.47706808,  0.16927545]])

>>> randMat.I

matrix([[  8.78378885,  -0.24249478, -20.47960842,  13.65333088],

[  3.84524822,  0.61977968, -10.7757186 ,  8.59005951],

[ -8.3398511 ,  -1.76950141,  22.72457239, -15.53996251],

[ -2.94705669,  1.74153052,  7.54216509,  -5.7224096 ]])

>>> randMat*invRandMat

matrix([[  1.00000000e+00,  -3.41706192e-17,  -5.84419709e-16,

3.03544577e-15],

[  1.17655922e-16,  1.00000000e+00,  -1.16736773e-15,

9.32123779e-16],

[ -7.81192093e-16,  -6.74387259e-17,  1.00000000e+00,

-1.49237318e-15],

[ -3.48749572e-16,  4.46109067e-17,  -1.61100173e-15,

1.00000000e+00]])

>>> myEye=randMat.I*invRandMat

>>> myEye - eye(4)

matrix([[ 205.78223862,  57.73607224, -539.69016914,  357.9673263 ],

[ 100.7115514 ,  32.47917437, -265.51369969,  176.12281464],

[-223.78206269,  -66.34881268,  588.06576143, -393.27984115],

[ -65.22597483,  -21.51761168,  169.82144272, -110.73628032]])

The function eye(4) just creates an identify matrix of size 4.

上一篇下一篇

猜你喜欢

热点阅读