-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinearAlgebra.pck.st
2560 lines (2177 loc) · 85.7 KB
/
LinearAlgebra.pck.st
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'From Cuis7.1 [latest update: #6511] on 9 July 2024 at 7:23:09 pm'!
'Description '!
!provides: 'LinearAlgebra' 1 61!
SystemOrganization addCategory: #LinearAlgebra!
!classDefinition: #NumericalMatrix category: #LinearAlgebra!
Array2D subclass: #NumericalMatrix
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'LinearAlgebra'!
!classDefinition: 'NumericalMatrix class' category: #LinearAlgebra!
NumericalMatrix class
instanceVariableNames: ''!
!classDefinition: #ByteMatrix category: #LinearAlgebra!
NumericalMatrix subclass: #ByteMatrix
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'LinearAlgebra'!
!classDefinition: 'ByteMatrix class' category: #LinearAlgebra!
ByteMatrix class
instanceVariableNames: ''!
!classDefinition: #DoubleByteMatrix category: #LinearAlgebra!
NumericalMatrix subclass: #DoubleByteMatrix
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'LinearAlgebra'!
!classDefinition: 'DoubleByteMatrix class' category: #LinearAlgebra!
DoubleByteMatrix class
instanceVariableNames: ''!
!classDefinition: #FloatMatrix category: #LinearAlgebra!
NumericalMatrix subclass: #FloatMatrix
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'LinearAlgebra'!
!classDefinition: 'FloatMatrix class' category: #LinearAlgebra!
FloatMatrix class
instanceVariableNames: ''!
!classDefinition: #Float64Matrix category: #LinearAlgebra!
FloatMatrix subclass: #Float64Matrix
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'LinearAlgebra'!
!classDefinition: 'Float64Matrix class' category: #LinearAlgebra!
Float64Matrix class
instanceVariableNames: ''!
!classDefinition: #FloatBandMatrix category: #LinearAlgebra!
FloatMatrix subclass: #FloatBandMatrix
instanceVariableNames: 'bandWidth'
classVariableNames: ''
poolDictionaries: ''
category: 'LinearAlgebra'!
!classDefinition: 'FloatBandMatrix class' category: #LinearAlgebra!
FloatBandMatrix class
instanceVariableNames: ''!
!classDefinition: #IntegerMatrix category: #LinearAlgebra!
NumericalMatrix subclass: #IntegerMatrix
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'LinearAlgebra'!
!classDefinition: 'IntegerMatrix class' category: #LinearAlgebra!
IntegerMatrix class
instanceVariableNames: ''!
!NumericalMatrix commentStamp: 'jmv 7/19/2012 11:52' prior: 0!
My instances are m x n matrices, where m is the height and n the width. A matrix is a kind of 2-d array. A Matrix whose width is 1 is called a "Column Vector". A Matrix whose height is 1 is called a "Row Vector".
My instances can hold elements of any kind, but include some methods that assume that the elements understand arithmetic operations. These are useful for Matrices whose elements are Fractions, LargeIntegers, Matrices and Vectors (column or row Matrices). They are also suitable for Complex numbers, but in such cases, an alternative is to use different FloatMatrices for Real and Imaginary parts.!
!ByteMatrix commentStamp: '<historical>' prior: 0!
Elements are integers between 0 and 255, i.e. bytes.
ByteMatrix fromArrayOfArrays: #(
#[ 0 255 94 ]
#[ 4 127 128 ]
#[ 1 254 3 ])!
!DoubleByteMatrix commentStamp: 'jmv 7/9/2024 19:21:00' prior: 0!
Elements are integers between 0 and 65535, i.e. unsigned 16-bit words.
DoubleByteMatrix fromArrayOfArrays: #(
#( 0 255 65500)
#( 40000 127 128)
#( 32768 254 32767 ))
!
!FloatMatrix commentStamp: '<historical>' prior: 0!
My instances are m x n matrices of Float values, where m is the height and n the width. Suitable for LinearAlgebra and SignalProcessing suff, among many other possible applications. It is possible to build VM plugins for fast processing.!
!FloatBandMatrix commentStamp: '<historical>' prior: 0!
My instances can have non-zero values only in the diagonal, and a band of certain width around it. Save space.!
!IntegerMatrix commentStamp: '<historical>' prior: 0!
Elements are integers between -16r80000000 and 16r7FFFFFFF, i.e. signed 32-bit words.
IntegerMatrix fromArrayOfArrays: {
{ 0. 1. -1 }.
{ (2 raisedTo: 31) negated. -10000. -16r80000000}.
{ (2 raisedTo: 31) -1. 12 factorial. 16r7FFFFFFF }}!
!NumericalMatrix methodsFor: 'aritmethic' stamp: 'jmv 5/14/2019 09:37:18'!
* aMatrixOrNumber
"Standard matrix multiplication"
| result |
(aMatrixOrNumber is: #NumericalMatrix) ifTrue: [
width = aMatrixOrNumber height ifFalse: [ ^self error: 'Matrix sizes do not match' ].
result _ self appropriateResultClass height: height width: aMatrixOrNumber width.
result fillWith: self multipliedBy: aMatrixOrNumber ]
ifFalse: [
result _ self copy.
result replaceValues: [ :i :j :value | value * aMatrixOrNumber ] ].
^result
! !
!NumericalMatrix methodsFor: 'aritmethic' stamp: 'jmv 5/14/2019 09:37:18'!
+ aMatrixOrNumber
"Add element by element"
| result |
(aMatrixOrNumber is: #NumericalMatrix) ifTrue: [
(width = aMatrixOrNumber width) & (height = aMatrixOrNumber height)
ifFalse: [ ^self error: 'Matrix sizes do not match' ].
result _ self appropriateResultClass height: height width: width.
result fillWith: self plus: aMatrixOrNumber ]
ifFalse: [
result _ self copy.
result replaceValues: [ :i :j :value | value + aMatrixOrNumber ] ].
^result
! !
!NumericalMatrix methodsFor: 'aritmethic' stamp: 'jmv 5/14/2019 09:37:18'!
- aMatrixOrNumber
"Subtract element by element"
| result |
(aMatrixOrNumber is: #NumericalMatrix) ifTrue: [
(width = aMatrixOrNumber width) & (height = aMatrixOrNumber height)
ifFalse: [ ^self error: 'Matrix sizes do not match' ].
result _ self appropriateResultClass height: height width: width.
result fillWith: self minus: aMatrixOrNumber ]
ifFalse: [
result _ self copy.
result replaceValues: [ :i :j :value | value - aMatrixOrNumber ] ].
^result
! !
!NumericalMatrix methodsFor: 'aritmethic' stamp: 'jmv 11/16/2012 12:15'!
/ aNumber
"Divide by a scalar"
| reciprocal result |
reciprocal _ 1 / aNumber.
result _ self copy.
result replaceValues: [ :i :j :value | value * reciprocal ].
^result
! !
!NumericalMatrix methodsFor: 'aritmethic' stamp: 'jmv 7/19/2012 11:18'!
fillWith: aMatrix minus: bMatrix
| elem |
1 to: height do: [ :i |
1 to: width do: [ :j |
elem _ ((aMatrix i: i j: j) - (bMatrix i: i j: j)).
self i: i j: j put: elem ] ]! !
!NumericalMatrix methodsFor: 'aritmethic' stamp: 'jmv 7/19/2012 12:36'!
fillWith: aMatrix multipliedBy: bMatrix
| zeroElement elem |
zeroElement _ self zeroElement.
1 to: height do: [ :i |
1 to: width do: [ :j |
elem _ zeroElement.
1 to: aMatrix width do: [ :k |
elem _ ((aMatrix i: i j: k) * (bMatrix i: k j: j)) + elem ].
self i: i j: j put: elem ] ]! !
!NumericalMatrix methodsFor: 'aritmethic' stamp: 'jmv 7/19/2012 11:18'!
fillWith: aMatrix plus: bMatrix
| elem |
1 to: height do: [ :i |
1 to: width do: [ :j |
elem _ ((aMatrix i: i j: j) + (bMatrix i: i j: j)).
self i: i j: j put: elem ] ]! !
!NumericalMatrix methodsFor: 'misc' stamp: 'jmv 9/27/2007 23:35'!
appropriateResultClass
"Some subclasses might need to redefine this method.
For example to specify a more general class than themselves."
^self class! !
!NumericalMatrix methodsFor: 'conversion' stamp: 'jmv 6/25/2018 15:23:45'!
as32bitForm: aBlock
"Fill 32bit Form with rgb values as computed by aBlock. See senders."
| answer bits word index |
answer _ Form extent: width@height depth: 32.
bits _ answer bits.
word _ LargePositiveInteger new: 4.
word at: 4 put: 255.
index _ 1.
1 to: height do: [ :i |
1 to: width do: [ :j |
aBlock value: index value: word.
bits at: index put: word normalize.
index _ index + 1 ]].
^answer! !
!NumericalMatrix methodsFor: 'conversion' stamp: 'jmv 8/9/2018 13:39:11'!
asForm
"Assume the receiver is normalized in [0.0 .. 255/256.0]. Clip values out of range.
Consider calling the much faster #asFormQuick"
^ self asFormMin: 0.0 max: 255/256.0! !
!NumericalMatrix methodsFor: 'conversion' stamp: 'jmv 8/9/2018 13:42:37'!
asFormAutoRange
"Answer a 8bit Form with gray values.
Map used range of pixel values to available gray values."
^ self asFormMin: elements min max: elements max! !
!NumericalMatrix methodsFor: 'conversion' stamp: 'jmv 8/9/2018 13:51:07'!
asFormAutoRangeG: greenMatrix b: blueMatrix
| min max |
min _ (self min min: greenMatrix min) min: blueMatrix min.
max _ (self max max: greenMatrix max) max: blueMatrix max.
^self asFormG: greenMatrix b: blueMatrix min: min max: max! !
!NumericalMatrix methodsFor: 'conversion' stamp: 'jmv 8/9/2018 13:52:02'!
asFormG: greenMatrix b: blueMatrix
"Assume the receiver and arguments are normalized in [0.0 .. 255/256.0]. Clip values out of range.
Redefined in subclasses for performance."
^self asFormG: greenMatrix b: blueMatrix min: 0.0 max: 255/256.0! !
!NumericalMatrix methodsFor: 'conversion' stamp: 'jmv 6/25/2018 15:24:21'!
asFormG: greenMatrix b: blueMatrix min: min max: max
| k r g b blueElements greenElements |
k _ 255.0 / (max-min).
greenElements _ greenMatrix elements.
blueElements _ blueMatrix elements.
^ self as32bitForm: [ :index :word |
r _ (((elements at: index) - min) * k + 0.5) truncated min: 255 max: 0.
g _ (((greenElements at: index) - min) * k + 0.5) truncated min: 255 max: 0.
b _ (((blueElements at: index) - min) * k + 0.5) truncated min: 255 max: 0.
word at: 1 put: b; at: 2 put: g; at: 3 put: r ]! !
!NumericalMatrix methodsFor: 'conversion' stamp: 'jmv 8/9/2018 13:42:46'!
asFormMin: min max: max
"Answer a 8bit Form with gray values.
Map requested range of pixel values to available gray values."
| k |
k _ 255.0 / (max-min).
^ self asGrayForm: [ :index |
(((elements at: index) - min) * k + 0.5) truncated min: 255 max: 0 ]! !
!NumericalMatrix methodsFor: 'conversion' stamp: 'jmv 8/9/2018 13:05:32'!
asGrayForm: aBlock
"Fill an 8-bit GrayForm with gray values."
| answer bits bitsIndex elementsIndex bytesPerRow |
bytesPerRow _ width + 3 // 4 * 4.
answer _ GrayForm extent: width@height.
bits _ answer bits.
elementsIndex _ 0.
1 to: height do: [ :i |
bitsIndex _ i-1 * bytesPerRow.
1 to: width do: [ :j |
elementsIndex _ elementsIndex + 1.
bitsIndex _ bitsIndex + 1.
bits byteAt: bitsIndex put: (aBlock value: elementsIndex) bigEndian: answer isBigEndian ]].
^answer! !
!NumericalMatrix methodsFor: 'conversion' stamp: 'jmv 8/9/2018 14:02:03'!
asGrayGreenForm
"Make positive values gray, and negative values green.
Use brightness for magnitude (absolute value).
Map used range of pixel values to available gray values."
| max min k b g r |
min _ self min.
max _ self max.
max > min ifFalse: [
max _ min + 1 ].
k _ 255.0 / (max max: min negated).
^ self as32bitForm: [ :index :word |
g _ (((elements at: index) abs) * k + 0.5) truncated min: 255 max: 0.
(elements at: index) > 0
ifTrue: [ r _ b _ g ]
ifFalse: [ r _ b _ 0 ].
word at: 1 put: b; at: 2 put: g; at: 3 put: r ]! !
!NumericalMatrix methodsFor: 'conversion' stamp: 'jmv 6/25/2018 13:43:26'!
asHeatmap: aBlock minVal: minVal maxVal: maxVal
"Fill 32bit Form with rgb values as computed by aBlock. See senders."
| color fraction |
^ self as32bitForm: [ :index :word |
fraction _ ((aBlock value: index) - minVal) / (maxVal - minVal).
color _ Color jet: fraction.
color _ color mixed: 0.6 with: Color gray.
word at: 1 put: color blue * 255 // 1; at: 2 put: color green * 255 // 1; at: 3 put: color red * 255// 1 ]! !
!NumericalMatrix methodsFor: 'statistics' stamp: 'jmv 9/20/2018 09:56:23'!
absMax
^ elements max abs max: elements min abs! !
!NumericalMatrix methodsFor: 'statistics' stamp: 'jmv 5/12/2015 11:56'!
average
^self mean! !
!NumericalMatrix methodsFor: 'statistics' stamp: 'jmv 5/12/2015 11:52'!
max
^ elements max! !
!NumericalMatrix methodsFor: 'statistics' stamp: 'jmv 5/12/2015 11:55'!
mean
^elements mean! !
!NumericalMatrix methodsFor: 'statistics' stamp: 'jmv 5/12/2015 11:52'!
min
^ elements min! !
!NumericalMatrix methodsFor: 'statistics' stamp: 'jmv 5/12/2015 11:56'!
sum
^elements sum! !
!NumericalMatrix methodsFor: 'filling' stamp: 'jmv 10/19/2017 12:40:15'!
border: box with: value
"box includes topLeft but doesn't include bottom right (as it is usual with Rectangles)
Coordinates are 1-based"
box top to: box bottom-1 do: [ :i |
self i: i j: box left put: value.
self i: i j: box right-1 put: value ].
box left to: box right-1 do: [ :j |
self i: box top j: j put: value.
self i: box bottom-1 j: j put: value ]! !
!NumericalMatrix methodsFor: 'filling' stamp: 'jmv 7/26/2016 11:32:38'!
fill: box with: value
"box includes topLeft but doesn't include bottom right (as it is usual with Rectangles)
Coordinates are 1-based"
box top to: box bottom-1 do: [ :i |
box left to: box right-1 do: [ :j |
self i: i j: j put: value ]]! !
!NumericalMatrix methodsFor: 'filling' stamp: 'jmv 7/19/2012 11:21'!
fillDiagonalWith: aVectorOrArray
1 to: (width min: height) do: [ :j |
self i: j j: j put: (aVectorOrArray at: j) ].! !
!NumericalMatrix methodsFor: 'filling' stamp: 'jmv 9/28/2007 19:30'!
fillWithIdentity
1 to: self width do: [ :i |
self i: i j: i put: 1.
i+1 to: self width do: [ :j |
self i: i j: j put: 0.
self i: j j: i put: 0 ] ].! !
!NumericalMatrix methodsFor: 'aux operations' stamp: 'jmv 7/19/2012 12:25'!
columnsWithoutPivot: count
"Return a collection with the indexes of the columns that are closer to not having a pivot"
| col col2 |
col _ SortedCollection sortBlock: [ :a :b | a y < b y ].
1 to: height do: [ :j |
col2 _ OrderedCollection new.
1 to: height do: [ :i |
(j = 1 or: [(self i: i j: j-1) isZero]) ifTrue: [
col2 add: (self i: i j: j) abs ] ].
col add: (j @ col2 max) ].
^((col copyFrom: 1 to: count) collect: [ :each | each x ] ) asSortedCollection! !
!NumericalMatrix methodsFor: 'aux operations' stamp: 'jmv 7/19/2012 12:04'!
rowWithMaxInColumn: j startingAtRow: jStart
| rowWithMax |
rowWithMax _ jStart.
jStart+1 to: height do: [ :i |
(self i: i j: j) abs > (self i: rowWithMax j: j) abs ifTrue: [
rowWithMax _ i ] ].
^rowWithMax! !
!NumericalMatrix methodsFor: 'aux operations' stamp: 'jmv 9/18/2017 17:31:53'!
subtractRow: i multipliedBy: factor to: k startingAtColumn: jStart
self i: k j: jStart put: self zeroElement.
jStart+1 to: width do: [ :j |
self i: k j: j put:
(self i: k j: j) - ((self i: i j: j) * factor) ]! !
!NumericalMatrix methodsFor: 'operations' stamp: 'jmv 7/19/2012 11:20'!
determinant
| aux rowPermutationCount result |
self isSquare ifFalse: [ ^self error: 'Only for Square Matrices' ].
aux _ self copy.
rowPermutationCount _ aux triangleWithPartialPivoting.
result _ -1 raisedToInteger: rowPermutationCount.
1 to: width do: [ :i |
result _ result * (aux i: i j: i) ].
^result! !
!NumericalMatrix methodsFor: 'operations' stamp: 'jmv 8/23/2014 23:27'!
inverse
"Answers the inverse matrix"
| zeroElement bigMatrix result |
zeroElement _ self zeroElement.
self isSquare ifFalse: [ ^self error: 'Only for Square Matrices' ].
result _ self appropriateResultClass height: height width: width.
bigMatrix _ self appropriateResultClass height: height width: width + 1.
1 to: width do: [ :i |
bigMatrix fillWith: self.
1 to: width do: [ :j | bigMatrix i: j j: width+1 put: zeroElement ].
bigMatrix i: i j: width + 1 put: 1.
result j: i put: bigMatrix solveLinearSystem ].
^result! !
!NumericalMatrix methodsFor: 'operations' stamp: 'jmv 7/19/2012 12:21'!
moveOriginToCenter: aBoolean
"This method performs a circular shift of the both in horizontal and vertical direction. The magnitude of the shift is half our extent.
This is useful, for example, to make a better visualization of convolution kernels:
To be properly applied, the kernel is centered at 1@1 . For instance a simple low pass filter could be
| 0.6 0.1 0.0 0.1 |
| 0.1 0.0 0.0 0.0 |
| 0.0 0.0 0.0 0.0 |
| 0.1 0.0 0.0 0.0 |
But it is much easier to 'see' it if the center of the filter is moved to the center of the matrix like this:
| 0.0 0.0 0.0 0.0 |
| 0.0 0.0 0.1 0.0 |
| 0.0 0.1 0.6 0.1 |
| 0.0 0.0 0.1 0.0 |
This method takes the receiver and answers a new instance, transforming it from 1@1 to center if aBoolean is true, or backwards if false.
(if the extent is even, then aBoolean makes no difference).
((FloatMatrix fromArrayOfArrays: #(
(0.6 0.1 0.0 0.1)
(0.1 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0)
(0.1 0.0 0.0 0.0)))
moveOriginToCenter: true)
print
((Matrix fromArrayOfArrays: #(
(0.6 0.1 0.0 0.0 0.1)
(0.1 0.0 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0 0.0)
(0.1 0.0 0.0 0.0 0.0)))
moveOriginToCenter: true)
print
"
| answer deltaI deltaJ e ex ey |
e _ self size.
ey _ e y.
ex _ e x.
deltaI _ ey //2.
deltaJ _ ex // 2.
aBoolean ifFalse: [
deltaI _ deltaI negated.
deltaJ _ deltaJ negated ].
answer _ self class newSize: e.
0 to: ey-1 do: [ :i |
0 to: ex-1 do: [ :j |
answer i: i+deltaI\\ey+1 j: j+deltaJ\\ex+1 put: (self i: i+1 j: j+1) ]].
^answer! !
!NumericalMatrix methodsFor: 'operations' stamp: 'jmv 7/19/2012 12:37'!
normalizeColumns
| zeroElement normSquared |
zeroElement _ self zeroElement.
1 to: width do: [ :j |
normSquared _ zeroElement.
1 to: height do: [ :i |
normSquared _(self i: i j: j) squared + normSquared ].
1 to: height do: [ :i |
self i: i j: j put: (self i: i j: j) / normSquared sqrt ] ]! !
!NumericalMatrix methodsFor: 'operations' stamp: 'jmv 10/7/2016 15:35:55'!
normalizeRows
| zeroElement normSquared |
zeroElement _ self zeroElement.
1 to: height do: [ :i |
normSquared _ zeroElement.
1 to: width do: [ :j |
normSquared _(self i: i j: j) squared + normSquared ].
1 to: width do: [ :j |
self i: i j: j put: (self i: i j: j) / normSquared sqrt ] ]! !
!NumericalMatrix methodsFor: 'operations' stamp: 'jmv 7/19/2012 12:17'!
permuteRow: i and: k
| a b |
i = k ifFalse: [
1 to: width do: [ :j |
a _ self i: k j: j.
b _ self i: i j: j.
self i: k j: j put: b.
self i: i j: j put: a ] ]! !
!NumericalMatrix methodsFor: 'operations' stamp: 'jmv 10/6/2016 15:35:41'!
trace
| trace |
trace _ self zeroElement.
1 to: height do: [ :i |
trace _ trace + (self i: i j: i ) ].
^trace! !
!NumericalMatrix methodsFor: 'operations' stamp: 'jmv 7/19/2012 12:17'!
transpose
"Traspose the receiver. Modify it."
| aux |
1 to: width do: [ :ii |
ii+1 to: width do: [ :jj |
aux _ self i: ii j: jj.
self i: ii j: jj put: (self i: jj j: ii).
self i: jj j: ii put: (aux) ] ].! !
!NumericalMatrix methodsFor: 'operations' stamp: 'jmv 8/23/2014 23:27'!
transposed
"Answer a new matrix, transposed."
| result |
result _ self appropriateResultClass height: width width: height.
1 to: result height do: [ :i |
1 to: result width do: [ :j |
result i: i j: j put: (self i: j j: i) ] ].
^result! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 11:18'!
diagonal
| x |
x _ self appropriateResultClass newVectorSize: height.
1 to: height do: [ :i |
x i: i j: 1 put: (self i: i j: i ) ].
^x! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 10:25'!
elements
^ elements! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 17:41'!
fillEndsWith1Derivative
"Fill one row and one column at each end with such values that the derivative is that at the ends of the inner submatrix.
If the size is less than 4 in any direction, meaning we don't have enough elements to compute the derivatives, do nothing.
Note: We could make derivatives zero, meaning to copy the values."
| m n third second first antepenultimate penultimate last |
m _ self m. n _ self n.
n >= 4 ifTrue: [
2 to: m-1 do: [ :i |
second _ self i: i j: 2. third _ self i: i j: 3.
first _ second * 2-third.
self i: i j: 1 put: first.
antepenultimate _ self i: i j: n-2.
penultimate _ self i: i j: n-1.
last _ penultimate * 2 - antepenultimate.
self i: i j: n put: last ]].
m >= 4 ifTrue: [
1 to: n do: [ :j |
second _ self i: 2 j: j. third _ self i: 3 j: j.
first _ second * 2-third.
self i: 1 j: j put: first.
antepenultimate _ self i: m-2 j: j.
penultimate _ self i: m-1 j: j.
last _ penultimate * 2 - antepenultimate.
self i: m j: j put: last ]]! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 17:42'!
fillEndsWith2Derivative2
"Fill two rows and two columns at each end with such values that the second derivative is that at the ends of the inner submatrix.
If the size is less than 7 in any direction, meaning we don't have enough elements to compute the derivatives, do nothing.
Note: We could make derivatives zero, meaning to copy the values."
| m n third fourth fifth second first fourthToEnd thirdToEnd antepenultimate penultimate last |
m _ self m. n _ self n.
n >= 7 ifTrue: [
3 to: m-2 do: [ :i |
third _ self i: i j: 3. fourth _ self i: i j: 4. fifth _ self i: i j: 5.
second _ (third-fourth) * 3.0 + fifth.
first _ (second-third) * 3.0 + fourth.
self i: i j: 2 put: second.
self i: i j: 1 put: first.
fourthToEnd _ self i: i j: n-4.
thirdToEnd _ self i: i j: n-3.
antepenultimate _ self i: i j: n-2.
penultimate _ (antepenultimate - thirdToEnd) * 3.0 + fourthToEnd.
last _ (penultimate -antepenultimate) * 3.0 + thirdToEnd.
self i: i j: n-1 put: penultimate.
self i: i j: n put: last ]].
m >= 7 ifTrue: [
1 to: n do: [ :j |
third _ self i: 3 j: j. fourth _ self i: 4 j: j. fifth _ self i: 5 j: j.
second _ (third-fourth) * 3.0 + fifth.
first _ (second-third) * 3.0 + fourth.
self i: 2 j: j put: second.
self i: 1 j: j put: first.
fourthToEnd _ self i: m-4 j: j.
thirdToEnd _ self i: m-3 j: j.
antepenultimate _ self i: m-2 j: j.
penultimate _ (antepenultimate - thirdToEnd) * 3.0 + fourthToEnd.
last _ (penultimate -antepenultimate) * 3.0 + thirdToEnd.
self i: m-1 j: j put: penultimate.
self i: m j: j put: last ]]! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 11:22'!
i: i
"Answers a row vector with the values at row y"
| result |
result _ self appropriateResultClass newRowVectorSize: width.
1 to: width do: [ :j |
result i: 1 j: j put: (self i: i j: j) ].
^result! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 11:22'!
i: i put: row
"aVector must be a row vector, i.e. a matrix with height 1"
1 to: width do: [ :j |
self i: i j: j put: (row at: j) ]! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 9/30/2007 09:31'!
i: i upTo: k
"Answers a row vector with the values at row y, but only up to index w"
| result |
result _ self appropriateResultClass newRowVectorSize: k.
1 to: k do: [ :j |
result i: 1 j: j put: (self i: i j: j) ].
^result! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 11:22'!
iExtended: i
"Answers a row vector with the values at row y,
but with 2 extra values at each end, such that the second derivative is that at the original ends"
| result |
result _ self appropriateResultClass newRowVectorSize: width + 4.
1 to: width do: [ :j |
result i: 1 j: j+2 put: (self i: i j: j) ].
"The first available second derivative is
(s5-s4)-(s4-s3)"
"
Tomar esto para lo que realmente hace falta. Ver el otro browser
second _ (third-fourth) * 3.0 + fifth.
first _ (second-third) * 3.0 + fourth.
penultimate _ (antepenultimate - thirdToEnd) * 3.0 + fourthToEnd.
last _ (penultimate -antepenultimate) * 3.0 + thirdToEnd.
"
^result! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 11:19'!
j: j
"Answers a column vector with the values at column x"
| result |
result _ self appropriateResultClass newColumnVectorSize: height.
1 to: height do: [ :i |
result i: i j: 1 put: (self i: i j: j) ].
^result! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 10/3/2007 06:19'!
j: j i: i
"Convenience accessor"
^self i: i j: j! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 11:19'!
j: j put: column
"column can be a column vector (m x 1 matrix), or a kind of Array"
1 to: height do: [ :i |
self i: i j: j put: (column at: i) ]! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 11:27'!
m
"Usual name in Linear Algebra"
^height! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/19/2012 11:27'!
n
"Usual name in Linear Algebra"
^width! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/4/2001 11:38'!
scalarSize
"Specially useful for Vectors, but defined for matrices too."
^height * width! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 9/30/2007 09:30'!
subMatrixTopLeft: aPoint bottomRight: bPoint
| result |
result _ self appropriateResultClass newSize: bPoint - aPoint + (1@1).
1 to: result height do: [ :i |
1 to: result width do: [ :j |
result i: i j: j put:
(self at: (j@i)+aPoint-(1@1)) ] ].
^result! !
!NumericalMatrix methodsFor: 'accessing' stamp: 'jmv 7/12/2011 17:49'!
subMatrixTopLeft: aPoint size: sizePoint
^ self subMatrixTopLeft: aPoint bottomRight: aPoint + sizePoint - 1! !
!NumericalMatrix methodsFor: 'displaying' stamp: 'jmv 8/9/2018 14:29:22'!
display
^self displayAt: 0@0 zoom: 1! !
!NumericalMatrix methodsFor: 'displaying' stamp: 'jmv 8/9/2018 14:24:43'!
displayAsHeatmapAt: aPoint zoom: scale
"Uses Nearest Neighbor interpolation, to help see each pixel. Usually useful to view very small images, such as PSFs
FloatImage lena displayAsHeatmapAt: 10@10 zoom: 4
(FloatImage lena copy: (120@120 extent: 30@30)) displayAsHeatmapAt: 10@10 zoom: 15
(FloatImage lena -0.5 * 2 copy: (120@120 extent: 30@30)) displayAsHeatmapAt: 10@10 zoom: 15
"
"Use Jet heatmap color. Zero is green. 1 is red. -1 is blue."
| form |
form _ self asHeatmap: [ :index | elements at: index ] minVal: -1 maxVal: 1.
scale = 1 ifFalse: [
form _ form magnifyBy: scale ].
form displayAt: aPoint.
^ form! !
!NumericalMatrix methodsFor: 'displaying' stamp: 'jmv 8/9/2018 14:29:14'!
displayAt: aPoint
"Display receiver, using default conversion to Form."
^ self displayAt: aPoint zoom: 1! !
!NumericalMatrix methodsFor: 'displaying' stamp: 'jmv 8/9/2018 14:22:59'!
displayAt: aPoint zoom: scale
"Display receiver, using default conversion to Form."
| form |
form _ self asForm.
scale = 1 ifFalse: [
form _ form magnifyBy: scale ].
form displayAt: aPoint.
^ form! !
!NumericalMatrix methodsFor: 'displaying' stamp: 'jmv 8/9/2018 14:29:08'!
displayAutoRangeAt: aPoint
"Display receiver, mapping used range to available gray levels"
^ self displayAutoRangeAt: aPoint zoom: 1! !
!NumericalMatrix methodsFor: 'displaying' stamp: 'jmv 8/9/2018 14:28:00'!
displayAutoRangeAt: aPoint zoom: scale
"Display receiver, mapping used range to available gray levels"
| form |
form _ self asFormAutoRange.
scale = 1 ifFalse: [
form _ form magnifyBy: scale ].
form displayAt: aPoint.
^ form! !
!NumericalMatrix methodsFor: 'displaying' stamp: 'jmv 8/9/2018 14:28:07'!
displayGrayGreenAt: aPoint zoom: scale
"Uses Nearest Neighbor interpolation, to help see each pixel. Usually useful to view very small images, such as PSFs
FloatImage lena displayGrayGreenAt: 10@10 zoom: 4
(FloatImage lena copy: (120@120 extent: 30@30)) displayGrayGreenAt: 10@10 zoom: 15
(FloatImage lena -0.5 * 2 copy: (120@120 extent: 30@30)) displayGrayGreenAt: 10@10 zoom: 15
"
| form |
form _ self asGrayGreenForm.
scale = 1 ifFalse: [
form _ form magnifyBy: scale ].
form displayAt: aPoint.
^ form! !
!NumericalMatrix methodsFor: 'private' stamp: 'jmv 7/19/2012 12:35'!
epsilon
"Something better is needed. For example, taking into accoung the largest elements, or the norm, or something like that... In any case it is better to check senders, and use a different strategy for ending iterative methods or such...
Check inheritance too"
^self zeroElement! !
!NumericalMatrix methodsFor: 'private' stamp: 'jmv 7/19/2012 12:35'!
zeroElement
"We don't really restrict the kind of objects we might hold... Our best guess for a kind of null additive value is to just ask some element..."
^elements first class zero! !
!NumericalMatrix methodsFor: 'testing' stamp: 'jmv 5/14/2019 09:37:18'!
is: aSymbol
^ aSymbol == #NumericalMatrix or: [ super is: aSymbol ]! !
!NumericalMatrix methodsFor: 'linear equation systems' stamp: 'jmv 7/19/2012 13:07'!
solveLinearSystem
| m n x sum |
"Solve the sistem A x = b where the receiver has this form | Ab |,
the last column of the receiver contains the independent term b.
(FloatMatrix fromArrayOfArrays: #(
#(1 2 0 0 0 0 3)
#(2 4 1 0 0 -4 3)
#(0 1 1 1 0 0 3)
#(0 0 1 1 1 0 3)
#(0 0 0 1 1 1 3)
#(0 1 0 0 1 1 3)
)) solveLinearSystem
(Matrix fromArrayOfArrays: {
{1. 2. 0. 0. 0. 0. 3/2}.
{2. 4. 1. 0. 0. -4. 3/2}.
{0. 1. 1. 1. 0. 0. 3/2}.
{0. 0. 1. 1. 1. 0. 3/2}.
{0. 0. 0. 1. 1. 1. 3/2}.
{0. 1. 0. 0. 1. 1. 3/2}
}) solveLinearSystem
"
m _ height.
n _ width.
x _ self appropriateResultClass newVectorSize: n-1.
"Check we have enough equations"
m < (n-1) ifTrue: [
^self error: 'This system does not have a single solution' ].
self triangleWithPartialPivoting.
"Checks"
"If the only coeficient of the last equation is zero, we have infinite solutions."
((self i: n-1 j: n-1) abs <= self epsilon) & ((self i: n-1 j: n) abs <= self epsilon) ifTrue: [
^self error: 'This system does not have a single solution' ].
"If the only coeficient of the last equation is zero, but not the independent term,
the system is incompatible."
(self i: n-1 j: n-1) abs <= self epsilon ifTrue: [
^self error: 'This system is incompatible (it does not have solution)' ].
"We have too much equations, and they did not go away. Incompatible system."
(m > (n-1) and: [ (self i: n j: n-1) abs > self epsilon ]) ifTrue: [
^self error: 'This system does is incompatible (it does not have solution)' ].
"We have too much equations, and they left their independent terms."
((n to: m) inject: true into: [ :previousValue :k |
previousValue & ((self i: k j: n) abs <= self epsilon ) ]) ifFalse: [
^self error: 'This system does is incompatible (it does not have solution)' ].
"Do backward substitution"
n-1 to: 1 by: -1 do: [ :i |
sum _ self i: i j: n.
n-1 to: i+1 by: -1 do: [ :k |
sum _ sum - ((self i: i j: k) * (x i: k j: 1)) ].
x i: i j: 1 put: sum / (self i: i j: i) ].
^x! !
!NumericalMatrix methodsFor: 'linear equation systems' stamp: 'jmv 9/18/2017 17:32:07'!
triangleWithPartialPivoting
"Triangle self, and answer the row permutation count"
| j jdelta k factor permutationCount |
permutationCount _ 0.
jdelta _ 0.
1 to: height-1 do: [ :i |
[
j _ i + jdelta.
j <= width ifFalse: [ ^self ].
"Look for the pivot for column j, from row i down. Call it row k"
k _ self rowWithMaxInColumn: j startingAtRow: i.
((self i: k j: j) isZero) & (j < width) ] whileTrue: [ jdelta _ jdelta + 1 ].
"Permute rows k and i"
i = k ifFalse: [
self permuteRow: i and: k.
permutationCount _ permutationCount + 1 ].
"Subtract the row i to all the ones below it"
i+1 to: height do: [ :ii |
(self i: ii j: j) isZero ifFalse: [
factor _ (self i: ii j: j) / (self i: i j: j).
"Only after column j"
self subtractRow: i multipliedBy: factor to: ii startingAtColumn: j ] ]
].
^ permutationCount! !
!NumericalMatrix class methodsFor: 'instance creation' stamp: 'jmv 8/23/2014 23:27'!
columnFrom: anArray
"Example:
FloatMatrix columnFrom: #(1 2 0 0 0 0 3)."
| result |
result _ self height: anArray size width: 1.
1 to: result height do: [ :i |
result i: i j: 1 put: (anArray at: i) ].
^result! !
!NumericalMatrix class methodsFor: 'instance creation' stamp: 'jmv 5/3/2016 17:55'!
from32BitColorForm: aForm component: aSymbol
"aSymbol must be in #(red green blue alpha)"
| instance |
instance _ self width: aForm width height: aForm height.
instance fillFrom32BitForm: aForm component: aSymbol.
^instance! !
!NumericalMatrix class methodsFor: 'instance creation' stamp: 'jmv 6/2/2016 20:18'!
fromGrayForm: aGrayForm
"aGrayForm can be an instance of GrayForm or a ColorForm with a colormap with 256 gray levels, going from black to white.
This is what we get, for instance, when reading a gray 8 bit bmp file"
| instance |
instance _ self width: aGrayForm width height: aGrayForm height.
instance fillFromGrayForm: aGrayForm.
^instance! !
!NumericalMatrix class methodsFor: 'instance creation' stamp: 'jmv 7/2/2018 11:16:49'!
fromMatrix: aMatrix
| answer |
answer _ self extent: aMatrix extent.
answer += aMatrix.
^ answer! !
!NumericalMatrix class methodsFor: 'instance creation' stamp: 'jmv 11/16/2014 19:56'!
hilbert: n
"
(Matrix hilbert: 15) inverse inverse
(FloatMatrix hilbert: 15) inverse inverse
"
| result |
result _ self height: n width: n.
1 to: n do: [ :i |
1 to: n do: [ :j |
result i: j j: i put: 1/(i+j+1) ] ].
^result! !
!NumericalMatrix class methodsFor: 'instance creation' stamp: 'jmv 8/23/2014 23:28'!
identity: size
| result |
result _ self height: size width: size.
result fillWithIdentity.
^result
! !
!NumericalMatrix class methodsFor: 'instance creation' stamp: 'jmv 6/2/2016 20:34'!
lena
"
(Base64MimeConverter mimeEncode: ((FileStream readOnlyFileNamed: 'Lena.png') binary)) upToEnd
Form lena display.
FloatImage lena display.
ByteMatrix lena display.
"
| form |
form _ Form lena.
^self fromGrayForm: form! !
!NumericalMatrix class methodsFor: 'instance creation' stamp: 'jmv 9/2/2014 10:10'!
m: height n: width
^self basicNew initHeight: height width: width! !
!NumericalMatrix class methodsFor: 'instance creation' stamp: 'jmv 8/23/2014 23:28'!
newColumnVectorSize: size
"A column vector is just a matrix with width 1"
^self height: size width: 1! !
!NumericalMatrix class methodsFor: 'instance creation' stamp: 'jmv 8/23/2014 23:28'!
newIdentity: size
| result |
result _ self height: size width: size.
result fillWithIdentity.