cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 
28 #ifndef _nmrLUSolver_h
29 #define _nmrLUSolver_h
30 
31 
36 
38 
85 class nmrLUSolver {
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 
90 protected:
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) {
105  } else {
107  }
108  }
109 
111  void AllocateLU(void) {
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 
151 public:
156  nmrLUSolver(void):
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  }
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;
231  if (AllocateLUFlag) AllocateLU();
232  if (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  }
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>
308  Solve(Aref);
309  }
311 
312 
316  inline const vctDynamicVector<CISSTNETLIB_INTEGER> &GetIpiv(void) const {
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 
void UpdateLU(const vctDynamicConstMatrixBase< _matrixOwnerType, CISSTNETLIB_DOUBLE > &A)
Definition: nmrLUSolver.h:123
Declaration of vctDynamicMatrix.
Definition: vctDynamicMatrixBase.h:42
#define CISST_DEPRECATED
Definition: cmnPortability.h:310
CISSTNETLIB_INTEGER Lda
Definition: nmrLUSolver.h:93
Definition: nmrLUSolver.h:85
Declaration of vctFixedSizeMatrix.
vctDynamicMatrix< CISSTNETLIB_DOUBLE > U
Definition: nmrLUSolver.h:99
void ExchangeColumns(const size_type col1Index, const size_type col2Index)
Definition: vctDynamicMatrixBase.h:328
nmrLUSolver(const vctDynamicMatrix< CISSTNETLIB_DOUBLE > &A, bool allocateLU=false, bool allocateP=false)
Definition: nmrLUSolver.h:196
void Allocate(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, bool allocateLU=false, bool allocateP=false)
Definition: nmrLUSolver.h:219
CISSTNETLIB_INTEGER GetInfo(void) const
Definition: nmrLUSolver.h:321
void SetSize(size_type size)
Definition: vctDynamicVector.h:315
Definition: vctDynamicConstMatrixBase.h:77
Declaration of vctDynamicVector.
void AllocateLU(void)
Definition: nmrLUSolver.h:111
value_type SetAll(const value_type value)
Definition: vctDynamicMatrixBase.h:452
Declaration of vctFixedSizeVector.
vctDynamicMatrix< CISSTNETLIB_DOUBLE > P
Definition: nmrLUSolver.h:99
size_type rows() const
Definition: vctDynamicConstMatrixBase.h:238
vctDynamicMatrix< CISSTNETLIB_DOUBLE > L
Definition: nmrLUSolver.h:99
size_type cols() const
Definition: vctDynamicConstMatrixBase.h:243
bool AllocateLUFlag
Definition: nmrLUSolver.h:98
void Allocate(const vctDynamicMatrix< CISSTNETLIB_DOUBLE > &A, bool allocateLU=false, bool allocateP=false)
Definition: nmrLUSolver.h:248
nmrLUSolver(void)
Definition: nmrLUSolver.h:156
void SetSize(size_type rows, size_type cols, bool storageOrder)
Definition: vctDynamicMatrix.h:364
#define cmnThrow(a)
Definition: MinimalCmn.h:4
Implementation of a fixed-size matrix using template metaprogramming.
Definition: vctFixedSizeMatrix.h:52
nmrLUSolver(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, bool allocateLU=false, bool allocateP=false)
Definition: nmrLUSolver.h:176
vctDynamicVector< CISSTNETLIB_INTEGER > Ipiv
Definition: nmrLUSolver.h:96
reference Element(size_type rowIndex, size_type colIndex)
Definition: vctDynamicMatrixBase.h:214
pointer Pointer(index_type index=0)
Definition: vctDynamicVectorBase.h:155
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetP(void) const
Definition: nmrLUSolver.h:326
value_type SetAll(const value_type value)
Definition: vctDynamicVectorBase.h:209
CISSTNETLIB_INTEGER Info
Definition: nmrLUSolver.h:97
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetU(void) const
Definition: nmrLUSolver.h:342
void Allocate(const vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _rows, _cols, VCT_COL_MAJOR > &A, bool allocateLU=false, bool allocateP=false)
Definition: nmrLUSolver.h:254
void UpdateP(void)
Definition: nmrLUSolver.h:140
DiagonalRefType Diagonal(void)
Definition: vctDynamicMatrixBase.h:239
const bool VCT_COL_MAJOR
Definition: vctForwardDeclarations.h:46
void Solve(vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _rows, _cols, VCT_COL_MAJOR > &A)
Definition: nmrLUSolver.h:306
const vctDynamicVector< CISSTNETLIB_INTEGER > & GetIpiv(void) const
Definition: nmrLUSolver.h:316
void AllocateP(void)
Definition: nmrLUSolver.h:102
CISSTNETLIB_INTEGER MaxMN
Definition: nmrLUSolver.h:94
void Solve(vctDynamicMatrixBase< _matrixOwnerType, CISSTNETLIB_DOUBLE > &A)
Definition: nmrLUSolver.h:279
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetL(void) const
Definition: nmrLUSolver.h:334
CISSTNETLIB_INTEGER MinMN
Definition: nmrLUSolver.h:94
CISSTNETLIB_INTEGER N
Definition: nmrLUSolver.h:92
CISSTNETLIB_INTEGER M
Definition: nmrLUSolver.h:91
nmrLUSolver(const vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _rows, _cols, VCT_COL_MAJOR > &A, bool allocateLU=false, bool allocateP=false)
Definition: nmrLUSolver.h:202
bool AllocatePFlag
Definition: nmrLUSolver.h:98