将 `sparse.eye` 替换为犰狳函数
Replace `sparse.eye` with an armadillo function
我有一个 python 脚本,我想使用犰狳用 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)
这会生成一个矩阵,其中除五个主对角线外的所有值均为零。不幸的是,我在犰狳中找不到类似的功能,目前我能看到的唯一方法是创建一个 ones()
-矩阵,然后用 .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
我有一个 python 脚本,我想使用犰狳用 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)
这会生成一个矩阵,其中除五个主对角线外的所有值均为零。不幸的是,我在犰狳中找不到类似的功能,目前我能看到的唯一方法是创建一个 ones()
-矩阵,然后用 .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