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