diff --git a/src/Math-Matrix-Tests/PMMatrixTest.class.st b/src/Math-Matrix-Tests/PMMatrixTest.class.st index 57e7c84..8e82e8b 100644 --- a/src/Math-Matrix-Tests/PMMatrixTest.class.st +++ b/src/Math-Matrix-Tests/PMMatrixTest.class.st @@ -163,6 +163,22 @@ PMMatrixTest >> testDimension [ ] +{ #category : 'linear algebra' } +PMMatrixTest >> testEigenForSingleValueMatrix [ + + | matrix expectedEigenvalues eigenvalues expectedEigenvectors eigenvectors | + matrix := PMMatrix rows: #( #( 3 ) ). + + expectedEigenvalues := #( 3 ). + expectedEigenvectors := PMVector with: (PMVector ones: 1). + + eigenvalues := matrix eigen values. + eigenvectors := matrix eigen vectors. + + eigenvalues with: expectedEigenvalues do: [ :actual :expected | self assert: actual closeTo: expected ]. + eigenvectors with: expectedEigenvectors do: [ :actual :expected | self assert: actual closeTo: expected ] +] + { #category : 'linear algebra' } PMMatrixTest >> testEigenvalues [ "Code Example 8.15" diff --git a/src/Math-Matrix/PMMatrix.class.st b/src/Math-Matrix/PMMatrix.class.st index 5a57326..125e781 100644 --- a/src/Math-Matrix/PMMatrix.class.st +++ b/src/Math-Matrix/PMMatrix.class.st @@ -498,6 +498,8 @@ PMMatrix >> eigen [ 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' ] diff --git a/src/Math-Matrix/PMSingleValueMatrixHelper.class.st b/src/Math-Matrix/PMSingleValueMatrixHelper.class.st new file mode 100644 index 0000000..34dfdb5 --- /dev/null +++ b/src/Math-Matrix/PMSingleValueMatrixHelper.class.st @@ -0,0 +1,13 @@ +Class { + #name : 'PMSingleValueMatrixHelper', + #superclass : 'PMJacobiTransformationHelper', + #category : 'Math-Matrix', + #package : 'Math-Matrix' +} + +{ #category : 'initialization' } +PMSingleValueMatrixHelper >> initialize: aSingleValueMatrix [ + + eigenvalues := Array with: (aSingleValueMatrix at: 1 at: 1). + eigenvectors := PMVector with: (PMVector ones: 1) +]