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