TableOfReal_extensions.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. /* TableOfReal_extensions.cpp
  2. *
  3. * Copyright (C) 1993-2018 David Weenink, 2017 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 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. djmw 20000202 17 typos in F1-3,L1-3 table corrected
  20. djmw 20030707 Changed TableOfReal_drawVectors interface.
  21. djmw 20031030 Added TableOfReal_appendColumns
  22. djmw 20031216 Interface change in TableOfReal_choleskyDecomposition
  23. djmw 20040108 Corrected a memory leak in TableOfReal_drawBoxPlots
  24. djmw 20040211 Modified TableOfReal_copyLabels behaviour: copy NULL-labels too.
  25. djmw 20040511 Removed TableOfReal_extractRowsByRowLabel,TableOfReal_selectColumnsWhereRow
  26. djmw 20040617 Removed column selection bug in TableOfReal_drawRowsAsHistogram
  27. djmw 20041105 Added TableOfReal_createFromVanNieropData_25females
  28. djmw 20041115 TableOfReal_drawScatterPlot: plotting a NULL-label crashed Praat.
  29. djmw 20041213 Added TableOfReal_createFromWeeninkData.
  30. djmw 20050221 TableOfReal_meansByRowLabels, extra reduce parameter.
  31. djmw 20050222 TableOfReal_drawVectors didn't draw rowlabels.
  32. djmw 20050512 TableOfReal TableOfReal_meansByRowLabels crashed if first label in sorted was NULL.
  33. djmw 20051116 TableOfReal_drawScatterPlot draw reverse permited by choosing xmin > xmax and/or ymin>ymax
  34. djmw 20060301 TableOfReal_meansByRowLabels extra medianize
  35. djmw 20060626 Extra NULL argument for ExecRE.
  36. djmw 20070822 wchar
  37. djmw 20070902 Better error messages (object type and name feedback)
  38. djmw 20070614 updated to version 1.30 of regular expressions.
  39. djmw 20071202 Melder_warning<n>
  40. djmw 20080122 float -> double
  41. djmw 20081119 +TableOfReal_areAllCellsDefined
  42. djmw 20090506 +setInner for _drawScatterPlotMatrix
  43. djmw 20091009 +TableOfReal_drawColumnAsDistribution
  44. djmw 20100222 Corrected a bug in TableOfReal_copyOneRowWithLabel which caused label corruption if
  45. from and to table were equal and rows were equal too.
  46. djmw 20111110 Use autostringvector
  47. djmw 20111123 Always use Melder_wcscmp
  48. */
  49. #include <ctype.h>
  50. #include "Graphics_extensions.h"
  51. #include "SSCP.h"
  52. #include "Matrix_extensions.h"
  53. #include "NUMclapack.h"
  54. #include "NUM2.h"
  55. #include "SVD.h"
  56. #include "Table_extensions.h"
  57. #include "TableOfReal_extensions.h"
  58. #include "TableOfReal_and_Permutation.h"
  59. #include "regularExp.h"
  60. #include "Formula.h"
  61. #define EMPTY_STRING(s) (! (s) || s [0] == '\0')
  62. #define Graphics_ARROW 1
  63. #define Graphics_TWOWAYARROW 2
  64. #define Graphics_LINE 3
  65. static autoTableOfReal TableOfReal_TableOfReal_columnCorrelations (TableOfReal me, TableOfReal thee, bool center, bool normalize);
  66. static autoTableOfReal TableOfReal_TableOfReal_rowCorrelations (TableOfReal me, TableOfReal thee, bool center, bool normalize);
  67. integer TableOfReal_getColumnIndexAtMaximumInRow (TableOfReal me, integer rowNumber) {
  68. integer columnNumber = 0;
  69. if (rowNumber > 0 && rowNumber <= my numberOfRows) {
  70. double max = my data [rowNumber] [1];
  71. columnNumber = 1;
  72. for (integer icol = 2; icol <= my numberOfColumns; icol ++) {
  73. if (my data [rowNumber] [icol] > max) {
  74. max = my data [rowNumber] [icol];
  75. columnNumber = icol;
  76. }
  77. }
  78. }
  79. return columnNumber;
  80. }
  81. conststring32 TableOfReal_getColumnLabelAtMaximumInRow (TableOfReal me, integer rowNumber) {
  82. integer columnNumber = TableOfReal_getColumnIndexAtMaximumInRow (me, rowNumber);
  83. return my v_getColStr (columnNumber);
  84. }
  85. void TableOfReal_copyOneRowWithLabel (TableOfReal me, TableOfReal thee, integer myrow, integer thyrow) {
  86. try {
  87. if (me == thee && myrow == thyrow) {
  88. return;
  89. }
  90. Melder_require (myrow > 0 && myrow <= my numberOfRows && thyrow > 0 && thyrow <= thy numberOfRows && my numberOfColumns == thy numberOfColumns,
  91. U"The dimensions do not fit.");
  92. thy rowLabels [thyrow] = Melder_dup (my rowLabels [myrow].get());
  93. if (my data [myrow] != thy data [thyrow]) {
  94. NUMvector_copyElements (my data [myrow], thy data [thyrow], 1, my numberOfColumns);
  95. }
  96. } catch (MelderError) {
  97. Melder_throw (me, U": row ", myrow, U" not copied to ", thee);
  98. }
  99. }
  100. bool TableOfReal_hasRowLabels (TableOfReal me) {
  101. if (! my rowLabels) {
  102. return false;
  103. }
  104. for (integer i = 1; i <= my numberOfRows; i ++) {
  105. if (EMPTY_STRING (my rowLabels [i])) {
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. bool TableOfReal_hasColumnLabels (TableOfReal me) {
  112. if (! my columnLabels) {
  113. return false;
  114. }
  115. for (integer i = 1; i <= my numberOfColumns; i ++) {
  116. if (EMPTY_STRING (my columnLabels [i])) {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. autoTableOfReal TableOfReal_createIrisDataset () {
  123. double iris [150] [4] = {
  124. {5.1, 3.5, 1.4, 0.2}, {4.9, 3.0, 1.4, 0.2}, {4.7, 3.2, 1.3, 0.2}, {4.6, 3.1, 1.5, 0.2}, {5.0, 3.6, 1.4, 0.2},
  125. {5.4, 3.9, 1.7, 0.4}, {4.6, 3.4, 1.4, 0.3}, {5.0, 3.4, 1.5, 0.2}, {4.4, 2.9, 1.4, 0.2}, {4.9, 3.1, 1.5, 0.1},
  126. {5.4, 3.7, 1.5, 0.2}, {4.8, 3.4, 1.6, 0.2}, {4.8, 3.0, 1.4, 0.1}, {4.3, 3.0, 1.1, 0.1}, {5.8, 4.0, 1.2, 0.2},
  127. {5.7, 4.4, 1.5, 0.4}, {5.4, 3.9, 1.3, 0.4}, {5.1, 3.5, 1.4, 0.3}, {5.7, 3.8, 1.7, 0.3}, {5.1, 3.8, 1.5, 0.3},
  128. {5.4, 3.4, 1.7, 0.2}, {5.1, 3.7, 1.5, 0.4}, {4.6, 3.6, 1.0, 0.2}, {5.1, 3.3, 1.7, 0.5}, {4.8, 3.4, 1.9, 0.2},
  129. {5.0, 3.0, 1.6, 0.2}, {5.0, 3.4, 1.6, 0.4}, {5.2, 3.5, 1.5, 0.2}, {5.2, 3.4, 1.4, 0.2}, {4.7, 3.2, 1.6, 0.2},
  130. {4.8, 3.1, 1.6, 0.2}, {5.4, 3.4, 1.5, 0.4}, {5.2, 4.1, 1.5, 0.1}, {5.5, 4.2, 1.4, 0.2}, {4.9, 3.1, 1.5, 0.2},
  131. {5.0, 3.2, 1.2, 0.2}, {5.5, 3.5, 1.3, 0.2}, {4.9, 3.6, 1.4, 0.1}, {4.4, 3.0, 1.3, 0.2}, {5.1, 3.4, 1.5, 0.2},
  132. {5.0, 3.5, 1.3, 0.3}, {4.5, 2.3, 1.3, 0.3}, {4.4, 3.2, 1.3, 0.2}, {5.0, 3.5, 1.6, 0.6}, {5.1, 3.8, 1.9, 0.4},
  133. {4.8, 3.0, 1.4, 0.3}, {5.1, 3.8, 1.6, 0.2}, {4.6, 3.2, 1.4, 0.2}, {5.3, 3.7, 1.5, 0.2}, {5.0, 3.3, 1.4, 0.2},
  134. {7.0, 3.2, 4.7, 1.4}, {6.4, 3.2, 4.5, 1.5}, {6.9, 3.1, 4.9, 1.5}, {5.5, 2.3, 4.0, 1.3}, {6.5, 2.8, 4.6, 1.5},
  135. {5.7, 2.8, 4.5, 1.3}, {6.3, 3.3, 4.7, 1.6}, {4.9, 2.4, 3.3, 1.0}, {6.6, 2.9, 4.6, 1.3}, {5.2, 2.7, 3.9, 1.4},
  136. {5.0, 2.0, 3.5, 1.0}, {5.9, 3.0, 4.2, 1.5}, {6.0, 2.2, 4.0, 1.0}, {6.1, 2.9, 4.7, 1.4}, {5.6, 2.9, 3.6, 1.3},
  137. {6.7, 3.1, 4.4, 1.4}, {5.6, 3.0, 4.5, 1.5}, {5.8, 2.7, 4.1, 1.0}, {6.2, 2.2, 4.5, 1.5}, {5.6, 2.5, 3.9, 1.1},
  138. {5.9, 3.2, 4.8, 1.8}, {6.1, 2.8, 4.0, 1.3}, {6.3, 2.5, 4.9, 1.5}, {6.1, 2.8, 4.7, 1.2}, {6.4, 2.9, 4.3, 1.3},
  139. {6.6, 3.0, 4.4, 1.4}, {6.8, 2.8, 4.8, 1.4}, {6.7, 3.0, 5.0, 1.7}, {6.0, 2.9, 4.5, 1.5}, {5.7, 2.6, 3.5, 1.0},
  140. {5.5, 2.4, 3.8, 1.1}, {5.5, 2.4, 3.7, 1.0}, {5.8, 2.7, 3.9, 1.2}, {6.0, 2.7, 5.1, 1.6}, {5.4, 3.0, 4.5, 1.5},
  141. {6.0, 3.4, 4.5, 1.6}, {6.7, 3.1, 4.7, 1.5}, {6.3, 2.3, 4.4, 1.3}, {5.6, 3.0, 4.1, 1.3}, {5.5, 2.5, 4.0, 1.3},
  142. {5.5, 2.6, 4.4, 1.2}, {6.1, 3.0, 4.6, 1.4}, {5.8, 2.6, 4.0, 1.2}, {5.0, 2.3, 3.3, 1.0}, {5.6, 2.7, 4.2, 1.3},
  143. {5.7, 3.0, 4.2, 1.2}, {5.7, 2.9, 4.2, 1.3}, {6.2, 2.9, 4.3, 1.3}, {5.1, 2.5, 3.0, 1.1}, {5.7, 2.8, 4.1, 1.3},
  144. {6.3, 3.3, 6.0, 2.5}, {5.8, 2.7, 5.1, 1.9}, {7.1, 3.0, 5.9, 2.1}, {6.3, 2.9, 5.6, 1.8}, {6.5, 3.0, 5.8, 2.2},
  145. {7.6, 3.0, 6.6, 2.1}, {4.9, 2.5, 4.5, 1.7}, {7.3, 2.9, 6.3, 1.8}, {6.7, 2.5, 5.8, 1.8}, {7.2, 3.6, 6.1, 2.5},
  146. {6.5, 3.2, 5.1, 2.0}, {6.4, 2.7, 5.3, 1.9}, {6.8, 3.0, 5.5, 2.1}, {5.7, 2.5, 5.0, 2.0}, {5.8, 2.8, 5.1, 2.4},
  147. {6.4, 3.2, 5.3, 2.3}, {6.5, 3.0, 5.5, 1.8}, {7.7, 3.8, 6.7, 2.2}, {7.7, 2.6, 6.9, 2.3}, {6.0, 2.2, 5.0, 1.5},
  148. {6.9, 3.2, 5.7, 2.3}, {5.6, 2.8, 4.9, 2.0}, {7.7, 2.8, 6.7, 2.0}, {6.3, 2.7, 4.9, 1.8}, {6.7, 3.3, 5.7, 2.1},
  149. {7.2, 3.2, 6.0, 1.8}, {6.2, 2.8, 4.8, 1.8}, {6.1, 3.0, 4.9, 1.8}, {6.4, 2.8, 5.6, 2.1}, {7.2, 3.0, 5.8, 1.6},
  150. {7.4, 2.8, 6.1, 1.9}, {7.9, 3.8, 6.4, 2.0}, {6.4, 2.8, 5.6, 2.2}, {6.3, 2.8, 5.1, 1.5}, {6.1, 2.6, 5.6, 1.4},
  151. {7.7, 3.0, 6.1, 2.3}, {6.3, 3.4, 5.6, 2.4}, {6.4, 3.1, 5.5, 1.8}, {6.0, 3.0, 4.8, 1.8}, {6.9, 3.1, 5.4, 2.1},
  152. {6.7, 3.1, 5.6, 2.4}, {6.9, 3.1, 5.1, 2.3}, {5.8, 2.7, 5.1, 1.9}, {6.8, 3.2, 5.9, 2.3}, {6.7, 3.3, 5.7, 2.5},
  153. {6.7, 3.0, 5.2, 2.3}, {6.3, 2.5, 5.0, 1.9}, {6.5, 3.0, 5.2, 2.0}, {6.2, 3.4, 5.4, 2.3}, {5.9, 3.0, 5.1, 1.8}
  154. };
  155. try {
  156. autoTableOfReal me = TableOfReal_create (150, 4);
  157. TableOfReal_setColumnLabel (me.get(), 1, U"sl");
  158. TableOfReal_setColumnLabel (me.get(), 2, U"sw");
  159. TableOfReal_setColumnLabel (me.get(), 3, U"pl");
  160. TableOfReal_setColumnLabel (me.get(), 4, U"pw");
  161. for (integer i = 1; i <= 150; i ++) {
  162. int kind = (i - 1) / 50 + 1;
  163. char32 const *label = kind == 1 ? U"1" : kind == 2 ? U"2" : U"3";
  164. for (integer j = 1; j <= 4; j ++) {
  165. my data [i] [j] = iris [i - 1] [j - 1];
  166. }
  167. TableOfReal_setRowLabel (me.get(), i, label);
  168. }
  169. Thing_setName (me.get(), U"iris");
  170. return me;
  171. } catch (MelderError) {
  172. Melder_throw (U"TableOfReal from iris data not created.");
  173. }
  174. }
  175. autoStrings TableOfReal_extractRowLabels (TableOfReal me) {
  176. try {
  177. autoStrings thee = Thing_new (Strings);
  178. if (my numberOfRows > 0) {
  179. thy strings = autostring32vector (my numberOfRows);
  180. thy numberOfStrings = my numberOfRows;
  181. for (integer i = 1; i <= my numberOfRows; i ++) {
  182. conststring32 label = my rowLabels [i] ? my rowLabels [i].get() : U"?";
  183. thy strings [i] = Melder_dup (label);
  184. }
  185. }
  186. return thee;
  187. } catch (MelderError) {
  188. Melder_throw (me, U": row labels not extracted.");
  189. }
  190. }
  191. autoStrings TableOfReal_extractColumnLabels (TableOfReal me) {
  192. try {
  193. autoStrings thee = Thing_new (Strings);
  194. if (my numberOfColumns > 0) {
  195. thy strings = autostring32vector (my numberOfColumns);
  196. thy numberOfStrings = my numberOfColumns;
  197. for (integer i = 1; i <= my numberOfColumns; i ++) {
  198. conststring32 label = my columnLabels [i] ? my columnLabels [i].get() : U"?";
  199. thy strings [i] = Melder_dup (label);
  200. }
  201. }
  202. return thee;
  203. } catch (MelderError) {
  204. Melder_throw (me, U": column labels not extracted.");
  205. }
  206. }
  207. autoTableOfReal TableOfReal_transpose (TableOfReal me) {
  208. try {
  209. autoTableOfReal thee = TableOfReal_create (my numberOfColumns, my numberOfRows);
  210. for (integer i = 1; i <= my numberOfRows; i ++) {
  211. for (integer j = 1; j <= my numberOfColumns; j ++) {
  212. thy data [j] [i] = my data [i] [j];
  213. }
  214. }
  215. thy columnLabels. copyElementsFrom (my rowLabels.get());
  216. thy rowLabels. copyElementsFrom (my columnLabels.get());
  217. return thee;
  218. } catch (MelderError) {
  219. Melder_throw (me, U": not transposed.");
  220. }
  221. }
  222. void TableOfReal_to_PatternList_and_Categories (TableOfReal me, integer fromrow, integer torow, integer fromcol, integer tocol,
  223. autoPatternList *p, autoCategories *c)
  224. {
  225. try {
  226. integer ncols = my numberOfColumns, nrows = my numberOfRows;
  227. fromrow = fromrow == 0 ? 1 : fromrow;
  228. torow = torow == 0 ? nrows : torow;
  229. Melder_require (fromrow > 0 && fromrow <= torow && torow <= nrows,
  230. U"Invalid row selection.");
  231. fromcol = fromcol == 0 ? 1 : fromcol;
  232. tocol = tocol == 0 ? ncols : tocol;
  233. Melder_require (fromcol > 0 && fromcol <= tocol && tocol <= ncols,
  234. U"Invalid column selection.");
  235. nrows = torow - fromrow + 1;
  236. ncols = tocol - fromcol + 1;
  237. autoPatternList ap = PatternList_create (nrows, ncols);
  238. autoCategories ac = Categories_create ();
  239. integer row = 1;
  240. for (integer i = fromrow; i <= torow; i ++, row ++) {
  241. char32 const *s = my rowLabels [i] ? my rowLabels [i].get() : U"?";
  242. autoSimpleString item = SimpleString_create (s);
  243. ac -> addItem_move (item.move());
  244. integer col = 1;
  245. for (integer j = fromcol; j <= tocol; j ++, col ++) {
  246. ap -> z [row] [col] = my data [i] [j];
  247. }
  248. }
  249. *p = ap.move();
  250. *c = ac.move();
  251. } catch (MelderError) {
  252. Melder_throw (U"PatternList and Categories not created from TableOfReal.");
  253. }
  254. }
  255. void TableOfReal_getColumnExtrema (TableOfReal me, integer col, double *p_min, double *p_max) {
  256. Melder_require (col > 0 && col <= my numberOfColumns,
  257. U"Invalid column number.");
  258. double min = my data [1] [col], max = my data [1] [col];
  259. for (integer i = 2; i <= my numberOfRows; i ++) {
  260. if (my data [i] [col] > max) {
  261. max = my data [i] [col];
  262. } else if (my data [i] [col] < min) {
  263. min = my data [i] [col];
  264. }
  265. }
  266. if (p_min) {
  267. *p_min = min;
  268. }
  269. if (p_max) {
  270. *p_max = max;
  271. }
  272. }
  273. void TableOfReal_drawRowsAsHistogram (TableOfReal me, Graphics g, conststring32 rows, integer colb, integer cole, double ymin,
  274. double ymax, double xoffsetFraction, double interbarFraction, double interbarsFraction, conststring32 greys, bool garnish)
  275. {
  276. colb = colb == 0 ? 1 : colb;
  277. cole = cole == 0 ? my numberOfColumns : cole;
  278. Melder_require (colb > 0 && colb <= cole && cole <= my numberOfColumns,
  279. U"Invalid columns");
  280. autoVEC irows = VEC_createFromString (rows);
  281. for (integer i = 1; i <= irows.size; i ++) {
  282. integer irow = Melder_ifloor (irows [i]);
  283. if (irow < 0 || irow > my numberOfRows) {
  284. Melder_throw (U"Invalid row (", irow, U").");
  285. }
  286. if (ymin >= ymax) {
  287. double min, max;
  288. NUMvector_extrema (my data [irow], colb, cole, & min, & max);
  289. if (i > 1) {
  290. if (min < ymin) {
  291. ymin = min;
  292. }
  293. if (max > ymax) {
  294. ymax = max;
  295. }
  296. } else {
  297. ymin = min;
  298. ymax = max;
  299. }
  300. }
  301. }
  302. autoVEC igreys = VEC_createFromString (greys);
  303. Graphics_setWindow (g, 0.0, 1.0, ymin, ymax);
  304. Graphics_setInner (g);
  305. integer ncols = cole - colb + 1, nrows = irows.size;
  306. ;
  307. double bar_width = 1.0 / (ncols * nrows + 2.0 * xoffsetFraction + (ncols - 1) * interbarsFraction + ncols * (nrows - 1) * interbarFraction);
  308. double dx = (interbarsFraction + nrows + (nrows - 1) * interbarFraction) * bar_width;
  309. for (integer i = 1; i <= nrows; i ++) {
  310. integer irow = Melder_ifloor (irows [i]);
  311. double xb = xoffsetFraction * bar_width + (i - 1) * (1.0 + interbarFraction) * bar_width;
  312. double x1 = xb;
  313. double grey = i <= igreys.size ? igreys [i] : igreys [igreys.size];
  314. for (integer j = colb; j <= cole; j ++) {
  315. double x2 = x1 + bar_width;
  316. double y1 = ymin, y2 = my data [irow] [j];
  317. if (y2 > ymin) {
  318. if (y2 > ymax) {
  319. y2 = ymax;
  320. }
  321. Graphics_setGrey (g, grey);
  322. Graphics_fillRectangle (g, x1, x2, y1, y2);
  323. Graphics_setGrey (g, 0.0); // black
  324. Graphics_rectangle (g, x1, x2, y1, y2);
  325. }
  326. x1 += dx;
  327. }
  328. }
  329. Graphics_unsetInner (g);
  330. if (garnish) {
  331. double xb = (xoffsetFraction + 0.5 * (nrows + (nrows - 1) * interbarFraction)) * bar_width;
  332. for (integer j = colb; j <= cole; j ++) {
  333. if (my columnLabels [j]) {
  334. Graphics_markBottom (g, xb, false, false, false, my columnLabels [j].get());
  335. }
  336. xb += dx;
  337. }
  338. Graphics_drawInnerBox (g);
  339. Graphics_marksLeft (g, 2, true, true, false);
  340. }
  341. }
  342. void TableOfReal_drawBiplot (TableOfReal me, Graphics g, double xmin, double xmax, double ymin, double ymax, double sv_splitfactor, int labelsize, bool garnish) {
  343. integer nr = my numberOfRows, nc = my numberOfColumns, nPoints = nr + nc;
  344. int fontsize = Graphics_inqFontSize (g);
  345. autoSVD svd = SVD_create (nr, nc);
  346. matrixcopy_preallocated (svd -> u.get(), my data.get());
  347. MATcentreEachColumn_inplace (svd -> u.get());
  348. SVD_compute (svd.get());
  349. integer numberOfZeroed = SVD_zeroSmallSingularValues (svd.get(), 0.0);
  350. integer nmin = std::min (nr, nc) - numberOfZeroed;
  351. Melder_require (nmin > 1,
  352. U"There should be at least two (independent) columns in the table.");
  353. autoVEC x = VECraw (nPoints);
  354. autoVEC y = VECraw ( nPoints);
  355. double lambda1 = pow (svd -> d [1], sv_splitfactor);
  356. double lambda2 = pow (svd -> d [2], sv_splitfactor);
  357. for (integer i = 1; i <= nr; i ++) {
  358. x [i] = svd -> u [i] [1] * lambda1;
  359. y [i] = svd -> u [i] [2] * lambda2;
  360. }
  361. lambda1 = svd -> d [1] / lambda1;
  362. lambda2 = svd -> d [2] / lambda2;
  363. for (integer i = 1; i <= nc; i ++) {
  364. x [nr + i] = svd -> v [i] [1] * lambda1;
  365. y [nr + i] = svd -> v [i] [2] * lambda2;
  366. }
  367. if (xmax <= xmin) {
  368. NUMvector_extrema (x.at, 1, nPoints, & xmin, & xmax);
  369. }
  370. if (xmax <= xmin) {
  371. xmax += 1; xmin -= 1;
  372. }
  373. if (ymax <= ymin) {
  374. NUMvector_extrema (y.at, 1, nPoints, & ymin, & ymax);
  375. }
  376. if (ymax <= ymin) {
  377. ymax += 1.0;
  378. ymin -= 1.0;
  379. }
  380. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  381. Graphics_setInner (g);
  382. if (labelsize > 0) {
  383. Graphics_setFontSize (g, labelsize);
  384. }
  385. Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF);
  386. for (integer i = 1; i <= nPoints; i ++) {
  387. char32 const *label;
  388. if (i <= nr) {
  389. label = my rowLabels [i].get();
  390. if (! label) {
  391. label = U"?__r_";
  392. }
  393. } else {
  394. label = my columnLabels [i - nr].get();
  395. if (! label) {
  396. label = U"?__c_";
  397. }
  398. }
  399. Graphics_text (g, x [i], y [i], label);
  400. }
  401. Graphics_unsetInner (g);
  402. if (garnish) {
  403. Graphics_drawInnerBox (g);
  404. Graphics_marksLeft (g, 2, true, true, false);
  405. Graphics_marksBottom (g, 2, true, true, false);
  406. }
  407. if (labelsize > 0) {
  408. Graphics_setFontSize (g, fontsize);
  409. }
  410. }
  411. void TableOfReal_drawBoxPlots (TableOfReal me, Graphics g, integer rowmin, integer rowmax, integer colmin, integer colmax, double ymin, double ymax, bool garnish) {
  412. if (rowmax < rowmin || rowmax < 1) {
  413. rowmin = 1; rowmax = my numberOfRows;
  414. }
  415. if (rowmin < 1) {
  416. rowmin = 1;
  417. }
  418. if (rowmax > my numberOfRows) {
  419. rowmax = my numberOfRows;
  420. }
  421. integer numberOfRows = rowmax - rowmin + 1;
  422. if (colmax < colmin || colmax < 1) {
  423. colmin = 1; colmax = my numberOfColumns;
  424. }
  425. if (colmin < 1) {
  426. colmin = 1;
  427. }
  428. if (colmax > my numberOfColumns) {
  429. colmax = my numberOfColumns;
  430. }
  431. if (ymax <= ymin) {
  432. NUMmatrix_extrema (my data.at, rowmin, rowmax, colmin, colmax, &ymin, &ymax);
  433. }
  434. Graphics_setWindow (g, colmin - 0.5, colmax + 0.5, ymin, ymax);
  435. Graphics_setInner (g);
  436. autoVEC data = VECraw (numberOfRows);
  437. for (integer j = colmin; j <= colmax; j ++) {
  438. double x = j, r = 0.05, w = 0.2, t;
  439. integer ndata = 0;
  440. for (integer i = 1; i <= numberOfRows; i ++) {
  441. if (isdefined (t = my data [rowmin + i - 1] [j])) {
  442. data [ ++ ndata] = t;
  443. }
  444. }
  445. Graphics_boxAndWhiskerPlot (g, data.get(), x, r, w, ymin, ymax);
  446. }
  447. Graphics_unsetInner (g);
  448. if (garnish) {
  449. Graphics_drawInnerBox (g);
  450. for (integer j = colmin; j <= colmax; j ++) {
  451. if (my columnLabels && my columnLabels [j] && my columnLabels [j] [0]) {
  452. Graphics_markBottom (g, j, false, true, false, my columnLabels [j].get());
  453. }
  454. }
  455. Graphics_marksLeft (g, 2, true, true, false);
  456. }
  457. }
  458. void TableOfReal_copyLabels (TableOfReal me, TableOfReal thee, int rowOrigin, int columnOrigin) {
  459. if (rowOrigin == 1) {
  460. Melder_require (my numberOfRows == thy numberOfRows,
  461. U"Both tables must have the same number of rows.");
  462. thy rowLabels. copyElementsFrom (my rowLabels.get());
  463. } else if (rowOrigin == -1) {
  464. Melder_require (my numberOfColumns == thy numberOfRows,
  465. U"Both tables must have the same number of columns.");
  466. thy rowLabels. copyElementsFrom (my columnLabels.get());
  467. }
  468. if (columnOrigin == 1) {
  469. Melder_require (my numberOfColumns == thy numberOfColumns,
  470. U"Both tables must have the same number of columns.");
  471. thy columnLabels. copyElementsFrom (my columnLabels.get());
  472. } else if (columnOrigin == -1) {
  473. Melder_require (my numberOfRows == thy numberOfColumns,
  474. U"Both tables must have the same number of rows.");
  475. thy columnLabels. copyElementsFrom (my rowLabels.get());
  476. }
  477. }
  478. void TableOfReal_setLabelsFromCollectionItemNames (TableOfReal me, Collection thee, bool setRowLabels, bool setColumnLabels) {
  479. try {
  480. if (setRowLabels) {
  481. Melder_assert (my numberOfRows == thy size);
  482. for (integer i = 1; i <= my numberOfRows; i ++) {
  483. conststring32 name = Thing_getName (thy at [i]);
  484. TableOfReal_setRowLabel (me, i, name);
  485. }
  486. }
  487. if (setColumnLabels) {
  488. Melder_assert (my numberOfColumns == thy size);
  489. for (integer i = 1; i <= my numberOfColumns; i ++) {
  490. conststring32 name = Thing_getName (thy at [i]);
  491. TableOfReal_setColumnLabel (me, i, name);
  492. }
  493. }
  494. } catch (MelderError) {
  495. Melder_throw (me, U": labels not changed.");
  496. }
  497. }
  498. void TableOfReal_centreColumns (TableOfReal me) {
  499. MATcentreEachColumn_inplace (my data.get());
  500. }
  501. void TableOfReal_Categories_setRowLabels (TableOfReal me, Categories thee) {
  502. try {
  503. Melder_require (my numberOfRows == thy size,
  504. U"The number of items in both objects should be equal.");
  505. /*
  506. Create without change.
  507. */
  508. autoCategories categories_copy = Data_copy (thee);
  509. /*
  510. Change without error.
  511. */
  512. for (integer i = 1; i <= my numberOfRows; i ++)
  513. my rowLabels [i] = categories_copy->at [i] -> string.move();
  514. } catch (MelderError) {
  515. Melder_throw (me, U": row labels not set from categories.");
  516. }
  517. }
  518. void TableOfReal_centreColumns_byRowLabel (TableOfReal me) {
  519. conststring32 label = my rowLabels [1].get();
  520. integer index = 1;
  521. for (integer i = 2; i <= my numberOfRows; i ++) {
  522. conststring32 li = my rowLabels [i].get();
  523. if (! Melder_equ (li, label)) {
  524. MATcentreEachColumn_inplace (my data.horizontalBand (index, i - 1));
  525. label = li;
  526. index = i;
  527. }
  528. }
  529. MATcentreEachColumn_inplace (my data.horizontalBand (index, my numberOfRows));
  530. }
  531. double TableOfReal_getRowSum (TableOfReal me, integer rowNumber) {
  532. Melder_require (rowNumber > 0 && rowNumber <= my numberOfRows,
  533. U"Row number not in valid range.");
  534. return NUMrowSum (my data.get(), rowNumber);
  535. }
  536. double TableOfReal_getColumnSumByLabel (TableOfReal me, conststring32 columnLabel) {
  537. integer columnNumber = TableOfReal_columnLabelToIndex (me, columnLabel);
  538. Melder_require (columnNumber > 0,
  539. U"There is no \"", columnLabel, U"\" column label.");
  540. return TableOfReal_getColumnSum (me, columnNumber);
  541. }
  542. double TableOfReal_getRowSumByLabel (TableOfReal me, conststring32 rowLabel) {
  543. integer rowNumber = TableOfReal_rowLabelToIndex (me, rowLabel);
  544. Melder_require (rowNumber > 0,
  545. U"There is no \"", rowLabel, U"\" row label.");
  546. return TableOfReal_getRowSum (me, rowNumber);
  547. }
  548. double TableOfReal_getColumnSum (TableOfReal me, integer columnNumber) {
  549. Melder_require (columnNumber > 0 && columnNumber <= my numberOfColumns,
  550. U"Column number not in valid range.");
  551. return NUMcolumnSum (my data.get(), columnNumber);
  552. }
  553. double TableOfReal_getGrandSum (TableOfReal me) {
  554. return NUMsum (my data.get());
  555. }
  556. void TableOfReal_centreRows (TableOfReal me) {
  557. MATcentreEachRow_inplace (my data.get());
  558. }
  559. void TableOfReal_doubleCentre (TableOfReal me) {
  560. MATdoubleCentre_inplace (my data.get());
  561. }
  562. void TableOfReal_normalizeColumns (TableOfReal me, double norm) {
  563. MATnormalizeColumns_inplace (my data.get(), 2.0, norm);
  564. }
  565. void TableOfReal_normalizeRows (TableOfReal me, double norm) {
  566. MATnormalizeRows_inplace (my data.get(), 2.0, norm);
  567. }
  568. void TableOfReal_standardizeColumns (TableOfReal me) {
  569. if (my numberOfRows <= 1) {
  570. for (integer irow = 1; irow <= my numberOfRows; irow ++) {
  571. for (integer icol = 1; icol <= my numberOfColumns; icol ++)
  572. my data [irow] [icol] = 0.0;
  573. }
  574. return;
  575. }
  576. for (integer icol = 1; icol <= my numberOfColumns; icol ++) {
  577. double mean, stdev;
  578. NUM_sum_mean_sumsq_variance_stdev (my data.get(), icol, nullptr, & mean, nullptr, nullptr, & stdev);
  579. for (integer irow = 1; irow <= my numberOfRows; irow ++)
  580. my data [irow] [icol] = (my data [irow] [icol] - mean) / stdev;
  581. }
  582. }
  583. void TableOfReal_standardizeRows (TableOfReal me) {
  584. if (my numberOfColumns <= 1) {
  585. for (integer irow = 1; irow <= my numberOfRows; irow ++) {
  586. for (integer icol = 1; icol <= my numberOfColumns; icol ++)
  587. my data [irow] [icol] = 0.0;
  588. }
  589. return;
  590. }
  591. for (integer irow = 1; irow <= my numberOfRows; irow ++) {
  592. double mean, stdev;
  593. NUM_sum_mean_sumsq_variance_stdev (constVEC (my data [irow], my numberOfColumns),
  594. nullptr, & mean, nullptr, nullptr, & stdev);
  595. for (integer icol = 1; icol <= my numberOfColumns; icol ++)
  596. my data [irow] [icol] = (my data [irow] [icol] - mean) / stdev;
  597. }
  598. }
  599. void TableOfReal_normalizeTable (TableOfReal me, double norm) {
  600. MATnormalize_inplace (my data.get(), 2.0, norm);
  601. }
  602. double TableOfReal_getTableNorm (TableOfReal me) {
  603. return NUMnorm (my data.get(), 2.0);
  604. }
  605. bool TableOfReal_checkNonNegativity (TableOfReal me) {
  606. for (integer i = 1; i <= my numberOfRows; i ++) {
  607. for (integer j = 1; j <= my numberOfColumns; j ++) {
  608. if (my data [i] [j] < 0.0)
  609. return false;
  610. }
  611. }
  612. return true;
  613. }
  614. static void NUMdmatrix_getColumnExtrema (double **a, integer rowb, integer rowe, integer icol, double *min, double *max) {
  615. *min = *max = a [rowb] [icol];
  616. for (integer i = rowb + 1; i <= rowe; i ++) {
  617. double t = a [i] [icol];
  618. if (t > *max) {
  619. *max = t;
  620. } else if (t < *min) {
  621. *min = t;
  622. }
  623. }
  624. }
  625. void TableOfReal_drawScatterPlotMatrix (TableOfReal me, Graphics g, integer colb, integer cole, double fractionWhite) {
  626. integer m = my numberOfRows;
  627. if (colb == 0 && cole == 0) {
  628. colb = 1; cole = my numberOfColumns;
  629. } else if (cole < colb || colb < 1 || cole > my numberOfColumns) {
  630. return;
  631. }
  632. integer n = cole - colb + 1;
  633. if (n == 1) {
  634. return;
  635. }
  636. autoNUMvector<double> xmin (colb, cole);
  637. autoNUMvector<double> xmax (colb, cole);
  638. for (integer j = colb; j <= cole; j ++) {
  639. xmin [j] = xmax [j] = my data [1] [j];
  640. }
  641. for (integer i = 2; i <= m; i ++) {
  642. for (integer j = colb; j <= cole; j ++) {
  643. if (my data [i] [j] > xmax [j]) {
  644. xmax [j] = my data [i] [j];
  645. } else if (my data [i] [j] < xmin [j]) {
  646. xmin [j] = my data [i] [j];
  647. }
  648. }
  649. }
  650. for (integer j = colb; j <= cole; j ++) {
  651. double extra = fractionWhite * fabs (xmax [j] - xmin [j]);
  652. if (extra == 0.0) {
  653. extra = 0.5;
  654. }
  655. xmin [j] -= extra; xmax [j] += extra;
  656. }
  657. Graphics_setWindow (g, 0.0, n, 0.0, n);
  658. Graphics_setInner (g);
  659. Graphics_line (g, 0.0, n, n, n);
  660. Graphics_line (g, 0.0, 0.0, 0.0, n);
  661. Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF);
  662. for (integer i = 1; i <= n; i ++) {
  663. integer xcol, ycol = colb + i - 1;
  664. Graphics_line (g, 0.0, n - i, n, n - i);
  665. Graphics_line (g, i, n, i, 0.0);
  666. for (integer j = 1; j <= n; j ++) {
  667. xcol = colb + j - 1;
  668. if (i == j) {
  669. conststring32 mark = my columnLabels [xcol].get();
  670. char32 label [40];
  671. if (! mark) {
  672. Melder_sprint (label,40, U"Column ", xcol);
  673. mark = label;
  674. }
  675. Graphics_text (g, j - 0.5, n - i + 0.5, mark);
  676. } else {
  677. for (integer k = 1; k <= m; k ++) {
  678. double x = j - 1 + (my data [k] [xcol] - xmin [xcol]) / (xmax [xcol] - xmin [xcol]);
  679. double y = n - i + (my data [k] [ycol] - xmin [ycol]) / (xmax [ycol] - xmin [ycol]);
  680. conststring32 mark = EMPTY_STRING (my rowLabels [k]) ? U"+" : my rowLabels [k].get();
  681. Graphics_text (g, x, y, mark);
  682. }
  683. }
  684. }
  685. }
  686. Graphics_unsetInner (g);
  687. }
  688. void TableOfReal_drawAsScalableSquares (TableOfReal me, Graphics g, integer rowmin, integer rowmax, integer colmin, integer colmax, kGraphicsMatrixOrigin origin, double cellSizeFactor, kGraphicsMatrixCellDrawingOrder fillOrder, bool garnish) {
  689. try {
  690. //cellSizeFactor = cellSizeFactor <= 0.0 ? 1.0 : cellSizeFactor;
  691. NUMfixIndicesInRange (1, my numberOfRows, & rowmin, & rowmax);
  692. NUMfixIndicesInRange (1, my numberOfColumns, & colmin, & colmax);
  693. autoMatrix thee = TableOfReal_to_Matrix (me);
  694. Graphics_setWindow (g, colmin - 0.5, colmax + 0.5, rowmin - 0.5, rowmax + 0.5);
  695. Graphics_setInner (g);
  696. Matrix_drawAsSquares_inside (thee.get(), g, colmin - 0.5, colmax + 0.5, rowmin - 0.5, rowmax + 0.5, origin, cellSizeFactor, fillOrder);
  697. Graphics_unsetInner (g);
  698. if (garnish) {
  699. Graphics_drawInnerBox (g);
  700. Graphics_marksBottomEvery (g, 1.0, 1.0, false, true, false);
  701. Graphics_marksLeftEvery (g, 1.0, 1.0, false, true, false);
  702. }
  703. } catch (MelderError) {
  704. Melder_clearError (); // drawing errors shall be ignored
  705. }
  706. }
  707. void TableOfReal_drawScatterPlot (TableOfReal me, Graphics g,
  708. integer icx, integer icy, integer rowb, integer rowe, double xmin, double xmax, double ymin, double ymax,
  709. int labelSize, bool useRowLabels, conststring32 label, bool garnish)
  710. {
  711. double m = my numberOfRows, n = my numberOfColumns;
  712. int fontSize = Graphics_inqFontSize (g);
  713. if (icx < 1 || icx > n || icy < 1 || icy > n)
  714. return;
  715. if (rowb < 1)
  716. rowb = 1;
  717. if (rowe > m)
  718. rowe = Melder_ifloor (m);
  719. if (rowe <= rowb) {
  720. rowb = 1;
  721. rowe = Melder_ifloor (m);
  722. }
  723. if (xmax == xmin) {
  724. NUMdmatrix_getColumnExtrema (my data.at, rowb, rowe, icx, & xmin, & xmax);
  725. double tmp = ( xmax == xmin ? 0.5 : 0.0 );
  726. xmin -= tmp;
  727. xmax += tmp;
  728. }
  729. if (ymax == ymin) {
  730. NUMdmatrix_getColumnExtrema (my data.at, rowb, rowe, icy, & ymin, & ymax);
  731. double tmp = ( ymax == ymin ? 0.5 : 0.0 );
  732. ymin -= tmp;
  733. ymax += tmp;
  734. }
  735. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  736. Graphics_setInner (g);
  737. Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF);
  738. Graphics_setFontSize (g, labelSize);
  739. integer noLabel = 0;
  740. for (integer i = rowb; i <= rowe; i ++) {
  741. double x = my data [i] [icx], y = my data [i] [icy];
  742. if (((xmin < xmax && x >= xmin && x <= xmax) || (xmin > xmax && x <= xmin && x >= xmax)) &&
  743. ((ymin < ymax && y >= ymin && y <= ymax) || (ymin > ymax && y <= ymin && y >= ymax)))
  744. {
  745. conststring32 plotLabel = useRowLabels ? my rowLabels [i].get() : label;
  746. if (! Melder_findInk (plotLabel)) {
  747. noLabel ++;
  748. continue;
  749. }
  750. Graphics_text (g, x, y, plotLabel);
  751. }
  752. }
  753. Graphics_setFontSize (g, fontSize);
  754. Graphics_unsetInner (g);
  755. if (garnish) {
  756. Graphics_drawInnerBox (g);
  757. if (ymin < ymax) {
  758. if (my columnLabels [icx]) {
  759. Graphics_textBottom (g, true, my columnLabels [icx].get());
  760. }
  761. Graphics_marksBottom (g, 2, true, true, false);
  762. } else {
  763. if (my columnLabels [icx]) {
  764. Graphics_textTop (g, true, my columnLabels [icx].get());
  765. }
  766. Graphics_marksTop (g, 2, true, true, false);
  767. }
  768. if (xmin < xmax) {
  769. if (my columnLabels [icy]) {
  770. Graphics_textLeft (g, true, my columnLabels [icy].get());
  771. }
  772. Graphics_marksLeft (g, 2, true, true, false);
  773. } else {
  774. if (my columnLabels [icy]) {
  775. Graphics_textRight (g, true, my columnLabels [icy].get());
  776. }
  777. Graphics_marksRight (g, 2, true, true, false);
  778. }
  779. }
  780. if (noLabel > 0) {
  781. Melder_warning (noLabel, U" from ", my numberOfRows, U" labels are "
  782. U"not visible because they are empty or they contain only spaces or non-printable characters");
  783. }
  784. }
  785. #pragma mark - class TableOfRealList
  786. autoTableOfReal TableOfRealList_sum (TableOfRealList me) {
  787. try {
  788. if (my size <= 0) {
  789. return autoTableOfReal();
  790. }
  791. autoTableOfReal thee = Data_copy (my at [1]);
  792. for (integer i = 2; i <= my size; i ++) {
  793. TableOfReal him = my at [i];
  794. Melder_require (thy numberOfRows == his numberOfRows && thy numberOfColumns == his numberOfColumns
  795. && NUMequal (thy rowLabels.get(), his rowLabels.get())
  796. && NUMequal (thy columnLabels.get(), his columnLabels.get()),
  797. U"Dimensions or labels differ for table ", i, U".");
  798. for (integer j = 1; j <= thy numberOfRows; j ++) {
  799. for (integer k = 1; k <= thy numberOfColumns; k ++) {
  800. thy data [j] [k] += his data [j] [k];
  801. }
  802. }
  803. }
  804. return thee;
  805. } catch (MelderError) {
  806. Melder_throw (me, U": sum not created.");
  807. }
  808. }
  809. bool TableOfRealList_haveIdenticalDimensions (TableOfRealList me) {
  810. if (my size < 2) {
  811. return true;
  812. }
  813. TableOfReal t1 = my at [1];
  814. for (integer i = 2; i <= my size; i ++) {
  815. TableOfReal t = my at [i];
  816. if (t -> numberOfColumns != t1 -> numberOfColumns || t -> numberOfRows != t1 -> numberOfRows) {
  817. return false;
  818. }
  819. }
  820. return true;
  821. }
  822. double TableOfReal_getColumnQuantile (TableOfReal me, integer columnNumber, double quantile) {
  823. try {
  824. if (columnNumber < 1 || columnNumber > my numberOfColumns)
  825. return undefined;
  826. autoVEC values = VECcolumn (my data.get(), columnNumber);
  827. VECsort_inplace (values.get());
  828. return NUMquantile (values.get(), quantile);
  829. } catch (MelderError) {
  830. return undefined;
  831. }
  832. }
  833. autoTableOfReal TableOfReal_create_sandwell1987 () {
  834. try {
  835. /*
  836. The following numbers are the 21 (approximate) data points in Fig, 2 in Sandwell (1987).
  837. They were measured by djmw from a printed picture blown up by 800%.
  838. The following numbers are in cm measured from the left (x) and the bottom (y) of the figure.
  839. Vertical scale: 8.25 cm in the picture is 12 units, y [1] is at y = 0.0.
  840. Horizontal scale: 17.75 cm is 10 units, x [1] is at x = 0.0.
  841. */
  842. double x [22] = { 0.0, 0.9, 2.15, 3.5, 4.75, 5.3, 6.15, 7.15, 7.95, 8.85, 9.95, 10.15, 10.3, 11.5, 12.4, 13.3, 14.2, 15.15, 16.0, 16.85, 17.25, 18.15 };
  843. double y [22] = { 0.0, 4.2, 3.5, 4.2, 5.65, 10.1, 8.5, 7.8, 7.1, 6.4, 5.65, 0.6, 5.65, 4.2, 5.65, 7.1, 6.75, 6.35, 4.2, 2.05, 4.95, 4.25 };
  844. integer numberOfSamples = 21;
  845. autoTableOfReal thee = TableOfReal_create (numberOfSamples, 2);
  846. for (integer isample = 1; isample <= numberOfSamples; isample ++) {
  847. thy data [isample] [1] = (x [isample] - x [1]) * 10.0 / 17.75;
  848. thy data [isample] [2] = (y [isample] - y [1]) * 12.0 / 8.25;
  849. }
  850. return thee;
  851. } catch (MelderError) {
  852. Melder_throw (U"Sandwell (1987) table not created.");
  853. }
  854. }
  855. static autoTableOfReal TableOfReal_createPolsVanNieropData (int choice, bool include_levels) {
  856. try {
  857. autoTable table = Table_create_polsVanNierop1973 ();
  858. // Default: Pols 50 males, first part of the table.
  859. integer nrows = 50 * 12;
  860. integer ncols = include_levels ? 6 : 3;
  861. integer ib = 1;
  862. if (choice == 2) { /* Van Nierop 25 females */
  863. ib = nrows + 1;
  864. nrows = 25 * 12;
  865. }
  866. autoTableOfReal thee = TableOfReal_create (nrows, ncols);
  867. for (integer i = 1; i <= nrows; i ++) {
  868. TableRow row = table -> rows.at [ib + i - 1];
  869. TableOfReal_setRowLabel (thee.get(), i, row -> cells [4]. string.get());
  870. for (integer j = 1; j <= 3; j ++) {
  871. thy data [i] [j] = Melder_atof (row -> cells [4 + j]. string.get());
  872. if (include_levels) {
  873. thy data [i] [3 + j] = Melder_atof (row -> cells [7 + j]. string.get());
  874. }
  875. }
  876. }
  877. for (integer j = 1; j <= 3; j ++) {
  878. conststring32 label = table -> columnHeaders [4 + j]. label.get();
  879. TableOfReal_setColumnLabel (thee.get(), j, label);
  880. if (include_levels) {
  881. label = table -> columnHeaders [7 + j]. label.get();
  882. TableOfReal_setColumnLabel (thee.get(), 3 + j, label);
  883. }
  884. }
  885. return thee;
  886. } catch (MelderError) {
  887. Melder_throw (U"TableOfReal from Pols & Van Nierop data not created.");
  888. }
  889. }
  890. autoTableOfReal TableOfReal_create_pols1973 (bool include_levels) {
  891. return TableOfReal_createPolsVanNieropData (1, include_levels);
  892. }
  893. autoTableOfReal TableOfReal_create_vanNierop1973 (bool include_levels) {
  894. return TableOfReal_createPolsVanNieropData (2, include_levels);
  895. }
  896. autoTableOfReal TableOfReal_create_weenink1983 (int option) {
  897. try {
  898. integer nvowels = 12, ncols = 3, nrows = 10 * nvowels;
  899. autoTable table = Table_create_weenink1983 ();
  900. integer ib = ( option == 1 ? 1 : option == 2 ? 11 : 21 ); /* m f c*/
  901. ib = (ib - 1) * nvowels + 1;
  902. autoTableOfReal thee = TableOfReal_create (nrows, ncols);
  903. for (integer i = 1; i <= nrows; i ++) {
  904. TableRow row = table -> rows.at [ib + i - 1];
  905. TableOfReal_setRowLabel (thee.get(), i, row -> cells [5]. string.get());
  906. for (integer j = 1; j <= 3; j ++) {
  907. thy data [i] [j] = Melder_atof (row -> cells [6 + j]. string.get()); /* Skip F0 */
  908. }
  909. }
  910. for (integer j = 1; j <= 3; j ++) {
  911. conststring32 label = table -> columnHeaders [6 + j]. label.get();
  912. TableOfReal_setColumnLabel (thee.get(), j, label);
  913. }
  914. return thee;
  915. } catch (MelderError) {
  916. Melder_throw (U"TableOfReal from Weenink data not created.");
  917. }
  918. }
  919. autoTableOfReal TableOfReal_randomizeRows (TableOfReal me) {
  920. try {
  921. autoPermutation p = Permutation_create (my numberOfRows);
  922. Permutation_permuteRandomly_inplace (p.get(), 0, 0);
  923. autoTableOfReal thee = TableOfReal_Permutation_permuteRows (me, p.get());
  924. return thee;
  925. } catch (MelderError) {
  926. Melder_throw (me, U": randomized rows not created");
  927. }
  928. }
  929. autoTableOfReal TableOfReal_bootstrap (TableOfReal me) {
  930. try {
  931. autoTableOfReal thee = TableOfReal_create (my numberOfRows, my numberOfColumns);
  932. // Copy column labels.
  933. for (integer i = 1; i <= my numberOfColumns; i ++) {
  934. if (my columnLabels [i]) {
  935. TableOfReal_setColumnLabel (thee.get(), i, my columnLabels [i].get());
  936. }
  937. }
  938. /*
  939. Select randomly from table with replacement. Because of replacement
  940. you do not get back the original data set. A random fraction,
  941. typically 1/e (37%) are replaced by duplicates.
  942. */
  943. for (integer i = 1; i <= my numberOfRows; i ++) {
  944. integer p = NUMrandomInteger (1, my numberOfRows);
  945. NUMvector_copyElements (my data [p], thy data [i], 1, my numberOfColumns);
  946. if (my rowLabels [p]) {
  947. TableOfReal_setRowLabel (thee.get(), i, my rowLabels [p].get());
  948. }
  949. }
  950. return thee;
  951. } catch (MelderError) {
  952. Melder_throw (me, U": bootstrapped data not created.");
  953. }
  954. }
  955. void TableOfReal_changeRowLabels (TableOfReal me,
  956. conststring32 search, conststring32 replace, integer maximumNumberOfReplaces,
  957. integer *nmatches, integer *nstringmatches, bool use_regexp)
  958. {
  959. try {
  960. autostring32vector rowLabels = string32vector_searchAndReplace (my rowLabels.get(),
  961. search, replace, maximumNumberOfReplaces, nmatches, nstringmatches, use_regexp);
  962. my rowLabels = std::move (rowLabels);
  963. } catch (MelderError) {
  964. Melder_throw (me, U": row labels not changed.");
  965. }
  966. }
  967. void TableOfReal_changeColumnLabels (TableOfReal me,
  968. conststring32 search, conststring32 replace, integer maximumNumberOfReplaces,
  969. integer *nmatches, integer *nstringmatches, bool use_regexp)
  970. {
  971. try {
  972. autostring32vector columnLabels = string32vector_searchAndReplace (my columnLabels.get(),
  973. search, replace, maximumNumberOfReplaces, nmatches, nstringmatches, use_regexp);
  974. my columnLabels = std::move (columnLabels);
  975. } catch (MelderError) {
  976. Melder_throw (me, U": column labels not changed.");
  977. }
  978. }
  979. integer TableOfReal_getNumberOfLabelMatches (TableOfReal me, conststring32 search, bool columnLabels, bool use_regexp) {
  980. integer nmatches = 0, numberOfLabels = my numberOfRows;
  981. char32 **labels = my rowLabels.peek2();
  982. regexp *compiled_regexp = nullptr;
  983. if (! search || search [0] == U'\0') {
  984. return 0;
  985. }
  986. if (columnLabels) {
  987. numberOfLabels = my numberOfColumns;
  988. labels = my columnLabels.peek2();
  989. }
  990. if (use_regexp) {
  991. compiled_regexp = CompileRE_throwable (search, 0);
  992. }
  993. for (integer i = 1; i <= numberOfLabels; i ++) {
  994. if (! labels [i]) {
  995. continue;
  996. }
  997. if (use_regexp) {
  998. if (ExecRE (compiled_regexp, nullptr, labels [i], nullptr, false, U'\0', U'\0', nullptr, nullptr)) {
  999. nmatches ++;
  1000. }
  1001. } else if (str32equ (labels [i], search)) {
  1002. nmatches ++;
  1003. }
  1004. }
  1005. if (use_regexp)
  1006. free (compiled_regexp); // LEAK if ExecRE throws
  1007. return nmatches;
  1008. }
  1009. void TableOfReal_drawVectors (TableOfReal me, Graphics g,
  1010. integer colx1, integer coly1, integer colx2, integer coly2,
  1011. double xmin, double xmax, double ymin, double ymax,
  1012. int vectype, int labelsize, bool garnish)
  1013. {
  1014. integer nx = my numberOfColumns, ny = my numberOfRows;
  1015. int fontsize = Graphics_inqFontSize (g);
  1016. Melder_require (colx1 > 0 && colx1 <= nx && coly1 > 0 && coly1 <= nx,
  1017. U"The index in the \"From\" column(s) should be in range [1, ", nx, U"].");
  1018. Melder_require (colx2 > 0 && colx2 <= nx && coly2 > 0 && coly2 <= nx,
  1019. U"The index in the \"To\" column(s) should be in range [1, ", nx, U"].");
  1020. double min, max;
  1021. if (xmin >= xmax) {
  1022. NUMmatrix_extrema (my data.at, 1, ny, colx1, colx1, & min, &max);
  1023. NUMmatrix_extrema (my data.at, 1, ny, colx2, colx2, & xmin, &xmax);
  1024. if (min < xmin) {
  1025. xmin = min;
  1026. }
  1027. if (max > xmax) {
  1028. xmax = max;
  1029. }
  1030. }
  1031. if (ymin >= ymax) {
  1032. NUMmatrix_extrema (my data.at, 1, ny, coly1, coly1, & min, & max);
  1033. NUMmatrix_extrema (my data.at, 1, ny, coly2, coly2, & ymin, & ymax);
  1034. if (min < ymin) {
  1035. ymin = min;
  1036. }
  1037. if (max > ymax) {
  1038. ymax = max;
  1039. }
  1040. }
  1041. if (xmin == xmax) {
  1042. if (ymin == ymax) {
  1043. return;
  1044. }
  1045. xmin -= 0.5;
  1046. xmax += 0.5;
  1047. }
  1048. if (ymin == ymax) {
  1049. ymin -= 0.5;
  1050. ymax += 0.5;
  1051. }
  1052. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  1053. Graphics_setInner (g);
  1054. Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF);
  1055. if (labelsize > 0) {
  1056. Graphics_setFontSize (g, labelsize);
  1057. }
  1058. for (integer i = 1; i <= ny; i ++) {
  1059. double x1 = my data [i] [colx1];
  1060. double y1 = my data [i] [coly1];
  1061. double x2 = my data [i] [colx2];
  1062. double y2 = my data [i] [coly2];
  1063. conststring32 mark = EMPTY_STRING (my rowLabels [i]) ? U"" : my rowLabels [i].get();
  1064. if (vectype == Graphics_LINE) {
  1065. Graphics_line (g, x1, y1, x2, y2);
  1066. } else if (vectype == Graphics_TWOWAYARROW) {
  1067. Graphics_arrow (g, x1, y1, x2, y2);
  1068. Graphics_arrow (g, x2, y2, x1, y1);
  1069. } else { /*if (vectype == Graphics_ARROW) */
  1070. Graphics_arrow (g, x1, y1, x2, y2);
  1071. }
  1072. if (labelsize > 0) {
  1073. Graphics_text (g, x1, y1, mark);
  1074. }
  1075. }
  1076. if (labelsize > 0) {
  1077. Graphics_setFontSize (g, fontsize);
  1078. }
  1079. Graphics_unsetInner (g);
  1080. if (garnish) {
  1081. Graphics_drawInnerBox (g);
  1082. Graphics_marksLeft (g, 2, true, true, false);
  1083. Graphics_marksBottom (g, 2, true, true, false);
  1084. }
  1085. }
  1086. void TableOfReal_drawColumnAsDistribution (TableOfReal me, Graphics g, integer column, double minimum, double maximum, integer nBins, double freqMin, double freqMax, bool cumulative, bool garnish) {
  1087. if (column < 1 || column > my numberOfColumns) {
  1088. return;
  1089. }
  1090. autoMatrix thee = TableOfReal_to_Matrix (me);
  1091. Matrix_drawDistribution (thee.get(), g, column - 0.5, column + 0.5, 0.0, 0.0, minimum, maximum, nBins, freqMin, freqMax, cumulative, garnish);
  1092. if (garnish && my columnLabels [column]) {
  1093. Graphics_textBottom (g, true, my columnLabels [column].get());
  1094. }
  1095. }
  1096. autoTableOfReal TableOfReal_sortRowsByIndex (TableOfReal me, constINTVEC index, bool reverse) {
  1097. try {
  1098. Melder_require (my rowLabels, U"No labels to sort");
  1099. double min, max;
  1100. NUMvector_extrema (index.at, 1, my numberOfRows, & min, & max);
  1101. Melder_require (min > 0 && min <= my numberOfRows && max > 0 && max <= my numberOfRows,
  1102. U"One or more indices out of range [1, ", my numberOfRows, U"].");
  1103. autoTableOfReal thee = TableOfReal_create (my numberOfRows, my numberOfColumns);
  1104. for (integer i = 1; i <= my numberOfRows; i ++) {
  1105. integer myindex = reverse ? i : index [i];
  1106. integer thyindex = reverse ? index [i] : i;
  1107. conststring32 mylabel = my rowLabels [myindex].get();
  1108. double *mydata = my data [myindex];
  1109. double *thydata = thy data [thyindex];
  1110. thy rowLabels [i] = Melder_dup (mylabel);
  1111. for (integer j = 1; j <= my numberOfColumns; j ++) {
  1112. thydata [j] = mydata [j];
  1113. }
  1114. }
  1115. thy columnLabels. copyElementsFrom (my columnLabels.get());
  1116. return thee;
  1117. } catch (MelderError) {
  1118. Melder_throw (me, U": not sorted by row index.");
  1119. }
  1120. }
  1121. autoINTVEC TableOfReal_getSortedIndexFromRowLabels (TableOfReal me) {
  1122. try {
  1123. return NUMindexx_s (my rowLabels.get());
  1124. } catch (MelderError) {
  1125. Melder_throw (me, U": no sorted index created.");
  1126. }
  1127. }
  1128. autoTableOfReal TableOfReal_sortOnlyByRowLabels (TableOfReal me) {
  1129. try {
  1130. autoPermutation index = TableOfReal_to_Permutation_sortRowLabels (me);
  1131. autoTableOfReal thee = TableOfReal_Permutation_permuteRows (me, index.get());
  1132. return thee;
  1133. } catch (MelderError) {
  1134. Melder_throw (me, U": not sorted by row labels.");
  1135. }
  1136. }
  1137. static void NUMmedianizeColumns (double **a, integer rb, integer re, integer cb, integer ce) {
  1138. integer n = re - rb + 1;
  1139. if (n < 2)
  1140. return;
  1141. autoVEC tmp = VECzero (n);
  1142. for (integer j = cb; j <= ce; j ++) {
  1143. integer k = 1;
  1144. for (integer i = rb; i <= re; i ++, k ++)
  1145. tmp [k] = a [i] [j];
  1146. VECsort_inplace (tmp.get());
  1147. double median = NUMquantile (tmp.get(), 0.5);
  1148. for (integer i = rb; i <= re; i ++)
  1149. a [i] [j] = median;
  1150. }
  1151. }
  1152. static void NUMstatsColumns (double **a, integer rb, integer re, integer cb, integer ce, bool useMedians) {
  1153. if (useMedians) {
  1154. NUMmedianizeColumns (a, rb, re, cb, ce);
  1155. } else {
  1156. NUMaverageColumns (a, rb, re, cb, ce);
  1157. }
  1158. }
  1159. autoTableOfReal TableOfReal_meansByRowLabels (TableOfReal me, bool expand, bool useMedians) {
  1160. try {
  1161. autoTableOfReal thee;
  1162. autoINTVEC index = TableOfReal_getSortedIndexFromRowLabels (me);
  1163. autoTableOfReal sorted = TableOfReal_sortRowsByIndex (me, index.get(), false);
  1164. integer indexi = 1, indexr = 0;
  1165. conststring32 label = sorted -> rowLabels [1].get();
  1166. for (integer i = 2; i <= my numberOfRows; i ++) {
  1167. conststring32 li = sorted -> rowLabels [i].get();
  1168. if (Melder_cmp (li, label) != 0) {
  1169. NUMstatsColumns (sorted -> data.at, indexi, i - 1, 1, my numberOfColumns, useMedians);
  1170. if (! expand) {
  1171. indexr ++;
  1172. TableOfReal_copyOneRowWithLabel (sorted.get(), sorted.get(), indexi, indexr);
  1173. }
  1174. label = li; indexi = i;
  1175. }
  1176. }
  1177. NUMstatsColumns (sorted -> data.at, indexi, my numberOfRows, 1, my numberOfColumns, useMedians);
  1178. if (expand) {
  1179. // Now invert the table.
  1180. autostring32vector tmp = std::move (sorted -> rowLabels);
  1181. sorted -> rowLabels = std::move (my rowLabels);
  1182. thee = TableOfReal_sortRowsByIndex (sorted.get(), index.get(), true);
  1183. sorted -> rowLabels = std::move (tmp);
  1184. } else {
  1185. indexr ++;
  1186. TableOfReal_copyOneRowWithLabel (sorted.get(), sorted.get(), indexi, indexr);
  1187. thee = TableOfReal_create (indexr, my numberOfColumns);
  1188. for (integer i = 1; i <= indexr; i ++) {
  1189. TableOfReal_copyOneRowWithLabel (sorted.get(), thee.get(), i, i);
  1190. }
  1191. thy columnLabels. copyElementsFrom (sorted -> columnLabels.get());
  1192. }
  1193. return thee;
  1194. } catch (MelderError) {
  1195. Melder_throw (me, U": means by row labels not created.");
  1196. }
  1197. }
  1198. autoTableOfReal TableOfReal_rankColumns (TableOfReal me) {
  1199. try {
  1200. autoTableOfReal thee = Data_copy (me);
  1201. NUMrankColumns (thy data.at, 1, thy numberOfRows, 1, thy numberOfColumns);
  1202. return thee;
  1203. } catch (MelderError) {
  1204. Melder_throw (me, U": column ranks not created.");
  1205. }
  1206. }
  1207. /*
  1208. s[lo] = precursor<number>
  1209. s[lo+1] = precursor<number+1>
  1210. ...
  1211. s[hi] = precursor<number+hi-lo>
  1212. */
  1213. void TableOfReal_setSequentialColumnLabels (TableOfReal me, integer from, integer to, conststring32 precursor, integer number, integer increment) {
  1214. from = ( from == 0 ? 1 : from );
  1215. to = ( to == 0 ? my numberOfColumns : to );
  1216. Melder_require (from > 0 && from <= to && to <= my numberOfColumns,
  1217. U"Wrong column indices.");
  1218. for (integer i = from; i <= to; i ++, number += increment)
  1219. my columnLabels [i] = Melder_dup (Melder_cat (precursor, number));
  1220. }
  1221. void TableOfReal_setSequentialRowLabels (TableOfReal me, integer from, integer to, conststring32 precursor, integer number, integer increment) {
  1222. from = ( from == 0 ? 1 : from );
  1223. to = ( to == 0 ? my numberOfRows : to );
  1224. Melder_require (from > 0 && from <= to && to <= my numberOfRows,
  1225. U"Wrong row indices.");
  1226. for (integer i = from; i <= to; i ++, number += increment)
  1227. my rowLabels [i] = Melder_dup (Melder_cat (precursor, number));
  1228. }
  1229. /* For the inheritors */
  1230. autoTableOfReal TableOfReal_to_TableOfReal (TableOfReal me) {
  1231. try {
  1232. autoTableOfReal thee = TableOfReal_create (my numberOfRows, my numberOfColumns);
  1233. matrixcopy_preallocated (thy data.get(), my data.get());
  1234. TableOfReal_copyLabels (me, thee.get(), 1, 1);
  1235. return thee;
  1236. } catch (MelderError) {
  1237. Melder_throw (me, U": not copied.");
  1238. }
  1239. }
  1240. autoTableOfReal TableOfReal_choleskyDecomposition (TableOfReal me, bool upper, bool inverse) {
  1241. try {
  1242. char diag = 'N';
  1243. integer n = my numberOfColumns, lda = my numberOfRows, info;
  1244. Melder_require (n == lda,
  1245. U"The table should be a square symmetric table.");
  1246. autoTableOfReal thee = Data_copy (me);
  1247. if (upper) {
  1248. for (integer i = 2; i <= n; i ++) {
  1249. for (integer j = 1; j < i; j ++) {
  1250. thy data [i] [j] = 0.0;
  1251. }
  1252. }
  1253. } else {
  1254. for (integer i = 1; i < n; i ++) {
  1255. for (integer j = i + 1; j <= n; j ++) {
  1256. thy data [i] [j] = 0.0;
  1257. }
  1258. }
  1259. }
  1260. char uplo = upper ? 'L' : 'U';
  1261. NUMlapack_dpotf2 (& uplo, & n, & thy data [1] [1], & lda, & info);
  1262. Melder_require (info == 0, U"dpotf2 fails");
  1263. if (inverse) {
  1264. NUMlapack_dtrtri (&uplo, &diag, &n, &thy data [1] [1], &lda, &info);
  1265. Melder_require (info == 0, U"dtrtri fails");
  1266. }
  1267. return thee;
  1268. } catch (MelderError) {
  1269. Melder_throw (me, U": Cholesky decomposition not performed.");
  1270. }
  1271. }
  1272. autoTableOfReal TableOfReal_appendColumns (TableOfReal me, TableOfReal thee) {
  1273. try {
  1274. integer ncols = my numberOfColumns + thy numberOfColumns;
  1275. integer labeldiffs = 0;
  1276. Melder_require (my numberOfRows == thy numberOfRows,
  1277. U"The numbers of rows should be equal.");
  1278. /* Stricter label checking???
  1279. append only if
  1280. (my rowLabels [i] == thy rowlabels [i], i=1..my numberOfRows) or
  1281. (my rowLabels [i] == 'empty', i=1..my numberOfRows) or
  1282. (thy rowLabels [i] == 'empty', i=1..my numberOfRows);
  1283. 'empty': nullptr or \w*
  1284. */
  1285. autoTableOfReal him = TableOfReal_create (my numberOfRows, ncols);
  1286. his rowLabels. copyElementsFrom (my rowLabels.get());
  1287. his columnLabels. copyElementsFrom_upTo (my columnLabels.get(), my numberOfColumns);
  1288. for (integer icol = 1; icol <= thy numberOfColumns; icol ++)
  1289. his columnLabels [my numberOfColumns + icol] = Melder_dup (thy columnLabels [icol].get());
  1290. for (integer i = 1; i <= my numberOfRows; i ++) {
  1291. if (! Melder_equ (my rowLabels [i].get(), thy rowLabels [i].get())) {
  1292. labeldiffs ++;
  1293. }
  1294. NUMvector_copyElements (my data [i], his data [i], 1, my numberOfColumns);
  1295. NUMvector_copyElements (thy data [i], &his data [i] [my numberOfColumns], 1, thy numberOfColumns);
  1296. }
  1297. if (labeldiffs > 0) {
  1298. Melder_warning (labeldiffs, U" row labels differed.");
  1299. }
  1300. return him;
  1301. } catch (MelderError) {
  1302. Melder_throw (U"TableOfReal with appended columns not created.");
  1303. }
  1304. }
  1305. autoTableOfReal TableOfRealList_appendColumnsMany (TableOfRealList me) {
  1306. try {
  1307. Melder_require (my size > 0, U"No tables selected.");
  1308. TableOfReal thee = my at [1];
  1309. integer nrows = thy numberOfRows;
  1310. integer ncols = thy numberOfColumns;
  1311. for (integer itab = 2; itab <= my size; itab ++) {
  1312. thee = my at [itab];
  1313. ncols += thy numberOfColumns;
  1314. Melder_require (thy numberOfRows == nrows,
  1315. U"Numbers of rows in item ", itab, U" differs from previous.");
  1316. }
  1317. autoTableOfReal him = TableOfReal_create (nrows, ncols);
  1318. /* Unsafe: new attributes not initialized. */
  1319. for (integer irow = 1; irow <= nrows; irow ++) {
  1320. TableOfReal_setRowLabel (him.get(), irow, thy rowLabels [irow].get());
  1321. }
  1322. ncols = 0;
  1323. for (integer itab = 1; itab <= my size; itab ++) {
  1324. thee = my at [itab];
  1325. for (integer icol = 1; icol <= thy numberOfColumns; icol ++) {
  1326. ncols ++;
  1327. TableOfReal_setColumnLabel (him.get(), ncols, thy columnLabels [icol].get());
  1328. for (integer irow = 1; irow <= nrows; irow ++) {
  1329. his data [irow] [ncols] = thy data [irow] [icol];
  1330. }
  1331. }
  1332. }
  1333. Melder_assert (ncols == his numberOfColumns);
  1334. return him;
  1335. } catch (MelderError) {
  1336. Melder_throw (U"TableOfReal with appended columns not created.");
  1337. }
  1338. }
  1339. double TableOfReal_normalityTest_BHEP (TableOfReal me, double *h, double *p_tnb, double *p_lnmu, double *p_lnvar) {
  1340. try {
  1341. /* Henze & Wagner (1997), A new approach to the BHEP tests for multivariate normality,
  1342. * Journal of Multivariate Analysis 62, 1-23.
  1343. */
  1344. integer n = my numberOfRows, p = my numberOfColumns;
  1345. double beta = *h > 0.0 ? NUMsqrt1_2 / *h : NUMsqrt1_2 * pow ((1.0 + 2 * p) / 4, 1.0 / (p + 4)) * pow (n, 1.0 / (p + 4));
  1346. double p2 = p / 2.0;
  1347. double beta2 = beta * beta, beta4 = beta2 * beta2, beta8 = beta4 * beta4;
  1348. double gamma = 1.0 + 2.0 * beta2, gamma2 = gamma * gamma, gamma4 = gamma2 * gamma2;
  1349. double delta = 1.0 + beta2 * (4.0 + 3.0 * beta2), delta2 = delta * delta;
  1350. double prob = undefined;
  1351. if (*h <= 0.0) {
  1352. *h = NUMsqrt1_2 / beta;
  1353. }
  1354. double tnb = undefined, lnmu = undefined, lnvar = undefined;
  1355. if (n < 2 || p < 1) {
  1356. return undefined;
  1357. }
  1358. autoCovariance thee = TableOfReal_to_Covariance (me);
  1359. try {
  1360. SSCP_expandLowerCholesky (thee.get());
  1361. } catch (MelderError) {
  1362. tnb = 4.0 * n;
  1363. }
  1364. {
  1365. double djk, djj, sumjk = 0.0, sumj = 0.0;
  1366. double b1 = beta2 / 2.0, b2 = b1 / (1.0 + beta2);
  1367. /* Heinze & Wagner (1997), page 3
  1368. We use d [j] [k] = ||Y [j]-Y [k]||^2 = (Y [j]-Y [k])'S^(-1)(Y [j]-Y [k])
  1369. So d [j] [k]= d [k] [j] and d [j] [j] = 0
  1370. */
  1371. for (integer j = 1; j <= n; j ++) {
  1372. for (integer k = 1; k < j; k ++) {
  1373. djk = NUMmahalanobisDistance (thy lowerCholesky.get(), my data.row(j), my data.row(k));
  1374. sumjk += 2.0 * exp (-b1 * djk); // factor 2 because d [j] [k] == d [k] [j]
  1375. }
  1376. sumjk += 1; // for k == j
  1377. djj = NUMmahalanobisDistance (thy lowerCholesky.get(), my data.row(j), thy centroid.get());
  1378. sumj += exp (-b2 * djj);
  1379. }
  1380. tnb = (1.0 / n) * sumjk - 2.0 * pow (1.0 + beta2, - p2) * sumj + n * pow (gamma, - p2); // n *
  1381. }
  1382. double mu = 1.0 - pow (gamma, -p2) * (1.0 + p * beta2 / gamma + p * (p + 2) * beta4 / (2.0 * gamma2));
  1383. double var = 2.0 * pow (1.0 + 4.0 * beta2, -p2)
  1384. + 2.0 * pow (gamma, -p) * (1.0 + 2 * p * beta4 / gamma2 + 3 * p * (p + 2) * beta8 / (4.0 * gamma4))
  1385. - 4.0 * pow (delta, -p2) * (1.0 + 3 * p * beta4 / (2.0 * delta) + p * (p + 2) * beta8 / (2.0 * delta2));
  1386. double mu2 = mu * mu;
  1387. lnmu = 0.5 * log (mu2 * mu2 / (mu2 + var)); //log (sqrt (mu2 * mu2 /(mu2 + var)));
  1388. lnvar = sqrt (log ( (mu2 + var) / mu2));
  1389. prob = NUMlogNormalQ (tnb, lnmu, lnvar);
  1390. if (p_tnb) {
  1391. *p_tnb = tnb;
  1392. }
  1393. if (p_lnmu) {
  1394. *p_lnmu = lnmu;
  1395. }
  1396. if (p_lnvar) {
  1397. *p_lnvar = lnvar;
  1398. }
  1399. return prob;
  1400. } catch (MelderError) {
  1401. Melder_throw (me, U": cannot determine normality.");
  1402. }
  1403. }
  1404. autoTableOfReal TableOfReal_TableOfReal_crossCorrelations (TableOfReal me, TableOfReal thee, bool by_columns, bool center, bool normalize) {
  1405. return by_columns ? TableOfReal_TableOfReal_columnCorrelations (me, thee, center, normalize) :
  1406. TableOfReal_TableOfReal_rowCorrelations (me, thee, center, normalize);
  1407. }
  1408. autoTableOfReal TableOfReal_TableOfReal_rowCorrelations (TableOfReal me, TableOfReal thee, bool centre, bool normalize) {
  1409. try {
  1410. if (my numberOfColumns != thy numberOfColumns)
  1411. Melder_throw (U"Both tables should have the same number of columns.");
  1412. autoTableOfReal him = TableOfReal_create (my numberOfRows, thy numberOfRows);
  1413. autoMAT my_data = MATcopy (my data.get());
  1414. autoMAT thy_data = MATcopy (thy data.get());
  1415. if (centre) {
  1416. MATcentreEachRow_inplace (my_data.get());
  1417. MATcentreEachRow_inplace (thy_data.get());
  1418. }
  1419. if (normalize) {
  1420. MATnormalizeRows_inplace (my_data.get(), 2.0, 1.0);
  1421. MATnormalizeRows_inplace (thy_data.get(), 2.0, 1.0);
  1422. }
  1423. his rowLabels. copyElementsFrom (my rowLabels.get());
  1424. his columnLabels. copyElementsFrom (thy rowLabels.get());
  1425. for (integer i = 1; i <= my numberOfRows; i ++) {
  1426. for (integer k = 1; k <= thy numberOfRows; k ++) {
  1427. longdouble ctmp = 0.0;
  1428. for (integer j = 1; j <= my numberOfColumns; j ++)
  1429. ctmp += my_data [i] [j] * thy_data [k] [j];
  1430. his data [i] [k] = (double) ctmp;
  1431. }
  1432. }
  1433. return him;
  1434. } catch (MelderError) {
  1435. Melder_throw (U"TableOfReal with row correlations not created.");
  1436. }
  1437. }
  1438. autoTableOfReal TableOfReal_TableOfReal_columnCorrelations (TableOfReal me, TableOfReal thee, bool center, bool normalize) {
  1439. try {
  1440. if (my numberOfRows != thy numberOfRows)
  1441. Melder_throw (U"Both tables should have the same number of rows.");
  1442. autoTableOfReal him = TableOfReal_create (my numberOfColumns, thy numberOfColumns);
  1443. autoMAT my_data = MATcopy (my data.get());
  1444. autoMAT thy_data = MATcopy (thy data.get());
  1445. if (center) {
  1446. MATcentreEachColumn_inplace (my_data.get());
  1447. MATcentreEachColumn_inplace (thy_data.get());
  1448. }
  1449. if (normalize) {
  1450. MATnormalizeColumns_inplace (my_data.get(), 2.0, 1.0);
  1451. MATnormalizeColumns_inplace (thy_data.get(), 2.0, 1.0);
  1452. }
  1453. his rowLabels. copyElementsFrom (my columnLabels.get());
  1454. his columnLabels. copyElementsFrom (thy columnLabels.get());
  1455. for (integer j = 1; j <= my numberOfColumns; j ++) {
  1456. for (integer k = 1; k <= thy numberOfColumns; k ++) {
  1457. longdouble sum = 0.0;
  1458. for (integer i = 1; i <= my numberOfRows; i ++) {
  1459. sum += my_data [i] [j] * thy_data [i] [k];
  1460. }
  1461. his data [j] [k] = (double) sum;
  1462. }
  1463. }
  1464. return him;
  1465. } catch (MelderError) {
  1466. Melder_throw (U"TableOfReal with column correlations not created.");
  1467. }
  1468. }
  1469. autoMatrix TableOfReal_to_Matrix_interpolateOnRectangularGrid (TableOfReal me, double xmin, double xmax, double nx, double ymin, double ymax, integer ny, int /* method */) {
  1470. try {
  1471. if (my numberOfColumns < 3 || my numberOfRows < 3)
  1472. Melder_throw (U"There should be at least three colums and three rows.");
  1473. autoVEC x = VECraw (my numberOfRows);
  1474. autoVEC y = VECraw (my numberOfRows);
  1475. autoVEC z = VECraw (my numberOfRows);
  1476. for (integer irow = 1; irow <= my numberOfRows; irow ++) {
  1477. x [irow] = my data [irow] [1];
  1478. y [irow] = my data [irow] [2];
  1479. z [irow] = my data [irow] [3];
  1480. }
  1481. autoVEC weights = NUMbiharmonic2DSplineInterpolation_getWeights (x.get(), y.get(), z.get());
  1482. double dx = (xmax - xmin) / nx, dy = (ymax - ymin) / ny;
  1483. autoMatrix thee = Matrix_create (xmin, xmax, nx, dx, xmin + 0.5 * dx,
  1484. ymin, ymax, ny, dy, ymin + 0.5 * dy);
  1485. for (integer irow = 1; irow <= ny; irow ++) {
  1486. double yp = thy y1 + (irow - 1) * dy;
  1487. for (integer icol = 1; icol <= nx; icol ++) {
  1488. double xp = thy x1 + (icol - 1) * dx;
  1489. thy z [irow] [icol] = NUMbiharmonic2DSplineInterpolation (x.get(), y.get(), weights.get(), xp, yp);
  1490. }
  1491. }
  1492. return thee;
  1493. } catch (MelderError) {
  1494. Melder_throw (me, U": interpolation not finished.");
  1495. }
  1496. }
  1497. #undef EMPTY_STRING
  1498. /* End of file TableOfReal_extensions.c 1869*/