ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
CholeskyDecomposition.php
Go to the documentation of this file.
1<?php
2
4
5use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
6
23{
29 private $L = [];
30
36 private $m;
37
43 private $isspd = true;
44
52 public function __construct(Matrix $A)
53 {
54 $this->L = $A->getArray();
55 $this->m = $A->getRowDimension();
56
57 for ($i = 0; $i < $this->m; ++$i) {
58 for ($j = $i; $j < $this->m; ++$j) {
59 for ($sum = $this->L[$i][$j], $k = $i - 1; $k >= 0; --$k) {
60 $sum -= $this->L[$i][$k] * $this->L[$j][$k];
61 }
62 if ($i == $j) {
63 if ($sum >= 0) {
64 $this->L[$i][$i] = sqrt($sum);
65 } else {
66 $this->isspd = false;
67 }
68 } else {
69 if ($this->L[$i][$i] != 0) {
70 $this->L[$j][$i] = $sum / $this->L[$i][$i];
71 }
72 }
73 }
74
75 for ($k = $i + 1; $k < $this->m; ++$k) {
76 $this->L[$i][$k] = 0.0;
77 }
78 }
79 }
80
86 public function isSPD()
87 {
88 return $this->isspd;
89 }
90
98 public function getL()
99 {
100 return new Matrix($this->L);
101 }
102
110 public function solve(Matrix $B)
111 {
112 if ($B->getRowDimension() == $this->m) {
113 if ($this->isspd) {
114 $X = $B->getArray();
115 $nx = $B->getColumnDimension();
116
117 for ($k = 0; $k < $this->m; ++$k) {
118 for ($i = $k + 1; $i < $this->m; ++$i) {
119 for ($j = 0; $j < $nx; ++$j) {
120 $X[$i][$j] -= $X[$k][$j] * $this->L[$i][$k];
121 }
122 }
123 for ($j = 0; $j < $nx; ++$j) {
124 $X[$k][$j] /= $this->L[$k][$k];
125 }
126 }
127
128 for ($k = $this->m - 1; $k >= 0; --$k) {
129 for ($j = 0; $j < $nx; ++$j) {
130 $X[$k][$j] /= $this->L[$k][$k];
131 }
132 for ($i = 0; $i < $k; ++$i) {
133 for ($j = 0; $j < $nx; ++$j) {
134 $X[$i][$j] -= $X[$k][$j] * $this->L[$k][$i];
135 }
136 }
137 }
138
139 return new Matrix($X, $this->m, $nx);
140 }
141
142 throw new CalculationException(Matrix::MATRIX_SPD_EXCEPTION);
143 }
144
145 throw new CalculationException(Matrix::MATRIX_DIMENSION_EXCEPTION);
146 }
147}
An exception for terminatinating execution or to throw for unit testing.
isSPD()
Is the matrix symmetric and positive definite?
getColumnDimension()
getColumnDimension.
Definition: Matrix.php:137
$i
Definition: disco.tpl.php:19
$X
Definition: test.php:23
Class for the creating "special" Matrices.
Definition: Builder.php:11