forked from fricas/fricas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mama.spad
209 lines (153 loc) · 6.03 KB
/
mama.spad
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
)abbrev package MAMA MatrixManipulation
++ Author: Raoul Bourquin
++ Date Created: 17 November 2012
++ Description:
++ Some functions for manipulating (dense) matrices.
++ Supported are various kinds of slicing, splitting
++ and stacking of matrices. The functions resemble
++ operations often used in numerical linear algebra
++ algorithms.
MatrixManipulation(R, Row, Col, M) : Exports == Implementation where
R : Type
Row : FiniteLinearAggregate R
Col : FiniteLinearAggregate R
M : TwoDimensionalArrayCategory(R, Row, Col)
I ==> Integer
PI ==> PositiveInteger
NNI ==> NonNegativeInteger
LI ==> List I
SI ==> Segment I
LNNI ==> List NNI
Exports ==> with
-- Slicing matrices
element : (M, I, I) -> M
++ \spad{element} returns a single element out of a matrix.
++ The element is put into a one by one matrix.
rowMatrix : (M, I) -> M
++ \spad{rowMatrix} returns a single row out of a matrix.
++ The row is put into a one by N matrix.
rows : (M, LI) -> M
++ \spad{rows} returns several rows out of a matrix.
++ The rows are stacked into a matrix.
rows : (M, SI) -> M
++ \spad{rows} returns several rows out of a matrix.
++ The rows are stacked into a matrix.
columnMatrix : (M, I) -> M
++ \spad{columnMatrix} returns a single column out of a matrix.
++ The column is put into a one by N matrix.
columns : (M, LI) -> M
++ \spad{columns} returns several columns out of a matrix.
++ The columns are stacked into a matrix.
columns : (M, SI) -> M
++ \spad{columns} returns several columns out of a matrix.
++ The columns are stacked into a matrix.
subMatrix : (M, LI, LI) -> M
++ \spad{subMatrix} returns several elements out of a matrix.
++ The elements are stacked into a submatrix.
subMatrix : (M, SI, SI) -> M
++ \spad{subMatrix} returns several elements out of a matrix.
++ The elements are stacked into a submatrix.
if R has AbelianMonoid then
diagonalMatrix : (M, I) -> M
++ \spad{diagonalMatrix} returns a diagonal out of a matrix.
++ The diagonal is put into a matrix of same shape as the
++ original one. Positive integer arguments select upper
++ off-diagonals, negative ones lower off-diagonals.
diagonalMatrix : M -> M
++ \spad{diagonalMatrix} returns the main diagonal out of
++ a matrix. The diagonal is put into a matrix of same shape
++ as the original one.
bandMatrix : (M, LI) -> M
++ \spad{bandMatrix} returns multiple diagonals out of a matrix.
++ The diagonals are put into a matrix of same shape as the
++ original one. Positive integer arguments select upper
++ off-diagonals, negative ones lower off-diagonals.
bandMatrix : (M, SI) -> M
++ \spad{bandMatrix} returns multiple diagonals out of a matrix.
++ The diagonals are put into a matrix of same shape as the
++ original one. Positive integer arguments select upper
++ off-diagonals, negative ones lower off-diagonals.
-- Splitting matrices
blockSplit : (M, LNNI, PI) -> List List M
++ \spad{blockSplit} splits a matrix into multiple
++ submatrices row and column wise, dividing
++ a matrix into blocks.
blockSplit : (M, PI, LNNI) -> List List M
++ \spad{blockSplit} splits a matrix into multiple
++ submatrices row and column wise, dividing
++ a matrix into blocks.
Implementation ==> add
minr ==> minRowIndex
maxr ==> maxRowIndex
minc ==> minColIndex
maxc ==> maxColIndex
element(A, r, c) ==
subMatrix(A, r, r, c, c)
rowMatrix(A : M, r : I) : M ==
subMatrix(A, r, r, minc A, maxc A)
rows(A : M, lst : LI) : M ==
nc := ncols(A)
nc = 0 => qnew(#lst, nc)$M
ls := expand((minc A .. maxc A)@SI)
subMatrix(A, lst, ls)
rows(A : M, si : SI) : M ==
rows(A, expand(si))
columnMatrix(A : M, c : I) : M ==
subMatrix(A, minr A, maxr A, c, c)
columns(A:M, lst : LI) : M ==
nr := nrows(A)
nr = 0 => qnew(nr, #lst)$M
ls := expand((minr A .. maxr A)@SI)
subMatrix(A, ls, lst)
columns(A : M, si : SI) : M ==
columns(A, expand(si))
fill_diagonal(B : M, A : M, nr : NNI, nc : NNI, n : I) : Void ==
n > (nc-1) => error "requested diagonal out of range"
n < 0 and abs(n) > (nr-1) => error "requested diagonal out of range"
if n >= 0 then
dl := min(nc-n, nr)
sr := minr(A)
sc := minc(A) + n
else
dl := min(nc, nr-abs(n))
sr := minr(A) + abs(n)
sc := minc(A)
for i in 0..(dl-1) repeat
qsetelt!(B, sr+i, sc+i, A(sr+i, sc+i))
if R has AbelianMonoid then
diagonalMatrix(A, n) ==
nr := nrows(A)
nc := ncols(A)
B := new(nr, nc, 0$R)
fill_diagonal(B, A, nr, nc, n)
B
diagonalMatrix(A) ==
diagonalMatrix(A, 0)
bandMatrix(A:M, ln:LI) : M ==
nr := nrows(A)
nc := ncols(A)
B := new(nr, nc, 0$R)
for n in ln repeat
fill_diagonal(B, A, nr, nc, n)
B
bandMatrix(A:M, si:SI) : M ==
bandMatrix(A, expand(si))
subMatrix(A:M, lr:LI, lc:LI) : M ==
n := #lr
m := #lc
minR := minr A
minC := minc A
res := qnew(n, m)$M
for i in 1..n for ii in lr repeat
for j in 1..m for jj in lc repeat
qsetelt!(res, minR-1+i, minC-1+j, qelt(A, ii, jj))
res
subMatrix(A:M, sr:SI, sc:SI) : M ==
subMatrix(A, low sr, high sr, low sc, high sc)
-- Stack matrices
blockSplit(A:M, lr:LNNI, nc:PI) : List List M ==
--map( (X:M):(List M) +-> horizSplit(X, nc), vertSplit(A, lr) )$ListFunctions2(M, List M)
[ horizSplit(X, nc) for X in vertSplit(A, lr) ]
blockSplit(A:M, nr:PI, lc:LNNI) : List List M ==
--map( (X:M):(List M) +-> horizSplit(X, lc), vertSplit(A, nr) )$ListFunctions2(M, List M)
[ horizSplit(X, lc) for X in vertSplit(A, nr) ]