Edinburgh Speech Tools 2.4-release
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
50class 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
56class EST_FMatrix : public EST_TSimpleMatrix<float> {
57private:
58public:
59 /// size constructor
60 EST_FMatrix(int m, int n):EST_TSimpleMatrix<float>(m, n) {}
61 /// copy constructor
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*/
114class EST_FVector: public EST_TSimpleVector<float> {
115public:
116 /// Size constructor.
118 /// Copy constructor.
120 /// Default constructor.
122
123 /// elementwise multiply
125
126 /// elementwise add
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
150float matrix_max(const EST_FMatrix &a);
151/// find largest element
152float vector_max(const EST_FVector &a);
153
154int square(const EST_FMatrix &a);
155/// inverse
156int inverse(const EST_FMatrix &a, EST_FMatrix &inv);
157int inverse(const EST_FMatrix &a, EST_FMatrix &inv, int &singularity);
158/// pseudo inverse (for non-square matrices)
159int pseudo_inverse(const EST_FMatrix &a, EST_FMatrix &inv);
160int 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
164void eye(EST_FMatrix &a, const int n);
165/// make already square matrix into I without resizing
166void eye(EST_FMatrix &a);
167
168/// the user should use est_seed to seed the random number generator
169void est_seed();
170void est_seed48();
171/// all elements are randomised
172void make_random_vector(EST_FVector &M, const float scale);
173/// all elements are randomised
174void make_random_matrix(EST_FMatrix &M, const float scale);
175/// used for variance
176void make_random_diagonal_matrix(EST_FMatrix &M, const float scale);
177/// used for covariance
178void make_random_symmetric_matrix(EST_FMatrix &M, const float scale);
179
180void make_poly_basis_function(EST_FMatrix &T, EST_FVector t);
181
182/// elementwise add
183EST_FVector add(const EST_FVector &a,const EST_FVector &b);
184/// elementwise subtract
185EST_FVector subtract(const EST_FVector &a,const EST_FVector &b);
186
187/// enforce symmetry
188void symmetrize(EST_FMatrix &a);
189/// stack columns on top of each other to make a vector
190void stack_matrix(const EST_FMatrix &M, EST_FVector &v);
191/// inplace diagonalise
192void inplace_diagonalise(EST_FMatrix &a);
193
194
195float determinant(const EST_FMatrix &a);
196/// not implemented ??
197int singular(EST_FMatrix &a);
198/// exchange rows and columns
199void transpose(const EST_FMatrix &a,EST_FMatrix &b);
200EST_FMatrix triangulate(const EST_FMatrix &a);
201
202/// extract leading diagonal as a matrix
203EST_FMatrix diagonalise(const EST_FMatrix &a);
204/// extract leading diagonal as a vector
205EST_FVector diagonal(const EST_FMatrix &a);
206/// sum of elements
207float sum(const EST_FMatrix &a);
208void multiply(const EST_FMatrix &a, const EST_FMatrix &b, EST_FMatrix &c);
209int 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)
212EST_FMatrix cov_prod(const EST_FVector &v1,const EST_FVector &v2);
213
214EST_FMatrix operator*(const EST_FMatrix &a, const EST_FMatrix &b);
215EST_FMatrix operator-(const EST_FMatrix &a, const EST_FMatrix &b);
216EST_FMatrix operator+(const EST_FMatrix &a, const EST_FMatrix &b);
217
218EST_FVector operator-(const EST_FVector &a, const EST_FVector &b);
219EST_FVector operator+(const EST_FVector &a, const EST_FVector &b);
220
221EST_FMatrix sub(const EST_FMatrix &a, int row, int col);
222EST_FMatrix fmatrix_abs(const EST_FMatrix &a);
223
224EST_FMatrix row(const EST_FMatrix &a, int row);
225EST_FMatrix column(const EST_FMatrix &a, int col);
226
227
228/// least squares fit
229bool
230polynomial_fit(EST_FVector &x, EST_FVector &y, EST_FVector &co_effs, int order);
231
232/// weighted least squares fit
233bool
234polynomial_fit(EST_FVector &x, EST_FVector &y, EST_FVector &co_effs,
235 EST_FVector &weights, int order);
236
237float
238polynomial_value(const EST_FVector &coeffs, const float x);
239
240/// vector dot product
241float operator*(const EST_FVector &v1, const EST_FVector &v2);
242
243VAL_REGISTER_CLASS_DCLS(fmatrix,EST_FMatrix)
244VAL_REGISTER_CLASS_DCLS(fvector,EST_FVector)
245
246#endif
EST_FMatrix & operator+=(const EST_FMatrix &a)
Add elements of 2 same sized matrices.
Definition: EST_FMatrix.cc:74
EST_FMatrix(const EST_FMatrix &a)
copy constructor
Definition: EST_FMatrix.h:62
EST_FMatrix()
default constructor
Definition: EST_FMatrix.h:68
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
friend EST_FMatrix operator*(const EST_FMatrix &a, const float x)
Multiply all elements of matrix by {\tt x}.
Definition: EST_FMatrix.cc:182
void copyin(float **x, int rows, int cols)
Copy 2-d array {\tt x} of size {\tt rows x cols} into matrix.
Definition: EST_FMatrix.cc:328
EST_write_status est_save(const EST_String &filename, const EST_String &type)
Save in file in est format.
Definition: EST_FMatrix.cc:376
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 multiply by scalar
Definition: EST_FMatrix.cc:114
EST_FMatrix & operator/=(const float f)
elementwise divide by scalar
Definition: EST_FMatrix.cc:125
EST_FMatrix(int m, int n)
size constructor
Definition: EST_FMatrix.h:60
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_read_status load(const EST_String &filename)
Load from file (ascii or binary as defined in file)
Definition: EST_FMatrix.cc:513
EST_FVector()
Default constructor.
Definition: EST_FMatrix.h:121
EST_read_status load(const EST_String &filename)
load vector from file filename.
Definition: EST_FMatrix.cc:681
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
EST_FVector & operator*=(const EST_FVector &s)
elementwise multiply
Definition: EST_FMatrix.cc:584
EST_FVector & operator+=(const EST_FVector &s)
elementwise add
Definition: EST_FMatrix.cc:567
EST_FVector & operator/=(const float f)
elementwise divide by scalar
Definition: EST_FMatrix.cc:607
EST_FVector(const EST_FVector &a)
Copy constructor.
Definition: EST_FMatrix.h:119
EST_FVector(int n)
Size constructor.
Definition: EST_FMatrix.h:117
EST_write_status save(const EST_String &filename, const EST_String &type)
save vector to file filename.
Definition: EST_FMatrix.cc:833
INLINE int n() const
number of items in vector.
Definition: EST_TVector.h:254