Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
EST_TMatrix.h
1  /*************************************************************************/
2  /* */
3  /* Centre for Speech Technology Research */
4  /* University of Edinburgh, UK */
5  /* Copyright (c) 1996 */
6  /* All Rights Reserved. */
7  /* */
8  /* Permission is hereby granted, free of charge, to use and distribute */
9  /* this software and its documentation without restriction, including */
10  /* without limitation the rights to use, copy, modify, merge, publish, */
11  /* distribute, sublicense, and/or sell copies of this work, and to */
12  /* permit persons to whom this work is furnished to do so, subject to */
13  /* the following conditions: */
14  /* 1. The code must retain the above copyright notice, this list of */
15  /* conditions and the following disclaimer. */
16  /* 2. Any modifications must be clearly marked as such. */
17  /* 3. Original authors' names are not deleted. */
18  /* 4. The authors' names are not used to endorse or promote products */
19  /* derived from this software without specific prior written */
20  /* permission. */
21  /* */
22  /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23  /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24  /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25  /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26  /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27  /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28  /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29  /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30  /* THIS SOFTWARE. */
31  /* */
32  /*************************************************************************/
33  /* */
34  /* Author : Paul Taylor */
35  /* Rewritten : Richard Caley */
36  /* --------------------------------------------------------------------- */
37  /* Matrix class */
38  /* */
39  /*************************************************************************/
40 
41 #ifndef __TMatrix_H__
42 #define __TMatrix_H__
43 
44 #include <iostream>
45 
46 using namespace std;
47 
48 #include "EST_rw_status.h"
49 #include "EST_TVector.h"
50 #include "instantiate/EST_TMatrixI.h"
51 
52 /* When set bounds checks (safe but slow) are done on matrix access */
53 #ifndef TMATRIX_BOUNDS_CHECKING
54 # define TMATRIX_BOUNDS_CHECKING 0
55 #endif
56 
57 #if TMATRIX_BOUNDS_CHECKING
58 #define A_CHECK a_check
59 #else
60 #define A_CHECK a_no_check
61 #endif
62 
63 #define INLINE inline
64 
65 /* This doesn't work as I thought so I have disabled it for now.
66  */
67 
68 #if defined(__GNUC__) && 0
69 # define mx_move_pointer(P, TY, STEP, N) \
70  ((TY *)\
71  ((void *) (((char (*) [sizeof(TY)*STEP])P) + N) ) \
72  )
73 # define fast_a_m_gcc(R,C) \
74  ( * mx_move_pointer(mx_move_pointer(p_memory,T,p_column_step,C),T,p_row_step,R))
75 # define fast_a_m_x(R,C) (fast_a_m_gcc(R,C))
76 #else
77 # define fast_a_m_x(R,C) (fast_a_m(R,C))
78 #endif
79 
80 
81 
82 /** \class EST_TMatrix
83  * \ingroup containerclasses
84  * \brief Template Matrix class. This is an extension of the EST_TVector class to two dimensions.
85  *
86  * @see matrix_example
87  * @see EST_TVector
88  */
89 template <class T>
90 class EST_TMatrix : public EST_TVector<T>
91 {
92 
93 protected:
94  /// Visible shape
95  unsigned int p_num_rows;
96 
97  /// How to access the memory
98  unsigned int p_row_step;
99 
100  INLINE unsigned int mcell_pos(int r, int c,
101  int rs, int cs) const
102  { return (rs==1?r:(r*rs)) + (cs==1?c:(c*cs));}
103 
104 
105  INLINE unsigned int mcell_pos(int r, int c) const
106  {
107 
108  return mcell_pos(r, c,
109  this->p_row_step, this->p_column_step);
110  }
111 
112  INLINE unsigned int mcell_pos_1(int r, int c) const
113  {
114 
115  (void)r;
116  return c;
117  }
118 
119  /// quick method for returning `x[m][n]`
120  INLINE const T &fast_a_m(int r, int c) const
121  { return this->p_memory[mcell_pos(r,c)]; }
122  INLINE T &fast_a_m(int r, int c)
123  { return this->p_memory[mcell_pos(r,c)]; }
124 
125  INLINE const T &fast_a_1(int r, int c) const
126  { return this->p_memory[mcell_pos_1(r,c)]; }
127  INLINE T &fast_a_1(int r, int c)
128  { return this->p_memory[mcell_pos_1(r,c)]; }
129 
130 
131  /// Get and set values from array
132  void set_values(const T *data,
133  int r_step, int c_step,
134  int start_r, int num_r,
135  int start_c, int num_c
136  );
137  void get_values(T *data,
138  int r_step, int c_step,
139  int start_r, int num_r,
140  int start_c, int num_c
141  ) const;
142 
143  /// private resize and copy function.
144  void copy(const EST_TMatrix<T> &a);
145  /// just copy data, no resizing, no size check.
146  void copy_data(const EST_TMatrix<T> &a);
147 
148  /// resize the memory and reset the bounds, but don't set values.
149  void just_resize(int new_rows, int new_cols, T** old_vals);
150 
151  /// sets data and length to default values (0 in both cases).
152  void default_vals();
153 public:
154 
155  ///default constructor
156  EST_TMatrix();
157 
158  /// copy constructor
159  EST_TMatrix(const EST_TMatrix<T> &m);
160 
161  /// "size" constructor
162  EST_TMatrix(int rows, int cols);
163 
164  /// construct from memory supplied by caller
165  EST_TMatrix(int rows, int cols,
166  T *memory, int offset=0, int free_when_destroyed=0);
167 
168  /// EST_TMatrix
169 
170  ~EST_TMatrix();
171 
172  /**@name access
173  * Basic access methods for matrices.
174  */
175  //@{
176 
177  /// return number of rows
178  int num_rows() const {return this->p_num_rows;}
179  /// return number of columns
180  int num_columns() const {return this->p_num_columns;}
181 
182  /// const access with no bounds check, care recommend
183  INLINE const T &a_no_check(int row, int col) const
184  { return fast_a_m_x(row,col); }
185  /// access with no bounds check, care recommend
186  INLINE T &a_no_check(int row, int col)
187  { return fast_a_m_x(row,col); }
188 
189  INLINE const T &a_no_check_1(int row, int col) const { return fast_a_1(row,col); }
190  INLINE T &a_no_check_1(int row, int col) { return fast_a_1(row,col); }
191 
192  /// const element access function
193  const T &a_check(int row, int col) const;
194  /// non-const element access function
195  T &a_check(int row, int col);
196 
197  const T &a(int row, int col) const { return A_CHECK(row,col); }
198  T &a(int row, int col) { return A_CHECK(row,col); }
199 
200  /// const element access operator
201  const T &operator () (int row, int col) const { return a(row,col); }
202  /// non-const element access operator
203  T &operator () (int row, int col) { return a(row,col); }
204 
205  //@}
206 
207  bool have_rows_before(int n) const;
208  bool have_columns_before(int n) const;
209 
210  /** resize matrix. If `set=1`, then the current values in
211  the matrix are preserved up to the new size `n`. If the
212  new size exceeds the old size, the rest of the matrix is
213  filled with the `def_val`
214  */
215  void resize(int rows, int cols, int set=1);
216 
217  /// fill matrix with value v
218  void fill(const T &v);
219  void fill() { fill(*this->def_val); }
220 
221  /// assignment operator
222  EST_TMatrix &operator=(const EST_TMatrix &s);
223 
224  /// The two versions of what might have been operator +=
225  EST_TMatrix &add_rows(const EST_TMatrix &s);
226  EST_TMatrix &add_columns(const EST_TMatrix &s);
227 
228  /**@name Sub-Matrix/Vector Extraction
229  *
230  * All of these return matrices and vectors which share
231  * memory with the original, so altering values them alters
232  * the original.
233  */
234  //@{
235 
236  /// Make the vector `rv` a window onto row `r`
237  void row(EST_TVector<T> &rv, int r, int start_c=0, int len=-1);
238  /// Make the vector `cv` a window onto column `c`
239  void column(EST_TVector<T> &cv, int c, int start_r=0, int len=-1);
240  /// Make the matrix `sm` a window into this matrix.
241  void sub_matrix(EST_TMatrix<T> &sm,
242  int r=0, int numr=EST_ALL,
243  int c=0, int numc=EST_ALL);
244  //@}
245 
246  /**@name Copy in and out
247  * Copy data between buffers and the matrix.
248  */
249  //@{
250  /** Copy row `r` of matrix to `buf`. `buf`
251  should be pre-malloced to the correct size.
252  */
253  void copy_row(int r, T *buf, int offset=0, int num=-1) const;
254 
255  /** Copy row `r` of matrix to
256  `buf`. `buf` should be
257  pre-malloced to the correct size. */
258 
259  void copy_row(int r, EST_TVector<T> &t, int offset=0, int num=-1) const;
260 
261  /** Copy column `c` of matrix to `buf`. `buf`
262  should be pre-malloced to the correct size.
263  */
264  void copy_column(int c, T *buf, int offset=0, int num=-1) const;
265 
266  /** Copy column `c` of matrix to
267  `buf`. `buf` should
268  be pre-malloced to the correct size. */
269 
270  void copy_column(int c, EST_TVector<T> &t, int offset=0, int num=-1)const;
271 
272  /** Copy buf into row `n` of matrix.
273  */
274  void set_row(int n, const T *buf, int offset=0, int num=-1);
275 
276  void set_row(int n, const EST_TVector<T> &t, int offset=0, int num=-1)
277  { set_row(n, t.memory(), offset, num); }
278 
279  void set_row(int r,
280  const EST_TMatrix<T> &from, int from_r, int from_offset=0,
281  int offset=0, int num=-1); // set nth row
282 
283 
284  /** Copy buf into column `n` of matrix.
285  */
286  void set_column(int n, const T *buf, int offset=0, int num=-1);
287 
288  void set_column(int n, const EST_TVector<T> &t, int offset=0, int num=-1)
289  { set_column(n, t.memory(), offset, num); }
290 
291  void set_column(int c,
292  const EST_TMatrix<T> &from, int from_c, int from_offset=0,
293  int offset=0, int num=-1); // set nth column
294 
295  /** For when you absolutely have to have access to the memory.
296  */
297  void set_memory(T *buffer, int offset, int rows, int columns,
298  int free_when_destroyed=0);
299 
300  //@}
301 
302  /**@name Matrix file input / output
303  */
304  //@{
305  /// load Matrix from file - Not currently implemented.
306  EST_read_status load(const class EST_String &filename);
307  /// save Matrix to file `filename`
308  EST_write_status save(const class EST_String &filename) const;
309 
310  /// print matrix.
311  friend ostream& operator << (ostream &st,const EST_TMatrix<T> &a)
312  {int i, j;
313  for (i = 0; i < a.num_rows(); ++i) {
314  for (j = 0; j < a.num_columns(); ++j)
315  st << a.a_no_check(i, j) << " "; st << endl;
316  }
317  return st;
318  }
319  //@}
320 
321 };
322 
323 #undef A_CHECK
324 
325 #endif
326 
INLINE const T & a_no_check(int row, int col) const
const access with no bounds check, care recommend
Definition: EST_TMatrix.h:183
int num_columns() const
return number of columns
Definition: EST_TMatrix.h:180
Template Matrix class. This is an extension of the EST_TVector class to two dimensions.
Definition: EST_TMatrix.h:90
INLINE T & a_no_check(int row, int col)
access with no bounds check, care recommend
Definition: EST_TMatrix.h:186
unsigned int p_row_step
How to access the memory.
Definition: EST_TMatrix.h:98
unsigned int p_num_rows
Visible shape.
Definition: EST_TMatrix.h:95
const T * memory() const
Definition: EST_TVector.h:239
Template vector.
Definition: EST_TVector.h:145
int num_rows() const
return number of rows
Definition: EST_TMatrix.h:178
INLINE const T & fast_a_m(int r, int c) const
quick method for returning x[m][n]
Definition: EST_TMatrix.h:120