-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsparse_cholesky.h
237 lines (181 loc) · 5.21 KB
/
sparse_cholesky.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* @author Hauke Strasdat
*
* Copyright (C) 2010 Hauke Strasdat
* Imperial College London
*
* sparse_cholesky.h is part of RobotVision.
*
* RobotVision is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* RobotVision is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RV_SPARSE_CHOLESKY_H
#define RV_SPARSE_CHOLESKY_H
#ifdef RV_SUITESPARSE_SUPPORT
#include "SuiteSparseQR.hpp"
#endif
#include <list>
#include "math.h"
#include "sparse_matrix.h"
#include <stdexcept>
namespace RobotVision
{
class NotPosSemiDefException : public std::runtime_error
{
public:
NotPosSemiDefException()
: std::runtime_error("Not positive semi-definite") { }
};
template<int Size=TooN::Dynamic>
class SparseSolver
{
public:
SparseSolver(const SparseMatrix<Size> & s_M) : s_M(s_M)
{
#ifdef RV_SUITESPARSE_SUPPORT
assert(s_M.data->stype==0||s_M.data->stype==1);
if (s_M.data->stype==0)
{
use_qr = true;
}
else
{
use_qr = false;
}
if (!use_qr)
{
factor = cholmod_l_analyze(s_M.data,
&(CholmodSingleton::getInstance().data));
cholmod_l_factorize(s_M.data,
factor,
&(CholmodSingleton::getInstance().data));
int status= CholmodSingleton::getInstance().data.status;
if (status == CHOLMOD_NOT_POSDEF)
{
throw NotPosSemiDefException();
}
}
#else
symbolic_structure = NULL;
numeric_structure = NULL;
symbolicDecomposition();
decomposition();
if (numeric_structure==NULL)
{
throw NotPosSemiDefException();
}
#endif
}
TooN::Matrix<Size,Size> get_L()
{
#ifdef RV_SUITESPARSE_SUPPORT
TooN::Matrix<Size,Size> L
= TooN::Zeros(factor->n,factor->n);
int col_idx = -1;
for (int r=0;r<(int)(s_M.data->nzmax); ++r)
{
if (reinterpret_cast<int *>(factor->p)[col_idx+1]==r)
++col_idx;
L(reinterpret_cast<int *>(factor->i)[r],col_idx)
= reinterpret_cast<double *>(factor->x)[r];
}
return L;
#else
TooN::Matrix<Size,Size> L
= TooN::Zeros(numeric_structure->L->m,numeric_structure->L->n);
int col_idx = -1;
for (int r=0;r<s_M.sparse_matrix.nzmax; ++r)
{
if (numeric_structure->L->p[col_idx+1]==r)
++col_idx;
L(numeric_structure->L->i[r],col_idx) = numeric_structure->L->x[r];
}
return L;
#endif
}
TooN::Vector<Size> backsub (const TooN::Vector<Size>& b) const
{
#ifdef RV_SUITESPARSE_SUPPORT
DenseVector dense_b(b);
cholmod_dense * dres;
if (use_qr)
{
dres = SuiteSparseQR<double>(s_M.data,
dense_b.data,
&(CholmodSingleton::getInstance().data));
}
else
{
dres = cholmod_l_solve(CHOLMOD_A,
factor,
dense_b.data,
&(CholmodSingleton::getInstance().data));
}
DenseVector res(dres);
return res.vec();
#else
TooN::Vector<Size> tmp = b;
TooN::Vector<Size> sol = b;
cs_ipvec(symbolic_structure->pinv,&b[0],&tmp[0],b.size());
//permute con. pivoting
cs_lsolve(numeric_structure->L,&tmp[0]);
cs_ltsolve(numeric_structure->L,&tmp[0]);
cs_pvec(symbolic_structure->pinv,&tmp[0],&sol[0],b.size());
//unpermute con. pivoting
return sol;
#endif
}
TooN::Matrix<Size,Size> backsub (const TooN::Matrix<Size,Size>& M) const
{
TooN::Matrix<Size,Size> res(s_M.num_rows(),s_M.num_rows());
for (int i=0;i<M.num_cols(); ++i)
{
res.T()[i] = backsub(M.T()[i]);
}
return res;
}
TooN::Matrix<Size,Size> get_inverse()const {
TooN::Matrix<Size,Size> I = TooN::Identity(s_M.num_rows());
return backsub(I);
}
~SparseSolver()
{
#ifdef RV_SUITESPARSE_SUPPORT
if (!use_qr)
cholmod_l_free_factor(&factor,&(CholmodSingleton::getInstance().data));
#else
cs_nfree(numeric_structure);
cs_sfree(symbolic_structure);
#endif
}
private:
SparseMatrix<Size> s_M;
#ifdef RV_SUITESPARSE_SUPPORT
cholmod_factor * factor;
bool use_qr;
#else
css * symbolic_structure;
csn * numeric_structure;
void symbolicDecomposition()
{
symbolic_structure = cs_schol (1, &(s_M.sparse_matrix)) ;
}
void decomposition()
{
numeric_structure = cs_chol (&(s_M.sparse_matrix),symbolic_structure) ;
}
#endif
};
}
#endif // RV_SPARSE_CHOLESKY_H