Edinburgh Speech Tools  2.1-release
EST_FMatrix.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  /* Date : April 1996 */
36  /* --------------------------------------------------------------------- */
37  /* Matrix class */
38  /* */
39  /*************************************************************************/
40 
41 #ifndef __FMatrix_H__
42 #define __FMatrix_H__
43 
44 #include <cstdlib>
45 #include "EST_TSimpleMatrix.h"
46 #include "EST_TSimpleVector.h"
47 
48 #include "EST_Val.h"
49 #include "EST_Val_defs.h"
50 
51 class EST_FVector;
52 
53 /** @class EST_FMatrix
54  * @ingroup containerclasses
55  * A matrix class for floating point numbers. EST_FMatrix x should be
56  used instead of float **x wherever possible.
57 */
58 
59 class EST_FMatrix : public EST_TSimpleMatrix<float> {
60 private:
61 public:
62  /// size constructor
63  EST_FMatrix(int m, int n):EST_TSimpleMatrix<float>(m, n) {}
64  /// copy constructor
66 
68  /// CHECK - what does this do???
69  EST_FMatrix(const EST_FMatrix &a, int b);
70  /// default constructor
72 
73  /// Save in file (ascii or binary)
74  EST_write_status save(const EST_String &filename,
75  const EST_String &type =
77  /// Load from file (ascii or binary as defined in file)
78  EST_read_status load(const EST_String &filename);
79  /// Save in file in est format
80  EST_write_status est_save(const EST_String &filename,
81  const EST_String &type);
82  /// Load from file in est format (binary/ascii defined in file itself)
83  EST_read_status est_load(const EST_String &filename);
84 
85  /// Copy 2-d array `x` of size `rows x cols` into matrix.
86  void copyin(float **x, ssize_t rows, ssize_t cols);
87 
88  /// Add elements of 2 same sized matrices.
90 
91  /// Subtract elements of 2 same sized matrices.
93 
94  /// elementwise multiply by scalar
95  EST_FMatrix &operator*=(const float f);
96 
97  /// elementwise divide by scalar
98  EST_FMatrix &operator/=(const float f);
99 
100  /// Multiply all elements of matrix by `x`.
101  friend EST_FMatrix operator*(const EST_FMatrix &a, const float x);
102 
103  /// Multiply matrix by vector.
104  friend EST_FVector operator*(const EST_FMatrix &a, const EST_FVector &v);
105 
106  /// Multiply vector by matrix
107  friend EST_FVector operator*(const EST_FVector &v,const EST_FMatrix &a);
108 
109  /// Multiply matrix by matrix.
110  friend EST_FMatrix operator*(const EST_FMatrix &a, const EST_FMatrix &b);
111 };
112 
113 /** \class EST_FVector
114  * @ingroup containerclasses
115  * \brief A vector class for floating point numbers.
116  `EST_FVector x` should be used instead of `float *x`
117  wherever possible.
118 */
119 class EST_FVector: public EST_TSimpleVector<float> {
120 public:
121  /// Size constructor.
123  /// Copy constructor.
125  /// Default constructor.
127 
128  /// elementwise multiply
129  EST_FVector &operator*=(const EST_FVector &s);
130 
131  /// elementwise add
132  EST_FVector &operator+=(const EST_FVector &s);
133 
134  /// elementwise multiply by scalar
135  EST_FVector &operator*=(const float f);
136 
137  /// elementwise divide by scalar
138  EST_FVector &operator/=(const float f);
139 
140  EST_write_status est_save(const EST_String &filename,
141  const EST_String &type);
142 
143  /// save vector to file <tt> filename</tt>.
144  EST_write_status save(const EST_String &filename,
145  const EST_String &type);
146 
147  /// load vector from file <tt> filename</tt>.
148  EST_read_status load(const EST_String &filename);
149  /// Load from file in est format (binary/ascii defined in file itself)
150  EST_read_status est_load(const EST_String &filename);
151 
152 };
153 
154 /// find largest element
155 float matrix_max(const EST_FMatrix &a);
156 /// find largest element
157 float vector_max(const EST_FVector &a);
158 
159 int square(const EST_FMatrix &a);
160 /// inverse
161 int inverse(const EST_FMatrix &a, EST_FMatrix &inv);
162 int inverse(const EST_FMatrix &a, EST_FMatrix &inv, int &singularity);
163 /// pseudo inverse (for non-square matrices)
164 int pseudo_inverse(const EST_FMatrix &a, EST_FMatrix &inv);
165 int pseudo_inverse(const EST_FMatrix &a, EST_FMatrix &inv,int &singularity);
166 
167 /// some useful matrix creators
168 /// make an identity matrix of dimension n
169 void eye(EST_FMatrix &a, const ssize_t n);
170 /// make already square matrix into I without resizing
171 void eye(EST_FMatrix &a);
172 
173 /// the user should use est_seed to seed the random number generator
174 void est_seed();
175 void est_seed48();
176 /// all elements are randomised
177 void make_random_vector(EST_FVector &M, const float scale);
178 /// all elements are randomised
179 void make_random_matrix(EST_FMatrix &M, const float scale);
180 /// used for variance
181 void make_random_diagonal_matrix(EST_FMatrix &M, const float scale);
182 /// used for covariance
183 void make_random_symmetric_matrix(EST_FMatrix &M, const float scale);
184 
186 
187 /// elementwise add
188 EST_FVector add(const EST_FVector &a,const EST_FVector &b);
189 /// elementwise subtract
190 EST_FVector subtract(const EST_FVector &a,const EST_FVector &b);
191 
192 /// enforce symmetry
193 void symmetrize(EST_FMatrix &a);
194 /// stack columns on top of each other to make a vector
195 void stack_matrix(const EST_FMatrix &M, EST_FVector &v);
196 /// inplace diagonalise
198 
199 
200 float determinant(const EST_FMatrix &a);
201 /// not implemented ??
202 int singular(EST_FMatrix &a);
203 /// exchange rows and columns
204 void transpose(const EST_FMatrix &a,EST_FMatrix &b);
206 
207 /// extract leading diagonal as a matrix
209 /// extract leading diagonal as a vector
211 /// sum of elements
212 float sum(const EST_FMatrix &a);
213 void multiply(const EST_FMatrix &a, const EST_FMatrix &b, EST_FMatrix &c);
214 ssize_t floor_matrix(EST_FMatrix &M, const float floor);
215 
216 /// matrix product of two vectors (#rows = length of first vector, #cols = length of second vector)
217 EST_FMatrix cov_prod(const EST_FVector &v1,const EST_FVector &v2);
218 
219 EST_FMatrix operator*(const EST_FMatrix &a, const EST_FMatrix &b);
220 EST_FMatrix operator-(const EST_FMatrix &a, const EST_FMatrix &b);
221 EST_FMatrix operator+(const EST_FMatrix &a, const EST_FMatrix &b);
222 
223 EST_FVector operator-(const EST_FVector &a, const EST_FVector &b);
224 EST_FVector operator+(const EST_FVector &a, const EST_FVector &b);
225 
226 EST_FMatrix sub(const EST_FMatrix &a, ssize_t row, ssize_t col);
228 
230 EST_FMatrix column(const EST_FMatrix &a, ssize_t col);
231 
232 
233 /// least squares fit
234 bool
235 polynomial_fit(EST_FVector &x, EST_FVector &y, EST_FVector &co_effs, int order);
236 
237 /// weighted least squares fit
238 bool
240  EST_FVector &weights, int order);
241 
242 float
243 polynomial_value(const EST_FVector &coeffs, const float x);
244 
245 /// vector dot product
246 float operator*(const EST_FVector &v1, const EST_FVector &v2);
247 
250 
251 #endif
void copyin(float **x, ssize_t rows, ssize_t cols)
Copy 2-d array x of size rows x cols into matrix.
Definition: EST_FMatrix.cc:329
ssize_t floor_matrix(EST_FMatrix &M, const float floor)
Definition: vec_mat_aux.cc:676
EST_FVector diagonal(const EST_FMatrix &a)
extract leading diagonal as a vector
Definition: vec_mat_aux.cc:537
EST_FMatrix & operator*=(const float f)
elementwise multiply by scalar
Definition: EST_FMatrix.cc:115
int square(const EST_FMatrix &a)
Definition: vec_mat_aux.cc:142
void eye(EST_FMatrix &a, const ssize_t n)
some useful matrix creators make an identity matrix of dimension n
Definition: vec_mat_aux.cc:473
void make_poly_basis_function(EST_FMatrix &T, EST_FVector t)
Definition: vec_mat_aux.cc:660
EST_write_status
EST_FMatrix(const EST_FMatrix &a)
copy constructor
Definition: EST_FMatrix.h:65
void make_random_vector(EST_FVector &M, const float scale)
all elements are randomised
Definition: vec_mat_aux.cc:612
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
Definition: EST_FMatrix.h:119
void make_random_matrix(EST_FMatrix &M, const float scale)
all elements are randomised
Definition: vec_mat_aux.cc:599
void est_seed()
the user should use est_seed to seed the random number generator
Definition: vec_mat_aux.cc:705
void make_random_symmetric_matrix(EST_FMatrix &M, const float scale)
used for covariance
Definition: vec_mat_aux.cc:624
void stack_matrix(const EST_FMatrix &M, EST_FVector &v)
stack columns on top of each other to make a vector
Definition: vec_mat_aux.cc:588
bool polynomial_fit(EST_FVector &x, EST_FVector &y, EST_FVector &co_effs, int order)
least squares fit
Definition: vec_mat_aux.cc:49
void multiply(const EST_FMatrix &a, const EST_FMatrix &b, EST_FMatrix &c)
Definition: EST_FMatrix.cc:306
void make_random_diagonal_matrix(EST_FMatrix &M, const float scale)
used for variance
Definition: vec_mat_aux.cc:644
EST_FMatrix fmatrix_abs(const EST_FMatrix &a)
Definition: vec_mat_aux.cc:270
EST_FMatrix(int m, int n)
size constructor
Definition: EST_FMatrix.h:63
int ssize_t
const float & a(ssize_t row, ssize_t col) const
Definition: EST_TMatrix.h:196
void column(EST_TVector< float > &cv, ssize_t c, ssize_t start_r=0, int len=-1)
Make the vector cv a window onto column c
EST_write_status save(const EST_String &filename, const EST_String &type=EST_FMatrix::default_file_type)
Save in file (ascii or binary)
Definition: EST_FMatrix.cc:341
EST_FMatrix triangulate(const EST_FMatrix &a)
Definition: vec_mat_aux.cc:232
EST_FMatrix & operator-=(const EST_FMatrix &a)
Subtract elements of 2 same sized matrices.
Definition: EST_FMatrix.cc:95
static EST_String default_file_type
Definition: EST_FMatrix.h:67
float polynomial_value(const EST_FVector &coeffs, const float x)
Definition: vec_mat_aux.cc:554
EST_FVector(ssize_t n)
Size constructor.
Definition: EST_FMatrix.h:122
EST_FMatrix & operator/=(const float f)
elementwise divide by scalar
Definition: EST_FMatrix.cc:126
float vector_max(const EST_FVector &a)
find largest element
#define VAL_REGISTER_CLASS_DCLS(NAME, CLASS)
Definition: EST_Val_defs.h:44
float determinant(const EST_FMatrix &a)
Definition: vec_mat_aux.cc:436
EST_FMatrix()
default constructor
Definition: EST_FMatrix.h:71
EST_FVector()
Default constructor.
Definition: EST_FMatrix.h:126
EST_FMatrix diagonalise(const EST_FMatrix &a)
extract leading diagonal as a matrix
Definition: vec_mat_aux.cc:158
void transpose(const EST_FMatrix &a, EST_FMatrix &b)
exchange rows and columns
Definition: vec_mat_aux.cc:244
EST_FVector subtract(const EST_FVector &a, const EST_FVector &b)
elementwise subtract
Definition: vec_mat_aux.cc:519
EST_FMatrix operator-(const EST_FMatrix &a, const EST_FMatrix &b)
Definition: EST_FMatrix.cc:159
float sum(const EST_FMatrix &a)
sum of elements
Definition: vec_mat_aux.cc:147
EST_read_status load(const EST_String &filename)
Load from file (ascii or binary as defined in file)
Definition: EST_FMatrix.cc:523
void symmetrize(EST_FMatrix &a)
enforce symmetry
Definition: vec_mat_aux.cc:564
EST_FVector add(const EST_FVector &a, const EST_FVector &b)
elementwise add
Definition: vec_mat_aux.cc:501
f
Definition: EST_item_aux.cc:48
void row(EST_TVector< float > &rv, ssize_t r, ssize_t start_c=0, int len=-1)
Make the vector rv a window onto row r
int singular(EST_FMatrix &a)
not implemented ??
friend EST_FMatrix operator*(const EST_FMatrix &a, const float x)
Multiply all elements of matrix by x.
Definition: EST_FMatrix.cc:183
void est_seed48()
EST_read_status
void inplace_diagonalise(EST_FMatrix &a)
inplace diagonalise
Definition: vec_mat_aux.cc:176
EST_FMatrix operator+(const EST_FMatrix &a, const EST_FMatrix &b)
Definition: EST_FMatrix.cc:137
int inverse(const EST_FMatrix &a, EST_FMatrix &inv)
inverse
Definition: vec_mat_aux.cc:298
int pseudo_inverse(const EST_FMatrix &a, EST_FMatrix &inv)
pseudo inverse (for non-square matrices)
Definition: vec_mat_aux.cc:405
A subclass of EST_TMatrix which copies using memcopy.
float matrix_max(const EST_FMatrix &a)
find largest element
Definition: vec_mat_aux.cc:129
EST_FMatrix sub(const EST_FMatrix &a, ssize_t row, ssize_t col)
Definition: vec_mat_aux.cc:187
INLINE ssize_t n() const
number of items in vector.
Definition: EST_TVector.h:251
EST_FVector(const EST_FVector &a)
Copy constructor.
Definition: EST_FMatrix.h:124
EST_read_status est_load(const EST_String &filename)
Load from file in est format (binary/ascii defined in file itself)
Definition: EST_FMatrix.cc:446
EST_FMatrix cov_prod(const EST_FVector &v1, const EST_FVector &v2)
matrix product of two vectors (#rows = length of first vector, #cols = length of second vector) ...
Definition: vec_mat_aux.cc:690
EST_FMatrix & operator+=(const EST_FMatrix &a)
Add elements of 2 same sized matrices.
Definition: EST_FMatrix.cc:75
EST_write_status est_save(const EST_String &filename, const EST_String &type)
Save in file in est format.
Definition: EST_FMatrix.cc:377