首先声明矩阵的构建运算均在numpy模块中由相应的函数,而本文的目的主要是因为闲的无聊
文章目录
矩阵及其运算
什么是矩阵?
由m × n m{\times}n m×n个 a i j a_{ij} aij(i=1,2,⋯ {\cdots} ⋯,m;j=1,2,⋯ {\cdots} ⋯,n) 数排列成的m行n列的数表
A = [ a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a m 1 a m 2 ⋯ a m n ] 记 作 A m ∗ n
A=\begin{bmatrix}
{a_{11}} & {a_{12}} & {\cdots} & {a_{1n}}\\
{a_{21}} & {a_{22}} & {\cdots} & {a_{2n}}\\
{\vdots} & {\vdots} & {\ddots} & {\vdots}\\
{a_{m1}} & {a_{m2}} & {\cdots} & {a_{mn}}\\
\end{bmatrix}记作{A_{m*n}}
A=⎣⎢⎢⎢⎡a11a21⋮am1a12a22⋮am2⋯⋯⋱⋯a1na2n⋮amn⎦⎥⎥⎥⎤记作Am∗n
矩阵基本运算
加法
两个m × n m{\times}n m×n的矩阵相加,即对应元素相加
[ a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a m 1 a m 2 ⋯ a m n ] + [ b 11 b 12 ⋯ b 1 n b 21 b 22 ⋯ b 2 n ⋮ ⋮ ⋱ ⋮ b m 1 b m 2 ⋯ b m n ] = [ a 11 + b 11 a 12 + b 12 ⋯ a 1 n + b 1 n a 21 + b 21 a 22 + b 22 ⋯ a 2 n + b 2 n ⋮ ⋮ ⋱ ⋮ a m 1 + b m 1 a m 2 + b m 2 ⋯ a m n + b m n ]
\begin{bmatrix}
{a_{11}} & {a_{12}} & {\cdots} & {a_{1n}}\\
{a_{21}} & {a_{22}} & {\cdots} & {a_{2n}}\\
{\vdots} & {\vdots} & {\ddots} & {\vdots}\\
{a_{m1}} & {a_{m2}} & {\cdots} & {a_{mn}}\\
\end{bmatrix}+
\begin{bmatrix}
{b_{11}} & {b_{12}} & {\cdots} & {b_{1n}}\\
{b_{21}} & {b_{22}} & {\cdots} & {b_{2n}}\\
{\vdots} & {\vdots} & {\ddots} & {\vdots}\\
{b_{m1}} & {b_{m2}} & {\cdots} & {b_{mn}}\\
\end{bmatrix}=
\begin{bmatrix}
{a_{11}+b_{11}} & {a_{12}+b_{12}} & {\cdots} & {a_{1n}+b_{1n}}\\
{a_{21}+b_{21}} & {a_{22}+b_{22}} & {\cdots} & {a_{2n}+b_{2n}}\\
{\vdots} & {\vdots} & {\ddots} & {\vdots}\\
{a_{m1}+b_{m1}} & {a_{m2}+b_{m2}} & {\cdots} & {a_{mn}+b_{mn}}\\
\end{bmatrix}
⎣⎢⎢⎢⎡a11a21⋮am1a12a22⋮am2⋯⋯⋱⋯a1na2n⋮amn⎦⎥⎥⎥⎤+⎣⎢⎢⎢⎡b11b21⋮bm1b12b22⋮bm2⋯⋯⋱⋯b1nb2n⋮bmn⎦⎥⎥⎥⎤=⎣⎢⎢⎢⎡a11+b11a21+b21⋮am1+bm1a12+b12a22+b22⋮am2+bm2⋯⋯⋱⋯a1n+b1na2n+b2n⋮amn+bmn⎦⎥⎥⎥⎤
加法满足:
A + B = B + A A + B = B + A A+B=B+A (交换律)
( A + B ) + C = A + ( B + C ) (A + B) + C = A + (B + C) (A+B)+C=A+(B+C) (结合律)
数乘
数λ \lambda λ与矩阵A A A的乘积记作λ A \lambda A λA或A λ A \lambda Aλ
λ A = A λ = [ λ a 11 λ a 12 ⋯ λ a 1 n λ a 21 λ a 22 ⋯ λ a 2 n ⋮ ⋮ ⋱ ⋮ λ a m 1 λ a m 2 ⋯ λ a m n ]
\lambda A = A \lambda=\begin{bmatrix}
{\lambda a_{11}} & {\lambda a_{12}} & {\cdots} & {\lambda a_{1n}}\\
{\lambda a_{21}} & {\lambda a_{22}} & {\cdots} & {\lambda a_{2n}}\\
{\vdots} & {\vdots} & {\ddots} & {\vdots}\\
{\lambda a_{m1}} & {\lambda a_{m2}} & {\cdots} & {\lambda a_{mn}}\\
\end{bmatrix}
λA=Aλ=⎣⎢⎢⎢⎡λa11λa21⋮λam1λa12λa22⋮λam2⋯⋯⋱⋯λa1nλa2n⋮λamn⎦⎥⎥⎥⎤
数乘满足:
( λ μ ) A = λ ( μ A ) (\lambda \mu)A = \lambda (\mu A) (λμ)A=λ(μA) λ μ \lambda \mu λμ为常数
( λ μ ) A = λ A + μ A (\lambda \mu)A = \lambda A + \mu A (λμ)A=λA+μA
λ ( A + B ) = λ A + λ B \lambda (A+B) = \lambda A + \lambda B λ(A+B)=λA+λB
乘法
Hadamard乘积
两个同型矩阵中对应元素乘积,记作A ⨀ B A\bigodot B A⨀B
[ a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a m 1 a m 2 ⋯ a m n ] + [ b 11 b 12 ⋯ b 1 n b 21 b 22 ⋯ b 2 n ⋮ ⋮ ⋱ ⋮ b m 1 b m 2 ⋯ b m n ] = [ a 11 × b 11 a 12 × b 12 ⋯ a 1 n × b 1 n a 21 × b 21 a 22 × b 22 ⋯ a 2 n × b 2 n ⋮ ⋮ ⋱ ⋮ a m 1 × b m 1 a m 2 × b m 2 ⋯ a m n × b m n ]
\begin{bmatrix}
{a_{11}} & {a_{12}} & {\cdots} & {a_{1n}}\\
{a_{21}} & {a_{22}} & {\cdots} & {a_{2n}}\\
{\vdots} & {\vdots} & {\ddots} & {\vdots}\\
{a_{m1}} & {a_{m2}} & {\cdots} & {a_{mn}}\\
\end{bmatrix}+
\begin{bmatrix}
{b_{11}} & {b_{12}} & {\cdots} & {b_{1n}}\\
{b_{21}} & {b_{22}} & {\cdots} & {b_{2n}}\\
{\vdots} & {\vdots} & {\ddots} & {\vdots}\\
{b_{m1}} & {b_{m2}} & {\cdots} & {b_{mn}}\\
\end{bmatrix}=
\begin{bmatrix}
{a_{11}{\times}b_{11}} & {a_{12}{\times}b_{12}} & {\cdots} & {a_{1n}{\times}b_{1n}}\\
{a_{21}{\times}b_{21}} & {a_{22}{\times}b_{22}} & {\cdots} & {a_{2n}{\times}b_{2n}}\\
{\vdots} & {\vdots} & {\ddots} & {\vdots}\\
{a_{m1}{\times}b_{m1}} & {a_{m2}{\times}b_{m2}} & {\cdots} & {a_{mn}{\times}b_{mn}}\\
\end{bmatrix}
⎣⎢⎢⎢⎡a11a21⋮am1a12a22⋮am2⋯⋯⋱⋯a1na2n⋮amn⎦⎥⎥⎥⎤+⎣⎢⎢⎢⎡b11b21⋮bm1b12b22⋮bm2⋯⋯⋱⋯b1nb2n⋮bmn⎦⎥⎥⎥⎤=⎣⎢⎢⎢⎡a11×b11a21×b21⋮am1×bm1a12×b12a22×b22⋮am2×bm2⋯⋯⋱⋯a1n×b1na2n×b2n⋮amn×bmn⎦⎥⎥⎥⎤
点积
A = ( a i j ) A=(a_{ij}) A=(aij)是m × s m{\times}s m×s矩阵,B = ( b i j ) B=(b_{ij}) B=(bij)是s × n s{\times}n s×n矩阵 规定矩阵A A A与矩阵B B B的乘积m × n m{\times}n m×n矩阵C = ( c i j ) C=(c_{ij}) C=(cij),其中:
c i j = a i 1 b 1 j + a i 2 b 2 j + ⋯ + a i s b s j = ∑ k = 1 s a i k b k j ( i = 1 , 2 , ⋯ , m ; j = 1 , 2 , ⋯ , n )
\boxed{c_{ij} = a_{i1}b_{1j}+a_{i2}b_{2j}+{\cdots}+a_{is}b_{sj}\\
\quad =\displaystyle \sum^{s}_{k=1}{a_{ik}b_{kj}}(i=1,2,{\cdots},m;j=1,2,{\cdots},n)}
cij=ai1b1j+ai2b2j+⋯+aisbsj=k=1∑saikbkj(i=1,2,⋯,m;j=1,2,⋯,n)
矩阵点积满足:
( A B ) C = A ( B C ) (AB)C=A(BC) (AB)C=A(BC)
λ ( A B ) = ( λ A ) B = A ( λ B ) \lambda (AB) = (\lambda A)B = A(\lambda B) λ(AB)=(λA)B=A(λB)
A ( B + C ) = A B + A C ( B + C ) A = B A + C A A(B+C) = AB + AC \;\;\;(B+C)A = BA + CA A(B+C)=AB+AC(B+C)A=BA+CA
A B ≠ B A AB\neq BA AB=BA
python实现矩阵基本运算思路及代码
环境
Anaconda 3 + Python 3.6.5 + Jupyter
模块导入
import numpy as np
加法
思路
先判断两矩阵能否相加
遍历两个矩阵
各相同位置元素相加
实现
def add(a, b):
if(a.shape != b.shape):
print('两矩阵不为同型矩阵!')
return
c = np.zeros(a.shape)
for i in range(a.shape[0]):
for j in range(a.shape[1]):
c[i][j] = a[i][j] + b[i][j]
return c
数乘
思路
遍历矩阵
相乘
实现
def numSub(a, b):
c = np.zeros(a.shape)
for i in range(a.shape[0]):
for j in range(a.shape[1]):
c[i][j] = a[i][j] * b
return c
Hadamard乘积
思路
与矩阵加法思路一致
实现
# 矩阵对应元素相乘 Hadamard乘积
def had(a,b):
if(a.shape != b.shape):
print('两矩阵不为同型矩阵!')
return
c = np.zeros(a.shape)
for i in range(c.shape[0]):
for j in range(c.shape[1]):
c[i][j] = a[i][j] * b[i][j]
点积
思路
理解公式:乘积C的第m行第n列的元素等于矩阵A的第m行的元素与矩阵B的第n列对应元素乘积之和。
判断两矩阵是否为同型矩阵
构建m行n列的结果矩阵
向结果矩阵中赋值: (1). 遍历结果矩阵 (2). 求出矩阵A对应行与矩阵B对应列的元素乘积之和: 根据公式,A矩阵的行与B矩阵的列均确定,因此根据累加条件遍历两个矩阵来确定另一个索引位置,并累加求和
实现
# 矩阵相乘
def mul(a,b):
if(a.shape[1] != b.shape[0]):
print('两矩阵不能相乘')
return
m = a.shape[0]
n = b.shape[1]
s = a.shape[1]
c = np.zeros((m,n))
for i in range(m):
for j in range(n):
for k in range(s):
c[i][j] += a[i][k]*b[k][j]
pass
pass
return c