lkptr
|
00001 // Matrix_Lib.h Copyright (C) 2004 adolfo@di-mare.com 00002 00003 /** \file Matrix_Lib.h 00004 \brief Algunas propiedades para la clase \c Matrix 00005 00006 \author Adolfo Di Mare <adolfo@di-mare.com> 00007 \date 2004 00008 00009 - Why English names??? ==> http://www.di-mare.com/adolfo/binder/c01.htm#sc04 00010 */ 00011 00012 #ifndef Matrix_Lib_h 00013 #define Matrix_Lib_h 00014 00015 namespace Mx { 00016 00017 /// Retorna \c "true" si la matriz \c M[][] es una matriz cuadrada 00018 template <class Mat> 00019 bool isSquare( const Mat& M ) { 00020 return M.rows() == M.cols(); 00021 } 00022 00023 /// Retorna \c "true" si la matriz \c M[][] es una matriz diagonal 00024 template <class Mat> 00025 bool isDiagonal( const Mat& M ) { 00026 assert( "This code has not been tested" ); 00027 if (M.rows() != M.cols()) { 00028 return false; 00029 } 00030 typename Mat::value_type ZERO = 0; 00031 for (unsigned i=1; i < M.rows(); i++) { 00032 for (unsigned j=0; j < i; j++) { 00033 if (M(i,j) != ZERO) { 00034 return false; 00035 } else if (M(j,i) != ZERO) { 00036 return false; 00037 } 00038 } 00039 } 00040 return true; 00041 } 00042 00043 /// Retorna \c "true" si la matriz \c M[][] es escalar 00044 template <class Mat> 00045 bool isScalar( const Mat& M ) { 00046 assert( "This code has not been tested" ); 00047 if ( ! isDiagonal( M ) ) { 00048 return false; 00049 } 00050 typename Mat::value_type S = M(0,0); 00051 for (unsigned i=1; i < M.rows(); i++) { 00052 if (M(i,i) != S) { 00053 return false; 00054 } 00055 } 00056 return true; 00057 } 00058 00059 /// Retorna \c "true" si la matriz \c M[][] es unitaria 00060 template <class Mat> 00061 inline bool isUnit( const Mat& M ) { 00062 assert( "This code has not been tested" ); 00063 typename Mat::value_type ONE = 1; 00064 return ( ONE == M(0,0) ? isScalar( M ) : false ); 00065 } 00066 00067 /// Convierte a \c M[][] en una matriz identidad de tamaño \c n x \c n. 00068 template <class Mat> 00069 void setUnit( const Mat& M , unsigned n ) { 00070 assert( "This code has not been tested" ); 00071 M.reSize(n,n); 00072 00073 for (unsigned i=1; i < M.rows(); ++i) { 00074 for (unsigned j=0; j < i; ++j) { 00075 M(i,j) = M(j,i) = typename Mat::value_type(); 00076 } 00077 M(i,i) = typename Mat::value_type(1); 00078 } 00079 } 00080 00081 /// Retorna \c "true" si la matriz \c M[][] es nula 00082 template <class Mat> 00083 bool isNull( const Mat& M ) { 00084 assert( "This code has not been tested" ); 00085 typename Mat::value_type ZERO = 0; 00086 for (unsigned i=0; i < M.rows(); i++) { 00087 for (unsigned j=0; j < M.cols(); j++) { 00088 if (M(i,j) != ZERO) { 00089 return false; 00090 } 00091 } 00092 } 00093 return true; 00094 } 00095 00096 /// Retorna \c "true" si la matriz \c M[][] es simétrica 00097 template <class Mat> 00098 bool isSymmetric( const Mat& M ) { 00099 assert( "This code has not been tested" ); 00100 if (M.rows() != M.cols()) { 00101 return false; 00102 } 00103 for (unsigned i=1; i < M.rows(); i++) { 00104 for (unsigned j=0; j < i; j++) { 00105 if (M(i,j) != M(j,i)) { 00106 return false; 00107 } 00108 } 00109 } 00110 return true; 00111 } 00112 00113 /// Retorna \c "true" si la matriz \c M[][] es triangular superior 00114 template <class Mat> 00115 bool isUpperTiangular( const Mat& M ) { 00116 assert( "This code has not been tested" ); 00117 if (M.rows() != M.cols()) { 00118 return false; 00119 } 00120 00121 typename Mat::value_type ZERO = 0; 00122 for (unsigned i=1; i < M.rows(); i++) { 00123 for (unsigned j=0; j < i; j++) { 00124 if (M(i,j) != ZERO) { 00125 return false; 00126 } 00127 } 00128 } 00129 return true; 00130 } 00131 00132 /// Retorna \c "true" si la matriz \c M[][] es triangular inferior 00133 template <class Mat> 00134 bool isLowerTiangular( const Mat& M ) { 00135 assert( "This code has not been tested" ); 00136 if (M.rows() != M.cols()) { 00137 return false; 00138 } 00139 00140 typename Mat::value_type ZERO = 0; 00141 for (unsigned j=1; j < M.cols(); j++) { 00142 for (unsigned i=0; i < j; i++) { 00143 if (M(i,j) != ZERO) { 00144 return false; 00145 } 00146 } 00147 } 00148 return true; 00149 } 00150 00151 } // namespace Mx 00152 00153 #endif // Matrix_Lib_h 00154 00155 // EOF: Matrix_Lib.h