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