Skip to content

Commit 135bdb3

Browse files
committed
Refactoring Matrix class
1 parent 66cf945 commit 135bdb3

File tree

5 files changed

+908
-228
lines changed

5 files changed

+908
-228
lines changed

classes/mathematics/Matrix.php

Lines changed: 0 additions & 226 deletions
This file was deleted.

public/pages/mathematics-for-ml/matrices/php-matrix-operations-usage.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use app\classes\mathematics\Matrix;
3+
use Apphp\MLKit\Math\Linear\Matrix;
44

55
$matrix1 = new Matrix([[1, 2], [3, 4]]);
66
$matrix2 = new Matrix([[5, 6], [7, 8]]);
@@ -19,3 +19,53 @@
1919
echo "Cofactor Matrix of Matrix 1:\n" . $matrix1->cofactorMatrix()->toString() . "\n";
2020
echo "Adjugate Matrix of Matrix 1:\n" . $matrix1->adjugateMatrix()->toString() . "\n";
2121
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";

public/pages/mathematics-for-ml/matrices/php-matrix-operations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
</div>
1515

1616
<div>
17-
<?= create_example_of_use_links(APP_PATH . 'classes/mathematics/Matrix.php', title: 'Example of class <code>Matrix</code>', opened: true); ?>
17+
<?= create_example_of_use_links(APP_PATH . 'src/Math/Linear/Matrix.php', title: 'Example of class <code>Matrix</code>', opened: true); ?>
1818
</div>

0 commit comments

Comments
 (0)