diff --git a/src/Math-Core/Number.extension.st b/src/Math-Core/Number.extension.st index 3bc58963..c9b28b08 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 b9514d98..20beb4db 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." @@ -268,17 +262,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 30551f46..81af666e 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 6631df75..bd43674c 100644 --- a/src/Math-Matrix/PMVector.extension.st +++ b/src/Math-Matrix/PMVector.extension.st @@ -1,5 +1,25 @@ 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 [ + + | 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 3a140188..51a433b6 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/PMMatrixTest.class.st b/src/Math-Tests-Matrix/PMMatrixTest.class.st index 628c6cba..0104706e 100644 --- a/src/Math-Tests-Matrix/PMMatrixTest.class.st +++ b/src/Math-Tests-Matrix/PMMatrixTest.class.st @@ -383,6 +383,18 @@ 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 +942,7 @@ PMMatrixTest >> testSymmetricMatrixAdd3 [ self assert: ((c rowAt: 3) at: 1) equals: 31 ] -{ #category : #test } +{ #category : #tests } PMMatrixTest >> testTake [ | m expected | @@ -967,6 +979,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"