Página principal | Lista de namespace | Lista de componentes | Lista de archivos | Miembros del Namespace  | Miembros de las clases | Archivos de los miembros

Matrix_Lib.h

Ir a la documentación de este archivo.
00001 // Matrix_Lib.h Copyright (C) 2004  adolfo@di-mare.com
00002 
00003 #ifdef Spanish_dox
00004 /** \file  Matrix_Lib.h
00005     \brief Funciones para manipular \c Matrix_BASE<>.
00006     \author Adolfo Di Mare <adolfo@di-mare.com>
00007     \date   2004
00008     - Why English names??? ==> http://www.di-mare.com/adolfo/binder/c01.htm#sc04
00009 */
00010 #endif
00011 #ifdef English_dox
00012 /** \file  Matrix_Lib.h
00013     \brief Functions to manipulate \c Matrix_BASE<>.
00014     \author Adolfo Di Mare <adolfo@di-mare.com>
00015     \date   2004
00016     - Why English names??? ==> http://www.di-mare.com/adolfo/binder/c01.htm#sc04
00017 */
00018 #endif
00019 
00020 #ifndef   Matrix_Lib_h
00021 #define   Matrix_Lib_h   // Evita la inclusión múltiple
00022 
00023 #include <cassert> // assert()
00024 
00025 namespace Mx {
00026 
00027 #ifdef Spanish_dox
00028 /// Retorna \c "true" si la matriz \c M[][] es una matriz cuadrada.
00029 #endif
00030 #ifdef English_dox
00031 /// Returns \c "true" if matrix \c M[][] is square.
00032 #endif
00033 template <class MAT>
00034 bool isSquare( const MAT& M ) {
00035     return M.rows() == M.cols();
00036 }
00037 
00038 #ifdef Spanish_dox
00039 /// Retorna \c "true" si la matriz \c M[][] es una matriz diagonal.
00040 #endif
00041 #ifdef English_dox
00042 /// Returns \c "true" if matrix \c M[][] is diagonal.
00043 #endif
00044 template <class MAT>
00045 bool isDiagonal( const MAT& M ) {
00046     assert( "This code has not been tested" );
00047     if (M.rows() != M.cols()) {
00048       return false;
00049     }
00050     typename MAT::value_type ZERO = 0;
00051     for (unsigned i=0; i < M.rows(); i++) {
00052         for (unsigned j=0; j < i; j++) {
00053             if (M(i,j) !=  ZERO) {
00054                 return false;
00055             } else if (M(j,i) !=  ZERO) {
00056                 return false;
00057             }
00058         }
00059     }
00060     return true;
00061 }
00062 
00063 #ifdef Spanish_dox
00064 /// Retorna \c "true" si la matriz \c M[][] es escalar.
00065 #endif
00066 #ifdef English_dox
00067 /// Returns \c "true" if matrix \c M[][] is scalar.
00068 #endif
00069 template <class MAT>
00070 bool isScalar( const MAT& M ) {
00071     assert( "This code has not been tested" );
00072     if ( ! isDiagonal( M ) ) {
00073         return false;
00074     }
00075     typename MAT::value_type S = M(0,0);
00076     for (unsigned i=1; i < M.rows(); i++) {
00077         if (M(i,i) != S) {
00078             return false;
00079         }
00080     }
00081    return true;
00082 }
00083 
00084 #ifdef Spanish_dox
00085 /// Retorna \c "true" si la matriz \c M[][] es unitaria.
00086 #endif
00087 #ifdef English_dox
00088 /// Returns \c "true" if matrix \c M[][] is a unit matrix.
00089 #endif
00090 template <class MAT>
00091 inline bool isUnit( const MAT& M ) {
00092     assert( "This code has not been tested" );
00093     typename MAT::value_type ONE = 1;
00094     return ( ONE == M(0,0) ? isScalar( M ) : false );
00095 }
00096 
00097 #ifdef Spanish_dox
00098 /// Convierte a \c M[][] en una matriz identidad de tamaño \c n x \c n.
00099 #endif
00100 #ifdef English_dox
00101 /// Transforms \c M[][] into a identity matrix of size \c n x \c n.
00102 #endif
00103 template <class MAT>
00104 void setUnit( MAT& M , unsigned n ) {
00105     assert( "This code has not been tested" );
00106     M.reSize(n,n);
00107 
00108     for (unsigned i=0; i < M.rows(); ++i) {
00109         for (unsigned j=0; j < i; ++j) {
00110             M(i,j) = M(j,i) = typename MAT::value_type();
00111         }
00112         M(i,i) = typename MAT::value_type(1);
00113     }
00114 }
00115 
00116 #ifdef Spanish_dox
00117 /// Retorna \c "true" si la matriz \c M[][] es nula.
00118 #endif
00119 #ifdef English_dox
00120 /// Returns \c "true" if matrix \c M[][] is null.
00121 #endif
00122 template <class MAT>
00123 bool isNull( const MAT& M ) {
00124     assert( "This code has not been tested" );
00125     typename MAT::value_type ZERO = 0;
00126     for (unsigned i=0; i < M.rows(); i++) {
00127         for (unsigned j=0; j < M.cols(); j++) {
00128             if (M(i,j) != ZERO) {
00129                 return false;
00130             }
00131         }
00132     }
00133     return true;
00134 }
00135 
00136 #ifdef Spanish_dox
00137 /// Retorna \c "true" si la matriz \c M[][] es simétrica.
00138 #endif
00139 #ifdef English_dox
00140 /// Returns \c "true" if matrix \c M[][] is symetric.
00141 #endif
00142 template <class MAT>
00143 bool isSymmetric( const MAT& M ) {
00144     assert( "This code has not been tested" );
00145     if (M.rows() != M.cols()) {
00146         return false;
00147     }
00148     for (unsigned i=0; i < M.rows(); i++) {
00149         for (unsigned j=0; j < i; j++) {
00150             if (M(i,j) != M(j,i)) {
00151                 return false;
00152             }
00153         }
00154     }
00155     return true;
00156 }
00157 
00158 #ifdef Spanish_dox
00159 /// Retorna \c "true" si la matriz \c M[][] es triangular superior.
00160 #endif
00161 #ifdef English_dox
00162 /// Returns \c "true" if matrix \c M[][] is upper triangular.
00163 #endif
00164 template <class MAT>
00165 bool isUpperTriangular( const MAT& M ) {
00166     assert( "This code has not been tested" );
00167     if (M.rows() != M.cols()) {
00168         return false;
00169     }
00170 
00171     typename MAT::value_type ZERO = 0;
00172     for (unsigned i=1; i < M.rows(); i++) {
00173         for (unsigned j=0; j < i; j++) {
00174             if (M(i,j) != ZERO) {
00175                 return false;
00176             }
00177         }
00178     }
00179     return true;
00180 }
00181 
00182 #ifdef Spanish_dox
00183 /// Retorna \c "true" si la matriz \c M[][] es triangular inferior.
00184 #endif
00185 #ifdef English_dox
00186 /// Returns \c "true" if matrix \c M[][] is lower triangular.
00187 #endif
00188 template <class MAT>
00189 bool isLowerTriangular( const MAT& M ) {
00190     assert( "This code has not been tested" );
00191     if (M.rows() != M.cols()) {
00192         return false;
00193     }
00194 
00195     typename MAT::value_type ZERO = 0;
00196     for (unsigned j=1; j < M.cols(); j++) {
00197         for (unsigned i=0; i < j; i++) {
00198             if (M(i,j) != ZERO) {
00199                 return false;
00200             }
00201         }
00202     }
00203     return true;
00204 }
00205 
00206 } // namespace Mx
00207 
00208 
00209 #endif // Matrix_Lib_h
00210 
00211 // EOF: Matrix_Lib.h

Generado el Wed Aug 28 09:27:07 2013 para Uso de Mx::Matrix: por  doxygen 1.3.9.1