如何在教堂

时间:2018-01-15 22:59:57

标签: sparse-matrix chapel

一个智者曾经开始,我有这个矩阵A ...(在这里称为" W")

use LinearAlgebra, 
    LayoutCS,
    LinearAlgebra.Sparse;

var nv: int = 8,
    D = {1..nv, 1..nv},
    SD: sparse subdomain(D) dmapped CS(),
    W: [SD] real;

SD += (1,2); W[1,2] = 1.0;
SD += (1,3); W[1,3] = 1.0;
SD += (1,4); W[1,4] = 1.0;
SD += (2,2); W[2,2] = 1.0;
SD += (2,4); W[2,4] = 1.0;
SD += (3,4); W[3,4] = 1.0;
SD += (4,5); W[4,5] = 1.0;
SD += (5,6); W[5,6] = 1.0;
SD += (6,7); W[6,7] = 1.0;
SD += (6,8); W[6,8] = 1.0;
SD += (7,8); W[7,8] = 1.0;

const a: real = 0.5;

我想构建多项式

const P = aW + a^2W^2 + .. + a^kW^k

但是,似乎无法将函数.dot链接起来。是否有明确的方法来构建中间元素?

1 个答案:

答案 0 :(得分:1)

这是实现这一目标的一种方法,但可以通过将多项式计算分开使得每个矩阵幂只计算一次来改进:

use LinearAlgebra, 
    LayoutCS,
    LinearAlgebra.Sparse;

var nv: int = 8,
    D = {1..nv, 1..nv},
    SD: sparse subdomain(D) dmapped CS(),
    W: [SD] real;

SD += (1,2); W[1,2] = 1.0;
SD += (1,3); W[1,3] = 1.0;
SD += (1,4); W[1,4] = 1.0;
SD += (2,2); W[2,2] = 1.0;
SD += (2,4); W[2,4] = 1.0;
SD += (3,4); W[3,4] = 1.0;
SD += (4,5); W[4,5] = 1.0;
SD += (5,6); W[5,6] = 1.0;
SD += (6,7); W[6,7] = 1.0;
SD += (6,8); W[6,8] = 1.0;
SD += (7,8); W[7,8] = 1.0;

const a: real = 0.5;
const polynomial = dot(a, W).plus(dot(a**2, W.dot(W))).plus(dot(a**3, W.dot(W).dot(W)));