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