cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nmrLSSolver.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): Ankur Kapoor
7  Created on: 2004-10-26
8 
9  (C) Copyright 2004-2013 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 _nmrLSSolver_h
29 #define _nmrLSSolver_h
30 
31 
34 
92 class nmrLSSolver {
93  // we have this class so that we reserve memory only one would
94  // help if svd of a same size matrix (or a matrix) that doesnt
95  // change much is desired.
96 
97 protected:
98  CISSTNETLIB_INTEGER M;
99  CISSTNETLIB_INTEGER N;
100  CISSTNETLIB_INTEGER NRHS;
101  CISSTNETLIB_INTEGER Lda;
102  CISSTNETLIB_INTEGER Ldb;
103  CISSTNETLIB_INTEGER Lwork;
104  char Trans;
106  CISSTNETLIB_INTEGER Info;
108 
109 public:
114  nmrLSSolver(void):
115  M(0),
116  N(0),
117  NRHS(0),
119  {
121  }
122 
123 
135  nmrLSSolver(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, CISSTNETLIB_INTEGER nrhs, bool storageOrder) {
136  Allocate(m, n, nrhs, storageOrder);
137  }
138 
139 
147  Allocate(A, B);
148  }
150 
151 
161  inline void Allocate(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, CISSTNETLIB_INTEGER nrhs, bool storageOrder) {
162  const CISSTNETLIB_INTEGER one = 1;
163  StorageOrder = storageOrder;
164  M = m;
165  N = n;
166  NRHS = nrhs;
167  Lda = std::max(one, M);
168  Ldb = std::max(one, std::max(M, N));
169  CISSTNETLIB_INTEGER MN = std::min(M, N);
170  Lwork = std::max (one, MN + std::max (MN, NRHS));
171  Trans = 'N';
173  }
174 
175 
182  Allocate(A.rows(), A.cols(), B.cols(), A.IsRowMajor());
183  }
185 
186 
192  template <class _matrixOwnerType>
194  /* check that the size and storage order matches with Allocate() */
195  if (A.IsRowMajor() != StorageOrder) {
196  cmnThrow(std::runtime_error("nmrLSSolver Solve: Storage order used for Allocate was different"));
197  }
198  if (B.IsRowMajor() != StorageOrder) {
199  cmnThrow(std::runtime_error("nmrLSSolver Solve: Storage order used for Allocate was different"));
200  }
201 
202  /* check sizes based on storage order, there is a more compact
203  expression for this test but I find this easier to read and
204  debug (Anton) */
205  if (A.IsColMajor()) {
206  if ((M != static_cast<CISSTNETLIB_INTEGER>(A.rows())) || (N != static_cast<CISSTNETLIB_INTEGER>(A.cols()))) {
207  cmnThrow(std::runtime_error("nmrLSSolver Solve: Size used for Allocate was different"));
208  }
209  }
210  if (B.IsColMajor()) {
211  if ((M != static_cast<CISSTNETLIB_INTEGER>(B.rows())) || (NRHS != static_cast<CISSTNETLIB_INTEGER>(B.cols()))) {
212  cmnThrow(std::runtime_error("nmrLSSolver Solve: Size used for Allocate was different"));
213  }
214  }
215 
216  /* check that the matrices are Fortran like */
217  if (! A.IsCompact()) {
218  cmnThrow(std::runtime_error("nmrLSSolver Solve: Requires a compact matrix"));
219  }
220  if (! B.IsCompact()) {
221  cmnThrow(std::runtime_error("nmrLSSolver Solve: Requires a compact matrix"));
222  }
223 
224  /* call the LAPACK C function */
225 #if defined(CISSTNETLIB_VERSION_MAJOR)
226 #if (CISSTNETLIB_VERSION_MAJOR >= 3)
227  cisstNetlib_dgels_(&Trans, &M, &N, &NRHS,
228  A.Pointer(), &Lda,
229  B.Pointer(), &Ldb,
230  Work.Pointer(), &Lwork, &Info);
231 #endif
232 #else // no major version
233  dgels_(&Trans, &M, &N, &NRHS,
234  A.Pointer(), &Lda,
235  B.Pointer(), &Ldb,
236  Work.Pointer(), &Lwork, &Info);
237 #endif // CISSTNETLIB_VERSION
238  }
240 
241 };
242 
243 
244 #endif // _nmrLSSolver_h
245 
Declaration of vctDynamicMatrix.
void Allocate(vctDynamicMatrix< double > &A, vctDynamicMatrix< double > &B)
Definition: nmrLSSolver.h:181
nmrLSSolver(vctDynamicMatrix< double > &A, vctDynamicMatrix< double > &B)
Definition: nmrLSSolver.h:146
Definition: vctDynamicMatrixBase.h:42
void Solve(vctDynamicMatrixBase< _matrixOwnerType, double > &A, vctDynamicMatrixBase< _matrixOwnerType, double > &B)
Definition: nmrLSSolver.h:193
CISSTNETLIB_INTEGER M
Definition: nmrLSSolver.h:98
CISSTNETLIB_INTEGER N
Definition: nmrLSSolver.h:99
nmrLSSolver(void)
Definition: nmrLSSolver.h:114
CISSTNETLIB_INTEGER Lwork
Definition: nmrLSSolver.h:103
CISSTNETLIB_INTEGER Ldb
Definition: nmrLSSolver.h:102
pointer Pointer(size_type rowIndex, size_type colIndex)
Definition: vctDynamicMatrixBase.h:143
nmrLSSolver(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, CISSTNETLIB_INTEGER nrhs, bool storageOrder)
Definition: nmrLSSolver.h:135
bool StorageOrder
Definition: nmrLSSolver.h:107
size_type rows() const
Definition: vctDynamicConstMatrixBase.h:238
size_type cols() const
Definition: vctDynamicConstMatrixBase.h:243
void Allocate(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, CISSTNETLIB_INTEGER nrhs, bool storageOrder)
Definition: nmrLSSolver.h:161
bool IsRowMajor(void) const
Definition: vctDynamicConstMatrixBase.h:635
void SetSize(size_type rows, size_type cols, bool storageOrder)
Definition: vctDynamicMatrix.h:364
CISSTNETLIB_INTEGER NRHS
Definition: nmrLSSolver.h:100
#define cmnThrow(a)
Definition: MinimalCmn.h:4
char Trans
Definition: nmrLSSolver.h:104
const bool VCT_COL_MAJOR
Definition: vctForwardDeclarations.h:46
Definition: nmrLSSolver.h:92
CISSTNETLIB_INTEGER Lda
Definition: nmrLSSolver.h:101
CISSTNETLIB_INTEGER Info
Definition: nmrLSSolver.h:106
vctDynamicMatrix< double > Work
Definition: nmrLSSolver.h:105