python实现基本的矩阵运算

首先声明矩阵的构建运算均在numpy模块中由相应的函数,而本文的目的主要是因为闲的无聊

文章目录

矩阵及其运算

什么是矩阵?

  • m×nm{\times}nm×n个  aija_{ij}aij​(i=1,2,{\cdots}⋯,m;j=1,2,{\cdots}⋯,n) 数排列成的m行n列的数表

A=[a11a12a1na21a22a2nam1am2amn]Amn 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=⎣⎢⎢⎢⎡​a11​a21​⋮am1​​a12​a22​⋮am2​​⋯⋯⋱⋯​a1n​a2n​⋮amn​​⎦⎥⎥⎥⎤​记作Am∗n​

这m×n个数称为矩阵A的 元素(元)

矩阵基本运算

加法

  • 两个m×nm{\times}nm×n的矩阵相加,即对应元素相加

[a11a12a1na21a22a2nam1am2amn]+[b11b12b1nb21b22b2nbm1bm2bmn]=[a11+b11a12+b12a1n+b1na21+b21a22+b22a2n+b2nam1+bm1am2+bm2amn+bmn] \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} ⎣⎢⎢⎢⎡​a11​a21​⋮am1​​a12​a22​⋮am2​​⋯⋯⋱⋯​a1n​a2n​⋮amn​​⎦⎥⎥⎥⎤​+⎣⎢⎢⎢⎡​b11​b21​⋮bm1​​b12​b22​⋮bm2​​⋯⋯⋱⋯​b1n​b2n​⋮bmn​​⎦⎥⎥⎥⎤​=⎣⎢⎢⎢⎡​a11​+b11​a21​+b21​⋮am1​+bm1​​a12​+b12​a22​+b22​⋮am2​+bm2​​⋯⋯⋱⋯​a1n​+b1n​a2n​+b2n​⋮amn​+bmn​​⎦⎥⎥⎥⎤​

加法满足:

  1. A+B=B+AA + B = B + AA+B=B+A (交换律)
  2. (A+B)+C=A+(B+C)(A + B) + C = A + (B + C)(A+B)+C=A+(B+C) (结合律)

数乘

  • λ\lambdaλ与矩阵AAA的乘积记作λA\lambda AλA或AλA \lambda

λA=Aλ=[λa11λa12λa1nλa21λa22λa2nλam1λam2λamn] \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​​⎦⎥⎥⎥⎤​

数乘满足:

  1. (λμ)A=λ(μA)(\lambda \mu)A = \lambda (\mu A)(λμ)A=λ(μA) λμ\lambda \muλμ为常数
  2. (λμ)A=λA+μA(\lambda \mu)A = \lambda A + \mu A(λμ)A=λA+μA
  3. λ(A+B)=λA+λB\lambda (A+B) = \lambda A + \lambda Bλ(A+B)=λA+λB

乘法

Hadamard乘积

  • 两个同型矩阵中对应元素乘积,记作ABA\bigodot BA⨀B

[a11a12a1na21a22a2nam1am2amn]+[b11b12b1nb21b22b2nbm1bm2bmn]=[a11×b11a12×b12a1n×b1na21×b21a22×b22a2n×b2nam1×bm1am2×bm2amn×bmn] \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} ⎣⎢⎢⎢⎡​a11​a21​⋮am1​​a12​a22​⋮am2​​⋯⋯⋱⋯​a1n​a2n​⋮amn​​⎦⎥⎥⎥⎤​+⎣⎢⎢⎢⎡​b11​b21​⋮bm1​​b12​b22​⋮bm2​​⋯⋯⋱⋯​b1n​b2n​⋮bmn​​⎦⎥⎥⎥⎤​=⎣⎢⎢⎢⎡​a11​×b11​a21​×b21​⋮am1​×bm1​​a12​×b12​a22​×b22​⋮am2​×bm2​​⋯⋯⋱⋯​a1n​×b1n​a2n​×b2n​⋮amn​×bmn​​⎦⎥⎥⎥⎤​

点积

  • A=(aij)A=(a_{ij})A=(aij​)是m×sm{\times}sm×s矩阵,B=(bij)B=(b_{ij})B=(bij​)是s×ns{\times}ns×n矩阵
    规定矩阵AAA与矩阵BBB的乘积m×nm{\times}nm×n矩阵C=(cij)C=(c_{ij})C=(cij​),其中:

cij=ai1b1j+ai2b2j++aisbsj=k=1saikbkj(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​=ai1​b1j​+ai2​b2j​+⋯+ais​bsj​=k=1∑s​aik​bkj​(i=1,2,⋯,m;j=1,2,⋯,n)​

矩阵点积满足:

  1. (AB)C=A(BC)(AB)C=A(BC)(AB)C=A(BC)
  2. λ(AB)=(λA)B=A(λB)\lambda (AB) = (\lambda A)B = A(\lambda B)λ(AB)=(λA)B=A(λB)
  3. A(B+C)=AB+AC      (B+C)A=BA+CAA(B+C) = AB + AC \;\;\;(B+C)A = BA + CAA(B+C)=AB+AC(B+C)A=BA+CA
  4. ABBAAB\neq BAAB​=BA
矩阵相乘不满足交换律

python实现矩阵基本运算思路及代码

环境

Anaconda 3 + Python 3.6.5 + Jupyter

模块导入

import numpy as np

加法

思路

  • 参考矩阵加法公式
  1. 先判断两矩阵能否相加
  2. 遍历两个矩阵
  3. 各相同位置元素相加

实现

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

数乘

思路

  • 参考矩阵数乘公式
  1. 遍历矩阵
  2. 相乘

实现

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乘积公式

与矩阵加法思路一致

实现

# 矩阵对应元素相乘 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列对应元素乘积之和。

  1. 判断两矩阵是否为同型矩阵
  2. 构建m行n列的结果矩阵
  3. 向结果矩阵中赋值:
    (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
上一篇:Centos7安装go环境


下一篇:Ubuntu20.04 安装星火商店的各种问题