~ubuntu-branches/ubuntu/maverick/speech-tools/maverick

« back to all changes in this revision

Viewing changes to testsuite/matrix_example.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kumar Appaiah, Kartik Mistry, Kumar Appaiah
  • Date: 2010-07-17 11:32:04 UTC
  • mfrom: (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100717113204-mnse3jo236j107q8
Tags: 1:2.0.95~beta-1
[ Kartik Mistry ]
* debian/control:
  + [Lintian] Added missing ${misc:Depends}
  + Updated Standards-Version to 3.8.4 (no changes needed)
* debian/patches/const_char.diff:
  + Added missing patch header
* Removed unused patch invalid_const_char_conversion_fixes.diff

[ Kumar Appaiah ]
* New upstream release.
* Standards Version is now 3.9.0 (No changes needed)
* Update debian/rules to specify version numbers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 /*************************************************************************/
2
 
 /*                                                                       */
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        */
19
 
 /*      permission.                                                      */
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       */
28
 
 /*  THIS SOFTWARE.                                                       */
29
 
 /*                                                                       */
30
 
 /*************************************************************************/
31
 
 /*                                                                       */
32
 
 /*                 Author: Richard Caley (rjc@cstr.ed.ac.uk)             */
33
 
 /*                   Date: Tue Jul 22 1997                               */
34
 
 /* --------------------------------------------------------------------- */
35
 
 /* Example of matrix class use.                                          */
36
 
 /*                                                                       */
37
 
 /*************************************************************************/
38
 
 
39
 
/**@name EST_TMatrix 
40
 
  * 
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.
45
 
  *
46
 
  * @see EST_TMatrix
47
 
  * @see EST_TVector
48
 
  */
49
 
//@{
50
 
 
51
 
#include <cstdlib>
52
 
#include <iostream>
53
 
 
54
 
//@{ code
55
 
 
56
 
#include "EST_TMatrix.h"
57
 
#include "EST_String.h"
58
 
 
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);
61
 
 
62
 
//@} code
63
 
 
64
 
static inline int max(int a, int b) { return a>b?a:b; }
65
 
 
66
 
 
67
 
int main(void)
68
 
{
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.
72
 
    */
73
 
    
74
 
  //@{
75
 
  /**@name Declaration
76
 
    * 
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.
79
 
    */
80
 
  //@{ code
81
 
 
82
 
  EST_TMatrix<float> m(10, 5);
83
 
 
84
 
  //@} code
85
 
 
86
 
 
87
 
  /**@name Access
88
 
    *
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.
92
 
    */
93
 
  //@{ code
94
 
 
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
98
 
 
99
 
  //@} code
100
 
 
101
 
  /**@name Output
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.
104
 
    */
105
 
  //@{ code
106
 
  cout << "Initial Matrix\n";
107
 
  save("-",m);
108
 
  cout << "\n";
109
 
  //@} code
110
 
 
111
 
  /**@name Resizing
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.
114
 
    */
115
 
  //@{ code
116
 
  m.resize(20,10);
117
 
  //@} code
118
 
 
119
 
  cout << "Resized Matrix\n";
120
 
  save("-",m);
121
 
  cout << "\n";
122
 
 
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;
127
 
 
128
 
  // Write to standard output in an ascii format.
129
 
  cout << "Full Matrix\n";
130
 
  save("-",m);
131
 
  cout << "\n";
132
 
 
133
 
  //@}
134
 
 
135
 
  /**@name Copying Data to/from a buffer
136
 
    * 
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.
140
 
    */
141
 
  //@{
142
 
 
143
 
  /** 
144
 
    * Data can be extracted into a buffer in one operation
145
 
    */
146
 
  //@{ code
147
 
  float *buf = new float[max(m.num_rows(),m.num_columns())];
148
 
 
149
 
  m.copy_row(5, buf);
150
 
  //@} code
151
 
 
152
 
  cout << "Row 5\n";
153
 
  for(int j1=0; j1<m.num_columns(); j1++)
154
 
    cout << buf[j1] << "\t";
155
 
  cout << "\n\n";
156
 
 
157
 
  /**
158
 
    * And data can be inserted in a similar manner.
159
 
    */
160
 
  for(int i1=0; i1<m.num_rows(); i1++)
161
 
    buf[i1] = i1+100;
162
 
 
163
 
  //@{ code
164
 
 
165
 
  m.set_column(5,buf);
166
 
 
167
 
  //@} code
168
 
 
169
 
  delete [] buf;
170
 
 
171
 
  //@}
172
 
 
173
 
  cout << "Updated Matrix (column 5 replaced with 100s from buffer)\n";
174
 
  save("-",m);
175
 
  cout << "\n";
176
 
 
177
 
  /**@name Sub-Matrices and Sub-Vectors
178
 
    *
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
182
 
    * matrix.
183
 
    */
184
 
 
185
 
  //@{
186
 
 
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).
191
 
    */
192
 
 
193
 
  //@{ code
194
 
  EST_TVector<float> row;
195
 
  EST_TVector<float> column;
196
 
  EST_TMatrix<float> rectangle;
197
 
  
198
 
  m.row(row, 10);
199
 
  m.column(column, 3);
200
 
  m.sub_matrix(rectangle, 
201
 
               8, 5, 
202
 
               2, 3);
203
 
  //@} code
204
 
 
205
 
  cout <<"Row 10 extracted as sub vector\n";
206
 
  save("-",row);
207
 
  cout << "\n";
208
 
 
209
 
  cout <<"Column 3 extracted as sub vector\n";
210
 
  save("-",column);
211
 
  cout << "\n";
212
 
 
213
 
  cout <<"Rectangle extracted as sub vector\n";
214
 
  save("-",rectangle);
215
 
  cout << "\n";
216
 
 
217
 
  /**  If we update the sub-vector, the main matrix changes.
218
 
    */
219
 
 
220
 
  //@{ code
221
 
 
222
 
  // 10th row becomes squares of the index
223
 
  for(int i2=0; i2<row.n(); i2++)
224
 
    row[i2] = i2*i2;
225
 
 
226
 
  // 3rd column becomes cubes of the index
227
 
  for(int i3=0; i3<column.n(); i3++)
228
 
    column[i3] = i3*i3*i3;
229
 
 
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;
234
 
 
235
 
  //@} code
236
 
 
237
 
  /** We can even extract rows and columns from a sub-matrix as follows.
238
 
    */
239
 
 
240
 
  //@{ code
241
 
  EST_TVector<float> rrow;
242
 
  EST_TVector<float> rcolumn;
243
 
 
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);
248
 
 
249
 
  //@} code
250
 
 
251
 
  for(int i6=0; i6<rcolumn.n(); i6++)
252
 
    rcolumn[i6] = -3;
253
 
  for(int i5=0; i5<rrow.n(); i5++)
254
 
    rrow[i5] = -2;
255
 
 
256
 
  //@}
257
 
 
258
 
  cout << "Updated Matrix (row 10 becomes squares, column 3 becomes cubes, center becomes negative)\n";
259
 
  save("-",m);
260
 
  cout << "\n";
261
 
 
262
 
  exit(0);
263
 
}
264
 
 
265
 
/**@name Template Instantiation.
266
 
  * 
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.
272
 
  */
273
 
//@{
274
 
 
275
 
#ifdef __NOT_REAL_CODE__
276
 
 
277
 
//@{ code
278
 
 
279
 
Declare_TMatrix(MyType)
280
 
 
281
 
#if defined(INSTANTIATE_TEMPLATES)
282
 
 
283
 
#include "../base_class/EST_TMatrix.cc"
284
 
 
285
 
Instantiate_TMatrix(MyType)
286
 
 
287
 
#endif
288
 
 
289
 
  //@} code
290
 
 
291
 
  /** By using this form of declaration, you should ensure that your
292
 
    * code will compile anywhere where the speech tools libraries will.
293
 
    */
294
 
 
295
 
 #endif
296
 
 
297
 
  //@}
298
 
 
299
 
//@}
300