[英]Make efficient - A symmetric matrix multiplication with two vectors in c#
As per following the inital thread make efficient the copy of symmetric matrix in c-sharp from cMinor.按照初始线程,从 cMinor 复制 c-sharp 中的对称矩阵。
I would be quite interesting with some inputs in how to build a symmetric square matrix multiplication with one line vector and one column vector by using an array implementation of the matrix, instead of the classical对于如何使用矩阵的数组实现而不是经典的矩阵实现来构建具有一个线向量和一个列向量的对称方阵乘法的一些输入,我会非常有趣
long s = 0;
List<double> columnVector = new List<double>(N);
List<double> lineVector = new List<double>(N);
//- init. vectors and symmetric square matrix m
for (int i=0; i < N; i++)
{
for(int j=0; j < N; j++){
s += lineVector[i] * columnVector[j] * m[i,j];
}
}
Thanks for your input !感谢您的输入 !
The line vector times symmetric matrix equals to the transpose of the matrix times the column vector.线向量乘以对称矩阵等于矩阵乘以列向量的转置。 So only the column vector case needs to be considered.
所以只需要考虑列向量的情况。
Originally the i
-th element of y=A*x
is defined as最初
y=A*x
第i
个元素被定义为
y[i] = SUM( A[i,j]*x[j], j=0..N-1 )
but since A
is symmetric, the sum be split into sums, one below the diagonal and the other above但由于
A
是对称的,所以总和被分成多个总和,一个在对角线下方,另一个在对角线上方
y[i] = SUM( A[i,j]*x[j], j=0..i-1) + SUM( A[i,j]*x[j], j=i..N-1 )
From the other posting the matrix index is从另一个发布矩阵索引是
A[i,j] = A[i*N-i*(i+1)/2+j] // j>=i
A[i,j] = A[j*N-j*(j+1)/2+i] // j< i
For a N×N
symmetric matrix A = new double[N*(N+1)/2];
对于
N×N
对称矩阵A = new double[N*(N+1)/2];
In C#
code the above is:在
C#
代码中,上面是:
int k;
for(int i=0; i<N; i++)
{
// start sum with zero
y[i]=0;
// below diagonal
k=i;
for(int j=0; j<=i-1; j++)
{
y[i]+=A[k]*x[j];
k+=N-j-1;
}
// above diagonal
k=i*N-i*(i+1)/2+i;
for(int j=i; j<=N-1; j++)
{
y[i]+=A[k]*x[j];
k++;
}
}
Example for you to try:您可以尝试的示例:
| -7 -6 -5 -4 -3 | | -2 | | -5 |
| -6 -2 -1 0 1 | | -1 | | 21 |
| -5 -1 2 3 4 | | 0 | = | 42 |
| -4 0 3 5 6 | | 1 | | 55 |
| -3 1 4 6 7 | | 7 | | 60 |
To get the quadratic form do a dot product with the multiplication result vector x·A·y = Dot(x,A*y)
要获得二次形式,请与乘法结果向量
x·A·y = Dot(x,A*y)
进行点积
You could make matrix multiplication pretty fast with unsafe code.您可以使用不安全的代码使矩阵乘法非常快。 I have blogged about it .
我已经写了关于它的博客。
Making matrix multiplication as fast as possible is easy: Use a well-known library.尽可能快地进行矩阵乘法很容易:使用众所周知的库。 Insane amounts of performance work has gone into such libraries.
疯狂的性能工作已经投入到这些库中。 You cannot compete with that.
你无法与之竞争。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.