Skip to content

Commit 5dc769b

Browse files
authored
Merge pull request #264 from olekscode/263-Remove-external-dependencies-of-Math-Tests-Matrix
Improved several test cases to reduce coupling and make the code cleaner
2 parents b88eee1 + ff3bd65 commit 5dc769b

File tree

2 files changed

+73
-50
lines changed

2 files changed

+73
-50
lines changed

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

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -157,34 +157,47 @@ PMMatrixTest >> testDimension [
157157
PMMatrixTest >> testEigenvalues [
158158
"Code Example 8.15"
159159

160-
| m charPol roots eigenvalues finder |
161-
m := PMMatrix rows: #(#(3 -2 0) #(-2 7 1) #(0 1 5)).
162-
charPol := PMPolynomial coefficients: #(82 -66 15 -1).
163-
roots := charPol roots asSortedCollection asArray reverse.
164-
finder := PMJacobiTransformation matrix: m.
160+
| matrix expectedEigenvalues eigenvalues finder |
161+
162+
matrix := PMMatrix rows: #(
163+
(3 -2 0)
164+
(-2 7 1)
165+
(0 1 5)).
166+
167+
expectedEigenvalues := #(8.105482616526306 4.776537928330764 2.1179794551429305).
168+
169+
finder := PMJacobiTransformation matrix: matrix.
165170
finder desiredPrecision: 1.0e-09.
171+
166172
eigenvalues := finder evaluate.
167-
self assert: eigenvalues size equals: 3.
168-
self assert: ((roots at: 1) - (eigenvalues at: 1)) abs < 1.0e-09.
169-
self assert: ((roots at: 2) - (eigenvalues at: 2)) abs < 1.0e-09.
170-
self assert: ((roots at: 3) - (eigenvalues at: 3)) abs < 1.0e-09
173+
174+
eigenvalues with: expectedEigenvalues do: [ :actual :expected |
175+
self assert: actual closeTo: expected ].
171176
]
172177

173178
{ #category : #'linear algebra' }
174179
PMMatrixTest >> testEigenvaluesLargest [
175180
"Code Example 8.13"
176181

177-
| m charPol roots eigenvalue finder |
178-
m := PMMatrix rows: #(#(3 -2 0) #(-2 7 1) #(0 1 5)).
179-
charPol := PMPolynomial coefficients: #(82 -66 15 -1).
180-
roots := charPol roots asSortedCollection asArray reverse.
181-
finder := PMLargestEigenValueFinder matrix: m.
182+
| matrix expectedEigenvalues firstEigenvalue secondEigenvalue finder |
183+
184+
matrix := PMMatrix rows: #(
185+
(3 -2 0)
186+
(-2 7 1)
187+
(0 1 5)).
188+
189+
expectedEigenvalues := #(8.105482616526306 4.776537928330764 2.1179794551429305).
190+
191+
finder := PMLargestEigenValueFinder matrix: matrix.
182192
finder desiredPrecision: 1.0e-08.
183-
eigenvalue := finder evaluate.
184-
self assert: ((roots at: 1) - eigenvalue) abs < 1.0e-08.
193+
194+
firstEigenvalue := finder evaluate.
195+
185196
finder := finder nextLargestEigenValueFinder.
186-
eigenvalue := finder evaluate.
187-
self assert: ((roots at: 2) - eigenvalue) abs < 1.0e-08
197+
secondEigenvalue := finder evaluate.
198+
199+
self assert: firstEigenvalue closeTo: expectedEigenvalues first.
200+
self assert: secondEigenvalue closeTo: expectedEigenvalues second.
188201
]
189202

190203
{ #category : #tests }
@@ -215,6 +228,30 @@ PMMatrixTest >> testIdentityMatrix [
215228
self assert: identityMatrix equals: expectedIdentityMatrix.
216229
]
217230

231+
{ #category : #'linear algebra' }
232+
PMMatrixTest >> testInversePureCRLSingularMatrixError [
233+
234+
self
235+
should: [ (PMSymmetricMatrix rows: #((1 2 3)(2 2 2)(3 2 1))) inversePureCRL ]
236+
raise: PMSingularMatrixError.
237+
]
238+
239+
{ #category : #'linear algebra' }
240+
PMMatrixTest >> testInversePureLUPSingularMatrixError [
241+
242+
self
243+
should: [ (PMSymmetricMatrix rows: #((1 2 3)(2 2 2)(3 2 1))) inversePureLUP ]
244+
raise: PMSingularMatrixError.
245+
]
246+
247+
{ #category : #'linear algebra' }
248+
PMMatrixTest >> testInverseSingularMatrixError [
249+
250+
self
251+
should: [ (PMMatrix rows: #((1 2 3)(3 4 6))) inverse ]
252+
raise: PMSingularMatrixError.
253+
]
254+
218255
{ #category : #tests }
219256
PMMatrixTest >> testIsRealOnComplexMatrix [
220257
| matrix |
@@ -757,22 +794,6 @@ PMMatrixTest >> testSimpleMatrixOperations [
757794
self assert: m transpose * m equals: m squared
758795
]
759796

760-
{ #category : #'linear algebra' }
761-
PMMatrixTest >> testSingularMatrixError [
762-
|h f|
763-
h:=PMHistogram new.
764-
h freeExtent: true.
765-
1 to: 3 do: [:i| h accumulate: i ].
766-
f:=PMLeastSquareFit histogram: h distributionClass: PMTriangularDistribution.
767-
self should: [ f evaluate ] raise: PMSingularMatrixError .
768-
"and not something completely incomprehensible"
769-
"also here:"
770-
self should: [ f errorMatrix ] raise: PMSingularMatrixError .
771-
self should: [(PMMatrix rows: #((1 2 3)(3 4 6)))inverse] raise: PMSingularMatrixError .
772-
self should: [(PMSymmetricMatrix rows: #((1 2 3)(2 2 2)(3 2 1)))inversePureLUP] raise: PMSingularMatrixError.
773-
self should: [(PMSymmetricMatrix rows: #((1 2 3)(2 2 2)(3 2 1)))inversePureCRL] raise: PMSingularMatrixError.
774-
]
775-
776797
{ #category : #tests }
777798
PMMatrixTest >> testSkalarMultiplication [
778799
| a r |

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,24 @@ PMNumericalMethodsTestCase >> testLeastSquarePolynomial [
391391
< (estimation error: 7.15)
392392
]
393393

394+
{ #category : #'iterative algorithms' }
395+
PMNumericalMethodsTestCase >> testLeastSquaresSingularMatrixError [
396+
| histogram leastSquares |
397+
398+
histogram := PMHistogram new
399+
freeExtent: true;
400+
yourself.
401+
402+
1 to: 3 do: [:i| histogram accumulate: i ].
403+
404+
leastSquares := PMLeastSquareFit
405+
histogram: histogram
406+
distributionClass: PMTriangularDistribution.
407+
408+
self should: [ leastSquares evaluate ] raise: PMSingularMatrixError.
409+
self should: [ leastSquares errorMatrix ] raise: PMSingularMatrixError .
410+
]
411+
394412
{ #category : #'iterative algorithms' }
395413
PMNumericalMethodsTestCase >> testLineSearch1 [
396414
"Test line searh for an initial step of Newton solver for equation
@@ -852,22 +870,6 @@ PMNumericalMethodsTestCase >> testOptimizeSimplex [
852870
self assert: (result at: 3) abs < 1.0e-6
853871
]
854872

855-
{ #category : #'linear algebra' }
856-
PMNumericalMethodsTestCase >> testSingularMatrixError [
857-
|h f|
858-
h:=PMHistogram new.
859-
h freeExtent: true.
860-
1 to: 3 do: [:i| h accumulate: i ].
861-
f:=PMLeastSquareFit histogram: h distributionClass: PMTriangularDistribution.
862-
self should: [ f evaluate ] raise: PMSingularMatrixError .
863-
"and not something completely incomprehensible"
864-
"also here:"
865-
self should: [ f errorMatrix ] raise: PMSingularMatrixError .
866-
self should: [(PMMatrix rows: #((1 2 3)(3 4 6)))inverse] raise: PMSingularMatrixError .
867-
self should: [(PMSymmetricMatrix rows: #((1 2 3)(2 2 2)(3 2 1)))inversePureLUP] raise: PMSingularMatrixError.
868-
self should: [(PMSymmetricMatrix rows: #((1 2 3)(2 2 2)(3 2 1)))inversePureCRL] raise: PMSingularMatrixError.
869-
]
870-
871873
{ #category : #statistics }
872874
PMNumericalMethodsTestCase >> testStatisticalMoments [
873875
"comment"

0 commit comments

Comments
 (0)