-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.tcl
293 lines (264 loc) · 6.34 KB
/
matrix.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
# matrix.tcl
################################################################################
# Utilities for matrices (2D lists)
# Copyright (C) 2024 Alex Baker, ambaker1@mtu.edu
# All rights reserved.
# See the file "LICENSE" in the top level directory for information on usage,
# redistribution, and for a DISCLAIMER OF ALL WARRANTIES.
################################################################################
# Define namespace and exported commands
namespace eval ::ndlist {
namespace export zeros ones eye; # Generate matrices
namespace export stack augment block; # Combine matrices
namespace export transpose matmul outerprod kronprod; # Linear algebra
namespace export zip zip3 cartprod; # Iteration tools
}
# zeros --
#
# Generate a matrix filled with zeros.
#
# Syntax:
# zeros $n $m
#
# Arguments:
# n Number of rows
# m Number of columns
proc ::ndlist::zeros {n m} {
if {$m == 0} {return}
lrepeat $n [lrepeat $m 0]
}
# ones --
#
# Generate a matrix filled with ones.
#
# Syntax:
# ones $n $m
#
# Arguments:
# n Number of rows
# m Number of columns
proc ::ndlist::ones {n m} {
if {$m == 0} {return}
lrepeat $n [lrepeat $m 1]
}
# eye --
#
# Generate an identity matrix of specified size
#
# Syntax:
# eye $n
#
# Arguments:
# n Size of matrix (nxn)
proc ::ndlist::eye {n} {
set x [zeros $n $n]
foreach i [range $n] {
lset x $i $i 1
}
return $x
}
# stack --
#
# Combines matrices (row-wise)
#
# Syntax:
# stack $mat1 $mat2 ...
#
# Arguments:
# $mat1 $mat2 ... Arbitrary number of matrices
proc ::ndlist::stack {args} {
set Matrix [lindex $args 0]
set M [llength [lindex $Matrix 0]]
foreach matrix [lrange $args 1 end] {
set m [llength [lindex $matrix 0]]
if {$m != $M} {
return -code error "incompatible number of columns"
}
set Matrix [concat $Matrix $matrix]
}
return $Matrix
}
# augment --
#
# Combines matrices (column-wise)
#
# Syntax:
# augment $mat1 $mat2 ...
#
# Arguments:
# mat1 mat2 ... Arbitrary number of matrices
proc ::ndlist::augment {args} {
set Matrix [lindex $args 0]
set N [llength $Matrix]
foreach matrix [lrange $args 1 end] {
set n [llength $matrix]
if {$n != $N} {
return -code error "incompatible number of rows"
}
set Matrix [lmap Row $Matrix row $matrix {concat $Row $row}]
}
return $Matrix
}
# block --
#
# Combine a matrix of matrices
#
# Syntax:
# block $matrices
#
# Arguments:
# matrices Matrix of matrices
proc ::ndlist::block {matrices} {
stack {*}[lmap row $matrices {augment {*}$row}]
}
# transpose --
#
# Transpose a matrix
# Similar to math::linearalgebra::transpose and lsearch example on Tcl wiki
# written by MJ (https://wiki.tcl-lang.org/page/Transposing+a+matrix)
#
# Syntax:
# transpose $matrix
#
# Arguments:
# matrix: Matrix to transpose
proc ::ndlist::transpose {matrix} {
set n [llength $matrix]
# Null case
if {$n == 0} {
return
}
set m [llength [lindex $matrix 0]]
if {$n == 1 && $m == 1} {
return $matrix
} elseif {$n > $m} {
set i -1
lmap x [lindex $matrix 0] {
lsearch -all -inline -subindices -index [incr i] $matrix *
}
} else {
set i -1
lmap x [lindex $matrix 0] {
incr i
lmap row $matrix {lindex $row $i}
}
}
}
# matmul --
#
# Multiplies two matrices. Inner dimensions must agree.
# Similar to math::linearalgebra::matmul.
# Returns a nxm matrix, by computing the dot-product of rows and columns
#
# Syntax:
# matmul $A $B
#
# Arguments:
# A B Matrices, matching inner dimensions (e.g. nxq and qxm)
proc ::ndlist::matmul {A B} {
# Check dimensions
if {[llength [lindex $A 0]] != [llength $B]} {
return -code error "incompatible inner matrix dimensions"
}
# Transpose B matrix for easy multiplication
set BT [transpose $B]
# Perform dot-product of all rows and columns
lmap rowA $A {
lmap colB $BT {
dot $rowA $colB
}
}
}
# outerprod --
#
# Outer product of two vectors
# [ a1 a2 ] x [ b1 b2 b3 ] = [ a1b1 a1b2 a1b3 ]
# [ a2b1 a2b2 a2b3 ]
# Syntax:
# outerprod $a $b
#
# Arguments:
# a b Vectors (equal length) to take outer product of.
proc ::ndlist::outerprod {a b} {
matmul $a [list $b]
}
# kronprod --
#
# Kronecker product of two matrices (vector spaces)
# Example:
#
# [ a11 a12 ] [ a11b11 a11b12 a12b11 a12b12]
# [ a21 a22 ] (x) [ b11 b12 ] = [ a21b11 a21b12 a22b11 a22b12]
#
# Syntax:
# kronprod $A $B
#
# Arguments:
# A B Vector spaces to take Kronecker product of.
proc ::ndlist::kronprod {A B} {
block [lmap rowA $A {lmap valueA $rowA {lmap rowB $B {lmap valueB $rowB {
expr {$valueA * $valueB}
}}}}]
}
# zip --
#
# Zip vectors (equal length) into a tuple list
# To unzip, use lassign and transpose.
# lassign [transpose $tuples] a b
#
# Syntax:
# zip $a $b ...
#
# Arguments:
# a b Vectors (equal length)
proc ::ndlist::zip {a b} {
if {[llength $a] != [llength $b]} {
return -code error "mismatched list lengths"
}
lmap ai $a bi $b {
list $ai $bi
}
}
# zip3 --
#
# Zip three vectors (equal length) into a triple
# To unzip, use lassign and transpose.
# lassign [transpose $triples] a b c
#
# Syntax:
# zip3 $a $b $c
#
# Arguments:
# a b c Vectors (equal length)
proc ::ndlist::zip3 {a b c} {
if {[llength $a] != [llength $b] || [llength $a] != [llength $c]} {
return -code error "mismatched list lengths"
}
lmap ai $a bi $b ci $c {
list $ai $bi $ci
}
}
# cartprod --
#
# Cartesian product of multiple vectors (can have duplicates)
# Returns a list of all combinations
# Modified from "cartesianNaryProduct", accessed on 12/15/2021 at
# https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
#
# Syntax:
# cartprod $arg ...
#
# Arguments:
# arg ... Vectors to take "cartesian product" of
proc ::ndlist::cartprod {args} {
foreach vector [lassign $args matrix] {
set newMatrix {}
foreach row $matrix {
foreach value $vector {
lappend newMatrix [linsert $row end $value]
}
}
set matrix $newMatrix
}
return $matrix
}