Chapel:你能重新索引一个域吗?

时间:2017-08-31 23:14:48

标签: chapel

一位伟大的人曾经说过,我有一个矩阵A。但这次她有一个朋友B。像Montagues和Capulets一样,它们有不同的领域。

// A.domain is { 1..10, 1..10 }
// B.domain is { 0.. 9, 0.. 9 }

for ij in B.domain {
  if B[ij] <has a condition> {
    // poops
    A[ij] = B[ij];
  }
}

我的猜测是我需要重新索引,以便B.domain{1..10, 1..10}。由于B是一个输入,我从编译器推回。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

reindex数组方法可以完成此操作,您可以为结果创建ref以防止创建新数组:

var Adom = {1..10,1..10},
    Bdom = {0..9, 0..9};

var A: [Adom] real,
    B: [Bdom] real;

// Set B to 1.0
B = 1;

// 0-based reference to A -- note that Bdom must be same shape as Adom
ref A0 = A.reindex(Bdom);

// Set all of A's values to B's values
for ij in B.domain {
  A0[ij] = B[ij];
}

// Confirm A is now 1.0 now
writeln(A);

答案 1 :(得分:0)

编译器必须对象,文档明确且清楚:

  

请注意,通过 .domain 方法或函数参数查询语法查询数组的域会导致域表达式可以被重新分配。特别是,我们做不到:

     

<强> VarArr.domain = {1..2*n};

如果 <has_a_condition> 不干预并且没有副作用,表达式的解决方案可能会使用类似于此纯连续域索引转换的域运算符:

forall ij in B.domain do {
   if <has_a_condition> {
      A[ ij(1) + A.domain.dims()(1).low,
         ij(2) + A.domain.dims()(2).low
         ] = B[ij];
   }
}
相关问题