|
| 1 | +" |
| 2 | +A PMLeastSquaresTest is a test class for testing the behavior of PMLeastSquares |
| 3 | +" |
| 4 | +Class { |
| 5 | + #name : #PMLeastSquaresTest, |
| 6 | + #superclass : #TestCase, |
| 7 | + #instVars : [ |
| 8 | + 'leastSquares' |
| 9 | + ], |
| 10 | + #category : #'Math-Tests-Numerical' |
| 11 | +} |
| 12 | + |
| 13 | +{ #category : #running } |
| 14 | +PMLeastSquaresTest >> setUp [ |
| 15 | + |
| 16 | + super setUp. |
| 17 | + leastSquares := PMLeastSquares new |
| 18 | +] |
| 19 | + |
| 20 | +{ #category : #tests } |
| 21 | +PMLeastSquaresTest >> testPseudoinverseOfDiagonalSquareMatrix [ |
| 22 | + |
| 23 | + | matrix expectedInverse inverse | |
| 24 | + |
| 25 | + matrix := PMMatrix rows: #( |
| 26 | + (2 0 0 0) |
| 27 | + (0 1 0 0) |
| 28 | + (0 0 -3 0) |
| 29 | + (0 0 0 -1) ). |
| 30 | + |
| 31 | + expectedInverse := PMMatrix rows: { |
| 32 | + { 1/2 . 0 . 0 . 0 } . |
| 33 | + { 0 . 1 . 0 . 0 } . |
| 34 | + { 0 . 0 . -1/3 . 0} . |
| 35 | + { 0 . 0 . 0 . -1 } }. |
| 36 | + |
| 37 | + inverse := leastSquares pseudoinverseOfDiagonal: matrix. |
| 38 | + self assert: inverse closeTo: expectedInverse. |
| 39 | +] |
| 40 | + |
| 41 | +{ #category : #tests } |
| 42 | +PMLeastSquaresTest >> testPseudoinverseOfDiagonalSquareMatrixWithZeros [ |
| 43 | + |
| 44 | + | matrix expectedInverse inverse | |
| 45 | + |
| 46 | + matrix := PMMatrix rows: #( |
| 47 | + (2 0 0 0) |
| 48 | + (0 1 0 0) |
| 49 | + (0 0 0 0) |
| 50 | + (0 0 0 0) ). |
| 51 | + |
| 52 | + expectedInverse := PMMatrix rows: #( |
| 53 | + (0.5 0 0 0) |
| 54 | + ( 0 1 0 0) |
| 55 | + ( 0 0 0 0) |
| 56 | + ( 0 0 0 0) ). |
| 57 | + |
| 58 | + inverse := leastSquares pseudoinverseOfDiagonal: matrix. |
| 59 | + self assert: inverse closeTo: expectedInverse. |
| 60 | +] |
| 61 | + |
| 62 | +{ #category : #tests } |
| 63 | +PMLeastSquaresTest >> testPseudoinverseOfDiagonalTallMatrix [ |
| 64 | + |
| 65 | + | matrix expectedInverse inverse | |
| 66 | + |
| 67 | + matrix := PMMatrix rows: #( |
| 68 | + (2 0 0 0) |
| 69 | + (0 1 0 0) |
| 70 | + (0 0 -3 0) |
| 71 | + (0 0 0 -1) |
| 72 | + (0 0 0 0) |
| 73 | + (0 0 0 0) ). |
| 74 | + |
| 75 | + expectedInverse := PMMatrix rows: { |
| 76 | + { 1/2 . 0 . 0 . 0 . 0 . 0 } . |
| 77 | + { 0 . 1 . 0 . 0 . 0 . 0 } . |
| 78 | + { 0 . 0 . -1/3 . 0 . 0 . 0 } . |
| 79 | + { 0 . 0 . 0 . -1 . 0 . 0 }}. |
| 80 | + |
| 81 | + inverse := leastSquares pseudoinverseOfDiagonal: matrix. |
| 82 | + self assert: inverse closeTo: expectedInverse. |
| 83 | +] |
| 84 | + |
| 85 | +{ #category : #tests } |
| 86 | +PMLeastSquaresTest >> testPseudoinverseOfDiagonalWideMatrix [ |
| 87 | + |
| 88 | + | matrix expectedInverse inverse | |
| 89 | + |
| 90 | + matrix := PMMatrix rows: #( |
| 91 | + (2 0 0 0 0 0) |
| 92 | + (0 1 0 0 0 0) |
| 93 | + (0 0 -3 0 0 0) |
| 94 | + (0 0 0 -1 0 0)). |
| 95 | + |
| 96 | + expectedInverse := PMMatrix rows: { |
| 97 | + { 1/2 . 0 . 0 . 0 } . |
| 98 | + { 0 . 1 . 0 . 0 } . |
| 99 | + {0 . 0 . -1/3 . 0} . |
| 100 | + {0 . 0 . 0 . -1 } . |
| 101 | + {0 . 0 . 0 . 0 } . |
| 102 | + {0 . 0 . 0 . 0 }}. |
| 103 | + |
| 104 | + inverse := leastSquares pseudoinverseOfDiagonal: matrix. |
| 105 | + self assert: inverse closeTo: expectedInverse. |
| 106 | +] |
| 107 | + |
| 108 | +{ #category : #tests } |
| 109 | +PMLeastSquaresTest >> testSolveSmallOneSolution [ |
| 110 | + "Small example of least squares system (AX = B) with one solution taken from here: https://textbooks.math.gatech.edu/ila/least-squares.html" |
| 111 | + | matrixA vectorB expectedSolution solution | |
| 112 | + |
| 113 | + matrixA := PMMatrix rows: #( |
| 114 | + (0 1.1) |
| 115 | + (1 0 ) |
| 116 | + (0 -0.2) ). |
| 117 | + |
| 118 | + vectorB := #(1.1 -1.1 -0.2) asPMVector. |
| 119 | + expectedSolution := #(-1.1 1) asPMVector. |
| 120 | + |
| 121 | + solution := leastSquares |
| 122 | + solveMatrixA: matrixA |
| 123 | + matrixB: vectorB. |
| 124 | + |
| 125 | + self assert: solution closeTo: expectedSolution. |
| 126 | +] |
0 commit comments