SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DirectSparseLinearSolver.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Soumyajit De
8  */
9 
10 #include <shogun/lib/config.h>
11 
12 #ifdef HAVE_EIGEN3
13 #include <shogun/lib/SGVector.h>
18 
19 using namespace Eigen;
20 
21 namespace shogun
22 {
23 
24 CDirectSparseLinearSolver::CDirectSparseLinearSolver()
26 {
27 }
28 
30 {
31 }
32 
35 {
36  REQUIRE(A, "Operator is NULL!\n");
37  REQUIRE(A->get_dimension()==b.vlen, "Dimension mismatch!\n");
39  =dynamic_cast<CSparseMatrixOperator<float64_t>*>(A);
40  REQUIRE(op, "Operator is not SparseMatrixOperator type!\n");
41 
42  // creating eigen3 Sparse Matrix
43  SGSparseMatrix<float64_t> sm=op->get_matrix_operator();
44  typedef SparseMatrix<float64_t> MatrixType;
45  const MatrixType& m=EigenSparseUtil<float64_t>::toEigenSparse(sm);
46 
47  // creating eigen3 maps for vectors
48  SGVector<float64_t> x(m.cols());
49  x.set_const(0.0);
50  Map<VectorXd> map_x(x.vector, x.vlen);
51  Map<VectorXd> map_b(b.vector, b.vlen);
52 
53  // using LLT to solve the system Ax=b
54  SimplicialLLT<MatrixType> llt;
55  llt.compute(m);
56  map_x=llt.solve(map_b);
57 
58  // checking for success
59  if (llt.info()==NumericalIssue)
60  SG_WARNING("Matrix is not Hermitian positive definite!\n");
61 
62  return x;
63 }
64 
65 }
66 #endif // HAVE_EIGEN3

SHOGUN Machine Learning Toolbox - Documentation