00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef Matrix_Lib_h
00013 #define Matrix_Lib_h
00014
00015 namespace Mx {
00016
00017
00018 template <class Mat>
00019 bool isSquare( const Mat& M ) {
00020 return M.rows() == M.cols();
00021 }
00022
00023
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
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
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
00068 template <class Mat>
00069 bool isNull( const Mat& M ) {
00070 assert( "This code has not been tested" );
00071 typename Mat::value_type ZERO = 0;
00072 for (unsigned i=0; i < M.rows(); i++) {
00073 for (unsigned j=0; j < M.cols(); j++) {
00074 if (M(i,j) != ZERO) {
00075 return false;
00076 }
00077 }
00078 }
00079 return true;
00080 }
00081
00082
00083 template <class Mat>
00084 bool isSymmetric( const Mat& M ) {
00085 assert( "This code has not been tested" );
00086 if (M.rows() != M.cols()) {
00087 return false;
00088 }
00089 for (unsigned i=1; i < M.rows(); i++) {
00090 for (unsigned j=0; j < i; j++) {
00091 if (M(i,j) != M(j,i)) {
00092 return false;
00093 }
00094 }
00095 }
00096 return true;
00097 }
00098
00099
00100 template <class Mat>
00101 bool isUpperTiangular( const Mat& M ) {
00102 assert( "This code has not been tested" );
00103 if (M.rows() != M.cols()) {
00104 return false;
00105 }
00106
00107 typename Mat::value_type ZERO = 0;
00108 for (unsigned i=1; i < M.rows(); i++) {
00109 for (unsigned j=0; j < i; j++) {
00110 if (M(i,j) != ZERO) {
00111 return false;
00112 }
00113 }
00114 }
00115 return true;
00116 }
00117
00118
00119 template <class Mat>
00120 bool isLowerTiangular( const Mat& M ) {
00121 assert( "This code has not been tested" );
00122 if (M.rows() != M.cols()) {
00123 return false;
00124 }
00125
00126 typename Mat::value_type ZERO = 0;
00127 for (unsigned j=1; j < M.cols(); j++) {
00128 for (unsigned i=0; i < j; i++) {
00129 if (M(i,j) != ZERO) {
00130 return false;
00131 }
00132 }
00133 }
00134 return true;
00135 }
00136
00137 }
00138
00139 #endif // Matrix_Lib_h
00140
00141