PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.2.0
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.2.0
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
visualizer / vendor / markbaker / matrix / classes / src / Functions.php

Functions.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.2.0, at vendor/markbaker/matrix/classes/src/Functions.php

319 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Matrix;
4
5 class Functions
6 {
7 /**
8 * Calculate the adjoint of the matrix
9 *
10 * @param Matrix $matrix The matrix whose adjoint we wish to calculate
11 * @return Matrix
12 **/
13 private static function getAdjoint(Matrix $matrix)
14 {
15 return self::transpose(
16 self::getCofactors($matrix)
17 );
18 }
19
20 /**
21 * Return the adjoint of this matrix
22 * The adjugate, classical adjoint, or adjunct of a square matrix is the transpose of its cofactor matrix.
23 * The adjugate has sometimes been called the "adjoint", but today the "adjoint" of a matrix normally refers
24 * to its corresponding adjoint operator, which is its conjugate transpose.
25 *
26 * @param Matrix $matrix The matrix whose adjoint we wish to calculate
27 * @return Matrix
28 * @throws Exception
29 **/
30 public static function adjoint(Matrix $matrix)
31 {
32 if (!$matrix->isSquare()) {
33 throw new Exception('Adjoint can only be calculated for a square matrix');
34 }
35
36 return self::getAdjoint($matrix);
37 }
38
39 /**
40 * Calculate the cofactors of the matrix
41 *
42 * @param Matrix $matrix The matrix whose cofactors we wish to calculate
43 * @return Matrix
44 **/
45 private static function getCofactors(Matrix $matrix)
46 {
47 $cofactors = self::getMinors($matrix);
48 $dimensions = $matrix->rows;
49
50 $cof = 1;
51 for ($i = 0; $i < $dimensions; ++$i) {
52 $cofs = $cof;
53 for ($j = 0; $j < $dimensions; ++$j) {
54 $cofactors[$i][$j] *= $cofs;
55 $cofs = -$cofs;
56 }
57 $cof = -$cof;
58 }
59
60 return new Matrix($cofactors);
61 }
62
63 /**
64 * Return the cofactors of this matrix
65 *
66 * @param Matrix $matrix The matrix whose cofactors we wish to calculate
67 * @return Matrix
68 * @throws Exception
69 **/
70 public static function cofactors(Matrix $matrix)
71 {
72 if (!$matrix->isSquare()) {
73 throw new Exception('Cofactors can only be calculated for a square matrix');
74 }
75
76 return self::getCofactors($matrix);
77 }
78
79 private static function getDeterminantSegment(Matrix $matrix, $row, $column)
80 {
81 $tmpMatrix = $matrix->toArray();
82 unset($tmpMatrix[$row]);
83 array_walk(
84 $tmpMatrix,
85 function (&$row) use ($column) {
86 unset($row[$column]);
87 }
88 );
89
90 return self::getDeterminant(new Matrix($tmpMatrix));
91 }
92
93 /**
94 * Calculate the determinant of the matrix
95 *
96 * @param Matrix $matrix The matrix whose determinant we wish to calculate
97 * @return float
98 **/
99 private static function getDeterminant(Matrix $matrix)
100 {
101 $dimensions = $matrix->rows;
102 if ($dimensions == 1) {
103 return $matrix->getValue(1, 1);
104 } elseif ($dimensions == 2) {
105 return $matrix->getValue(1, 1) * $matrix->getValue(2, 2) - $matrix->getValue(1, 2) * $matrix->getValue(2, 1);
106 }
107
108 $determinant = 0;
109 for ($i = 1; $i <= $dimensions; ++$i) {
110 $det = $matrix->getValue(1, $i) * self::getDeterminantSegment($matrix, 0, $i-1);
111 if (($i % 2) == 0) {
112 $determinant -= $det;
113 } else {
114 $determinant += $det;
115 }
116 }
117
118 return $determinant;
119 }
120
121 /**
122 * Return the determinant of this matrix
123 *
124 * @param Matrix $matrix The matrix whose determinant we wish to calculate
125 * @return float
126 * @throws Exception
127 **/
128 public static function determinant(Matrix $matrix)
129 {
130 if (!$matrix->isSquare()) {
131 throw new Exception('Determinant can only be calculated for a square matrix');
132 }
133
134 return self::getDeterminant($matrix);
135 }
136
137 /**
138 * Return the diagonal of this matrix
139 *
140 * @param Matrix $matrix The matrix whose diagonal we wish to calculate
141 * @return Matrix
142 * @throws Exception
143 **/
144 public static function diagonal(Matrix $matrix)
145 {
146 if (!$matrix->isSquare()) {
147 throw new Exception('Diagonal can only be extracted from a square matrix');
148 }
149
150 $dimensions = $matrix->rows;
151 $grid = Builder::createFilledMatrix(0, $dimensions, $dimensions)
152 ->toArray();
153
154 for ($i = 0; $i < $dimensions; ++$i) {
155 $grid[$i][$i] = $matrix->getValue($i + 1, $i + 1);
156 }
157
158 return new Matrix($grid);
159 }
160
161 /**
162 * Return the antidiagonal of this matrix
163 *
164 * @param Matrix $matrix The matrix whose antidiagonal we wish to calculate
165 * @return Matrix
166 * @throws Exception
167 **/
168 public static function antidiagonal(Matrix $matrix)
169 {
170 if (!$matrix->isSquare()) {
171 throw new Exception('Anti-Diagonal can only be extracted from a square matrix');
172 }
173
174 $dimensions = $matrix->rows;
175 $grid = Builder::createFilledMatrix(0, $dimensions, $dimensions)
176 ->toArray();
177
178 for ($i = 0; $i < $dimensions; ++$i) {
179 $grid[$i][$dimensions - $i - 1] = $matrix->getValue($i + 1, $dimensions - $i);
180 }
181
182 return new Matrix($grid);
183 }
184
185 /**
186 * Return the identity matrix
187 * The identity matrix, or sometimes ambiguously called a unit matrix, of size n is the n × n square matrix
188 * with ones on the main diagonal and zeros elsewhere
189 *
190 * @param Matrix $matrix The matrix whose identity we wish to calculate
191 * @return Matrix
192 * @throws Exception
193 **/
194 public static function identity(Matrix $matrix)
195 {
196 if (!$matrix->isSquare()) {
197 throw new Exception('Identity can only be created for a square matrix');
198 }
199
200 $dimensions = $matrix->rows;
201
202 return Builder::createIdentityMatrix($dimensions);
203 }
204
205 /**
206 * Return the inverse of this matrix
207 *
208 * @param Matrix $matrix The matrix whose inverse we wish to calculate
209 * @return Matrix
210 * @throws Exception
211 **/
212 public static function inverse(Matrix $matrix)
213 {
214 if (!$matrix->isSquare()) {
215 throw new Exception('Inverse can only be calculated for a square matrix');
216 }
217
218 $determinant = self::getDeterminant($matrix);
219 if ($determinant == 0.0) {
220 throw new Exception('Inverse can only be calculated for a matrix with a non-zero determinant');
221 }
222
223 if ($matrix->rows == 1) {
224 return new Matrix([[1 / $matrix->getValue(1, 1)]]);
225 }
226
227 return self::getAdjoint($matrix)
228 ->multiply(1 / $determinant);
229 }
230
231 /**
232 * Calculate the minors of the matrix
233 *
234 * @param Matrix $matrix The matrix whose minors we wish to calculate
235 * @return array[]
236 **/
237 protected static function getMinors(Matrix $matrix)
238 {
239 $minors = $matrix->toArray();
240 $dimensions = $matrix->rows;
241 if ($dimensions == 1) {
242 return $minors;
243 }
244
245 for ($i = 0; $i < $dimensions; ++$i) {
246 for ($j = 0; $j < $dimensions; ++$j) {
247 $minors[$i][$j] = self::getDeterminantSegment($matrix, $i, $j);
248 }
249 }
250
251 return $minors;
252 }
253
254 /**
255 * Return the minors of the matrix
256 * The minor of a matrix A is the determinant of some smaller square matrix, cut down from A by removing one or
257 * more of its rows or columns.
258 * Minors obtained by removing just one row and one column from square matrices (first minors) are required for
259 * calculating matrix cofactors, which in turn are useful for computing both the determinant and inverse of
260 * square matrices.
261 *
262 * @param Matrix $matrix The matrix whose minors we wish to calculate
263 * @return Matrix
264 * @throws Exception
265 **/
266 public static function minors(Matrix $matrix)
267 {
268 if (!$matrix->isSquare()) {
269 throw new Exception('Minors can only be calculated for a square matrix');
270 }
271
272 return new Matrix(self::getMinors($matrix));
273 }
274
275 /**
276 * Return the trace of this matrix
277 * The trace is defined as the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right)
278 * of the matrix
279 *
280 * @param Matrix $matrix The matrix whose trace we wish to calculate
281 * @return float
282 * @throws Exception
283 **/
284 public static function trace(Matrix $matrix)
285 {
286 if (!$matrix->isSquare()) {
287 throw new Exception('Trace can only be extracted from a square matrix');
288 }
289
290 $dimensions = $matrix->rows;
291 $result = 0;
292 for ($i = 1; $i <= $dimensions; ++$i) {
293 $result += $matrix->getValue($i, $i);
294 }
295
296 return $result;
297 }
298
299 /**
300 * Return the transpose of this matrix
301 *
302 * @param Matrix $matrix The matrix whose transpose we wish to calculate
303 * @return Matrix
304 * @throws Exception
305 **/
306 public static function transpose(Matrix $matrix)
307 {
308 $grid = call_user_func_array(
309 'array_map',
310 array_merge(
311 [null],
312 $matrix->toArray()
313 )
314 );
315
316 return new Matrix($grid);
317 }
318 }
319