Matrix.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #ifndef _Matrix_h_
  2. #define _Matrix_h_
  3. /* Matrix.h
  4. *
  5. * Copyright (C) 1992-2011,2013,2014,2015,2016 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. #include "SampledXY.h"
  21. #include "Graphics.h"
  22. #include "../stat/Table.h"
  23. #include "../stat/TableOfReal.h"
  24. Thing_declare (Interpreter);
  25. #include "Matrix_def.h"
  26. #if 1
  27. template <typename T, typename... Args>
  28. autoSomeThing <T> Thing_create (Args ... args) {
  29. autoSomeThing <T> me (new T); // this `new` has to set classInfo
  30. my T::init (args...);
  31. return me;
  32. }
  33. template <typename... ArgumentTypes>
  34. autoMatrix CreateMatrix (ArgumentTypes... arguments) {
  35. try {
  36. autoMatrix me = Thing_new (Matrix);
  37. Matrix_init (me.get(), arguments...);
  38. return me;
  39. } catch (MelderError) {
  40. Melder_throw (U"Matrix object not created.");
  41. }
  42. }
  43. #endif
  44. void Matrix_init
  45. (Matrix me, double xmin, double xmax, integer nx, double dx, double x1,
  46. double ymin, double ymax, integer ny, double dy, double y1);
  47. autoMatrix Matrix_create
  48. (double xmin, double xmax, integer nx, double dx, double x1,
  49. double ymin, double ymax, integer ny, double dy, double y1);
  50. /*
  51. Function:
  52. return a new empty Matrix.
  53. Preconditions:
  54. xmin <= xmax;
  55. ymin <= ymax;
  56. nx >= 1;
  57. ny >= 1;
  58. dx > 0.0;
  59. dy > 0.0;
  60. Postconditions:
  61. result -> xmin == xmin;
  62. result -> xmax == xmax;
  63. result -> ymin == ymin;
  64. result -> ymax == ymax;
  65. result -> nx == nx;
  66. result -> ny == ny;
  67. result -> dx == dx;
  68. result -> dy == dy;
  69. result -> x1 == x1;
  70. result -> y1 == y1;
  71. result -> z [1..ny] [1..nx] == 0.0;
  72. */
  73. autoMatrix Matrix_createSimple (integer numberOfRows, integer numberOfColumns);
  74. /*
  75. Function:
  76. return a new empty Matrix.
  77. Preconditions:
  78. numberOfRows >= 1;
  79. numberOfColumns >= 1;
  80. Postconditions:
  81. result -> xmin == 0.5;
  82. result -> xmax == numberOfColumns + 0.5;
  83. result -> ymin == 0.5;
  84. result -> ymax == numberOfRows + 0.5;
  85. result -> nx == numberOfColumns;
  86. result -> ny == numberOfRows;
  87. result -> dx == 1.0;
  88. result -> dy == 1.0;
  89. result -> x1 == 1.0;
  90. result -> y1 == 1.0;
  91. result -> z [1..ny] [1..nx] == 0.0;
  92. */
  93. /* An implemented method
  94. void structMatrix :: v_writeText (MelderFile file);
  95. writes a Matrix as text to `file`.
  96. A sample of the format follows:
  97. 0 5000 1 2 ! xmin xmax ymin ymax
  98. 8193 2 ! nx ny
  99. 0.61035156 1 ! dx dy
  100. 0 1 ! x1 y1
  101. 5.1e-8 ! 0 1
  102. 5 ! 0.61035156 1
  103. -3.556473 ! 1.2207031 1
  104. ...
  105. 90000000 ! 4998.7793 2
  106. 3.1415927 ! 4999.3896 2
  107. -5.735668e35 ! 5000 2
  108. The data lines (all lines after the fourth) contain: my z [iy, ix], x, y.
  109. things written after the "!" are mere comments:
  110. you cannot use them to change the meaning or order of the data.
  111. */
  112. integer Matrix_getWindowSamplesX (Matrix me, double xmin, double xmax, integer *ixmin, integer *ixmax);
  113. /*
  114. Function:
  115. return the number of samples with x values in [xmin, xmax].
  116. Put the first of these samples in ixmin.
  117. Put the last of these samples in ixmax.
  118. Postconditions:
  119. *ixmin >= 1;
  120. *ixmax <= my nx;
  121. if (result != 0) *ixmin <= *ixmax; else *ixmin > *ixmax;
  122. if (result != 0) result == *ixmax - *ixmin + 1;
  123. */
  124. double Matrix_getValueAtXY (Matrix me, double x, double y);
  125. /*
  126. Linear interpolation between matrix points,
  127. constant extrapolation in cells on the edge,
  128. undefined outside the union of the unit squares around the points.
  129. */
  130. double Matrix_getSum (Matrix me);
  131. double Matrix_getNorm (Matrix me);
  132. double Matrix_columnToX (Matrix me, double column); // return my x1 + (column - 1) * my dx
  133. double Matrix_rowToY (Matrix me, double row); // return my y1 + (row - 1) * my dy
  134. double Matrix_xToColumn (Matrix me, double x); // return (x - xmin) / my dx + 1
  135. integer Matrix_xToLowColumn (Matrix me, double x); // return Melder_ifloor (Matrix_xToColumn (me, x))
  136. integer Matrix_xToHighColumn (Matrix me, double x); // return Melder_iceiling (Matrix_xToColumn (me, x))
  137. integer Matrix_xToNearestColumn (Matrix me, double x); // return Melder_iround (Matrix_xToColumn (me, x))
  138. double Matrix_yToRow (Matrix me, double y); // return (y - ymin) / my dy + 1
  139. integer Matrix_yToLowRow (Matrix me, double y); // return Melder_ifloor (Matrix_yToRow (me, y))
  140. integer Matrix_yToHighRow (Matrix me, double x); // return Melder_iceiling (Matrix_yToRow (me, y))
  141. integer Matrix_yToNearestRow (Matrix me, double y); // return Melder_iround (Matrix_yToRow (me, y))
  142. integer Matrix_getWindowSamplesY (Matrix me, double ymin, double ymax, integer *iymin, integer *iymax);
  143. integer Matrix_getWindowExtrema (Matrix me, integer ixmin, integer ixmax, integer iymin, integer iymax,
  144. double *minimum, double *maximum);
  145. /*
  146. Function:
  147. compute the minimum and maximum values of my z over all samples inside [ixmin, ixmax] * [iymin, iymax].
  148. Arguments:
  149. if ixmin = 0, start at first column; if ixmax = 0, end at last column (same for iymin and iymax).
  150. Return value:
  151. the number of samples inside the window.
  152. Postconditions:
  153. if result == 0, *minimum and *maximum are not changed;
  154. */
  155. void Matrix_formula (Matrix me, conststring32 expression, Interpreter interpreter, Matrix target);
  156. /*
  157. Arguments:
  158. "me" is the Matrix referred to as "self" or with "nx" etc. in the expression
  159. "target" is the Matrix whose elements will change according to:
  160. FOR row FROM 1 TO my ny
  161. FOR col FROM 1 TO my nx
  162. target -> z [row, col] = expression
  163. ENDFOR
  164. ENDFOR
  165. "expression" is the text to be compiled and interpreted.
  166. If "target" is null, the result will go to "me"; otherwise, to "target".
  167. Return value:
  168. 0 in case of failure, otherwise 1.
  169. */
  170. void Matrix_formula_part (Matrix me, double xmin, double xmax, double ymin, double ymax,
  171. conststring32 expression, Interpreter interpreter, Matrix target);
  172. /***** Graphics routines. *****/
  173. /*
  174. All of these routines show the samples of a Matrix whose x and y values
  175. are inside the window [xmin, xmax] * [ymin, ymax].
  176. The scaling of the values of these samples is determined by "minimum" and "maximum".
  177. All of these routines can perform automatic windowing and scaling:
  178. if xmax <= xmin, the window in the x direction will be set to [my xmin, my xmax];
  179. if ymax <= ymin, the window in the y direction will be set to [my ymin, my ymax];
  180. if maximum <= minimum, the windowing (scaling) in the z direction will be determined
  181. by the minimum and maximum values of the samples inside the window.
  182. */
  183. void Matrix_drawRows (Matrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  184. double minimum, double maximum);
  185. /*
  186. Every row is plotted as a function of x,
  187. with straight lines connecting the sample points.
  188. The rows are stacked from bottom to top.
  189. */
  190. void Matrix_drawOneContour (Matrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  191. double height);
  192. void Matrix_drawContours (Matrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  193. double minimum, double maximum);
  194. /* A contour altitude plot with curves at multiple heights. */
  195. void Matrix_paintContours (Matrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  196. double minimum, double maximum);
  197. /* A contour plot with multiple shades of grey and white (low) and black (high) paint. */
  198. void Matrix_paintImage (Matrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  199. double minimum, double maximum);
  200. /*
  201. Two-dimensional interpolation of greys.
  202. The larger the value of the sample, the darker the greys.
  203. */
  204. void Matrix_paintCells (Matrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  205. double minimum, double maximum);
  206. /*
  207. Every sample is drawn as a grey rectangle.
  208. The larger the value of the sample, the darker the rectangle.
  209. */
  210. void Matrix_paintSurface (Matrix me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  211. double minimum, double maximum, double elevation, double azimuth);
  212. /*
  213. 3D surface plot. Every space between adjacent four samples is drawn as a tetragon filled with a grey value.
  214. 'elevation' may be 30 degrees, 'azimuth' may be 45 degrees.
  215. */
  216. void Matrix_movie (Matrix me, Graphics g);
  217. autoMatrix Matrix_readFromRawTextFile (MelderFile file);
  218. autoMatrix Matrix_readAP (MelderFile file);
  219. autoMatrix Matrix_appendRows (Matrix me, Matrix thee, ClassInfo klas);
  220. void Matrix_eigen (Matrix me, autoMatrix *eigenvectors, autoMatrix *eigenvalues);
  221. autoMatrix Matrix_power (Matrix me, integer power);
  222. void Matrix_scaleAbsoluteExtremum (Matrix me, double scale);
  223. autoMatrix Table_to_Matrix (Table me);
  224. void Matrix_writeToMatrixTextFile (Matrix me, MelderFile file);
  225. void Matrix_writeToHeaderlessSpreadsheetFile (Matrix me, MelderFile file);
  226. autoMatrix TableOfReal_to_Matrix (TableOfReal me);
  227. autoTableOfReal Matrix_to_TableOfReal (Matrix me);
  228. /* End of file Matrix.h */
  229. #endif