| 1 |
/* Matrix.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2025 Paul Boersma |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "Matrix.h" |
| 20 |
#include "NUM2.h" |
| 21 |
#include "Formula.h" |
| 22 |
#include "Eigen.h" |
| 23 |
|
| 24 |
#include "oo_DESTROY.h" |
| 25 |
#include "Matrix_def.h" |
| 26 |
#include "oo_COPY.h" |
| 27 |
#include "Matrix_def.h" |
| 28 |
#include "oo_EQUAL.h" |
| 29 |
#include "Matrix_def.h" |
| 30 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 31 |
#include "Matrix_def.h" |
| 32 |
#include "oo_WRITE_TEXT.h" |
| 33 |
#include "Matrix_def.h" |
| 34 |
#include "oo_WRITE_BINARY.h" |
| 35 |
#include "Matrix_def.h" |
| 36 |
#include "oo_READ_BINARY.h" |
| 37 |
#include "Matrix_def.h" |
| 38 |
#include "oo_DESCRIPTION.h" |
| 39 |
#include "Matrix_def.h" |
| 40 |
|
| 41 |
Thing_implement (Matrix, SampledXY, 2); |
| 42 |
|
| 43 |
void structMatrix :: v1_info () { |
| 44 |
structDaata :: v1_info (); |
| 45 |
double minimum = 0.0, maximum = 0.0; |
| 46 |
Matrix_getWindowExtrema (this, 1, our nx, 1, our ny, & minimum, & maximum); |
| 47 |
MelderInfo_writeLine (U"xmin: ", our xmin); |
| 48 |
MelderInfo_writeLine (U"xmax: ", our xmax); |
| 49 |
MelderInfo_writeLine (U"Number of columns: ", our nx); |
| 50 |
MelderInfo_writeLine (U"dx: ", our dx, U" (-> sampling rate ", 1.0 / our dx, U" )"); |
| 51 |
MelderInfo_writeLine (U"x1: ", our x1); |
| 52 |
MelderInfo_writeLine (U"ymin: ", our ymin); |
| 53 |
MelderInfo_writeLine (U"ymax: ", our ymax); |
| 54 |
MelderInfo_writeLine (U"Number of rows: ", our ny); |
| 55 |
MelderInfo_writeLine (U"dy: ", our dy, U" (-> sampling rate ", 1.0 / our dy, U" )"); |
| 56 |
MelderInfo_writeLine (U"y1: ", our y1); |
| 57 |
MelderInfo_writeLine (U"Minimum value: ", minimum); |
| 58 |
MelderInfo_writeLine (U"Maximum value: ", maximum); |
| 59 |
} |
| 60 |
|
| 61 |
void structMatrix :: v1_readText (MelderReadText text, int formatVersion) { |
| 62 |
if (formatVersion < 0) { |
| 63 |
our xmin = texgetr64 (text); |
| 64 |
our xmax = texgetr64 (text); |
| 65 |
our ymin = texgetr64 (text); |
| 66 |
our ymax = texgetr64 (text); |
| 67 |
our nx = texgeti32 (text); |
| 68 |
our ny = texgeti32 (text); |
| 69 |
our dx = texgetr64 (text); |
| 70 |
our dy = texgetr64 (text); |
| 71 |
our x1 = texgetr64 (text); |
| 72 |
our y1 = texgetr64 (text); |
| 73 |
} else { |
| 74 |
Matrix_Parent :: v1_readText (text, formatVersion); |
| 75 |
} |
| 76 |
Melder_require (our xmin <= our xmax, U"xmin should be less than or equal to xmax."); |
| 77 |
Melder_require (our ymin <= our ymax, U"ymin should be less than or equal to ymax."); |
| 78 |
Melder_require (our nx >= 1, U"nx should be at least 1."); |
| 79 |
Melder_require (our ny >= 1, U"ny should be at least 1."); |
| 80 |
Melder_require (our dx > 0.0, U"dx should be greater than 0.0."); |
| 81 |
Melder_require (our dy > 0.0, U"dy should be greater than 0.0."); |
| 82 |
our z = matrix_readText_r64 (our ny, our nx, text, "z"); |
| 83 |
} |
| 84 |
|
| 85 |
double structMatrix :: v_getValueAtSample (integer isamp, integer ilevel, int unit) const { |
| 86 |
const double value = our z [ilevel] [isamp]; |
| 87 |
return ( isdefined (value) ? our v_convertStandardToSpecialUnit (value, ilevel, unit) : undefined ); |
| 88 |
} |
| 89 |
|
| 90 |
double structMatrix :: v_getMatrix (integer irow, integer icol) const { |
| 91 |
if (irow < 1 || irow > our ny) |
| 92 |
return 0.0; |
| 93 |
if (icol < 1 || icol > our nx) |
| 94 |
return 0.0; |
| 95 |
return z [irow] [icol]; |
| 96 |
} |
| 97 |
|
| 98 |
double structMatrix :: v_getFunction2 (double x, double y) const { |
| 99 |
const double rrow = (y - our y1) / our dy + 1.0; |
| 100 |
const double rcol = (x - our x1) / our dx + 1.0; |
| 101 |
const integer irow = Melder_ifloor (rrow), icol = Melder_ifloor (rcol); |
| 102 |
const double drow = rrow - irow, dcol = rcol - icol; |
| 103 |
const double z1 = irow < 1 || irow > our ny || icol < 1 || icol > our nx ? 0.0 : z [irow] [icol]; |
| 104 |
const double z2 = irow < 0 || irow >= our ny || icol < 1 || icol > our nx ? 0.0 : z [irow + 1] [icol]; |
| 105 |
const double z3 = irow < 1 || irow > our ny || icol < 0 || icol >= our nx ? 0.0 : z [irow] [icol + 1]; |
| 106 |
const double z4 = irow < 0 || irow >= our ny || icol < 0 || icol >= our nx ? 0.0 : z [irow + 1] [icol + 1]; |
| 107 |
return (1.0 - drow) * (1.0 - dcol) * z1 + drow * (1.0 - dcol) * z2 + (1.0 - drow) * dcol * z3 + drow * dcol * z4; |
| 108 |
} |
| 109 |
|
| 110 |
void Matrix_init (const mutableMatrix me, |
| 111 |
const double xmin, const double xmax, const integer nx, const double dx, const double x1, |
| 112 |
const double ymin, const double ymax, const integer ny, const double dy, const double y1 |
| 113 |
) { |
| 114 |
Sampled_init (me, xmin, xmax, nx, dx, x1); |
| 115 |
my ymin = ymin; |
| 116 |
my ymax = ymax; |
| 117 |
my ny = ny; |
| 118 |
my dy = dy; |
| 119 |
my y1 = y1; |
| 120 |
my z = zero_MAT (my ny, my nx); |
| 121 |
} |
| 122 |
|
| 123 |
autoMatrix Matrix_create ( |
| 124 |
const double xmin, const double xmax, const integer nx, const double dx, const double x1, |
| 125 |
const double ymin, const double ymax, const integer ny, const double dy, const double y1 |
| 126 |
) { |
| 127 |
try { |
| 128 |
autoMatrix me = Thing_new (Matrix); |
| 129 |
Matrix_init (me.get(), xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1); |
| 130 |
return me; |
| 131 |
} catch (MelderError) { |
| 132 |
Melder_throw (U"Matrix object not created."); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
autoMatrix Matrix_createSimple (const integer numberOfRows, const integer numberOfColumns) { |
| 137 |
try { |
| 138 |
autoMatrix me = Thing_new (Matrix); |
| 139 |
Matrix_init (me.get(), |
| 140 |
0.5, numberOfColumns + 0.5, numberOfColumns, 1.0, 1.0, |
| 141 |
0.5, numberOfRows + 0.5, numberOfRows , 1.0, 1.0 |
| 142 |
); |
| 143 |
return me; |
| 144 |
} catch (MelderError) { |
| 145 |
Melder_throw (U"Matrix object not created."); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
double Matrix_columnToX (const constMatrix me, const double column) { return my x1 + (column - 1.0) * my dx; } // FIXME inline and use Sampled |
| 150 |
|
| 151 |
double Matrix_rowToY (const constMatrix me, const double row) { return my y1 + (row - 1.0) * my dy; } |
| 152 |
|
| 153 |
double Matrix_xToColumn (const constMatrix me, const double x) { return (x - my x1) / my dx + 1.0; } |
| 154 |
|
| 155 |
integer Matrix_xToLowColumn (const constMatrix me, const double x) { return Melder_ifloor (Matrix_xToColumn (me, x)); } |
| 156 |
|
| 157 |
integer Matrix_xToHighColumn (const constMatrix me, const double x) { return Melder_iceiling (Matrix_xToColumn (me, x)); } |
| 158 |
|
| 159 |
integer Matrix_xToNearestColumn (const constMatrix me, const double x) { return Melder_iround (Matrix_xToColumn (me, x)); } |
| 160 |
|
| 161 |
double Matrix_yToRow (const constMatrix me, const double y) { return (y - my y1) / my dy + 1.0; } |
| 162 |
|
| 163 |
integer Matrix_yToLowRow (const constMatrix me, const double y) { return Melder_ifloor (Matrix_yToRow (me, y)); } |
| 164 |
|
| 165 |
integer Matrix_yToHighRow (const constMatrix me, const double y) { return Melder_iceiling (Matrix_yToRow (me, y)); } |
| 166 |
|
| 167 |
integer Matrix_yToNearestRow (const constMatrix me, const double y) { return Melder_iround (Matrix_yToRow (me, y)); } |
| 168 |
|
| 169 |
integer Matrix_getWindowSamplesX (const constMatrix me, |
| 170 |
const double xmin, const double xmax, integer *const ixmin, integer *const ixmax |
| 171 |
) { |
| 172 |
*ixmin = 1 + Melder_iceiling ((xmin - my x1) / my dx); |
| 173 |
*ixmax = 1 + Melder_ifloor ((xmax - my x1) / my dx); |
| 174 |
if (*ixmin < 1) |
| 175 |
*ixmin = 1; |
| 176 |
if (*ixmax > my nx) |
| 177 |
*ixmax = my nx; |
| 178 |
if (*ixmin > *ixmax) |
| 179 |
return 0; |
| 180 |
return *ixmax - *ixmin + 1; |
| 181 |
} |
| 182 |
|
| 183 |
integer Matrix_getWindowSamplesY (const constMatrix me, |
| 184 |
const double ymin, const double ymax, |
| 185 |
integer *const iymin, integer *const iymax |
| 186 |
) { |
| 187 |
*iymin = 1 + Melder_iceiling ((ymin - my y1) / my dy); |
| 188 |
*iymax = 1 + Melder_ifloor ((ymax - my y1) / my dy); |
| 189 |
if (*iymin < 1) |
| 190 |
*iymin = 1; |
| 191 |
if (*iymax > my ny) |
| 192 |
*iymax = my ny; |
| 193 |
if (*iymin > *iymax) |
| 194 |
return 0; |
| 195 |
return *iymax - *iymin + 1; |
| 196 |
} |
| 197 |
|
| 198 |
integer Matrix_getWindowExtrema (const constMatrix me, |
| 199 |
integer ixmin, integer ixmax, |
| 200 |
integer iymin, integer iymax, |
| 201 |
double *const minimum, double *const maximum) |
| 202 |
{ |
| 203 |
if (ixmin == 0) // default = all |
| 204 |
ixmin = 1; |
| 205 |
if (ixmax == 0) // default = all |
| 206 |
ixmax = my nx; |
| 207 |
if (iymin == 0) // default = all |
| 208 |
iymin = 1; |
| 209 |
if (iymax == 0) // default = all |
| 210 |
iymax = my ny; |
| 211 |
if (ixmin > ixmax || iymin > iymax) |
| 212 |
return 0; |
| 213 |
MelderExtremaWithInit extrema; |
| 214 |
for (integer iy = iymin; iy <= iymax; iy ++) |
| 215 |
for (integer ix = ixmin; ix <= ixmax; ix ++) |
| 216 |
extrema.update (my z [iy] [ix]); |
| 217 |
*minimum = extrema.min; |
| 218 |
*maximum = extrema.max; |
| 219 |
return (ixmax - ixmin + 1) * (iymax - iymin + 1); |
| 220 |
} |
| 221 |
|
| 222 |
double Matrix_getValueAtXY (const constMatrix me, const double x, const double y) { |
| 223 |
const double row_real = (y - my y1) / my dy + 1.0; |
| 224 |
const double col_real = (x - my x1) / my dx + 1.0; |
| 225 |
/* |
| 226 |
We imagine a unit square around every (xi, yi) point in the matrix. |
| 227 |
For (x, y) values outside the union of these squares, the z value is undefined. |
| 228 |
*/ |
| 229 |
if (row_real < 0.5 || row_real > my ny + 0.5) |
| 230 |
return undefined; |
| 231 |
if (col_real < 0.5 || col_real > my nx + 0.5) |
| 232 |
return undefined; |
| 233 |
/* |
| 234 |
Determine the four nearest (xi, yi) points. |
| 235 |
*/ |
| 236 |
integer bottomRow = Melder_ifloor (row_real); // 0 <= bottomRow <= my ny |
| 237 |
integer topRow = bottomRow + 1; // 1 <= topRow <= my ny + 1 |
| 238 |
integer leftCol = Melder_ifloor (col_real); // 0 <= leftCol <= my nx |
| 239 |
integer rightCol = leftCol + 1; // 1 <= rightCol <= my nx + 1 |
| 240 |
const double drow = row_real - bottomRow; // 0.0 <= drow < 1.0 |
| 241 |
const double dcol = col_real - leftCol; // 0.0 <= dcol < 1.0 |
| 242 |
/* |
| 243 |
If adjacent points exist |
| 244 |
(i.e., both row numbers are between 1 and my ny, |
| 245 |
or both column numbers are between 1 and my nx), |
| 246 |
we do linear interpolation. |
| 247 |
If not, we do constant extrapolation, |
| 248 |
which can be simulated by an interpolation between equal z values. |
| 249 |
*/ |
| 250 |
Melder_clipLeft (1_integer, & bottomRow); // 1 <= bottomRow <= my ny |
| 251 |
Melder_clipRight (& topRow, my ny); // 1 <= topRow <= my ny |
| 252 |
Melder_clipLeft (1_integer, & leftCol); // 1 <= leftCol <= my nx |
| 253 |
Melder_clipRight (& rightCol, my nx); // 1 <= rightCol <= my nx |
| 254 |
return (1.0 - drow) * (1.0 - dcol) * my z [bottomRow] [leftCol] + |
| 255 |
drow * (1.0 - dcol) * my z [topRow] [leftCol] + |
| 256 |
(1.0 - drow) * dcol * my z [bottomRow] [rightCol] + |
| 257 |
drow * dcol * my z [topRow] [rightCol]; |
| 258 |
} |
| 259 |
|
| 260 |
double Matrix_getSum (const constMatrix me) { |
| 261 |
return NUMsum (my z.all()); |
| 262 |
} |
| 263 |
|
| 264 |
double Matrix_getNorm (const constMatrix me) { |
| 265 |
return NUMnorm (my z.get(), 2.0); |
| 266 |
} |
| 267 |
|
| 268 |
void Matrix_drawRows (const constMatrix me, Graphics g, |
| 269 |
/* mutable autowindow */ double xmin, /* mutable autowindow */ double xmax, |
| 270 |
/* mutable autowindow */ double ymin, /* mutable autowindow */ double ymax, |
| 271 |
/* mutable autoscaling */ double minimum, /* mutable autoscaling */ double maximum |
| 272 |
) { |
| 273 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 274 |
SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax); |
| 275 |
integer ixmin, ixmax, iymin, iymax; |
| 276 |
(void) Matrix_getWindowSamplesX (me, xmin, xmax, & ixmin, & ixmax); |
| 277 |
(void) Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax); |
| 278 |
if (maximum <= minimum) |
| 279 |
(void) Matrix_getWindowExtrema (me, ixmin, ixmax, iymin, iymax, & minimum, & maximum); |
| 280 |
if (maximum <= minimum) { |
| 281 |
minimum -= 1.0; |
| 282 |
maximum += 1.0; |
| 283 |
} |
| 284 |
if (xmin >= xmax) |
| 285 |
return; |
| 286 |
Graphics_setInner (g); |
| 287 |
for (integer iy = iymin; iy <= iymax; iy ++) { |
| 288 |
Graphics_setWindow (g, xmin, xmax, |
| 289 |
minimum - (iy - iymin) * (maximum - minimum), |
| 290 |
maximum + (iymax - iy) * (maximum - minimum) |
| 291 |
); |
| 292 |
Graphics_function (g, & my z [iy] [0], ixmin, ixmax, |
| 293 |
Matrix_columnToX (me, ixmin), Matrix_columnToX (me, ixmax)); |
| 294 |
} |
| 295 |
Graphics_unsetInner (g); |
| 296 |
if (iymin < iymax) |
| 297 |
Graphics_setWindow (g, xmin, xmax, my y1 + (iymin - 1.5) * my dy, my y1 + (iymax - 0.5) * my dy); |
| 298 |
} |
| 299 |
|
| 300 |
void Matrix_drawOneContour (const constMatrix me, Graphics g, |
| 301 |
/* mutable autowindow */ double xmin, /* mutable autowindow */ double xmax, |
| 302 |
/* mutable autowindow */ double ymin, /* mutable autowindow */ double ymax, |
| 303 |
const double height |
| 304 |
) { |
| 305 |
Function_bidirectionalAutowindow (me, & xmin, & xmax); |
| 306 |
SampledXY_bidirectionalAutowindowY (me, & ymin, & ymax); |
| 307 |
const bool xreversed = xmin > xmax; |
| 308 |
if (xreversed) |
| 309 |
std::swap (xmin, xmax); |
| 310 |
const bool yreversed = ymin > ymax; |
| 311 |
if (yreversed) |
| 312 |
std::swap (ymin, ymax); |
| 313 |
integer ixmin, ixmax, iymin, iymax; |
| 314 |
(void) Matrix_getWindowSamplesX (me, xmin, xmax, & ixmin, & ixmax); |
| 315 |
(void) Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax); |
| 316 |
if (xmin == xmax || ymin == ymax) |
| 317 |
return; |
| 318 |
Graphics_setInner (g); |
| 319 |
Graphics_setWindow (g, |
| 320 |
xreversed ? xmax : xmin, |
| 321 |
xreversed ? xmin : xmax, |
| 322 |
yreversed ? ymax : ymin, |
| 323 |
yreversed ? ymin : ymax |
| 324 |
); |
| 325 |
Graphics_contour (g, |
| 326 |
my z.part (iymin, iymax, ixmin, ixmax), |
| 327 |
Matrix_columnToX (me, ixmin), |
| 328 |
Matrix_columnToX (me, ixmax), |
| 329 |
Matrix_rowToY (me, iymin), |
| 330 |
Matrix_rowToY (me, iymax), |
| 331 |
height |
| 332 |
); |
| 333 |
Graphics_rectangle (g, xmin, xmax, ymin, ymax); |
| 334 |
Graphics_unsetInner (g); |
| 335 |
} |
| 336 |
|
| 337 |
void Matrix_drawContours (const constMatrix me, Graphics g, |
| 338 |
/* mutable autowindow */ double xmin, /* mutable autowindow */ double xmax, |
| 339 |
/* mutable autowindow */ double ymin, /* mutable autowindow */ double ymax, |
| 340 |
/* mutable autoscaling */ double minimum, /* mutable autoscaling */ double maximum |
| 341 |
) { |
| 342 |
Function_bidirectionalAutowindow (me, & xmin, & xmax); |
| 343 |
SampledXY_bidirectionalAutowindowY (me, & ymin, & ymax); |
| 344 |
integer ixmin, ixmax, iymin, iymax; |
| 345 |
(void) Matrix_getWindowSamplesX (me, xmin, xmax, & ixmin, & ixmax); |
| 346 |
(void) Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax); |
| 347 |
if (maximum <= minimum) |
| 348 |
(void) Matrix_getWindowExtrema (me, ixmin, ixmax, iymin, iymax, & minimum, & maximum); |
| 349 |
if (maximum <= minimum) { |
| 350 |
minimum -= 1.0; |
| 351 |
maximum += 1.0; |
| 352 |
} |
| 353 |
double border [1 + 8]; |
| 354 |
for (integer iborder = 1; iborder <= 8; iborder ++) |
| 355 |
border [iborder] = minimum + iborder * (maximum - minimum) / (8 + 1); |
| 356 |
if (xmin == xmax || ymin == ymax) |
| 357 |
return; |
| 358 |
Graphics_setInner (g); |
| 359 |
Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 360 |
Graphics_altitude (g, |
| 361 |
my z.part (iymin, iymax, ixmin, ixmax), |
| 362 |
Matrix_columnToX (me, ixmin), |
| 363 |
Matrix_columnToX (me, ixmax), |
| 364 |
Matrix_rowToY (me, iymin), |
| 365 |
Matrix_rowToY (me, iymax), |
| 366 |
8, |
| 367 |
border |
| 368 |
); |
| 369 |
Graphics_rectangle (g, xmin, xmax, ymin, ymax); |
| 370 |
Graphics_unsetInner (g); |
| 371 |
} |
| 372 |
|
| 373 |
void Matrix_paintContours (const constMatrix me, Graphics g, |
| 374 |
/* mutable autowindow */ double xmin, /* mutable autowindow */ double xmax, |
| 375 |
/* mutable autowindow */ double ymin, /* mutable autowindow */ double ymax, |
| 376 |
/* mutable autoscaling */ double minimum, /* mutable autoscaling */ double maximum |
| 377 |
) { |
| 378 |
double border [1 + 30]; |
| 379 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 380 |
SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax); |
| 381 |
integer ixmin, ixmax, iymin, iymax; |
| 382 |
(void) Matrix_getWindowSamplesX (me, xmin, xmax, & ixmin, & ixmax); |
| 383 |
(void) Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax); |
| 384 |
if (maximum <= minimum) |
| 385 |
(void) Matrix_getWindowExtrema (me, ixmin, ixmax, iymin, iymax, & minimum, & maximum); |
| 386 |
if (maximum <= minimum) { |
| 387 |
minimum -= 1.0; |
| 388 |
maximum += 1.0; |
| 389 |
} |
| 390 |
for (integer iborder = 1; iborder <= 30; iborder ++) |
| 391 |
border [iborder] = minimum + iborder * (maximum - minimum) / (30 + 1); |
| 392 |
if (xmin >= xmax || ymin >= ymax) |
| 393 |
return; |
| 394 |
Graphics_setInner (g); |
| 395 |
Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 396 |
Graphics_grey (g, |
| 397 |
my z.part (iymin, iymax, ixmin, ixmax), |
| 398 |
Matrix_columnToX (me, ixmin), |
| 399 |
Matrix_columnToX (me, ixmax), |
| 400 |
Matrix_rowToY (me, iymin), |
| 401 |
Matrix_rowToY (me, iymax), |
| 402 |
30, |
| 403 |
border |
| 404 |
); |
| 405 |
Graphics_rectangle (g, xmin, xmax, ymin, ymax); |
| 406 |
Graphics_unsetInner (g); |
| 407 |
} |
| 408 |
|
| 409 |
static void cellArrayOrImage (const constMatrix me, Graphics g, |
| 410 |
/* mutable autowindow */ double xmin, /* mutable autowindow */ double xmax, |
| 411 |
/* mutable autowindow */ double ymin, /* mutable autowindow */ double ymax, |
| 412 |
/* mutable autoscaling */ double minimum, /* mutable autoscaling */ double maximum, |
| 413 |
const bool interpolate |
| 414 |
) { |
| 415 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 416 |
SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax); |
| 417 |
integer ixmin, ixmax, iymin, iymax; |
| 418 |
(void) Matrix_getWindowSamplesX (me, xmin - 0.49999 * my dx, xmax + 0.49999 * my dx, |
| 419 |
& ixmin, & ixmax); |
| 420 |
(void) Matrix_getWindowSamplesY (me, ymin - 0.49999 * my dy, ymax + 0.49999 * my dy, |
| 421 |
& iymin, & iymax); |
| 422 |
if (maximum <= minimum) |
| 423 |
(void) Matrix_getWindowExtrema (me, ixmin, ixmax, iymin, iymax, & minimum, & maximum); |
| 424 |
if (maximum <= minimum) { |
| 425 |
minimum -= 1.0; |
| 426 |
maximum += 1.0; |
| 427 |
} |
| 428 |
if (xmin >= xmax || ymin >= ymax) |
| 429 |
return; |
| 430 |
Graphics_setInner (g); |
| 431 |
Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 432 |
if (interpolate) |
| 433 |
Graphics_image (g, |
| 434 |
my z.part (iymin, iymax, ixmin, ixmax), |
| 435 |
Matrix_columnToX (me, ixmin - 0.5), |
| 436 |
Matrix_columnToX (me, ixmax + 0.5), |
| 437 |
Matrix_rowToY (me, iymin - 0.5), |
| 438 |
Matrix_rowToY (me, iymax + 0.5), |
| 439 |
minimum, |
| 440 |
maximum |
| 441 |
); |
| 442 |
else |
| 443 |
Graphics_cellArray (g, |
| 444 |
my z.part (iymin, iymax, ixmin, ixmax), |
| 445 |
Matrix_columnToX (me, ixmin - 0.5), |
| 446 |
Matrix_columnToX (me, ixmax + 0.5), |
| 447 |
Matrix_rowToY (me, iymin - 0.5), |
| 448 |
Matrix_rowToY (me, iymax + 0.5), |
| 449 |
minimum, |
| 450 |
maximum |
| 451 |
); |
| 452 |
Graphics_rectangle (g, xmin, xmax, ymin, ymax); |
| 453 |
Graphics_unsetInner (g); |
| 454 |
} |
| 455 |
|
| 456 |
void Matrix_paintImage (const constMatrix me, Graphics g, |
| 457 |
const double xmin, const double xmax, |
| 458 |
const double ymin, const double ymax, |
| 459 |
const double minimum, const double maximum |
| 460 |
) { |
| 461 |
cellArrayOrImage (me, g, xmin, xmax, ymin, ymax, minimum, maximum, true); |
| 462 |
} |
| 463 |
|
| 464 |
void Matrix_paintCells (const constMatrix me, Graphics g, |
| 465 |
const double xmin, const double xmax, |
| 466 |
const double ymin, const double ymax, |
| 467 |
const double minimum, const double maximum |
| 468 |
) { |
| 469 |
cellArrayOrImage (me, g, xmin, xmax, ymin, ymax, minimum, maximum, false); |
| 470 |
} |
| 471 |
|
| 472 |
void Matrix_paintSurface (const constMatrix me, Graphics g, |
| 473 |
/* mutable autowindow */ double xmin, /* mutable autowindow */ double xmax, |
| 474 |
/* mutable autowindow */ double ymin, /* mutable autowindow */ double ymax, |
| 475 |
/* mutable autoscaling */ double minimum, /* mutable autoscaling */ double maximum, |
| 476 |
const double elevation, const double azimuth |
| 477 |
) { |
| 478 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 479 |
SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax); |
| 480 |
integer ixmin, ixmax, iymin, iymax; |
| 481 |
(void) Matrix_getWindowSamplesX (me, xmin, xmax, & ixmin, & ixmax); |
| 482 |
(void) Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax); |
| 483 |
if (maximum <= minimum) |
| 484 |
(void) Matrix_getWindowExtrema (me, ixmin, ixmax, iymin, iymax, & minimum, & maximum); |
| 485 |
if (maximum <= minimum) { |
| 486 |
minimum -= 1.0; |
| 487 |
maximum += 1.0; |
| 488 |
} |
| 489 |
Graphics_setInner (g); |
| 490 |
Graphics_setWindow (g, -1.0, 1.0, minimum, maximum); |
| 491 |
Graphics_surface (g, |
| 492 |
my z.part (iymin, iymax, ixmin, ixmax), |
| 493 |
Matrix_columnToX (me, ixmin), |
| 494 |
Matrix_columnToX (me, ixmax), |
| 495 |
Matrix_rowToY (me, iymin), |
| 496 |
Matrix_rowToY (me, iymax), |
| 497 |
minimum, |
| 498 |
maximum, |
| 499 |
elevation, |
| 500 |
azimuth |
| 501 |
); |
| 502 |
Graphics_unsetInner (g); |
| 503 |
} |
| 504 |
|
| 505 |
void Matrix_playMovie (const constMatrix me, Graphics g) { |
| 506 |
Melder_require (my ny >= 2, |
| 507 |
me, U": cannot play a movie for a Matrix with less than 2 rows."); |
| 508 |
if (my xmin == my xmax || my ymin == my ymax) |
| 509 |
return; |
| 510 |
autoVEC column = raw_VEC (my ny); |
| 511 |
/* mutable init */ double minimum = 0.0, maximum = 1.0; |
| 512 |
Matrix_getWindowExtrema (me, 1, my nx, 1, my ny, & minimum, & maximum); |
| 513 |
if (minimum == maximum) { |
| 514 |
minimum -= 0.5; |
| 515 |
maximum += 0.5; |
| 516 |
} |
| 517 |
for (integer icol = 1; icol <= my nx; icol ++) { |
| 518 |
column.all() <<= my z.column (icol); |
| 519 |
Graphics_beginMovieFrame (g, & Melder_WHITE); |
| 520 |
Graphics_setWindow (g, my ymin, my ymax, minimum, maximum); |
| 521 |
Graphics_function (g, column.asArgumentToFunctionThatExpectsOneBasedArray(), 1, my ny, my ymin, my ymax); |
| 522 |
Graphics_endMovieFrame (g, 0.03); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
autoMatrix Matrix_readAP (MelderFile file) { |
| 527 |
try { |
| 528 |
autofile f = Melder_fopen (file, "rb"); |
| 529 |
int16 header [256]; |
| 530 |
for (integer i = 0; i < 256; i ++) |
| 531 |
header [i] = bingeti16LE (f); |
| 532 |
const integer numberOfFrames = header [34]; |
| 533 |
const integer numberOfWordsPerFrame = header [35]; |
| 534 |
const double samplingFrequency = double (header [100]); // converting up (from 16 to 54 bits) |
| 535 |
//const integer numberOfSamplesPerFrame = header [110]; |
| 536 |
Melder_casual (U"Sampling frequency ", samplingFrequency); |
| 537 |
autoMatrix me = Matrix_create (0.0, (double) numberOfFrames, numberOfFrames, 1.0, 0.5, |
| 538 |
0.0, (double) numberOfWordsPerFrame, numberOfWordsPerFrame, 1.0, 0.5); |
| 539 |
Melder_casual (U"... Loading ", numberOfFrames, U" frames", |
| 540 |
U" of ", numberOfWordsPerFrame, U" words each ..."); |
| 541 |
for (integer i = 1; i <= my nx; i ++) |
| 542 |
for (integer j = 1; j <= my ny; j ++) |
| 543 |
my z [j] [i] = bingeti16LE (f); // converting up (from 16 to 54 bits) |
| 544 |
|
| 545 |
/* |
| 546 |
Get pitch frequencies. |
| 547 |
*/ |
| 548 |
for (integer i = 1; i <= my nx; i ++) |
| 549 |
if (my z [1] [i] != 0.0) |
| 550 |
my z [1] [i] = - samplingFrequency / my z [1] [i]; |
| 551 |
|
| 552 |
f.close (file); |
| 553 |
return me; |
| 554 |
} catch (MelderError) { |
| 555 |
Melder_throw (U"Matrix object not read from AP file ", file); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
autoMatrix Matrix_appendRows (const constMatrix me, const constMatrix thee, ClassInfo klas) { |
| 560 |
try { |
| 561 |
autoMatrix him = Thing_newFromClass (klas).static_cast_move <structMatrix>(); |
| 562 |
Matrix_init (him.get(), |
| 563 |
std::min (my xmin, thy xmin), |
| 564 |
std::max (my xmax, thy xmax), |
| 565 |
std::max (my nx, thy nx), |
| 566 |
my dx, |
| 567 |
std::min (my x1, thy x1), |
| 568 |
my ymin, my ymax + (thy ymax - thy ymin), my ny + thy ny, my dy, my y1 |
| 569 |
); |
| 570 |
for (integer irow = 1; irow <= my ny; irow ++) |
| 571 |
for (integer icol = 1; icol <= my nx; icol ++) |
| 572 |
his z [irow] [icol] = my z [irow] [icol]; |
| 573 |
for (integer irow = 1; irow <= thy ny; irow ++) |
| 574 |
for (integer icol = 1; icol <= thy nx; icol ++) |
| 575 |
his z [irow + my ny] [icol] = thy z [irow] [icol]; |
| 576 |
return him; |
| 577 |
} catch (MelderError) { |
| 578 |
Melder_throw (me, U" & ", thee, U": rows not appended."); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
autoMatrix Matrix_readFromRawTextFile (MelderFile file) { // BUG: not Unicode-compatible (use of fscanf) |
| 583 |
try { |
| 584 |
autofile f = Melder_fopen (file, "rb"); |
| 585 |
|
| 586 |
/* |
| 587 |
Count columns. |
| 588 |
*/ |
| 589 |
integer numberOfColumns = 0; |
| 590 |
for (;;) { |
| 591 |
int kar = fgetc (f); |
| 592 |
if (kar == EOF || Melder_isVerticalSpace ((char32) kar)) |
| 593 |
break; |
| 594 |
if (Melder_isHorizontalSpace ((char32) kar)) |
| 595 |
continue; |
| 596 |
numberOfColumns ++; |
| 597 |
do { |
| 598 |
kar = fgetc (f); |
| 599 |
} while (kar != EOF && ! Melder_isHorizontalOrVerticalSpace ((char32) kar)); |
| 600 |
if (kar == EOF || Melder_isVerticalSpace ((char32) kar)) |
| 601 |
break; |
| 602 |
} |
| 603 |
if (numberOfColumns == 0) |
| 604 |
Melder_throw (U"File empty"); |
| 605 |
|
| 606 |
/* |
| 607 |
Count elements. |
| 608 |
*/ |
| 609 |
rewind (f); |
| 610 |
integer numberOfElements = 0; |
| 611 |
for (;;) { |
| 612 |
double element; |
| 613 |
if (fscanf (f, "%lf", & element) < 1) |
| 614 |
break; // zero or end-of-file |
| 615 |
numberOfElements ++; |
| 616 |
} |
| 617 |
|
| 618 |
/* |
| 619 |
Check if all columns are complete. |
| 620 |
*/ |
| 621 |
if (numberOfElements == 0 || numberOfElements % numberOfColumns != 0) |
| 622 |
Melder_throw (U"The number of elements (", numberOfElements, U") is not a multiple of the number of columns (", numberOfColumns, U")."); |
| 623 |
|
| 624 |
/* |
| 625 |
Create simple matrix. |
| 626 |
*/ |
| 627 |
integer numberOfRows = numberOfElements / numberOfColumns; |
| 628 |
autoMatrix me = Matrix_createSimple (numberOfRows, numberOfColumns); |
| 629 |
|
| 630 |
/* |
| 631 |
Read elements. |
| 632 |
*/ |
| 633 |
rewind (f); |
| 634 |
for (integer irow = 1; irow <= numberOfRows; irow ++) |
| 635 |
for (integer icol = 1; icol <= numberOfColumns; icol ++) |
| 636 |
fscanf (f, "%lf", & my z [irow] [icol]); |
| 637 |
|
| 638 |
f.close (file); |
| 639 |
return me; |
| 640 |
} catch (MelderError) { |
| 641 |
Melder_throw (U"Matrix object not read from raw text file ", file, U"."); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
static bool isSymmetric (const constMatrix me) { |
| 646 |
for (integer irow = 1; irow <= my ny - 1; irow ++) |
| 647 |
for (integer icol = irow + 1; icol <= my nx; icol ++) |
| 648 |
if (my z [irow] [icol] != my z [icol] [irow]) |
| 649 |
return false; |
| 650 |
return true; |
| 651 |
} |
| 652 |
|
| 653 |
void Matrix_eigen (const constMatrix me, autoMatrix *out_eigenvectors, autoMatrix *out_eigenvalues) { |
| 654 |
try { |
| 655 |
Melder_require (my nx == my ny, |
| 656 |
U"The number of rows (here ", my ny, U") should be equal to the number of columns (here ", my nx, U")."); |
| 657 |
Melder_require (isSymmetric (me), |
| 658 |
U"The matrix should be symmetric."); |
| 659 |
autoEigen eigen = Eigen_createFromSquareMAT (my z.get(), kMAT_TYPE::SYMMETRIC, 0, false); |
| 660 |
autoMatrix eigenvectors = Data_copy (me); |
| 661 |
autoMatrix eigenvalues = Matrix_create (1.0, 1.0, 1, 1.0, 1.0, my ymin, my ymax, my ny, my dy, my y1); |
| 662 |
for (integer i = 1; i <= my nx; i ++) { |
| 663 |
eigenvalues -> z [i] [1] = eigen -> eigenvalues [i]; |
| 664 |
for (integer j = 1; j <= my nx; j ++) |
| 665 |
eigenvectors -> z [i] [j] = eigen -> eigenvectors [j] [i]; |
| 666 |
} |
| 667 |
*out_eigenvectors = eigenvectors.move(); |
| 668 |
*out_eigenvalues = eigenvalues.move(); |
| 669 |
} catch (MelderError) { |
| 670 |
Melder_throw (me, U": eigenstructure not computed."); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
autoMatrix Matrix_power (const constMatrix me, const integer power) { |
| 675 |
try { |
| 676 |
Melder_require (my nx == my ny, |
| 677 |
U"The number of rows (here ", my ny, U") should be equal to the number of columns (here ", my nx, U")."); |
| 678 |
autoMatrix thee = Data_copy (me); |
| 679 |
autoMatrix him = Data_copy (me); |
| 680 |
for (integer ipow = 2; ipow <= power; ipow ++) { |
| 681 |
std::swap (his z, thy z); |
| 682 |
for (integer irow = 1; irow <= my ny; irow ++) { |
| 683 |
for (integer icol = 1; icol <= my nx; icol ++) { |
| 684 |
thy z [irow] [icol] = 0.0; |
| 685 |
for (integer i = 1; i <= my nx; i ++) |
| 686 |
thy z [irow] [icol] += his z [irow] [i] * my z [i] [icol]; |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
return thee; |
| 691 |
} catch (MelderError) { |
| 692 |
Melder_throw (me, U": power not computed."); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
void Matrix_writeToMatrixTextFile (const constMatrix me, MelderFile file) { |
| 697 |
try { |
| 698 |
autofile f = Melder_fopen (file, "w"); |
| 699 |
fprintf (f, "\"ooTextFile\"\n\"Matrix\"\n%s %s %s %s %s\n%s %s %s %s %s\n", |
| 700 |
Melder8_double (my xmin), Melder8_double (my xmax), Melder8_integer (my nx), |
| 701 |
Melder8_double (my dx), Melder8_double (my x1), |
| 702 |
Melder8_double (my ymin), Melder8_double (my ymax), Melder8_integer (my ny), |
| 703 |
Melder8_double (my dy), Melder8_double (my y1)); |
| 704 |
for (integer i = 1; i <= my ny; i ++) { |
| 705 |
for (integer j = 1; j <= my nx; j ++) { |
| 706 |
if (j > 1) |
| 707 |
fprintf (f, " "); |
| 708 |
fprintf (f, "%s", Melder8_double (my z [i] [j])); |
| 709 |
} |
| 710 |
fprintf (f, "\n"); |
| 711 |
} |
| 712 |
f.close (file); |
| 713 |
} catch (MelderError) { |
| 714 |
Melder_throw (me, U": not written to Matrix text file."); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
void Matrix_writeToHeaderlessSpreadsheetFile (const constMatrix me, MelderFile file) { |
| 719 |
try { |
| 720 |
autofile f = Melder_fopen (file, "w"); |
| 721 |
for (integer i = 1; i <= my ny; i ++) { |
| 722 |
for (integer j = 1; j <= my nx; j ++) { |
| 723 |
if (j > 1) |
| 724 |
fprintf (f, "\t"); |
| 725 |
fprintf (f, "%s", Melder8_single (my z [i] [j])); |
| 726 |
} |
| 727 |
fprintf (f, "\n"); |
| 728 |
} |
| 729 |
f.close (file); |
| 730 |
} catch (MelderError) { |
| 731 |
Melder_throw (me, U": not saved as tab-separated file ", file); |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
void Matrix_formula (const mutableMatrix me, |
| 736 |
conststring32 expression, Interpreter interpreter, /* mutable default */ mutableMatrix target) |
| 737 |
{ |
| 738 |
try { |
| 739 |
Formula_compile (interpreter, me, expression, kFormula_EXPRESSION_TYPE_NUMERIC, true); |
| 740 |
Formula_Result result; |
| 741 |
if (! target) |
| 742 |
target = me; |
| 743 |
for (integer irow = 1; irow <= my ny; irow ++) { |
| 744 |
for (integer icol = 1; icol <= my nx; icol ++) { |
| 745 |
Formula_run (irow, icol, & result); |
| 746 |
target -> z [irow] [icol] = result. numericResult; |
| 747 |
} |
| 748 |
} |
| 749 |
} catch (MelderError) { |
| 750 |
Melder_throw (me, U": formula not completed."); |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
void Matrix_formula_part (const mutableMatrix me, double xmin, double xmax, double ymin, double ymax, |
| 755 |
conststring32 expression, Interpreter interpreter, /* mutable default */ mutableMatrix target) |
| 756 |
{ |
| 757 |
try { |
| 758 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 759 |
if (ymax <= ymin) { |
| 760 |
ymin = my ymin; |
| 761 |
ymax = my ymax; |
| 762 |
} |
| 763 |
integer ixmin, ixmax, iymin, iymax; |
| 764 |
(void) Matrix_getWindowSamplesX (me, xmin, xmax, & ixmin, & ixmax); |
| 765 |
(void) Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax); |
| 766 |
Formula_compile (interpreter, me, expression, kFormula_EXPRESSION_TYPE_NUMERIC, true); |
| 767 |
Formula_Result result; |
| 768 |
if (! target) |
| 769 |
target = me; |
| 770 |
for (integer irow = iymin; irow <= iymax; irow ++) { |
| 771 |
for (integer icol = ixmin; icol <= ixmax; icol ++) { |
| 772 |
Formula_run (irow, icol, & result); |
| 773 |
target -> z [irow] [icol] = result. numericResult; |
| 774 |
} |
| 775 |
} |
| 776 |
} catch (MelderError) { |
| 777 |
Melder_throw (me, U": formula not completed."); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
void Matrix_scaleAbsoluteExtremum (const mutableMatrix me, const double scale) { |
| 782 |
double extremum = 0.0; |
| 783 |
for (integer i = 1; i <= my ny; i ++) |
| 784 |
for (integer j = 1; j <= my nx; j ++) |
| 785 |
if (fabs (my z [i] [j]) > extremum) |
| 786 |
extremum = fabs (my z [i] [j]); |
| 787 |
if (extremum != 0.0) { |
| 788 |
double factor = scale / extremum; |
| 789 |
for (integer i = 1; i <= my ny; i ++) |
| 790 |
for (integer j = 1; j <= my nx; j ++) |
| 791 |
my z [i] [j] *= factor; |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
autoMatrix TableOfReal_to_Matrix (const constTableOfReal me) { |
| 796 |
try { |
| 797 |
autoMatrix thee = Matrix_createSimple (my numberOfRows, my numberOfColumns); |
| 798 |
for (integer i = 1; i <= my numberOfRows; i ++) |
| 799 |
for (integer j = 1; j <= my numberOfColumns; j ++) |
| 800 |
thy z [i] [j] = my data [i] [j]; |
| 801 |
return thee; |
| 802 |
} catch (MelderError) { |
| 803 |
Melder_throw (me, U": not converted to Matrix."); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
autoTableOfReal Matrix_to_TableOfReal (const constMatrix me) { |
| 808 |
try { |
| 809 |
autoTableOfReal thee = TableOfReal_create (my ny, my nx); |
| 810 |
for (integer i = 1; i <= my ny; i ++) |
| 811 |
for (integer j = 1; j <= my nx; j ++) |
| 812 |
thy data [i] [j] = my z [i] [j]; |
| 813 |
return thee; |
| 814 |
} catch (MelderError) { |
| 815 |
Melder_throw (me, U": not converted to TableOfReal."); |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
autoMatrix Table_to_Matrix (Table me) { |
| 820 |
try { |
| 821 |
autoMatrix thee = Matrix_createSimple (my rows.size, my numberOfColumns); |
| 822 |
for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 823 |
Table_numericize_a (me, icol); |
| 824 |
for (integer irow = 1; irow <= my rows.size; irow ++) { |
| 825 |
TableRow row = my rows.at [irow]; |
| 826 |
for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 827 |
thy z [irow] [icol] = row -> cells [icol]. number; |
| 828 |
} |
| 829 |
return thee; |
| 830 |
} catch (MelderError) { |
| 831 |
Melder_throw (me, U": not converted to Matrix."); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
/* End of file Matrix.cpp */ |