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 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
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
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
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
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 }
00152
00153 #endif // Matrix_Lib_h
00154
00155