python中的@
方法,也就是matmul
方法,可以实现矩阵乘法。
矩阵乘法
使用dot函数
import np
S = np.dot((np.dot(H, beta) - r).T,
np.dot(inv(np.dot(np.dot(H, V), H.T)), np.dot(H, beta) - r))
使用dot方法
import np
S = (H.dot(beta) - r).T.dot(inv(H.dot(V).dot(H.T))).dot(H.dot(beta) - r)
使用@
后的矩阵乘法,更加简洁
S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)
>>> import numpy as np
>>> x = np.ones(3)
>>> x
array([ 1., 1., 1.])
>>> m = np.eye(3)
>>> m
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> x @ m
array([ 1., 1., 1.])
参考:
https://stackoverflow.com/questions/27385633/what-is-the-symbol-for-in-python