|
| 1 | +" |
| 2 | +SVD-based implementation of a minimum-norm solution to the overdetermined least squares problem. |
| 3 | +" |
1 | 4 | Class { |
2 | 5 | #name : #PMLeastSquares, |
3 | 6 | #superclass : #Object, |
4 | 7 | #category : #'Math-Numerical' |
5 | 8 | } |
6 | 9 |
|
7 | | -{ #category : #'as yet unclassified' } |
| 10 | +{ #category : #accessing } |
8 | 11 | PMLeastSquares >> pseudoinverseOfDiagonal: aMatrix [ |
9 | | - "To get pseudoinverse of a diagonal rectangular matrix, we take reciprocal of any no-zero element of the main diagonal, leaving all zeros in place. Then we transpose the matrix." |
10 | | - |
| 12 | + |
| 13 | + "To get pseudoinverse of a diagonal rectangular matrix, we take reciprocal of any no-zero |
| 14 | + element of the main diagonal, leaving all zeros in place. Then we transpose the matrix." |
| 15 | + |
11 | 16 | | pseudoinverse diagonalSize | |
12 | | - |
13 | 17 | "Rows become columns and columns become rows because we transpose" |
14 | | - pseudoinverse := PMMatrix |
15 | | - zerosRows: aMatrix numberOfColumns |
16 | | - cols: aMatrix numberOfRows. |
17 | | - |
| 18 | + pseudoinverse := PMMatrix zerosRows: aMatrix numberOfColumns cols: aMatrix numberOfRows. |
| 19 | + |
18 | 20 | "The size of the main diagonal of a rectangular matrix is its smallest dimension" |
19 | 21 | diagonalSize := aMatrix numberOfRows min: aMatrix numberOfColumns. |
20 | | - |
| 22 | + |
21 | 23 | "Inverting the elements on the main diaginal" |
22 | | - 1 to: diagonalSize do: [ :i | |
23 | | - pseudoinverse at: i at: i put: ((aMatrix at: i at: i) = 0 |
24 | | - ifTrue: [ 0 ] ifFalse: [ 1 / (aMatrix at: i at: i) ]) ]. |
25 | | - |
| 24 | + 1 to: diagonalSize do: [ :i | |
| 25 | + pseudoinverse at: i at: i put: ((aMatrix at: i at: i) = 0 ifTrue: [ 0 ] |
| 26 | + ifFalse: [ 1 / (aMatrix at: i at: i) ]) ]. |
| 27 | + |
26 | 28 | ^ pseudoinverse |
27 | 29 | ] |
28 | 30 |
|
29 | | -{ #category : #'as yet unclassified' } |
| 31 | +{ #category : #api } |
30 | 32 | PMLeastSquares >> solveMatrixA: aMatrix matrixB: aMatrixOrVector [ |
31 | 33 |
|
32 | | - "SVD-based implementation of a minimum-norm solution to the overdetermined least squares problem. |
33 | | - |
34 | | - If b is a vector: |
| 34 | + "If b is a vector: |
35 | 35 | x' = minimize || b - Ax || |
36 | 36 | |
37 | 37 | If B is a matrix: |
|
0 commit comments