ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Matrix\Decomposition\LU Class Reference
+ Collaboration diagram for Matrix\Decomposition\LU:

Public Member Functions

 __construct (Matrix $matrix)
 
 getL ()
 Get lower triangular factor. More...
 
 getU ()
 Get upper triangular factor. More...
 
 getP ()
 Return pivot permutation vector. More...
 
 getPivot ()
 Return pivot permutation vector. More...
 
 isNonsingular ()
 Is the matrix nonsingular? More...
 
 solve (Matrix $B)
 Solve A*X = B. More...
 

Private Member Functions

 buildPivot ()
 
 localisedReferenceColumn ($column)
 
 applyTransformations ($column, array $luColumn)
 
 findPivot ($column, array $luColumn)
 
 pivotExchange ($pivot, $column)
 
 computeMultipliers ($diagonal)
 
 pivotB (Matrix $B)
 

Private Attributes

 $luMatrix
 
 $rows
 
 $columns
 
 $pivot = []
 

Detailed Description

Definition at line 8 of file LU.php.

Constructor & Destructor Documentation

◆ __construct()

Matrix\Decomposition\LU::__construct ( Matrix  $matrix)

Definition at line 16 of file LU.php.

17 {
18 $this->luMatrix = $matrix->toArray();
19 $this->rows = $matrix->rows;
20 $this->columns = $matrix->columns;
21
22 $this->buildPivot();
23 }
$matrix
Definition: test.php:18

References $matrix, and Matrix\Decomposition\LU\buildPivot().

+ Here is the call graph for this function:

Member Function Documentation

◆ applyTransformations()

Matrix\Decomposition\LU::applyTransformations (   $column,
array  $luColumn 
)
private

Definition at line 151 of file LU.php.

151 : void
152 {
153 for ($row = 0; $row < $this->rows; ++$row) {
154 $luRow = $this->luMatrix[$row];
155 // Most of the time is spent in the following dot product.
156 $kmax = min($row, $column);
157 $sValue = 0.0;
158 for ($kValue = 0; $kValue < $kmax; ++$kValue) {
159 $sValue += $luRow[$kValue] * $luColumn[$kValue];
160 }
161 $luRow[$column] = $luColumn[$row] -= $sValue;
162 }
163 }
$row

References $row, and Matrix\Decomposition\LU\$rows.

Referenced by Matrix\Decomposition\LU\buildPivot().

+ Here is the caller graph for this function:

◆ buildPivot()

Matrix\Decomposition\LU::buildPivot ( )
private

Definition at line 118 of file LU.php.

118 : void
119 {
120 for ($row = 0; $row < $this->rows; ++$row) {
121 $this->pivot[$row] = $row;
122 }
123
124 for ($column = 0; $column < $this->columns; ++$column) {
125 $luColumn = $this->localisedReferenceColumn($column);
126
127 $this->applyTransformations($column, $luColumn);
128
129 $pivot = $this->findPivot($column, $luColumn);
130 if ($pivot !== $column) {
131 $this->pivotExchange($pivot, $column);
132 }
133
134 $this->computeMultipliers($column);
135
136 unset($luColumn);
137 }
138 }
applyTransformations($column, array $luColumn)
Definition: LU.php:151
localisedReferenceColumn($column)
Definition: LU.php:140
computeMultipliers($diagonal)
Definition: LU.php:190
pivotExchange($pivot, $column)
Definition: LU.php:177
findPivot($column, array $luColumn)
Definition: LU.php:165

References Matrix\Decomposition\LU\$columns, Matrix\Decomposition\LU\$pivot, $row, Matrix\Decomposition\LU\$rows, Matrix\Decomposition\LU\applyTransformations(), Matrix\Decomposition\LU\computeMultipliers(), Matrix\Decomposition\LU\findPivot(), Matrix\Decomposition\LU\localisedReferenceColumn(), and Matrix\Decomposition\LU\pivotExchange().

Referenced by Matrix\Decomposition\LU\__construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ computeMultipliers()

Matrix\Decomposition\LU::computeMultipliers (   $diagonal)
private

Definition at line 190 of file LU.php.

190 : void
191 {
192 if (($diagonal < $this->rows) && ($this->luMatrix[$diagonal][$diagonal] != 0.0)) {
193 for ($row = $diagonal + 1; $row < $this->rows; ++$row) {
194 $this->luMatrix[$row][$diagonal] /= $this->luMatrix[$diagonal][$diagonal];
195 }
196 }
197 }

References $row, and Matrix\Decomposition\LU\$rows.

Referenced by Matrix\Decomposition\LU\buildPivot().

+ Here is the caller graph for this function:

◆ findPivot()

Matrix\Decomposition\LU::findPivot (   $column,
array  $luColumn 
)
private

Definition at line 165 of file LU.php.

165 : int
166 {
167 $pivot = $column;
168 for ($row = $column + 1; $row < $this->rows; ++$row) {
169 if (abs($luColumn[$row]) > abs($luColumn[$pivot])) {
170 $pivot = $row;
171 }
172 }
173
174 return $pivot;
175 }

References Matrix\Decomposition\LU\$pivot, $row, and Matrix\Decomposition\LU\$rows.

Referenced by Matrix\Decomposition\LU\buildPivot().

+ Here is the caller graph for this function:

◆ getL()

Matrix\Decomposition\LU::getL ( )

Get lower triangular factor.

Returns
Matrix Lower triangular factor

Definition at line 30 of file LU.php.

30 : Matrix
31 {
32 $lower = [];
33
34 $columns = min($this->rows, $this->columns);
35 for ($row = 0; $row < $this->rows; ++$row) {
36 for ($column = 0; $column < $columns; ++$column) {
37 if ($row > $column) {
38 $lower[$row][$column] = $this->luMatrix[$row][$column];
39 } elseif ($row === $column) {
40 $lower[$row][$column] = 1.0;
41 } else {
42 $lower[$row][$column] = 0.0;
43 }
44 }
45 }
46
47 return new Matrix($lower);
48 }
Class for the creating "special" Matrices.
Definition: Builder.php:11

References Matrix\Decomposition\LU\$columns, $row, and Matrix\Decomposition\LU\$rows.

◆ getP()

Matrix\Decomposition\LU::getP ( )

Return pivot permutation vector.

Returns
Matrix Pivot matrix

Definition at line 78 of file LU.php.

78 : Matrix
79 {
80 $pMatrix = [];
81
82 $pivots = $this->pivot;
83 $pivotCount = count($pivots);
84 foreach ($pivots as $row => $pivot) {
85 $pMatrix[$row] = array_fill(0, $pivotCount, 0);
86 $pMatrix[$row][$pivot] = 1;
87 }
88
89 return new Matrix($pMatrix);
90 }

References Matrix\Decomposition\LU\$pivot, and $row.

◆ getPivot()

Matrix\Decomposition\LU::getPivot ( )

Return pivot permutation vector.

Returns
array Pivot vector

Definition at line 97 of file LU.php.

97 : array
98 {
99 return $this->pivot;
100 }

References Matrix\Decomposition\LU\$pivot.

◆ getU()

Matrix\Decomposition\LU::getU ( )

Get upper triangular factor.

Returns
Matrix Upper triangular factor

Definition at line 55 of file LU.php.

55 : Matrix
56 {
57 $upper = [];
58
59 $rows = min($this->rows, $this->columns);
60 for ($row = 0; $row < $rows; ++$row) {
61 for ($column = 0; $column < $this->columns; ++$column) {
62 if ($row <= $column) {
63 $upper[$row][$column] = $this->luMatrix[$row][$column];
64 } else {
65 $upper[$row][$column] = 0.0;
66 }
67 }
68 }
69
70 return new Matrix($upper);
71 }

References Matrix\Decomposition\LU\$columns, $row, and Matrix\Decomposition\LU\$rows.

◆ isNonsingular()

Matrix\Decomposition\LU::isNonsingular ( )

Is the matrix nonsingular?

Returns
bool true if U, and hence A, is nonsingular

Definition at line 107 of file LU.php.

107 : bool
108 {
109 for ($diagonal = 0; $diagonal < $this->columns; ++$diagonal) {
110 if ($this->luMatrix[$diagonal][$diagonal] === 0.0) {
111 return false;
112 }
113 }
114
115 return true;
116 }

References Matrix\Decomposition\LU\$columns.

Referenced by Matrix\Decomposition\LU\solve().

+ Here is the caller graph for this function:

◆ localisedReferenceColumn()

Matrix\Decomposition\LU::localisedReferenceColumn (   $column)
private

Definition at line 140 of file LU.php.

140 : array
141 {
142 $luColumn = [];
143
144 for ($row = 0; $row < $this->rows; ++$row) {
145 $luColumn[$row] = &$this->luMatrix[$row][$column];
146 }
147
148 return $luColumn;
149 }

References $row, and Matrix\Decomposition\LU\$rows.

Referenced by Matrix\Decomposition\LU\buildPivot().

+ Here is the caller graph for this function:

◆ pivotB()

Matrix\Decomposition\LU::pivotB ( Matrix  $B)
private

Definition at line 199 of file LU.php.

199 : array
200 {
201 $X = [];
202 foreach ($this->pivot as $rowId) {
203 $row = $B->getRows($rowId + 1)->toArray();
204 $X[] = array_pop($row);
205 }
206
207 return $X;
208 }
getRows(int $row, int $rowCount=1)
Return a new matrix as a subset of rows from this matrix, starting at row number $row,...
Definition: Matrix.php:165
$X
Definition: test.php:23

References $row, $X, and Matrix\Matrix\getRows().

Referenced by Matrix\Decomposition\LU\solve().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pivotExchange()

Matrix\Decomposition\LU::pivotExchange (   $pivot,
  $column 
)
private

Definition at line 177 of file LU.php.

177 : void
178 {
179 for ($kValue = 0; $kValue < $this->columns; ++$kValue) {
180 $tValue = $this->luMatrix[$pivot][$kValue];
181 $this->luMatrix[$pivot][$kValue] = $this->luMatrix[$column][$kValue];
182 $this->luMatrix[$column][$kValue] = $tValue;
183 }
184
185 $lValue = $this->pivot[$pivot];
186 $this->pivot[$pivot] = $this->pivot[$column];
187 $this->pivot[$column] = $lValue;
188 }

References Matrix\Decomposition\LU\$columns, and Matrix\Decomposition\LU\$pivot.

Referenced by Matrix\Decomposition\LU\buildPivot().

+ Here is the caller graph for this function:

◆ solve()

Matrix\Decomposition\LU::solve ( Matrix  $B)

Solve A*X = B.

Parameters
Matrix$Ba Matrix with as many rows as A and any number of columns
Exceptions
Exception
Returns
Matrix X so that L*U*X = B(piv,:)

Definition at line 219 of file LU.php.

219 : Matrix
220 {
221 if ($B->rows !== $this->rows) {
222 throw new Exception('Matrix row dimensions are not equal');
223 }
224
225 if ($this->rows !== $this->columns) {
226 throw new Exception('LU solve() only works on square matrices');
227 }
228
229 if (!$this->isNonsingular()) {
230 throw new Exception('Can only perform operation on singular matrix');
231 }
232
233 // Copy right hand side with pivoting
234 $nx = $B->columns;
235 $X = $this->pivotB($B);
236
237 // Solve L*Y = B(piv,:)
238 for ($k = 0; $k < $this->columns; ++$k) {
239 for ($i = $k + 1; $i < $this->columns; ++$i) {
240 for ($j = 0; $j < $nx; ++$j) {
241 $X[$i][$j] -= $X[$k][$j] * $this->luMatrix[$i][$k];
242 }
243 }
244 }
245
246 // Solve U*X = Y;
247 for ($k = $this->columns - 1; $k >= 0; --$k) {
248 for ($j = 0; $j < $nx; ++$j) {
249 $X[$k][$j] /= $this->luMatrix[$k][$k];
250 }
251 for ($i = 0; $i < $k; ++$i) {
252 for ($j = 0; $j < $nx; ++$j) {
253 $X[$i][$j] -= $X[$k][$j] * $this->luMatrix[$i][$k];
254 }
255 }
256 }
257
258 return new Matrix($X);
259 }
pivotB(Matrix $B)
Definition: LU.php:199
isNonsingular()
Is the matrix nonsingular?
Definition: LU.php:107
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
Definition: Matrix.php:282
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
Definition: Matrix.php:297
$i
Definition: disco.tpl.php:19

References Matrix\Decomposition\LU\$columns, $i, $X, Matrix\Matrix\columns(), Matrix\Decomposition\LU\isNonsingular(), Matrix\Decomposition\LU\pivotB(), and Matrix\Matrix\rows().

+ Here is the call graph for this function:

Field Documentation

◆ $columns

◆ $luMatrix

Matrix\Decomposition\LU::$luMatrix
private

Definition at line 10 of file LU.php.

◆ $pivot

◆ $rows


The documentation for this class was generated from the following file: