26 #ifndef _nmrPInverse_h
27 #define _nmrPInverse_h
115 if (allocateOutput) {
125 if (allocateWorkspace) {
140 template <
class _vectorOwnerTypeWorkspace>
149 current += (MMember *
MMember);
154 current += (NMember *
NMember);
172 template <
typename _matrixOwnerTypePInverse>
174 throw(std::runtime_error)
177 if ((
MMember != pInverse.cols()) || (
NMember != pInverse.rows())) {
178 cmnThrow(std::runtime_error(
"nmrPInverseDynamicData: Size of matrix pInverse is incorrect."));
181 cmnThrow(std::runtime_error(
"nmrPInverseDynamicData: Storage order of pInverse is incorrect."));
192 template <
typename _vectorOwnerTypeWorkspace>
195 throw(std::runtime_error)
198 if (lwork > workspace.size()) {
199 cmnThrow(std::runtime_error(
"nmrPInverseDynamicData: Workspace is too small."));
201 if (!workspace.IsCompact()) {
202 cmnThrow(std::runtime_error(
"nmrPInverseDynamicData: Workspace must be compact."));
217 const size_type lwork_1 = 3 * minmn + maxmn;
219 const size_type lwork = (lwork_1 > lwork_2) ? lwork_1 : lwork_2;
221 return m * m + n * n + minmn + lwork + n * m;
229 template <
class _matrixOwnerTypeA>
276 #endif // #ifndef SWIG
300 template <
class _matrixOwnerTypeA>
315 template <
class _matrixOwnerTypeA,
class _vectorOwnerTypeWorkspace>
331 template <
class _matrixOwnerTypeA,
332 class _matrixOwnerTypePInverse,
333 class _vectorOwnerTypeWorkspace>
338 this->
SetRef(pInverse, workspace);
352 template <
class _matrixOwnerTypeA,
class _matrixOwnerTypePInverse>
371 template <
class _matrixOwnerTypeA>
397 template <
class _matrixOwnerTypeA,
class _vectorOwnerTypeWorkspace>
418 template <
class _matrixOwnerTypePInverse,
419 class _vectorOwnerTypeWorkspace>
447 template <
class _matrixOwnerTypePInverse>
496 template <vct::
size_type _rows, vct::
size_type _cols,
bool _storageOrder>
502 enum {
MIN_MN = (_rows < _cols) ? _rows : _cols};
508 static_cast<size_type>(
LWORK_1)
531 MatrixTypePInverse PInverseMember;
536 VectorTypeWorkspace WorkspaceMember;
540 MatrixTypeU UReference;
541 MatrixTypeVt VtReference;
542 VectorTypeS SReference;
543 VectorTypeSVDWorkspace SVDWorkspaceReference;
544 MatrixTypeP PReference;
568 return Data.PInverseMember;
570 inline VectorTypeS &
S(
void) {
571 return Data.SReference;
573 inline MatrixTypeU &
U(
void) {
574 return Data.UReference;
576 inline MatrixTypeVt &
Vt(
void) {
577 return Data.VtReference;
580 return Data.WorkspaceMember;
582 inline MatrixTypeP &
P(
void) {
583 return Data.PReference;
594 UReference(WorkspaceMember.Pointer(0)),
595 VtReference(WorkspaceMember.Pointer(_rows * _rows)),
596 SReference(WorkspaceMember.Pointer(_rows * _rows + _cols * _cols)),
597 SVDWorkspaceReference(WorkspaceMember.Pointer(_rows * _rows + _cols * _cols +
MIN_MN)),
598 PReference(WorkspaceMember.Pointer(_rows * _rows + _cols * _cols +
MIN_MN +
LWORK_3))
609 inline const MatrixTypePInverse &
PInverse(
void)
const {
610 return PInverseMember;
612 inline const VectorTypeS &
S(
void)
const {
615 inline const MatrixTypeU &
U(
void)
const {
618 inline const MatrixTypeVt &
Vt(
void)
const {
621 inline const MatrixTypeP &
P(
void)
const {
626 #endif // #ifndef SWIG
783 template <
class _matrixOwnerType>
789 CISSTNETLIB_INTEGER ret_value;
792 cmnThrow(std::runtime_error(
"nmrPInverse Solve: Storage order used for Allocate was different"));
794 if ((A.rows() != dataFriend.
M()) || (A.cols() != dataFriend.
N())) {
795 cmnThrow(std::runtime_error(
"nmrPInverse Solve: Size used for Allocate was different"));
797 const size_type rows = A.rows();
798 const size_type cols = A.cols();
799 const size_type minmn = (rows < cols) ? rows : cols;
800 const size_type maxmn = (rows > cols) ? rows : cols;
802 ret_value =
nmrSVD(A, dataFriend.
U(), dataFriend.
S(),
807 CISSTNETLIB_DOUBLE singularValue;
808 size_type irank, i, j;
809 for (irank = 0; irank < minmn; irank++) {
810 if ((singularValue = dataFriend.
S().
at(irank)) > eps) {
811 for (j = 0; j < rows; j++) {
812 for (i = 0; i < cols; i++) {
814 + dataFriend.
Vt().
at(irank, i) * dataFriend.
U().
at(j, irank) / singularValue;
839 template <
class _matrixOwnerTypeA,
class _matrixOwnerTypePInverse,
class _vectorOwnerTypeWorkspace>
844 CISSTNETLIB_INTEGER ret_value =
nmrPInverse(A, svdData);
863 template <
class _matrixOwnerTypeA,
class _matrixOwnerTypePInverse>
868 CISSTNETLIB_INTEGER ret_value =
nmrPInverse(A, svdData);
891 template <vct::
size_type _rows, vct::
size_type _cols, vct::
size_type _work,
bool _storageOrder,
class _dataPtrType>
900 const size_type maxmn = (_rows > _cols) ? _rows : _cols;
908 (_storageOrder) ? _rows : 1,
909 (_storageOrder) ? 1 : _rows,
912 (_storageOrder) ? _cols : 1,
913 (_storageOrder) ? 1 : _cols,
914 workspace.
Pointer(_rows*_rows));
916 workspace.
Pointer(_rows * _rows + _cols * _cols));
918 workspace.
Pointer(_rows * _rows + _cols * _cols + minmn));
920 workspace.
Pointer(_rows * _rows + _cols * _cols + minmn + lwork_3));
922 CISSTNETLIB_INTEGER ret_value;
923 ret_value =
nmrSVD(ARef, URef, SRef, VtRef, SVDWorkspaceRef);
937 for (irank = 0; irank < minmn; irank++) {
938 if (SRef(irank) > eps) {
939 PRef.Row(irank).ProductOf(URef.Transpose().Row(irank), 1.0/SRef(irank));
941 PRef.Row(irank).SetAll(0.);
944 pInverseRef.
ProductOf(VtRef.Transpose(), PRef);
964 template <vct::
size_type _rows, vct::
size_type _cols,
bool _storageOrder>
969 CISSTNETLIB_INTEGER ret_value =
nmrPInverse(A, pInverse, workspace);
993 template <vct::
size_type _rows, vct::
size_type _cols,
bool _storageOrder>
1002 #endif // #ifndef SWIG
vctDynamicMatrixRef< CISSTNETLIB_DOUBLE > UReference
Definition: nmrPInverse.h:74
vct::size_type size_type
Definition: nmrPInverse.h:54
Declaration of vctDynamicMatrix.
CISSTNETLIB_INTEGER nmrPInverse(vctDynamicMatrixBase< _matrixOwnerType, CISSTNETLIB_DOUBLE > &A, nmrPInverseDynamicData &data)
Definition: nmrPInverse.h:784
#define CMN_ASSERT(expr)
Definition: cmnAssert.h:90
bool StorageOrderMember
Definition: nmrPInverse.h:85
Definition: vctDynamicMatrixBase.h:42
nmrPInverseDynamicData(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &CMN_UNUSED(A), vctDynamicMatrixBase< _matrixOwnerTypePInverse, CISSTNETLIB_DOUBLE > &pInverse, vctDynamicVectorBase< _vectorOwnerTypeWorkspace, CISSTNETLIB_DOUBLE > &workspace)
Definition: nmrPInverse.h:334
vctFixedSizeVectorRef< CISSTNETLIB_DOUBLE, LWORK_3, 1 > VectorTypeSVDWorkspace
Definition: nmrPInverse.h:527
nmrPInverseDynamicData(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A, vctDynamicVectorBase< _vectorOwnerTypeWorkspace, CISSTNETLIB_DOUBLE > &workspace)
Definition: nmrPInverse.h:316
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
VectorTypeS & S(void)
Definition: nmrPInverse.h:570
vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _cols, _rows, _storageOrder > MatrixTypePInverse
Definition: nmrPInverse.h:517
size_type MMember
Definition: nmrPInverse.h:83
size_type M(void)
Definition: nmrPInverse.h:264
static Type Tolerance(void)
Definition: cmnTypeTraits.h:170
vctDynamicMatrixRef< CISSTNETLIB_DOUBLE > & U(void)
Definition: nmrPInverse.h:255
Declaration of vctFixedSizeMatrix.
const vctDynamicMatrixRef< CISSTNETLIB_DOUBLE > & U(void) const
Definition: nmrPInverse.h:470
void ThrowUnlessOutputSizeIsCorrect(vctDynamicMatrixBase< _matrixOwnerTypePInverse, CISSTNETLIB_DOUBLE > &pInverse) const
Definition: nmrPInverse.h:173
void ThrowUnlessWorkspaceSizeIsCorrect(vctDynamicVectorBase< _vectorOwnerTypeWorkspace, CISSTNETLIB_DOUBLE > &workspace) const
Definition: nmrPInverse.h:194
size_t size_type
Definition: vctContainerTraits.h:35
size_type N(void)
Definition: nmrPInverse.h:267
void SetRefSVD(vctDynamicVectorBase< _vectorOwnerTypeWorkspace, CISSTNETLIB_DOUBLE > &workspace)
Definition: nmrPInverse.h:141
nmrPInverseDynamicData(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &CMN_UNUSED(A), vctDynamicMatrixBase< _matrixOwnerTypePInverse, CISSTNETLIB_DOUBLE > &pInverse)
Definition: nmrPInverse.h:353
Definition: nmrPInverse.h:511
void AllocateOutputWorkspace(bool allocateOutput, bool allocateWorkspace)
Definition: nmrPInverse.h:112
bool StorageOrder(void) const
Definition: vctDynamicConstMatrixBase.h:656
Definition: nmrPInverse.h:46
vctFixedSizeMatrixRef< CISSTNETLIB_DOUBLE, _cols, _rows, _storageOrder?_cols:1, _storageOrder?1:_rows > MatrixTypeP
Definition: nmrPInverse.h:528
void SetRef(size_type size, pointer data, stride_type stride=1)
Definition: vctDynamicVectorRef.h:156
Definition: nmrPInverse.h:243
void SetRefWorkspace(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A, vctDynamicVectorBase< _vectorOwnerTypeWorkspace, CISSTNETLIB_DOUBLE > &workspace)
Definition: nmrPInverse.h:398
vctDynamicMatrixRef< CISSTNETLIB_DOUBLE > & Vt(void)
Definition: nmrPInverse.h:258
vctDynamicVector< CISSTNETLIB_DOUBLE > OutputMemory
Definition: nmrPInverse.h:68
void SetSize(size_type size)
Definition: vctDynamicVector.h:315
ThisType & ProductOf(const vctDynamicConstMatrixBase< __matrixOwnerType, _elementType > &matrix, const value_type scalar)
Definition: vctDynamicMatrixBase.h:971
Friend(nmrPInverseFixedSizeData< _rows, _cols, _storageOrder > &data)
Definition: nmrPInverse.h:565
const vctDynamicVectorRef< CISSTNETLIB_DOUBLE > & S(void) const
Definition: nmrPInverse.h:467
const VectorTypeS & S(void) const
Definition: nmrPInverse.h:612
vctDynamicVectorRef< CISSTNETLIB_DOUBLE > & S(void)
Definition: nmrPInverse.h:249
const MatrixTypeP & P(void) const
Definition: nmrPInverse.h:621
vctFixedSizeMatrix< CISSTNETLIB_DOUBLE, _rows, _cols, _storageOrder > MatrixTypeA
Definition: nmrPInverse.h:515
VectorTypeWorkspace & Workspace(void)
Definition: nmrPInverse.h:579
vctDynamicVector< CISSTNETLIB_DOUBLE > WorkspaceMemory
Definition: nmrPInverse.h:64
void SetRef(size_type rows, size_type cols, stride_type rowStride, stride_type colStride, pointer dataPointer)
Definition: vctDynamicMatrixRef.h:217
Definition: nmrPInverse.h:503
vctFixedSizeVectorRef< CISSTNETLIB_DOUBLE, MIN_MN, 1 > VectorTypeS
Definition: nmrPInverse.h:525
nmrPInverseFixedSizeData()
Definition: nmrPInverse.h:593
reference at(index_type index)
Definition: vctDynamicVectorBase.h:170
static size_type WorkspaceSize(size_type m, size_type n)
Definition: nmrSVD.h:298
void SetDimension(size_type m, size_type n, bool storageOrder)
Definition: nmrPInverse.h:92
value_type SetAll(const value_type value)
Definition: vctDynamicMatrixBase.h:452
Definition: nmrPInverse.h:502
size_type rows() const
Definition: vctDynamicConstMatrixBase.h:238
vctDynamicVectorRef< CISSTNETLIB_DOUBLE > WorkspaceReference
Definition: nmrPInverse.h:77
size_type cols() const
Definition: vctDynamicConstMatrixBase.h:243
Definition: nmrPInverse.h:505
vctDynamicMatrixRef< CISSTNETLIB_DOUBLE > VtReference
Definition: nmrPInverse.h:75
nmrPInverseDynamicData()
Definition: nmrPInverse.h:284
MatrixTypeP & P(void)
Definition: nmrPInverse.h:582
const MatrixTypeVt & Vt(void) const
Definition: nmrPInverse.h:618
vctDynamicVectorRef< CISSTNETLIB_DOUBLE > & Workspace(void)
Definition: nmrPInverse.h:261
reference at(size_type index)
Definition: vctDynamicMatrixBase.h:171
vctDynamicMatrixRef< CISSTNETLIB_DOUBLE > & PInverse(void)
Definition: nmrPInverse.h:252
const MatrixTypeU & U(void) const
Definition: nmrPInverse.h:615
size_type NMember
Definition: nmrPInverse.h:84
#define cmnThrow(a)
Definition: MinimalCmn.h:4
Implementation of a fixed-size matrix using template metaprogramming.
Definition: vctFixedSizeMatrix.h:52
static size_type WorkspaceSize(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A)
Definition: nmrPInverse.h:230
Definition: nmrPInverse.h:561
pointer Pointer(index_type index=0)
Definition: vctDynamicVectorBase.h:155
const vctDynamicMatrixRef< CISSTNETLIB_DOUBLE > & PInverse(void) const
Definition: nmrPInverse.h:476
A template for a fixed length vector with fixed spacing in memory.
Definition: vctFixedSizeVectorBase.h:76
void Allocate(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A)
Definition: nmrPInverse.h:372
MatrixTypePInverse & PInverse(void)
Definition: nmrPInverse.h:567
vct::size_type size_type
Definition: nmrPInverse.h:500
MatrixTypeU & U(void)
Definition: nmrPInverse.h:573
vctFixedSizeMatrixRef< CISSTNETLIB_DOUBLE, _cols, _cols, _storageOrder?_cols:1, _storageOrder?1:_cols > MatrixTypeVt
Definition: nmrPInverse.h:523
const MatrixTypePInverse & PInverse(void) const
Definition: nmrPInverse.h:609
void SetRefOutput(vctDynamicMatrixBase< _matrixOwnerTypePInverse, CISSTNETLIB_DOUBLE > &pInverse)
Definition: nmrPInverse.h:448
pointer Pointer(size_type index=0)
Definition: vctFixedSizeVectorBase.h:226
vctDynamicVectorRef< CISSTNETLIB_DOUBLE > SReference
Definition: nmrPInverse.h:76
Definition: nmrPInverse.h:504
Declaration of the template function cmnThrow.
vctDynamicMatrixRef< CISSTNETLIB_DOUBLE > PInverseReference
Definition: nmrPInverse.h:73
MatrixTypeVt & Vt(void)
Definition: nmrPInverse.h:576
const bool VCT_COL_MAJOR
Definition: vctForwardDeclarations.h:46
Definition: nmrPInverse.h:497
CISSTNETLIB_INTEGER nmrSVD(vctDynamicMatrixBase< _matrixOwnerType, CISSTNETLIB_DOUBLE > &A, nmrSVDDynamicData &data)
Definition: nmrSVD.h:1000
bool StorageOrder(void)
Definition: nmrPInverse.h:270
static size_type WorkspaceSize(size_type m, size_type n)
Definition: nmrPInverse.h:213
const vctDynamicMatrixRef< CISSTNETLIB_DOUBLE > & Vt(void) const
Definition: nmrPInverse.h:473
Friend(nmrPInverseDynamicData &data)
Definition: nmrPInverse.h:247
nmrPInverseDynamicData(vctDynamicMatrixBase< _matrixOwnerTypeA, CISSTNETLIB_DOUBLE > &A)
Definition: nmrPInverse.h:301
vctFixedSizeVector< CISSTNETLIB_DOUBLE, LWORK > VectorTypeWorkspace
Definition: nmrPInverse.h:519
Definition: vctDynamicVectorBase.h:61
void SetRef(vctDynamicMatrixBase< _matrixOwnerTypePInverse, CISSTNETLIB_DOUBLE > &pInverse, vctDynamicVectorBase< _vectorOwnerTypeWorkspace, CISSTNETLIB_DOUBLE > &workspace)
Definition: nmrPInverse.h:420
vctFixedSizeMatrixRef< CISSTNETLIB_DOUBLE, _rows, _rows, _storageOrder?_rows:1, _storageOrder?1:_rows > MatrixTypeU
Definition: nmrPInverse.h:521