forked from PolyMathOrg/vector-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPMMatrix.class.st
More file actions
1119 lines (892 loc) · 30.5 KB
/
PMMatrix.class.st
File metadata and controls
1119 lines (892 loc) · 30.5 KB
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
"
I represent a mathematical matrix. I can be build from rows as follows:
[[[
PMMatrix rows: #((1 2 3)(4 5 6)).
]]]
I understand the usual matrix operations.
"
Class {
#name : 'PMMatrix',
#superclass : 'Object',
#instVars : [
'rows',
'lupDecomposition'
],
#category : 'Math-Matrix',
#package : 'Math-Matrix'
}
{ #category : 'example' }
PMMatrix class >> example [
""
| a b c d |
"This is how we can create a matrix, a and b are 2x3 matrices in
this example"
a := PMMatrix rows: #( #( 1 0 1 ) #( -1 -2 3 ) ).
b := PMMatrix rows: #( #( 1 2 3 ) #( -2 1 7 ) ).
"Matrix product"
c := a * b.
"Elementwise matrix product"
d := a hadamardProduct: b.
"This is how we can create a vector"
a := #( 1 4 9 16 25 ) asPMVector.
"Vectors and Matrices support basic logical and arithmetic
operations"
Float pi sin * d.
a sqrt.
a > 3.
c cos.
c < 0.
"It is possible to create a vector/matrix of random numbers"
a := PMVector randomSize: 10 maxNumber: 3.
b := PMMatrix rows: 2 columns: 3 random: 5.
"It is also easy to create a vector/matrix of zeros/ones"
a := PMVector ones: 15.
b := PMMatrix zerosRows: 2 cols: 3.
"We can also compute the cumulative sum or regular sum the vector/
matrix as following"
a := PMMatrix rows: #( #( 1 0 1 ) #( -1 -2 3 ) ).
a cumsum.
"a PMVector(1 1 2)"
"a PMVector(-1 -3 0)"
a sum.
"a PMVector(2 0)"
"Matrix trace (sum of a diagonal elements for a square matrix)"
a := PMMatrix rows: #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ).
a tr
"15"
]
{ #category : 'instance creation' }
PMMatrix class >> identity: dimension [
^ PMSymmetricMatrix identity: dimension
]
{ #category : 'instance creation' }
PMMatrix class >> join: anArrayOfMatrices [
"Inverse of the split operation."
| rows n row rowSize n1 n2 |
rows := OrderedCollection new.
n1 := ( anArrayOfMatrices at: 1) numberOfColumns.
n2 := n1 + 1.
rowSize := n1 + ( anArrayOfMatrices at: 2) numberOfColumns.
n := 0.
( anArrayOfMatrices at: 1) rowsDo:
[ :each |
n := n + 1.
row := PMVector new: rowSize.
row replaceFrom: 1 to: n1 with: each startingAt: 1;
replaceFrom: n2 to: rowSize with: ( ( anArrayOfMatrices at: 2) rowAt: n) startingAt: 1.
rows add: row.
].
n := 0.
( anArrayOfMatrices at: 3) rowsDo:
[ :each |
n := n + 1.
row := PMVector new: rowSize.
row replaceFrom: 1 to: n1 with: each startingAt: 1;
replaceFrom: n2 to: rowSize with: ( ( anArrayOfMatrices at: 4) rowAt: n) startingAt: 1.
rows add: row.
].
^self rows: rows
]
{ #category : 'information' }
PMMatrix class >> lupCRLCriticalDimension [
^ 40
]
{ #category : 'instance creation' }
PMMatrix class >> new: dimension [
"Create an empty square matrix of size dimension x dimension."
^ self new initializeSquare: dimension
]
{ #category : 'instance creation' }
PMMatrix class >> onesRows: rows cols: columns [
"Creates MxN matrix of ones"
| a b |
a := (1 to: rows) collect: [ :row | b := PMVector ones: columns ].
^ self rows: a
]
{ #category : 'instance creation' }
PMMatrix class >> rows: anArrayOrVector [
"Create a new matrix with given components."
^ self new initializeRows: anArrayOrVector
]
{ #category : 'instance creation' }
PMMatrix class >> rows: rowsInteger columns: columnsInteger [
^ self new initializeRows: rowsInteger columns: columnsInteger
]
{ #category : 'instance creation' }
PMMatrix class >> rows: nRows columns: nCols element: fillElement [
" Answer a new matrix of nRows x nCols initialized with fillElement in all cells "
^ (self new initializeRows: nRows columns: nCols)
atAllPut: fillElement;
yourself
]
{ #category : 'instance creation' }
PMMatrix class >> rows: aNumberOfRows columns: aNumberOfColumns random: aMaxNumber [
^ self
rows: aNumberOfRows
columns: aNumberOfColumns
random: aMaxNumber
generator: Random new
]
{ #category : 'instance creation' }
PMMatrix class >> rows: aNumberOfRows columns: aNumberOfColumns random: aMaxNumber generator: aGenerator [
"Answer a new Matrix of the given dimensions filled with random numbers"
| rows |
rows := (1 to: aNumberOfRows) collect: [ :i | (1 to: aNumberOfColumns) collect: [ :j | aGenerator nextBetween: 0 and: aMaxNumber ] ].
^ self rows: rows
]
{ #category : 'instance creation' }
PMMatrix class >> zerosRows: rows cols: columns [
"Creates MxN matrix of zeros"
| a b |
a := (1 to: rows) collect: [ :row | b := PMVector zeros: columns ].
^ self rows: a
]
{ #category : 'operation' }
PMMatrix >> * aNumberOrMatrixOrVector [
"Answers the product of the receiver with the argument. The argument can be a number, matrix or vector."
^ aNumberOrMatrixOrVector productWithMatrix: self
]
{ #category : 'operation' }
PMMatrix >> + aMatrixOrNumber [
"Answers the sum of the receiver with aMatrix."
^ aMatrixOrNumber addWithRegularMatrix: self
]
{ #category : 'operation' }
PMMatrix >> - aMatrix [
"Answers the difference between the receiver and aMatrix."
^ aMatrix subtractWithRegularMatrix: self
]
{ #category : 'operating' }
PMMatrix >> -= aRow [
"Assume a collection is a matrix of one row"
self rowWiseSubstractionWith: aRow
]
{ #category : 'operation' }
PMMatrix >> < aNumber [
"Apply < operator to each element of the matrix"
^ PMMatrix rows: (self rowsCollect: [ :each | each < aNumber ])
]
{ #category : 'comparing' }
PMMatrix >> = aNumberOrMatrix [
^ (aNumberOrMatrix species = self species) and: [ self rows = aNumberOrMatrix rows ]
]
{ #category : 'operation' }
PMMatrix >> > aNumber [
"Apply > operator to each element of the matrix"
^ PMMatrix rows: (self rowsCollect: [ :each | each > aNumber ])
]
{ #category : 'arithmetic' }
PMMatrix >> abs [
"Computes the element-wise absolute value."
^ self class rows: (rows collect: #abs).
]
{ #category : 'double dispatching' }
PMMatrix >> adaptToNumber: rcvr andSend: selector [
"selector must obviously be commutative for this simple solution, but at the moment its only used for multiplication"
^ self perform: selector with: rcvr.
]
{ #category : 'double dispatching' }
PMMatrix >> addWithRegularMatrix: aMatrix [
"Answers the sum of the receiver with aMatrix as a PMMatrix."
| n |
n := 0.
(self numberOfRows = aMatrix numberOfRows) &
(self numberOfColumns = aMatrix numberOfColumns)
ifFalse: [ SizeMismatch signal ].
^ PMMatrix rows: ( self rowsCollect: [ :each | n := n + 1. each + ( aMatrix rowAt: n)])
]
{ #category : 'double dispatching' }
PMMatrix >> addWithSymmetricMatrix: aMatrix [
^ aMatrix addWithRegularMatrix: self
]
{ #category : 'as yet unclassified' }
PMMatrix >> argMaxOnColumns [
^ self columnsCollect: [ :each | each argMax ]
]
{ #category : 'as yet unclassified' }
PMMatrix >> argMaxOnRows [
^ self rowsCollect: [ :each | each argMax ]
]
{ #category : 'transformation' }
PMMatrix >> asSymmetricMatrix [
"Convert the receiver to a symmetric matrix (no check is made)."
^ PMSymmetricMatrix rows: rows
]
{ #category : 'converting' }
PMMatrix >> asVector [
^ self flattenRows.
]
{ #category : 'cell accessing' }
PMMatrix >> at: aRowIndex at: aColumnIndex [
"Answers the aRowIndex-th, aColumnIndex-th entry in the receiver."
^ self rowAt: aRowIndex columnAt: aColumnIndex
]
{ #category : 'cell accessing' }
PMMatrix >> at: rowIndex at: columnIndex put: value [
self rowAt: rowIndex columnAt: columnIndex put: value
]
{ #category : 'cell accessing' }
PMMatrix >> atAllPut: element [
"Put element at every one of the receiver's cells."
self rowsDo: [ : row | row atAllPut: element ]
]
{ #category : 'cell accessing' }
PMMatrix >> atColumn: aColumnNumber [
^ self columnAt: aColumnNumber
]
{ #category : 'cell accessing' }
PMMatrix >> atColumn: aColumnIndex put: aCollection [
aCollection withIndexDo: [: value : rowIndex |
self rowAt: rowIndex columnAt: aColumnIndex put: value ]
]
{ #category : 'cell accessing' }
PMMatrix >> atColumn: columnIndex put: aValue repeat: repNumber [
" Example: self atColumn: 1 fillWith: 'BM1818' repeat: 3
produces
[ 'BM1818' nil nil nil
'BM1818' nil nil nil
'BM1818' nil nil nil
nil nil nil nil
nil nil nil nil ]
"
1 to: repNumber do: [ : index | self rowAt: index columnAt: columnIndex put: aValue ].
]
{ #category : 'cell accessing' }
PMMatrix >> atColumn: aColumnNumber put: aCollection startingAt: rowNumber [
" Fill the receiver with aCollection at aColumnNumber begining at rowNumber. "
aCollection withIndexDo: [: value : rowIndex |
(rowIndex + rowNumber ) <= self numberOfRows
ifTrue:
[ self rowAt: rowIndex + rowNumber
columnAt: aColumnNumber
put: value ]]
]
{ #category : 'cell accessing' }
PMMatrix >> atRow: aRowNumber [
"answers the aRowNumber-th row in the receiver"
^ self rowAt: aRowNumber
]
{ #category : 'cell accessing' }
PMMatrix >> atRow: rowIndex put: aCollection [
aCollection withIndexDo: [: value : columnIndex |
self rowAt: rowIndex columnAt: columnIndex put: value ]
]
{ #category : 'cell accessing' }
PMMatrix >> atRow: rowIndex put: aCollection startingAt: startColumnNumber [
"Fill the receiver with aCollection at rowIndex beggining at startColumnNumber. "
aCollection withIndexDo: [: value : columnIndex |
(columnIndex + startColumnNumber ) <= self numberOfColumns
ifTrue:
[ self
rowAt: rowIndex
columnAt: columnIndex + startColumnNumber
put: value ]]
]
{ #category : 'as yet unclassified' }
PMMatrix >> choleskyDecomposition [
| upperTriangular rowSum partialSum diagonalValue nonDiagonalValue factor |
self isPositiveDefinite ifFalse: [
Error signal: 'Choleski decomposition can only be applied to positive-definite matrices' ].
upperTriangular := self class
zerosRows: self numberOfRows
cols: self numberOfColumns.
1 to: self numberOfRows do: [ :i |
1 to: i do: [ :j |
i = j
ifTrue: [
rowSum := (1 to: j - 1) inject: 0 into: [ :sum :k |
sum + (upperTriangular at: k at: j) squared ].
diagonalValue := ((self at: j at: j) - rowSum) sqrt.
upperTriangular at: j at: j put: diagonalValue ]
ifFalse: [
partialSum := (1 to: j - 1) inject: 0 into: [ :sum :k |
sum + ((upperTriangular at: k at: i) * (upperTriangular at: k at: j)).
].
factor := upperTriangular at: j at: j.
nonDiagonalValue := ((self at: j at: i) - partialSum) / factor.
upperTriangular at: j at: i put: nonDiagonalValue.
] ] ].
^ upperTriangular
]
{ #category : 'comparing' }
PMMatrix >> closeTo: aPMMatrix [
"Tests that we are within the default Float >> #closeTo: precision of aPMMatrix (0.0001)."
^ self closeTo: aPMMatrix precision: 0.0001
]
{ #category : 'comparing' }
PMMatrix >> closeTo: aPMMatrix precision: aPrecision [
^ (self - aPMMatrix) abs sum sum < aPrecision
]
{ #category : 'iterators' }
PMMatrix >> collect: aBlock [
"Applies aBlock elementwise to each cell of the matrix."
^ self class rows: (rows collect: [ :r | r collect: aBlock ])
]
{ #category : 'cell accessing' }
PMMatrix >> columnAt: anInteger [
"Answers the anInteger-th column of the receiver."
^ rows collect: [ :each | each at: anInteger ]
]
{ #category : 'information' }
PMMatrix >> columnAverage [
^ (1 to: self numberOfColumns) collect: [ :colIndex |
(self columnAt: colIndex) average ]
]
{ #category : 'cell accessing' }
PMMatrix >> columnVectorAt: col size: dimension [
^ (self columnAt: col) copyFrom: col to: dimension
]
{ #category : 'iterators' }
PMMatrix >> columnsCollect: aBlock [
"Perform the collect: operation on the rows of the receiver."
| n |
n := 0.
^ rows last collect: [ :each | n := n + 1. aBlock value: (self columnAt: n)]
]
{ #category : 'iterators' }
PMMatrix >> columnsDo: aBlock [
"Perform the collect: operation on the rows of the receiver."
| n |
n := 0.
^ rows last do: [ :each | n := n + 1. aBlock value: ( self columnAt: n)]
]
{ #category : 'operation' }
PMMatrix >> cos [
"Apply cos to each element of a matrix"
^ PMMatrix rows: (self rowsCollect: [ :each | each cos ])
]
{ #category : 'operation' }
PMMatrix >> cosh [
"Apply cosh to each element of a matrix"
^ PMMatrix rows: (self rowsCollect: [ :each | each cosh ])
]
{ #category : 'transformation' }
PMMatrix >> cumsum [
"Computes the cumulative sum for each row."
^ PMMatrix rows: (rows collect: [ :each | each cumsum ])
]
{ #category : 'as yet unclassified' }
PMMatrix >> decomposeSV [
^ PMSingularValueDecomposition decompose: self
]
{ #category : 'accessing' }
PMMatrix >> determinant [
^ self lupDecomposition determinant
]
{ #category : 'accessing' }
PMMatrix >> dimension [
^ self rows size @ (self rows at: 1) size
]
{ #category : 'operation' }
PMMatrix >> eigen [
"Computes all eigenvalues and eigenvectors of a matrix.
Usage:
matrix eigen values.
matrix eigen vectors."
self numberOfColumns == 1 & self numberOfRows == 1 ifTrue: [ ^ PMSingleValueMatrixHelper matrix: self ].
self isSymmetric
ifTrue: [ ^ self asSymmetricMatrix eigen ]
ifFalse: [ self error: 'Eigenvalues and eigenvectors of non-symmetric matrix are currently not supported' ]
]
{ #category : 'double dispatching' }
PMMatrix >> elementwiseProductWithMatrix: aMatrix [
"Answers the elementwise product between aMatrix and the receiver as a Matrix."
| n |
n := 0.
^ self class rows: ( aMatrix rowsCollect: [ :each | n := n + 1. each hadamardProduct: ( self rowAt: n)])
]
{ #category : 'as yet unclassified' }
PMMatrix >> equalsTo: aMatrix [
self
deprecated: 'Use closeTo: instead'
transformWith: '`@rec equalsTo: `@arg' -> '`@rec closeTo: `@arg'.
^ self closeTo: aMatrix
]
{ #category : 'as yet unclassified' }
PMMatrix >> flattenColumns [
| answer |
answer := #().
self columnsDo: [ :each | answer := answer , each asArray ].
^ answer asPMVector
]
{ #category : 'as yet unclassified' }
PMMatrix >> flattenRows [
| answer |
answer := #().
self rowsDo: [ :each | answer := answer , each asArray ].
^ answer asPMVector
]
{ #category : 'operation' }
PMMatrix >> hadamardProduct: aMatrix [
^ aMatrix elementwiseProductWithMatrix: self
]
{ #category : 'comparing' }
PMMatrix >> hash [
^ rows hash
]
{ #category : 'initialization' }
PMMatrix >> initializeRows: anArrayOrVector [
"Defines the components of the recevier. No check is made: components are assumed to be orgainized in rows."
rows := anArrayOrVector asPMVector collect: [ :each | each asPMVector].
]
{ #category : 'initialization' }
PMMatrix >> initializeRows: rowsInteger columns: columnsInteger [
"Build empty components for a matrix."
self assert: [ rowsInteger isInteger and: [ rowsInteger > 0 ] ] description: 'Row size of a matrix must be a positive integer'.
self assert: [ columnsInteger isInteger and: [ columnsInteger > 0 ] ] description: 'Column size of a matrix must be a positive integer'.
rows := (1 to: rowsInteger) asPMVector collect: [ :each | PMVector new: columnsInteger ].
]
{ #category : 'initialization' }
PMMatrix >> initializeSquare: dimension [
"Build empty components for a square matrix. No check is made: components are assumed to be orgainized in rows."
^ self initializeRows: dimension columns: dimension
]
{ #category : 'operation' }
PMMatrix >> inverse [
"Answer the inverse of the receiver."
^ self isSquare
ifTrue: [ self lupInverse ]
ifFalse: [ self squared inverse * self transpose ]
]
{ #category : 'as yet unclassified' }
PMMatrix >> inversePivotColumns: anArray [
"uses vector encoding of an interchange permutation matrix in anArray as in qrFactorizationWithPivoting. Does inverse pivoting!"
| res |
res :=self deepCopy.
anArray reverseWith: (1 to: anArray size ) do: [ :piv :ind | piv ~= ind ifTrue: [res swapColumn: piv withColumn: ind ] ].
^ res
]
{ #category : 'operation' }
PMMatrix >> inversePureCRL [
"Answer the inverse of the receiver."
^ self squared inversePureCRL * self transpose
]
{ #category : 'testing' }
PMMatrix >> isHermitian [
"Hermitian matrix (or self-adjoint matrix) is a complex square matrix that is equal to its own conjugate transpose — that is, the element in the i-th row and j-th column is equal to the complex conjugate of the element in the j-th row and i-th column, for all indices i and j"
self isSquare ifFalse: [ ^ false ].
1 to: self numberOfRows do: [ :i |
1 to: (i - 1) do: [ :j |
((self at: i at: j) complexConjugate = (self at: j at: i))
ifFalse: [ ^ false ] ] ].
^ true
]
{ #category : 'testing' }
PMMatrix >> isNegativeDefinite [
"A Hermitian matrix is negative definite if and only if all its eigenvalues are strictly negative"
self isHermitian ifFalse: [ ^ false ].
^ self eigen values allSatisfy: [ :each | each < 0 ]
]
{ #category : 'testing' }
PMMatrix >> isNegativeSemiDefinite [
"A Hermitian matrix is negative semi-definite if and only if all its eigenvalues are non-positive"
self isHermitian ifFalse: [ ^ false ].
^ self eigen values allSatisfy: [ :each | each <= 0 ]
]
{ #category : 'testing' }
PMMatrix >> isPositiveDefinite [
"A Hermitian matrix is positive definite if and only if all its eigenvalues are strictly positive"
self isHermitian ifFalse: [ ^ false ].
^ self eigen values allSatisfy: [ :each | each > 0 ]
]
{ #category : 'testing' }
PMMatrix >> isPositiveSemiDefinite [
"A Hermitian matrix is positive semi-definite if and only if all its eigenvalues are non-negative"
self isHermitian ifFalse: [ ^ false ].
^ self eigen values allSatisfy: [ :each | each >= 0 ]
]
{ #category : 'testing' }
PMMatrix >> isReal [
"Answer true if all values of the matrix are real numbers"
^ rows allSatisfy: [ :vector | vector isReal ].
]
{ #category : 'testing' }
PMMatrix >> isSquare [
"Answers true if the number of rows is equal to the number of columns."
^ rows size = rows last size
]
{ #category : 'testing' }
PMMatrix >> isSymmetric [
^ self = self transpose
]
{ #category : 'private' }
PMMatrix >> largestPowerOf2SmallerThan: anInteger [
"Private"
| m m2|
m := 2.
[ m2 := m * 2.
m2 < anInteger] whileTrue:[ m := m2].
^m
]
{ #category : 'operation' }
PMMatrix >> log [
"Apply log to each element of a matrix"
^ PMMatrix rows: ( self rowsCollect: [ :each | each log])
]
{ #category : 'accessing' }
PMMatrix >> lupDecomposition [
lupDecomposition isNil
ifTrue: [ lupDecomposition :=PMLUPDecomposition equations: rows ].
^ lupDecomposition
]
{ #category : 'operation' }
PMMatrix >> lupInverse [
self lupDecomposition inverseMatrixComponents
ifNil: [ PMSingularMatrixError new signal ]
ifNotNil: [ :i | ^ self class rows: i ]
]
{ #category : 'operation' }
PMMatrix >> minor: rowIndex and: columnIndex [
^ PMMatrix rows:
((self rows allButFirst: columnIndex) collect: [ :aRow |
aRow allButFirst: rowIndex ])
]
{ #category : 'as yet unclassified' }
PMMatrix >> mpInverse [
"Moore Penrose Inverse. "
|f g|
self numberOfRows < self numberOfColumns
ifTrue:[ f := self transpose qrFactorizationWithPivoting.
g := f first.
f := f second inversePivotColumns: (f at:3) ]
ifFalse: [ f := self qrFactorizationWithPivoting.
g := (f second inversePivotColumns: (f at:3)) transpose.
f := f first transpose ].
^ g * ((f *self *g) inverse) *f
]
{ #category : 'transformation' }
PMMatrix >> negate [
"Inverse the sign of all components of the receiver."
rows do: [ :each |each negate ]
]
{ #category : 'accessing' }
PMMatrix >> numberOfColumns [
"Answer the number of rows of the receiver."
^ rows last size
]
{ #category : 'accessing' }
PMMatrix >> numberOfRows [
"Answer the number of rows of the receiver."
^ rows size
]
{ #category : 'as yet unclassified' }
PMMatrix >> orthogonalize [
"returns an orthonormal basis of column (!) vectors for a matrix of column vectors"
^ self qrFactorizationWithPivoting first
]
{ #category : 'as yet unclassified' }
PMMatrix >> principalDiagonal [
"https://en.wikipedia.org/wiki/Diagonal#Matrices for definitions"
| diag |
"Check for square"
self isSquare ifFalse: [ self error: 'Diagonal is not defined for a matrix that is not square.' ].
diag := PMVector new: self rows size.
(1 to: diag size) do: [ :i | diag at: i put: (self at:i at: i) ].
^ diag
]
{ #category : 'printing' }
PMMatrix >> printOn: aStream [
(rows isNil or: [rows first isNil])
ifTrue: [ super printOn: aStream.
aStream nextPutAll:'(uninitialized)'. ^ self ].
rows
do: [ :each | each printOn: aStream]
separatedBy: [ aStream cr].
]
{ #category : 'private' }
PMMatrix >> privateTranspose [
^ self transpose
]
{ #category : 'double dispatching' }
PMMatrix >> productWithMatrix: aMatrix [
"Answers the product of aMatrix with the receiver (in this order)."
^ self productWithMatrixFinal: aMatrix
]
{ #category : 'double dispatching' }
PMMatrix >> productWithMatrixFinal: aMatrix [
"Answers the product of aMatrix with the receiver (in this order)."
"speed optimized"
|t|
t :=self privateTranspose.
^ PMMatrix rows: ( aMatrix rowsCollect: [ :row | t rowsCollect: [ :col | row * col]])
]
{ #category : 'double dispatching' }
PMMatrix >> productWithTransposeMatrix: aMatrix [
"Answers the product of the receiver with the transpose of aMatrix(in this order)."
^ PMMatrix rows: (self rowsCollect: [ :row | aMatrix rowsCollect: [ :col | row * col]])
]
{ #category : 'double dispatching' }
PMMatrix >> productWithVector: aVector [
"Answers the product of the receiver with aVector"
^ self columnsCollect: [ :each | each * aVector ]
]
{ #category : 'as yet unclassified' }
PMMatrix >> qrFactorization [
^ (PMQRDecomposition of: self) decompose
]
{ #category : 'as yet unclassified' }
PMMatrix >> qrFactorizationWithPivoting [
| identMat q r hh colSize i lengthArray rank mx pivot |
self numberOfRows < self numberOfColumns ifTrue: [
self error: 'numberOfRows<numberOfColumns' ].
lengthArray := self columnsCollect: [ :col | col * col ].
mx := lengthArray indexOf: lengthArray max.
pivot := Array new: lengthArray size.
rank := 0.
r := PMMatrix rows: rows deepCopy.
colSize := self numberOfRows.
q := PMSymmetricMatrix identity: colSize.
identMat := q deepCopy.
[
rank := rank + 1.
pivot at: rank put: mx.
r swapColumn: rank withColumn: mx.
lengthArray swap: rank with: mx.
hh := ((r columnAt: rank) copyFrom: rank to: colSize) householder.
i := (PMVector new: rank - 1 withAll: 0) , (hh at: 2).
q := q * (identMat - ((hh at: 1) * i tensorProduct: i)).
i := PMMatrix rows:
((r rows allButFirst: rank - 1) collect: [ :aRow |
aRow allButFirst: rank - 1 ]).
i := i - ((hh at: 2) tensorProduct: (hh at: 1) * (hh at: 2) * i).
i rows withIndexDo: [ :aRow :index |
aRow withIndexDo: [ :n :c |
r
rowAt: rank + index - 1
columnAt: rank + c - 1
put: ((n closeTo: 0)
ifTrue: [ 0 ]
ifFalse: [ n ]) ] ].
rank + 1 to: lengthArray size do: [ :ind |
lengthArray
at: ind
put: (lengthArray at: ind) - (r rowAt: rank columnAt: ind) squared ].
rank < lengthArray size
ifTrue: [
mx := (lengthArray copyFrom: rank + 1 to: lengthArray size) max.
(mx closeTo: 0) ifTrue: [ mx := 0 ].
mx := mx > 0
ifTrue: [ lengthArray indexOf: mx startingAt: rank + 1 ]
ifFalse: [ 0 ] ]
ifFalse: [ mx := 0 ].
mx > 0 ] whileTrue.
i := 0.
[ (r rowAt: colSize) allSatisfy: [ :n | n = 0 ] ] whileTrue: [
i := i + 1.
colSize := colSize - 1 ].
i > 0 ifTrue: [
r := PMMatrix rows: (r rows copyFrom: 1 to: colSize).
i := q numberOfColumns - i.
pivot := pivot copyFrom: 1 to: i.
q := PMMatrix rows:
(q rows collect: [ :row | row copyFrom: 1 to: i ]) ].
^ Array with: q with: r with: pivot
]
{ #category : 'operation' }
PMMatrix >> raisedTo: aPower [
" Answers the receiver raised to a power, aPower .
If aPower is negative, inverse of the receiver is raised to the absolute value of aPower."
|aRaisedPMMatrix|
self assert: self isSquare description: 'Matrix should be square'.
aPower < 0 ifTrue: [
^ self inverse raisedTo: aPower abs ].
aRaisedPMMatrix := PMMatrix identity: self numberOfRows.
1 to: aPower do: [ :each |
aRaisedPMMatrix := aRaisedPMMatrix * self ].
^ aRaisedPMMatrix
]
{ #category : 'as yet unclassified' }
PMMatrix >> rank [
^ ((self numberOfRows < self numberOfColumns
ifTrue: [ self transpose ]
ifFalse: [ self ]) qrFactorizationWithPivoting at: 2) rows size
]
{ #category : 'cell accessing' }
PMMatrix >> rowAt: anInteger [
"Answers the anInteger-th row of the receiver."
^ rows at: anInteger
]
{ #category : 'cell accessing' }
PMMatrix >> rowAt: aRowIndex columnAt: aColumnIndex [
"Answers the aRowIndex-th, aColumnIndex-th entry in the receiver."
^ (rows at: aRowIndex) at: aColumnIndex
]
{ #category : 'cell accessing' }
PMMatrix >> rowAt: aRowIndex columnAt: aColumnIndex put: aValue [
^(rows at: aRowIndex) at: aColumnIndex put: aValue
]
{ #category : 'operating' }
PMMatrix >> rowWiseSubstractionWith: aRow [
"Assume a collection is a matrix of one row"
1 to: self numberOfColumns do: [ :col |
| me |
me := self atColumn: col.
self atColumn: col put: me - (aRow at: col) ].
]
{ #category : 'cell accessing' }
PMMatrix >> rows [
^rows
]
{ #category : 'iterators' }
PMMatrix >> rowsCollect: aBlock [
"Perform the collect: operation on the rows of the receiver."
^ rows collect: aBlock
]
{ #category : 'iterators' }
PMMatrix >> rowsDo: aBlock [
"Perform the collect: operation on the rows of the receiver."
^ rows do: aBlock
]
{ #category : 'iterators' }
PMMatrix >> rowsWithIndexDo: aBlock [
^ rows withIndexDo: aBlock
]
{ #category : 'transformation' }
PMMatrix >> scaleBy: aNumber [
rows do: [ :each | each scaleBy: aNumber ]
]
{ #category : 'cell accessing' }
PMMatrix >> setDiagonal: aVector [
| n m |
n := self numberOfRows.
m := self numberOfColumns.
n < m
ifTrue: [
1 to: n do: [ :i | self rowAt: i columnAt: i put: (aVector at: i)].
]
ifFalse: [
1 to: m do: [ :i | self rowAt: i columnAt: i put: (aVector at: i)].
].
^self
]
{ #category : 'operation' }
PMMatrix >> sign [
"Apply sign to each element of a matrix"
^ PMMatrix rows: (self rowsCollect: [ :each | each sign ])
]
{ #category : 'operation' }
PMMatrix >> sin [
"Apply sin to each element of a matrix"
^ PMMatrix rows: (self rowsCollect: [ :each | each sin ])
]
{ #category : 'operation' }
PMMatrix >> sinh [
"Apply sinh to each element of a matrix"
^ PMMatrix rows: (self rowsCollect: [ :each | each sinh ])
]
{ #category : 'private' }
PMMatrix >> species [
^ PMMatrix
]
{ #category : 'private' }
PMMatrix >> split [
"Private - Answers an array of 4 matrices split from the receiver."
| n m n1 m1 |
n := self numberOfRows.
m := self numberOfColumns.
n1 := self largestPowerOf2SmallerThan: n.
m1 := self largestPowerOf2SmallerThan: m.
^ Array
with: ( self class rows: ( ( 1 to: n1) asPMVector collect: [ :k | ( rows at: k) copyFrom: 1 to: m1]))
with:( self class rows: ( ( 1 to: n1) asPMVector collect: [ :k | ( rows at: k) copyFrom: (m1 + 1) to: m]))
with: ( self class rows: ( ( (n1 + 1) to: n) asPMVector collect: [ :k | ( rows at: k) copyFrom: 1 to: m1]))
with:( self class rows: ( ( (n1 + 1) to: n) asPMVector collect: [ :k | ( rows at: k) copyFrom: (m1 + 1) to: m]))
]
{ #category : 'operation' }
PMMatrix >> sqrt [
"Apply sqrt to each element of a matrix"