Changeset 4286


Ignore:
Timestamp:
Jan 30, 2023, 3:08:38 AM (8 weeks ago)
Author:
Peter
Message:

provide the SVD inverse function as a member function so the decomposition can be recycled.

Location:
trunk/yat/utility
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/Matrix.cc

    r4207 r4286  
    260260    assert(A.rows() == A.columns());
    261261    SVD svd(A);
    262     // A = U S V'
    263262    svd.decompose();
    264 
    265     // result = A^-1 = V * S^-1 * U'
    266 
    267     DiagonalMatrix D(A.rows(), A.columns());
    268     // If eigenvalue is zero, A is not invertable, perhaps that should
    269     // trigger an exception
    270     for (size_t i=0; i<D.rows(); ++i)
    271       D(i) = 1.0 / svd.s()(i);
    272 
    273     result = svd.V() * D * transpose(svd.U());
    274 
     263    svd.inverse(result);
    275264    assert(result.columns() == result.rows());
    276265    assert(result.rows() == A.rows());
  • trunk/yat/utility/SVD.cc

    r4207 r4286  
    3030#include "SVD.h"
    3131
     32#include "DiagonalMatrix.h"
    3233#include "Exception.h"
    3334#include "Vector.h"
     
    109110
    110111
     112  void SVD::inverse(Matrix& result) const
     113  {
     114    assert(U_.rows()==U_.columns() && "input matrix must be square");
     115    // A = U S V'
     116
     117    // result = A^-1 = V * S^-1 * U'
     118
     119    DiagonalMatrix D(s_.size(), s_.size());
     120    // If eigenvalue is zero, A is not invertable, perhaps that should
     121    // trigger an exception
     122    for (size_t i=0; i<D.rows(); ++i)
     123      D(i) = 1.0 / s_(i);
     124
     125    result = V_ * D * transpose(U_);
     126  }
     127
     128
    111129  const Vector& SVD::s(void) const
    112130  {
  • trunk/yat/utility/SVD.h

    r4207 r4286  
    101101
    102102    /**
     103       Calculate the inverse of the Matrix provided in the
     104       constructor. Behaviour is undefined if the input matrix is not
     105       square.
     106
     107       \since New in yat 0.21
     108
     109       \see inverse_svd(const MatrixBase&, Matrix&)
     110     */
     111    void inverse(Matrix& result) const;
     112
     113    /**
    103114       \brief Access to the s vector.
    104115
Note: See TracChangeset for help on using the changeset viewer.