source: trunk/la1.0/la.test @ 931

Last change on this file since 931 was 931, checked in by toby, 11 years ago

include rest of files

File size: 11.4 KB
Line 
1# Part of the Hume La package
2# (C)Copyright 2001, Hume Integration Services
3
4source la.tcl
5catch { namespace import La::*}
6
7if { [info commands dim] == {} } {
8  puts "Cannot find La package procedures like dim"
9  return
10  }
11
12if {[string compare test [info procs test]] == 1} then \
13  {source defs}
14
15test mathprec {compute precision of math calculations} {
16   set eps [mathprec]
17   expr $eps + 1 > 1
18} {1}
19
20test dim-1 {various good args} {
21    set list {}
22    foreach arg { {} {2.3} {2 1 0 2.3} {2 2 2 1.1 1.2 1.3 1.4} {3 1 1 1 1}} {
23        lappend list [dim $arg]
24        }
25    set list
26} {{} 0 1 2 3}
27
28test dim-2 {bad vector arg} {
29    catch {dim {2 0 3 1 2 3}} txt
30    set txt
31} {improper length of matrix}
32
33test dim-3 {bad scalar arg} {
34    catch {dim {2 0 0 3.14}} txt
35    set txt
36} {improper length of vector}
37
38test dim-4 {bad vector arg} {
39    catch {dim {2 3 0 1 2 3 4}} txt
40    set txt
41} {improper length of vector}
42
43test dim-5 {overlong matrix arg} {
44    catch {dim {2 3 1 1 2 3 4}} txt
45    set txt
46} {improper length of matrix}
47
48test dim-6 {short matrix arg} {
49    catch {dim {2 3 1 1 2}} txt
50    set txt
51} {improper length of matrix}
52
53test dim-7 {incomplete vector/matrix} {
54    catch {dim {2 1}} txt
55    set txt
56} {improper La operand format}
57
58test dim-8 {wrong type arg} {
59    catch {dim {hello world}} txt
60    set txt
61} {improper La operand format}
62
63test dim-9 {wrong type arg} {
64    catch {dim {hello}} txt
65    set txt
66} {improper La operand format}
67
68test dotprod-1 {vxv good} {
69    dotprod {2 3 0 1 2 3} {2 3 0 1 2 3}
70} {14.0}
71
72test dotprod-2 {v x v bad} {
73    catch {dotprod {2 3 0 1 2 3} {2 4 0 1 2 3 4}} txt
74    set txt
75} {Assumed length does not seem proper.}
76
77test dotprod-3 {v x Nx1 good} {
78    dotprod {2 3 0 1 2 3} {2 3 1 1 2 3}
79} {14.0}
80
81test dotprod-4 {v x 1XN good} {
82    dotprod {2 3 0 1 2 3} {2 1 3 1 2 3}
83} {14.0}
84
85test dotprod-5 {1xN x Nx1 good} {
86    dotprod {2 1 3 1 2 3} {2 3 1 1 2 3}
87} {14.0}
88
89test dotprod-6 {1xN x v good} {
90    dotprod {2 1 3 1 2 3} {2 3 0 1 2 3}
91} {14.0}
92
93test dotprod-7 {1xN x 1xN good} {
94    dotprod {2 1 3 1 2 3} {2 1 3 1 2 3}
95} {14.0}
96test dotprod-8 {Nx1 x v good} {
97    dotprod {2 3 1 1 2 3} {2 3 0 1 2 3}
98} {14.0}
99test dotprod-9 {Nx1 x 1xN good} {
100    dotprod {2 3 1 1 2 3} {2 1 3 1 2 3}
101} {14.0}
102test dotprod-10 {Nx1 x Nx1 good} {
103    dotprod {2 3 1 1 2 3} {2 3 1 1 2 3}
104} {14.0}
105
106test dotprod-11 {Nx1 x NxN good} {
107    dotprod {2 3 1 1 2 3} {2 3 3 1 2 3 4 5 6 7 8 9}
108} {14.0}
109
110test dotprod-12 {Nx1 x NxN good, 2nd row} {
111    dotprod {2 3 1 1 2 3} {2 3 3 1 2 3 0 1 0 7 8 9} 3 3 6
112} {2.0}
113
114test dotprod-13 {Nx1 x NxN good, 2nd col} {
115    dotprod {2 3 1 1 2 3} {2 3 3 1 2 3 0 1 0 7 8 9} 3 3 4 1 3
116} {28.0}
117
118test join_cols-1 {v v good} {
119    join_cols {2 3 0 1 2 3} {2 3 0 1.1 2.2 3.3}
120} {2 3 2 1 1.1 2 2.2 3 3.3}
121
122test join_cols-2 {v v bad} {
123    catch {join_cols {2 3 0 1 2 3} {2 4 0 1.1 2.2 3.3 4.4}} txt
124    set txt
125} {cannot append columns with inequal rows A[3,] + B[4,]}
126
127test join_cols-3 {m v good} {
128    join_cols {2 3 3 1 2 3 4 5 6 7 8 9} {2 3 0 1.1 2.2 3.3}
129} {2 3 4 1 2 3 1.1 4 5 6 2.2 7 8 9 3.3}
130
131test join_cols-4 {v m good} {
132    join_cols {2 3 0 1.1 2.2 3.3} {2 3 3 1 2 3 4 5 6 7 8 9}
133} {2 3 4 1.1 1 2 3 2.2 4 5 6 3.3 7 8 9}
134
135test join_cols-3 {m m good} {
136    join_cols {2 3 3 1 2 3 4 5 6 7 8 9} {2 3 3 1 2 3 4 5 6 7 8 9}
137} {2 3 6 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9}
138
139test join_rows-1 {v v equal} {
140    join_rows {2 3 0 1 2 3} {2 3 0 1.1 2.2 3.3}
141} {2 6 1 1 2 3 1.1 2.2 3.3}
142
143test join_rows-2 {v v not equal} {
144   catch {join_rows {2 3 0 1 2 3} {2 2 0 1.1 2.2}} txt
145    set txt
146} {2 5 1 1 2 3 1.1 2.2}
147
148test join_rows-3 {m v bad} {
149    catch {join_rows {2 3 3 1 2 3 4 5 6 7 8 9} {2 3 0 1.1 2.2 3.3}} txt
150    set txt
151} {cannot append rows with inequal columns A[,3] + B[,1]}
152
153test join_rows-4 {m v good} {
154    catch {join_rows {2 3 3 1 2 3 4 5 6 7 8 9} [transpose {2 3 0 1.1 2.2 3.3}]} txt
155    set txt
156} {2 4 3 1 2 3 4 5 6 7 8 9 1.1 2.2 3.3}
157
158test join_rows-5 {v m good} {
159    join_rows [transpose {2 3 0 1.1 2.2 3.3}] {2 3 3 1 2 3 4 5 6 7 8 9}
160} {2 4 3 1.1 2.2 3.3 1 2 3 4 5 6 7 8 9}
161
162test join_cols-6 {m m good} {
163    join_rows {2 3 3 1 2 3 4 5 6 7 8 9} {2 3 3 1 2 3 4 5 6 7 8 9}
164} {2 6 3 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9}
165
166test lassign_br-1 {good} {
167    set a {1 2 3}
168    lassign_br a 1 2.2
169    set a
170} {1 2.2 3}
171
172test lassign_br-2 {out-of-range} {
173    set a {1 2 3}
174    catch {lassign_br a 3 2.2} txt
175    set txt
176} {list doesn't contain element 3}
177
178test madd-1 {matrix add} {
179    set a {2 2 2 1 2 3 4}
180    madd $a $a
181} {2 2 2 2 4 6 8}
182
183test madd-2 {matrix add} {
184    set a {2 2 2 1 2 3 4}
185    catch {madd $a {2 1 1 2}} txt
186    set txt
187} {arguments are not conformable A[2,2] vs B[1,1]}
188
189test mcols-1 {col count} {
190    mcols {2 1 3 1 2 3}
191} {3}
192
193
194test mdiag-1 {v} {
195    mdiag {2 3 0 1 2 3}
196} {2 3 3 1 0 0 0 2 0 0 0 3}
197
198test mdiag-2 {1xN} {
199    mdiag {2 1 3 1 2 3}
200} {2 3 3 1 0 0 0 2 0 0 0 3}
201
202test mdiag-3 {Nx1} {
203    mdiag {2 3 1 1 2 3}
204} {2 3 3 1 0 0 0 2 0 0 0 3}
205
206
207test mevsvd-1 {evals of hilbert matrix} {
208   global mevect
209   set mevect [mhilbert 4]
210   mevsvd_br mevect evals
211   set txt evals=\n[show $evals %.6g]\n
212   append txt vects=\n[show $mevect %.6g]
213   set txt
214} {evals=
2151.50021 0.169141 0.00673827 9.67023e-005
216vects=
2170.792608 -0.582076 0.179186 -0.0291933
2180.451923 0.370502 -0.741918 0.328712
2190.322416 0.509579 0.100228 -0.791411
2200.252161 0.514048 0.638283 0.514553}
221
222test mevsvd-2 {evect test} {
223    global mevect
224    set m [mmult [transpose $mevect] $mevect]
225    show [mround $m]
226} {1.0 0.0 0.0 0.0
2270.0 1.0 0.0 0.0
2280.0 0.0 1.0 0.0
2290.0 0.0 0.0 1.0}
230
231
232test mident-1 {1} {
233   mident 1
234} {2 1 1 1}
235
236test mident-2 {3} {
237   mident 3
238} {2 3 3 1 0 0 0 1 0 0 0 1}
239
240test mident-3 {3.5} {
241   catch {mident 3.5} txt
242   set txt
243} {improper size "3.5"}
244
245test mlssvd {Nash data} {
246  set A {2 13 5 \
247 1  563 262 461 221\
248 1  658 291 473 222\
249 1  676 294 513 221\
250 1  749 302 516 218\
251 1  834 320 540 217\
252 1  973 350 596 218\
253 1 1079 386 650 218\
254 1 1151 401 676 225\
255 1 1324 446 769 228\
256 1 1499 492 870 230\
257 1 1690 510 907 237\
258 1 1735 534 932 235\
259 1 1778 559 956 236}
260    set y {2 13 0\
261 305 342 331 339 354 369 378 368 405 438 438 451 485}
262    set x [mlssvd $A $y 0.0 {}]
263 show $x %.6f
264} {207.782626 -0.046192 1.019387 -0.159823 -0.290376}
265
266test mmult-1 {good} {
267    set id3 [mident 3]
268    mmult $id3 $id3
269} {2 3 3 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0}
270
271test mmult-2 {good} {
272    set id3 [mident 3]
273    set m {2 3 3 1 2 3 4 5 6 7 8 9}
274    mmult $id3 $m
275} {2 3 3 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0}
276
277test mmult-3 {good} {
278    set id3 [mident 3]
279    set m {2 3 3 1 2 3 4 5 6 7 8 9}
280    mmult $m $id3
281} {2 3 3 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0}
282
283test mmult-4 {m v good} {
284    set m {2 3 3 1 2 3 4 5 6 7 8 9}
285    mmult $m {2 3 0 1 2 3}
286} {2 3 1 14.0 32.0 50.0}
287
288test mmult-5 {m v mismatch} {
289    set m {2 3 3 1 2 3 4 5 6 7 8 9}
290    catch {mmult $m {2 4 0 1 2 3 4}} txt
291    set txt
292} {matrices are not conformable A[3,3] x B[4,1]}
293
294test mmult-6 {v m mismatch} {
295    set m {2 3 3 1 2 3 4 5 6 7 8 9}
296    catch {mmult {2 3 0 1 2 3} $m} txt
297    set txt
298} {matrices are not conformable A[3,1] x B[3,3]}
299
300test mmult-7 {vt m match} {
301    set m {2 3 3 1 2 3 4 5 6 7 8 9}
302    catch {mmult [transpose {2 3 0 1 2 3}] $m} txt
303    set txt
304} {2 1 3 30.0 36.0 42.0}
305
306test mnorms-1 {id3} {
307    show [mnorms [mident 3]] %6.4f
308} {0.3333 0.3333 0.3333
3090.5774 0.5774 0.5774}
310
311test mnorms-2 {3x3 mat} {
312    show [mnorms {2 3 3 1 2 3 4 5 6 7 8 9}] %6.4f
313} {4.0000 5.0000 6.0000
3143.0000 3.0000 3.0000}
315
316test mnorms-3 {2x2 mat} {
317    show [mnorms {2 2 2 1 2 1 2}] %6.4f
318} {1.0000 2.0000
3190.0000 0.0000}
320
321test mnorms-4 {4x2 mat} {
322    show [mnorms {2 4 2 1 2 -1 -2 3 5 -5 -3}] %6.4f
323} {-0.5000 0.5000
3243.4157 3.6968}
325
326test mnormalize-1 {4x2 mat} {
327    set m {2 4 2 1 2 -1 -2 3 5 -5 -3}
328    mnorms_br m means sigmas
329    show [mnormalize $m $means $sigmas] %6.4f
330} {0.4392 0.4058
331-0.1464 -0.6763
3321.0247 1.2173
333-1.3175 -0.9468}
334
335test mrange-1 {3x3 mat} {
336   set m {2 3 3 1 2 3 4 5 6 7 8 9}
337   mrange $m 0 1
338} {2 3 2 1 2 4 5 7 8}
339
340test mrange-2 {3x3 mat} {
341   set m {2 3 3 1 2 3 4 5 6 7 8 9}
342   mrange $m 2 2
343} {2 3 1 3 6 9}
344
345test mrange-3 {3x3 mat index err} {
346   set m {2 3 3 1 2 3 4 5 6 7 8 9}
347   catch {mrange $m 3 3} txt
348   set txt
349} {column index 3 > 2}
350
351test mrows-1 {row count} {
352  mrows {2 1 3 1 2 3}
353} {1}
354
355test msolve-1 {inverse of hilbert matrix} {
356   global mevect
357   set mevect [mhilbert 4]
358   set mevect [msolve $mevect [mident 4]]
359   show $mevect
360} {16.0 -120.0 240.0 -140.0
361-120.0 1200.0 -2700.0 1680.0
362240.0 -2700.0 6480.0 -4200.0
363-140.0 1680.0 -4200.0 2800.0}
364
365test msolve-2 {minv test} {
366    global mevect
367    set m [mmult [transpose [mhilbert 4]] $mevect]
368    show [mround $m]
369} {1.0 0.0 0.0 0.0
3700.0 1.0 0.0 0.0
3710.0 0.0 1.0 0.0
3720.0 0.0 0.0 1.0}
373
374test moffset-1 {add const} {
375   set m {2 3 2 1 2 3 4 5 6}
376   moffset $m 0
377} {2 3 2 1 2 3 4 5 6}
378
379test moffset-2 {add const} {
380   set m {2 3 2 1 2 3 4 5 6}
381   moffset $m 0.1
382} {2 3 2 1.1 2.1 3.1 4.1 5.1 6.1}
383
384test mscale-1 {mult const} {
385   set m {2 3 2 1 2 3 4 5 6}
386   mscale $m 0.1
387} {2 3 2 0.1 0.2 0.3 0.4 0.5 0.6}
388
389test madjust-1 {mult const} {
390   set m {2 3 2 1 2 3 4 5 6}
391   madjust $m 2.0 0.1
392} {2 3 2 2.1 4.1 6.1 8.1 10.1 12.1}
393
394test msum-1 {reduce vector} {
395    msum {2 3 0 1 2 3}
396} {6.0}
397
398test msum-2 {reduce matrix cols} {
399    msum {2 3 3 1 2 3 4 5 6 7 8 9}
400} {2 3 0 12.0 15.0 18.0}
401
402test msum-3 {reduce matrix to scalar} {
403    msum [msum {2 3 3 1 2 3 4 5 6 7 8 9}]
404} {45.0}
405
406test promote-1 {scalar} {
407    promote 3.1415
408} {2 1 1 3.1415}
409
410test promote-2 {vector} {
411    promote {2 2 0 1 2}
412} {2 2 1 1 2}
413
414test promote-3 {matrix} {
415    promote {2 2 1 1 2}
416} {2 2 1 1 2}
417
418test promote-4 {3-D} {
419    promote {3 1 1 1 3.14}
420} {3 1 1 1 3.14}
421
422test demote-1 scalar {
423   demote 3.14
424} {3.14}
425
426test demote-2 {vector 1} {
427    demote {2 1 0 3.14}
428} {3.14}
429
430test demote-3 {vector 2} {
431    demote {2 2 0 1 2}
432} {2 2 0 1 2}
433
434test demote-4 {matrix Nx1} {
435   demote {2 3 1 1 2 3}
436} {2 3 0 1 2 3}
437
438test demote-5 {matrix 1xN} {
439   demote {2 1 3 1 2 3}
440} {2 3 0 1 2 3}
441
442test demote-6 {garbarge} {
443   catch {demote {garbage in garbage out}} txt
444   set txt
445} {improper La operand format}
446
447test show-1 {optional separators} {
448   set m {2 2 3 1 2 3 4 5 6}
449   show $m {} , \;
450} {1,2,3;4,5,6}
451
452test show-2 {optional separators, non default format} {
453   set m {2 2 3 1 2 3 4 5 6}
454   show $m %g , \;
455} {1,2,3;4,5,6}
456
457test show-3 {optional separators, non-default format} {
458   set m {2 2 3 1 2 3 4 5 6}
459   show $m %.1f , \;
460} {1.0,2.0,3.0;4.0,5.0,6.0}
461
462test show-4 {optional separators} {
463   set v {2 3 0 1 2 3}
464   show $v {} ,
465} {1,2,3}
466
467test show-5 {optional separators, non default format} {
468   set v {2 3 0 1 2 3}
469   show $v %g ,
470} {1,2,3}
471
472test show-6 {optional separators, non default format} {
473   set v {2 3 0 1 2 3}
474   show $v %.1f ,
475} {1.0,2.0,3.0}
476
477test transpose-1 vector {
478   transpose {2 3 0 1 2 3}
479} {2 1 3 1 2 3}
480
481test transpose-2 {nxm mat} {
482  transpose {2 3 2 1 2 3 4 5 6}
483} {2 2 3 1 3 5 2 4 6}
484
485test vdiag-1 {diag of matrix} {
486   set m {2 3 3 1 2 3 4 5 6 7 8 9}
487   vdiag $m
488} {2 3 0 1 5 9}
489
490test vdiag-2 {diag of matrix} {
491   set m {2 2 3 1 2 3 4 5 6}
492   vdiag $m
493} {2 2 0 1 5}
494
495test vtrim-1 vect {
496  vtrim {2 3 0 1 2 3}
497} {1 2 3}
498
499
500
501
Note: See TracBrowser for help on using the repository browser.