1
/*************************************************************************/
3
/* Centre for Speech Technology Research */
4
/* University of Edinburgh, UK */
5
/* Copyright (c) 1996,1997 */
6
/* All Rights Reserved. */
7
/* Permission is hereby granted, free of charge, to use and distribute */
8
/* this software and its documentation without restriction, including */
9
/* without limitation the rights to use, copy, modify, merge, publish, */
10
/* distribute, sublicense, and/or sell copies of this work, and to */
11
/* permit persons to whom this work is furnished to do so, subject to */
12
/* the following conditions: */
13
/* 1. The code must retain the above copyright notice, this list of */
14
/* conditions and the following disclaimer. */
15
/* 2. Any modifications must be clearly marked as such. */
16
/* 3. Original authors' names are not deleted. */
17
/* 4. The authors' names are not used to endorse or promote products */
18
/* derived from this software without specific prior written */
20
/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
21
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
22
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
23
/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
24
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
25
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
26
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
27
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30
/*************************************************************************/
32
/* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
33
/* Date: Tue Jul 22 1997 */
34
/* --------------------------------------------------------------------- */
35
/* Example of matrix class use. */
37
/*************************************************************************/
41
* The EST_TMatrix class is a general purpose 2 dimensional array
42
* container class. It handles memory allocation and (if required) bounds
43
* checking and is reasonably efficient, so there should be little need
44
* to use bare &cpp; arrays.
56
#include "EST_TMatrix.h"
57
#include "EST_String.h"
59
EST_write_status save(const EST_String &filename, const EST_TVector<float> &a);
60
EST_write_status save(const EST_String &filename, const EST_TMatrix<float> &a);
64
static inline int max(int a, int b) { return a>b?a:b; }
69
/** @name Basic Matrix Use
70
* Instances of the TMatrix class are intended to behave as
71
* you would expect two dimensional arrays to work.
77
* Matrices are declared by giving the type and the number of
78
* rows and columns. Here we create a 10 row by 5 column matrix.
82
EST_TMatrix<float> m(10, 5);
89
* Access to values in the matrix is via the a() member function.
90
* This returns a reference, so you can assign values to matrix cells.
91
* As is usually the case in &cpp;, column and row indices start from 0.
95
for(int i=0; i<m.num_rows(); i++)
96
for(int j=0; j<m.num_columns(); j++)
97
m.a(i,j) = i+j/100.0; // Just something easy to recognise
102
* A simple output method is supplied, it just outputs a row at a time,
103
* tab separated to a named file. A filename of "-" means standard output.
106
cout << "Initial Matrix\n";
112
* Resize to 20 rows by 10 columns This fills the new
113
* area with <parameter>def_val</parameter>, which is 0.0 for floats.
119
cout << "Resized Matrix\n";
123
// Fill it with something easy to recognise.
124
for(int i0=0; i0<m.num_rows(); i0++)
125
for(int j=0; j<m.num_columns(); j++)
126
m.a(i0,j) = i0+j/100.0;
128
// Write to standard output in an ascii format.
129
cout << "Full Matrix\n";
135
/**@name Copying Data to/from a buffer
137
* Whole rows or columns can be extracted into a buffer, or can be
138
* filled with data from a buffer. The buffer must be pre-declared,
139
* and it is up to you to ensure it is big enough.
144
* Data can be extracted into a buffer in one operation
147
float *buf = new float[max(m.num_rows(),m.num_columns())];
153
for(int j1=0; j1<m.num_columns(); j1++)
154
cout << buf[j1] << "\t";
158
* And data can be inserted in a similar manner.
160
for(int i1=0; i1<m.num_rows(); i1++)
173
cout << "Updated Matrix (column 5 replaced with 100s from buffer)\n";
177
/**@name Sub-Matrices and Sub-Vectors
179
* A sub-vector or sub-matrix is a window onto a matrix. If you obtain a
180
* sub vector representing a row, for instance, you can treat it
181
* a normal vector, any changes you make affecting the underlying
187
/** Here is how we can create new variables which refer to the 11th
188
* row, 4th column and a 5X3 rectangle with top left hand corner (8,2).
189
* (since the first column or row is numbered 0, the numbers may be one
190
* less than you expect).
194
EST_TVector<float> row;
195
EST_TVector<float> column;
196
EST_TMatrix<float> rectangle;
200
m.sub_matrix(rectangle,
205
cout <<"Row 10 extracted as sub vector\n";
209
cout <<"Column 3 extracted as sub vector\n";
213
cout <<"Rectangle extracted as sub vector\n";
217
/** If we update the sub-vector, the main matrix changes.
222
// 10th row becomes squares of the index
223
for(int i2=0; i2<row.n(); i2++)
226
// 3rd column becomes cubes of the index
227
for(int i3=0; i3<column.n(); i3++)
228
column[i3] = i3*i3*i3;
230
// Central rectangle filled with -1
231
for(int i4=0; i4<rectangle.num_rows(); i4++)
232
for(int j4=0; j4<rectangle.num_columns(); j4++)
233
rectangle.a(i4, j4) = -1;
237
/** We can even extract rows and columns from a sub-matrix as follows.
241
EST_TVector<float> rrow;
242
EST_TVector<float> rcolumn;
244
// 3rd row of sub-matrix, part of 12th row of main matrix
245
rectangle.row(rrow, 2);
246
// 2nd column of sub-matrix, part of 8th column of main matrix
247
rectangle.column(rcolumn, 1);
251
for(int i6=0; i6<rcolumn.n(); i6++)
253
for(int i5=0; i5<rrow.n(); i5++)
258
cout << "Updated Matrix (row 10 becomes squares, column 3 becomes cubes, center becomes negative)\n";
265
/**@name Template Instantiation.
267
* Some &cpp; compilers require explicit guidance about which types
268
* of Matrix will be used. For many common types, including float,
269
* this guidance is included in the &est; libraries. However, if you
270
* need to use matrices of your own types, you will need to include
271
* declarations similar to the following.
275
#ifdef __NOT_REAL_CODE__
279
Declare_TMatrix(MyType)
281
#if defined(INSTANTIATE_TEMPLATES)
283
#include "../base_class/EST_TMatrix.cc"
285
Instantiate_TMatrix(MyType)
291
/** By using this form of declaration, you should ensure that your
292
* code will compile anywhere where the speech tools libraries will.