高效率地在C#中对称矩阵的副本 [英] make efficient the copy of symmetric matrix in c#

查看:154
本文介绍了高效率地在C#中对称矩阵的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个数组来存储一个对称矩阵

I want to store in an array a symmetric matrix

有关矩阵我这样做

    double[,] mat = new double[size,size];
    for (int i = 0; i < size; i++)
    {
      for (int j = 0; j <= i; j++)
           mat[i, j] = mat[j, i] = (n * other_matrix[i,j]);
    }

如果我想在一个数组来存储

If I want to store in an array

double[] mat = new double[size*size];

而不是

 double[,] mat

什么是最有效的方法?

What would be the most efficient way?

使用垫[我* N + J]

推荐答案

存储按行的元素,其中 I 行对应和Ĵ -th列存储索引 K = I * NC + J NC 列数。这适用于一个非对称通用矩阵

Store the elements by row, where the i-th row and j-th column is stored in index k=i*NC+j with NC the number of columns. This applies to a non-symmetric general matrix.

要存储大小 N 你只需要 N *(N + 1)/ 2 元素的对称矩阵在该阵列。你可以假设 I&LT; = j的使得数组索引是这样的:

To store a symmetric matrix of size N you only need N*(N+1)/2 elements in the array. You can assume that i<=j such that the array indexes go like this:

k(i,j) = i*N-i*(i+1)/2+j            i<=j  //above the diagonal
k(i,j) = j*N-j*(j+1)/2+i            i>j   //below the diagonal

i = 0 .. N-1
j = 0 .. N-1

例如,当N = 5,数组的索引是这样的。

Example when N=5, the array indexes go like this

| 0   1   2   3   4 |
|                   |
| 1   5   6   7   8 |
|                   |
| 2   6   9  10  11 |
|                   |
| 3   7  10  12  13 |
|                   |
| 4   8  11  13  14 |

所需的总元素是 5 *(5 + 1)/ 2 = 15 ,因此,从指标走 0..14

The total elements needed are 5*(5+1)/2 = 15 and thus the indexes go from 0..14.

I -th对角线具有指数 K(I,I)= I *(N + 1)* -i(I + 1 )/ 2 。因此,第三行( I = 2 )的对角线指数 K(2,2)= 2 *(5 + 1)-2 *(2 +1)/ 2 = 9

The i-th diagonal has index k(i,i) = i*(N+1)-i*(i+1)/2. So the 3rd row (i=2) has diagonal index k(2,2) = 2*(5+1)-2*(2+1)/2 = 9.

的最后一个元素我行对应的索引= K(I,N)= N *(I + 1)-i *(I + 1)/ 2-1 。所以第三排的最后一个元素是 K(2,4)= 5 *(2 + 1)-2 *(2 + 1)/ 2-1 = 11

The last element of the i-th row has index = k(i,N) = N*(i+1)-i*(i+1)/2-1. So the last element of the 3rd row is k(2,4) = 5*(2+1)-2*(2+1)/2-1 = 11.

这可能需要的最后一部分是如何从数组索引 K 去行 I 和列Ĵ。再假设 I&LT; = j的(上面的对角线)的答案

The last part that you might need is how to go from the array index k to the row i and column j. Again assuming that i<=j (above the diagonal) the answer is

i(k) = (int)Math.Floor(N+0.5-Math.Sqrt(N*(N+1)-2*k+0.25))
j(k) = k + i*(i+1)/2-N*i

要检查我运行此为 N = 5 K = 0..14 ,并得到了上述结果如下:

To check the above I run this for N=5, k=0..14 and got the following results:

这是正确的!

Which is correct!

要使复印就用 Array.Copy()上是超级快的元素。还做运算,如加法和缩放,你只需要在数组中的元素减少工作,而不是完整的 N×N个矩阵。矩阵乘法是有点棘手,但可行的。也许你可以问另外一个问题这个,如果你想要的。

To make the copy then just use Array.Copy() on the elements which is super fast. Also to do operations such as addition and scaling you just need to work on the reduced elements in the array, and not on the full N*N matrix. Matrix multiplication is a little tricky, but doable. Maybe you can ask another question for this if you want.

这篇关于高效率地在C#中对称矩阵的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆