matrix.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright (c) 2007 John Weaver
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #ifndef _MATRIX_H_
25 #define _MATRIX_H_
26 
27 #include <cassert>
28 #include <cstdlib>
29 #include <algorithm>
30 
31 template <class T>
32 class Matrix {
33  public:
35  {
36  m_rows = 0;
37  m_columns = 0;
38  m_matrix = NULL;
39  }
40 
41  Matrix(int rows, int columns)
42  {
43  m_matrix = NULL;
44  resize(rows, columns);
45  }
46 
47  Matrix(const Matrix<T> &other)
48  {
49  if ( other.m_matrix != NULL ) {
50  // copy arrays
51  m_matrix = NULL;
52  resize(other.m_rows, other.m_columns);
53  for ( int i = 0 ; i < m_rows ; i++ )
54  for ( int j = 0 ; j < m_columns ; j++ )
55  m_matrix[i][j] = other.m_matrix[i][j];
56  } else {
57  m_matrix = NULL;
58  m_rows = 0;
59  m_columns = 0;
60  }
61  }
62 
63  Matrix<T> & operator= (const Matrix<T> &other)
64  {
65  if ( other.m_matrix != NULL ) {
66  // copy arrays
67  resize(other.m_rows, other.m_columns);
68  for ( int i = 0 ; i < m_rows ; i++ )
69  for ( int j = 0 ; j < m_columns ; j++ )
70  m_matrix[i][j] = other.m_matrix[i][j];
71  } else {
72  // free arrays
73  for ( int i = 0 ; i < m_columns ; i++ )
74  delete [] m_matrix[i];
75 
76  delete [] m_matrix;
77 
78  m_matrix = NULL;
79  m_rows = 0;
80  m_columns = 0;
81  }
82 
83  return *this;
84  }
85 
87  {
88  if ( m_matrix != NULL ) {
89  // free arrays
90  for ( int i = 0 ; i < m_rows ; i++ )
91  delete [] m_matrix[i];
92 
93  delete [] m_matrix;
94  }
95  m_matrix = NULL;
96  }
97 
98  // all operations except product modify the matrix in-place.
99  void resize(int rows, int columns)
100  {
101  if ( m_matrix == NULL ) {
102  // alloc arrays
103  m_matrix = new T*[rows]; // rows
104  for ( int i = 0 ; i < rows ; i++ )
105  m_matrix[i] = new T[columns]; // columns
106 
107  m_rows = rows;
108  m_columns = columns;
109  clear();
110  } else {
111  // save array pointer
112  T **new_matrix;
113  // alloc new arrays
114  new_matrix = new T*[rows]; // rows
115  for ( int i = 0 ; i < rows ; i++ ) {
116  new_matrix[i] = new T[columns]; // columns
117  for ( int j = 0 ; j < columns ; j++ )
118  new_matrix[i][j] = 0;
119  }
120 
121  // copy data from saved pointer to new arrays
122  int minrows = std::min<int>(rows, m_rows);
123  int mincols = std::min<int>(columns, m_columns);
124  for ( int x = 0 ; x < minrows ; x++ )
125  for ( int y = 0 ; y < mincols ; y++ )
126  new_matrix[x][y] = m_matrix[x][y];
127 
128  // delete old arrays
129  if ( m_matrix != NULL ) {
130  for ( int i = 0 ; i < m_rows ; i++ )
131  delete [] m_matrix[i];
132 
133  delete [] m_matrix;
134  }
135 
136  m_matrix = new_matrix;
137  }
138 
139  m_rows = rows;
140  m_columns = columns;
141  }
142 
143  void identity(void)
144  {
145  assert( m_matrix != NULL );
146 
147  clear();
148 
149  int x = std::min<int>(m_rows, m_columns);
150  for ( int i = 0 ; i < x ; i++ )
151  m_matrix[i][i] = 1;
152  }
153 
154  void clear(void)
155  {
156  assert( m_matrix != NULL );
157 
158  for ( int i = 0 ; i < m_rows ; i++ )
159  for ( int j = 0 ; j < m_columns ; j++ )
160  m_matrix[i][j] = 0;
161  }
162 
163  T& operator () (int x, int y)
164  {
165  assert ( x >= 0 );
166  assert ( y >= 0 );
167  assert ( x < m_rows );
168  assert ( y < m_columns );
169  assert ( m_matrix != NULL );
170  return m_matrix[x][y];
171  }
172 
173  T trace(void)
174  {
175  assert( m_matrix != NULL );
176 
177  T value = 0;
178 
179  int x = std::min<int>(m_rows, m_columns);
180  for ( int i = 0 ; i < x ; i++ )
181  value += m_matrix[i][i];
182 
183  return value;
184  }
185 
186 
188  {
189  assert( m_rows > 0 );
190  assert( m_columns > 0 );
191 
192  int new_rows = m_columns;
193  int new_columns = m_rows;
194 
195  if ( m_rows != m_columns ) {
196  // expand matrix
197  int m = std::max<int>(m_rows, m_columns);
198  resize(m,m);
199  }
200 
201  for ( int i = 0 ; i < m_rows ; i++ ) {
202  for ( int j = i+1 ; j < m_columns ; j++ ) {
203  T tmp = m_matrix[i][j];
204  m_matrix[i][j] = m_matrix[j][i];
205  m_matrix[j][i] = tmp;
206  }
207  }
208 
209  if ( new_columns != new_rows ) {
210  // trim off excess.
211  resize(new_rows, new_columns);
212  }
213 
214  return *this;
215  }
216 
218  {
219  assert( m_matrix != NULL );
220  assert( other.m_matrix != NULL );
221  assert ( m_columns == other.m_rows );
222 
223  Matrix<T> out(m_rows, other.m_columns);
224 
225  for ( int i = 0 ; i < out.m_rows ; i++ ) {
226  for ( int j = 0 ; j < out.m_columns ; j++ ) {
227  for ( int x = 0 ; x < m_columns ; x++ ) {
228  out(i,j) += m_matrix[i][x] * other.m_matrix[x][j];
229  }
230  }
231  }
232 
233  return out;
234  }
235 
236  int minsize(void)
237  {
238  return ((m_rows < m_columns) ? m_rows : m_columns);
239  }
240 
241  int columns(void)
242  {
243  return m_columns;
244  }
245 
246  int rows(void)
247  {
248  return m_rows;
249  }
250  private:
251  T **m_matrix;
252  int m_rows;
254 };
255 
256 // #ifndef USE_EXPORT_KEYWORD
257 // #include "matrix.cpp"
258 //#define export /*export*/
259 // #endif
260 
261 #endif /* !defined(_MATRIX_H_) */
262 
void clear(void)
Definition: matrix.h:154
void resize(int rows, int columns)
Definition: matrix.h:99
Matrix< T > product(Matrix< T > &other)
Definition: matrix.h:217
T & operator()(int x, int y)
Definition: matrix.h:163
int m_columns
Definition: matrix.h:253
void identity(void)
Definition: matrix.h:143
Matrix(const Matrix< T > &other)
Definition: matrix.h:47
int m_rows
Definition: matrix.h:252
int columns(void)
Definition: matrix.h:241
T trace(void)
Definition: matrix.h:173
T ** m_matrix
Definition: matrix.h:251
Matrix(int rows, int columns)
Definition: matrix.h:41
int minsize(void)
Definition: matrix.h:236
Matrix()
Definition: matrix.h:34
Matrix< T > & transpose(void)
Definition: matrix.h:187
Matrix< T > & operator=(const Matrix< T > &other)
Definition: matrix.h:63
Definition: matrix.h:32
int rows(void)
Definition: matrix.h:246
~Matrix()
Definition: matrix.h:86


mtt
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:18