cisst-saw
Loading...
Searching...
No Matches
nmrLU.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 Author(s): Anton Deguet
6 Created on: 2006-01-10
7
8 (C) Copyright 2006-2019 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19
24
25
26#ifndef _nmrLU_h
27#define _nmrLU_h
28
33
34// Always include last
36
37
83
84public:
91
93
94protected:
97
102
109
115 {
116 MMember = m;
117 NMember = n;
118 }
119
132 inline void AllocateOutput(bool allocateOutput)
133 {
134 // allocate output
135 if (allocateOutput) {
136 const size_type minmn = (MMember < NMember) ? MMember : NMember;
137 this->OutputMemory.SetSize(minmn);
138 this->PivotIndicesReference.SetRef(this->OutputMemory);
139 } else {
140 this->OutputMemory.SetSize(0);
141 }
142 }
143
144
151 template <class _vectorOwnerTypePivotIndices>
153 CISST_THROW(std::runtime_error)
154 {
155 // check sizes and compacity
156 const size_type minmn = (MMember < NMember) ? MMember : NMember;
157 if (minmn != pivotIndices.size()) {
158 cmnThrow(std::runtime_error("nmrLUDynamicData: Size of vector pivotIndices is incorrect."));
159 }
160 if (!pivotIndices.IsCompact()) {
161 cmnThrow(std::runtime_error("nmrLUDynamicData: Vector pivotIndices must be compact."));
162 }
163 }
164
165
166public:
167
175 template <class _matrixOwnerTypeA>
176 static inline
178 {
179 nsize_type matrixSize(A.rows(), A.rows());
180 return matrixSize;
181 }
182
190 template <class _matrixOwnerTypeA>
191 static inline
193 {
194 const size_type minmn = (A.rows() < A.cols()) ? A.rows() : A.cols();
195 nsize_type matrixSize(A.rows(), minmn);
196 return matrixSize;
197 }
198
206 template <class _matrixOwnerTypeA>
207 static inline
209 {
210 const size_type minmn = (A.rows() < A.cols()) ? A.rows() : A.cols();
211 nsize_type matrixSize(minmn, A.cols());
212 return matrixSize;
213 }
214
224 template <class _matrixOwnerTypeA, class _vectorOwnerTypePivotIndices, class _matrixOwnerTypeP>
225 static inline
230 CISST_THROW(std::runtime_error)
231 {
232 const size_type minmn = (A.rows() < A.cols()) ? A.rows() : A.cols();
233 // check sizes
234 if (pivotIndices.size() != minmn) {
235 cmnThrow(std::runtime_error("nmrLUDynamicData::UpdateMatrixP: Size of vector pivotIndices is incorrect."));
236 }
237 if (! P.IsSquare(A.rows())) {
238 cmnThrow(std::runtime_error("nmrLUDynamicData::UpdateMatrixP: Size of matrix P is incorrect."));
239 }
240 // update permutation matrix
241 P.SetAll(0.0);
242 P.Diagonal().SetAll(1.0);
243 size_type rowIndex, colIndex;
244 for (rowIndex = 0; rowIndex < minmn; ++rowIndex) {
245 colIndex = pivotIndices[rowIndex] - 1;
246 P.ExchangeColumns(rowIndex, colIndex);
247 }
248 return P;
249 }
250
265 template <class _matrixOwnerTypeA, class _matrixOwnerTypeL, class _matrixOwnerTypeU>
266 static inline
270 CISST_THROW(std::runtime_error)
271 {
272 const size_type rows = A.rows();
273 const size_type cols = A.cols();
274 size_type rowIndex, colIndex;
275 L.SetAll(0.0);
276 L.Diagonal().SetAll(1.0);
277 U.SetAll(0.0);
278 for (rowIndex = 0; rowIndex < rows; ++rowIndex) {
279 for (colIndex = 0; colIndex < cols; ++colIndex) {
280 if (rowIndex > colIndex) {
281 L.Element(rowIndex, colIndex) = A.Element(rowIndex, colIndex);
282 } else {
283 U.Element(rowIndex, colIndex) = A.Element(rowIndex, colIndex);
284 }
285 }
286 }
287
288 }
289
290
291#ifndef SWIG
292#ifndef DOXYGEN
299 class Friend {
300 private:
301 nmrLUDynamicData & Data;
302 public:
303 Friend(nmrLUDynamicData &data): Data(data) {
304 }
306 return Data.PivotIndicesReference;
307 }
308 inline size_type M(void) {
309 return Data.MMember;
310 }
311 inline size_type N(void) {
312 return Data.NMember;
313 }
314 };
315 friend class Friend;
316#endif // DOXYGEN
317#endif // SWIG
318
327 MMember(static_cast<size_type>(0)),
328 NMember(static_cast<size_type>(0))
329 {
330 AllocateOutput(false);
331 }
332
343 {
344 this->Allocate(m, n);
345 }
346
356 template <class _matrixOwnerTypeA>
361
373 template <class _matrixOwnerTypeA,
374 class _vectorOwnerTypePivotIndices>
380
390 template <class _matrixOwnerTypeA>
392 {
393 this->Allocate(A.rows(), A.cols());
394 }
395
404 {
405 this->SetDimension(m, n);
406 this->AllocateOutput(true);
407 }
408
420 template <class _matrixOwnerTypeA,
421 class _vectorOwnerTypePivotIndices>
424 CISST_THROW(std::runtime_error)
425 {
426 this->SetDimension(A.rows(), A.cols());
427 this->AllocateOutput(false);
428 this->ThrowUnlessOutputSizeIsCorrect(pivotIndices);
429 this->PivotIndicesReference.SetRef(pivotIndices);
430 }
431
438};
439
440
441
466#ifndef SWIG
467template <vct::size_type _rows, vct::size_type _cols>
469{
470public:
471#ifndef DOXYGEN
473 enum {MIN_MN = (_rows < _cols) ? _rows : _cols};
474#endif // DOXYGEN
490
491protected:
493
494public:
495#ifndef DOXYGEN
502 class Friend {
503 private:
505 public:
507 }
509 return Data.PivotIndicesMember;
510 }
511 };
512 friend class Friend;
513#endif // DOXYGEN
514
518
522 inline const VectorTypePivotIndices & PivotIndices(void) const {
523 return PivotIndicesMember;
524 }
525
526
535 inline static MatrixTypeP &
537 MatrixTypeP & P)
538 CISST_THROW(std::runtime_error)
539 {
540 // update permutation matrix
541 P.SetAll(0.0);
542 P.Diagonal().SetAll(1.0);
543 size_type rowIndex, colIndex;
544 for (rowIndex = 0; rowIndex < MIN_MN; ++rowIndex) {
545 colIndex = pivotIndices[rowIndex] - 1;
546 P.ExchangeColumns(rowIndex, colIndex);
547 }
548 return P;
549 }
550
551
566 static inline
568 MatrixTypeL & L,
569 MatrixTypeU & U)
570 CISST_THROW(std::runtime_error)
571 {
572 vct::size_type rowIndex, colIndex;
573 L.SetAll(0.0);
574 L.Diagonal().SetAll(1.0);
575 U.SetAll(0.0);
576 for (rowIndex = 0; rowIndex < _rows; ++rowIndex) {
577 for (colIndex = 0; colIndex < _cols; ++colIndex) {
578 if (rowIndex > colIndex) {
579 L.Element(rowIndex, colIndex) = A.Element(rowIndex, colIndex);
580 } else {
581 U.Element(rowIndex, colIndex) = A.Element(rowIndex, colIndex);
582 }
583 }
584 }
585 }
586};
587#endif // SWIG
588
589
590
697
698
717template <class _matrixOwnerType>
719 nmrLUDynamicData & data)
720 CISST_THROW(std::runtime_error)
721{
722 typename nmrLUDynamicData::Friend dataFriend(data);
723 CISSTNETLIB_INTEGER info;
724
725 /* check that storage order is VCT_COL_MAJOR */
726 if (!A.IsColMajor()) {
727 cmnThrow(std::runtime_error("nmrLU: Input must use VCT_COL_MAJOR storage order."));
728 }
729 /* check sizes */
730 if ((dataFriend.M() != A.rows()) || (dataFriend.N() != A.cols())) {
731 cmnThrow(std::runtime_error("nmrLU: Size used for Allocate was different."));
732 }
733 /* check that the matrices are compact */
734 if (! A.IsCompact()) {
735 cmnThrow(std::runtime_error("nmrLU: Requires a compact matrix."));
736 }
737
738 CISSTNETLIB_INTEGER m = dataFriend.M();
739 CISSTNETLIB_INTEGER n = dataFriend.N();
740 CISSTNETLIB_INTEGER lda = (m > 1) ? m : 1;
741
742 /* call the LAPACK C function */
743#if defined(CISSTNETLIB_VERSION_MAJOR)
744#if (CISSTNETLIB_VERSION_MAJOR >= 3)
745 cisstNetlib_dgetrf_(&m, &n,
746 A.Pointer(), &lda,
747 dataFriend.PivotIndices().Pointer(), &info);
748#endif
749#else // no major version
750 dgetrf_(&m, &n,
751 A.Pointer(), &lda,
752 dataFriend.PivotIndices().Pointer(), &info);
753#endif // CISSTNETLIB_VERSION
754 return info;
755}
756
757
758
772template <class _matrixOwnerTypeA, class _vectorOwnerTypePivotIndices>
775{
776 nmrLUDynamicData data(A, pivotIndices);
777 return nmrLU(A, data);
778}
779
780
781#ifndef SWIG // don't have fixed size containers in Python
782
808template <vct::size_type _rows, vct::size_type _cols, vct::size_type _minmn>
811{
812#if CMN_ASSERT_IS_DEFINED
813 const CISSTNETLIB_INTEGER minmn = static_cast<CISSTNETLIB_INTEGER>(nmrLUFixedSizeData<_rows, _cols>::MIN_MN);
814#endif
815 //Assert if requirement is equal to size provided!
816 CMN_ASSERT(minmn == static_cast<CISSTNETLIB_INTEGER>(_minmn));
817
818 CISSTNETLIB_INTEGER info;
819 CISSTNETLIB_INTEGER lda = (_rows> 1) ? _rows : 1;
820 CISSTNETLIB_INTEGER m = _rows;
821 CISSTNETLIB_INTEGER n = _cols;
822
823 /* call the LAPACK C function */
824#if defined(CISSTNETLIB_VERSION_MAJOR)
825#if (CISSTNETLIB_VERSION_MAJOR >= 3)
826 cisstNetlib_dgetrf_(&m, &n,
827 A.Pointer(), &lda,
828 pivotIndices.Pointer(), &info);
829#endif
830#else // no major version
831 dgetrf_(&m, &n,
832 A.Pointer(), &lda,
833 pivotIndices.Pointer(), &info);
834#endif // CISSTNETLIB_VERSION
835 return info;
836}
837
838
860template <vct::size_type _rows, vct::size_type _cols>
863{
864 typename nmrLUFixedSizeData<_rows, _cols>::Friend dataFriend(data);
865 return nmrLU(A, dataFriend.PivotIndices());
866}
867#endif // SWIG
868
870
871
872#endif // _nmrLU_h
Definition nmrLU.h:299
size_type N(void)
Definition nmrLU.h:311
vctDynamicVectorRef< CISSTNETLIB_INTEGER > & PivotIndices(void)
Definition nmrLU.h:305
Friend(nmrLUDynamicData &data)
Definition nmrLU.h:303
size_type M(void)
Definition nmrLU.h:308
Data of LU problem (Dynamic).
Definition nmrLU.h:82
nmrLUDynamicData(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A)
Definition nmrLU.h:357
nmrLUDynamicData(size_type m, size_type n)
Definition nmrLU.h:342
void ThrowUnlessOutputSizeIsCorrect(vctDynamicVectorBase< _vectorOwnerTypePivotIndices, CISSTNETLIB_INTEGER > &pivotIndices) CISST_THROW(std
Definition nmrLU.h:152
nmrLUDynamicData(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A, vctDynamicVectorBase< _vectorOwnerTypePivotIndices, CISSTNETLIB_INTEGER > &pivotIndices)
Definition nmrLU.h:375
size_type NMember
Definition nmrLU.h:107
vctDynamicVectorRef< CISSTNETLIB_INTEGER > PivotIndicesReference
Definition nmrLU.h:101
nmrLUDynamicData()
Definition nmrLU.h:326
vctFixedSizeVector< size_type, 2 > nsize_type
Definition nmrLU.h:92
const vctDynamicVectorRef< CISSTNETLIB_INTEGER > & PivotIndices(void) const
Definition nmrLU.h:435
vct::size_type size_type
Definition nmrLU.h:90
void SetRef(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A, vctDynamicVectorBase< _vectorOwnerTypePivotIndices, CISSTNETLIB_INTEGER > &pivotIndices) CISST_THROW(std
Definition nmrLU.h:422
size_type MMember
Definition nmrLU.h:106
static nsize_type MatrixUSize(const vctDynamicConstMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A)
Definition nmrLU.h:208
static nsize_type MatrixPSize(const vctDynamicConstMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A)
Definition nmrLU.h:177
static vctDynamicMatrixBase< _matrixOwnerTypeP, CISSTNETLIB_DOUBLE > & UpdateMatrixP(const vctDynamicConstMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A, const vctDynamicConstVectorBase< _vectorOwnerTypePivotIndices, CISSTNETLIB_INTEGER > &pivotIndices, vctDynamicMatrixBase< _matrixOwnerTypeP, CISSTNETLIB_DOUBLE > &P) CISST_THROW(std
Definition nmrLU.h:227
void Allocate(size_type m, size_type n)
Definition nmrLU.h:403
static nsize_type MatrixLSize(const vctDynamicConstMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A)
Definition nmrLU.h:192
vctDynamicVector< CISSTNETLIB_INTEGER > OutputMemory
Definition nmrLU.h:96
void Allocate(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A)
Definition nmrLU.h:391
static void UpdateMatrixLU(const vctDynamicConstMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A, vctDynamicMatrixBase< _matrixOwnerTypeL, CISSTNETLIB_DOUBLE > &L, vctDynamicMatrixBase< _matrixOwnerTypeU, CISSTNETLIB_DOUBLE > &U) CISST_THROW(std
Definition nmrLU.h:267
void SetDimension(size_type m, size_type n)
Definition nmrLU.h:114
void AllocateOutput(bool allocateOutput)
Definition nmrLU.h:132
Definition nmrLU.h:502
Friend(nmrLUFixedSizeData< _rows, _cols > &data)
Definition nmrLU.h:506
VectorTypePivotIndices & PivotIndices(void)
Definition nmrLU.h:508
Data of LU problem (Fixed size).
Definition nmrLU.h:469
vct::size_type size_type
Definition nmrLU.h:472
nmrLUFixedSizeData()
Definition nmrLU.h:517
vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _rows, MIN_MN, VCT_COL_MAJOR > MatrixTypeL
Definition nmrLU.h:486
static void UpdateMatrixLU(const MatrixTypeA &A, MatrixTypeL &L, MatrixTypeU &U) CISST_THROW(std
Definition nmrLU.h:567
vctFixedSizeVector< CISSTNETLIB_INTEGER, MIN_MN > VectorTypePivotIndices
Definition nmrLU.h:480
vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _rows, _rows, VCT_COL_MAJOR > MatrixTypeP
Definition nmrLU.h:483
@ MIN_MN
Definition nmrLU.h:473
const VectorTypePivotIndices & PivotIndices(void) const
Definition nmrLU.h:522
static MatrixTypeP & UpdateMatrixP(const VectorTypePivotIndices &pivotIndices, MatrixTypeP &P) CISST_THROW(std
Definition nmrLU.h:536
vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _rows, _cols, VCT_COL_MAJOR > MatrixTypeA
Definition nmrLU.h:477
vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, MIN_MN, _cols, VCT_COL_MAJOR > MatrixTypeU
Definition nmrLU.h:489
VectorTypePivotIndices PivotIndicesMember
Definition nmrLU.h:492
Definition vctForwardDeclarations.h:145
Definition vctForwardDeclarations.h:119
Definition vctDynamicMatrixBase.h:43
Definition vctDynamicVectorBase.h:62
pointer Pointer(index_type index=0)
Definition vctDynamicVectorBase.h:155
Definition vctForwardDeclarations.h:131
Dynamic vector referencing existing memory.
Definition vctDynamicVectorRef.h:78
void SetRef(size_type size, pointer data, stride_type stride=1)
Definition vctDynamicVectorRef.h:156
pointer Pointer(size_type rowIndex, size_type colIndex)
Definition vctFixedSizeMatrixBase.h:161
Implementation of a fixed-size matrix using template metaprogramming.
Definition vctFixedSizeMatrix.h:54
pointer Pointer(size_type index=0)
Definition vctFixedSizeVectorBase.h:226
Implementation of a fixed-size vector using template metaprogramming.
Definition vctFixedSizeVector.h:54
#define CMN_ASSERT(expr)
Definition cmnAssert.h:97
#define CISST_THROW(exceptionParameter)
Somewhat portable compilation warning message. This works with very recent versions of gcc (4....
Definition cmnPortability.h:559
Declaration of the template function cmnThrow.
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76
size_t size_type
Definition vctContainerTraits.h:35
Rules of exporting.
CISSTNETLIB_INTEGER nmrLU(vctDynamicMatrixBase< _matrixOwnerType, CISSTNETLIB_DOUBLE > &A, nmrLUDynamicData &data) CISST_THROW(std
Definition nmrLU.h:718
size_type cols() const
Definition vctDynamicConstMatrixBase.h:243
size_type rows() const
Definition vctDynamicConstMatrixBase.h:238
Declaration of vctDynamicMatrix.
Declaration of vctFixedSizeMatrix.