File: amatrix.mac

package info (click to toggle)
maxima 5.44.0-2
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 108,368 kB
  • sloc: lisp: 383,860; fortran: 14,665; perl: 14,369; tcl: 11,147; sh: 4,517; makefile: 2,580; ansic: 447; python: 262; xml: 59; awk: 37; sed: 17
file content (337 lines) | stat: -rw-r--r-- 11,967 bytes parent folder | download | duplicates (2)
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
/* copyright 2007 by Robert Dodier
 * I release this file under the terms of the GNU General Public License.
 */

/* operations on amatrix objects */

matchdeclare ([A%, A1%, A2%], amatrixp);
matchdeclare ([X%, X1%, X2%], lambda ([e], not amatrixp (e)));

/* troublesome ??: tellsimp (is (A%), amatrixmap (is, A%)); */

tellsimpafter (A% < X%, amatrixmap (buildq ([X%], lambda ([a%], a% < X%)), A%));
tellsimpafter (A% <= X%, amatrixmap (buildq ([X%], lambda ([a%], a% <= X%)), A%));
tellsimpafter (A% = X%, amatrixmap (buildq ([X%], lambda ([a%], a% = X%)), A%));
tellsimpafter (equal (A%, X%), amatrixmap (buildq ([X%], lambda ([a%], equal (a%, X%))), A%));
tellsimpafter (notequal (A%, X%), amatrixmap (buildq ([X%], lambda ([a%], notequal (a%, X%))), A%));
tellsimpafter (A% # X%, amatrixmap (buildq ([X%], lambda ([a%], a% # X%)), A%));
tellsimpafter (A% >= X%, amatrixmap (buildq ([X%], lambda ([a%], a% >= X%)), A%));
tellsimpafter (A% > X%, amatrixmap (buildq ([X%], lambda ([a%], a% > X%)), A%));

tellsimpafter (X% < A%, amatrixmap (buildq ([X%], lambda ([a%], X% < a%)), A%));
tellsimpafter (X% <= A%, amatrixmap (buildq ([X%], lambda ([a%], X% <= a%)), A%));
tellsimpafter (X% = A%, amatrixmap (buildq ([X%], lambda ([a%], X% = a%)), A%));
tellsimpafter (equal (X%, A%), amatrixmap (buildq ([X%], lambda ([a%], equal (X%, a%))), A%));
tellsimpafter (notequal (X%, A%), amatrixmap (buildq ([X%], lambda ([a%], notequal (X%, a%))), A%));
tellsimpafter (X% # A%, amatrixmap (buildq ([X%], lambda ([a%], X% # a%)), A%));
tellsimpafter (X% >= A%, amatrixmap (buildq ([X%], lambda ([a%], X% >= a%)), A%));
tellsimpafter (X% > A%, amatrixmap (buildq ([X%], lambda ([a%], X% > a%)), A%));

tellsimpafter (A1% < A2%, amatrixmap ("<", A1%, A2%));
tellsimpafter (A1% <= A2%, amatrixmap ("<=", A1%, A2%));
tellsimpafter (A1% = A2%, amatrixmap ("=", A1%, A2%));
tellsimpafter (equal (A1%, A2%), amatrixmap ('equal, A1%, A2%));
tellsimpafter (notequal (A1%, A2%), amatrixmap ('notequal, A1%, A2%));
tellsimpafter (A1% # A2%, amatrixmap ("#", A1%, A2%));
tellsimpafter (A1% >= A2%, amatrixmap (">=", A1%, A2%));
tellsimpafter (A1% > A2%, amatrixmap (">", A1%, A2%));

tellsimpafter (not A%, amatrixmap ("not", A%));
tellsimpafter (A1% and A2%, amatrixmap (lambda ([a1%, a2%], a1% and a2%), A1%, A2%));
tellsimpafter (A1% or A2%, amatrixmap (lambda ([a1%, a2%], a1% or a2%), A1%, A2%));

/* implementation */

defstruct (amatrix (nr, r0, rinc, nc, c0, cinc, storage));

elements (m) := create_list (get_element (m, i, j), i, 1, m@nr, j, 1, m@nc);

make_matrix  (nr, nc, [a]) := block
  ([aa : ?gensym()],
    ?putprop (aa, 1, '?refcount),

    if a = []
    then a : make_array (fixnum, nr*nc)
    /* ELSE STORAGE ARRAY IS SPECIFIED; SHOULD VERIFY HERE THAT
     * (1) IT IS 1-DIMENSIONAL, AND (2) IT IS BIG ENOUGH
     */
    else a : a[1],

    ?putprop (aa, a, '?storage_array),
    new (amatrix (nr, 0, 1, nc, 0, nr, aa)));

/* DECLARING A% SYMBOLP CAUSES TROUBLE IF STORAGE GENSYM IS EVALUATED ...
 * CONSIDER ATTACHING ARRAY TO GENSYM AS A PROPERTY INSTEAD OF A VALUE (SIGH)
 */
matchdeclare
   (a%, symbolp,
    [LI%, LI1%, LI2%], integer_listp,
    [MB%, MB1%, MB2%], putative_boolean_amatrixp,
    [ii%, jj%, i0%, j0%, u%, v%, x%, y%], integerp);

integer_listp (e) := not atom(e) and op(e) = "[" and every (integerp, e);
putative_boolean_amatrixp (e) :=
  not atom(e)
    and op(e) = amatrix
    and every (lambda ([x], not atom(x) or booleanp(x)), elements (e));
booleanp (e) := is (e = true or e = false);

/* USEFUL FOR DEBUGGING
announce_rules_firing : true;
 */

incprop (aa, bb) := ?putprop (aa, ?get (aa, bb) + 1, bb);

decprop (aa, bb) := ?putprop (aa, ?get (aa, bb) - 1, bb);

simp : false;

get_element (m%, ii%, jj%) :=
    get_element_internal (m%@nr, m%@r0, m%@rinc, m%@nc, m%@c0, m%@cinc, m%@storage, ii%, jj%);

get_element_internal (u%, i0%, x%, v%, j0%, y%, a%, ii%, jj%) := block

  ([a% : ?get (a%, '?storage_array),
    i% : (i0% + ii% - 1)*x% + (j0% + jj% - 1)*y%],

    if ii% < 1 or jj% < 1 or ii% > u% or jj% > v%
    then error ("Matrix index out of bounds")
    else ?aref (a%, i%));

/* one integer */

tellsimpafter
    (amatrix (1, i0%, x%, v%, j0%, y%, a%) [ii%],
     get_element_internal (1, i0%, x%, v%, j0%, y%, a%, 1, ii%));

tellsimpafter
    (amatrix (u%, i0%, x%, 1, j0%, y%, a%) [ii%],
     get_element_internal (u%, i0%, x%, 1, j0%, y%, a%, ii%, 1));

/* integers both */

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [ii%, jj%],
     get_element_internal (u%, i0%, x%, v%, j0%, y%, a%, ii%, jj%));

/* one 'all */

tellsimpafter
    (amatrix (1, i0%, x%, v%, j0%, y%, a%) [all],
    (incprop (a%, '?refcount),
     amatrix (1, i0%, x%, v%, j0%, y%, a%)));
     
tellsimpafter
    (amatrix (u%, i0%, x%, 1, j0%, y%, a%) [all],
    (incprop (a%, '?refcount),
     amatrix (u%, i0%, x%, 1, j0%, y%, a%)));
     
/* 'all and an integer */

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [all, jj%],
    (incprop (a%, '?refcount),
     amatrix (u%, i0%, x%, 1, j0% + jj% - 1, y%, a%)));

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [ii%, all],
    (incprop (a%, '?refcount),
     amatrix (1, i0% + ii% - 1, x%, v%, j0%, y%, a%)));

/* 'all both */

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [all, all],
    (incprop (a%, '?refcount),
     amatrix (u%, i0%, x%, v%, j0%, y%, a%)));

/* one list of integers */

tellsimpafter
    (amatrix (1, i0%, x%, v%, j0%, y%, a%) [LI%],
     submatrix_by_indices (1, i0%, x%, v%, j0%, y%, a%, [1], LI%));

tellsimpafter
    (amatrix (u%, i0%, x%, 1, j0%, y%, a%) [LI%],
     submatrix_by_indices (u%, i0%, x%, 1, j0%, y%, a%, LI%, [1]));

/* list of integers and an integer */

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [ii%, LI%],
     submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, [ii%], LI%));

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [LI%, jj%],
     submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, LI%, [jj%]));

/* list of integers and 'all */

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [all, LI%],
     submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, makelist (i, i, 1, u%), LI%));

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [LI%, all],
     submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, LI%, makelist (i, i, 1, v%)));

/* lists of integers both */

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [LI1%, LI2%],
     submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, LI1%, LI2%));

/* one amatrix of booleans */

tellsimpafter
    (amatrix (1, i0%, x%, v%, j0%, y%, a%) [MB%],
     submatrix_by_indices_and_flags (1, i0%, x%, v%, j0%, y%, a%, [1], MB%));

tellsimpafter
    (amatrix (u%, i0%, x%, 1, j0%, y%, a%) [MB%],
     submatrix_by_flags_and_indices (u%, i0%, x%, 1, j0%, y%, a%, MB%, [1]));

/* amatrix of booleans and an integer */

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [ii%, MB%],
     submatrix_by_indices_and_flags (u%, i0%, x%, v%, j0%, y%, a%, [ii%], MB%));

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [MB%, jj%],
     submatrix_by_flags_and_indices (u%, i0%, x%, v%, j0%, y%, a%, MB%, [jj%]));

/* amatrix of booleans and 'all */

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [all, MB%],
     submatrix_by_all_and_flags (u%, i0%, x%, v%, j0%, y%, a%, MB%));

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [MB%, all],
     submatrix_by_flags_and_all (u%, i0%, x%, v%, j0%, y%, a%, MB%));

/* amatrix of booleans and a list of integers */

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [LI%, MB%],
     submatrix_by_indices_and_flags (u%, i0%, x%, v%, j0%, y%, a%, LI%, MB%));

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [MB%, LI%],
     submatrix_by_flags_and_indices (u%, i0%, x%, v%, j0%, y%, a%, MB%, LI%));

/* amatrix of booleans both */

tellsimpafter
    (amatrix (u%, i0%, x%, v%, j0%, y%, a%) [MB1%, MB2%],
     submatrix_by_flags_and_flags (u%, i0%, x%, v%, j0%, y%, a%, MB1%, MB2%));

simp : true;

submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, LI1%, LI2%) := block
   ([M0 : amatrix (u%, i0%, x%, v%, j0%, y%, a%),
     M : make_matrix (length (LI1%), length (LI2%)),
     ?\*afterflag : false],
    /* Do this the simple way.
     * Probably some special cases could be faster.
     */
    for i:1 thru nrows(M)
        do for j:1 thru ncols(M)
            do M[i, j] : get_element (M0, LI1%[i], LI2%[j]),
    M);

submatrix_by_indices_and_flags (u%, i0%, x%, v%, j0%, y%, a%, LI%, MB%) := block
   ([LI2% : sublist_indices (elements (MB%), lambda ([x], is (is(x) = true)))],
    submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, LI%, LI2%));

submatrix_by_flags_and_indices (u%, i0%, x%, v%, j0%, y%, a%, MB%, LI%) := block
   ([LI1% : sublist_indices (elements (MB%), lambda ([x], is (is(x) = true)))],
    submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, LI1%, LI%));

submatrix_by_all_and_flags (u%, i0%, x%, v%, j0%, y%, a%, MB%) := block
  ([LI1% : makelist (i, i, 1, u%),
    LI2% : sublist_indices (elements (MB%), lambda ([x], is (is(x) = true)))],
    submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, LI1%, LI2%));

submatrix_by_flags_and_all (u%, i0%, x%, v%, j0%, y%, a%, MB%) := block
  ([LI1% : sublist_indices (elements (MB%), lambda ([x], is (is(x) = true)))],
    LI2% : makelist (i, i, 1, v%),
    submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, LI1%, LI2%));
   
submatrix_by_flags_and_flags (u%, i0%, x%, v%, j0%, y%, a%, MB1%, MB2%) := block
  ([LI1% : sublist_indices (elements (MB1%), lambda ([x], is (is(x) = true))),
    LI2% : sublist_indices (elements (MB2%), lambda ([x], is (is(x) = true)))],
    submatrix_by_indices (u%, i0%, x%, v%, j0%, y%, a%, LI1%, LI2%));

t(m):=
    (incprop (m@storage, '?refcount),
     amatrix
        (m@nc, m@c0,
         if m@rinc = 1 then m@nr else 1,
         m@nr, m@r0,
         if m@cinc = 1 then m@nc else 1,
         m@storage));

compute_index1 (aa, i, j) :=
    if i < 1 or j < 1 or i > aa@nr or j > aa@nc
    then error ("Matrix index out of bounds")
    else 1 + (aa@r0 + i - 1)*aa@rinc + (aa@c0 + j - 1)*aa@cinc;
    
compute_index0 (aa, i, j) :=
    if i < 0 or j < 0 or i >= aa@nr or j >= aa@nc
    then error ("Matrix index out of bounds")
    else (aa@r0 + i)*aa@rinc + (aa@c0 + j)*aa@cinc;

/* REVISIT THE FOLLOWING !! IT IS BROKEN AS IT STANDS !! */
copy_amatrix (m) :=
    if atom(m) or op(m) # 'amatrix
    then copy (m)
    else 
       (incprop (m@storage, '?refcount),
        amatrix (m@nr, m@r0, m@rinc, m@nc, m@c0, m@cinc, m@storage));
        /* copy(m) copies everything but the storage array -- that's just what we want */
        /* comment out until storage is actually an array
        block ([m2 : copy(m)], m2@refcount : m2@refcount + 1, m2);
         */

copy_array (a) := if ?arrayp (a) then ?copy\-seq (a) else ?copy\-tree (a);

if not ?get ('amatrix, 'present) then load ("amatrix.lisp");

/* SHOULD TRY TO MERGE THIS WITH EXISTING MEAN FUNCTION IN SHARE PACKAGE DESCRIPTIVE */
amatrix_mean (x) := block
  ([m : nrows(x),
    n : ncols(x)],
    if n=1
        then 1/m * sum (x [i], i, 1, m)
        else makelist (mean (x [all, j]), j, 1, n));

matchdeclare (A%, amatrixp);
tellsimp (mean (A%), amatrix_mean (A%));

/* FOLLOWING NEEDED MOSTLY FOR TESTING ALTHOUGH IT WOULD BE OK TO LEAVE IT IN */

random_matrix (nr, nc) := block
  ([M : make_matrix (nr, nc), A],
    A : ?get (M@storage, '?storage_array),
    for i from 0 thru nr*nc-1
        do A[i] : floor (random (256.0)) / 256.0,
    M);

new2old_matrix (M) :=
  if nrows(M) = 0 or ncols(M) = 0
    then matrix()
    else genmatrix (lambda ([i, j], get_element (M, i, j)), nrows(M), ncols(M));


old2new_matrix (M) := block ([nr, nc, M2],
    nr : length (M),
    nc : if nr = 0 then 0 else length (M [1]),
    M2 : make_matrix (nr, nc),
    for i thru nr
        do for j thru nc
            do M2 [i, j] : M [i, j],
    M2);