MatrixOperations Class Reference

#include <SP_MatrixOperations.h>

List of all members.

Static Public Member Functions

static SPtMatrix< double > EigenValsByQRFactorization (const SPtMatrix< double > &, int=-6, SPtMatrix< double > *=NULL)
static SPtMatrix< double > HouseHolderMatrix (const SPtMatrix< double > &, SPtMatrix< double > *=NULL, SPtMatrix< double > *=NULL)
static bool QRFactorization (const SPtMatrix< double > &, SPtMatrix< double > &, SPtMatrix< double > &)


Detailed Description

Definition at line 23 of file SP_MatrixOperations.h.


Member Function Documentation

SPtMatrix< double > MatrixOperations::EigenValsByQRFactorization ( const SPtMatrix< double > &  A,
int  times = -6,
SPtMatrix< double > *  ptrO = NULL 
) [static]

Definition at line 143 of file SP_MatrixOperations.cpp.

00146 {
00147     // only square matrix
00148     if ( A.m_iRows != A.m_iCols )   return NULL;
00149 
00150     // Given a real n-by-n matrix A:
00151     // Define A_(1) = A
00152     // For k = 1 to times
00153     //      Factor A_(k)   =  Q_(k)R_(k)        (so that Q_(k)'A_(k) = R_(k))
00154     //      Define A_(k+1) =  R_(k)Q_(k)        (so that A_(k+1) = Q_(k)'A_(k)Q_(k))
00155     // end.
00156     double errThreshold = 1.0;
00157     int limit = 10000;
00158     int n = A.m_iRows;
00159     SPtMatrix< double > M( A ), Q( n, n ), R( n, n );
00160     if ( times < 0 ) {
00161         errThreshold = pow( 10.0, times );
00162         times = limit;
00163     }
00164 
00165     for ( int k = 0; k < times; k++ ) {
00166         //cout << k << " ";
00167         
00168         //M.DisplayElements();
00169         
00170         QRFactorization( M, Q, R );
00171         
00172         //cout << "R is \n";
00173         //R.DisplayElements();
00174         //cout << "Q is \n";
00175         //Q.DisplayElements();
00176 
00177         M = R*Q;
00178     
00179         if ( 1.0 > errThreshold ) {
00180             bool isEnough = true;
00181             for ( int r = n/2; r < n; r++ ) {
00182                 for ( int c = 0; c <= n/2; c++ ) {
00183                     if ( r > c ) {
00184                         //cout << "(" << r << ", " << c << ") ";
00185                         if ( fabs(M.m_tElement[r][c]) > errThreshold ) {
00186                             isEnough = false;
00187                             break;
00188                         }
00189                     }
00190                 }
00191             }
00192             if ( isEnough ) {
00193                 cout << "Stopped at Iteration# " << k << "\n";
00194                 break;
00195             }
00196         }
00197     }
00198     //cout << "M_(" << k << ")\n";
00199     //M.DisplayElements();
00200 
00201     if ( ptrO != NULL ) *ptrO = M;
00202 
00203     SPtMatrix< double > outMatrix( n, 1 );
00204     for ( int i = 0; i < n; i++ ) {
00205         outMatrix.m_tElement[i][0] = M.m_tElement[i][i];
00206     }
00207 
00208     return M;
00209     //return outMatrix;
00210 } // END Member Fn: EigenValsByQRFactorization()

SPtMatrix< double > MatrixOperations::HouseHolderMatrix ( const SPtMatrix< double > &  A,
SPtMatrix< double > *  ptrH = NULL,
SPtMatrix< double > *  ptrR = NULL 
) [static]

Definition at line 17 of file SP_MatrixOperations.cpp.

00020 {
00021     // only for square matrix
00022     if ( A.m_iRows != A.m_iCols )   return NULL;
00023     if ( ptrR != NULL ) {
00024         if ( ptrR->m_iRows != A.m_iRows || ptrR->m_iCols != A.m_iCols ) return false;
00025     }
00026 
00027     SPtMatrix< double > B( A );
00028     SPtMatrix< double > H( A );
00029 
00030     int i, k;
00031     double g, s;
00032     int n = H.m_iRows;
00033     SPtMatrix< double > W( n, 1 );
00034     //SPtMatrix< double > V( n, 1 );
00035     SPtMatrix< double > U( n, 1 );
00036     
00037     // Create Identity matrix
00038     SPtMatrix< double > I( n, n );
00039     I.SetToIdentity();
00040     
00041 
00042     for ( k = 0; k < n-1; k++ ) {
00043         //cout << "\nk = " << k << "\n";
00044 
00045         // Step 1: Set W_i = 0 for i = 0, ... , k-2
00046         for ( i = 0; i < k; i++ ) {
00047             W.m_tElement[i][0] = 0.0;
00048         }
00049 
00050         // Step 2.1: Find g
00051         g = 0;
00052         for ( i = k; i < n; i++ ) {
00053             g += B.m_tElement[i][k] * B.m_tElement[i][k];
00054         }
00055         g = sqrt(g);
00056 
00057         // Step 2.2: Find s
00058         s = sqrt( 2 * g * ( g + fabs(B.m_tElement[k][k]) ) );
00059 
00060         // Step 3.1: Set W_k
00061         W.m_tElement[k][0] = ( B.m_tElement[k][k] + (B.m_tElement[k][k] >= 0.0 ? +1.0 : -1.0)*g ) / s;
00062 
00063         // Step 3.2: Set W_i
00064         for ( i = k+1; i < n; i++ ) {
00065             W.m_tElement[i][0] = B.m_tElement[i][k] / s;
00066         }
00067 
00068         //cout << "W" << endl;
00069         //W.DisplayElements();
00070 
00071         U = 2 * B.GetTranspose() * W;
00072 
00073         //cout << "W*U'" << endl;
00074         //(W*U.GetTranspose()).DisplayElements();
00075 
00076         //H -= W*U.GetTranspose();
00077 
00078         H = I - ( 2 * W * W.GetTranspose() );
00079         if ( ptrH != NULL )     ptrH[k] = H;
00080 
00081         B = H*B;
00082     }
00083 
00084     if ( ptrR != NULL )     *ptrR = B;
00085 
00086     return H;
00087 } // END Member Fn: HouseHolderMatrix()

bool MatrixOperations::QRFactorization ( const SPtMatrix< double > &  A,
SPtMatrix< double > &  Q,
SPtMatrix< double > &  R 
) [static]

Definition at line 91 of file SP_MatrixOperations.cpp.

00093 {
00094     // only square matrix
00095     if ( A.m_iRows != A.m_iCols )   return false;
00096     if ( Q.m_iRows != A.m_iRows || Q.m_iCols != A.m_iCols ) return false;
00097     if ( R.m_iRows != A.m_iRows || R.m_iCols != A.m_iCols ) return false;
00098 
00099     int n = A.m_iRows;
00100     int k;
00101 
00102     // Define R_(0) = A
00103     //R = A;
00104     SPtMatrix< double > *arrayH = new SPtMatrix< double >[ n-1 ];
00105 
00106     // For k = 1,...,n-1
00107     for ( k = 0; k < n-1; k++ ) {
00108         // Find H_(k) in order to reduce positions k+1,...,n, 
00109         // in the k_th column of R_(k-1) to zero
00110         HouseHolderMatrix( A, arrayH, &R );
00111         //R = ptrH[n-2] * R;
00112     }
00113 
00114     // Define Q = I;
00115     // For k = n-1,...,1
00116     //    Q = H_(k) * Q
00117     Q.SetToIdentity();
00118     for ( k = n-2; k >= 0; k-- ) {
00119         Q = arrayH[k]*Q;
00120     }
00121 
00122     //cout << "R is\n";
00123     //R.DisplayElements();
00124     //cout << "Q is\n";
00125     //Q.DisplayElements();
00126     //cout << "Q'*Q is ";
00127     //(Q.GetTranspose()*Q).DisplayElements();
00128     //cout << "Q*R is\n";
00129     //(Q*R).DisplayElements();
00130 
00131     delete [] arrayH;
00132 
00133     return true;
00134 } // END Member Fn: QRFactorization()


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

Generated on Mon Oct 13 11:45:11 2008 for TAPs by  doxygen 1.5.6