diff --git a/src/Math-Matrix/PMMatrix.class.st b/src/Math-Matrix/PMMatrix.class.st index 60b26384..8fe8dcc8 100644 --- a/src/Math-Matrix/PMMatrix.class.st +++ b/src/Math-Matrix/PMMatrix.class.st @@ -824,6 +824,27 @@ PMMatrix >> qrFactorizationWithPivoting [ ^ 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 diff --git a/src/Math-Tests-Matrix/PMMatrixTest.class.st b/src/Math-Tests-Matrix/PMMatrixTest.class.st index 9e872b51..c4e97103 100644 --- a/src/Math-Tests-Matrix/PMMatrixTest.class.st +++ b/src/Math-Tests-Matrix/PMMatrixTest.class.st @@ -632,6 +632,56 @@ PMMatrixTest >> testPrintOn [ m printOn: stream ] +{ #category : #tests } +PMMatrixTest >> testRaisedToNegativeInteger [ + + |aPMMatrix expected| + + aPMMatrix := PMMatrix rows: #(#(3 1) #(1 1)). + + aPMMatrix := aPMMatrix raisedTo: -2. + + expected := PMMatrix rows: #(#(0.5 -1) #(-1 2.5)). + self assert: aPMMatrix equals: expected. +] + +{ #category : #tests } +PMMatrixTest >> testRaisedToNonSquareMatrix [ + + |aPMMatrix| + + aPMMatrix := PMMatrix rows: #(#(3 1 4) #(1 1 2)). + + self should: [ aPMMatrix raisedTo: 3 ] raise: AssertionFailure. + +] + +{ #category : #tests } +PMMatrixTest >> testRaisedToPositiveInteger [ + + |aPMMatrix expected| + + aPMMatrix := PMMatrix rows: #(#(3 1) #(1 1)). + + aPMMatrix := aPMMatrix raisedTo: 3. + + expected := PMMatrix rows: #(#(34 14) #(14 6)). + self assert: aPMMatrix equals: expected. +] + +{ #category : #tests } +PMMatrixTest >> testRaisedToZero [ + + |aPMMatrix expected| + + aPMMatrix := PMMatrix rows: #(#(3 1) #(1 1)). + + aPMMatrix := aPMMatrix raisedTo: 0. + + expected := PMMatrix rows: #( #(1 0) #(0 1)). + self assert: aPMMatrix equals: expected. +] + { #category : #comparing } PMMatrixTest >> testRowsColumns [ | a |