在Chapel中创建/操纵对角矩阵

时间:2017-08-18 17:46:49

标签: chapel

我有一个方阵A

use LinearAlgebra;    
proc main() {
  var A = Matrix(
       [4.0, 0.8, 1.1, 0.0, 2.0]
      ,[0.8, 9.0, 1.3, 1.0, 0.0]
      ,[1.1, 1.3, 1.0, 0.5, 1.7]
      ,[0.0, 1.0, 0.5, 4.0, 1.5]
      ,[2.0, 0.0, 1.7, 1.5, 16.0]
      );
}

我想构建对角矩阵D = 1/sqrt(a_ii)。似乎我必须提取对角线,然后对每个元素进行操作。我希望这个矩阵非常大且稀疏,如果这改变了答案。

3 个答案:

答案 0 :(得分:2)

以下是使用LinearAlgebra中的1.16 (pre-release)模块的解决方案:

use LinearAlgebra;

var A = Matrix(
               [4.0, 0.8, 1.1, 0.0, 2.0],
               [0.8, 9.0, 1.3, 1.0, 0.0],
               [1.1, 1.3, 1.0, 0.5, 1.7],
               [0.0, 1.0, 0.5, 4.0, 1.5],
               [2.0, 0.0, 1.7, 1.5, 16.0]
              );

var S = sqrt(1.0/diag(A));

// Type required because of promotion-flatting
// See Linear Algebra documentation for more details..
var B: A.type = diag(S);

writeln(B);

答案 1 :(得分:1)

您尝试过这种方法吗?

use Math;
var D: [A.domain];
forall      i in D.dim( 1 ) {
        D[i,i] = 1 / Math.sqrt( A[i,i] ); // ought get fused-DIV!0 protection
}

(ATM <TiO>-IDE目前尚未完全发挥 LinearAlgebra 套餐的功能,因此无法向您展示结果,但希望您能享受前进的方式)

答案 2 :(得分:1)

这里有一些代码可以在1.15版本中使用稀疏对角线数组,而不支持线性代数库:

config const n = 10;   // problem size; override with --n=1000 on command-line

const D = {1..n, 1..n},                        // dense/conceptual matrix size
      Diag: sparse subdomain(D) = genDiag(n);  // sparse diagonal matrix

// iterator that yields indices describing the diagonal
iter genDiag(n) {
  for i in 1..n do
    yield (i,i);
}

// sparse diagonal matrix
var DiagMat: [Diag] real;

// assign sparse matrix elements in parallel
forall ((r,c), elem) in zip(Diag, DiagMat) do
  elem = r + c/10.0;

// print sparse matrix elements serially
for (ind, elem) in zip(Diag, DiagMat) do
  writeln("A[", ind, "] is ", elem);

// dense array
var Dense: [D] real;

// assign from sparse to dense
forall ij in D do
  Dense[ij] = DiagMat[ij];

// print dense array
writeln(Dense);