Skip to content

Commit 67ad541

Browse files
Merge pull request #206 from rakki-18/PMMatrix-negativeRows
handle negative values for shapes in PMMatrix
2 parents a6d0892 + c8aaa3b commit 67ad541

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

src/Math-Matrix/PMMatrix.class.st

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ PMMatrix class >> lupCRLCriticalDimension [
107107
]
108108

109109
{ #category : #'instance creation' }
110-
PMMatrix class >> new: anInteger [
111-
"Create an empty square matrix of dimension anInteger."
110+
PMMatrix class >> new: dimension [
111+
"Create an empty square matrix of size dimension x dimension."
112112

113-
^ self new initialize: anInteger
113+
^ self new initializeSquare: dimension
114114
]
115115

116116
{ #category : #'instance creation' }
@@ -510,12 +510,6 @@ PMMatrix >> hash [
510510
^ rows hash
511511
]
512512

513-
{ #category : #initialization }
514-
PMMatrix >> initialize: anInteger [
515-
"Build empty components for a square matrix. No check is made: components are assumed to be orgainized in rows."
516-
rows := (1 to: anInteger) asPMVector collect: [ :each | PMVector new: anInteger].
517-
]
518-
519513
{ #category : #initialization }
520514
PMMatrix >> initializeRows: anArrayOrVector [
521515
"Defines the components of the recevier. No check is made: components are assumed to be orgainized in rows."
@@ -525,10 +519,17 @@ PMMatrix >> initializeRows: anArrayOrVector [
525519
{ #category : #initialization }
526520
PMMatrix >> initializeRows: rowsInteger columns: columnsInteger [
527521
"Build empty components for a matrix."
528-
522+
self assert: [ rowsInteger isInteger and: [ rowsInteger > 0 ] ] description: 'Row size of a matrix must be a positive integer'.
523+
self assert: [ columnsInteger isInteger and: [ columnsInteger > 0 ] ] description: 'Column size of a matrix must be a positive integer'.
529524
rows := (1 to: rowsInteger) asPMVector collect: [ :each | PMVector new: columnsInteger ].
530525
]
531526

527+
{ #category : #initialization }
528+
PMMatrix >> initializeSquare: dimension [
529+
"Build empty components for a square matrix. No check is made: components are assumed to be orgainized in rows."
530+
^ self initializeRows: dimension columns: dimension
531+
]
532+
532533
{ #category : #operation }
533534
PMMatrix >> inverse [
534535
"Answer the inverse of the receiver."

src/Math-Tests-Matrix/PMMatrixTest.class.st

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,20 @@ PMMatrixTest >> testMatrixHash [
485485
]
486486

487487
{ #category : #'linear algebra' }
488-
PMMatrixTest >> testMatrixInitialize [
489-
| a |
490-
a := PMMatrix new initialize: 2.
491-
self assert: a numberOfRows equals: 2.
492-
self assert: a numberOfColumns equals: 2
488+
PMMatrixTest >> testMatrixInitializationWithNegativeDimensions [
489+
490+
" matrices with negative dimension and zero dimension should not be allowed."
491+
self should: [ PMMatrix new: -2 ] raise: AssertionFailure .
492+
self should: [ PMMatrix rows: 0 columns: 4 ] raise: AssertionFailure.
493+
]
494+
495+
{ #category : #'linear algebra' }
496+
PMMatrixTest >> testMatrixInitializeSquare [
497+
| aPMMatrix |
498+
aPMMatrix := PMMatrix new initializeSquare: 2.
499+
self assert: aPMMatrix numberOfRows equals: 2.
500+
self assert: aPMMatrix numberOfColumns equals: 2.
501+
493502
]
494503

495504
{ #category : #'linear algebra' }
@@ -543,6 +552,15 @@ PMMatrixTest >> testMatrixMultiply [
543552
self assert: ((c rowAt: 2) at: 3) equals: 4
544553
]
545554

555+
{ #category : #'linear algebra' }
556+
PMMatrixTest >> testMatrixNew [
557+
| aPMMatrix |
558+
aPMMatrix := PMMatrix new: 3.
559+
self assert: aPMMatrix numberOfRows equals: 3.
560+
self assert: aPMMatrix numberOfColumns equals: 3.
561+
562+
]
563+
546564
{ #category : #'linear algebra' }
547565
PMMatrixTest >> testMatrixPrincipalDiagonal [
548566
| a |

src/Math-Tests-Numerical/PMNumericalMethodsTestCase.class.st

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -637,11 +637,11 @@ PMNumericalMethodsTestCase >> testMatrixHash [
637637
]
638638

639639
{ #category : #'linear algebra' }
640-
PMNumericalMethodsTestCase >> testMatrixInitialize [
641-
| a |
642-
a := PMMatrix new initialize: 2.
643-
self assert: a numberOfRows equals: 2.
644-
self assert: a numberOfColumns equals: 2
640+
PMNumericalMethodsTestCase >> testMatrixInitializeSquare [
641+
| aPMMatrix |
642+
aPMMatrix := PMMatrix new initializeSquare: 2.
643+
self assert: aPMMatrix numberOfRows equals: 2.
644+
self assert: aPMMatrix numberOfColumns equals: 2
645645
]
646646

647647
{ #category : #'linear algebra' }

0 commit comments

Comments
 (0)