-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatrix_algorithms.php
206 lines (171 loc) · 4.04 KB
/
matrix_algorithms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
// TODO : norm, eig, etc.
/**
* Class MatrixAlgorithms
*/
abstract class MatrixAlgorithms
{
/**
* Return LUP-factorization P*A=L*U
*
* @see http://en.wikipedia.org/wiki/LUP_decomposition
* @see Introduction to Algorithms by Thomas H. Cormen and other
* @static
* @param Matrix $A
* @return array($C, $P, $singular, $even) - C = L + U - E, $singular === true for singular matrix, $even - for det
*/
public static function LUP($A)
{
if (!Matrix::isMatrix($A)) {
throw new InvalidArgumentException('Matrix needed. Given' . gettype($A));
}
if (!$A->isSquare()) {
throw new InvalidArgumentException("Matrix must be square");
}
$size = $A->getRowsCount();
$C = clone $A;
$P = array();
for ($i = 0; $i < $size; $i++) {
$P[$i] = $i;
}
$singular = false;
$even = true;
for ($i = 0; $i < $size; $i++) {
//поиск опорного элемента
$pivotValue = 0;
$pivot = -1;
for ($row = $i; $row < $size; $row++) {
if (abs($C->getElem($row, $i)) > $pivotValue) {
$pivotValue = abs($C->getElem($row, $i));
$pivot = $row;
}
}
if ($pivotValue == 0) {
$singular = true;
break;
}
//меняем местами i-ю строку и строку с опорным элементом
if ($pivot !== $i) {
list($P[$i], $P[$pivot]) = array($P[$pivot], $P[$i]);
$C->swapRows($pivot, $i);
$even = !$even;
}
for ($j = $i + 1; $j < $size; $j++) {
$temp = $C->getElem($j, $i) / $C->getElem($i, $i);
$C->setElem($j, $i, $temp);
for($k = $i + 1; $k < $size; $k++) {
$temp = $C->getElem($j, $k) - $C->getElem($j, $i) * $C->getElem($i, $k);
$C->setElem($j, $k, $temp);
}
}
}
return array($C, $P, $singular, $even);
}
/**
*
* @static
* @param Matrix $C
* @param Matrix $P
* @param Matrix $b
* @return Matrix
*/
public static function LUP_solve($C, $P, $b)
{
// TODO : Проверка входных
$n = $C->getRowsCount();
$y = array();
for ($i = 0; $i < $n; $i++) {
$sum = 0;
for ($j = 0; $j < $i; $j++) {
$sum += $C->getElem($i, $j) * $y[$j];
}
$y[$i] = $b->getElem($P[$i], 0) - $sum;
}
$x = array();
for ($i = $n - 1; $i >= 0; $i--) {
$sum = 0;
for ($j = $i + 1; $j < $n; $j++) {
$sum += $C->getElem($i, $j) * $x[$j];
}
$x[$i] = ($y[$i] - $sum) / $C->getElem($i, $i);
}
return MatrixFactory::fromArray($x)->T();
}
/**
* Return determinant of matrix
*
* @static
* @param Matrix $matrix
* @return number
*/
public static function determinant($matrix)
{
list($C, , $singular, $even) = self::LUP($matrix);
$e = $even ? 1 : -1;
return $singular ? 0 : $e * $C->prodTrace();
}
/**
* Return inverse matrix
*
* @static
* @param Matrix $matrix
* @return Matrix
* @throws LogicException
*/
public static function inverseMatrix($matrix)
{
list($C, $P, $singular) = self::LUP($matrix);
if ($singular) {
throw new LogicException("Matrix is singular. Can't find inverse");
}
$size = $matrix->getRowsCount();
$inverse = MatrixFactory::zeroMatrix($size);
for ($i = 0; $i < $size; $i++) {
$b = MatrixFactory::zeroMatrix($size, 1);
$b->setElem($i, 0, 1);
$value = self::LUP_solve($C, $P, $b);
$inverse->setColumn($i, $value);
}
return $inverse;
}
/**
* Return transposed matrix
*
* @static
* @param Matrix $matrix
* @return Matrix
*/
public static function transpose($matrix)
{
list($rows, $cols) = $matrix->getSize();
$T = MatrixFactory::zeroMatrix($cols, $rows);
for ($i = 0; $i < $rows; $i++) {
for ($j = 0; $j < $cols; $j++) {
$T->setElem($j, $i, $matrix->getElem($i, $j));
}
}
return $T;
}
/**
* Return P-norm of matrix
*
* @static
* @param Matrix $matrix
* @param number $p
* @return number
*/
public static function pNorm($matrix, $p)
{
$callback = function($sum, $elem) use ($p) {
return $sum + pow(abs($elem), $p);
};
$sum = $matrix->reduce($callback);
return pow($sum, 1 / $p);
}
public static function mNorm()
{
}
public static function lNorm()
{
}
}