Edinburgh Speech Tools  2.1-release
EST_DMatrix.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 : 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 <cstddef>
45 #include "EST_TSimpleMatrix.h"
46 #include "EST_TSimpleVector.h"
47 #include "EST_FMatrix.h"
48 
49 
50 class EST_DVector;
51 
52 /** \class EST_DMatrix
53  * @ingroup containerclasses
54  * A matrix class for double precision floating point numbers.
55 EST_DMatrix x should be used instead of `double **x` wherever
56 possible.
57 */
58 class EST_DMatrix : public EST_TSimpleMatrix<double> {
59 private:
60 public:
61  /// size constructor
62  EST_DMatrix(int m, int n):EST_TSimpleMatrix<double>(m, n) {}
63  /// copy constructor
65 
67  /// CHECK - what does this do???
68  EST_DMatrix(const EST_DMatrix &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 =
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(double **x, ssize_t rows, ssize_t 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_DMatrix &operator*=(const double f);
95 
96  /// elementwise divide by scalar
97  EST_DMatrix &operator/=(const double f);
98 
99  /// Multiply all elements of matrix by `x`.
100  friend EST_DMatrix operator*(const EST_DMatrix &a, const double x);
101 
102  /// Multiply all elements of matrix by `x`.
103  friend EST_DMatrix operator*(const double x, const EST_DMatrix &a);
104 
105  /// Multiply matrix by vector.
106  friend EST_DVector operator*(const EST_DMatrix &a, const EST_DVector &v);
107 
108  /// Multiply vector by matrix
109  friend EST_DVector operator*(const EST_DVector &v,const EST_DMatrix &a);
110 
111  /// Multiply matrix by matrix.
112  friend EST_DMatrix operator*(const EST_DMatrix &a, const EST_DMatrix &b);
113 };
114 
115 
116 /** \class EST_DVector
117  * @ingroup containerclasses
118  \brief A vector class for double precision floating point
119  numbers. `EST_DVector x` should be used instead of
120  `float *x` wherever possible.
121 */
122 class EST_DVector: public EST_TSimpleVector<double> {
123 public:
124  /// Size constructor.
125  EST_DVector(int n): EST_TSimpleVector<double>(n) {}
126  /// Copy constructor.
128  /// Default constructor.
130 
131  /// elementwise multiply
132  EST_DVector &operator*=(const EST_DVector &s);
133 
134  /// elementwise add
135  EST_DVector &operator+=(const EST_DVector &s);
136 
137  /// elementwise multiply by scalar
138  EST_DVector &operator*=(const double d);
139 
140  /// elementwise divide by scalar
141  EST_DVector &operator/=(const double d);
142 
143  EST_write_status est_save(const EST_String &filename,
144  const EST_String &type);
145 
146  /// save vector to file <tt> filename</tt>.
147  EST_write_status save(const EST_String &filename,
148  const EST_String &type);
149 
150  /// load vector from file <tt> filename</tt>.
151  EST_read_status load(const EST_String &filename);
152  /// Load from file in est format (binary/ascii defined in file itself)
153  EST_read_status est_load(const EST_String &filename);
154 };
155 
156 int square(const EST_DMatrix &a);
157 /// inverse
158 int inverse(const EST_DMatrix &a, EST_DMatrix &inv);
159 int inverse(const EST_DMatrix &a, EST_DMatrix &inv, int &singularity);
160 /// pseudo inverse (for non-square matrices)
161 int pseudo_inverse(const EST_DMatrix &a, EST_DMatrix &inv);
162 int pseudo_inverse(const EST_DMatrix &a, EST_DMatrix &inv,int &singularity);
163 
164 /// some useful matrix creators
165 /// make an identity matrix of dimension n
166 void eye(EST_DMatrix &a, const int n);
167 /// make already square matrix into I without resizing
168 void eye(EST_DMatrix &a);
169 
170 /// the user should use est_seed to seed the random number generator
171 void est_seed();
172 void est_seed48();
173 /// all elements are randomised
174 void make_random_vector(EST_DVector &M, const double scale);
175 /// all elements are randomised
176 void make_random_matrix(EST_DMatrix &M, const double scale);
177 /// used for variance
178 void make_random_diagonal_matrix(EST_DMatrix &M, const double scale);
179 /// used for covariance
180 void make_random_symmetric_matrix(EST_DMatrix &M, const double scale);
181 
183 
184 /// elementwise add
185 EST_DVector add(const EST_DVector &a,const EST_DVector &b);
186 /// elementwise subtract
187 EST_DVector subtract(const EST_DVector &a,const EST_DVector &b);
188 
189 /// enforce symmetry
190 void symmetrize(EST_DMatrix &a);
191 /// stack columns on top of each other to make a vector
192 void stack_matrix(const EST_DMatrix &M, EST_DVector &v);
193 /// inplace diagonalise
195 
196 
197 double determinant(const EST_DMatrix &a);
198 /// not implemented ??
199 int singular(EST_DMatrix &a);
200 /// exchange rows and columns
201 void transpose(const EST_DMatrix &a,EST_DMatrix &b);
203 
204 /// extract leading diagonal as a matrix
206 /// extract leading diagonal as a vector
208 /// sum of elements
209 double sum(const EST_DMatrix &a);
210 void multiply(const EST_DMatrix &a, const EST_DMatrix &b, EST_DMatrix &c);
211 int floor_matrix(EST_DMatrix &M, const double floor);
212 
213 /// matrix product of two vectors (#rows = length of first vector, #cols = length of second vector)
214 EST_DMatrix cov_prod(const EST_DVector &v1,const EST_DVector &v2);
215 
216 EST_DMatrix operator*(const EST_DMatrix &a, const EST_DMatrix &b);
217 EST_DMatrix operator-(const EST_DMatrix &a, const EST_DMatrix &b);
218 EST_DMatrix operator+(const EST_DMatrix &a, const EST_DMatrix &b);
219 
220 EST_DVector operator-(const EST_DVector &a, const EST_DVector &b);
221 EST_DVector operator+(const EST_DVector &a, const EST_DVector &b);
222 
223 EST_DMatrix sub(const EST_DMatrix &a, ssize_t row, ssize_t col);
225 
227 EST_DMatrix column(const EST_DMatrix &a, ssize_t col);
228 
229 
230 /// least squares fit
231 bool
232 polynomial_fit(EST_DVector &x, EST_DVector &y, EST_DVector &co_effs, int order);
233 
234 /// weighted least squares fit
235 bool
237  EST_DVector &weights, int order);
238 
239 double
240 polynomial_value(const EST_DVector &coeffs, const double x);
241 
242 /// vector dot product
243 double operator*(const EST_DVector &v1, const EST_DVector &v2);
244 
245 
246 #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:328
EST_DVector(const EST_DVector &a)
Copy constructor.
Definition: EST_DMatrix.h:127
int square(const EST_DMatrix &a)
void est_seed()
the user should use est_seed to seed the random number generator
Definition: vec_mat_aux.cc:705
EST_write_status
void eye(EST_DMatrix &a, const int n)
some useful matrix creators make an identity matrix of dimension n
EST_DMatrix cov_prod(const EST_DVector &v1, const EST_DVector &v2)
matrix product of two vectors (#rows = length of first vector, #cols = length of second vector) ...
static EST_String default_file_type
Definition: EST_DMatrix.h:66
void make_random_vector(EST_DVector &M, const double scale)
all elements are randomised
double determinant(const EST_DMatrix &a)
EST_DMatrix(const EST_DMatrix &a)
copy constructor
Definition: EST_DMatrix.h:64
void inplace_diagonalise(EST_DMatrix &a)
inplace diagonalise
friend EST_DMatrix operator*(const EST_DMatrix &a, const double x)
Multiply all elements of matrix by x.
Definition: EST_DMatrix.cc:183
double polynomial_value(const EST_DVector &coeffs, const double x)
EST_DMatrix DMatrix_abs(const EST_DMatrix &a)
int ssize_t
const double & a(ssize_t row, ssize_t col) const
Definition: EST_TMatrix.h:196
void copyin(double **x, ssize_t rows, ssize_t cols)
Copy 2-d array x of size rows x cols into matrix.
Definition: EST_DMatrix.cc:316
void make_poly_basis_function(EST_DMatrix &T, EST_DVector t)
void column(EST_TVector< double > &cv, ssize_t c, ssize_t start_r=0, int len=-1)
Make the vector cv a window onto column c
EST_DVector subtract(const EST_DVector &a, const EST_DVector &b)
elementwise subtract
int pseudo_inverse(const EST_DMatrix &a, EST_DMatrix &inv)
pseudo inverse (for non-square matrices)
EST_DMatrix()
default constructor
Definition: EST_DMatrix.h:70
void make_random_symmetric_matrix(EST_DMatrix &M, const double scale)
used for covariance
double sum(const EST_DMatrix &a)
sum of elements
EST_DVector diagonal(const EST_DMatrix &a)
extract leading diagonal as a vector
int floor_matrix(EST_DMatrix &M, const double floor)
EST_DMatrix & operator+=(const EST_DMatrix &a)
Add elements of 2 same sized matrices.
Definition: EST_DMatrix.cc:70
void make_random_diagonal_matrix(EST_DMatrix &M, const double scale)
used for variance
void symmetrize(EST_DMatrix &a)
enforce symmetry
EST_DMatrix operator+(const EST_DMatrix &a, const EST_DMatrix &b)
Definition: EST_DMatrix.cc:132
EST_DVector add(const EST_DVector &a, const EST_DVector &b)
elementwise add
A vector class for double precision floating point numbers. EST_DVector x should be used instead of f...
Definition: EST_DMatrix.h:122
f
Definition: EST_item_aux.cc:48
bool polynomial_fit(EST_DVector &x, EST_DVector &y, EST_DVector &co_effs, int order)
least squares fit
EST_DVector()
Default constructor.
Definition: EST_DMatrix.h:129
void row(EST_TVector< double > &rv, ssize_t r, ssize_t start_c=0, int len=-1)
Make the vector rv a window onto row r
void transpose(const EST_DMatrix &a, EST_DMatrix &b)
exchange rows and columns
EST_DMatrix & operator*=(const double f)
elementwise multiply by scalar
Definition: EST_DMatrix.cc:110
void make_random_matrix(EST_DMatrix &M, const double scale)
all elements are randomised
void multiply(const EST_DMatrix &a, const EST_DMatrix &b, EST_DMatrix &c)
Definition: EST_DMatrix.cc:293
EST_read_status
void stack_matrix(const EST_DMatrix &M, EST_DVector &v)
stack columns on top of each other to make a vector
EST_DMatrix triangulate(const EST_DMatrix &a)
EST_DMatrix operator-(const EST_DMatrix &a, const EST_DMatrix &b)
Definition: EST_DMatrix.cc:154
EST_DMatrix & operator/=(const double f)
elementwise divide by scalar
Definition: EST_DMatrix.cc:121
EST_DVector(int n)
Size constructor.
Definition: EST_DMatrix.h:125
EST_read_status load(const EST_String &filename)
Load from file (ascii or binary as defined in file)
Definition: EST_DMatrix.cc:516
EST_DMatrix diagonalise(const EST_DMatrix &a)
extract leading diagonal as a matrix
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:90
int singular(EST_DMatrix &a)
not implemented ??
INLINE ssize_t n() const
number of items in vector.
Definition: EST_TVector.h:251
EST_write_status est_save(const EST_String &filename, const EST_String &type)
Save in file in est format.
Definition: EST_DMatrix.cc:365
EST_DMatrix(int m, int n)
size constructor
Definition: EST_DMatrix.h:62
int inverse(const EST_DMatrix &a, EST_DMatrix &inv)
inverse
EST_DMatrix sub(const EST_DMatrix &a, ssize_t row, ssize_t col)
void est_seed48()
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:439