SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
Definition: SGMatrix.h:20
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual SGVector< float64_t > solve(CLinearOperator< SGVector< float64_t >, SGVector< float64_t > > *A, SGVector< float64_t > b)
index_t vlen
Definition: SGVector.h:494
static Eigen::SparseMatrix< T > toEigenSparse(SGSparseMatrix< T > sg_matrix)
Definition: eigen3.cpp:23
double float64_t
Definition: common.h:50
Abstract template base class that provides an abstract solve method for linear systems, that takes a linear operator , a vector , solves the system and returns the vector .
Definition: LinearSolver.h:25
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Class that represents a sparse-matrix linear operator. It computes matrix-vector product in its appl...
Abstract template base class that represents a linear operator, e.g. a matrix.
#define SG_WARNING(...)
Definition: SGIO.h:128
void set_const(T const_elem)
Definition: SGVector.cpp:152

SHOGUN Machine Learning Toolbox - Documentation