- 问题:鸡兔同笼,10个头,28只脚,问鸡兔各有多少只
- 解答:列二元一次方程,鸡x只,兔有y只
x+y=10 2x+4y=28
- 线性方程组
在这里插入代码片
import numpy as np
A=np.array([[1,1],[2,4]])
b=np.array([10,28])
x=np.linalg.solve(A,b)
print('线性方程组的解为:',x)
D:\why\python.exe D:/opencv-python/xianxingdaishu.py 线性方程组的解为: [6. 4.]
4.问题:后来又引入了公鸡和鸭子,之后共14头,40脚,眼睛28,问鸡兔鸭各有几只 假设鸭z只 方程组: x+y+z=14 2x+4y+2z=40 2x+2y+2z=28(没用的) 5、插入:生成向量,向量加法,数乘
import numpy as np
x=np.array([1,2,3])
y=np.array([4,5,6])
print("x={},y={}".format(x,y))
print('x的维度为{}'.format(x.shape))
print('x+y={]'.format(x+y))
k=3
print('kx={}'.format(k*x))
print('3x+2y={}'.format(3*x+2*y))
D:\why\python.exe D:/opencv-python/xianxingdaishu.py x=[1 2 3],y=[4 5 6] x的维度为(3,) x+y=[5 7 9] kx=[3 6 9] 3x+2y=[11 16 21]
判断一个方程有唯一解:
6.插入:行列式
import numpy as np
A=np.array([[1,1,1],[2,4,2],[2,2,2]])
A_det=np.linalg.det(A)
print('A的行列式值为',A_det)
B=np.array([[1,1,1,1],[1,2,0,0],[1,0,3,0],[1,0,0,4]])
B_det=np.linalg.det(B)
print('B的行列式值为',B_det)
D:\why\python.exe D:/opencv-python/jj.py A的行列式值为 0.0 B的行列式值为 -2.0
7、克莱默法则 结论:2阶行列式是由2维向量组成的,结果为已这两个向量为邻边的平行四边形面积
import numpu as np
D=np.array([[2.,1,-5,1],[1,-3,0,-6],[0,2,-1,2],[1,4,-7,6]])
D_det=np.linalg.det(D)
D1=np.array([[8.,1,-5,1],[9,-3,0,-6],[-5,2,-1,2],[0,4,-7,6]])
D1_det=np.linalg.det(D1)
D2=np.array([[2.,8,-5,1],[1,9,0,-6],[0,-5,-1,2],[1,0,-7,6]])
D2_det=np.linalg.det(D2)
D3=np.array([[2.,1,8,1],[1,3,9,-6],[0,2,-15,2],[1,4,0,6]])
D1_det=np.linalg.det(D3)
D4=np.array([[2.,1,-5,8],[1,-3,0,9],[0,2,-1,-5],[1,4,-7,0]])
D4_det=np.linalg.det(D4)
x1=D1_det/D_det
x2=D2_det/D_det
x3=D3_det/D_det
x4=D4_det/D_det
print('克莱姆法则解线性微分方程组的解为\n x1={:.2f},\n x2={:.2f},\n x3={:.2f},\n x4={:.2f}'.format(x1,x2,x3,x4))
8、矩阵
import numpy as np
A=np.array([[1,2],[1,-1]])
B=np.array([[1,2,-3],[-1,1,2]])
print('A规模{}'.format(A.shape))
print('B规模{}'.format(B.shape))
print('AB=\n{}'.format(np.matmul(A,B)))
8.1单位矩阵
import numpy as np
print('B=\n',B,'\n','E=\n',np.eye(3))
np.matmul(B,np.eye(3))
8.2初等矩阵
import numpy as np
A=np.array([[1,1,1],[2,4,2]])
print('A=\n',A)
8.3交换矩阵的两行
import numpy as np
A=np.array([[0,1],[1,0]])
np.matmul(P,A)
8.4`逆矩阵
import numpy as np
A=np.array([[1,0,0],[0,2,0],[0,0,3]])
np.matmul(A,A)
import numpy as np
B=np.array([[0,1],[0,-1]])
print(np.linalg.det(B),'行列式为0,奇异阵')
print(np.linalg.pinv(B))
print(np.matul(np,matmul(B,np,linalg.pinv(B)),B))
注意:向量在空间中的位置是绝对的,而其坐标值却是相对的,坐标的取值依托于所选取的坐标向量(基底)
8.5对角矩阵 并不是所有的矩阵都能相似于对角矩阵
8.6特征值,特征向量,对角化
import numpy as np
A=np.array([[-2,1,1],[0,2,0],[-4,1,3]])
lamb,p=np.linalg.eig(A)
print(lamb)
print(p)
print(np.matmul(np.linalg.inv(p),np.matmul(A,p)))
8.7数值过滤
import numpy as np
res=np.matmul(np.linalg.inv(p),np.matmul(A,p))
res[np.abs(res)<1e-6]=0
print(res)
8.8施密特正交化
import numpy as np
from scipy.linalg import*
A=np.array([[1,2,3],[2,1,3],[3,2,1]])
B=orth(A)
print(np.matmul(B,np.transpose(B))
res=np.matmul(B,np.transpose(B)
res[np.abs(res)<1e-6]=0
print(res)
9.项目实战–基于矩阵变换的图像变换
import numpy as np
from math import cos,sin,pi
def vec_2d(x0,y0,alpha):
origin=np.array([[x0,y0,1]])
Transnp.array([[cos(alpha),-sin(alpha),0],[sin(alpha),cos(alpha),0],[0,0,1]])
res=origin.dot(Trans)
x=
y=
return (x,y)
def Trans(x0,y0,W,H,alpha):
origin=np.array([x0,y0,1])
res = origin.dot(np.array[[cos(alpha),0],
[-sin(alpha),cos(alpha),0],
[-0.5*W*cos(alpha)+0.5*H*sin(alpha)+0.5*W,
-0.5*W*sin(alpha)-0.5*H*cos(alpha)+
0.5*H,1]])
return (int(res[0,:2][0]),int(res[0,:2][1]))
from skimage import io,data
imgs=data.horse()
io.imshow(img3)
img3.shape
img4=np.zeros((400,400))
for x in range(img3.shape[0]):
for y in range(img3.shape[1]):
x1,y1=Trans(x,y,328,400,pi/2)
img4[x1-355,y1]=img3[x,y]
io.imshow(img4)
|