SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
mathematics
linalg
linsolver
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
>
14
#include <
shogun/lib/SGSparseMatrix.h
>
15
#include <
shogun/mathematics/eigen3.h
>
16
#include <
shogun/mathematics/linalg/linop/SparseMatrixOperator.h
>
17
#include <
shogun/mathematics/linalg/linsolver/DirectSparseLinearSolver.h
>
18
19
using namespace
Eigen;
20
21
namespace
shogun
22
{
23
24
CDirectSparseLinearSolver::CDirectSparseLinearSolver()
25
:
CLinearSolver
<
float64_t
,
float64_t
>()
26
{
27
}
28
29
CDirectSparseLinearSolver::~CDirectSparseLinearSolver
()
30
{
31
}
32
33
SGVector<float64_t>
CDirectSparseLinearSolver::solve
(
34
CLinearOperator<float64_t>
* A,
SGVector<float64_t>
b)
35
{
36
REQUIRE
(A,
"Operator is NULL!\n"
);
37
REQUIRE
(A->
get_dimension
()==b.
vlen
,
"Dimension mismatch!\n"
);
38
CSparseMatrixOperator<float64_t>
* op
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