用Armadillo功能替换“稀疏”

Replace `sparse.eye` with an armadillo function

本文关键字:稀疏 替换 Armadillo 功能      更新时间:2023-10-16

我有一个python脚本,我想使用armadillo在C 中重写。在Python中,我有一行

matrix = 1/(12*h)*(sparse.eye(num_points, k = -2, dtype=np.complex).toarray() * 1 + sparse.eye(num_points, k = -1, dtype=np.complex).toarray() * -8 + sparse.eye(num_points, k = 1, dtype=np.complex).toarray() * 8 + sparse.eye(num_points, k = 2, dtype=np.complex).toarray() * -1)

生成我一个矩阵,其中所有值除了五个主角符为零。不幸的是,我在armadillo中找不到类似的功能,目前我能看到的最大的方式是创建一个ones() -matrix,然后用.diag()设置对角线,然后将其余的零。有一个更简单的方法吗?

以下代码应具有等效功能:

// sparse matrices have all values as zero at initialization
sp_mat X(10,10);
X.diag(-2).fill( 1);
X.diag(-1).fill(-8);
X.diag(+1).fill( 8);
X.diag(+2).fill(-1);
X *= 1.0 / (12*h);  // the .0 in 1.0 tells the compiler to use the double type