| 1 |
writeInfoLine: "vectors and matrices..." |
| 2 |
|
| 3 |
allow_metal = 0 |
| 4 |
|
| 5 |
a# = zero#(16) |
| 6 |
a#[3] = 4 |
| 7 |
assert a#[3] = 4 |
| 8 |
a# ~ col ^ 2 |
| 9 |
assert a# = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256 } |
| 10 |
|
| 11 |
asserterror Vector b# does not exist. |
| 12 |
b# [5] = 3 |
| 13 |
|
| 14 |
asserterror Unknown variable:'newline$'« xyz# |
| 15 |
xyz# = xyz# + 5 |
| 16 |
|
| 17 |
asserterror Unknown variable:'newline$'« xyz## |
| 18 |
xyz## = xyz## + 5 |
| 19 |
|
| 20 |
asserterror Unknown variable:'newline$'« xyz$# |
| 21 |
xyz$# = xyz$# + "hello" |
| 22 |
|
| 23 |
asserterror A vector index cannot be less than 1 (the index you supplied is 0). |
| 24 |
a# [0] = 932875289 |
| 25 |
|
| 26 |
asserterror A vector index cannot be greater than the number of elements (here 16). The index you supplied is 20. |
| 27 |
a# [20] = 45786457 |
| 28 |
|
| 29 |
assert numberOfRows (zero## (5, 6)) = 5 |
| 30 |
assert numberOfColumns (zero## (5, 6)) = 6 |
| 31 |
a## = zero## (5, 6) |
| 32 |
assert numberOfRows (a##) = 5 |
| 33 |
assert a## [3, 4] = 0 |
| 34 |
|
| 35 |
a## [5, 6] = 567 |
| 36 |
assert a##[5,6] = 567 |
| 37 |
|
| 38 |
c# = linear# (0, 100, 101) |
| 39 |
assert c# [98] = 97 |
| 40 |
c# = linear# (0, 100, 101, 0) |
| 41 |
assert c# [98] = 97 |
| 42 |
c# = linear# (0, 100, 100, 1) |
| 43 |
assert c# [98] = 97.5 |
| 44 |
|
| 45 |
d# = randomGauss# (c#, 20, 1) |
| 46 |
a = d# [98] |
| 47 |
b = d# [99] |
| 48 |
c = d# [100] |
| 49 |
appendInfoLine: a, " ", b, " ", c |
| 50 |
|
| 51 |
d# = randomUniform# (c#, 7, 10) |
| 52 |
a = d# [98] |
| 53 |
b = d# [99] |
| 54 |
c = d# [100] |
| 55 |
appendInfoLine: a, " ", b, " ", c |
| 56 |
|
| 57 |
d# = randomInteger# (c#, 7, 10) |
| 58 |
a = d# [98] |
| 59 |
b = d# [99] |
| 60 |
c = d# [100] |
| 61 |
appendInfoLine: a, " ", b, " ", c |
| 62 |
|
| 63 |
e# = a# + a# |
| 64 |
assert e# [3] = 18 |
| 65 |
|
| 66 |
asserterror numbers of elements should be equal |
| 67 |
e# = a# + d# |
| 68 |
|
| 69 |
; q### = |
| 70 |
; data#### |
| 71 |
;e# = d# + c# |
| 72 |
|
| 73 |
;speaker$# = empty$# [2] |
| 74 |
;speaker$# [1] = "JM" |
| 75 |
;speaker$# [2] = "PB" |
| 76 |
|
| 77 |
;speaker$ [1] = "JM" |
| 78 |
;speaker$ [2] = "PB" |
| 79 |
|
| 80 |
a# = zero# (0) |
| 81 |
a## = { a#, a# } |
| 82 |
assert numberOfRows (a##) = 2 |
| 83 |
assert numberOfColumns (a##) = 0 |
| 84 |
|
| 85 |
# |
| 86 |
# outer## |
| 87 |
# |
| 88 |
n# = zero# (100) |
| 89 |
x# = randomInteger# (n#, 1, 1e7) |
| 90 |
y# = randomInteger# (n#, 1, 1e7) |
| 91 |
mat## = outer## (x#, y#) |
| 92 |
for irow to 100 |
| 93 |
for icol to 100 |
| 94 |
assert mat## [irow, icol] = x# [irow] * y# [icol] |
| 95 |
endfor |
| 96 |
endfor |
| 97 |
|
| 98 |
squares# = { 1, 4, 9, 16, 25 } |
| 99 |
assert sum (squares#) = 55 |
| 100 |
assert mean (squares#) = 11 |
| 101 |
assert abs (mean (squares#) - sum (squares#) / 5) < 1e-14 |
| 102 |
assert abs (stdev (squares#) - 9.669539802906858) < 1e-14 |
| 103 |
assert abs (stdev (squares#) - sqrt (sumOver (i to 5, (squares# [i] - mean (squares#)) ^ 2) / 4)) < 1e-14 |
| 104 |
assert abs (center (squares#) - 4.090909090909091) < 1e-14 |
| 105 |
assert abs (center (squares#) - sumOver (i to 5, i * squares# [i]) / sum (squares#)) < 1e-14 |
| 106 |
other# = { 2, 1.5, 1, 0.5, 0 } |
| 107 |
assert inner (squares#, other#) = 25 |
| 108 |
assert sumOver (i to 5, squares# [i] * other# [i]) = 25 |
| 109 |
|
| 110 |
combi## = { squares#, other# } |
| 111 |
assert combi## = { { 1, 4, 9, 16, 25 }, { 2, 1.5, 1, 0.5, 0 } } |
| 112 |
|
| 113 |
a# = squares# + 5 |
| 114 |
assert a# = { 6, 9, 14, 21, 30 } |
| 115 |
b# = a# + { 3.14, 2.72, 3.16, -1, 7.5 } |
| 116 |
assert b# = { 9.14, 11.72, 17.16, 20, 37.5 } |
| 117 |
c# = b# / 2 |
| 118 |
appendInfoLine: c# |
| 119 |
assert c# = { 4.57, 5.86, 8.58, 10, 18.75 } |
| 120 |
d# = b# * c# |
| 121 |
norm = norm (d# - { 41.7698, 68.6792, 147.2328, 200, 703.125 }) |
| 122 |
assert norm < 1e-13 ; 'norm' |
| 123 |
|
| 124 |
stopwatch |
| 125 |
iterations = 1e5 |
| 126 |
for i to iterations |
| 127 |
a = 10+5 |
| 128 |
a = 10+5 |
| 129 |
a = 10+5 |
| 130 |
a = 10+5 |
| 131 |
a = 10+5 |
| 132 |
a = 10+5 |
| 133 |
a = 10+5 |
| 134 |
a = 10+5 |
| 135 |
a = 10+5 |
| 136 |
a = 10+5 |
| 137 |
endfor |
| 138 |
t = stopwatch / 10 * 1e9 / iterations |
| 139 |
appendInfoLine: t, " ns" |
| 140 |
|
| 141 |
stopwatch |
| 142 |
a# = { 4, 9, 16 } |
| 143 |
b# = { 25, 36, 49 } |
| 144 |
iterations = 1e5 |
| 145 |
for i to iterations |
| 146 |
c# = a# - b# |
| 147 |
c# = a# - b# |
| 148 |
c# = a# - b# |
| 149 |
c# = a# - b# |
| 150 |
c# = a# - b# |
| 151 |
c# = a# - b# |
| 152 |
c# = a# - b# |
| 153 |
c# = a# - b# |
| 154 |
c# = a# - b# |
| 155 |
c# = a# - b# |
| 156 |
endfor |
| 157 |
t = stopwatch / 10 * 1e9 / iterations / 3 |
| 158 |
appendInfoLine: t, " ns" |
| 159 |
|
| 160 |
stopwatch |
| 161 |
n = 100 |
| 162 |
z# = zero# (n) |
| 163 |
a# = randomGauss# (z#, 0, 1) |
| 164 |
b# = randomGauss# (z#, 0, 1) |
| 165 |
iterations = 1e4 |
| 166 |
for i to iterations |
| 167 |
m## = outer## (a#, b#) |
| 168 |
m## = outer## (a#, b#) |
| 169 |
m## = outer## (a#, b#) |
| 170 |
m## = outer## (a#, b#) |
| 171 |
m## = outer## (a#, b#) |
| 172 |
m## = outer## (a#, b#) |
| 173 |
m## = outer## (a#, b#) |
| 174 |
m## = outer## (a#, b#) |
| 175 |
m## = outer## (a#, b#) |
| 176 |
m## = outer## (a#, b#) |
| 177 |
endfor |
| 178 |
t = stopwatch / 10 * 1e9 / iterations / n^2 |
| 179 |
appendInfoLine: t, " ns" |
| 180 |
|
| 181 |
a## = zero## (20, 50) |
| 182 |
b## = zero## (50, 17) |
| 183 |
a## ~ row ^ 2 + col ^ 3 |
| 184 |
b## ~ row ^ 5 + col ^ 7 |
| 185 |
appendInfoLine: "mul##:", newline$, mul## (a##, b##) |
| 186 |
appendInfoLine: "mul_fast##", newline$, mul_fast## (a##, b##) |
| 187 |
if allow_metal |
| 188 |
appendInfoLine: "mul_metal##", newline$, mul_metal## (a##, b##) |
| 189 |
endif |
| 190 |
|
| 191 |
a## = {{ 2, 3, 5 }, { 7, 3, 2 }} |
| 192 |
b## = {{ 11, 1, 5, 2 }, { 8, 2, 3, 6 }, { 1, 3, 4, 9 }} |
| 193 |
product## = mul## (a##, b##) |
| 194 |
assert product## = {{ 51, 23, 39, 67 }, { 103, 19, 52, 50 }} |
| 195 |
product_fast## = mul_fast## (a##, b##) |
| 196 |
appendInfoLine: product_fast## |
| 197 |
assert numberOfRows (product_fast##) = 2 |
| 198 |
assert numberOfColumns (product_fast##) = 4 |
| 199 |
assert product## = product_fast## |
| 200 |
if allow_metal |
| 201 |
product_metal## = mul_metal## (a##, b##) |
| 202 |
appendInfoLine: product_metal## |
| 203 |
assert numberOfRows (product_metal##) = 2 |
| 204 |
assert numberOfColumns (product_metal##) = 4 |
| 205 |
assert product## = product_metal## |
| 206 |
endif |
| 207 |
at## = transpose## (a##) |
| 208 |
bt## = transpose## (b##) |
| 209 |
assert at## = {{ 2, 7 }, { 3, 3 }, { 5, 2 }} |
| 210 |
assert transpose## (bt##) = b## |
| 211 |
assert mul_tn## (at##, b##) = product## |
| 212 |
assert mul_nt## (a##, bt##) = product## |
| 213 |
assert mul_tt## (at##, bt##) = product## |
| 214 |
|
| 215 |
@do: { 1, 103, 7 } |
| 216 |
procedure do: v# |
| 217 |
.result = mean (v#) |
| 218 |
endproc |
| 219 |
assert do.result = 37 |
| 220 |
|
| 221 |
appendInfoLine: "OK" |