Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to convert vectors to matrices and vice versa #129

Merged
23 changes: 23 additions & 0 deletions src/Math-Core/PMVector.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ PMVector >> asPMVector [
^ self
]

{ #category : #'as yet unclassified' }
PMVector >> checkDimensionalCompatibility: dimensionArray [
|prod|
prod := 1.

dimensionArray do: [ :each | prod := prod * each ].

self assert: (self size = prod) description: 'Imcompatible combination of Dimensions provided'.

^true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is unnecessary as you're not relying on the output in places where you send this message.

]

{ #category : #comparing }
PMVector >> closeTo: aPMVector [
"Compare two vectors using the default precision from Float >> #closeTo:."
Expand Down Expand Up @@ -237,6 +249,17 @@ PMVector >> productWithVector: aVector [
into: [ :sum :each | n := n + 1. (aVector at: n) * each + sum]
]

{ #category : #'as yet unclassified' }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this categorised as 'transformation'?

PMVector >> reshapeWithDimensions: dimensionArray [
| computedRows rowNum colNum |
self checkDimensionalCompatibility: dimensionArray.
rowNum := dimensionArray at: 1.
colNum := dimensionArray at: 2.
computedRows := ((1 to: rowNum) collect: [ :i | (1 to: colNum) collect: [ :j | self at: (i-1*colNum)+j ] ]).

^PMMatrix rows: computedRows
]

{ #category : #operation }
PMVector >> scalarProduct: aVector [

Expand Down
2 changes: 1 addition & 1 deletion src/Math-Matrix/PMJacobiTransformationHelper.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Class {
'eigenvalues',
'eigenvectors'
],
#category : #'Math-Matrix'
#category : 'Math-Matrix'
}

{ #category : #creation }
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Matrix/PMLUPDecomposition.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Class {
'permutation',
'parity'
],
#category : #'Math-Matrix'
#category : 'Math-Matrix'
}

{ #category : #creation }
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Matrix/PMLargestEigenValueFinder.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Class {
'eigenvector',
'transposeEigenvector'
],
#category : #'Math-Matrix'
#category : 'Math-Matrix'
}

{ #category : #information }
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Matrix/PMLinearEquationSystem.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Class {
'rows',
'solutions'
],
#category : #'Math-Matrix'
#category : 'Math-Matrix'
}

{ #category : #creation }
Expand Down
7 changes: 6 additions & 1 deletion src/Math-Matrix/PMMatrix.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Class {
'rows',
'lupDecomposition'
],
#category : #'Math-Matrix'
#category : 'Math-Matrix'
}

{ #category : #example }
Expand Down Expand Up @@ -243,6 +243,11 @@ PMMatrix >> asSymmetricMatrix [
^ 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."
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Matrix/PMSingularMatrixError.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ some calculations dont work with singular matrices and result eg with errors lik
Class {
#name : #PMSingularMatrixError,
#superclass : #ArithmeticError,
#category : #'Math-Matrix'
#category : 'Math-Matrix'
}

{ #category : #accessing }
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Matrix/PMSingularValueDecomposition.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Class {
'signU',
'signV'
],
#category : #'Math-Matrix'
#category : 'Math-Matrix'
}

{ #category : #'instance creation' }
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Matrix/PMSymmetricMatrix.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This class can be instantiated like DhbMatrix via #rows:, but the user has to ma
Class {
#name : #PMSymmetricMatrix,
#superclass : #PMMatrix,
#category : #'Math-Matrix'
#category : 'Math-Matrix'
}

{ #category : #'instance creation' }
Expand Down
22 changes: 22 additions & 0 deletions src/Math-Tests-Core/PMVectorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ PMVectorTest >> testAsArray [
self assert: #(1 2 3 4) asPMVector asArray equals: #(1 2 3 4)
]

{ #category : #tests }
PMVectorTest >> testMatrixConversionWithBothDims [
| vect result expected |
vect := #(1 0.5 0.2 3 1 -1 7 3 2 12 13 3) asPMVector .
result := vect reshapeWithDimensions: #(6 2).

expected := PMMatrix rows: #(#(1 0.5) #(0.2 3) #(1 -1) #(7 3) #(2 12) #(13 3)).

self assert: result equals: expected.
]

{ #category : #tests }
PMVectorTest >> testScalarProduct [
| u v |
Expand Down Expand Up @@ -299,6 +310,17 @@ PMVectorTest >> testVectorSum [
self assert: (u sum) equals: 6.
]

{ #category : #tests }
PMVectorTest >> testVectorToVectorConversion [
| vect result expected |
vect := #(1 0.5 0.2 3 1 -1 7 3 2 12 13 3) asPMVector .
result := vect reshapeWithDimensions: #(1 12).

expected := PMMatrix rows: #(#(1 0.5 0.2 3 1 -1 7 3 2 12 13 3)).

self assert: result equals: expected.
]

{ #category : #tests }
PMVectorTest >> testVectorZeros [
| v |
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Tests-Matrix/PMAdditionalTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ here are tests that would be in Math-Tests-DHB-Numerical, if it could construct
Class {
#name : #PMAdditionalTest,
#superclass : #TestCase,
#category : #'Math-Tests-Matrix'
#category : 'Math-Tests-Matrix'
}

{ #category : #tests }
Expand Down
9 changes: 8 additions & 1 deletion src/Math-Tests-Matrix/PMMatrixTest.class.st
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Class {
#name : #PMMatrixTest,
#superclass : #TestCase,
#category : #'Math-Tests-Matrix'
#category : 'Math-Tests-Matrix'
}

{ #category : #tests }
Expand Down Expand Up @@ -672,6 +672,13 @@ PMMatrixTest >> testSymmetricMatrixAdd3 [
self assert: ((c rowAt: 3) at: 1) equals: 31
]

{ #category : #tests }
PMMatrixTest >> testVectorConversion [
| m |
m := PMMatrix rows: #(#(1 2 3) #(4 5 6) #(7 8 9)).
self assert: m asVector equals: #(1 2 3 4 5 6 7 8 9) asPMVector
]

{ #category : #'linear algebra' }
PMMatrixTest >> testVectorMatrixOperation [
"Code Example 8.1"
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Tests-Matrix/PMQRTest.class.st
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Class {
#name : #PMQRTest,
#superclass : #TestCase,
#category : #'Math-Tests-Matrix'
#category : 'Math-Tests-Matrix'
}

{ #category : #running }
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Tests-Matrix/PMRestTest.class.st
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Class {
#name : #PMRestTest,
#superclass : #TestCase,
#category : #'Math-Tests-Matrix'
#category : 'Math-Tests-Matrix'
}

{ #category : #tests }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Class {
'actualV',
'actualS'
],
#category : #'Math-Tests-Matrix'
#category : 'Math-Tests-Matrix'
}

{ #category : #'as yet unclassified' }
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Tests-Matrix/PMSymmetricMatrixTest.class.st
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Class {
#name : #PMSymmetricMatrixTest,
#superclass : #TestCase,
#category : #'Math-Tests-Matrix'
#category : 'Math-Tests-Matrix'
}

{ #category : #tests }
Expand Down