Armadillo: solve with sp_mat



我正在使用Armadillo编写一个基本的FEM程序。我将sp_matvec用作矩阵和向量类型。问题是,当我做solve(X, b)时,我会遇到错误。可能是solve不支持sp_mat。除了使用密集的矩阵外,还有其他选择吗?以下是sp_mat不编译的代码。如果我使用注释的行mat A,则可以正常工作。

    int N = 3;
double h = 1./N;
//mat A = zeros<mat>(N+1,N+1);
sp_mat A(N+1,N+1); 
for(int i=0;i<=N;i++) {
    if(i>0) {A(i,i-1)=-1.;}
    A(i,i)=2.;
    if(i<N) {A(i,i+1)=-1.;}
}
A(N,N)=1;
vec b = zeros(N+1);
for(int i=0;i<=N;i++) {
    b(i)=h; 
}
vec zeta = solve(A,b);
cout << zeta;

错误:

make all 
Building file: ../src/FEM.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/FEM.d" -MT"src/FEM.d" -o "src/FEM.o" "../src/FEM.cpp"
../src/FEM.cpp: In function ‘int main()’:
../src/FEM.cpp:37:22: error: no matching function for call to ‘solve(arma::sp_mat&, arma::vec&)’
  vec zeta = solve(A,b);
                      ^
../src/FEM.cpp:37:22: note: candidates are:
In file included from /usr/include/armadillo:397:0,
                 from ../src/FEM.cpp:10:
/usr/include/armadillo_bits/fn_solve.hpp:25:1: note: template<class T1, class T2> const arma::Glue<T1, T2, arma::glue_solve> arma::solve(const arma::Base<typename T1::elem_type, T1>&, const arma::Base<typename T1::elem_type, T2>&, bool, const typename arma::arma_blas_type_only<typename T1::elem_type>::result*)
 solve
 ^
/usr/include/armadillo_bits/fn_solve.hpp:25:1: note:   template argument deduction/substitution failed:
../src/FEM.cpp:37:22: note:   ‘arma::sp_mat {aka arma::SpMat<double>}’ is not derived from ‘const arma::Base<typename T1::elem_type, T1>’
  vec zeta = solve(A,b);
                      ^
In file included from /usr/include/armadillo:397:0,
                 from ../src/FEM.cpp:10:
/usr/include/armadillo_bits/fn_solve.hpp:44:1: note: template<class T1, class T2> const arma::Glue<T1, T2, arma::glue_solve> arma::solve(const arma::Base<typename T1::elem_type, T1>&, const arma::Base<typename T1::elem_type, T2>&, const char*, const typename arma::arma_blas_type_only<typename T1::elem_type>::result*)
 solve
 ^
/usr/include/armadillo_bits/fn_solve.hpp:44:1: note:   template argument deduction/substitution failed:
../src/FEM.cpp:37:22: note:   ‘arma::sp_mat {aka arma::SpMat<double>}’ is not derived from ‘const arma::Base<typename T1::elem_type, T1>’
  vec zeta = solve(A,b);
                      ^
In file included from /usr/include/armadillo:397:0,
                 from ../src/FEM.cpp:10:
/usr/include/armadillo_bits/fn_solve.hpp:67:1: note: template<class T1, class T2> const arma::Glue<T1, T2, arma::glue_solve_tr> arma::solve(const arma::Op<T1, arma::op_trimat>&, const arma::Base<typename T1::elem_type, T2>&, bool, const typename arma::arma_blas_type_only<typename T1::elem_type>::result*)
 solve
 ^
/usr/include/armadillo_bits/fn_solve.hpp:67:1: note:   template argument deduction/substitution failed:
../src/FEM.cpp:37:22: note:   ‘arma::sp_mat {aka arma::SpMat<double>}’ is not derived from ‘const arma::Op<T1, arma::op_trimat>’
  vec zeta = solve(A,b);
                      ^
In file included from /usr/include/armadillo:397:0,
                 from ../src/FEM.cpp:10:
/usr/include/armadillo_bits/fn_solve.hpp:87:1: note: template<class T1, class T2> const arma::Glue<T1, T2, arma::glue_solve_tr> arma::solve(const arma::Op<T1, arma::op_trimat>&, const arma::Base<typename T1::elem_type, T2>&, const char*, const typename arma::arma_blas_type_only<typename T1::elem_type>::result*)
 solve
 ^
/usr/include/armadillo_bits/fn_solve.hpp:87:1: note:   template argument deduction/substitution failed:
../src/FEM.cpp:37:22: note:   ‘arma::sp_mat {aka arma::SpMat<double>}’ is not derived from ‘const arma::Op<T1, arma::op_trimat>’
  vec zeta = solve(A,b);
                      ^
In file included from /usr/include/armadillo:397:0,
                 from ../src/FEM.cpp:10:
/usr/include/armadillo_bits/fn_solve.hpp:110:1: note: template<class T1, class T2> bool arma::solve(arma::Mat<typename T1::elem_type>&, const arma::Base<typename T1::elem_type, T1>&, const arma::Base<typename T1::elem_type, T2>&, bool, const typename arma::arma_blas_type_only<typename T1::elem_type>::result*)
 solve
 ^
/usr/include/armadillo_bits/fn_solve.hpp:110:1: note:   template argument deduction/substitution failed:
../src/FEM.cpp:37:22: note:   ‘arma::sp_mat {aka arma::SpMat<double>}’ is not derived from ‘arma::Mat<typename T1::elem_type>’
  vec zeta = solve(A,b);
                      ^
In file included from /usr/include/armadillo:397:0,
                 from ../src/FEM.cpp:10:
/usr/include/armadillo_bits/fn_solve.hpp:139:1: note: template<class T1, class T2> bool arma::solve(arma::Mat<typename T1::elem_type>&, const arma::Base<typename T1::elem_type, T1>&, const arma::Base<typename T1::elem_type, T2>&, const char*, const typename arma::arma_blas_type_only<typename T1::elem_type>::result*)
 solve
 ^
/usr/include/armadillo_bits/fn_solve.hpp:139:1: note:   template argument deduction/substitution failed:
../src/FEM.cpp:37:22: note:   ‘arma::sp_mat {aka arma::SpMat<double>}’ is not derived from ‘arma::Mat<typename T1::elem_type>’
  vec zeta = solve(A,b);
                      ^
make: *** [src/FEM.o] Error 1

armadillo中的稀疏矩阵支持尚未完成。

您可以将arpack用于(特征 - )稀疏矩阵的分解。稀疏矩阵求解器可能会出现在下一个版本中,该版本可能会使用Suitesparse项目中的CXSPARSE库。

从5.0版本开始,armadillo具有求解稀疏系统的spsolve()函数。

相关内容

  • 没有找到相关文章

最新更新