Debian Codegraph @

fon/Matrix.h

raw
1 #ifndef _Matrix_h_
2 #define _Matrix_h_
3 /* Matrix.h
4 *
5 * Copyright (C) 1992-2005,2007-2019,2022,2024 Paul Boersma
6 *
7 * This code is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
11 *
12 * This code is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this work. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "SampledXY.h"
22 #include "Graphics.h"
23 #include "../stat/Table.h"
24 #include "../stat/TableOfReal.h"
25 Thing_declare (Interpreter);
26
27 #include "Matrix_def.h"
28
29 #if 0
30 template <typename T, typename... Args>
31 autoSomeThing <T> Thing_create (Args ... args) {
32 autoSomeThing <T> me (new T); // this `new` has to set classInfo
33 my T::init (args...);
34 return me;
35 }
36
37 template <typename... ArgumentTypes>
38 autoMatrix CreateMatrix (ArgumentTypes... arguments) {
39 try {
40 autoMatrix me = Thing_new (Matrix);
41 Matrix_init (me.get(), arguments...);
42 return me;
43 } catch (MelderError) {
44 Melder_throw (U"Matrix object not created.");
45 }
46 }
47 #endif
48
49 void Matrix_init
50 (Matrix me, double xmin, double xmax, integer nx, double dx, double x1,
51 double ymin, double ymax, integer ny, double dy, double y1);
52
53 autoMatrix Matrix_create
54 (double xmin, double xmax, integer nx, double dx, double x1,
55 double ymin, double ymax, integer ny, double dy, double y1);
56 /*
57 Function:
58 return a new empty Matrix.
59 Preconditions:
60 xmin <= xmax;
61 ymin <= ymax;
62 nx >= 1;
63 ny >= 1;
64 dx > 0.0;
65 dy > 0.0;
66 Postconditions:
67 result -> xmin == xmin;
68 result -> xmax == xmax;
69 result -> ymin == ymin;
70 result -> ymax == ymax;
71 result -> nx == nx;
72 result -> ny == ny;
73 result -> dx == dx;
74 result -> dy == dy;
75 result -> x1 == x1;
76 result -> y1 == y1;
77 result -> z [1..ny] [1..nx] == 0.0;
78 */
79
80 autoMatrix Matrix_createSimple (integer numberOfRows, integer numberOfColumns);
81 /*
82 Function:
83 return a new empty Matrix.
84 Preconditions:
85 numberOfRows >= 1;
86 numberOfColumns >= 1;
87 Postconditions:
88 result -> xmin == 0.5;
89 result -> xmax == numberOfColumns + 0.5;
90 result -> ymin == 0.5;
91 result -> ymax == numberOfRows + 0.5;
92 result -> nx == numberOfColumns;
93 result -> ny == numberOfRows;
94 result -> dx == 1.0;
95 result -> dy == 1.0;
96 result -> x1 == 1.0;
97 result -> y1 == 1.0;
98 result -> z [1..ny] [1..nx] == 0.0;
99 */
100
101 /* An implemented method
102
103 void structMatrix :: v1_writeText (MelderFile file);
104 writes a Matrix as text to `file`.
105 A sample of the format follows:
106 0 5000 1 2 ! xmin xmax ymin ymax
107 8193 2 ! nx ny
108 0.61035156 1 ! dx dy
109 0 1 ! x1 y1
110 5.1e-8 ! 0 1
111 5 ! 0.61035156 1
112 -3.556473 ! 1.2207031 1
113 ...
114 90000000 ! 4998.7793 2
115 3.1415927 ! 4999.3896 2
116 -5.735668e35 ! 5000 2
117 The data lines (all lines after the fourth) contain: my z [iy, ix], x, y.
118 things written after the "!" are mere comments:
119 you cannot use them to change the meaning or order of the data.
120 */
121
122 integer Matrix_getWindowSamplesX (constMatrix me,
123 double xmin, double xmax,
124 integer *out_ixmin, integer *out_ixmax);
125 /*
126 Function:
127 return the number of samples with x values in [xmin, xmax].
128 Put the first of these samples in ixmin.
129 Put the last of these samples in ixmax.
130 Postconditions:
131 *out_ixmin >= 1;
132 *out_ixmax <= my nx;
133 if (result != 0) *out_ixmin <= *out_ixmax; else *out_ixmin > *out_ixmax;
134 if (result != 0) result == *out_ixmax - *out_ixmin + 1;
135 */
136
137 double Matrix_getValueAtXY (constMatrix me, double x, double y);
138 /*
139 Linear interpolation between matrix points,
140 constant extrapolation in cells on the edge,
141 undefined outside the union of the unit squares around the points.
142 */
143
144 double Matrix_getSum (constMatrix me);
145 double Matrix_getNorm (constMatrix me);
146
147 double Matrix_columnToX (constMatrix me, double column);
148 // return my x1 + (column - 1) * my dx
149
150 double Matrix_rowToY (constMatrix me, double row);
151 // return my y1 + (row - 1) * my dy
152
153 double Matrix_xToColumn (constMatrix me, double x);
154 // return (x - xmin) / my dx + 1
155
156 integer Matrix_xToLowColumn (constMatrix me, double x);
157 // return Melder_ifloor (Matrix_xToColumn (me, x))
158
159 integer Matrix_xToHighColumn (constMatrix me, double x);
160 // return Melder_iceiling (Matrix_xToColumn (me, x))
161
162 integer Matrix_xToNearestColumn (constMatrix me, double x);
163 // return Melder_iround (Matrix_xToColumn (me, x))
164
165 double Matrix_yToRow (constMatrix me, double y);
166 // return (y - ymin) / my dy + 1
167
168 integer Matrix_yToLowRow (constMatrix me, double y);
169 // return Melder_ifloor (Matrix_yToRow (me, y))
170
171 integer Matrix_yToHighRow (constMatrix me, double x);
172 // return Melder_iceiling (Matrix_yToRow (me, y))
173
174 integer Matrix_yToNearestRow (constMatrix me, double y);
175 // return Melder_iround (Matrix_yToRow (me, y))
176
177 integer Matrix_getWindowSamplesY (constMatrix me,
178 double ymin, double ymax,
179 integer *out_iymin, integer *out_iymax
180 );
181
182 integer Matrix_getWindowExtrema (constMatrix me,
183 integer ixmin, integer ixmax,
184 integer iymin, integer iymax,
185 double *out_minimum, double *out_maximum
186 );
187 /*
188 Function:
189 compute the minimum and maximum values of my z over all samples inside [ixmin, ixmax] * [iymin, iymax].
190 Arguments:
191 if ixmin = 0, start at first column; if ixmax = 0, end at last column (same for iymin and iymax).
192 Return value:
193 the number of samples inside the window.
194 Postconditions:
195 if result == 0, *minimum and *maximum are not changed;
196 */
197
198 void Matrix_formula (mutableMatrix me, conststring32 expression, Interpreter interpreter, mutableMatrix target);
199 /*
200 Arguments:
201 "me" is the Matrix referred to as "self" or with "nx" etc. in the expression
202 "target" is the Matrix whose elements will change according to:
203 FOR row FROM 1 TO my ny
204 FOR col FROM 1 TO my nx
205 target -> z [row, col] = expression
206 ENDFOR
207 ENDFOR
208 "expression" is the text to be compiled and interpreted.
209 If "target" is null, the result will go to "me"; otherwise, to "target".
210 Return value:
211 0 in case of failure, otherwise 1.
212 */
213 void Matrix_formula_part (mutableMatrix me, double xmin, double xmax, double ymin, double ymax,
214 conststring32 expression, Interpreter interpreter, mutableMatrix target);
215
216 /***** Graphics routines. *****/
217 /*
218 All of these routines show the samples of a Matrix whose x and y values
219 are inside the window [xmin, xmax] * [ymin, ymax].
220 The scaling of the values of these samples is determined by "minimum" and "maximum".
221 All of these functions can perform automatic windowing and scaling:
222 if xmax <= xmin, the window in the x direction will be set to [my xmin, my xmax];
223 if ymax <= ymin, the window in the y direction will be set to [my ymin, my ymax];
224 if maximum <= minimum, the windowing (scaling) in the z direction will be determined
225 by the minimum and maximum values of the samples inside the window.
226 */
227
228 void Matrix_drawRows (constMatrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
229 double minimum, double maximum);
230 /*
231 Every row is plotted as a function of x,
232 with straight lines connecting the sample points.
233 The rows are stacked from bottom to top.
234 */
235
236 void Matrix_drawOneContour (constMatrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
237 double height);
238
239 void Matrix_drawContours (constMatrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
240 double minimum, double maximum);
241 /* A contour altitude plot with curves at multiple heights. */
242
243 void Matrix_paintContours (constMatrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
244 double minimum, double maximum);
245 /* A contour plot with multiple shades of grey and white (low) and black (high) paint. */
246
247 void Matrix_paintImage (constMatrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
248 double minimum, double maximum);
249 /*
250 Two-dimensional interpolation of greys.
251 The larger the value of the sample, the darker the greys.
252 */
253
254 void Matrix_paintCells (constMatrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
255 double minimum, double maximum);
256 /*
257 Every sample is drawn as a grey rectangle.
258 The larger the value of the sample, the darker the rectangle.
259 */
260
261 void Matrix_paintSurface (constMatrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
262 double minimum, double maximum, double elevation, double azimuth);
263 /*
264 3D surface plot. Every space between adjacent four samples is drawn as a tetragon filled with a grey value.
265 'elevation' may be 30 degrees, 'azimuth' may be 45 degrees.
266 */
267
268 void Matrix_playMovie (constMatrix me, Graphics g);
269
270 autoMatrix Matrix_readFromRawTextFile (MelderFile file);
271 autoMatrix Matrix_readAP (MelderFile file);
272 autoMatrix Matrix_appendRows (constMatrix me, constMatrix thee, ClassInfo klas);
273
274 void Matrix_eigen (constMatrix me, autoMatrix *eigenvectors, autoMatrix *eigenvalues);
275 autoMatrix Matrix_power (constMatrix me, integer power);
276
277 void Matrix_scaleAbsoluteExtremum (mutableMatrix me, double scale);
278
279 autoMatrix Table_to_Matrix (Table me);
280 void Matrix_writeToMatrixTextFile (constMatrix me, MelderFile file);
281 void Matrix_writeToHeaderlessSpreadsheetFile (constMatrix me, MelderFile file);
282
283 autoMatrix TableOfReal_to_Matrix (constTableOfReal me);
284 autoTableOfReal Matrix_to_TableOfReal (constMatrix me);
285
286 /* End of file Matrix.h */
287 #endif