-
Notifications
You must be signed in to change notification settings - Fork 48
/
pipistack_linalg.tcl
2287 lines (2146 loc) · 63.7 KB
/
pipistack_linalg.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# linalg.tcl --
# Linear algebra package, based partly on Hume's LA package,
# partly on experiments with various representations of
# matrices. Also the functionality of the BLAS library has
# been taken into account.
#
# General information:
# - The package provides both a high-level general interface and
# a lower-level specific interface for various LA functions
# and tasks.
# - The general procedures perform some checks and then call
# the various specific procedures. The general procedures are
# aimed at robustness and ease of use.
# - The specific procedures do not check anything, they are
# designed for speed. Failure to comply to the interface
# requirements will presumably lead to [expr] errors.
# - Vectors are represented as lists, matrices as lists of
# lists, where the rows are the innermost lists:
#
# / a11 a12 a13 \
# | a21 a22 a23 | == { {a11 a12 a13} {a21 a22 a23} {a31 a32 a33} }
# \ a31 a32 a33 /
#
package require Tcl 8.4
namespace eval ::math::linearalgebra {
# Define the namespace
namespace export dim shape conforming symmetric
namespace export norm norm_one norm_two norm_max normMatrix
namespace export dotproduct unitLengthVector normalizeStat
namespace export axpy axpy_vect axpy_mat crossproduct
namespace export add add_vect add_mat
namespace export sub sub_vect sub_mat
namespace export scale scale_vect scale_mat matmul transpose
namespace export rotate angle choleski
namespace export getrow getcol getelem setrow setcol setelem
namespace export mkVector mkMatrix mkIdentity mkDiagonal
namespace export mkHilbert mkDingdong mkBorder mkFrank
namespace export mkMoler mkWilkinsonW+ mkWilkinsonW-
namespace export solveGauss solveTriangular
namespace export solveGaussBand solveTriangularBand
namespace export solvePGauss
namespace export determineSVD eigenvectorsSVD
namespace export leastSquaresSVD
namespace export orthonormalizeColumns orthonormalizeRows
namespace export show to_LA from_LA
namespace export swaprows swapcols
namespace export dger dgetrf mkRandom mkTriangular
namespace export det largesteigen
}
# dim --
# Return the dimension of an object (scalar, vector or matrix)
# Arguments:
# obj Object like a scalar, vector or matrix
# Result:
# Dimension: 0 for a scalar, 1 for a vector, 2 for a matrix
#
proc ::math::linearalgebra::dim { obj } {
set shape [shape $obj]
if { $shape != 1 } {
return [llength [shape $obj]]
} else {
return 0
}
}
# shape --
# Return the shape of an object (scalar, vector or matrix)
# Arguments:
# obj Object like a scalar, vector or matrix
# Result:
# List of the sizes: 1 for a scalar, number of components
# for a vector, number of rows and columns for a matrix
#
proc ::math::linearalgebra::shape { obj } {
set result [llength $obj]
if { [llength [lindex $obj 0]] <= 1 } {
return $result
} else {
lappend result [llength [lindex $obj 0]]
}
return $result
}
# show --
# Return a string representing the vector or matrix,
# for easy printing
# Arguments:
# obj Object like a scalar, vector or matrix
# format Format to be used (defaults to %6.4f)
# rowsep Separator for rows (defaults to \n)
# colsep Separator for columns (defaults to " ")
# Result:
# String representing the vector or matrix
#
proc ::math::linearalgebra::show { obj {format %6.4f} {rowsep \n} {colsep " "} } {
set result ""
if { [llength [lindex $obj 0]] == 1 } {
foreach v $obj {
append result "[format $format $v]$rowsep"
}
} else {
foreach row $obj {
foreach v $row {
append result "[format $format $v]$colsep"
}
append result $rowsep
}
}
return $result
}
# conforming --
# Determine if two objects (vector or matrix) are conforming
# in shape, rows or for a matrix multiplication
# Arguments:
# type Type of conforming: shape, rows or matmul
# obj1 First object (vector or matrix)
# obj2 Second object (vector or matrix)
# Result:
# 1 if they conform, 0 if not
#
proc ::math::linearalgebra::conforming { type obj1 obj2 } {
set shape1 [shape $obj1]
set shape2 [shape $obj2]
set result 0
if { $type == "shape" } {
set result [expr {[lindex $shape1 0] == [lindex $shape2 0] &&
[lindex $shape1 1] == [lindex $shape2 1]}]
}
if { $type == "rows" } {
set result [expr {[lindex $shape1 0] == [lindex $shape2 0]}]
}
if { $type == "matmul" } {
set result [expr {[lindex $shape1 1] == [lindex $shape2 0]}]
}
return $result
}
# crossproduct --
# Return the "cross product" of two 3D vectors
# Arguments:
# vect1 First vector
# vect2 Second vector
# Result:
# Cross product
#
proc ::math::linearalgebra::crossproduct { vect1 vect2 } {
if { [llength $vect1] == 3 && [llength $vect2] == 3 } {
foreach {v11 v12 v13} $vect1 {v21 v22 v23} $vect2 {break}
return [list \
[expr {$v12*$v23 - $v13*$v22}] \
[expr {$v13*$v21 - $v11*$v23}] \
[expr {$v11*$v22 - $v12*$v21}] ]
} else {
return -code error "Cross-product only defined for 3D vectors"
}
}
# angle --
# Return the "angle" between two vectors (in radians)
# Arguments:
# vect1 First vector
# vect2 Second vector
# Result:
# Angle between the two vectors
#
proc ::math::linearalgebra::angle { vect1 vect2 } {
set dp [dotproduct $vect1 $vect2]
set n1 [norm_two $vect1]
set n2 [norm_two $vect2]
if { $n1 == 0.0 || $n2 == 0.0 } {
return -code error "Angle not defined for null vector"
}
return [expr {acos($dp/$n1/$n2)}]
}
# norm --
# Compute the (1-, 2- or Inf-) norm of a vector
# Arguments:
# vector Vector (list of numbers)
# type Either 1, 2 or max/inf to indicate the type of
# norm (default: 2, the euclidean norm)
# Result:
# The (1-, 2- or Inf-) norm of a vector
# Level-1 BLAS :
# if type = 1, corresponds to DASUM
# if type = 2, corresponds to DNRM2
#
proc ::math::linearalgebra::norm { vector {type 2} } {
if { $type == 2 } {
return [norm_two $vector]
}
if { $type == 1 } {
return [norm_one $vector]
}
if { $type == "max" || $type == "inf" } {
return [norm_max $vector]
}
return -code error "Unknown norm: $type"
}
# norm_one --
# Compute the 1-norm of a vector
# Arguments:
# vector Vector
# Result:
# The 1-norm of a vector
#
proc ::math::linearalgebra::norm_one { vector } {
set sum 0.0
foreach c $vector {
set sum [expr {$sum+abs($c)}]
}
return $sum
}
# norm_two --
# Compute the 2-norm of a vector (euclidean norm)
# Arguments:
# vector Vector
# Result:
# The 2-norm of a vector
# Note:
# Rely on the function hypot() to make this robust
# against overflow and underflow
#
proc ::math::linearalgebra::norm_two { vector } {
set sum 0.0
foreach c $vector {
set sum [expr {hypot($c,$sum)}]
}
return $sum
}
# norm_max --
# Compute the inf-norm of a vector (maximum of its components)
# Arguments:
# vector Vector
# index, optional if non zero, returns a list made of the maximum
# value and the index where that maximum was found.
# if zero, returns the maximum value.
# Result:
# The inf-norm of a vector
# Level-1 BLAS :
# if index!=0, corresponds to IDAMAX
#
proc ::math::linearalgebra::norm_max { vector {index 0}} {
set max [lindex $vector 0]
set imax 0
set i 0
foreach c $vector {
if {[expr {abs($c)>$max}]} then {
set imax $i
set max [expr {abs($c)}]
}
incr i
}
if {$index == 0} then {
set result $max
} else {
set result [list $max $imax]
}
return $result
}
# normMatrix --
# Compute the (1-, 2- or Inf-) norm of a matrix
# Arguments:
# matrix Matrix (list of row vectors)
# type Either 1, 2 or max/inf to indicate the type of
# norm (default: 2, the euclidean norm)
# Result:
# The (1-, 2- or Inf-) norm of the matrix
#
proc ::math::linearalgebra::normMatrix { matrix {type 2} } {
set v {}
foreach row $matrix {
lappend v [norm $row $type]
}
return [norm $v $type]
}
# symmetric --
# Determine if the matrix is symmetric or not
# Arguments:
# matrix Matrix (list of row vectors)
# eps Tolerance (defaults to 1.0e-8)
# Result:
# 1 if symmetric (within the tolerance), 0 if not
#
proc ::math::linearalgebra::symmetric { matrix {eps 1.0e-8} } {
set shape [shape $matrix]
if { [lindex $shape 0] != [lindex $shape 1] } {
return 0
}
set norm_org [normMatrix $matrix]
set norm_asymm [normMatrix [sub $matrix [transpose $matrix]]]
if { $norm_asymm <= $eps*$norm_org } {
return 1
} else {
return 0
}
}
# dotproduct --
# Compute the dot product of two vectors
# Arguments:
# vect1 First vector
# vect2 Second vector
# Result:
# The dot product of the two vectors
# Level-1 BLAS : corresponds to DDOT
#
proc ::math::linearalgebra::dotproduct { vect1 vect2 } {
if { [llength $vect1] != [llength $vect2] } {
return -code error "Vectors must be of equal length"
}
set sum 0.0
foreach c1 $vect1 c2 $vect2 {
set sum [expr {$sum + $c1*$c2}]
}
return $sum
}
# unitLengthVector --
# Normalize a vector so that a length 1 results and return the new vector
# Arguments:
# vector Vector to be normalized
# Result:
# A vector of length 1
#
proc ::math::linearalgebra::unitLengthVector { vector } {
set scale [norm_two $vector]
if { $scale == 0.0 } {
return -code error "Can not normalize a null-vector"
}
return [scale [expr {1.0/$scale}] $vector]
}
# normalizeStat --
# Normalize a matrix or vector in a statistical sense and return the result
# Arguments:
# mv Matrix or vector to be normalized
# Result:
# A matrix or vector whose columns are normalised to have a mean of
# 0 and a standard deviation of 1.
#
proc ::math::linearalgebra::normalizeStat { mv } {
if { [llength [lindex $mv 0]] > 1 } {
set result {}
foreach vector [transpose $mv] {
lappend result [NormalizeStat_vect $vector]
}
return [transpose $result]
} else {
return [NormalizeStat_vect $mv]
}
}
# NormalizeStat_vect --
# Normalize a vector in a statistical sense and return the result
# Arguments:
# v Vector to be normalized
# Result:
# A vector whose elements are normalised to have a mean of
# 0 and a standard deviation of 1. If all coefficients are equal,
# a null-vector is returned.
#
proc ::math::linearalgebra::NormalizeStat_vect { v } {
if { [llength $v] <= 1 } {
return -code error "Vector can not be normalised - too few coefficients"
}
set sum 0.0
set sum2 0.0
set count 0.0
foreach c $v {
set sum [expr {$sum + $c}]
set sum2 [expr {$sum2 + $c*$c}]
set count [expr {$count + 1.0}]
}
set corr [expr {$sum/$count}]
set factor [expr {($sum2-$sum*$sum/$count)/($count-1)}]
if { $factor > 0.0 } {
set factor [expr {1.0/sqrt($factor)}]
} else {
set factor 0.0
}
set result {}
foreach c $v {
lappend result [expr {$factor*($c-$corr)}]
}
return $result
}
# axpy --
# Compute the sum of a scaled vector/matrix and another
# vector/matrix: a*x + y
# Arguments:
# scale Scale factor (a) for the first vector/matrix
# mv1 First vector/matrix (x)
# mv2 Second vector/matrix (y)
# Result:
# The result of a*x+y
# Level-1 BLAS : if mv1 is a vector, corresponds to DAXPY
#
proc ::math::linearalgebra::axpy { scale mv1 mv2 } {
if { [llength [lindex $mv1 0]] > 1 } {
return [axpy_mat $scale $mv1 $mv2]
} else {
return [axpy_vect $scale $mv1 $mv2]
}
}
# axpy_vect --
# Compute the sum of a scaled vector and another vector: a*x + y
# Arguments:
# scale Scale factor (a) for the first vector
# vect1 First vector (x)
# vect2 Second vector (y)
# Result:
# The result of a*x+y
# Level-1 BLAS : corresponds to DAXPY
#
proc ::math::linearalgebra::axpy_vect { scale vect1 vect2 } {
set result {}
foreach c1 $vect1 c2 $vect2 {
lappend result [expr {$scale*$c1+$c2}]
}
return $result
}
# axpy_mat --
# Compute the sum of a scaled matrix and another matrix: a*x + y
# Arguments:
# scale Scale factor (a) for the first matrix
# mat1 First matrix (x)
# mat2 Second matrix (y)
# Result:
# The result of a*x+y
#
proc ::math::linearalgebra::axpy_mat { scale mat1 mat2 } {
set result {}
foreach row1 $mat1 row2 $mat2 {
lappend result [axpy_vect $scale $row1 $row2]
}
return $result
}
# add --
# Compute the sum of two vectors/matrices
# Arguments:
# mv1 First vector/matrix (x)
# mv2 Second vector/matrix (y)
# Result:
# The result of x+y
#
proc ::math::linearalgebra::add { mv1 mv2 } {
if { [llength [lindex $mv1 0]] > 1 } {
return [add_mat $mv1 $mv2]
} else {
return [add_vect $mv1 $mv2]
}
}
# add_vect --
# Compute the sum of two vectors
# Arguments:
# vect1 First vector (x)
# vect2 Second vector (y)
# Result:
# The result of x+y
#
proc ::math::linearalgebra::add_vect { vect1 vect2 } {
set result {}
foreach c1 $vect1 c2 $vect2 {
lappend result [expr {$c1+$c2}]
}
return $result
}
# add_mat --
# Compute the sum of two matrices
# Arguments:
# mat1 First matrix (x)
# mat2 Second matrix (y)
# Result:
# The result of x+y
#
proc ::math::linearalgebra::add_mat { mat1 mat2 } {
set result {}
foreach row1 $mat1 row2 $mat2 {
lappend result [add_vect $row1 $row2]
}
return $result
}
# sub --
# Compute the difference of two vectors/matrices
# Arguments:
# mv1 First vector/matrix (x)
# mv2 Second vector/matrix (y)
# Result:
# The result of x-y
#
proc ::math::linearalgebra::sub { mv1 mv2 } {
if { [llength [lindex $mv1 0]] > 0 } {
return [sub_mat $mv1 $mv2]
} else {
return [sub_vect $mv1 $mv2]
}
}
# sub_vect --
# Compute the difference of two vectors
# Arguments:
# vect1 First vector (x)
# vect2 Second vector (y)
# Result:
# The result of x-y
#
proc ::math::linearalgebra::sub_vect { vect1 vect2 } {
set result {}
foreach c1 $vect1 c2 $vect2 {
lappend result [expr {$c1-$c2}]
}
return $result
}
# sub_mat --
# Compute the difference of two matrices
# Arguments:
# mat1 First matrix (x)
# mat2 Second matrix (y)
# Result:
# The result of x-y
#
proc ::math::linearalgebra::sub_mat { mat1 mat2 } {
set result {}
foreach row1 $mat1 row2 $mat2 {
lappend result [sub_vect $row1 $row2]
}
return $result
}
# scale --
# Scale a vector or a matrix
# Arguments:
# scale Scale factor (scalar; a)
# mv Vector/matrix (x)
# Result:
# The result of a*x
# Level-1 BLAS : if mv is a vector, corresponds to DSCAL
#
proc ::math::linearalgebra::scale { scale mv } {
if { [llength [lindex $mv 0]] > 1 } {
return [scale_mat $scale $mv]
} else {
return [scale_vect $scale $mv]
}
}
# scale_vect --
# Scale a vector
# Arguments:
# scale Scale factor to apply (a)
# vect Vector to be scaled (x)
# Result:
# The result of a*x
# Level-1 BLAS : corresponds to DSCAL
#
proc ::math::linearalgebra::scale_vect { scale vect } {
set result {}
foreach c $vect {
lappend result [expr {$scale*$c}]
}
return $result
}
# scale_mat --
# Scale a matrix
# Arguments:
# scale Scale factor to apply
# mat Matrix to be scaled
# Result:
# The result of x+y
#
proc ::math::linearalgebra::scale_mat { scale mat } {
set result {}
foreach row $mat {
lappend result [scale_vect $scale $row]
}
return $result
}
# rotate --
# Apply a planar rotation to two vectors
# Arguments:
# c Cosine of the angle
# s Sine of the angle
# vect1 First vector (x)
# vect2 Second vector (y)
# Result:
# A list of two elements: c*x-s*y and s*x+c*y
#
proc ::math::linearalgebra::rotate { c s vect1 vect2 } {
set result1 {}
set result2 {}
foreach v1 $vect1 v2 $vect2 {
lappend result1 [expr {$c*$v1-$s*$v2}]
lappend result2 [expr {$s*$v1+$c*$v2}]
}
return [list $result1 $result2]
}
# transpose --
# Transpose a matrix
# Arguments:
# matrix Matrix to be transposed
# Result:
# The transposed matrix
# Note:
# The second transpose implementation is faster on large
# matrices (100x100 say), there is no significant difference
# on small ones (10x10 say).
#
#
proc ::math::linearalgebra::transpose_old { matrix } {
set row {}
set transpose {}
foreach c [lindex $matrix 0] {
lappend row 0.0
}
foreach r $matrix {
lappend transpose $row
}
set nr 0
foreach r $matrix {
set nc 0
foreach c $r {
lset transpose $nc $nr $c
incr nc
}
incr nr
}
return $transpose
}
proc ::math::linearalgebra::transpose { matrix } {
set transpose {}
set c 0
foreach col [lindex $matrix 0] {
set newrow {}
foreach row $matrix {
lappend newrow [lindex $row $c]
}
lappend transpose $newrow
incr c
}
return $transpose
}
# MorV --
# Identify if the object is a row/column vector or a matrix
# Arguments:
# obj Object to be examined
# Result:
# The letter R, C or M depending on the shape
# (just to make it all work fine: S for scalar)
# Note:
# Private procedure to fix a bug in matmul
#
proc ::math::linearalgebra::MorV { obj } {
if { [llength $obj] > 1 } {
if { [llength [lindex $obj 0]] > 1 } {
return "M"
} else {
return "C"
}
} else {
if { [llength [lindex $obj 0]] > 1 } {
return "R"
} else {
return "S"
}
}
}
# matmul --
# Multiply a vector/matrix with another vector/matrix
# Arguments:
# mv1 First vector/matrix (x)
# mv2 Second vector/matrix (y)
# Result:
# The result of x*y
#
proc ::math::linearalgebra::matmul_org { mv1 mv2 } {
if { [llength [lindex $mv1 0]] > 1 } {
if { [llength [lindex $mv2 0]] > 1 } {
return [matmul_mm $mv1 $mv2]
} else {
return [matmul_mv $mv1 $mv2]
}
} else {
if { [llength [lindex $mv2 0]] > 1 } {
return [matmul_vm $mv1 $mv2]
} else {
return [matmul_vv $mv1 $mv2]
}
}
}
proc ::math::linearalgebra::matmul { mv1 mv2 } {
switch -exact -- "[MorV $mv1][MorV $mv2]" {
"MM" {
return [matmul_mm $mv1 $mv2]
}
"MC" {
return [matmul_mv $mv1 $mv2]
}
"MR" {
return -code error "Can not multiply a matrix with a row vector - wrong order"
}
"RM" {
return [matmul_vm [transpose $mv1] $mv2]
}
"RC" {
return [dotproduct [transpose $mv1] $mv2]
}
"RR" {
return -code error "Can not multiply a matrix with a row vector - wrong order"
}
"CM" {
return [transpose [matmul_vm $mv1 $mv2]]
}
"CR" {
return [matmul_vv $mv1 [transpose $mv2]]
}
"CC" {
return [matmul_vv $mv1 $mv2]
}
"SS" {
return [expr {$mv1 * $mv2}]
}
default {
return -code error "Can not use a scalar object"
}
}
}
# matmul_mv --
# Multiply a matrix and a column vector
# Arguments:
# matrix Matrix (applied left: A)
# vector Vector (interpreted as column vector: x)
# Result:
# The vector A*x
# Level-2 BLAS : corresponds to DTRMV
#
proc ::math::linearalgebra::matmul_mv { matrix vector } {
set newvect {}
foreach row $matrix {
set sum 0.0
foreach v $vector c $row {
set sum [expr {$sum+$v*$c}]
}
lappend newvect $sum
}
return $newvect
}
# matmul_vm --
# Multiply a row vector with a matrix
# Arguments:
# vector Vector (interpreted as row vector: x)
# matrix Matrix (applied right: A)
# Result:
# The vector xtrans*A = Atrans*x
#
proc ::math::linearalgebra::matmul_vm { vector matrix } {
return [transpose [matmul_mv [transpose $matrix] $vector]]
}
# matmul_vv --
# Multiply two vectors to obtain a matrix
# Arguments:
# vect1 First vector (column vector, x)
# vect2 Second vector (row vector, y)
# Result:
# The "outer product" x*ytrans
#
proc ::math::linearalgebra::matmul_vv { vect1 vect2 } {
set newmat {}
foreach v1 $vect1 {
set newrow {}
foreach v2 $vect2 {
lappend newrow [expr {$v1*$v2}]
}
lappend newmat $newrow
}
return $newmat
}
# matmul_mm --
# Multiply two matrices
# Arguments:
# mat1 First matrix (A)
# mat2 Second matrix (B)
# Result:
# The matrix product A*B
# Note:
# By transposing matrix B we can access the columns
# as rows - much easier and quicker, as they are
# the elements of the outermost list.
# Level-3 BLAS :
# corresponds to DGEMM (alpha op(A) op(B) + beta C) when alpha=1, op(X)=X and beta=0
# corresponds to DTRMM (alpha op(A) B) when alpha = 1, op(X)=X
#
proc ::math::linearalgebra::matmul_mm { mat1 mat2 } {
set newmat {}
set tmat [transpose $mat2]
foreach row1 $mat1 {
set newrow {}
foreach row2 $tmat {
lappend newrow [dotproduct $row1 $row2]
}
lappend newmat $newrow
}
return $newmat
}
# mkVector --
# Make a vector of a given size
# Arguments:
# ndim Dimension of the vector
# value Default value for all elements (default: 0.0)
# Result:
# A list with ndim elements, representing a vector
#
proc ::math::linearalgebra::mkVector { ndim {value 0.0} } {
set result {}
while { $ndim > 0 } {
lappend result $value
incr ndim -1
}
return $result
}
# mkUnitVector --
# Make a unit vector in a given direction
# Arguments:
# ndim Dimension of the vector
# dir The direction (0, ... ndim-1)
# Result:
# A list with ndim elements, representing a unit vector
#
proc ::math::linearalgebra::mkUnitVector { ndim dir } {
if { $dir < 0 || $dir >= $ndim } {
return -code error "Invalid direction for unit vector - $dir"
} else {
set result [mkVector $ndim]
lset result $dir 1.0
}
return $result
}
# mkMatrix --
# Make a matrix of a given size
# Arguments:
# nrows Number of rows
# ncols Number of columns
# value Default value for all elements (default: 0.0)
# Result:
# A nested list, representing an nrows x ncols matrix
#
proc ::math::linearalgebra::mkMatrix { nrows ncols {value 0.0} } {
set result {}
while { $nrows > 0 } {
lappend result [mkVector $ncols $value]
incr nrows -1
}
return $result
}
# mkIdent --
# Make an identity matrix of a given size
# Arguments:
# size Number of rows/columns
# Result:
# A nested list, representing an size x size identity matrix
#
proc ::math::linearalgebra::mkIdentity { size } {
set result [mkMatrix $size $size 0.0]
while { $size > 0 } {
incr size -1
lset result $size $size 1.0
}
return $result
}
# mkDiagonal --
# Make a diagonal matrix of a given size
# Arguments:
# diag List of values to appear on the diagonal
#
# Result:
# A nested list, representing a diagonal matrix
#
proc ::math::linearalgebra::mkDiagonal { diag } {
set size [llength $diag]
set result [mkMatrix $size $size 0.0]
while { $size > 0 } {
incr size -1
lset result $size $size [lindex $diag $size]
}
return $result
}
# mkHilbert --
# Make a Hilbert matrix of a given size
# Arguments:
# size Size of the matrix
# Result:
# A nested list, representing a Hilbert matrix
# Notes:
# Hilbert matrices are very ill-conditioned wrt
# eigenvalue/eigenvector problems. Therefore they
# are good candidates for testing the accuracy
# of algorithms and implementations.
#
proc ::math::linearalgebra::mkHilbert { size } {
set result {}
for { set j 0 } { $j < $size } { incr j } {
set row {}
for { set i 0 } { $i < $size } { incr i } {
lappend row [expr {1.0/($i+$j+1.0)}]
}
lappend result $row
}
return $result
}
# mkDingdong --
# Make a Dingdong matrix of a given size
# Arguments:
# size Size of the matrix
# Result:
# A nested list, representing a Dingdong matrix
# Notes:
# Dingdong matrices are imprecisely represented,
# but have the property of being very stable in
# such algorithms as Gauss elimination.
#
proc ::math::linearalgebra::mkDingdong { size } {
set result {}
for { set j 0 } { $j < $size } { incr j } {
set row {}
for { set i 0 } { $i < $size } { incr i } {
lappend row [expr {0.5/($size-$i-$j-0.5)}]
}
lappend result $row
}
return $result
}
# mkOnes --
# Make a square matrix consisting of ones
# Arguments:
# size Number of rows/columns
# Result:
# A nested list, representing a size x size matrix,
# filled with 1.0
#
proc ::math::linearalgebra::mkOnes { size } {
return [mkMatrix $size $size 1.0]
}
# mkMoler --
# Make a Moler matrix
# Arguments:
# size Size of the matrix