QuOc

 

Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes

aol::QRInverse< DataType > Class Template Reference

#include <matrixInverse.h>

Inheritance diagram for aol::QRInverse< DataType >:
aol::Matrix< DataType > aol::MatrixAbstractBase< DataType > aol::GenSparseOp< DataType > aol::Op< aol::Vector< DataType > >

List of all members.

Public Member Functions

 QRInverse (const QRInverse< DataType > &inv)
 Copy constructor.
QRInverse< DataType > & operator= (const QRInverse< DataType > &inv)
 Assignment operator.
 QRInverse (const Matrix< DataType > &m)
 Construct from matrix via QR decomposition.
void apply (const Vector< DataType > &arg, Vector< DataType > &dest) const
 Apply solves the QR system.
void applyAdd (const Vector< DataType > &arg, Vector< DataType > &dest) const
 Add the solution to the previous value of the destination vector.
DataType get (int row, int col) const
 Compute entry in inverse matrix, slow.
FullMatrix< DataTypegetFM () const
 Compute direct representation of inverse, for output, debugging etc.
FullMatrix< DataTypegetQ () const
 Compute direct representation of reflection.
FullMatrix< DataTypegetR () const
 Show upper triangluar part of decomposition.

Protected Member Functions

 QRInverse ()
 Default constructor not allowed.

Private Member Functions

void set (int, int, DataType)
 Write operations to single entries not permitted.
void add (int, int, DataType)
 Write operations to single entries not permitted.
void setZero ()
 Sets matrix entries to zeros.

Private Attributes

FullMatrix< DataType_decomposition
 Stores the QR decomposition.
Vector< DataType_lengths
 Stores lengths of reflected vectors.

Detailed Description

template<class DataType>
class aol::QRInverse< DataType >

Definition at line 171 of file matrixInverse.h.


Constructor & Destructor Documentation

template<class DataType >
aol::QRInverse< DataType >::QRInverse (  )  [protected]

Default constructor not allowed.

Definition at line 613 of file matrixInverse.h.

  : Matrix<DataType> (), _decomposition (), _lengths () {}

template<class DataType >
aol::QRInverse< DataType >::QRInverse ( const QRInverse< DataType > &  inv  ) 

Copy constructor.

Definition at line 619 of file matrixInverse.h.

    : Matrix<DataType> ( inv.getNumRows (), inv.getNumCols () ),
    _decomposition ( inv._decomposition ),
    _lengths ( inv._lengths ) {}

template<class DataType >
aol::QRInverse< DataType >::QRInverse ( const Matrix< DataType > &  m  ) 

Construct from matrix via QR decomposition.

Definition at line 640 of file matrixInverse.h.

References aol::QRInverse< DataType >::_decomposition, aol::QRInverse< DataType >::_lengths, aol::computeReflection(), aol::doReflectionColumn(), aol::GenSparseOp< RealType >::getNumCols(), aol::GenSparseOp< RealType >::getNumRows(), aol::FullMatrix< _DataType >::getSubColumn(), and aol::writeReflection().

    : Matrix<DataType> ( m.getNumCols (), m.getNumRows () ),
    _decomposition ( m ),
    _lengths ( m.getNumCols () ) {
  if ( m.getNumCols () > m.getNumRows () )
    throw OutOfBoundsException ( "Matrix must be square or higher "
                                 "for QR inversion", __FILE__, __LINE__ );

  int r = m.getNumRows (), c = m.getNumCols ();

  for ( int i = 0; i < c; ++i ) {
    // Consider entries [i;r) of column i
    Vector<DataType> v ( r - i );
    _decomposition.getSubColumn ( i, i, v );

    // Compute vector normal to reflection hyperplane, eliminating entries (i;n)
    DataType alpha;
    computeReflection ( v, alpha );

    // Reflect all columns
    #ifdef _OPENMP
    #pragma omp parallel for
    #endif
    for ( int j = i + 1; j < c; ++j ) { // Result in current column is clear

      doReflectionColumn ( _decomposition, j, v, alpha );
    }

    // Store reflection vector
    writeReflection ( _decomposition, _lengths, i, v, alpha );
  }
}


Member Function Documentation

template<class DataType >
void aol::QRInverse< DataType >::add ( int  ,
int  ,
DataType   
) [private, virtual]

Write operations to single entries not permitted.

Reimplemented from aol::Matrix< DataType >.

Definition at line 796 of file matrixInverse.h.

                                                   {
  throw UnimplementedCodeException ( "Cannot write to QR Inverse", __FILE__, __LINE__ );
}

template<class DataType >
void aol::QRInverse< DataType >::apply ( const Vector< DataType > &  arg,
Vector< DataType > &  dest 
) const [inline]

Apply solves the QR system.

Definition at line 676 of file matrixInverse.h.

References aol::QRInverse< DataType >::_decomposition, aol::QRInverse< DataType >::_lengths, aol::doReflectionVector(), aol::FullMatrix< _DataType >::get(), aol::GenSparseOp< RealType >::getNumCols(), and aol::readReflection().

Referenced by aol::QRInverse< DataType >::applyAdd(), aol::QRInverse< DataType >::get(), and aol::QRInverse< DataType >::getFM().

                                                                                            {
  int c = _decomposition.getNumCols ();

  Vector<DataType> rhs ( arg ), v;
  DataType alpha;

  // Reflect right hand side like matrix
  for ( int i = 0; i < c; ++i ) {

    // Reflect subblock
    readReflection ( _decomposition, _lengths, i, v, alpha );
    doReflectionVector ( rhs, v, alpha );
  }

  // Solve triangular system, diagonal is in _lengths
  for ( int i = c - 1; i >= 0; --i ) {

    dest [i] = rhs [i];

    for ( int j = i + 1; j < c; ++j )
      dest [i] -= dest [j] * _decomposition.get ( i, j );

    dest [i] /= _lengths [i];
  }
}

template<class DataType >
void aol::QRInverse< DataType >::applyAdd ( const Vector< DataType > &  arg,
Vector< DataType > &  dest 
) const [inline]

Add the solution to the previous value of the destination vector.

Reimplemented from aol::Matrix< DataType >.

Definition at line 705 of file matrixInverse.h.

References aol::QRInverse< DataType >::apply().

                                                                                               {
  Vector<DataType> temp ( dest );
  apply ( arg, dest );
  dest += temp;
}

template<class DataType >
DataType aol::QRInverse< DataType >::get ( int  row,
int  col 
) const [virtual]

Compute entry in inverse matrix, slow.

Implements aol::Matrix< DataType >.

Definition at line 714 of file matrixInverse.h.

References aol::GenSparseOp< DataType >::_numCols, aol::GenSparseOp< DataType >::_numRows, aol::QRInverse< DataType >::apply(), and aol::Vector< _DataType >::setZero().

                                                           {
    // Use getFM if all entries are needed
    cerr << "Warning: QRInverse<DataType>::get is slow." << endl;

    Vector<DataType> colv ( this->_numCols );
    Vector<DataType> resv ( this->_numRows );
    colv.setZero ();
    colv [col] = 1.0;
    apply ( colv, resv );
    return resv [row];
  }

template<class DataType >
FullMatrix< DataType > aol::QRInverse< DataType >::getFM (  )  const

Compute direct representation of inverse, for output, debugging etc.

Definition at line 729 of file matrixInverse.h.

References aol::GenSparseOp< DataType >::_numCols, aol::GenSparseOp< DataType >::_numRows, aol::QRInverse< DataType >::apply(), aol::FullMatrix< _DataType >::set(), and aol::Vector< _DataType >::setZero().

                                                       {
  // Apply to columns of unit matrix
  FullMatrix<DataType> m ( this->_numRows, this->_numCols );
  Vector<DataType> x ( this->_numRows ), b ( this->_numCols );

  b.setZero ();
  for ( int i = 0; i < this->N; ++i ) {
    b [i] = 1;
    if ( i ) b [i - 1] = 0;
    apply ( b, x );
    for ( int j = 0; j < this->M; ++j )
      m.set ( j, i, x [j] );
  }

  return m;
}

template<class DataType >
FullMatrix< DataType > aol::QRInverse< DataType >::getQ (  )  const

Compute direct representation of reflection.

Definition at line 765 of file matrixInverse.h.

References aol::QRInverse< DataType >::_decomposition, aol::QRInverse< DataType >::_lengths, aol::GenSparseOp< DataType >::_numCols, aol::doReflectionColumn(), aol::readReflection(), and aol::Matrix< _DataType >::setIdentity().

                                                      {
  FullMatrix<DataType> m ( this->_numCols, this->_numCols );

  // Applies all reflections successively to the unit matrix
  // Must start from end since inverse is wanted
  m.setIdentity ();
  Vector<DataType> v ( this->_numCols );
  DataType alpha;

  // For all reflections
  for ( int i = this->_numCols - 2; i >= 0; --i ) {

    readReflection ( _decomposition, _lengths, i, v, alpha );

    // For all columns of m where the reflection does something
    for ( int j = i; j < this->_numCols; ++j )
      doReflectionColumn ( m, j, v, alpha );
  }
  return m;
}

template<class DataType >
FullMatrix< DataType > aol::QRInverse< DataType >::getR (  )  const

Show upper triangluar part of decomposition.

Definition at line 749 of file matrixInverse.h.

References aol::QRInverse< DataType >::_decomposition, aol::QRInverse< DataType >::_lengths, aol::GenSparseOp< DataType >::_numCols, aol::GenSparseOp< DataType >::_numRows, aol::FullMatrix< _DataType >::get(), aol::FullMatrix< _DataType >::set(), and aol::FullMatrix< _DataType >::setZero().

                                                      {
  FullMatrix<DataType> m ( this->_numCols, this->_numRows );
  m.setZero ();
  for ( int i = 0; i < this->_numRows; ++i )
    m.set ( i, i, _lengths [i] );

  for ( int i = 0; i < this->_numCols; ++i )
    for ( int j = i + 1; j < this->_numRows; ++j )
      m.set ( i, j, _decomposition.get ( i, j ) );

  return m;
}

template<class DataType >
QRInverse< DataType > & aol::QRInverse< DataType >::operator= ( const QRInverse< DataType > &  inv  ) 

Assignment operator.

Definition at line 627 of file matrixInverse.h.

References aol::QRInverse< DataType >::_decomposition, and aol::QRInverse< DataType >::_lengths.

                                                                                      {
  // Beware of self-assignment
  if ( this == &inv ) return *this;

  _decomposition = inv._decomposition;
  _lengths = inv._lengths;

  return *this;
}

template<class DataType >
void aol::QRInverse< DataType >::set ( int  ,
int  ,
DataType   
) [private, virtual]

Write operations to single entries not permitted.

Implements aol::Matrix< DataType >.

Definition at line 789 of file matrixInverse.h.

                                                   {
  throw UnimplementedCodeException ( "Cannot write to QR Inverse", __FILE__, __LINE__ );
}

template<class DataType>
void aol::QRInverse< DataType >::setZero (  )  [inline, private, virtual]

Sets matrix entries to zeros.

Implements aol::MatrixAbstractBase< DataType >.

Definition at line 219 of file matrixInverse.h.

                 {
    throw aol::UnimplementedCodeException ( "aol::QRInverse::setZero() "
                                            "does not make sense for this class.",
                                            __FILE__, __LINE__ );
  }


Member Data Documentation


The documentation for this class was generated from the following file:

Generated on Fri Sep 9 2011 21:09:09 for QuocMesh by doxygen 1.7.1