From 0315406ceb8d78b680dd7cdcfd936a0f3e72d1e6 Mon Sep 17 00:00:00 2001 From: nikhilpinnaparaju Date: Sun, 26 May 2019 04:23:46 +0530 Subject: [PATCH 1/6] Added Basic Scalar subtraction test --- src/Math-Tests-Core/PMVectorTest.class.st | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Math-Tests-Core/PMVectorTest.class.st b/src/Math-Tests-Core/PMVectorTest.class.st index 082e82fe0..5c57e8ec5 100644 --- a/src/Math-Tests-Core/PMVectorTest.class.st +++ b/src/Math-Tests-Core/PMVectorTest.class.st @@ -93,6 +93,21 @@ PMVectorTest >> testScalarProductIsCommutative [ self assert: (u scalarProduct: v) equals: (v scalarProduct: u) ] +{ #category : #tests } +PMVectorTest >> testSubtractScalar [ + "Can we subtract Number from PMVector?" + + | vec num actual expected | + + vec := #(5 6 7) asPMVector. + num := 4. + + actual := vec - num. + expected := #(1 2 3) asPMVector. + + self assert: actual equals: expected. +] + { #category : #tests } PMVectorTest >> testTensorProduct [ | a b c | From edf0a44e71a64bb0f90b4b185c3b3deb705e7b00 Mon Sep 17 00:00:00 2001 From: nikhilpinnaparaju Date: Sun, 26 May 2019 04:32:22 +0530 Subject: [PATCH 2/6] added test for subtract PMVector from a Number --- src/Math-Tests-Core/PMVectorTest.class.st | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Math-Tests-Core/PMVectorTest.class.st b/src/Math-Tests-Core/PMVectorTest.class.st index 5c57e8ec5..38a2dbe0f 100644 --- a/src/Math-Tests-Core/PMVectorTest.class.st +++ b/src/Math-Tests-Core/PMVectorTest.class.st @@ -93,6 +93,21 @@ PMVectorTest >> testScalarProductIsCommutative [ self assert: (u scalarProduct: v) equals: (v scalarProduct: u) ] +{ #category : #tests } +PMVectorTest >> testSubFromScalar [ + "Can we subtract PMVector from a Number?" + + | num vec actual expected | + + num := 4. + vec := #(1 2 3) asPMVector. + + actual := num - vec. + expected := #(3 2 1) asPMVector. + + self assert: actual equals: expected. +] + { #category : #tests } PMVectorTest >> testSubtractScalar [ "Can we subtract Number from PMVector?" From e22d0a49e77fa6c635c0f18f1703c555f6b205e3 Mon Sep 17 00:00:00 2001 From: nikhilpinnaparaju Date: Sun, 26 May 2019 04:39:02 +0530 Subject: [PATCH 3/6] QuickFix of the scalar subtraction with PMVector Issue --- src/Math-Core/PMVector.class.st | 16 ++++------------ src/Math-Core/PMWeightedPoint.class.st | 2 +- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/Math-Core/PMVector.class.st b/src/Math-Core/PMVector.class.st index 9a015f0ee..f8eb04909 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' } @@ -70,17 +70,9 @@ PMVector >> + aVectorOrNumber [ ] { #category : #operation } -PMVector >> - aVector [ - "Answers the difference 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 +PMVector >> - aVectorOrNumber [ + "Answers the difference of the receiver with a vector or number." + ^ -1*aVectorOrNumber addWithVector: self ] { #category : #operation } diff --git a/src/Math-Core/PMWeightedPoint.class.st b/src/Math-Core/PMWeightedPoint.class.st index 68a6f6146..a7beea989 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 } From ccbe2d5211b5af0d6714a236ed88453c61a302d2 Mon Sep 17 00:00:00 2001 From: nikhilpinnaparaju Date: Thu, 30 May 2019 04:13:38 +0530 Subject: [PATCH 4/6] Added methods for intuitive conversion between vectors and matrices --- src/Math-Core/PMVector.class.st | 20 +++++++++++++++++++ .../PMJacobiTransformationHelper.class.st | 2 +- src/Math-Matrix/PMLUPDecomposition.class.st | 2 +- .../PMLargestEigenValueFinder.class.st | 2 +- .../PMLinearEquationSystem.class.st | 2 +- src/Math-Matrix/PMMatrix.class.st | 7 ++++++- .../PMSingularMatrixError.class.st | 2 +- .../PMSingularValueDecomposition.class.st | 2 +- src/Math-Matrix/PMSymmetricMatrix.class.st | 2 +- 9 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Math-Core/PMVector.class.st b/src/Math-Core/PMVector.class.st index f8eb04909..100f10c93 100644 --- a/src/Math-Core/PMVector.class.st +++ b/src/Math-Core/PMVector.class.st @@ -252,6 +252,26 @@ PMVector >> productWithVector: aVector [ into: [ :sum :each | n := n + 1. (aVector at: n) * each + sum] ] +{ #category : #'as yet unclassified' } +PMVector >> resizeWithRows: numOfRows Cols: numOfCols [ + + | computedRows rowNum colNum | + + numOfRows isNil ifFalse: [rowNum := numOfRows ]. + numOfCols isNil ifFalse: [colNum := numOfCols ]. + + (rowNum isNil and: colNum isNil) ifFalse: [ + numOfRows isNil ifTrue: [ rowNum := self size / colNum ] . + numOfCols isNil ifTrue: [ colNum := self size / rowNum ] . + ]. + + self assert: (rowNum * colNum = self size) description: 'Number of Rows not equal to number of Columns'. + + 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 [ diff --git a/src/Math-Matrix/PMJacobiTransformationHelper.class.st b/src/Math-Matrix/PMJacobiTransformationHelper.class.st index 7675cc075..be6f6f9ce 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 fe6392d72..75dbe790d 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 81478c9ab..06dfac388 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 3e5b8e3b6..8b021c459 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 900d3f209..37bb97f26 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 } @@ -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." diff --git a/src/Math-Matrix/PMSingularMatrixError.class.st b/src/Math-Matrix/PMSingularMatrixError.class.st index 6aa9392a8..6bf7cc4d7 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 65a2723e3..9753ef804 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 2e8a34764..fbfce867b 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' } From 702da9b9b95171c6bb81579ed18c14f4d7989b5c Mon Sep 17 00:00:00 2001 From: nikhilpinnaparaju Date: Thu, 30 May 2019 04:33:06 +0530 Subject: [PATCH 5/6] Tests for matrix <-> vector interconversions --- src/Math-Core/PMVector.class.st | 2 +- src/Math-Tests-Core/PMVectorTest.class.st | 33 +++++++++++++++++++++ src/Math-Tests-Matrix/PMMatrixTest.class.st | 7 +++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Math-Core/PMVector.class.st b/src/Math-Core/PMVector.class.st index c6b819bce..affbf1b6d 100644 --- a/src/Math-Core/PMVector.class.st +++ b/src/Math-Core/PMVector.class.st @@ -250,7 +250,7 @@ PMVector >> resizeWithRows: numOfRows Cols: numOfCols [ numOfCols isNil ifTrue: [ colNum := self size / rowNum ] . ]. - self assert: (rowNum * colNum = self size) description: 'Number of Rows not equal to number of Columns'. + self assert: (rowNum * colNum = self size) description: 'Number of Rows not compatible with number of Columns'. computedRows := ((1 to: rowNum) collect: [ :i | (1 to: colNum) collect: [ :j | self at: (i-1*colNum)+j ] ]). diff --git a/src/Math-Tests-Core/PMVectorTest.class.st b/src/Math-Tests-Core/PMVectorTest.class.st index 80fb47bc2..1876e854f 100644 --- a/src/Math-Tests-Core/PMVectorTest.class.st +++ b/src/Math-Tests-Core/PMVectorTest.class.st @@ -59,6 +59,39 @@ 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 resizeWithRows: 6 Cols: 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 >> testMatrixConversionWithColDim [ + | vect result expected | + vect := #(1 0.5 0.2 3 1 -1 7 3 2 12 13 3) asPMVector . + result := vect resizeWithRows: nil Cols: 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 >> testMatrixConversionWithRowDim [ + | vect result expected | + vect := #(1 0.5 0.2 3 1 -1 7 3 2 12 13 3) asPMVector . + result := vect resizeWithRows: 6 Cols: nil. + + 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 | diff --git a/src/Math-Tests-Matrix/PMMatrixTest.class.st b/src/Math-Tests-Matrix/PMMatrixTest.class.st index 96d4aa8de..32a26fd66 100644 --- a/src/Math-Tests-Matrix/PMMatrixTest.class.st +++ b/src/Math-Tests-Matrix/PMMatrixTest.class.st @@ -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" From 2d44383f05e65a12808141b35fa9d856b9e75a28 Mon Sep 17 00:00:00 2001 From: nikhilpinnaparaju Date: Wed, 5 Jun 2019 09:42:08 +0530 Subject: [PATCH 6/6] Fixed implementation of reshape --- src/Math-Core/PMVector.class.st | 29 ++++++++++--------- src/Math-Tests-Core/PMVectorTest.class.st | 35 ++++++++--------------- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/Math-Core/PMVector.class.st b/src/Math-Core/PMVector.class.st index affbf1b6d..1bc96b8e6 100644 --- a/src/Math-Core/PMVector.class.st +++ b/src/Math-Core/PMVector.class.st @@ -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 +] + { #category : #comparing } PMVector >> closeTo: aPMVector [ "Compare two vectors using the default precision from Float >> #closeTo:." @@ -238,20 +250,11 @@ PMVector >> productWithVector: aVector [ ] { #category : #'as yet unclassified' } -PMVector >> resizeWithRows: numOfRows Cols: numOfCols [ - +PMVector >> reshapeWithDimensions: dimensionArray [ | computedRows rowNum colNum | - - numOfRows isNil ifFalse: [rowNum := numOfRows ]. - numOfCols isNil ifFalse: [colNum := numOfCols ]. - - (rowNum isNil and: colNum isNil) ifFalse: [ - numOfRows isNil ifTrue: [ rowNum := self size / colNum ] . - numOfCols isNil ifTrue: [ colNum := self size / rowNum ] . - ]. - - self assert: (rowNum * colNum = self size) description: 'Number of Rows not compatible with number of Columns'. - + 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 diff --git a/src/Math-Tests-Core/PMVectorTest.class.st b/src/Math-Tests-Core/PMVectorTest.class.st index 1876e854f..6624f6f8b 100644 --- a/src/Math-Tests-Core/PMVectorTest.class.st +++ b/src/Math-Tests-Core/PMVectorTest.class.st @@ -63,29 +63,7 @@ PMVectorTest >> testAsArray [ PMVectorTest >> testMatrixConversionWithBothDims [ | vect result expected | vect := #(1 0.5 0.2 3 1 -1 7 3 2 12 13 3) asPMVector . - result := vect resizeWithRows: 6 Cols: 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 >> testMatrixConversionWithColDim [ - | vect result expected | - vect := #(1 0.5 0.2 3 1 -1 7 3 2 12 13 3) asPMVector . - result := vect resizeWithRows: nil Cols: 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 >> testMatrixConversionWithRowDim [ - | vect result expected | - vect := #(1 0.5 0.2 3 1 -1 7 3 2 12 13 3) asPMVector . - result := vect resizeWithRows: 6 Cols: nil. + result := vect reshapeWithDimensions: #(6 2). expected := PMMatrix rows: #(#(1 0.5) #(0.2 3) #(1 -1) #(7 3) #(2 12) #(13 3)). @@ -332,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 |