Skip to content

Commit 52694cb

Browse files
authored
add raisedTo method for PMMatrix (#209)
* add raisedTo method for PMMatrix A raisedTo: anInteger method is added for PMMatrix. Now, exponentiation of matrices can be performed using this method. * remove nested ifs by a guard clause Some formatting changes, renaming arguments are also done * replace recursion with iteration
1 parent 024af6b commit 52694cb

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/Math-Matrix/PMMatrix.class.st

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,27 @@ PMMatrix >> qrFactorizationWithPivoting [
824824
^ Array with: q with: r with: pivot
825825
]
826826

827+
{ #category : #operation }
828+
PMMatrix >> raisedTo: aPower [
829+
830+
" Answers the receiver raised to a power, aPower .
831+
If aPower is negative, inverse of the receiver is raised to the absolute value of aPower."
832+
833+
|aRaisedPMMatrix|
834+
835+
self assert: self isSquare description: 'Matrix should be square'.
836+
837+
aPower < 0 ifTrue: [
838+
^ self inverse raisedTo: aPower abs ].
839+
840+
aRaisedPMMatrix := PMMatrix identity: self numberOfRows.
841+
842+
1 to: aPower do: [ :each |
843+
aRaisedPMMatrix := aRaisedPMMatrix * self ].
844+
845+
^ aRaisedPMMatrix
846+
]
847+
827848
{ #category : #'as yet unclassified' }
828849
PMMatrix >> rank [
829850
^ ((self numberOfRows < self numberOfColumns

src/Math-Tests-Matrix/PMMatrixTest.class.st

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,56 @@ PMMatrixTest >> testPrintOn [
632632
m printOn: stream
633633
]
634634

635+
{ #category : #tests }
636+
PMMatrixTest >> testRaisedToNegativeInteger [
637+
638+
|aPMMatrix expected|
639+
640+
aPMMatrix := PMMatrix rows: #(#(3 1) #(1 1)).
641+
642+
aPMMatrix := aPMMatrix raisedTo: -2.
643+
644+
expected := PMMatrix rows: #(#(0.5 -1) #(-1 2.5)).
645+
self assert: aPMMatrix equals: expected.
646+
]
647+
648+
{ #category : #tests }
649+
PMMatrixTest >> testRaisedToNonSquareMatrix [
650+
651+
|aPMMatrix|
652+
653+
aPMMatrix := PMMatrix rows: #(#(3 1 4) #(1 1 2)).
654+
655+
self should: [ aPMMatrix raisedTo: 3 ] raise: AssertionFailure.
656+
657+
]
658+
659+
{ #category : #tests }
660+
PMMatrixTest >> testRaisedToPositiveInteger [
661+
662+
|aPMMatrix expected|
663+
664+
aPMMatrix := PMMatrix rows: #(#(3 1) #(1 1)).
665+
666+
aPMMatrix := aPMMatrix raisedTo: 3.
667+
668+
expected := PMMatrix rows: #(#(34 14) #(14 6)).
669+
self assert: aPMMatrix equals: expected.
670+
]
671+
672+
{ #category : #tests }
673+
PMMatrixTest >> testRaisedToZero [
674+
675+
|aPMMatrix expected|
676+
677+
aPMMatrix := PMMatrix rows: #(#(3 1) #(1 1)).
678+
679+
aPMMatrix := aPMMatrix raisedTo: 0.
680+
681+
expected := PMMatrix rows: #( #(1 0) #(0 1)).
682+
self assert: aPMMatrix equals: expected.
683+
]
684+
635685
{ #category : #comparing }
636686
PMMatrixTest >> testRowsColumns [
637687
| a |

0 commit comments

Comments
 (0)