test_SVD.praat 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # test_SVD.praat
  2. # djmw 20171208
  3. appendInfoLine: "test_SVD.praat"
  4. n = 4
  5. eps = 1.5e-5
  6. appendInfoLine: tab$, "Hilbert matrix order 4"
  7. t = Create TableOfReal: "hilbert", n, n
  8. for i to n
  9. for j to n
  10. Set value: i, j, 1 / (i + j - 1)
  11. endfor
  12. endfor
  13. svd = To SVD
  14. utab = Extract left singular vectors
  15. selectObject: svd
  16. vtab = Extract right singular vectors
  17. selectObject: svd
  18. dvec = Extract singular values
  19. u$= " -0.792608 0.582076 -0.179186 -0.029193 "
  20. ... + " -0.451923 -0.370502 0.741918 0.328712 "
  21. ... + " -0.322416 -0.509579 -0.100228 -0.791411 "
  22. ... + " -0.252161 -0.514048 -0.638283 0.514553 "
  23. v$ = "-0.792608 0.582076 -0.179186 -0.029193 "
  24. ... + " -0.451923 -0.370502 0.741918 0.328712 "
  25. ... + " -0.322416 -0.509579 -0.100228 -0.791411 "
  26. ... + " -0.252161 -0.514048 -0.638283 0.514553"
  27. d# = {1.5002, 1.6914e-01, 6.7383e-03, 9.6702e-05}
  28. appendInfoLine: tab$, tab$, "UDV' test equality of U"
  29. @string_to_table: u$, n, n
  30. utab_octave = selected ("TableOfReal")
  31. appendInfoLine: tab$, tab$, "UDV' test equality of V"
  32. @string_to_table: v$, n, n
  33. vtab_octave = selected ("TableOfReal")
  34. @check_tors: utab, utab_octave, eps
  35. @check_tors: vtab, vtab_octave, eps
  36. removeObject: utab, utab_octave, vtab, vtab_octave, svd, t, dvec
  37. appendInfoLine: tab$, "reconstruct 6x2 matrix"
  38. @test_reconstruction: 6, 2, eps
  39. appendInfoLine: tab$, "reconstruct 2x6 matrix"
  40. @test_reconstruction: 2, 6, eps
  41. appendInfoLine: tab$, "reconstruct 30x500 matrix"
  42. @test_reconstruction: 30, 500, eps
  43. appendInfoLine: "test_SVD.praat OK"
  44. procedure test_reconstruction: .nrows, .ncols, .eps
  45. .t = Create TableOfReal: "t", .nrows, .ncols
  46. Formula: "randomUniform (-1,1)"
  47. .svd = To SVD
  48. .tr = To TableOfReal: 1, 0
  49. @check_tors: .t, .tr, .eps
  50. removeObject: .t, .svd, .tr
  51. endproc
  52. procedure check_tors: .tor1, .tor2, .eps
  53. selectObject: .tor1
  54. .nrows = Get number of rows
  55. .ncols = Get number of columns
  56. for .i to .nrows
  57. for .j to .ncols
  58. .d =abs ((object[.tor1, .i, .j]-object[.tor2, .i, .j])/object[.tor2, .i, .j])
  59. assert .d < .eps; ['.i','.j']: '.tor1', '.tor2', '.d'
  60. endfor
  61. endfor
  62. endproc
  63. procedure string_to_table: .string$, .nrows, .ncols
  64. .s = Create Strings as tokens: .string$
  65. .numberOfTokens = Get number of strings
  66. assert .numberOfTokens == .nrows * .ncols
  67. .t = Create TableOfReal: "t", .nrows, .ncols
  68. for .i to .nrows
  69. for .j to .ncols
  70. selectObject: .s
  71. .val$ = Get string: (.i-1)*.ncols + .j
  72. selectObject: .t
  73. Set value: .i, .j, number (.val$)
  74. endfor
  75. endfor
  76. removeObject: .s
  77. selectObject: .t
  78. endproc