cisst-saw
Loading...
Searching...
No Matches
nmrLUSolver.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3
4/*
5
6 Author(s): Anton Deguet
7 Created on: 2005-07-27
8
9 (C) Copyright 2005-2007 Johns Hopkins University (JHU), All Rights
10 Reserved.
11
12--- begin cisst license - do not edit ---
13
14This software is provided "as is" under an open source license, with
15no warranty. The complete license can be found in license.txt and
16http://www.cisst.org/cisst/license.txt.
17
18--- end cisst license ---
19*/
20
21
26
27
28#ifndef _nmrLUSolver_h
29#define _nmrLUSolver_h
30
31
36
38
86 // we have this class so that we reserve memory only one
87 // would help if svd of a same size matrix (or a matrix)
88 // that doesnt change much is desired.
89
90protected:
91 CISSTNETLIB_INTEGER M;
92 CISSTNETLIB_INTEGER N;
93 CISSTNETLIB_INTEGER Lda;
94 CISSTNETLIB_INTEGER MinMN, MaxMN;
95 // vctDynamicMatrix<CISSTNETLIB_INTEGER> Ipiv;
97 CISSTNETLIB_INTEGER Info;
100
102 void AllocateP(void) {
103 if (M > N) {
104 P.SetSize(MaxMN, MaxMN, VCT_COL_MAJOR);
105 } else {
106 P.SetSize(MinMN, MinMN, VCT_COL_MAJOR);
107 }
108 }
109
111 void AllocateLU(void) {
112 L.SetSize(M, MinMN, VCT_COL_MAJOR);
113 U.SetSize(MinMN, N, VCT_COL_MAJOR);
114 // These wont change, assign now
115 L.SetAll(0.0);
116 L.Diagonal().SetAll(1.0);
117 U.SetAll(0.0);
118 }
119
122 template <class _matrixOwnerType>
124 const unsigned int rows = A.rows();
125 const unsigned int cols = A.cols();
126 unsigned int rowIndex, colIndex;
127 for (rowIndex = 0; rowIndex < rows; ++rowIndex) {
128 for (colIndex = 0; colIndex < cols; ++colIndex) {
129 if (rowIndex > colIndex) {
130 L.Element(rowIndex, colIndex) = A[rowIndex][colIndex];
131 } else {
132 U.Element(rowIndex, colIndex) = A[rowIndex][colIndex];
133 }
134 }
135 }
136 }
137
140 void UpdateP(void) {
141 P.SetAll(0.0);
142 P.Diagonal().SetAll(1.0);
143 CISSTNETLIB_INTEGER rowIndex, colIndex;
144 for (rowIndex = 0; rowIndex < MinMN; ++rowIndex) {
145 colIndex = Ipiv[rowIndex] - 1;
146 P.ExchangeColumns(rowIndex, colIndex);
147 }
148 }
149
150
151public:
157 M(0),
158 N(0),
159 AllocatePFlag(false),
160 AllocateLUFlag(false)
161 {
163 }
164
165
176 nmrLUSolver(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n,
177 bool allocateLU = false,
178 bool allocateP = false) {
179 Allocate(m, n);
180 }
181
182
197 bool allocateLU = false, bool allocateP = false) {
198 Allocate(A, allocateLU, allocateP);
199 }
200
201 template <vct::size_type _rows, vct::size_type _cols>
203 bool allocateLU = false, bool allocateP = false) {
204 Allocate(A, allocateLU, allocateP);
205 }
206
207
208
219 inline void Allocate(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n,
220 bool allocateLU = false, bool allocateP = false) {
221 const CISSTNETLIB_INTEGER one = 1;
222 M = m;
223 N = n;
224 Lda = std::max(one, M);
225 MinMN = std::min(M, N);
226 MaxMN = std::max(M, N);
227 Ipiv.SetSize(MinMN);
228
229 AllocateLUFlag = allocateLU;
230 AllocatePFlag = allocateP;
233 }
234
235
249 bool allocateLU = false, bool allocateP = false) {
250 Allocate(A.rows(), A.cols(), allocateLU, allocateP);
251 }
252
253 template <vct::size_type _rows, vct::size_type _cols>
255 bool allocateLU = false, bool allocateP = false) {
256 Allocate(_rows, _cols, allocateLU, allocateP);
257 }
258
259
260
278 template <class _matrixOwnerType>
279 inline void Solve(vctDynamicMatrixBase<_matrixOwnerType, CISSTNETLIB_DOUBLE> &A) throw (std::runtime_error) {
280 /* check that the size and storage order matches with Allocate() */
281 if (! A.IsColMajor()) {
282 cmnThrow(std::runtime_error("nmrLUSolver Solve: The input must be column major"));
283 }
284
285 /* check sizes */
286 if ((M != static_cast<CISSTNETLIB_INTEGER>(A.rows())) || (N != static_cast<CISSTNETLIB_INTEGER>(A.cols()))) {
287 cmnThrow(std::runtime_error("nmrLUSolver Solve: Size used for Allocate was different"));
288 }
289
290 /* check that the matrix is compact */
291 if (! A.IsCompact()) {
292 cmnThrow(std::runtime_error("nmrLUSolver Solve: Requires a compact matrix"));
293 }
294
295 /* call the LAPACK C function */
296 dgetrf_(&M, &N,
297 A.Pointer(), &Lda,
298 Ipiv.Pointer(), &Info);
299
300 /* Fill P, LU if required */
301 if (AllocateLUFlag) UpdateLU(A);
302 if (AllocatePFlag) UpdateP();
303 }
304
305 template <vct::size_type _rows, vct::size_type _cols>
310
311
312
317 return Ipiv;
318 }
319
321 inline CISSTNETLIB_INTEGER GetInfo(void) const {
322 return Info;
323 }
324
326 inline const vctDynamicMatrix<CISSTNETLIB_DOUBLE> &GetP(void) const throw(std::runtime_error) {
327 if (!AllocatePFlag) {
328 cmnThrow(std::runtime_error("nmrLUSolver GetP: P was not calculated since allocateP is not set"));
329 }
330 return P;
331 }
332
334 inline const vctDynamicMatrix<CISSTNETLIB_DOUBLE> &GetL(void) const throw(std::runtime_error) {
335 if (!AllocateLUFlag) {
336 cmnThrow(std::runtime_error("nmrLUSolver GetL: L was not calculated since allocateLU is not set"));
337 }
338 return L;
339 }
340
342 inline const vctDynamicMatrix<CISSTNETLIB_DOUBLE> &GetU(void) const throw(std::runtime_error) {
343 if (!AllocateLUFlag) {
344 cmnThrow(std::runtime_error("nmrLUSolver GetU: U was not calculated since allocateLU is not set"));
345 }
346 return U;
347 }
348};
349
350
351#ifdef CISST_COMPILER_IS_MSVC
353#endif // CISST_COMPILER_IS_MSVC
354
355
356#endif // _nmrLUSolver_h
357
Definition nmrLUSolver.h:85
vctDynamicMatrix< CISSTNETLIB_DOUBLE > U
Definition nmrLUSolver.h:99
nmrLUSolver(const vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _rows, _cols, VCT_COL_MAJOR > &A, bool allocateLU=false, bool allocateP=false)
Definition nmrLUSolver.h:202
CISSTNETLIB_INTEGER Lda
Definition nmrLUSolver.h:93
void Solve(vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _rows, _cols, VCT_COL_MAJOR > &A)
Definition nmrLUSolver.h:306
vctDynamicMatrix< CISSTNETLIB_DOUBLE > P
Definition nmrLUSolver.h:99
nmrLUSolver(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, bool allocateLU=false, bool allocateP=false)
Definition nmrLUSolver.h:176
bool AllocateLUFlag
Definition nmrLUSolver.h:98
void Allocate(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, bool allocateLU=false, bool allocateP=false)
Definition nmrLUSolver.h:219
nmrLUSolver(void)
Definition nmrLUSolver.h:156
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetP(void) const
Definition nmrLUSolver.h:326
void Allocate(const vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _rows, _cols, VCT_COL_MAJOR > &A, bool allocateLU=false, bool allocateP=false)
Definition nmrLUSolver.h:254
void AllocateLU(void)
Definition nmrLUSolver.h:111
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetU(void) const
Definition nmrLUSolver.h:342
void UpdateP(void)
Definition nmrLUSolver.h:140
CISSTNETLIB_INTEGER Info
Definition nmrLUSolver.h:97
nmrLUSolver(const vctDynamicMatrix< CISSTNETLIB_DOUBLE > &A, bool allocateLU=false, bool allocateP=false)
Definition nmrLUSolver.h:196
CISSTNETLIB_INTEGER GetInfo(void) const
Definition nmrLUSolver.h:321
void Allocate(const vctDynamicMatrix< CISSTNETLIB_DOUBLE > &A, bool allocateLU=false, bool allocateP=false)
Definition nmrLUSolver.h:248
void AllocateP(void)
Definition nmrLUSolver.h:102
vctDynamicMatrix< CISSTNETLIB_DOUBLE > L
Definition nmrLUSolver.h:99
void Solve(vctDynamicMatrixBase< _matrixOwnerType, CISSTNETLIB_DOUBLE > &A)
Definition nmrLUSolver.h:279
const vctDynamicVector< CISSTNETLIB_INTEGER > & GetIpiv(void) const
Definition nmrLUSolver.h:316
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetL(void) const
Definition nmrLUSolver.h:334
CISSTNETLIB_INTEGER MaxMN
Definition nmrLUSolver.h:94
CISSTNETLIB_INTEGER M
Definition nmrLUSolver.h:91
vctDynamicVector< CISSTNETLIB_INTEGER > Ipiv
Definition nmrLUSolver.h:96
void UpdateLU(const vctDynamicConstMatrixBase< _matrixOwnerType, CISSTNETLIB_DOUBLE > &A)
Definition nmrLUSolver.h:123
CISSTNETLIB_INTEGER N
Definition nmrLUSolver.h:92
bool AllocatePFlag
Definition nmrLUSolver.h:98
CISSTNETLIB_INTEGER MinMN
Definition nmrLUSolver.h:94
Definition vctForwardDeclarations.h:145
Definition vctDynamicMatrixBase.h:43
Definition vctForwardDeclarations.h:157
Dynamic matrix referencing existing memory.
Definition vctDynamicMatrixRef.h:75
Definition vctForwardDeclarations.h:131
Implementation of a fixed-size matrix using template metaprogramming.
Definition vctFixedSizeMatrix.h:54
#define CISST_DEPRECATED
Definition cmnPortability.h:314
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76
size_type cols() const
Definition vctDynamicConstMatrixBase.h:243
size_type rows() const
Definition vctDynamicConstMatrixBase.h:238
Declaration of vctDynamicMatrix.
Declaration of vctDynamicVector.
Declaration of vctFixedSizeMatrix.
Declaration of vctFixedSizeVector.
const bool VCT_COL_MAJOR
Definition vctForwardDeclarations.h:44