diff --git a/src/Math-Matrix/PMMatrix.class.st b/src/Math-Matrix/PMMatrix.class.st index 89823ece3..6eb9c81ca 100644 --- a/src/Math-Matrix/PMMatrix.class.st +++ b/src/Math-Matrix/PMMatrix.class.st @@ -124,6 +124,22 @@ PMMatrix class >> rows: anArrayOrVector [ ^ self new initializeRows: anArrayOrVector ] +{ #category : #'instance creation' } +PMMatrix class >> rows: rowsInteger columns: columnsInteger [ + + ^ self new initializeRows: rowsInteger columns: columnsInteger +] + +{ #category : #'instance creation' } +PMMatrix class >> rows: nRows columns: nCols element: fillElement [ + " Answer a new matrix of nRows x nCols initialized with fillElement in all cells " + + ^ (self new initializeRows: nRows columns: nCols) + atAllPut: fillElement; + yourself + +] + { #category : #'as yet unclassified' } PMMatrix class >> rows: rows columns: columns random: aMaxNumber [ "Answer a new Matrix of the given dimensions filled with random numbers" @@ -231,6 +247,13 @@ PMMatrix >> at: rowIndex at: columnIndex put: value [ ] +{ #category : #'cell accessing' } +PMMatrix >> atAllPut: element [ + "Put element at every one of the receiver's cells." + + self rowsDo: [ : row | row atAllPut: element ] +] + { #category : #'cell accessing' } PMMatrix >> atColumn: anInteger [ @@ -246,6 +269,19 @@ PMMatrix >> atColumn: aColumnIndex put: aCollection [ ] +{ #category : #'cell accessing' } +PMMatrix >> atColumn: columnIndex put: aValue repeat: repNumber [ + " Example: self atColumn: 1 fillWith: 'BM1818' repeat: 3 + produces + [ 'BM1818' nil nil nil + 'BM1818' nil nil nil + 'BM1818' nil nil nil + nil nil nil nil + nil nil nil nil ] +" + 1 to: repNumber do: [ : index | self rowAt: index columnAt: columnIndex put: aValue ]. +] + { #category : #'cell accessing' } PMMatrix >> atColumn: aColumnNumber put: aCollection startingAt: rowNumber [ " Fill the receiver with aCollection at aColumnNumber begining at rowNumber. " @@ -415,6 +451,13 @@ PMMatrix >> initializeRows: anArrayOrVector [ rows := anArrayOrVector asPMVector collect: [ :each | each asPMVector]. ] +{ #category : #initialization } +PMMatrix >> initializeRows: rowsInteger columns: columnsInteger [ + "Build empty components for a matrix." + + rows := (1 to: rowsInteger) asPMVector collect: [ :each | PMVector new: columnsInteger ]. +] + { #category : #operation } PMMatrix >> inverse [ "Answer the inverse of the receiver." diff --git a/src/Math-Tests-Matrix/PMMatrixTest.class.st b/src/Math-Tests-Matrix/PMMatrixTest.class.st index 35c864902..4ea448145 100644 --- a/src/Math-Tests-Matrix/PMMatrixTest.class.st +++ b/src/Math-Tests-Matrix/PMMatrixTest.class.st @@ -4,6 +4,32 @@ Class { #category : 'Math-Tests-Matrix' } +{ #category : #tests } +PMMatrixTest >> testAtAllPut [ + + | a | + a := PMMatrix new: 3. + a atAllPut: 4. + self assert: a equals: (PMMatrix rows: #(#(4 4 4) #(4 4 4) #(4 4 4))). + +] + +{ #category : #tests } +PMMatrixTest >> testAtColumnPutRepeat [ + + | a | + a := PMMatrix new: 3. + a + atColumn: 1 put: 1 repeat: 3; + atColumn: 2 put: 2 repeat: 3; + atColumn: 3 put: 3 repeat: 3. + self assert: a equals: (PMMatrix rows: #(#(1 2 3) #(1 2 3) #(1 2 3))). + a := PMMatrix new: 3. + a + atColumn: 1 put: 1 repeat: 2. + self assert: a equals: (PMMatrix rows: #(#(1 nil nil) #(1 nil nil) #(nil nil nil))). +] + { #category : #'linear algebra' } PMMatrixTest >> testAtRowPutAtColumnPut [ | a | @@ -432,6 +458,25 @@ PMMatrixTest >> testPrintOn [ self shouldnt: [m printOn: stream] raise: Error ] +{ #category : #comparing } +PMMatrixTest >> testRowsColumns [ + | a | + + a := PMMatrix rows: 3 columns: 4. + self assert: a dimension equals: 3 @ 4. +] + +{ #category : #tests } +PMMatrixTest >> testRowsColumnsElement [ + + | a | + + a := PMMatrix rows: 3 columns: 4 element: 1. + self assert: a dimension equals: 3 @ 4. + self assert: a equals: (PMMatrix rows: #(#(1 1 1 1) #(1 1 1 1) #(1 1 1 1))) + +] + { #category : #tests } PMMatrixTest >> testSimpleMatrixOperations [ | s m s2 r r2 |