From 735e927050d6e0b2c192546b3344142ed9012563 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 20 May 2019 21:39:54 +0100 Subject: [PATCH 1/5] [issue-29] Marked PMMatrix's dot message as deprecated and introduced the hadamard product in its place. Replaced calls to dot in places where we can be confident nothing breaks. --- src/Math-Matrix/PMJacobiTransformationHelper.class.st | 2 +- src/Math-Matrix/PMLUPDecomposition.class.st | 2 +- src/Math-Matrix/PMLargestEigenValueFinder.class.st | 2 +- src/Math-Matrix/PMLinearEquationSystem.class.st | 2 +- src/Math-Matrix/PMMatrix.class.st | 11 ++++++++--- src/Math-Matrix/PMSingularMatrixError.class.st | 2 +- src/Math-Matrix/PMSingularValueDecomposition.class.st | 2 +- src/Math-Matrix/PMSymmetricMatrix.class.st | 2 +- .../PMSciKitLearnSVDFlipAlgorithm.class.st | 4 ++-- src/Math-Tests-Matrix/PMAdditionalTest.class.st | 2 +- src/Math-Tests-Matrix/PMMatrixTest.class.st | 4 ++-- src/Math-Tests-Matrix/PMQRTest.class.st | 2 +- src/Math-Tests-Matrix/PMRestTest.class.st | 2 +- .../PMSingularValueDecompositionTest.class.st | 2 +- src/Math-Tests-Matrix/PMSymmetricMatrixTest.class.st | 2 +- 15 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Math-Matrix/PMJacobiTransformationHelper.class.st b/src/Math-Matrix/PMJacobiTransformationHelper.class.st index be6f6f9ce..7675cc075 100644 --- a/src/Math-Matrix/PMJacobiTransformationHelper.class.st +++ b/src/Math-Matrix/PMJacobiTransformationHelper.class.st @@ -8,7 +8,7 @@ Class { 'eigenvalues', 'eigenvectors' ], - #category : 'Math-Matrix' + #category : #'Math-Matrix' } { #category : #creation } diff --git a/src/Math-Matrix/PMLUPDecomposition.class.st b/src/Math-Matrix/PMLUPDecomposition.class.st index 75dbe790d..fe6392d72 100644 --- a/src/Math-Matrix/PMLUPDecomposition.class.st +++ b/src/Math-Matrix/PMLUPDecomposition.class.st @@ -26,7 +26,7 @@ Class { 'permutation', 'parity' ], - #category : 'Math-Matrix' + #category : #'Math-Matrix' } { #category : #creation } diff --git a/src/Math-Matrix/PMLargestEigenValueFinder.class.st b/src/Math-Matrix/PMLargestEigenValueFinder.class.st index 06dfac388..81478c9ab 100644 --- a/src/Math-Matrix/PMLargestEigenValueFinder.class.st +++ b/src/Math-Matrix/PMLargestEigenValueFinder.class.st @@ -20,7 +20,7 @@ Class { 'eigenvector', 'transposeEigenvector' ], - #category : 'Math-Matrix' + #category : #'Math-Matrix' } { #category : #information } diff --git a/src/Math-Matrix/PMLinearEquationSystem.class.st b/src/Math-Matrix/PMLinearEquationSystem.class.st index 8b021c459..3e5b8e3b6 100644 --- a/src/Math-Matrix/PMLinearEquationSystem.class.st +++ b/src/Math-Matrix/PMLinearEquationSystem.class.st @@ -22,7 +22,7 @@ Class { 'rows', 'solutions' ], - #category : 'Math-Matrix' + #category : #'Math-Matrix' } { #category : #creation } diff --git a/src/Math-Matrix/PMMatrix.class.st b/src/Math-Matrix/PMMatrix.class.st index c3547d753..900d3f209 100644 --- a/src/Math-Matrix/PMMatrix.class.st +++ b/src/Math-Matrix/PMMatrix.class.st @@ -15,7 +15,7 @@ Class { 'rows', 'lupDecomposition' ], - #category : 'Math-Matrix' + #category : #'Math-Matrix' } { #category : #example } @@ -31,7 +31,7 @@ b := PMMatrix rows: #( ( 1 2 3 ) (-2 1 7)). c := a * b. "Elementwise matrix product" -d := a dot:b. +d := a hadamardProduct: b. "This is how we can create a vector" a := #(1 4 9 16 25) asPMVector. @@ -407,7 +407,7 @@ PMMatrix >> dimension [ ^ self rows size @ (self rows at: 1) size ] -{ #category : #operation } +{ #category : #deprecated } PMMatrix >> dot: aMatrix [ "Answers the elementwise product of the receiver with aMatrix." ^ aMatrix elementwiseProductWithMatrix: self @@ -458,6 +458,11 @@ PMMatrix >> flattenRows [ ^ answer asPMVector ] +{ #category : #operation } +PMMatrix >> hadamardProduct: aMatrix [ + ^ aMatrix elementwiseProductWithMatrix: self +] + { #category : #comparing } PMMatrix >> hash [ ^ rows hash diff --git a/src/Math-Matrix/PMSingularMatrixError.class.st b/src/Math-Matrix/PMSingularMatrixError.class.st index 6bf7cc4d7..6aa9392a8 100644 --- a/src/Math-Matrix/PMSingularMatrixError.class.st +++ b/src/Math-Matrix/PMSingularMatrixError.class.st @@ -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 } diff --git a/src/Math-Matrix/PMSingularValueDecomposition.class.st b/src/Math-Matrix/PMSingularValueDecomposition.class.st index 9753ef804..65a2723e3 100644 --- a/src/Math-Matrix/PMSingularValueDecomposition.class.st +++ b/src/Math-Matrix/PMSingularValueDecomposition.class.st @@ -40,7 +40,7 @@ Class { 'signU', 'signV' ], - #category : 'Math-Matrix' + #category : #'Math-Matrix' } { #category : #'instance creation' } diff --git a/src/Math-Matrix/PMSymmetricMatrix.class.st b/src/Math-Matrix/PMSymmetricMatrix.class.st index fbfce867b..2e8a34764 100644 --- a/src/Math-Matrix/PMSymmetricMatrix.class.st +++ b/src/Math-Matrix/PMSymmetricMatrix.class.st @@ -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' } diff --git a/src/Math-PrincipalComponentAnalysis/PMSciKitLearnSVDFlipAlgorithm.class.st b/src/Math-PrincipalComponentAnalysis/PMSciKitLearnSVDFlipAlgorithm.class.st index 44f7ceeeb..1edaf980a 100644 --- a/src/Math-PrincipalComponentAnalysis/PMSciKitLearnSVDFlipAlgorithm.class.st +++ b/src/Math-PrincipalComponentAnalysis/PMSciKitLearnSVDFlipAlgorithm.class.st @@ -78,10 +78,10 @@ PMSciKitLearnSVDFlipAlgorithm >> signs [ { #category : #accessing } PMSciKitLearnSVDFlipAlgorithm >> uFlipped [ - ^ u dot: (self signMatrixForU). + ^ u hadamardProduct: (self signMatrixForU). ] { #category : #accessing } PMSciKitLearnSVDFlipAlgorithm >> vFlipped [ - ^ v dot: (self signMatrixForV) . + ^ v hadamardProduct: (self signMatrixForV) . ] diff --git a/src/Math-Tests-Matrix/PMAdditionalTest.class.st b/src/Math-Tests-Matrix/PMAdditionalTest.class.st index eea0c4266..8b32fa46b 100644 --- a/src/Math-Tests-Matrix/PMAdditionalTest.class.st +++ b/src/Math-Tests-Matrix/PMAdditionalTest.class.st @@ -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 } diff --git a/src/Math-Tests-Matrix/PMMatrixTest.class.st b/src/Math-Tests-Matrix/PMMatrixTest.class.st index 2c065d7bd..7febfe59c 100644 --- a/src/Math-Tests-Matrix/PMMatrixTest.class.st +++ b/src/Math-Tests-Matrix/PMMatrixTest.class.st @@ -1,7 +1,7 @@ Class { #name : #PMMatrixTest, #superclass : #TestCase, - #category : 'Math-Tests-Matrix' + #category : #'Math-Tests-Matrix' } { #category : #tests } @@ -428,7 +428,7 @@ PMMatrixTest >> testMatrixMultiplyElementwise [ | a b c | a := PMMatrix rows: #(#(1 0 1) #(-1 -2 3)). b := PMMatrix rows: #(#(1 2 3) #(-2 1 7)). - c := a dot:b. + c := a hadamardProduct: b. self assert: c numberOfRows equals: 2. self assert: c numberOfColumns equals: 3. self assert: ((c rowAt: 1) at: 1) equals: 1. diff --git a/src/Math-Tests-Matrix/PMQRTest.class.st b/src/Math-Tests-Matrix/PMQRTest.class.st index 6f8a23303..930267b70 100644 --- a/src/Math-Tests-Matrix/PMQRTest.class.st +++ b/src/Math-Tests-Matrix/PMQRTest.class.st @@ -1,7 +1,7 @@ Class { #name : #PMQRTest, #superclass : #TestCase, - #category : 'Math-Tests-Matrix' + #category : #'Math-Tests-Matrix' } { #category : #running } diff --git a/src/Math-Tests-Matrix/PMRestTest.class.st b/src/Math-Tests-Matrix/PMRestTest.class.st index d4db439f1..667530dd8 100644 --- a/src/Math-Tests-Matrix/PMRestTest.class.st +++ b/src/Math-Tests-Matrix/PMRestTest.class.st @@ -1,7 +1,7 @@ Class { #name : #PMRestTest, #superclass : #TestCase, - #category : 'Math-Tests-Matrix' + #category : #'Math-Tests-Matrix' } { #category : #tests } diff --git a/src/Math-Tests-Matrix/PMSingularValueDecompositionTest.class.st b/src/Math-Tests-Matrix/PMSingularValueDecompositionTest.class.st index d3f6d1b79..43e06794e 100644 --- a/src/Math-Tests-Matrix/PMSingularValueDecompositionTest.class.st +++ b/src/Math-Tests-Matrix/PMSingularValueDecompositionTest.class.st @@ -35,7 +35,7 @@ Class { 'actualV', 'actualS' ], - #category : 'Math-Tests-Matrix' + #category : #'Math-Tests-Matrix' } { #category : #'as yet unclassified' } diff --git a/src/Math-Tests-Matrix/PMSymmetricMatrixTest.class.st b/src/Math-Tests-Matrix/PMSymmetricMatrixTest.class.st index 0d6ad00be..f0a1e2870 100644 --- a/src/Math-Tests-Matrix/PMSymmetricMatrixTest.class.st +++ b/src/Math-Tests-Matrix/PMSymmetricMatrixTest.class.st @@ -1,7 +1,7 @@ Class { #name : #PMSymmetricMatrixTest, #superclass : #TestCase, - #category : 'Math-Tests-Matrix' + #category : #'Math-Tests-Matrix' } { #category : #tests } From b422b39d245b77f14c5e3fa9367200ca2ca19b37 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 20 May 2019 22:37:13 +0100 Subject: [PATCH 2/5] [issue-29] Introduce the new and better-named elementwiseTimes message. --- src/Math-Core/PMVector.class.st | 17 ++++++++++++++++- src/Math-Core/PMWeightedPoint.class.st | 2 +- src/Math-Tests-Core/PMVectorTest.class.st | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Math-Core/PMVector.class.st b/src/Math-Core/PMVector.class.st index 2786e4288..6bc438ddf 100644 --- a/src/Math-Core/PMVector.class.st +++ b/src/Math-Core/PMVector.class.st @@ -20,7 +20,7 @@ Class { #name : #PMVector, #superclass : #Array, #type : #variable, - #category : 'Math-Core' + #category : #'Math-Core' } { #category : #'instance creation' } @@ -182,6 +182,21 @@ PMVector >> dot: aVector [ ] +{ #category : #operation } +PMVector >> elementwiseTimes: aVector [ + "Answers the elementwise product of the receiver with aVector." + + | answer n | + answer := self class new: self size. + n := 0. + self + with: aVector + do: [ :a :b | + n := n + 1. + answer at: n put: a * b ]. + ^ answer +] + { #category : #'as yet unclassified' } PMVector >> householder [ "returns a collection of the skalar beta and the housholder vector" diff --git a/src/Math-Core/PMWeightedPoint.class.st b/src/Math-Core/PMWeightedPoint.class.st index a7beea989..68a6f6146 100644 --- a/src/Math-Core/PMWeightedPoint.class.st +++ b/src/Math-Core/PMWeightedPoint.class.st @@ -12,7 +12,7 @@ Class { 'weight', 'error' ], - #category : 'Math-Core' + #category : #'Math-Core' } { #category : #creation } diff --git a/src/Math-Tests-Core/PMVectorTest.class.st b/src/Math-Tests-Core/PMVectorTest.class.st index 84afe11c0..b0aef091a 100644 --- a/src/Math-Tests-Core/PMVectorTest.class.st +++ b/src/Math-Tests-Core/PMVectorTest.class.st @@ -151,6 +151,20 @@ PMVectorTest >> testVectorDotProduct [ self assert: (w at: 3) equals: 15 ] +{ #category : #tests } +PMVectorTest >> testVectorElementwiseTimes [ + "Code Example 8.1" + + | u v w | + u := #(1 2 3) asPMVector. + v := #(3 4 5) asPMVector. + w := u elementwiseTimes: v. + self assert: (w size) equals: 3. + self assert: (w at: 1) equals: 3. + self assert: (w at: 2) equals: 8. + self assert: (w at: 3) equals: 15 +] + { #category : #tests } PMVectorTest >> testVectorGreater [ From ff48ea0121ba08525865f347506d6e867b602f74 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 20 May 2019 22:39:03 +0100 Subject: [PATCH 3/5] [issue-29] Marked the PMVector dot message as deprecated. --- src/Math-Core/PMVector.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Math-Core/PMVector.class.st b/src/Math-Core/PMVector.class.st index 6bc438ddf..e650a739e 100644 --- a/src/Math-Core/PMVector.class.st +++ b/src/Math-Core/PMVector.class.st @@ -167,7 +167,7 @@ PMVector >> cumsum [ ] -{ #category : #operation } +{ #category : #deprecated } PMVector >> dot: aVector [ "Answers the elementwise product of the receiver with aVector." | answer n | From 736ba82b2ee7c3b07dd0756d841a1ca29abb63fc Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 20 May 2019 22:42:41 +0100 Subject: [PATCH 4/5] [issue-29] Added a separate test for the Hadamard product and reverted the element-wise multiply test to its original state. --- src/Math-Tests-Matrix/PMMatrixTest.class.st | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Math-Tests-Matrix/PMMatrixTest.class.st b/src/Math-Tests-Matrix/PMMatrixTest.class.st index 7febfe59c..b27b24464 100644 --- a/src/Math-Tests-Matrix/PMMatrixTest.class.st +++ b/src/Math-Tests-Matrix/PMMatrixTest.class.st @@ -352,6 +352,22 @@ PMMatrixTest >> testMatrixGreater [ self assert: ((b rowAt: 2) at: 3) equals: true ] +{ #category : #'linear algebra' } +PMMatrixTest >> testMatrixHadamardProduct [ + | a b c | + a := PMMatrix rows: #(#(1 0 1) #(-1 -2 3)). + b := PMMatrix rows: #(#(1 2 3) #(-2 1 7)). + c := a hadamardProduct: b. + self assert: c numberOfRows equals: 2. + self assert: c numberOfColumns equals: 3. + self assert: ((c rowAt: 1) at: 1) equals: 1. + self assert: ((c rowAt: 1) at: 2) equals: 0. + self assert: ((c rowAt: 1) at: 3) equals: 3. + self assert: ((c rowAt: 2) at: 1) equals: 2. + self assert: ((c rowAt: 2) at: 2) equals: -2. + self assert: ((c rowAt: 2) at: 3) equals: 21 +] + { #category : #comparing } PMMatrixTest >> testMatrixHash [ | a b c | @@ -428,7 +444,7 @@ PMMatrixTest >> testMatrixMultiplyElementwise [ | a b c | a := PMMatrix rows: #(#(1 0 1) #(-1 -2 3)). b := PMMatrix rows: #(#(1 2 3) #(-2 1 7)). - c := a hadamardProduct: b. + c := a dot: b. self assert: c numberOfRows equals: 2. self assert: c numberOfColumns equals: 3. self assert: ((c rowAt: 1) at: 1) equals: 1. From a362fdf44675ac2d7f877a2541af645e9cee59c1 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Tue, 21 May 2019 22:06:17 +0100 Subject: [PATCH 5/5] [issue-29] Improved the name of the method. --- src/Math-Core/PMVector.class.st | 2 +- src/Math-Tests-Core/PMVectorTest.class.st | 28 +++++++++++------------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Math-Core/PMVector.class.st b/src/Math-Core/PMVector.class.st index e650a739e..9a015f0ee 100644 --- a/src/Math-Core/PMVector.class.st +++ b/src/Math-Core/PMVector.class.st @@ -183,7 +183,7 @@ PMVector >> dot: aVector [ ] { #category : #operation } -PMVector >> elementwiseTimes: aVector [ +PMVector >> hadamardProduct: aVector [ "Answers the elementwise product of the receiver with aVector." | answer n | diff --git a/src/Math-Tests-Core/PMVectorTest.class.st b/src/Math-Tests-Core/PMVectorTest.class.st index b0aef091a..082e82fe0 100644 --- a/src/Math-Tests-Core/PMVectorTest.class.st +++ b/src/Math-Tests-Core/PMVectorTest.class.st @@ -151,20 +151,6 @@ PMVectorTest >> testVectorDotProduct [ self assert: (w at: 3) equals: 15 ] -{ #category : #tests } -PMVectorTest >> testVectorElementwiseTimes [ - "Code Example 8.1" - - | u v w | - u := #(1 2 3) asPMVector. - v := #(3 4 5) asPMVector. - w := u elementwiseTimes: v. - self assert: (w size) equals: 3. - self assert: (w at: 1) equals: 3. - self assert: (w at: 2) equals: 8. - self assert: (w at: 3) equals: 15 -] - { #category : #tests } PMVectorTest >> testVectorGreater [ @@ -178,6 +164,20 @@ PMVectorTest >> testVectorGreater [ ] +{ #category : #tests } +PMVectorTest >> testVectorHadamardProduct [ + "Code Example 8.1" + + | u v w | + u := #(1 2 3) asPMVector. + v := #(3 4 5) asPMVector. + w := u hadamardProduct: v. + self assert: (w size) equals: 3. + self assert: (w at: 1) equals: 3. + self assert: (w at: 2) equals: 8. + self assert: (w at: 3) equals: 15 +] + { #category : #tests } PMVectorTest >> testVectorLess [