Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 矩阵乘法:
- $$
- \mathbf{A}_{m\times n} =
- \left[\begin{matrix}
- 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{matrix}\right], \quad
- \mathbf{B}_{n\times p} =
- \left[\begin{matrix}
- b_{11} & b_{12} & \cdots & b_{1p} \\
- b_{21} & b_{22} & \cdots & b_{2p} \\
- \vdots & \vdots & \ddots & \vdots \\
- b_{n1} & b_{n2} & \cdots & b_{np} \\
- \end{matrix}\right] \\[4em]
- \mathbf{C}_{m\times n} = \mathbf{A}_{m\times n} \mathbf{B}_{n\times p} =
- \left[\begin{matrix}
- \sum\limits_{k=1}^n a_{1k}b_{k1} & \sum\limits_{k=1}^n a_{1k}b_{k2} & \cdots & \sum\limits_{k=1}^n a_{1k}b_{kp} \\
- \sum\limits_{k=1}^n a_{2k}b_{k1} & \sum\limits_{k=1}^n a_{2k}b_{k2} & \cdots & \sum\limits_{k=1}^n a_{2k}b_{kp} \\
- \vdots & \vdots & \ddots & \vdots \\
- \sum\limits_{k=1}^n a_{mk}b_{k1} & \sum\limits_{k=1}^n a_{mk}b_{k2} & \cdots & \sum\limits_{k=1}^n a_{mk}b_{kp} \\
- \end{matrix}\right]
- \Longrightarrow
- C_{ij} = \sum_{k=1}^n a_{ik}b_{kj}
- $$
- ```Python
- from copy import deepcopy
- from random import randint
- import numpy as np
- N = 3
- P = 5
- m = n = p = N
- mat = [[randint(-5, 5) for n in range(N)] for m in range(N)]
- print(f'M_{{{m}x{n}}} = ')
- print(np.matrix(mat))
- mul = deepcopy(mat)
- for _ in range(P - 1):
- _mat = deepcopy(mat)
- # 提供 a_{ik}
- for i in range(m):
- # 提供 b_{kj}
- for j in range(p):
- mat[i][j] = sum(_mat[i][k] * mul[k][j] for k in range(n))
- print(f'M ^ {P} = ')
- print(np.matrix(mat))
- print(f'(numpy) M ^ {P} = ')
- print(np.matrix(mul) ** P)
- ```
- ```
- M_{3x3} =
- [[ 4 1 -5]
- [ 5 5 0]
- [-3 -3 -1]]
- M ^ 5 =
- [[ 24129 15921 -12300]
- [ 31530 20820 -16025]
- [-16995 -11226 8624]]
- (numpy) M ^ 5 =
- [[ 24129 15921 -12300]
- [ 31530 20820 -16025]
- [-16995 -11226 8624]]
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement