cisst-saw
Loading...
Searching...
No Matches
nmrPInverseSolver.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: 2005-07-29
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 _nmrPInverseSolver_h
29#define _nmrPInverseSolver_h
30
33
34
76 // we have this class so that we reserve memory only one
77 // would help if svd of a same size matrix (or a matrix)
78 // that doesnt change much is desired.
79
80protected:
81 CISSTNETLIB_INTEGER M;
82 CISSTNETLIB_INTEGER N;
83 CISSTNETLIB_INTEGER MaxMN;
84 CISSTNETLIB_INTEGER MinMN;
85 CISSTNETLIB_DOUBLE EPS;
91
92#ifdef CISST_COMPILER_IS_MSVC
93#pragma warning (push)
94#pragma warning (disable: 4996)
95#endif // CISST_COMPILER_IS_MSVC
96
98
99#ifdef CISST_COMPILER_IS_MSVC
100#pragma warning (pop)
101#endif // CISST_COMPILER_IS_MSVC
102
103
104public:
110 M(0),
111 N(0),
113 {
115 }
116
117
128 nmrPInverseSolver(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, bool storageOrder = VCT_COL_MAJOR) {
129 Allocate(m, n, storageOrder);
130 }
131
132
142
143
144
153 inline void Allocate(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, bool storageOrder) {
154 StorageOrder = storageOrder;
155 M = m;
156 N = n;
157 MaxMN = std::max(M, N);
158 MinMN = std::min(M, N);
159 svd.Allocate(M, N, storageOrder);
160 S.SetSize(MinMN, 1, storageOrder);
161 U.SetSize(M, M, storageOrder);
162 V.SetSize(N, N, storageOrder);
163 PInverseA.SetSize(N, M, storageOrder);
164 /* compute machine precision */
165 EPS = 1.0;
166 do {
167 EPS = EPS / 2.0;
168 } while (EPS + 1.0 != 1.0);
169 EPS = EPS * 2.0;
170 }
171
172
179 Allocate(A.rows(), A.cols(), A.IsRowMajor());
180 }
181
182
183
195 template <class _matrixOwnerType>
197 throw (std::runtime_error) {
198 /* all the checking is done by SVD */
199 svd.Solve(A);
200 S.Assign(svd.GetS());
201 U.Assign(svd.GetU());
202 V.Assign(svd.GetVt().Transpose());
203 CISSTNETLIB_DOUBLE eps = EPS * svd.GetS().at(0, 0);
204 PInverseA.SetAll(0);
205 CISSTNETLIB_DOUBLE singularValue;
206 for (int irank = 0; irank < MinMN; irank++) {
207 if ((singularValue = S(irank, 0)) > eps) {
208 for (int j = 0; j < M; j++) {
209 for (int i = 0; i < N; i++) {
210 PInverseA(i, j) = PInverseA(i, j)
211 + V(i, irank)*U(j, irank)/singularValue;
212 }
213 }
214 }
215 }
216 }
217
218
219 inline const vctDynamicMatrix<CISSTNETLIB_DOUBLE> &GetS(void) const {
220 return S;
221 }
222
223 inline const vctDynamicMatrix<CISSTNETLIB_DOUBLE> &GetU(void) const {
224 return U;
225 }
226
227 inline const vctDynamicMatrix<CISSTNETLIB_DOUBLE> &GetV(void) const {
228 return V;
229 }
230
232 return PInverseA;
233 }
234};
235
236
237#ifdef CISST_COMPILER_IS_MSVC
239#endif // CISST_COMPILER_IS_MSVC
240
241
242#endif // _nmrPInverseSolver_h
243
Definition nmrPInverseSolver.h:75
void Allocate(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, bool storageOrder)
Definition nmrPInverseSolver.h:153
bool StorageOrder
Definition nmrPInverseSolver.h:90
vctDynamicMatrix< CISSTNETLIB_DOUBLE > U
Definition nmrPInverseSolver.h:87
CISSTNETLIB_INTEGER MinMN
Definition nmrPInverseSolver.h:84
nmrPInverseSolver(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, bool storageOrder=VCT_COL_MAJOR)
Definition nmrPInverseSolver.h:128
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetU(void) const
Definition nmrPInverseSolver.h:223
vctDynamicMatrix< CISSTNETLIB_DOUBLE > S
Definition nmrPInverseSolver.h:86
CISSTNETLIB_INTEGER N
Definition nmrPInverseSolver.h:82
CISSTNETLIB_INTEGER M
Definition nmrPInverseSolver.h:81
CISSTNETLIB_DOUBLE EPS
Definition nmrPInverseSolver.h:85
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetV(void) const
Definition nmrPInverseSolver.h:227
nmrPInverseSolver(void)
Definition nmrPInverseSolver.h:109
void Solve(vctDynamicMatrixBase< _matrixOwnerType, CISSTNETLIB_DOUBLE > &A)
Definition nmrPInverseSolver.h:196
CISSTNETLIB_INTEGER MaxMN
Definition nmrPInverseSolver.h:83
void Allocate(const vctDynamicMatrix< CISSTNETLIB_DOUBLE > &A)
Definition nmrPInverseSolver.h:178
nmrSVDSolver svd
Definition nmrPInverseSolver.h:97
nmrPInverseSolver(const vctDynamicMatrix< CISSTNETLIB_DOUBLE > &A)
Definition nmrPInverseSolver.h:139
vctDynamicMatrix< CISSTNETLIB_DOUBLE > PInverseA
Definition nmrPInverseSolver.h:89
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetPInverse(void) const
Definition nmrPInverseSolver.h:231
vctDynamicMatrix< CISSTNETLIB_DOUBLE > V
Definition nmrPInverseSolver.h:88
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetS(void) const
Definition nmrPInverseSolver.h:219
Definition nmrSVDSolver.h:87
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetVt(void) const
Definition nmrSVDSolver.h:294
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetS(void) const
Definition nmrSVDSolver.h:288
void Solve(vctDynamicMatrixBase< _matrixOwnerType, CISSTNETLIB_DOUBLE > &A)
Definition nmrSVDSolver.h:232
const vctDynamicMatrix< CISSTNETLIB_DOUBLE > & GetU(void) const
Definition nmrSVDSolver.h:291
void Allocate(CISSTNETLIB_INTEGER m, CISSTNETLIB_INTEGER n, bool storageOrder)
Definition nmrSVDSolver.h:168
Definition vctDynamicMatrixBase.h:43
Definition vctForwardDeclarations.h:157
#define CISST_DEPRECATED
Definition cmnPortability.h:314
Declaration of nmrSVDSolver.
Declaration of vctDynamicMatrix.
const bool VCT_COL_MAJOR
Definition vctForwardDeclarations.h:44