SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 #include <shogun/lib/SGVector.h>
17 
18 using namespace Eigen;
19 
20 namespace shogun
21 {
22 
23 CDirectSparseLinearSolver::CDirectSparseLinearSolver()
25 {
26 }
27 
29 {
30 }
31 
34 {
35  REQUIRE(A, "Operator is NULL!\n");
36  REQUIRE(A->get_dimension()==b.vlen, "Dimension mismatch!\n");
38  =dynamic_cast<CSparseMatrixOperator<float64_t>*>(A);
39  REQUIRE(op, "Operator is not SparseMatrixOperator type!\n");
40 
41  // creating eigen3 Sparse Matrix
42  SGSparseMatrix<float64_t> sm=op->get_matrix_operator();
43  typedef SparseMatrix<float64_t> MatrixType;
44  const MatrixType& m=EigenSparseUtil<float64_t>::toEigenSparse(sm);
45 
46  // creating eigen3 maps for vectors
47  SGVector<float64_t> x(m.cols());
48  x.set_const(0.0);
49  Map<VectorXd> map_x(x.vector, x.vlen);
50  Map<VectorXd> map_b(b.vector, b.vlen);
51 
52  // using LLT to solve the system Ax=b
53  SimplicialLLT<MatrixType> llt;
54  llt.compute(m);
55  map_x=llt.solve(map_b);
56 
57  // checking for success
58  if (llt.info()==NumericalIssue)
59  SG_WARNING("Matrix is not Hermitian positive definite!\n");
60 
61  return x;
62 }
63 
64 }
const index_t get_dimension() const
virtual SGVector< float64_t > solve(CLinearOperator< float64_t > *A, SGVector< float64_t > b)
Definition: SGMatrix.h:20
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t vlen
Definition: SGVector.h:494
static Eigen::SparseMatrix< T > toEigenSparse(SGSparseMatrix< T > sg_matrix)
Definition: eigen3.cpp:22
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...
#define SG_WARNING(...)
Definition: SGIO.h:128
void set_const(T const_elem)
Definition: SGVector.cpp:150

SHOGUN Machine Learning Toolbox - Documentation