// Matrix_Lib.h Copyright (C) 2004 adolfo@di-mare.com /** \file Matrix_Lib.h \brief Algunas propiedades para la clase \c Matrix \author Adolfo Di Mare \date 2004 - Why English names??? ==> http://www.di-mare.com/adolfo/binder/c01.htm#sc04 */ #ifndef Matrix_Lib_h #define Matrix_Lib_h namespace Mx { /// Retorna \c "true" si la matriz \c M[][] es una matriz cuadrada template bool isSquare( const Mat& M ) { return M.rows() == M.cols(); } /// Retorna \c "true" si la matriz \c M[][] es una matriz diagonal template bool isDiagonal( const Mat& M ) { assert( "This code has not been tested" ); if (M.rows() != M.cols()) { return false; } typename Mat::value_type ZERO = 0; for (unsigned i=1; i < M.rows(); i++) { for (unsigned j=0; j < i; j++) { if (M(i,j) != ZERO) { return false; } else if (M(j,i) != ZERO) { return false; } } } return true; } /// Retorna \c "true" si la matriz \c M[][] es escalar template bool isScalar( const Mat& M ) { assert( "This code has not been tested" ); if ( ! isDiagonal( M ) ) { return false; } typename Mat::value_type S = M(0,0); for (unsigned i=1; i < M.rows(); i++) { if (M(i,i) != S) { return false; } } return true; } /// Retorna \c "true" si la matriz \c M[][] es unitaria template inline bool isUnit( const Mat& M ) { assert( "This code has not been tested" ); typename Mat::value_type ONE = 1; return ( ONE == M(0,0) ? isScalar( M ) : false ); } /// Convierte a \c M[][] en una matriz identidad de tamaño \c n x \c n. template void setUnit( const Mat& M , unsigned n ) { assert( "This code has not been tested" ); M.reSize(n,n); for (unsigned i=1; i < M.rows(); ++i) { for (unsigned j=0; j < i; ++j) { M(i,j) = M(j,i) = typename Mat::value_type(); } M(i,i) = typename Mat::value_type(1); } } /// Retorna \c "true" si la matriz \c M[][] es nula template bool isNull( const Mat& M ) { assert( "This code has not been tested" ); typename Mat::value_type ZERO = 0; for (unsigned i=0; i < M.rows(); i++) { for (unsigned j=0; j < M.cols(); j++) { if (M(i,j) != ZERO) { return false; } } } return true; } /// Retorna \c "true" si la matriz \c M[][] es simétrica template bool isSymmetric( const Mat& M ) { assert( "This code has not been tested" ); if (M.rows() != M.cols()) { return false; } for (unsigned i=1; i < M.rows(); i++) { for (unsigned j=0; j < i; j++) { if (M(i,j) != M(j,i)) { return false; } } } return true; } /// Retorna \c "true" si la matriz \c M[][] es triangular superior template bool isUpperTiangular( const Mat& M ) { assert( "This code has not been tested" ); if (M.rows() != M.cols()) { return false; } typename Mat::value_type ZERO = 0; for (unsigned i=1; i < M.rows(); i++) { for (unsigned j=0; j < i; j++) { if (M(i,j) != ZERO) { return false; } } } return true; } /// Retorna \c "true" si la matriz \c M[][] es triangular inferior template bool isLowerTiangular( const Mat& M ) { assert( "This code has not been tested" ); if (M.rows() != M.cols()) { return false; } typename Mat::value_type ZERO = 0; for (unsigned j=1; j < M.cols(); j++) { for (unsigned i=0; i < j; i++) { if (M(i,j) != ZERO) { return false; } } } return true; } } // namespace Mx #endif // Matrix_Lib_h // EOF: Matrix_Lib.h