00001
00002
00003 #ifdef Spanish_dox
00004
00005
00006
00007
00008
00009
00010 #endif
00011 #ifdef English_dox
00012
00013
00014
00015
00016
00017
00018 #endif
00019
00020 #ifndef Matrix_Lib_h
00021 #define Matrix_Lib_h // Evita la inclusión múltiple
00022
00023 #include <cassert>
00024
00025 namespace Mx {
00026
00027 #ifdef Spanish_dox
00028
00029 #endif
00030 #ifdef English_dox
00031
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
00040 #endif
00041 #ifdef English_dox
00042
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
00065 #endif
00066 #ifdef English_dox
00067
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
00086 #endif
00087 #ifdef English_dox
00088
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
00099 #endif
00100 #ifdef English_dox
00101
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
00118 #endif
00119 #ifdef English_dox
00120
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
00138 #endif
00139 #ifdef English_dox
00140
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
00160 #endif
00161 #ifdef English_dox
00162
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
00184 #endif
00185 #ifdef English_dox
00186
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 }
00207
00208
00209 #endif // Matrix_Lib_h
00210
00211