Advertisement
shinemic

矩阵乘法说明.md

Jan 22nd, 2022
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. 矩阵乘法:
  2.  
  3. $$
  4. \mathbf{A}_{m\times n} =
  5. \left[\begin{matrix}
  6. a_{11} & a_{12} & \cdots & a_{1n} \\
  7. a_{21} & a_{22} & \cdots & a_{2n} \\
  8. \vdots & \vdots & \ddots & \vdots \\
  9. a_{m1} & a_{m2} & \cdots & a_{mn} \\
  10. \end{matrix}\right], \quad
  11. \mathbf{B}_{n\times p} =
  12. \left[\begin{matrix}
  13. b_{11} & b_{12} & \cdots & b_{1p} \\
  14. b_{21} & b_{22} & \cdots & b_{2p} \\
  15. \vdots & \vdots & \ddots & \vdots \\
  16. b_{n1} & b_{n2} & \cdots & b_{np} \\
  17. \end{matrix}\right] \\[4em]
  18. \mathbf{C}_{m\times n} = \mathbf{A}_{m\times n} \mathbf{B}_{n\times p} =
  19. \left[\begin{matrix}
  20. \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} \\
  21. \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} \\
  22. \vdots & \vdots & \ddots & \vdots \\
  23. \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} \\
  24. \end{matrix}\right]
  25. \Longrightarrow
  26. C_{ij} = \sum_{k=1}^n a_{ik}b_{kj}
  27. $$
  28.  
  29. ```Python
  30. from copy import deepcopy
  31. from random import randint
  32. import numpy as np
  33.  
  34. N = 3
  35. P = 5
  36. m = n = p = N
  37.  
  38. mat = [[randint(-5, 5) for n in range(N)] for m in range(N)]
  39. print(f'M_{{{m}x{n}}} = ')
  40. print(np.matrix(mat))
  41.  
  42. mul = deepcopy(mat)
  43.  
  44. for _ in range(P - 1):
  45. _mat = deepcopy(mat)
  46. # 提供 a_{ik}
  47. for i in range(m):
  48. # 提供 b_{kj}
  49. for j in range(p):
  50. mat[i][j] = sum(_mat[i][k] * mul[k][j] for k in range(n))
  51.  
  52. print(f'M ^ {P} = ')
  53. print(np.matrix(mat))
  54. print(f'(numpy) M ^ {P} = ')
  55. print(np.matrix(mul) ** P)
  56. ```
  57.  
  58. ```
  59. M_{3x3} =
  60. [[ 4 1 -5]
  61. [ 5 5 0]
  62. [-3 -3 -1]]
  63.  
  64. M ^ 5 =
  65. [[ 24129 15921 -12300]
  66. [ 31530 20820 -16025]
  67. [-16995 -11226 8624]]
  68.  
  69. (numpy) M ^ 5 =
  70. [[ 24129 15921 -12300]
  71. [ 31530 20820 -16025]
  72. [-16995 -11226 8624]]
  73. ```
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement