Skip to content

Commit cf2b92d

Browse files
committed
cleaned up methods and added accessors
1 parent 36bc15e commit cf2b92d

2 files changed

Lines changed: 44 additions & 50 deletions

File tree

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

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,6 @@ PMQRTest >> assert: inverse isMoorePenroseInverseOf: aMatrix [
2828
closeTo: aMatrix mpInverse transpose.
2929
]
3030

31-
{ #category : 'tests' }
32-
PMQRTest >> testDecompositionOfMatrixCausingErraticFailure [
33-
34-
| a qrDecomposition matricesAndPivot q r expectedMatrix pivot |
35-
a := PMSymmetricMatrix rows:
36-
#( #( 0.41929313699681925 0.05975350554089691
37-
0.2771676258543356 0.35628773381760703 )
38-
#( 0.05975350554089691 0.12794227252152854
39-
0.3257742693302102 0.28814463284245906 )
40-
#( 0.2771676258543356 0.3257742693302102 0.8468441832097453
41-
0.9101872061892353 )
42-
#( 0.35628773381760703 0.28814463284245906
43-
0.9101872061892353 0.5163744224777326 ) ).
44-
45-
qrDecomposition := PMQRDecomposition of: a.
46-
matricesAndPivot := qrDecomposition decomposeWithPivot.
47-
48-
expectedMatrix := PMMatrix rows:
49-
#( #( 0.2771676258543356 0.35628773381760703
50-
0.41929313699681925 0.05975350554089691 )
51-
#( 0.3257742693302102 0.28814463284245906
52-
0.05975350554089691 0.12794227252152854 )
53-
#( 0.8468441832097453 0.9101872061892353
54-
0.2771676258543356 0.3257742693302102 )
55-
#( 0.9101872061892353 0.5163744224777326
56-
0.35628773381760703 0.28814463284245906 ) ).
57-
q := matricesAndPivot at: 1.
58-
r := matricesAndPivot at: 2.
59-
pivot := matricesAndPivot at: 3.
60-
self assert: q * r closeTo: expectedMatrix.
61-
self assert: pivot equals: #( 3 4 3 nil )
62-
]
63-
6431
{ #category : 'tests' }
6532
PMQRTest >> testHorizontalRectangularMatrixCannotBeDecomposed [
6633

@@ -225,24 +192,25 @@ PMQRTest >> testSimpleQRDecomposition [
225192
{ #category : 'tests' }
226193
PMQRTest >> testSimpleQRDecompositionWithPivot [
227194

228-
| a qrDecomposition decomposition expected |
229-
a := PMMatrix rows: {
195+
| a qrDecomposition expectedQR expectedPivot reconstruction |
196+
a := PMMatrix rows: {
230197
{ 12. -51. 4 }.
231198
{ 6. 167. -68 }.
232199
{ -4. 24. -41 } }.
200+
expectedQR := PMMatrix rows: {
201+
{ -51. 4. 12 }.
202+
{ 167. -68. 6 }.
203+
{ 24. -41. -4 } }.
204+
expectedPivot := #( 2 3 1 ).
233205

234206
qrDecomposition := PMQRDecomposition of: a.
207+
qrDecomposition decomposeWithPivot.
235208

236-
decomposition := qrDecomposition decomposeWithPivot.
237-
decomposition first * decomposition second.
209+
self assert: qrDecomposition q * qrDecomposition r closeTo: expectedQR.
210+
self assert: qrDecomposition pivot equals: expectedPivot.
238211

239-
expected := PMMatrix rows: {
240-
{ -51. 4. 12 }.
241-
{ 167. -68. 6 }.
242-
{ 24. -41. -4 } }.
243-
self
244-
assert: decomposition first * decomposition second
245-
closeTo: expected
212+
reconstruction := qrDecomposition q * qrDecomposition r * qrDecomposition permutationMatrixFromPivot inverse.
213+
self assert: reconstruction closeTo: a
246214
]
247215

248216
{ #category : 'tests' }

src/Math-Matrix/PMQRDecomposition.class.st

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,8 @@ PMQRDecomposition >> householderReflectionOf: columnVector atColumnNumber: colum
121121
householderVector := columnVector householder.
122122
v := (PMVector zeros: columnNumber - 1) , (householderVector at: 2).
123123
identityMatrix := PMSymmetricMatrix identity: colSize.
124-
householderMatrix := identityMatrix
125-
-
126-
((householderVector at: 1) * v tensorProduct: v).
127-
^ Array with: householderVector with: householderMatrix .
124+
householderMatrix := identityMatrix - (householderVector first * v tensorProduct: v).
125+
^ Array with: householderVector with: householderMatrix
128126
]
129127

130128
{ #category : 'private' }
@@ -150,8 +148,36 @@ PMQRDecomposition >> of: matrix [
150148

151149
matrixToDecompose := matrix.
152150
colSize := matrixToDecompose numberOfRows.
153-
r := self initialRMatrix.
154-
q := self initialQMatrix.
151+
r := self initialRMatrix.
152+
q := self initialQMatrix
153+
]
154+
155+
{ #category : 'accessing' }
156+
PMQRDecomposition >> permutationMatrixFromPivot [
157+
158+
| matrix |
159+
matrix := PMMatrix zerosRows: matrixToDecompose numberOfRows cols: matrixToDecompose numberOfColumns.
160+
pivot withIndexCollect: [ :column :index | matrix at: column at: index put: 1 ].
161+
162+
^ matrix
163+
]
164+
165+
{ #category : 'accessing' }
166+
PMQRDecomposition >> pivot [
167+
168+
^ pivot
169+
]
170+
171+
{ #category : 'accessing' }
172+
PMQRDecomposition >> q [
173+
174+
^ q
175+
]
176+
177+
{ #category : 'accessing' }
178+
PMQRDecomposition >> r [
179+
180+
^ r
155181
]
156182

157183
{ #category : 'private' }

0 commit comments

Comments
 (0)