Skip to content

Commit 4f7bd73

Browse files
committed
fix #207 in cholesky decomposition
A precedence error in the binary block of #inject:into was causing cholesky decomposition to fail for matrices of size more than 3. It has been corrected by adding parenthesis in the block. New tests with matrix size more than 3 has also been added.
1 parent 46ffd24 commit 4f7bd73

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/Math-Matrix/PMMatrix.class.st

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,20 @@ PMMatrix >> choleskyDecomposition [
366366

367367
diagonalValue := ((self at: j at: j) - rowSum) sqrt.
368368

369+
369370
upperTriangular at: j at: j put: diagonalValue ]
370371
ifFalse: [
371-
partialSum := (1 to: j - 1) inject: 0 into: [ :sum :k |
372-
sum + (upperTriangular at: k at: i) * (upperTriangular at: k at: j) ].
373372

373+
partialSum := (1 to: j - 1) inject: 0 into: [ :sum :k |
374+
sum + ((upperTriangular at: k at: i) * (upperTriangular at: k at: j)).
375+
].
376+
374377
factor := upperTriangular at: j at: j.
375378
nonDiagonalValue := ((self at: j at: i) - partialSum) / factor.
376379

377-
upperTriangular at: j at: i put: nonDiagonalValue ] ] ].
380+
upperTriangular at: j at: i put: nonDiagonalValue.
381+
382+
] ] ].
378383

379384
^ upperTriangular
380385
]

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,20 @@ PMMatrixTest >> testCholeskyDecomposition [
7676
| matrix upperTriangular expected |
7777

7878
matrix := PMMatrix rows: #(
79-
(4 12 -16)
80-
(12 37 -43)
81-
(-16 -43 98)).
79+
(4 20 8 2 18)
80+
(20 116 80 18 126)
81+
(8 80 117 32 135)
82+
(2 18 32 73 109)
83+
(18 126 135 109 272)).
8284

8385
upperTriangular := matrix choleskyDecomposition.
8486

8587
expected := PMMatrix rows: #(
86-
(2 6 -8)
87-
(0 1 5)
88-
(0 0 3)).
88+
(2 10 4 1 9)
89+
(0 4 10 2 9)
90+
(0 0 1 8 9)
91+
(0 0 0 2 5)
92+
(0 0 0 0 2)).
8993

9094
self assert: upperTriangular equals: expected.
9195
]

0 commit comments

Comments
 (0)