| 1 |
/* Matrix_extensions.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2023, 2026 David Weenink |
| 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 2 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. See the GNU |
| 13 |
* 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 |
/* |
| 20 |
djmw 20020813 GPL header |
| 21 |
djmw 20040226 Matrix_drawAsSquares: respect the colour environment (fill with current colour). |
| 22 |
djmw 20041110 Matrix_drawDistribution did't draw lowest bin correctly. |
| 23 |
djmw 20050221 Matrix_drawDistribution would draw outside window. |
| 24 |
djmw 20050405 Matrix_drawDistribution crashed if minimum > data minimum5 |
| 25 |
djmw 20080122 float -> double |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "Matrix_extensions.h" |
| 29 |
#include "MAT_numerics.h" |
| 30 |
#include "Eigen.h" |
| 31 |
#include "NUM2.h" |
| 32 |
#include "Permutation.h" |
| 33 |
|
| 34 |
void Matrix_scatterPlot (Matrix me, Graphics g, integer icx, integer icy, |
| 35 |
double xmin, double xmax, double ymin, double ymax, |
| 36 |
double size_mm, conststring32 mark, bool garnish) |
| 37 |
{ |
| 38 |
const integer ix = integer_abs (icx), iy = integer_abs (icy); |
| 39 |
|
| 40 |
if (ix < 1 || ix > my nx || iy < 1 || iy > my nx) |
| 41 |
return; |
| 42 |
if (xmax <= xmin) { |
| 43 |
(void) Matrix_getWindowExtrema (me, ix, ix, 1, my ny, & xmin, & xmax); |
| 44 |
if (xmax <= xmin) { |
| 45 |
xmin -= 0.5; |
| 46 |
xmax += 0.5; |
| 47 |
} |
| 48 |
} |
| 49 |
if (ymax <= ymin) { |
| 50 |
(void) Matrix_getWindowExtrema (me, iy, iy, 1, my ny, & ymin, & ymax); |
| 51 |
if (ymax <= ymin) { |
| 52 |
ymin -= 0.5; |
| 53 |
ymax += 0.5; |
| 54 |
} |
| 55 |
} |
| 56 |
Graphics_setInner (g); |
| 57 |
if (icx < 0) |
| 58 |
std::swap (xmin, xmax); |
| 59 |
if (icy < 0) |
| 60 |
std::swap (ymin, ymax); |
| 61 |
Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 62 |
for (integer i = 1; i <= my ny; i ++) |
| 63 |
if (my z [i] [ix] >= xmin && my z [i] [ix] <= xmax && my z [i] [iy] >= ymin && my z [i] [iy] <= ymax) |
| 64 |
Graphics_mark (g, my z [i] [ix], my z [i] [iy], size_mm, mark); |
| 65 |
|
| 66 |
Graphics_unsetInner (g); |
| 67 |
if (garnish) { |
| 68 |
Graphics_drawInnerBox (g); |
| 69 |
Graphics_marksLeft (g, 2, true, true, false); |
| 70 |
if (ymin * ymax < 0.0) |
| 71 |
Graphics_markLeft (g, 0.0, true, true, true, nullptr); |
| 72 |
Graphics_marksBottom (g, 2, true, true, false); |
| 73 |
if (xmin * xmax < 0.0) |
| 74 |
Graphics_markBottom (g, 0.0, true, true, true, nullptr); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static autoVEC nummat_vectorize (constMATVU const& m, integer rowmin, integer rowmax, integer colmin, integer colmax) { |
| 79 |
const integer numberOfElements = (rowmax - rowmin + 1) * (colmax - colmin + 1); |
| 80 |
autoVEC result = raw_VEC (numberOfElements); |
| 81 |
for (integer irow = rowmin, index = 1; irow <= rowmax; irow ++) |
| 82 |
for (integer icol = colmin; icol <= colmax; icol ++) |
| 83 |
result [index ++] = m [irow] [icol]; |
| 84 |
return result; |
| 85 |
} |
| 86 |
|
| 87 |
void Matrix_drawAsSquares_inside (Matrix me, Graphics g, double xmin, double xmax, double ymin, double ymax, kGraphicsMatrixOrigin origin, double cellAreaScaleFactor, kGraphicsMatrixCellDrawingOrder drawingOrder) { |
| 88 |
integer colmin, colmax, rowmin, rowmax; |
| 89 |
const integer numberOfColumns = Matrix_getWindowSamplesX (me, xmin, xmax, & colmin, & colmax); |
| 90 |
const integer numberOfRows = Matrix_getWindowSamplesY (me, ymin, ymax, & rowmin, & rowmax); |
| 91 |
|
| 92 |
const integer numberOfCells = numberOfRows * numberOfColumns; |
| 93 |
autoPermutation p = Permutation_create (numberOfCells, true); |
| 94 |
|
| 95 |
if (drawingOrder == kGraphicsMatrixCellDrawingOrder::ROWS) { |
| 96 |
// identity permutation |
| 97 |
} else if (drawingOrder == kGraphicsMatrixCellDrawingOrder::RANDOM) { |
| 98 |
Permutation_permuteRandomly_inplace (p.get(), 1, numberOfCells); |
| 99 |
} else if (drawingOrder == kGraphicsMatrixCellDrawingOrder::INCREASING_VALUES || drawingOrder == kGraphicsMatrixCellDrawingOrder::DECREASING_VALUES) { |
| 100 |
autoVEC v = nummat_vectorize (my z.get(), rowmin, rowmax, colmin, colmax); |
| 101 |
NUMsortTogether (v.get(), p -> p.get()); |
| 102 |
if (drawingOrder == kGraphicsMatrixCellDrawingOrder::DECREASING_VALUES) |
| 103 |
Permutation_reverse_inline (p.get(), 1, numberOfCells); |
| 104 |
} else if (drawingOrder == kGraphicsMatrixCellDrawingOrder::COLUMNS) { |
| 105 |
Permutation_tableJump_inline (p.get(), numberOfColumns, 1); |
| 106 |
} |
| 107 |
|
| 108 |
const double extremum = NUMextremum_u (my z.get()); |
| 109 |
if (isundef (extremum)) |
| 110 |
return; |
| 111 |
const MelderColour colour = Graphics_inqColour (g); |
| 112 |
const double scaleFactor = sqrt (cellAreaScaleFactor); |
| 113 |
for (integer i = 1; i <= numberOfCells; i++) { |
| 114 |
const integer index = Permutation_getValueAtIndex (p.get(), i); |
| 115 |
const integer irow = rowmin + (index - 1) / numberOfColumns; |
| 116 |
const integer icol = colmin + (index - 1) % numberOfColumns; |
| 117 |
const double z = my z [irow] [icol]; |
| 118 |
const double xfraction = sqrt (fabs (z) / extremum), yfraction = xfraction; |
| 119 |
const double halfCellWidth = xfraction * 0.5 * my dx * scaleFactor; |
| 120 |
const double halfCellHeight = yfraction * 0.5 * my dy * scaleFactor; |
| 121 |
double cellLeft, cellTop; |
| 122 |
if (origin == kGraphicsMatrixOrigin::TOP_LEFT) { |
| 123 |
cellLeft = Matrix_columnToX (me, icol) - halfCellWidth; |
| 124 |
cellTop = Matrix_rowToY (me, rowmax - irow + rowmin) + halfCellHeight; |
| 125 |
} else if (origin == kGraphicsMatrixOrigin::TOP_RIGHT) { |
| 126 |
cellLeft = Matrix_columnToX (me, colmax - icol + colmin) - halfCellWidth; |
| 127 |
cellTop = Matrix_rowToY (me, rowmax - irow + rowmin) + halfCellHeight; |
| 128 |
} else if (origin == kGraphicsMatrixOrigin::BOTTOM_LEFT) { |
| 129 |
cellLeft = Matrix_columnToX (me, icol) - halfCellWidth; |
| 130 |
cellTop = Matrix_rowToY (me, irow) + halfCellHeight; |
| 131 |
} else { // origin == kGraphicsMatrixOrigin::BottomRight |
| 132 |
cellLeft = Matrix_columnToX (me, colmax - icol + colmin) - halfCellWidth; |
| 133 |
cellTop = Matrix_rowToY (me, irow) + halfCellHeight; |
| 134 |
} |
| 135 |
double cellRight = cellLeft + 2.0 * halfCellWidth; |
| 136 |
double cellBottom = cellTop - 2.0 * halfCellHeight; |
| 137 |
cellLeft = std::max (cellLeft, xmin); |
| 138 |
cellRight = std::min (cellRight, xmax); |
| 139 |
cellTop = std::min (cellTop, ymax); |
| 140 |
cellBottom = std::max (cellBottom, ymin); |
| 141 |
if (z > 0.0) |
| 142 |
Graphics_setColour (g, Melder_WHITE); |
| 143 |
Graphics_fillRectangle (g, cellRight, cellLeft, cellBottom, cellTop); |
| 144 |
Graphics_setColour (g, colour); |
| 145 |
Graphics_rectangle (g, cellRight, cellLeft, cellBottom, cellTop); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void Matrix_drawAsSquares (Matrix me, Graphics g, double xmin, double xmax, double ymin, double ymax, bool garnish) { |
| 150 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 151 |
SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax); |
| 152 |
if (xmin >= xmax || ymin >= ymax) |
| 153 |
return; |
| 154 |
Graphics_setInner (g); |
| 155 |
Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 156 |
|
| 157 |
Matrix_drawAsSquares_inside (me, g, xmin, xmax, ymin, ymax, kGraphicsMatrixOrigin::BOTTOM_LEFT, 0.95 * 0.95, kGraphicsMatrixCellDrawingOrder::ROWS); |
| 158 |
|
| 159 |
Graphics_setGrey (g, 0.0); |
| 160 |
Graphics_unsetInner (g); |
| 161 |
if (garnish) { |
| 162 |
Graphics_drawInnerBox (g); |
| 163 |
Graphics_marksLeft (g, 2, true, true, false); |
| 164 |
if (ymin * ymax < 0.0) |
| 165 |
Graphics_markLeft (g, 0.0, true, true, true, nullptr); |
| 166 |
Graphics_marksBottom (g, 2, true, true, false); |
| 167 |
if (xmin * xmax < 0.0) |
| 168 |
Graphics_markBottom (g, 0.0, true, true, true, nullptr); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void Matrix_scale (Matrix me, int choice) { |
| 173 |
double min, max, extremum; |
| 174 |
integer nZero = 0; |
| 175 |
Melder_require (choice > 0 && choice < 4, |
| 176 |
U"Matrix_scale: choice should be > 0 && <= 3."); |
| 177 |
if (choice == 2) { // by row |
| 178 |
for (integer irow = 1; irow <= my ny; irow ++) { |
| 179 |
Matrix_getWindowExtrema (me, 1, my nx, irow, irow, & min, & max); |
| 180 |
extremum = std::max (fabs (max), fabs (min)); |
| 181 |
if (extremum == 0.0) |
| 182 |
nZero ++; |
| 183 |
else |
| 184 |
my z.row (irow) /= extremum; |
| 185 |
} |
| 186 |
} else if (choice == 3) { // by col |
| 187 |
for (integer icol = 1; icol <= my nx; icol ++) { |
| 188 |
Matrix_getWindowExtrema (me, icol, icol, 1, my ny, & min, & max); |
| 189 |
extremum = std::max (fabs (max), fabs (min)); |
| 190 |
if (extremum == 0.0) |
| 191 |
nZero ++; |
| 192 |
else |
| 193 |
my z.column (icol) /= extremum; |
| 194 |
} |
| 195 |
} else if (choice == 1) { // overall |
| 196 |
Matrix_getWindowExtrema (me, 1, my nx, 1, my ny, & min, & max); |
| 197 |
extremum = std::max (fabs (max), fabs (min)); |
| 198 |
if (extremum == 0.0) |
| 199 |
nZero ++; |
| 200 |
else |
| 201 |
my z.get() /= extremum; |
| 202 |
} |
| 203 |
if (nZero) |
| 204 |
Melder_warning (U"Matrix_scale: extremum == 0, (part of) matrix unscaled."); |
| 205 |
} |
| 206 |
|
| 207 |
autoMatrix Matrix_transpose (Matrix me) { |
| 208 |
try { |
| 209 |
autoMatrix thee = Matrix_create (my ymin, my ymax, my ny, my dy, my y1, my xmin, my xmax, my nx, my dx, my x1); |
| 210 |
thy z.all() <<= my z.transpose(); |
| 211 |
return thee; |
| 212 |
} catch (MelderError) { |
| 213 |
Melder_throw (me, U": not transposed."); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
void Matrix_drawDistribution (Matrix me, Graphics g, double xmin, double xmax, double ymin, double ymax, double minimum, double maximum, |
| 218 |
integer nBins, double freqMin, double freqMax, bool cumulative, bool garnish) |
| 219 |
{ |
| 220 |
if (nBins <= 0) |
| 221 |
return; |
| 222 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 223 |
SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax); |
| 224 |
integer ixmin, ixmax, iymin, iymax; |
| 225 |
if ((Matrix_getWindowSamplesX (me, xmin, xmax, & ixmin, & ixmax) == 0) || |
| 226 |
(Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax) == 0)) |
| 227 |
return; |
| 228 |
if (maximum <= minimum) |
| 229 |
Matrix_getWindowExtrema (me, ixmin, ixmax, iymin, iymax, & minimum, & maximum); |
| 230 |
if (maximum <= minimum) { |
| 231 |
minimum -= 1.0; |
| 232 |
maximum += 1.0; |
| 233 |
} |
| 234 |
/* |
| 235 |
Count the numbers per bin and the total |
| 236 |
*/ |
| 237 |
if (nBins < 1) |
| 238 |
nBins = 10; |
| 239 |
autoVEC freq = zero_VEC (nBins); |
| 240 |
const double binWidth = (maximum - minimum) / nBins; |
| 241 |
integer nxy = 0; |
| 242 |
for (integer i = iymin; i <= iymax; i ++) { |
| 243 |
for (integer j = ixmin; j <= ixmax; j ++) { |
| 244 |
const integer bin = 1 + Melder_ifloor ((my z [i] [j] - minimum) / binWidth); |
| 245 |
if (bin <= nBins && bin > 0) { |
| 246 |
freq [bin] ++; |
| 247 |
nxy ++; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if (freqMax <= freqMin) { |
| 253 |
if (cumulative) { |
| 254 |
freqMin = 0.0; |
| 255 |
freqMax = 1.0; |
| 256 |
} else { |
| 257 |
NUMextrema_u (freq.get(), & freqMin, & freqMax); |
| 258 |
if (isundef (freqMin) || isundef (freqMax)) |
| 259 |
return; |
| 260 |
if (freqMax <= freqMin) { |
| 261 |
freqMin = ( freqMin > 1.0 ? freqMin - 1.0 : 0.0 ); |
| 262 |
freqMax += 1.0; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
Graphics_setInner (g); |
| 268 |
Graphics_setWindow (g, minimum, maximum, freqMin, freqMax); |
| 269 |
double fi = 0.0; |
| 270 |
for (integer i = 1; i <= nBins; i ++) { |
| 271 |
fi = ( cumulative ? fi + freq [i] / nxy : freq [i] ); |
| 272 |
const double ftmp = std::min (fi, freqMax); |
| 273 |
if (ftmp > freqMin) |
| 274 |
Graphics_rectangle (g, minimum + (i - 1) * binWidth, minimum + i * binWidth, freqMin, ftmp); |
| 275 |
} |
| 276 |
Graphics_unsetInner (g); |
| 277 |
if (garnish) { |
| 278 |
Graphics_drawInnerBox (g); |
| 279 |
Graphics_marksBottom (g, 2, true, true, false); |
| 280 |
Graphics_marksLeft (g, 2, true, true, false); |
| 281 |
if (! cumulative) |
| 282 |
Graphics_textLeft (g, true, U"Number/bin"); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
void Matrix_drawSliceY (Matrix me, Graphics g, double x, double ymin, double ymax, double min, double max) { |
| 287 |
|
| 288 |
if (x < my xmin || x > my xmax) |
| 289 |
return; |
| 290 |
const integer ix = Matrix_xToNearestColumn (me, x); |
| 291 |
|
| 292 |
if (ymax <= ymin) { |
| 293 |
ymin = my ymin; |
| 294 |
ymax = my ymax; |
| 295 |
} |
| 296 |
|
| 297 |
integer iymin, iymax; |
| 298 |
const integer ny = Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax); |
| 299 |
if (ny < 1) |
| 300 |
return; |
| 301 |
|
| 302 |
if (max <= min) |
| 303 |
Matrix_getWindowExtrema (me, ix, ix, iymin, iymax, & min, & max); |
| 304 |
if (max <= min) { |
| 305 |
min -= 0.5; |
| 306 |
max += 0.5; |
| 307 |
} |
| 308 |
const integer ysize = iymax - iymin + 1; |
| 309 |
autoVEC y = raw_VEC (ysize); |
| 310 |
|
| 311 |
Graphics_setWindow (g, ymin, ymax, min, max); |
| 312 |
Graphics_setInner (g); |
| 313 |
|
| 314 |
for (integer i = iymin; i <= iymax; i ++) |
| 315 |
y [i - iymin + 1] = my z [i] [ix]; |
| 316 |
Graphics_function (g, y.asArgumentToFunctionThatExpectsOneBasedArray(), 1, ysize, Matrix_rowToY (me, iymin), Matrix_rowToY (me, iymax)); |
| 317 |
Graphics_unsetInner (g); |
| 318 |
} |
| 319 |
|
| 320 |
autoMatrix Matrix_solveEquation (Matrix me, double tolerance) { |
| 321 |
try { |
| 322 |
const integer nr = my ny, nc = my nx - 1; |
| 323 |
Melder_require (nc > 0, |
| 324 |
U"There should be at least 2 columns in the matrix."); |
| 325 |
|
| 326 |
if (nr < nc) |
| 327 |
Melder_warning (U"Solution is not unique (there are fewer equations than unknowns)."); |
| 328 |
|
| 329 |
autoMAT u = raw_MAT (nr, nc); |
| 330 |
autoVEC b = raw_VEC (nr); |
| 331 |
autoMatrix thee = Matrix_create (0.5, 0.5 + nc, nc, 1, 1, 0.5, 1.5, 1, 1, 1); |
| 332 |
|
| 333 |
u.all() <<= my z.part (1, nr, 1, nc); |
| 334 |
b.all() <<= my z.column (my nx); |
| 335 |
|
| 336 |
autoVEC x = solve_VEC (u.get(), b.get(), tolerance); |
| 337 |
thy z.row (1) <<= x.all(); |
| 338 |
|
| 339 |
return thee; |
| 340 |
} catch (MelderError) { |
| 341 |
Melder_throw (me, U": matrix equation not solved."); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
autoMatrix Matrix_solveEquation (Matrix me, Matrix thee, double tolerance) { |
| 346 |
try { |
| 347 |
Melder_require (my ny == thy ny, |
| 348 |
U"The number of rows must be equal."); |
| 349 |
|
| 350 |
if (my ny < my nx) |
| 351 |
Melder_warning (U"Solution is not unique (there are fewer equations than unknowns)."); |
| 352 |
|
| 353 |
autoMatrix him = Matrix_create (0.5, 0.5 + thy nx, thy nx, 1, 1, 0.5, 0.5 + my nx, my nx, 1, 1); |
| 354 |
autoSVD svd = SVD_createFromGeneralMatrix (my z.get()); |
| 355 |
SVD_zeroSmallSingularValues (svd.get(), tolerance); |
| 356 |
SVD_solve_preallocated (svd.get(), thy z.get(), his z.get()); |
| 357 |
return him; |
| 358 |
} catch (MelderError) { |
| 359 |
Melder_throw (me, U": matrix equation not solved."); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
double Matrix_getMean (Matrix me, double xmin, double xmax, double ymin, double ymax) { |
| 364 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 365 |
SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax); |
| 366 |
integer ixmin, ixmax, iymin, iymax; |
| 367 |
if ((Matrix_getWindowSamplesX (me, xmin, xmax, & ixmin, & ixmax) == 0) || |
| 368 |
(Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax) == 0)) { |
| 369 |
return undefined; |
| 370 |
} |
| 371 |
double mean = NUMmean (my z.part (iymin, iymax, ixmin, ixmax)); |
| 372 |
return mean; |
| 373 |
} |
| 374 |
|
| 375 |
double Matrix_getStandardDeviation (Matrix me, double xmin, double xmax, double ymin, double ymax) { |
| 376 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 377 |
SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax); |
| 378 |
integer ixmin, ixmax, iymin, iymax; |
| 379 |
if ((Matrix_getWindowSamplesX (me, xmin, xmax, & ixmin, & ixmax) == 0) || |
| 380 |
(Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax) == 0)) |
| 381 |
return undefined; |
| 382 |
|
| 383 |
integer nx = ixmax - ixmin + 1, ny = iymax - iymin + 1; |
| 384 |
if (nx == 1 && ny == 1) |
| 385 |
return undefined; |
| 386 |
|
| 387 |
MelderGaussianStats stats = NUMmeanStdev (my z.part (iymin, iymax, ixmin, ixmax)); |
| 388 |
|
| 389 |
return stats.stdev; |
| 390 |
} |
| 391 |
|
| 392 |
autoDaata IDXFormattedMatrixFileRecognizer (integer numberOfBytesRead, const char *header, MelderFile file) { |
| 393 |
unsigned int numberOfDimensions, type, pos = 4; |
| 394 |
/* |
| 395 |
9: minimum size is 4 bytes (magic number) + 4 bytes for 1 dimension + 1 value of 1 byte |
| 396 |
*/ |
| 397 |
if (numberOfBytesRead < 9 || header [0] != 0 || header [1] != 0 || (type = header [2]) < 8 || |
| 398 |
numberOfBytesRead < 4 + (numberOfDimensions = header [3]) * 4) // each dimension occupies 4 bytes |
| 399 |
return autoDaata (); |
| 400 |
|
| 401 |
trace (U"dimensions = ", numberOfDimensions, U" type = ", type); |
| 402 |
/* |
| 403 |
Check if the file size (bytes) equals the number of data cells in the matrix times the size of each cell (bytes) plus three |
| 404 |
offset of the data (4 + numberOfDimensions * 4) |
| 405 |
*/ |
| 406 |
double numberOfCells = 1.0; // double because sizes of the dimensions could turn out to be very large if not an IDX format file |
| 407 |
for (integer i = 1; i <= numberOfDimensions; i ++, pos += 4) { |
| 408 |
const unsigned char b1 = header [pos], b2 = header [pos + 1], b3 = header [pos + 2], b4 = header [pos + 3]; |
| 409 |
const integer size = ((uint32) b1 << 24) + ((uint32) b2 << 16) + ((uint32) b3 << 8) + (uint32) b4; |
| 410 |
trace (U"size = ", size, U" ", b1, U" ", b2, U" ", b3, U" ", b4); |
| 411 |
numberOfCells *= size; |
| 412 |
} |
| 413 |
trace (U"Number of cells =", numberOfCells); |
| 414 |
/* |
| 415 |
Check how many bytes each cell needs |
| 416 |
*/ |
| 417 |
const integer cellSizeBytes = ( (type == 0x08 || type == 0x09) ? 1 : ( type == 0x0B ? 2 : ( (type == 0x0C || type == 0x0D) ? 4 : ( type == 0x0E ? 8 : 0 ) ) ) ); |
| 418 |
if (cellSizeBytes == 0) |
| 419 |
return autoDaata (); |
| 420 |
|
| 421 |
trace (U"Cell size =", cellSizeBytes); |
| 422 |
const double numberOfBytes = numberOfCells * cellSizeBytes + 4 + numberOfDimensions * 4; |
| 423 |
trace (U"Number of bytes =", numberOfBytes); |
| 424 |
const integer numberOfBytesInFile = MelderFile_length (file); |
| 425 |
trace (U"File size = ", numberOfBytesInFile); |
| 426 |
if (numberOfBytes > numberOfBytesInFile || (integer) numberOfBytes < numberOfBytesInFile) // may occur if it is not an IDX file |
| 427 |
return autoDaata (); |
| 428 |
autoMatrix thee = Matrix_readFromIDXFormatFile (file); |
| 429 |
return thee.move(); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
autoMatrix Matrix_readFromIDXFormatFile (MelderFile file) { |
| 434 |
/* |
| 435 |
From: http://yann.lecun.com/exdb/mnist/ |
| 436 |
|
| 437 |
The IDX file format is a simple format for multidimensional arrays of various numerical types. |
| 438 |
|
| 439 |
The basic format is |
| 440 |
|
| 441 |
magic number |
| 442 |
size in dimension 0 |
| 443 |
size in dimension 1 |
| 444 |
size in dimension 2 |
| 445 |
.... |
| 446 |
size in dimension N |
| 447 |
data |
| 448 |
|
| 449 |
The magic number is a four-byte integer (most significant byte first). The first 2 bytes are always 0. |
| 450 |
|
| 451 |
The third byte encodes the type of the data: |
| 452 |
0x08: unsigned byte |
| 453 |
0x09: signed byte |
| 454 |
0x0B: short (2 bytes) |
| 455 |
0x0C: int (4 bytes) |
| 456 |
0x0D: float (4 bytes) |
| 457 |
0x0E: double (8 bytes) |
| 458 |
|
| 459 |
The 4-th byte encodes the number of dimensions (indices) of the array: 1 for vectors, 2 for matrices.... |
| 460 |
|
| 461 |
The numbers of elements in each dimension (for a matrix: number of rows and number of columns) |
| 462 |
are 4-byte integers (MSB first, big-endian, as in most non-Intel processors). |
| 463 |
|
| 464 |
The data is stored like in a C array, i.e. the index in the last dimension changes the fastest. |
| 465 |
|
| 466 |
*/ |
| 467 |
try { |
| 468 |
autofile f = Melder_fopen (file, "r"); |
| 469 |
const unsigned int b1 = bingetu8 (f); // 0 |
| 470 |
const unsigned int b2 = bingetu8 (f); // 0 |
| 471 |
|
| 472 |
Melder_require (b1 == 0 && b2 == 0, |
| 473 |
U"Starting two bytes should be zero."); |
| 474 |
|
| 475 |
const unsigned int b3 = bingetu8 (f); // data type |
| 476 |
unsigned int b4 = bingetu8 (f); // number of dimensions |
| 477 |
integer ncols = bingeti32 (f), nrows = 1; // ok if vector |
| 478 |
if (b4 > 1) { |
| 479 |
nrows = ncols; |
| 480 |
ncols = bingeti32 (f); |
| 481 |
} |
| 482 |
while (b4 > 2) { // accumulate all other dimensions in the columns |
| 483 |
const integer n2 = bingeti32 (f); |
| 484 |
ncols *= n2; // put the matrix in one row |
| 485 |
-- b4; |
| 486 |
} |
| 487 |
autoMatrix me = Matrix_create (0.0, ncols, ncols, 1, 0.5, 0, nrows, nrows, 1.0, 0.5); |
| 488 |
if (b3 == 0x08) { // 8 bits unsigned |
| 489 |
for (integer irow = 1; irow <= nrows; irow ++) { |
| 490 |
for (integer icol = 1; icol <= ncols; icol ++) { |
| 491 |
my z [irow] [icol] = bingetu8 (f); |
| 492 |
} |
| 493 |
} |
| 494 |
} else if (b3 == 0x09) { // 8 bits signed |
| 495 |
for (integer irow = 1; irow <= nrows; irow ++) { |
| 496 |
for (integer icol = 1; icol <= ncols; icol ++) { |
| 497 |
my z [irow] [icol] = bingeti8 (f); |
| 498 |
} |
| 499 |
} |
| 500 |
} else if (b3 == 0x0B) { // 16 bits signed |
| 501 |
for (integer irow = 1; irow <= nrows; irow ++) { |
| 502 |
for (integer icol = 1; icol <= ncols; icol ++) { |
| 503 |
my z [irow] [icol] = bingeti16 (f); |
| 504 |
} |
| 505 |
} |
| 506 |
} else if (b3 == 0x0C) { // 32 bits signed |
| 507 |
for (integer irow = 1; irow <= nrows; irow ++) { |
| 508 |
for (integer icol = 1; icol <= ncols; icol ++) { |
| 509 |
my z [irow] [icol] = bingeti32 (f); |
| 510 |
} |
| 511 |
} |
| 512 |
} else if (b3 == 0x0D) { // 32-bits IEEE floating point |
| 513 |
for (integer irow = 1; irow <= nrows; irow ++) { |
| 514 |
for (integer icol = 1; icol <= ncols; icol ++) { |
| 515 |
my z [irow] [icol] = bingetr32 (f); |
| 516 |
} |
| 517 |
} |
| 518 |
} else if (b3 == 0x0E) { // 64-bits IEEE floating point |
| 519 |
for (integer irow = 1; irow <= nrows; irow ++) { |
| 520 |
for (integer icol = 1; icol <= ncols; icol ++) { |
| 521 |
my z [irow] [icol] = bingetr64 (f); |
| 522 |
} |
| 523 |
} |
| 524 |
} else { |
| 525 |
Melder_throw (U"Not a valid data type."); |
| 526 |
} |
| 527 |
f.close (file); |
| 528 |
return me; |
| 529 |
} catch (MelderError) { |
| 530 |
Melder_throw (U"Cannot read from IDX format file ", MelderFile_messageName (file), U"."); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
autoEigen Matrix_to_Eigen (Matrix me) { |
| 535 |
try { |
| 536 |
Melder_require (my nx == my ny, |
| 537 |
U"The Matrix should be square."); |
| 538 |
Melder_require (NUMisSymmetric (my z.get()), |
| 539 |
U"The Matrix should be symmetric."); |
| 540 |
autoEigen thee = Eigen_createFromSquareMAT (my z.get(), kMAT_TYPE::SYMMETRIC, my nx, false); |
| 541 |
return thee; |
| 542 |
} catch (MelderError) { |
| 543 |
Melder_throw (U"Cannot create Eigen from Matrix."); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
autoEigen Matrix_to_Eigen_special (Matrix me, kMAT_TYPE matType, integer numberOfEigenvalues, bool sortAcending) { |
| 548 |
Melder_require (my nx == my ny, |
| 549 |
U"The number of rows and the number of columns should be equal."); |
| 550 |
if (numberOfEigenvalues < 1 || numberOfEigenvalues > my ny) |
| 551 |
numberOfEigenvalues = my ny; |
| 552 |
autoEigen eigen = Eigen_createFromSquareMAT (my z.get(), matType, numberOfEigenvalues, sortAcending); |
| 553 |
return eigen; |
| 554 |
} |
| 555 |
|
| 556 |
void Matrix_Eigen_complex (Matrix me, autoMatrix *out_eigenvectors, autoMatrix *out_eigenvalues) { |
| 557 |
try { |
| 558 |
Melder_require (my nx == my ny, |
| 559 |
U"The Matrix should be square."); |
| 560 |
Melder_require ((out_eigenvectors || out_eigenvalues), |
| 561 |
U"You should want either eigenvalues or eigenvectors or both to be calculated."); |
| 562 |
|
| 563 |
autoEigen eigen = Eigen_createFromSquareMAT (my z.get(), kMAT_TYPE::GENERAL, my nx, false); |
| 564 |
|
| 565 |
if (out_eigenvectors) { |
| 566 |
autoMatrix eigenvectors = Matrix_createSimple (my ny, 2 * my ny); |
| 567 |
for (integer ivec = 1; ivec <= my ny; ivec ++) { |
| 568 |
for (long irow = 1; irow <= my ny; irow ++) { |
| 569 |
eigenvectors -> z [irow] [2 * ivec - 1] = eigen -> eigenvectors [irow] [ivec]; |
| 570 |
eigenvectors -> z [irow] [2 * ivec ] = eigen -> eigenvectors_im [irow] [ivec]; |
| 571 |
} |
| 572 |
} |
| 573 |
*out_eigenvectors = eigenvectors.move(); |
| 574 |
} |
| 575 |
if (out_eigenvalues) { |
| 576 |
autoMatrix eigenvalues = Matrix_createSimple (my ny, 2); |
| 577 |
for (long i = 1; i <= my ny; i ++) { |
| 578 |
eigenvalues -> z [i] [1] = eigen -> eigenvalues [i]; |
| 579 |
eigenvalues -> z [i] [2] = eigen -> eigenvalues_im [i]; |
| 580 |
} |
| 581 |
*out_eigenvalues = eigenvalues.move(); |
| 582 |
} |
| 583 |
} catch (MelderError) { |
| 584 |
Melder_throw (U"Cannot create Eigenvalues from Matrix."); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
autoMatrix SVD_to_Matrix (SVD me, integer from, integer to) { |
| 589 |
try { |
| 590 |
autoMAT synthesis = SVD_synthesize (me, from, to); |
| 591 |
autoMatrix thee = Matrix_create (0.5, 0.5 + synthesis.ncol, synthesis.ncol, 1.0, 1.0, |
| 592 |
0.5, 0.5 + synthesis.nrow, synthesis.nrow, 1.0, 1.0); |
| 593 |
thy z.all() <<= synthesis.all(); |
| 594 |
return thee; |
| 595 |
} catch (MelderError) { |
| 596 |
Melder_throw (me, U": no Matrix synthesized."); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
/* End of file Matrix_extensions.cpp */ |