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