From 6733ef288f2c893fca01f678eb4cf220510d3128 Mon Sep 17 00:00:00 2001 From: CyrilFerlicot Date: Mon, 28 Nov 2022 20:26:36 +0100 Subject: [PATCH 1/3] Remove dependency of Math-Core to Math-Matrix to cut circular dependency --- src/Math-Core/Number.extension.st | 6 ----- src/Math-Core/PMVector.class.st | 11 -------- src/Math-Matrix/Number.extension.st | 6 +++++ src/Math-Matrix/PMVector.extension.st | 12 +++++++++ src/Math-Tests-Core/PMVectorTest.class.st | 22 ---------------- .../PMAdditionalTest.class.st | 11 ++++++++ src/Math-Tests-Matrix/PMMatrixTest.class.st | 25 ++++++++++++++++++- 7 files changed, 53 insertions(+), 40 deletions(-) diff --git a/src/Math-Core/Number.extension.st b/src/Math-Core/Number.extension.st index 3bc58963d..c9b28b082 100644 --- a/src/Math-Core/Number.extension.st +++ b/src/Math-Core/Number.extension.st @@ -1,11 +1,5 @@ Extension { #name : #Number } -{ #category : #'*Math-Core' } -Number >> addWithRegularMatrix: aMatrix [ - "Adds itself to every row of the matrix" - ^ PMMatrix rows: (aMatrix rowsCollect: [ :row | row + self ]) -] - { #category : #'*Math-Core' } Number >> addWithVector: aVector [ "Adds itself to each element of the vector" diff --git a/src/Math-Core/PMVector.class.st b/src/Math-Core/PMVector.class.st index b9514d988..b4eb257ee 100644 --- a/src/Math-Core/PMVector.class.st +++ b/src/Math-Core/PMVector.class.st @@ -268,17 +268,6 @@ PMVector >> productWithVector: aVector [ into: [ :sum :each | n := n + 1. (aVector at: n) * each + sum] ] -{ #category : #'as yet unclassified' } -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 [ diff --git a/src/Math-Matrix/Number.extension.st b/src/Math-Matrix/Number.extension.st index 30551f460..81af666ef 100644 --- a/src/Math-Matrix/Number.extension.st +++ b/src/Math-Matrix/Number.extension.st @@ -1,5 +1,11 @@ Extension { #name : #Number } +{ #category : #'*Math-Matrix' } +Number >> addWithRegularMatrix: aMatrix [ + "Adds itself to every row of the matrix" + ^ PMMatrix rows: (aMatrix rowsCollect: [ :row | row + self ]) +] + { #category : #'*Math-Matrix' } Number >> productWithMatrix: aMatrix [ ^aMatrix class rows: (aMatrix rowsCollect: [:r| self productWithVector: r]) diff --git a/src/Math-Matrix/PMVector.extension.st b/src/Math-Matrix/PMVector.extension.st index 6631df75a..cef567a5f 100644 --- a/src/Math-Matrix/PMVector.extension.st +++ b/src/Math-Matrix/PMVector.extension.st @@ -1,5 +1,17 @@ Extension { #name : #PMVector } +{ #category : #'*Math-Matrix' } +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 : #'*Math-Matrix' } PMVector >> tensorProduct: aVector [ "Answers the tensor product of the receiver with aVector." diff --git a/src/Math-Tests-Core/PMVectorTest.class.st b/src/Math-Tests-Core/PMVectorTest.class.st index 3a1401888..51a433b6f 100644 --- a/src/Math-Tests-Core/PMVectorTest.class.st +++ b/src/Math-Tests-Core/PMVectorTest.class.st @@ -108,17 +108,6 @@ PMVectorTest >> testLessThan [ self assert: vec equals: vecCopy asPMVector. ] -{ #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 | @@ -375,17 +364,6 @@ 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 | diff --git a/src/Math-Tests-Matrix/PMAdditionalTest.class.st b/src/Math-Tests-Matrix/PMAdditionalTest.class.st index 2b64adb4c..88a4d37a9 100644 --- a/src/Math-Tests-Matrix/PMAdditionalTest.class.st +++ b/src/Math-Tests-Matrix/PMAdditionalTest.class.st @@ -8,6 +8,17 @@ Class { #category : #'Math-Tests-Matrix' } +{ #category : #tests } +PMAdditionalTest >> 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 } PMAdditionalTest >> testMatrixInversionSmall [ "it is here since it uses random matrices" diff --git a/src/Math-Tests-Matrix/PMMatrixTest.class.st b/src/Math-Tests-Matrix/PMMatrixTest.class.st index 628c6cba7..43fc84001 100644 --- a/src/Math-Tests-Matrix/PMMatrixTest.class.st +++ b/src/Math-Tests-Matrix/PMMatrixTest.class.st @@ -383,6 +383,17 @@ PMMatrixTest >> testMatrixCloseToPrecision [ self deny: (a closeTo: b precision: 0.2) ] +{ #category : #tests } +PMMatrixTest >> 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 : #comparing } PMMatrixTest >> testMatrixCos [ | a | @@ -930,7 +941,7 @@ PMMatrixTest >> testSymmetricMatrixAdd3 [ self assert: ((c rowAt: 3) at: 1) equals: 31 ] -{ #category : #test } +{ #category : #tests } PMMatrixTest >> testTake [ | m expected | @@ -967,6 +978,18 @@ PMMatrixTest >> testVectorMatrixOperation [ self assert: (v at: 2) equals: 4 ] +{ #category : #tests } +PMMatrixTest >> 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 : #'linear algebra' } PMMatrixTest >> testVectorTransposeMatrixOperation [ "Code Example 8.1" From 1745cc40bbe911fdbe904781a01acad3eeb9fb00 Mon Sep 17 00:00:00 2001 From: CyrilFerlicot Date: Mon, 28 Nov 2022 20:36:29 +0100 Subject: [PATCH 2/3] Remove duplication I introduced --- src/Math-Tests-Matrix/PMAdditionalTest.class.st | 11 ----------- src/Math-Tests-Matrix/PMMatrixTest.class.st | 13 +++++++------ 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/Math-Tests-Matrix/PMAdditionalTest.class.st b/src/Math-Tests-Matrix/PMAdditionalTest.class.st index 88a4d37a9..2b64adb4c 100644 --- a/src/Math-Tests-Matrix/PMAdditionalTest.class.st +++ b/src/Math-Tests-Matrix/PMAdditionalTest.class.st @@ -8,17 +8,6 @@ Class { #category : #'Math-Tests-Matrix' } -{ #category : #tests } -PMAdditionalTest >> 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 } PMAdditionalTest >> testMatrixInversionSmall [ "it is here since it uses random matrices" diff --git a/src/Math-Tests-Matrix/PMMatrixTest.class.st b/src/Math-Tests-Matrix/PMMatrixTest.class.st index 43fc84001..0104706e2 100644 --- a/src/Math-Tests-Matrix/PMMatrixTest.class.st +++ b/src/Math-Tests-Matrix/PMMatrixTest.class.st @@ -385,13 +385,14 @@ PMMatrixTest >> testMatrixCloseToPrecision [ { #category : #tests } PMMatrixTest >> 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. + 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 : #comparing } From eb3d2c251f67237e11d7be669983cb6a16a29c8f Mon Sep 17 00:00:00 2001 From: CyrilFerlicot Date: Mon, 28 Nov 2022 20:42:12 +0100 Subject: [PATCH 3/3] Move one more method from Core to Matrix --- src/Math-Core/PMVector.class.st | 6 ------ src/Math-Matrix/PMVector.extension.st | 8 ++++++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Math-Core/PMVector.class.st b/src/Math-Core/PMVector.class.st index b4eb257ee..20beb4db2 100644 --- a/src/Math-Core/PMVector.class.st +++ b/src/Math-Core/PMVector.class.st @@ -253,12 +253,6 @@ PMVector >> normalized [ ^ (1 / self norm) * self ] -{ #category : #operation } -PMVector >> productWithMatrix: aMatrix [ - "Answers the product of aMatrix with the receiver." - ^aMatrix rowsCollect: [ :each | each * self] -] - { #category : #operation } PMVector >> productWithVector: aVector [ "Answers the scalar product of aVector with the receiver." diff --git a/src/Math-Matrix/PMVector.extension.st b/src/Math-Matrix/PMVector.extension.st index cef567a5f..bd43674cf 100644 --- a/src/Math-Matrix/PMVector.extension.st +++ b/src/Math-Matrix/PMVector.extension.st @@ -1,5 +1,13 @@ Extension { #name : #PMVector } +{ #category : #'*Math-Matrix' } +PMVector >> productWithMatrix: aMatrix [ + + "Answers the product of aMatrix with the receiver." + + ^ aMatrix rowsCollect: [ :each | each * self ] +] + { #category : #'*Math-Matrix' } PMVector >> reshapeWithDimensions: dimensionArray [