ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
CholeskyDecomposition.php
Go to the documentation of this file.
1<?php
19
25 private $L = array();
26
32 private $m;
33
39 private $isspd = true;
40
41
48 public function __construct($A = null) {
49 if ($A instanceof Matrix) {
50 $this->L = $A->getArray();
51 $this->m = $A->getRowDimension();
52
53 for($i = 0; $i < $this->m; ++$i) {
54 for($j = $i; $j < $this->m; ++$j) {
55 for($sum = $this->L[$i][$j], $k = $i - 1; $k >= 0; --$k) {
56 $sum -= $this->L[$i][$k] * $this->L[$j][$k];
57 }
58 if ($i == $j) {
59 if ($sum >= 0) {
60 $this->L[$i][$i] = sqrt($sum);
61 } else {
62 $this->isspd = false;
63 }
64 } else {
65 if ($this->L[$i][$i] != 0) {
66 $this->L[$j][$i] = $sum / $this->L[$i][$i];
67 }
68 }
69 }
70
71 for ($k = $i+1; $k < $this->m; ++$k) {
72 $this->L[$i][$k] = 0.0;
73 }
74 }
75 } else {
77 }
78 } // function __construct()
79
80
86 public function isSPD() {
87 return $this->isspd;
88 } // function isSPD()
89
90
97 public function getL() {
98 return new Matrix($this->L);
99 } // function getL()
100
101
108 public function solve($B = null) {
109 if ($B instanceof Matrix) {
110 if ($B->getRowDimension() == $this->m) {
111 if ($this->isspd) {
112 $X = $B->getArrayCopy();
113 $nx = $B->getColumnDimension();
114
115 for ($k = 0; $k < $this->m; ++$k) {
116 for ($i = $k + 1; $i < $this->m; ++$i) {
117 for ($j = 0; $j < $nx; ++$j) {
118 $X[$i][$j] -= $X[$k][$j] * $this->L[$i][$k];
119 }
120 }
121 for ($j = 0; $j < $nx; ++$j) {
122 $X[$k][$j] /= $this->L[$k][$k];
123 }
124 }
125
126 for ($k = $this->m - 1; $k >= 0; --$k) {
127 for ($j = 0; $j < $nx; ++$j) {
128 $X[$k][$j] /= $this->L[$k][$k];
129 }
130 for ($i = 0; $i < $k; ++$i) {
131 for ($j = 0; $j < $nx; ++$j) {
132 $X[$i][$j] -= $X[$k][$j] * $this->L[$k][$i];
133 }
134 }
135 }
136
137 return new Matrix($X, $this->m, $nx);
138 } else {
140 }
141 } else {
143 }
144 } else {
146 }
147 } // function solve()
148
149} // class CholeskyDecomposition
const MatrixDimensionException
Definition: Error.php:39
const MatrixSPDException
Definition: Error.php:49
const ArgumentTypeException
Definition: Error.php:29
JAMAError($errorNumber=null)
Custom error handler.
Definition: Error.php:70
An exception for terminatinating execution or to throw for unit testing.
isSPD()
Is the matrix symmetric and positive definite?
__construct($A=null)
CholeskyDecomposition.
solve($B=null)
Solve A*X = B.