Edinburgh Speech Tools  2.1-release
EST_TMatrix.h
Go to the documentation of this file.
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 #include <cstddef>
46 
47 #include "EST_rw_status.h"
48 #include "EST_TVector.h"
50 
51 /* When set bounds checks (safe but slow) are done on matrix access */
52 #ifndef TMATRIX_BOUNDS_CHECKING
53 # define TMATRIX_BOUNDS_CHECKING 0
54 #endif
55 
56 #if TMATRIX_BOUNDS_CHECKING
57 #define A_CHECK a_check
58 #else
59 #define A_CHECK a_no_check
60 #endif
61 
62 #define INLINE inline
63 
64 /* This doesn't work as I thought so I have disabled it for now.
65  */
66 
67 #if defined(__GNUC__) && 0
68 # define mx_move_pointer(P, TY, STEP, N) \
69  ((TY *)\
70  ((void *) (((char (*) [sizeof(TY)*STEP])P) + N) ) \
71  )
72 # define fast_a_m_gcc(R,C) \
73  ( * mx_move_pointer(mx_move_pointer(p_memory,T,p_column_step,C),T,p_row_step,R))
74 # define fast_a_m_x(R,C) (fast_a_m_gcc(R,C))
75 #else
76 # define fast_a_m_x(R,C) (fast_a_m(R,C))
77 #endif
78 
79 
80 
81 /** \class EST_TMatrix
82  * \ingroup containerclasses
83  * \brief Template Matrix class. This is an extension of the EST_TVector class to two dimensions.
84  *
85  * @see matrix_example
86  * @see EST_TVector
87  */
88 template <class T>
89 class EST_TMatrix : public EST_TVector<T>
90 {
91 
92 protected:
93  /// Visible shape
95 
96  /// How to access the memory
98 
100  ssize_t rs, ssize_t cs) const
101  { return (rs==1?r:(r*rs)) + (cs==1?c:(c*cs));}
102 
103 
105  {
106 
107  return mcell_pos(r, c,
108  this->p_row_step, this->p_column_step);
109  }
110 
112  {
113 
114  (void)r;
115  return c;
116  }
117 
118  /// quick method for returning `x[m][n]`
119  INLINE const T &fast_a_m(ssize_t r, ssize_t c) const
120  { return this->p_memory[mcell_pos(r,c)]; }
122  { return this->p_memory[mcell_pos(r,c)]; }
123 
124  INLINE const T &fast_a_1(ssize_t r, ssize_t c) const
125  { return this->p_memory[mcell_pos_1(r,c)]; }
127  { return this->p_memory[mcell_pos_1(r,c)]; }
128 
129 
130  /// Get and set values from array
131  void set_values(const T *data,
132  ssize_t r_step, ssize_t c_step,
133  ssize_t start_r, ssize_t num_r,
134  ssize_t start_c, ssize_t num_c
135  );
136  void get_values(T *data,
137  ssize_t r_step, ssize_t c_step,
138  ssize_t start_r, ssize_t num_r,
139  ssize_t start_c, ssize_t num_c
140  ) const;
141 
142  /// private resize and copy function.
143  void copy(const EST_TMatrix<T> &a);
144  /// just copy data, no resizing, no size check.
145  void copy_data(const EST_TMatrix<T> &a);
146 
147  /// resize the memory and reset the bounds, but don't set values.
148  void just_resize(ssize_t new_rows, ssize_t new_cols, T** old_vals);
149 
150  /// sets data and length to default values (0 in both cases).
151  void default_vals();
152 public:
153 
154  ///default constructor
155  EST_TMatrix();
156 
157  /// copy constructor
158  EST_TMatrix(const EST_TMatrix<T> &m);
159 
160  /// "size" constructor
161  EST_TMatrix(ssize_t rows, ssize_t cols);
162 
163  /// construct from memory supplied by caller
164  EST_TMatrix(ssize_t rows, ssize_t cols,
165  T *memory, ptrdiff_t offset=0, int free_when_destroyed=0);
166 
167  /// EST_TMatrix
168 
169  ~EST_TMatrix();
170 
171  /**@name access
172  * Basic access methods for matrices.
173  */
174  ///@{
175 
176  /// return number of rows
177  ssize_t num_rows() const {return this->p_num_rows;}
178  /// return number of columns
179  ssize_t num_columns() const {return this->p_num_columns;}
180 
181  /// const access with no bounds check, care recommend
182  INLINE const T &a_no_check(ssize_t row, ssize_t col) const
183  { return fast_a_m_x(row,col); }
184  /// access with no bounds check, care recommend
186  { return fast_a_m_x(row,col); }
187 
188  INLINE const T &a_no_check_1(ssize_t row, ssize_t col) const { return fast_a_1(row,col); }
189  INLINE T &a_no_check_1(ssize_t row, ssize_t col) { return fast_a_1(row,col); }
190 
191  /// const element access function
192  const T &a_check(ssize_t row, ssize_t col) const;
193  /// non-const element access function
194  T &a_check(ssize_t row, ssize_t col);
195 
196  const T &a(ssize_t row, ssize_t col) const { return A_CHECK(row,col); }
197  T &a(ssize_t row, ssize_t col) { return A_CHECK(row,col); }
198 
199  /// const element access operator
200  const T &operator () (ssize_t row, ssize_t col) const { return a(row,col); }
201  /// non-const element access operator
202  T &operator () (ssize_t row, ssize_t col) { return a(row,col); }
203 
204  ///@}
205 
206  bool have_rows_before(ssize_t n) const;
207  bool have_columns_before(ssize_t n) const;
208 
209  /** resize matrix. If `set=1`, then the current values in
210  the matrix are preserved up to the new size `n`. If the
211  new size exceeds the old size, the rest of the matrix is
212  filled with the `def_val`
213  */
214  void resize(ssize_t rows, ssize_t cols, ssize_t set=1);
215 
216  /// fill matrix with value v
217  void fill(const T &v);
218  void fill() { fill(*this->def_val); }
219 
220  /// assignment operator
221  EST_TMatrix &operator=(const EST_TMatrix &s);
222 
223  /// The two versions of what might have been operator +=
224  EST_TMatrix &add_rows(const EST_TMatrix &s);
225  EST_TMatrix &add_columns(const EST_TMatrix &s);
226 
227  /**@name Sub-Matrix/Vector Extraction
228  *
229  * All of these return matrices and vectors which share
230  * memory with the original, so altering values them alters
231  * the original.
232  */
233  ///@{
234 
235  /// Make the vector `rv` a window onto row `r`
236  void row(EST_TVector<T> &rv, ssize_t r, ssize_t start_c=0, int len=-1);
237  /// Make the vector `cv` a window onto column `c`
238  void column(EST_TVector<T> &cv, ssize_t c, ssize_t start_r=0, int len=-1);
239  /// Make the matrix `sm` a window into this matrix.
240  void sub_matrix(EST_TMatrix<T> &sm,
241  ssize_t r=0, ptrdiff_t numr=EST_ALL,
242  ssize_t c=0, ptrdiff_t numc=EST_ALL);
243  ///@}
244 
245  /**@name Copy in and out
246  * Copy data between buffers and the matrix.
247  */
248  ///@{
249  /** Copy row `r` of matrix to `buf`. `buf`
250  should be pre-malloced to the correct size.
251  */
252  void copy_row(ssize_t r, T *buf, ptrdiff_t offset=0, int num=-1) const;
253 
254  /** Copy row `r` of matrix to
255  `buf`. `buf` should be
256  pre-malloced to the correct size. */
257 
258  void copy_row(ssize_t r, EST_TVector<T> &t, ptrdiff_t offset=0, int num=-1) const;
259 
260  /** Copy column `c` of matrix to `buf`. `buf`
261  should be pre-malloced to the correct size.
262  */
263  void copy_column(ssize_t c, T *buf, ptrdiff_t offset=0, int num=-1) const;
264 
265  /** Copy column `c` of matrix to
266  `buf`. `buf` should
267  be pre-malloced to the correct size. */
268 
269  void copy_column(ssize_t c, EST_TVector<T> &t, ptrdiff_t offset=0, int num=-1)const;
270 
271  /** Copy buf into row `n` of matrix.
272  */
273  void set_row(ssize_t n, const T *buf, ptrdiff_t offset=0, int num=-1);
274 
275  void set_row(ssize_t n, const EST_TVector<T> &t, ptrdiff_t offset=0, int num=-1)
276  { set_row(n, t.memory(), offset, num); }
277 
278  void set_row(ssize_t r,
279  const EST_TMatrix<T> &from, ssize_t from_r, ssize_t from_offset=0,
280  ptrdiff_t offset=0, int num=-1); // set nth row
281 
282 
283  /** Copy buf into column `n` of matrix.
284  */
285  void set_column(ssize_t n, const T *buf, ptrdiff_t offset=0, int num=-1);
286 
287  void set_column(ssize_t n, const EST_TVector<T> &t, ptrdiff_t offset=0, int num=-1)
288  { set_column(n, t.memory(), offset, num); }
289 
290  void set_column(ssize_t c,
291  const EST_TMatrix<T> &from, ssize_t from_c, ssize_t from_offset=0,
292  ptrdiff_t offset=0, int num=-1); // set nth column
293 
294  /** For when you absolutely have to have access to the memory.
295  */
296  void set_memory(T *buffer, ptrdiff_t offset, ssize_t rows, ssize_t columns,
297  int free_when_destroyed=0);
298 
299  ///@}
300 
301  /**@name Matrix file input / output
302  */
303  ///@{
304  /// load Matrix from file - Not currently implemented.
305  EST_read_status load(const class EST_String &filename);
306  /// save Matrix to file `filename`
307  EST_write_status save(const class EST_String &filename) const;
308 
309  /// print matrix.
310  friend std::ostream& operator << (std::ostream &st,const EST_TMatrix<T> &a)
311  {ssize_t i, j;
312  for (i = 0; i < a.num_rows(); ++i) {
313  for (j = 0; j < a.num_columns(); ++j)
314  st << a.a_no_check(i, j) << " ";
315  st << std::endl;
316  }
317  return st;
318  }
319  ///@}
320 
321 };
322 
323 #undef A_CHECK
324 
325 #endif
326 
void set_column(ssize_t n, const T *buf, ptrdiff_t offset=0, int num=-1)
Definition: EST_TMatrix.cc:478
#define fast_a_m_x(R, C)
Definition: EST_TMatrix.h:76
ssize_t p_column_step
Definition: EST_TVector.h:159
bool have_rows_before(ssize_t n) const
Definition: EST_TMatrix.cc:302
bool have_columns_before(ssize_t n) const
Definition: EST_TMatrix.cc:308
ssize_t p_num_columns
Visible shape.
Definition: EST_TVector.h:155
Template Matrix class. This is an extension of the EST_TVector class to two dimensions.
Definition: EST_TMatrix.h:89
EST_write_status
void copy_column(ssize_t c, T *buf, ptrdiff_t offset=0, int num=-1) const
Definition: EST_TMatrix.cc:420
EST_TMatrix & add_rows(const EST_TMatrix &s)
The two versions of what might have been operator +=.
Definition: EST_TMatrix.cc:167
INLINE ssize_t mcell_pos(ssize_t r, ssize_t c, ssize_t rs, ssize_t cs) const
Definition: EST_TMatrix.h:99
ssize_t num_columns() const
return number of columns
Definition: EST_TMatrix.h:179
void set_row(ssize_t n, const T *buf, ptrdiff_t offset=0, int num=-1)
Definition: EST_TMatrix.cc:466
INLINE const T & fast_a_m(ssize_t r, ssize_t c) const
quick method for returning x[m][n]
Definition: EST_TMatrix.h:119
ssize_t p_num_rows
Visible shape.
Definition: EST_TMatrix.h:94
EST_TMatrix()
default constructor
Definition: EST_TMatrix.cc:62
void copy(const EST_TMatrix< T > &a)
private resize and copy function.
Definition: EST_TMatrix.cc:153
EST_TMatrix & add_columns(const EST_TMatrix &s)
Definition: EST_TMatrix.cc:188
INLINE T & a_no_check(ssize_t row, ssize_t col)
access with no bounds check, care recommend
Definition: EST_TMatrix.h:185
ssize_t num_rows() const
return number of rows
Definition: EST_TMatrix.h:177
int ssize_t
const int EST_ALL
const T & a(ssize_t row, ssize_t col) const
Definition: EST_TMatrix.h:196
INLINE const T & a_no_check_1(ssize_t row, ssize_t col) const
Definition: EST_TMatrix.h:188
const EST_DMatrix * def_val
void column(EST_TVector< T > &cv, ssize_t c, ssize_t start_r=0, int len=-1)
Make the vector cv a window onto column c
Definition: EST_TMatrix.cc:556
void just_resize(ssize_t new_rows, ssize_t new_cols, T **old_vals)
resize the memory and reset the bounds, but don&#39;t set values.
Definition: EST_TMatrix.cc:209
void copy_data(const EST_TMatrix< T > &a)
just copy data, no resizing, no size check.
Definition: EST_TMatrix.cc:119
INLINE T & fast_a_m(ssize_t r, ssize_t c)
Definition: EST_TMatrix.h:121
EST_write_status save(const class EST_String &filename) const
save Matrix to file filename
Definition: EST_TMatrix.cc:324
INLINE T & fast_a_1(ssize_t r, ssize_t c)
Definition: EST_TMatrix.h:126
void default_vals()
sets data and length to default values (0 in both cases).
Definition: EST_TMatrix.cc:54
void sub_matrix(EST_TMatrix< T > &sm, ssize_t r=0, ptrdiff_t numr=EST_ALL, ssize_t c=0, ptrdiff_t numc=EST_ALL)
Make the matrix sm a window into this matrix.
Definition: EST_TMatrix.cc:578
EST_read_status load(const class EST_String &filename)
load Matrix from file - Not currently implemented.
Definition: EST_TMatrix.cc:360
#define A_CHECK
Definition: EST_TMatrix.h:59
~EST_TMatrix()
EST_TMatrix.
Definition: EST_TMatrix.cc:90
const T & operator()(ssize_t row, ssize_t col) const
const element access operator
Definition: EST_TMatrix.h:200
INLINE T & a_no_check_1(ssize_t row, ssize_t col)
Definition: EST_TMatrix.h:189
void row(EST_TVector< T > &rv, ssize_t r, ssize_t start_c=0, int len=-1)
Make the vector rv a window onto row r
Definition: EST_TMatrix.cc:534
void copy_row(ssize_t r, T *buf, ptrdiff_t offset=0, int num=-1) const
Definition: EST_TMatrix.cc:381
EST_TMatrix & operator=(const EST_TMatrix &s)
assignment operator
Definition: EST_TMatrix.cc:160
void set_row(ssize_t n, const EST_TVector< T > &t, ptrdiff_t offset=0, int num=-1)
Definition: EST_TMatrix.h:275
EST_read_status
const T * memory() const
Definition: EST_TVector.h:238
INLINE ssize_t mcell_pos_1(ssize_t r, ssize_t c) const
Definition: EST_TMatrix.h:111
void set_column(ssize_t n, const EST_TVector< T > &t, ptrdiff_t offset=0, int num=-1)
Definition: EST_TMatrix.h:287
const T & a_check(ssize_t row, ssize_t col) const
const element access function
Definition: EST_TMatrix.cc:113
void set_memory(T *buffer, ptrdiff_t offset, ssize_t rows, ssize_t columns, int free_when_destroyed=0)
Definition: EST_TMatrix.cc:371
INLINE ssize_t mcell_pos(ssize_t r, ssize_t c) const
Definition: EST_TMatrix.h:104
Template vector.
Definition: EST_TVector.h:145
void get_values(T *data, ssize_t r_step, ssize_t c_step, ssize_t start_r, ssize_t num_r, ssize_t start_c, ssize_t num_c) const
Definition: EST_TMatrix.cc:141
T & a(ssize_t row, ssize_t col)
Definition: EST_TMatrix.h:197
void resize(ssize_t rows, ssize_t cols, ssize_t set=1)
Definition: EST_TMatrix.cc:250
INLINE const T & a_no_check(ssize_t row, ssize_t col) const
const access with no bounds check, care recommend
Definition: EST_TMatrix.h:182
void set_values(const T *data, ssize_t r_step, ssize_t c_step, ssize_t start_r, ssize_t num_r, ssize_t start_c, ssize_t num_c)
Get and set values from array.
Definition: EST_TMatrix.cc:129
INLINE ssize_t n() const
number of items in vector.
Definition: EST_TVector.h:251
void fill()
Definition: EST_TMatrix.h:218
#define INLINE
Definition: EST_TMatrix.h:62
ssize_t p_row_step
How to access the memory.
Definition: EST_TMatrix.h:97
INLINE const T & fast_a_1(ssize_t r, ssize_t c) const
Definition: EST_TMatrix.h:124