5091
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
5375
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2018 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
5082
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
AbstractMultidimensionalArray variableSubclass:#Matrix
|
|
19 |
instanceVariableNames:'dimensions'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-MultiDimensional'
|
|
23 |
!
|
|
24 |
|
|
25 |
!Matrix class methodsFor:'documentation'!
|
|
26 |
|
5375
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2018 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
5082
|
41 |
documentation
|
|
42 |
"
|
|
43 |
Generic Matrix with arbitrary number of dimensions.
|
|
44 |
Especially useful with the ArrayIndexing-Parser extension.
|
|
45 |
|
|
46 |
[author:]
|
|
47 |
Claus Gittinger (cg@sinir)
|
|
48 |
|
|
49 |
[instance variables:]
|
|
50 |
|
|
51 |
[class variables:]
|
|
52 |
|
|
53 |
[see also:]
|
5109
|
54 |
MatrixAccessor
|
5082
|
55 |
"
|
|
56 |
!
|
|
57 |
|
|
58 |
examples
|
|
59 |
"
|
|
60 |
|
|
61 |
You have to enable the Parsers arrayIndexingExtension support
|
|
62 |
in order to be able to execute the examples below.
|
|
63 |
|
|
64 |
Parser allowArrayIndexSyntaxExtension:true
|
|
65 |
|
|
66 |
[exBegin]
|
|
67 |
|m|
|
|
68 |
|
|
69 |
m := Matrix[3,3].
|
|
70 |
m[2,1] := 11.
|
|
71 |
m[2,2] := 12.
|
|
72 |
m[2,3] := 13.
|
|
73 |
m
|
|
74 |
[exEnd]
|
|
75 |
|
|
76 |
[exBegin]
|
|
77 |
|m|
|
|
78 |
|
|
79 |
m := Matrix3_3 new.
|
|
80 |
m[2,1] := 11.
|
|
81 |
m[2,2] := 12.
|
|
82 |
m[2,3] := 13.
|
|
83 |
m
|
|
84 |
[exEnd]
|
|
85 |
|
|
86 |
[exBegin]
|
|
87 |
|m1 m2|
|
|
88 |
|
|
89 |
m1 := Matrix3_3 new.
|
|
90 |
m1 atAllPut:1.
|
|
91 |
|
|
92 |
m2 := Matrix3_3 new.
|
|
93 |
m2 atAllPut:2.
|
|
94 |
|
|
95 |
m1 - m2
|
|
96 |
[exEnd]
|
|
97 |
|
|
98 |
[exBegin]
|
|
99 |
|m1 m2|
|
|
100 |
|
|
101 |
m1 := Matrix3_3 new.
|
|
102 |
m1 atAllPut:1.
|
|
103 |
|
|
104 |
m2 := Matrix3_3 new.
|
|
105 |
m2 atAllPut:2.
|
|
106 |
|
|
107 |
m1 + m2
|
|
108 |
[exEnd]
|
|
109 |
"
|
|
110 |
! !
|
|
111 |
|
|
112 |
!Matrix class methodsFor:'instance creation'!
|
|
113 |
|
|
114 |
_at:nIndices
|
|
115 |
"this is a synthetic selector, generated by the compiler,
|
|
116 |
if a construct of the form expr[idx...] is parsed.
|
|
117 |
I.e.
|
|
118 |
Matrix[n]
|
|
119 |
generates
|
|
120 |
Matrix _at: n
|
|
121 |
"
|
|
122 |
|
|
123 |
^ Array new:nIndices
|
|
124 |
!
|
|
125 |
|
|
126 |
_at:dim1 at:dim2
|
|
127 |
"this is a synthetic selector, generated by the compiler,
|
|
128 |
if a construct of the form expr[idx...] is parsed.
|
|
129 |
I.e.
|
5134
|
130 |
Matrix[n][m]
|
5082
|
131 |
generates
|
|
132 |
Matrix _at:n at:m
|
|
133 |
"
|
|
134 |
|
|
135 |
^ self newForRows:dim1 cols:dim2
|
|
136 |
!
|
|
137 |
|
|
138 |
_at:dim1 at:dim2 at:dim3
|
|
139 |
"this is a synthetic selector, generated by the compiler,
|
|
140 |
if a construct of the form expr[idx...] is parsed.
|
|
141 |
I.e.
|
5134
|
142 |
Matrix[n][m][o]
|
5082
|
143 |
generates
|
|
144 |
Matrix _at:n at:m at:o
|
|
145 |
"
|
|
146 |
|
|
147 |
^ (self basicNew:(dim1 * dim2 * dim3))
|
|
148 |
dimensions:(Array with:dim1 with:dim2 with:dim3)
|
|
149 |
!
|
|
150 |
|
|
151 |
identity: anInteger
|
|
152 |
"Answer an identity matrix of order 'anInteger@anInteger'."
|
|
153 |
|
|
154 |
| mat |
|
|
155 |
|
|
156 |
mat := self newForRows:anInteger cols:anInteger.
|
|
157 |
mat atAllPut:0.
|
|
158 |
1 to: anInteger do: [ :i | mat _at: i at: i put: 1 ].
|
|
159 |
^ mat
|
|
160 |
|
|
161 |
"
|
|
162 |
self identity:3
|
|
163 |
self identity:2
|
|
164 |
self identity:5
|
|
165 |
"
|
|
166 |
!
|
|
167 |
|
|
168 |
newForRows:nRows cols:nCols
|
|
169 |
|mClass|
|
|
170 |
|
|
171 |
nRows == nCols ifTrue:[
|
|
172 |
nCols == 2 ifTrue:[
|
|
173 |
^ Matrix2_2 new dimensions:#(2 2)
|
|
174 |
].
|
|
175 |
nCols == 3 ifTrue:[
|
|
176 |
^ Matrix3_3 new dimensions:#(3 3)
|
|
177 |
].
|
|
178 |
mClass := SquareMatrix
|
|
179 |
] ifFalse:[
|
|
180 |
mClass := self
|
|
181 |
].
|
|
182 |
|
|
183 |
^ (mClass basicNew:(nRows * nCols))
|
|
184 |
dimensions:(Array with:nRows with:nCols)
|
|
185 |
!
|
|
186 |
|
|
187 |
ones: aPoint
|
|
188 |
"Answer a matrix of order 'aPoint' filled with ones"
|
|
189 |
|
|
190 |
|mat|
|
|
191 |
|
|
192 |
mat := self newForRows:aPoint y cols:aPoint x.
|
|
193 |
mat atAllPut: 1.
|
|
194 |
^ mat
|
|
195 |
|
|
196 |
"
|
|
197 |
self ones:(3 @ 3)
|
|
198 |
self ones:(3 @ 2)
|
|
199 |
self ones:(5 @ 5)
|
|
200 |
"
|
|
201 |
!
|
|
202 |
|
|
203 |
zero: aPoint
|
|
204 |
"Answer a matrix of order 'aPoint' filled with zeros"
|
|
205 |
|
|
206 |
|mat|
|
|
207 |
|
|
208 |
mat := self newForRows:aPoint y cols:aPoint x.
|
|
209 |
mat atAllPut: 0.
|
|
210 |
^ mat
|
|
211 |
|
|
212 |
"
|
|
213 |
self zero:(3 @ 3)
|
|
214 |
self zero:(3 @ 2)
|
|
215 |
self zero:(5 @ 5)
|
|
216 |
"
|
|
217 |
! !
|
|
218 |
|
|
219 |
!Matrix methodsFor:'accessing'!
|
|
220 |
|
|
221 |
_at:index
|
|
222 |
"this is a synthetic selector, generated by the compiler,
|
|
223 |
if a construct of the form expr[idx...] is parsed.
|
|
224 |
I.e.
|
|
225 |
foo[n]
|
|
226 |
generates
|
|
227 |
foo _at: n
|
|
228 |
"
|
|
229 |
dimensions size ~~ 1 ifTrue:[self dimensionError].
|
|
230 |
^ self basicAt:index.
|
|
231 |
!
|
|
232 |
|
|
233 |
_at:index1 at:index2
|
|
234 |
"this is a synthetic selector, generated by the compiler,
|
|
235 |
if a construct of the form expr[idx...] is parsed.
|
|
236 |
I.e.
|
5134
|
237 |
foo[n][m]
|
5082
|
238 |
generates
|
|
239 |
foo _at:n at:m
|
|
240 |
"
|
|
241 |
|idx|
|
|
242 |
|
|
243 |
dimensions size ~~ 2 ifTrue:[self dimensionError].
|
|
244 |
(index1 between:1 and:(dimensions at:1)) ifFalse:[self subscriptBoundsError:index1].
|
|
245 |
(index2 between:1 and:(dimensions at:2)) ifFalse:[self subscriptBoundsError:index2].
|
|
246 |
|
|
247 |
idx := ((index1-1) * (dimensions at:2)).
|
|
248 |
idx := idx + index2.
|
|
249 |
^ self basicAt:idx.
|
|
250 |
|
|
251 |
"
|
|
252 |
|m|
|
5109
|
253 |
m := MatrixAccessor new
|
5082
|
254 |
collection:
|
|
255 |
#(
|
|
256 |
11 12 13 14
|
|
257 |
21 22 23 24
|
|
258 |
31 32 33 34
|
|
259 |
)
|
|
260 |
dimensions:
|
|
261 |
#(3 4).
|
|
262 |
|
|
263 |
m[1,4]
|
|
264 |
"
|
|
265 |
!
|
|
266 |
|
|
267 |
_at:index1 at:index2 at:index3
|
|
268 |
"this is a synthetic selector, generated by the compiler,
|
|
269 |
if a construct of the form expr[idx...] is parsed.
|
|
270 |
I.e.
|
5134
|
271 |
foo[n][m][o]
|
5082
|
272 |
generates
|
|
273 |
foo _at:n at:m at:o
|
|
274 |
"
|
|
275 |
|idx|
|
|
276 |
|
|
277 |
dimensions size ~~ 3 ifTrue:[self dimensionError].
|
|
278 |
(index1 between:1 and:(dimensions at:1)) ifFalse:[self subscriptBoundsError:index1].
|
|
279 |
(index2 between:1 and:(dimensions at:2)) ifFalse:[self subscriptBoundsError:index2].
|
|
280 |
(index3 between:1 and:(dimensions at:3)) ifFalse:[self subscriptBoundsError:index3].
|
|
281 |
|
|
282 |
idx := ((((index1-1) * (dimensions at:2)) + (index2-1)) * (dimensions at:3)).
|
|
283 |
idx := idx + index3.
|
|
284 |
^ self basicAt:idx.
|
|
285 |
|
|
286 |
"
|
|
287 |
|m|
|
5109
|
288 |
m := MatrixAccessor new
|
5082
|
289 |
collection:
|
|
290 |
#(
|
|
291 |
111 112 113 114
|
|
292 |
121 122 123 124
|
|
293 |
131 132 133 134
|
|
294 |
|
|
295 |
211 212 213 214
|
|
296 |
221 222 223 224
|
|
297 |
231 232 233 234 )
|
|
298 |
dimensions:
|
|
299 |
#(2 3 4).
|
|
300 |
|
|
301 |
m[2,1,4]
|
|
302 |
"
|
|
303 |
!
|
|
304 |
|
|
305 |
_at:index1 at:index2 at:index3 put:val
|
|
306 |
"this is a synthetic selector, generated by the compiler,
|
|
307 |
if a construct of the form expr[idx...] := val is parsed.
|
|
308 |
I.e.
|
5134
|
309 |
foo[n][m][o] := val
|
5082
|
310 |
generates
|
|
311 |
foo _at:n at:m at:o put:val
|
|
312 |
"
|
|
313 |
|idx|
|
|
314 |
|
|
315 |
dimensions size ~~ 3 ifTrue:[self dimensionError].
|
|
316 |
(index1 between:1 and:(dimensions at:1)) ifFalse:[self subscriptBoundsError:index1].
|
|
317 |
(index2 between:1 and:(dimensions at:2)) ifFalse:[self subscriptBoundsError:index2].
|
|
318 |
(index3 between:1 and:(dimensions at:3)) ifFalse:[self subscriptBoundsError:index3].
|
|
319 |
|
|
320 |
idx := ((((index1-1) * (dimensions at:2)) + (index2-1)) * (dimensions at:3)).
|
|
321 |
idx := idx + index3.
|
|
322 |
^ self basicAt:idx put:val.
|
|
323 |
!
|
|
324 |
|
|
325 |
_at:index1 at:index2 put:val
|
|
326 |
"this is a synthetic selector, generated by the compiler,
|
|
327 |
if a construct of the form expr[idx...] := val is parsed.
|
|
328 |
I.e.
|
5134
|
329 |
foo[n][m] := val
|
5082
|
330 |
generates
|
|
331 |
foo _at:n at:m put:val
|
|
332 |
"
|
|
333 |
|idx|
|
|
334 |
|
|
335 |
dimensions size ~~ 2 ifTrue:[self dimensionError].
|
|
336 |
(index1 between:1 and:(dimensions at:1)) ifFalse:[self subscriptBoundsError:index1].
|
|
337 |
(index2 between:1 and:(dimensions at:2)) ifFalse:[self subscriptBoundsError:index2].
|
|
338 |
|
|
339 |
idx := ((index1-1) * (dimensions at:2)).
|
|
340 |
idx := idx + index2.
|
|
341 |
^ self basicAt:idx put:val.
|
|
342 |
!
|
|
343 |
|
|
344 |
_at:index put:val
|
|
345 |
"this is a synthetic selector, generated by the compiler,
|
|
346 |
if a construct of the form expr[idx...] := val is parsed.
|
|
347 |
I.e.
|
|
348 |
foo[n] := val
|
|
349 |
generates
|
|
350 |
foo _at:n put:val
|
|
351 |
"
|
|
352 |
dimensions size ~~ 1 ifTrue:[self dimensionError].
|
|
353 |
^ self basicAt:index put:val.
|
|
354 |
!
|
|
355 |
|
|
356 |
at:index
|
|
357 |
"/ dimensions size ~~ 1 ifTrue:[self dimensionError].
|
|
358 |
^ self basicAt:index.
|
|
359 |
!
|
|
360 |
|
|
361 |
at:index put:val
|
|
362 |
"/ dimensions size ~~ 1 ifTrue:[self dimensionError].
|
|
363 |
^ self basicAt:index put:val.
|
|
364 |
!
|
|
365 |
|
|
366 |
dimensions
|
|
367 |
^ dimensions
|
|
368 |
!
|
|
369 |
|
|
370 |
size
|
|
371 |
^ self basicSize
|
|
372 |
! !
|
|
373 |
|
|
374 |
!Matrix methodsFor:'arithmetic'!
|
|
375 |
|
|
376 |
+ aMatrix
|
|
377 |
"Answer the result of adding 'aMatrix' to this matrix."
|
|
378 |
|
|
379 |
^ aMatrix sumFromMatrix:self
|
|
380 |
!
|
|
381 |
|
|
382 |
- aMatrix
|
|
383 |
"Answer the result of subtracting 'aMatrix' from this matrix."
|
|
384 |
|
|
385 |
^ aMatrix differenceFromMatrix:self
|
|
386 |
!
|
|
387 |
|
|
388 |
= aMatrix
|
|
389 |
"Answer true, if the argument represents the same matrix"
|
|
390 |
|
|
391 |
self species == aMatrix species ifFalse: [ ^ false ].
|
|
392 |
dimensions = aMatrix dimensions ifFalse: [ ^ false ].
|
|
393 |
|
|
394 |
1 to: self basicSize do:[ :idx |
|
|
395 |
(aMatrix basicAt: idx) == (self basicAt: idx) ifFalse:[^ false].
|
|
396 |
].
|
|
397 |
^ true
|
|
398 |
!
|
|
399 |
|
|
400 |
differenceFromMatrix: aMatrix
|
|
401 |
"Answer the result of subtracting the receiver from 'aMatrix'."
|
|
402 |
|
|
403 |
| result |
|
|
404 |
|
5091
|
405 |
dimensions = aMatrix dimensions ifFalse: [
|
|
406 |
IncompatibleMatrixError raiseRequestWith:aMatrix errorString:'argument matrix has different dimensions'
|
|
407 |
].
|
5082
|
408 |
|
|
409 |
result := (self class new:(self basicSize)) dimensions:dimensions.
|
|
410 |
|
|
411 |
1 to: self basicSize do:[ :idx |
|
|
412 |
result basicAt: idx put: (aMatrix basicAt: idx) - (self basicAt: idx).
|
|
413 |
].
|
|
414 |
^ result
|
|
415 |
!
|
|
416 |
|
|
417 |
sumFromMatrix: aMatrix
|
|
418 |
"Answer the result of adding 'aMatrix' to this matrix."
|
|
419 |
|
|
420 |
| result |
|
|
421 |
|
5091
|
422 |
dimensions = aMatrix dimensions ifFalse: [
|
|
423 |
IncompatibleMatrixError raiseRequestWith:aMatrix errorString:'argument matrix has different dimensions'
|
|
424 |
].
|
5082
|
425 |
|
|
426 |
result := (self class new:(self basicSize)) dimensions:dimensions.
|
|
427 |
|
|
428 |
1 to: self basicSize do:[ :idx |
|
|
429 |
result basicAt: idx put: (self basicAt: idx) + (aMatrix basicAt: idx).
|
|
430 |
].
|
|
431 |
^ result
|
|
432 |
! !
|
|
433 |
|
|
434 |
!Matrix methodsFor:'matrix operations'!
|
|
435 |
|
|
436 |
determinant
|
5091
|
437 |
IncompatibleMatrixError raiseRequestWith:self errorString: 'Matrix must be square'
|
5082
|
438 |
! !
|
|
439 |
|
|
440 |
!Matrix methodsFor:'private'!
|
|
441 |
|
|
442 |
dimensions:dimensionsArg
|
|
443 |
dimensionsArg isArray ifFalse:[
|
|
444 |
dimensions := Array with:dimensionsArg y with:dimensionsArg x.
|
|
445 |
] ifTrue:[
|
|
446 |
dimensions := dimensionsArg.
|
|
447 |
].
|
5121
|
448 |
!
|
|
449 |
|
|
450 |
setDimensions:dimensionsArg
|
|
451 |
dimensions := dimensionsArg.
|
|
452 |
|
5082
|
453 |
! !
|
|
454 |
|
|
455 |
!Matrix methodsFor:'queries'!
|
|
456 |
|
|
457 |
columns
|
|
458 |
^ dimensions at:2
|
|
459 |
!
|
|
460 |
|
|
461 |
isSquare
|
|
462 |
^ false
|
|
463 |
|
|
464 |
"
|
|
465 |
(self zero:(3 @ 3)) isSquare
|
|
466 |
(self zero:(2 @ 2)) isSquare
|
|
467 |
(self zero:(3 @ 2)) isSquare
|
|
468 |
"
|
|
469 |
!
|
|
470 |
|
|
471 |
rows
|
|
472 |
^ dimensions at:1
|
|
473 |
! !
|
|
474 |
|
|
475 |
!Matrix class methodsFor:'documentation'!
|
|
476 |
|
|
477 |
version_CVS
|
|
478 |
^ '$Header$'
|
|
479 |
! !
|
|
480 |
|