犰狳:用sp_mat解决

时间:2014-03-07 13:18:29

标签: linear-algebra sparse-matrix armadillo

我正在使用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

2 个答案:

答案 0 :(得分:2)

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

您可以使用ARPACK进行稀疏矩阵的(特征)分解。稀疏矩阵求解器可能会出现在下一个版本中,可能会使用SuiteSparse项目中的CXSparse库。

答案 1 :(得分:1)

从5.0版本开始,Armadillo具有spsolve()函数来解决稀疏系统。