00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef _MATRIX_H_
00025 #define _MATRIX_H_
00026
00027 #include <cassert>
00028 #include <cstdlib>
00029 #include <algorithm>
00030
00031 template <class T>
00032 class Matrix {
00033 public:
00034 Matrix()
00035 {
00036 m_rows = 0;
00037 m_columns = 0;
00038 m_matrix = NULL;
00039 }
00040
00041 Matrix(int rows, int columns)
00042 {
00043 m_matrix = NULL;
00044 resize(rows, columns);
00045 }
00046
00047 Matrix(const Matrix<T> &other)
00048 {
00049 if ( other.m_matrix != NULL ) {
00050
00051 m_matrix = NULL;
00052 resize(other.m_rows, other.m_columns);
00053 for ( int i = 0 ; i < m_rows ; i++ )
00054 for ( int j = 0 ; j < m_columns ; j++ )
00055 m_matrix[i][j] = other.m_matrix[i][j];
00056 } else {
00057 m_matrix = NULL;
00058 m_rows = 0;
00059 m_columns = 0;
00060 }
00061 }
00062
00063 Matrix<T> & operator= (const Matrix<T> &other)
00064 {
00065 if ( other.m_matrix != NULL ) {
00066
00067 resize(other.m_rows, other.m_columns);
00068 for ( int i = 0 ; i < m_rows ; i++ )
00069 for ( int j = 0 ; j < m_columns ; j++ )
00070 m_matrix[i][j] = other.m_matrix[i][j];
00071 } else {
00072
00073 for ( int i = 0 ; i < m_columns ; i++ )
00074 delete [] m_matrix[i];
00075
00076 delete [] m_matrix;
00077
00078 m_matrix = NULL;
00079 m_rows = 0;
00080 m_columns = 0;
00081 }
00082
00083 return *this;
00084 }
00085
00086 ~Matrix()
00087 {
00088 if ( m_matrix != NULL ) {
00089
00090 for ( int i = 0 ; i < m_rows ; i++ )
00091 delete [] m_matrix[i];
00092
00093 delete [] m_matrix;
00094 }
00095 m_matrix = NULL;
00096 }
00097
00098
00099 void resize(int rows, int columns)
00100 {
00101 if ( m_matrix == NULL ) {
00102
00103 m_matrix = new T*[rows];
00104 for ( int i = 0 ; i < rows ; i++ )
00105 m_matrix[i] = new T[columns];
00106
00107 m_rows = rows;
00108 m_columns = columns;
00109 clear();
00110 } else {
00111
00112 T **new_matrix;
00113
00114 new_matrix = new T*[rows];
00115 for ( int i = 0 ; i < rows ; i++ ) {
00116 new_matrix[i] = new T[columns];
00117 for ( int j = 0 ; j < columns ; j++ )
00118 new_matrix[i][j] = 0;
00119 }
00120
00121
00122 int minrows = std::min<int>(rows, m_rows);
00123 int mincols = std::min<int>(columns, m_columns);
00124 for ( int x = 0 ; x < minrows ; x++ )
00125 for ( int y = 0 ; y < mincols ; y++ )
00126 new_matrix[x][y] = m_matrix[x][y];
00127
00128
00129 if ( m_matrix != NULL ) {
00130 for ( int i = 0 ; i < m_rows ; i++ )
00131 delete [] m_matrix[i];
00132
00133 delete [] m_matrix;
00134 }
00135
00136 m_matrix = new_matrix;
00137 }
00138
00139 m_rows = rows;
00140 m_columns = columns;
00141 }
00142
00143 void identity(void)
00144 {
00145 assert( m_matrix != NULL );
00146
00147 clear();
00148
00149 int x = std::min<int>(m_rows, m_columns);
00150 for ( int i = 0 ; i < x ; i++ )
00151 m_matrix[i][i] = 1;
00152 }
00153
00154 void clear(void)
00155 {
00156 assert( m_matrix != NULL );
00157
00158 for ( int i = 0 ; i < m_rows ; i++ )
00159 for ( int j = 0 ; j < m_columns ; j++ )
00160 m_matrix[i][j] = 0;
00161 }
00162
00163 T& operator () (int x, int y)
00164 {
00165 assert ( x >= 0 );
00166 assert ( y >= 0 );
00167 assert ( x < m_rows );
00168 assert ( y < m_columns );
00169 assert ( m_matrix != NULL );
00170 return m_matrix[x][y];
00171 }
00172
00173 T trace(void)
00174 {
00175 assert( m_matrix != NULL );
00176
00177 T value = 0;
00178
00179 int x = std::min<int>(m_rows, m_columns);
00180 for ( int i = 0 ; i < x ; i++ )
00181 value += m_matrix[i][i];
00182
00183 return value;
00184 }
00185
00186
00187 Matrix<T>& transpose(void)
00188 {
00189 assert( m_rows > 0 );
00190 assert( m_columns > 0 );
00191
00192 int new_rows = m_columns;
00193 int new_columns = m_rows;
00194
00195 if ( m_rows != m_columns ) {
00196
00197 int m = std::max<int>(m_rows, m_columns);
00198 resize(m,m);
00199 }
00200
00201 for ( int i = 0 ; i < m_rows ; i++ ) {
00202 for ( int j = i+1 ; j < m_columns ; j++ ) {
00203 T tmp = m_matrix[i][j];
00204 m_matrix[i][j] = m_matrix[j][i];
00205 m_matrix[j][i] = tmp;
00206 }
00207 }
00208
00209 if ( new_columns != new_rows ) {
00210
00211 resize(new_rows, new_columns);
00212 }
00213
00214 return *this;
00215 }
00216
00217 Matrix<T> product(Matrix<T> &other)
00218 {
00219 assert( m_matrix != NULL );
00220 assert( other.m_matrix != NULL );
00221 assert ( m_columns == other.m_rows );
00222
00223 Matrix<T> out(m_rows, other.m_columns);
00224
00225 for ( int i = 0 ; i < out.m_rows ; i++ ) {
00226 for ( int j = 0 ; j < out.m_columns ; j++ ) {
00227 for ( int x = 0 ; x < m_columns ; x++ ) {
00228 out(i,j) += m_matrix[i][x] * other.m_matrix[x][j];
00229 }
00230 }
00231 }
00232
00233 return out;
00234 }
00235
00236 int minsize(void)
00237 {
00238 return ((m_rows < m_columns) ? m_rows : m_columns);
00239 }
00240
00241 int columns(void)
00242 {
00243 return m_columns;
00244 }
00245
00246 int rows(void)
00247 {
00248 return m_rows;
00249 }
00250 private:
00251 T **m_matrix;
00252 int m_rows;
00253 int m_columns;
00254 };
00255
00256
00257
00258
00259
00260
00261 #endif
00262