test_TableOfReal_extensions.praat 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # test_TableOfReal_extensions.praat
  2. # djmw 20151023, 20180911
  3. eps = 2.3e-16
  4. appendInfoLine: "test_TableOfReal_extensions.praat"
  5. @means_by_row_labels
  6. @centre_rows
  7. @centre_columns
  8. @normalize
  9. appendInfoLine: "test_TableOfReal_extensions.praat OK"
  10. procedure means_by_row_labels
  11. appendInfoLine: tab$, "means_by_row_labels"
  12. .nrows = 100
  13. .ncols = 3
  14. .tab1 = Create TableOfReal: "tab", .nrows, .ncols
  15. for .i to .nrows
  16. if (.i mod 2) = 0
  17. Set row label (index): .i, "even"
  18. else
  19. Set row label (index): .i, "odd"
  20. endif
  21. endfor
  22. Formula: ~ if (row mod 2) = 0 then col else 2*col fi
  23. .tabm = To TableOfReal (means by row labels): "no"
  24. .nrowsm = Get number of rows
  25. .ncolsm = Get number of columns
  26. assert .nrowsm = 2
  27. assert .ncolsm = .ncols
  28. for .i to .nrowsm
  29. .rowLabel$ = Get row label: .i
  30. for .j to .ncols
  31. if .rowLabel$ = "even"
  32. .dif = abs (object [.tabm, .i, .j] - .j)
  33. else
  34. .dif = abs (object [.tabm, .i, .j] - .j*2)
  35. endif
  36. assert .dif <= eps
  37. endfor
  38. endfor
  39. removeObject: .tab1, .tabm
  40. endproc
  41. procedure centre_rows
  42. appendInfoLine: tab$, "centre_rows"
  43. .eps = 3*2.3e-16
  44. .nrow = 20
  45. .ncol = 3
  46. .tab = Create TableOfReal: "tab", .nrow, .ncol
  47. Formula: ~ if col=1 then row else if col = 2 then 0 else -row fi fi
  48. Formula: ~ self+1
  49. Centre rows
  50. for .irow to .nrow
  51. .dif = abs (object [.tab, .irow, 1] - .irow) / .irow
  52. assert .dif < .eps
  53. .dif = abs (object [.tab, .irow, 3] + .irow) / .irow
  54. assert .dif < .eps
  55. assert abs (object [.tab, .irow, 2]) < .eps
  56. endfor
  57. removeObject: .tab
  58. endproc
  59. procedure centre_columns
  60. appendInfoLine: tab$, "centre_columns"
  61. .eps = 3*2.3e-16
  62. .nrow = 3
  63. .ncol = 20
  64. .tab = Create TableOfReal: "tab", .nrow, .ncol
  65. Formula: ~ if row=1 then col else if row = 2 then 0 else -col fi fi
  66. Formula: ~ self+1
  67. Centre columns
  68. for .icol to .ncol
  69. .dif = abs (object [.tab, 1, .icol] - .icol) / .icol
  70. assert .dif < .eps
  71. .dif = abs (object [.tab, 3, .icol] + .icol) / .icol
  72. assert .dif < .eps
  73. assert abs (object [.tab, 2, .icol]) < .eps
  74. endfor
  75. removeObject: .tab
  76. endproc
  77. procedure normalize
  78. # simple check
  79. appendInfoLine: tab$, "normalize"
  80. .nrow = 10
  81. .ncol = 10
  82. .tab = Create TableOfReal: "tab", .nrow, .ncol
  83. Formula: ~ row
  84. .norm = Get table norm
  85. # sum(k,1,n, k^2)=n(n+1)(2n+1)/6
  86. .sumsq_column = .nrow *(.nrow+1)*(2*.nrow+1)/6
  87. .normwanted = sqrt (.ncol*.sumsq_column)
  88. .eps =1e-15
  89. assert 1-.eps < .norm/.normwanted && .norm/.normwanted < 1+.eps; before
  90. .nall = Copy: "all"
  91. Normalize table: 1.0
  92. .norm = Get table norm
  93. assert 1-.eps < .norm && .norm < 1+.eps; after
  94. selectObject: .tab
  95. .rows = Copy: "rows"
  96. Normalize rows: 1.0
  97. .norm = Get table norm
  98. .normwanted = sqrt (10)
  99. assert 1-.eps < .norm/.normwanted && .norm/.normwanted < 1+.eps; rows
  100. selectObject: .tab
  101. Normalize columns: 1.0
  102. .norm = Get table norm
  103. .normwanted = sqrt (10)
  104. assert 1-.eps < .norm/.normwanted && .norm/.normwanted < 1+.eps; rows
  105. removeObject: .tab, .nall, .rows
  106. endproc