Inner
Inner
两个矩阵的内积。
matrix matrix::Inner(
const matrix& b // 第二个矩阵
);参数
- b
[输入] 矩阵。
返回值
矩阵。
注意
两个向量的内积是两个向量的点积 vector::Dot()。
以 MQL5 实现的两个矩阵内积的简单算法:
bool MatrixInner(matrix& c, const matrix& a, const matrix& b)
{
//--- 列数必须相等
if(a.Cols()!=b.Cols())
return(false);
//--- 所得矩阵的大小取决于每个矩阵中向量的数量
ulong rows=a.Rows();
ulong cols=b.Rows();
matrix result(rows,cols);
//---
for(ulong i=0; i<rows; i++)
{
vector v1=a.Row(i);
for(ulong j=0; j<cols; j++)
{
vector v2=b.Row(j);
result[i][j]=v1.Dot(v2);
}
}
//---
c=result;
return(true);
}MQL5 示例:
matrix a={{0,1,2},{3,4,5}};
matrix b={{0,1,2},{3,4,5},{6,7,8}};
matrix c=a.Inner(b);
Print(c);
matrix a1={{0,1,2}};
matrix c1=a1.Inner(b);
Print(c1);
/*
[[5,14,23]
[14,50,86]]
[[5,14,23]]
*/Python 示例:
import numpy as np
A = np.arange(6).reshape(2, 3)
B = np.arange(9).reshape(3, 3)
A1= np.arange(3)
print(np.inner(A, B))
print("");
print(np.inner(A1, B))
import numpy as np
A = np.arange(6).reshape(2, 3)
B = np.arange(9).reshape(3, 3)
A1= np.arange(3)
print(np.inner(A, B))
print("");
print(np.inner(A1, B))