Debian Codegraph @

dwtest/test_Matrix_solve.praat

raw
1 # test_Matrix_solve.praat
2 # djmw 20031020, 20171211,20180918,20100406, 20240708
3
4 appendInfoLine: "test_Matrix_solve.praat"
5
6 @solve_undetermined: 10, 100
7 @solve3x3
8
9 appendInfoLine: tab$, "Solve sparse systems"
10 for i to 200
11 @solve_sparse_system
12 endfor
13 appendInfoLine: tab$, "Solve sparse systems: OK"
14
15 @solve2by3
16 @matrix_solve: 1
17 @matrix_solve: 10
18 @matrix_solve: 100
19
20 procedure solve_sparse_system
21 .nrow = 100
22 .ncol = 1000
23 .x# = zero# (.ncol)
24 .numberOfNonZeros = 0
25 for .i to size (.x#)
26 .x# [.i] = 0.0
27 if randomUniform (0,1) < 0.005
28 .x# [.i] = randomUniform (0.1, 10)
29 .numberOfNonZeros += 1;
30 endif
31 endfor
32 if .numberOfNonZeros > 0
33 .phi## = randomGauss## (.nrow, .ncol, 0.0, 1.0 / .nrow)
34 .y# = mul# (.phi##, .x#)
35 .numberOfNonzerosToSearch = .numberOfNonZeros + 5
36 appendInfoLine: tab$, tab$, "Sparse 100x100: solve for ", .numberOfNonZeros, " non zero elements."
37 .xs# = solveSparse# (.phi##, .y#, .numberOfNonzerosToSearch, 200, 1e-17, 0) ; 6 arguments
38 .dif# = .x# - .xs#
39 .inner = inner (.dif#, .dif#)
40 assert .inner < 1e-7; '.inner'
41 .xs2# = solveSparse# (.phi##, .y#, .xs#, .numberOfNonzerosToSearch, 10, 1e-20, 1) ; 7 arguments
42 .dif# = .x# - .xs2#
43 .inner = inner (.dif#, .dif#)
44 assert .inner < 1e-7; '.inner'
45 endif
46 endproc
47
48 procedure matrix_solve: .ncol
49 appendInfoLine: tab$, "Solve equation:"
50 for .i to 4
51 .nrow = .i * .ncol
52 appendInfoLine: tab$, tab$, "nrow = ", .nrow, ", ncol = ", .ncol
53 .eps = .nrow * 1e-7
54 .m = Create simple Matrix: string$(.i), .nrow, .ncol+1, "0.0"
55 Formula: "if (col <= ((row - 1) mod .ncol)+1) then 1 else 0 fi"
56 Formula: "if col = 1 then self + ((row-1) div .ncol) else self fi"
57 Formula: "if col = .ncol+1 then (row-1) mod .ncol + 1 + ((row-1) div .ncol) else self fi"
58
59 .ms = Solve equation: 0
60 # solution must be all ones.
61 for .j to .ncol
62 .s = Get value in cell: 1, .j
63 assert abs(.s - 1.0) < .eps ; nowsr = '.nrow' ncols = '.ncol'
64 endfor
65 removeObject: .m, .ms
66 endfor
67 appendInfoLine: tab$, "Solve equation: OK"
68
69 endproc
70
71 procedure solve_undetermined: .nrow, .ncol
72 appendInfoLine: tab$, "underdetermined system"
73 .m = Create simple Matrix: "u", .nrow, .ncol, "if row==col then 1 else 0 fi"
74 Formula: "if col==row+1 then 1 else self fi"
75 Formula: "if col == .ncol then if row == .nrow then 1 else 2 fi else self fi"
76 .ms = nowarn Solve equation: 1e-7
77 .ncols = Get number of columns
78 assert .ncols == .ncol - 1
79 for .irow to .nrow
80 .c = Get value in cell: 1, .irow
81 assert .c > 1-1e-7 and .c < 1+1e-7
82 endfor
83 removeObject: .m, .ms
84 endproc
85
86 # test for several dimensions
87
88 procedure solve2by3
89 appendInfoLine: tab$, "Solve 2x3:"
90 .nrow = 2
91 .ncol = 3
92 appendInfoLine: tab$, tab$, "nrow = ", .nrow, ", ncol = ", .ncol
93 .m = Create simple Matrix: "2x3", .nrow, .ncol, "0"
94 Set value: 1, 1, 1
95 Set value: 1, 2, 2
96 Set value: 1, 3, 3
97 Set value: 2, 1, 3
98 Set value: 2, 2, 5
99 Set value: 2, 3, 8
100 .eps = 1.2e-7
101 .solution = Solve equation: .eps
102 .s1 = Get value in cell: 1, 1
103 .s2 = Get value in cell: 1, 2
104 assert .s1-.eps < 1 and .s1+.eps > 1; '.s1'
105 assert .s2-.eps < 1 and .s2+.eps > 1; '.s2'
106 removeObject: .m, .solution
107 appendInfoLine: tab$, "Solve 2x3: OK"
108 endproc
109
110 procedure solve3x3
111 appendInfoLine: tab$, "Solve 3x3:"
112 ; a * x = b
113 .a## = {{1,2,3}, {1,5,7},{2,3,5}}
114 .x## = {{1,12},{2,20},{3,30}}
115 .b## ={{14,142},{32,322},{23,234}}
116 .a = Create simple Matrix: "1", 3, 4, "0"
117 for .irow to 3
118 for .icol to 3
119 Set value: .irow, .icol, .a## [.irow, .icol]
120 endfor
121 Set value: .irow, 4, .b## [.irow, 1]
122 endfor
123 .xx = Solve equation: 0
124 .eps = 1e-9
125 for .irow to 3
126 .dif = abs (object [.xx, 1, .irow] - .x## [.irow, 1])
127 assert .dif < .eps; '.dif'< '.eps'
128 endfor
129 appendInfoLine: tab$, tab$, "A[3x3] * X[3x2] = B[3x2]"
130 removeObject: .a, .xx
131 .a = Create simple Matrix: "1", 3, 3, "0"
132 for .irow to 3
133 for .icol to 3
134 Set value: .irow, .icol, .a## [.irow, .icol]
135 endfor
136 endfor
137 .b = Create simple Matrix: "1", 3, 2, "0"
138 for .irow to 3
139 for .icol to 2
140 Set value: .irow, .icol, .b## [.irow, .icol]
141 endfor
142 endfor
143 selectObject: .a, .b
144 .xx = Solve matrix equation: 1e-7
145 for .irow to 3
146 for .icol to 2
147 .dif = abs (object [.xx, .irow, .icol] - .x## [.irow, .icol])
148 assert .dif < .eps; '.dif'< '.eps'
149 endfor
150 removeObject: .b, .xx, .a
151 appendInfoLine: tab$, "Solve 3x3: OK"
152 endproc
153 appendInfoLine: "test_Matrix_solve.praat OK"