|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -use app\classes\mathematics\Matrix; |
| 3 | +use Apphp\MLKit\Math\Linear\Matrix; |
4 | 4 |
|
5 | 5 | $matrix1 = new Matrix([[1, 2], [3, 4]]);
|
6 | 6 | $matrix2 = new Matrix([[5, 6], [7, 8]]);
|
|
19 | 19 | echo "Cofactor Matrix of Matrix 1:\n" . $matrix1->cofactorMatrix()->toString() . "\n";
|
20 | 20 | echo "Adjugate Matrix of Matrix 1:\n" . $matrix1->adjugateMatrix()->toString() . "\n";
|
21 | 21 | echo "Inverse of Matrix 1:\n" . $matrix1->inverse()->toString() . "\n";
|
| 22 | + |
| 23 | +echo "\nIdentity Matrix (3x3):\n" . Matrix::identity(3)->toString() . "\n"; |
| 24 | +echo "Zero Matrix (2x3):\n" . Matrix::zero(2, 3)->toString() . "\n"; |
| 25 | +echo "Is Matrix 1 square? " . ($matrix1->isSquare() ? 'Yes' : 'No') . "\n"; |
| 26 | +echo "Is Matrix 1 symmetric? " . ($matrix1->isSymmetric() ? 'Yes' : 'No') . "\n"; |
| 27 | +echo "Trace of Matrix 1: " . $matrix1->trace() . "\n"; |
| 28 | +echo "First row of Matrix 1: [" . implode(', ', $matrix1->getRow(0)) . "]\n"; |
| 29 | +echo "Second column of Matrix 2: [" . implode(', ', $matrix2->getColumn(1)) . "]\n"; |
| 30 | +echo "Are Matrix 1 and Matrix 2 equal? " . ($matrix1->equals($matrix2) ? 'Yes' : 'No') . "\n"; |
| 31 | + |
| 32 | +// Example of fill and map |
| 33 | +$filled = new Matrix([[0, 0], [0, 0]]); |
| 34 | +$filled->fill(9); |
| 35 | +echo "Filled Matrix (all 9s):\n" . $filled->toString() . "\n"; |
| 36 | + |
| 37 | +$mapped = $matrix1->map(fn($v) => $v * 10); |
| 38 | +echo "Matrix 1 with each element multiplied by 10 (map):\n" . $mapped->toString() . "\n"; |
| 39 | + |
| 40 | +// --- Additional Examples --- |
| 41 | + |
| 42 | +// Matrix shape, size, and dimensions |
| 43 | +$matrix3 = new Matrix([[1,2,3],[4,5,6],[7,8,9]]); |
| 44 | +echo "Matrix 3:\n" . $matrix3->toString() . "\n"; |
| 45 | +echo 'Matrix 3: ' . $matrix3->getRows() . ' rows x ' . $matrix3->getCols() . " columns\n"; |
| 46 | +echo 'Matrix 3 is square? ' . ($matrix3->isSquare() ? 'Yes' : 'No') . "\n"; |
| 47 | +echo 'Matrix 3 size (total elements): ' . ($matrix3->getRows() * $matrix3->getCols()) . "\n"; |
| 48 | + |
| 49 | +// Accessing a single element (row 2, col 3) |
| 50 | +echo 'Element at row 2, column 3 of Matrix 3: ' . $matrix3->getRow(1)[2] . "\n"; |
| 51 | + |
| 52 | +// Absolute value (element-wise) |
| 53 | +$negMatrix = new Matrix([[-1, -2], [3, -4]]); |
| 54 | +$absMatrix = $negMatrix->map(fn($v) => abs($v)); |
| 55 | +echo "Absolute value of negMatrix:\n" . $absMatrix->toString() . "\n"; |
| 56 | + |
| 57 | +// Row and column sums |
| 58 | +$rowSums = array_map(fn($row) => array_sum($row), $matrix3->toArray()); |
| 59 | +echo 'Row sums of Matrix 3: [' . implode(', ', $rowSums) . "]\n"; |
| 60 | +$colSums = []; |
| 61 | +for ($j = 0; $j < $matrix3->getCols(); $j++) { |
| 62 | + $col = $matrix3->getColumn($j); |
| 63 | + $colSums[] = array_sum($col); |
| 64 | +} |
| 65 | +echo 'Column sums of Matrix 3: [' . implode(', ', $colSums) . "]\n"; |
| 66 | + |
| 67 | +// Minimum, maximum, mean |
| 68 | +$all = array_merge(...$matrix3->toArray()); |
| 69 | +echo 'Minimum value in Matrix 3: ' . min($all) . "\n"; |
| 70 | +echo 'Maximum value in Matrix 3: ' . max($all) . "\n"; |
| 71 | +echo 'Mean value in Matrix 3: ' . (array_sum($all)/count($all)) . "\n"; |
0 commit comments