| 1 |
# test_Polynomial.praat |
| 2 |
# djmw 20160509 |
| 3 |
|
| 4 |
appendInfoLine: "test_Polynomial.praat" |
| 5 |
for i to 10 |
| 6 |
@test_roots |
| 7 |
endfor |
| 8 |
@test_products |
| 9 |
appendInfoLine: "test_Polynomial.praat OK" |
| 10 |
|
| 11 |
procedure test_roots |
| 12 |
# random polynomials can behave very wildly. Therefore we cannot |
| 13 |
# be too strict in checking the differences between generated and |
| 14 |
# measured roots. |
| 15 |
# Once in a while the precision is not as good as wanted and a notification will be given. |
| 16 |
.eps1 = 2e-6 |
| 17 |
.eps2 = 1e-6 |
| 18 |
appendInfoLine: tab$, "Test roots of polynomial with random coefficients:" |
| 19 |
for .i to 20 |
| 20 |
.xmin = -2 |
| 21 |
.xmax = randomUniform (1, 2) |
| 22 |
.numberOfRoots = randomInteger (2, 20) |
| 23 |
appendInfoLine: tab$, tab$, .i, ": ", .numberOfRoots, " random roots in [",.xmin, ",", .xmax, "]" |
| 24 |
.rootsTable = Create TableOfReal: "r", .numberOfRoots, 1 |
| 25 |
Formula: ~ randomUniform (.xmin, .xmax) |
| 26 |
Sort by column: 1, 0 |
| 27 |
.roots$ = "" |
| 28 |
for .j to .numberOfRoots |
| 29 |
.root [.j] = Get value: .j, 1 |
| 30 |
.roots$ = .roots$ + string$ (.root [.j]) + if .j == .numberOfRoots then "" else " " fi |
| 31 |
endfor |
| 32 |
.p = Create Polynomial from real zeros: "p", .xmin, .xmax, .roots$ |
| 33 |
# divide by a root |
| 34 |
for .j to .numberOfRoots |
| 35 |
.rootj = .root [.j] |
| 36 |
.remainder = Get remainder after division: .rootj |
| 37 |
.test = abs (.remainder / .rootj) |
| 38 |
assert .test < .eps1; '.remainder' '.rootj' |
| 39 |
endfor |
| 40 |
.root [0] = .xmin |
| 41 |
.root [.numberOfRoots + 1] = .xmax |
| 42 |
.eps2 = .numberOfRoots * 1e-6 |
| 43 |
|
| 44 |
for .j to .numberOfRoots |
| 45 |
.xmini = randomUniform (.root [.j-1], .root [.j]) |
| 46 |
.xmaxi = randomUniform (.root [.j], .root [.j+1]) |
| 47 |
.root = Get one real root: .xmini, .xmaxi |
| 48 |
.rootj = .root [.j] |
| 49 |
.dif = abs (.rootj - .root) |
| 50 |
if .dif > .eps2 * abs (.rootj) |
| 51 |
appendInfoLine: tab$, tab$, "Possible precision problem for ", .numberOfRoots, " roots: ", .roots$ |
| 52 |
appendInfoLine: tab$, tab$, .dif, "(= abs(root-rootFound):", .rootj, "-", .root, ") for ", .j, "th root)" |
| 53 |
endif |
| 54 |
;assert .dif < .eps2 * abs (.root [.j]); '.root' '.rootj' '.j' |
| 55 |
endfor |
| 56 |
removeObject: .p, .rootsTable |
| 57 |
endfor |
| 58 |
appendInfoLine: tab$, "Test roots of polynomial with random coefficients: OK" |
| 59 |
endproc |
| 60 |
|
| 61 |
procedure test_products |
| 62 |
appendInfo: tab$, "Test product terms:" |
| 63 |
.eps = 1e-15 |
| 64 |
.p = Create Polynomial from product terms: "p", -3, 3, "1 2 -1 -2" |
| 65 |
.coefs$ = "1 0 -1 0 0 0 -1 0 1" |
| 66 |
.strings = Create Strings as tokens: .coefs$ |
| 67 |
.ntokens = Get number of strings |
| 68 |
for .i to .ntokens |
| 69 |
selectObject: .strings |
| 70 |
.coef$ = Get string: .i |
| 71 |
selectObject: .p |
| 72 |
.coef = Get coefficient: .i |
| 73 |
assert abs (number (.coef$) - .coef) < .eps; '.coef' '.coef$' |
| 74 |
endfor |
| 75 |
removeObject: .p, .strings |
| 76 |
appendInfoLine: " OK" |
| 77 |
endproc |
| 78 |
|