-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worksheets, new methods and examples
- Loading branch information
Showing
3 changed files
with
296 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
include __DIR__ . "/../src/matrix.php"; | ||
|
||
use divengine\matrix; | ||
|
||
// simple list of nums | ||
$nums = new matrix([ | ||
["", 1, 2, 3], | ||
["", 4, 5, 6] | ||
]); | ||
|
||
// get item | ||
echo $nums->get(1, 3); // 6 | ||
|
||
// set item | ||
$nums->set(1, 3, 10); | ||
echo $nums->formatTXT(); | ||
echo PHP_EOL; | ||
|
||
// get row | ||
print_r($nums->getRow(1)); // [4, 5, 10] | ||
|
||
// get column | ||
print_r($nums->getColumn(3)); // [3, 10] | ||
|
||
// get range | ||
$range = $nums->range(0, 0, 1, 1); // [[1, 2], [4, 5]] | ||
|
||
// new matrix from range | ||
$rangeMatrix = new matrix($range); | ||
|
||
// show $range | ||
echo $rangeMatrix->formatTXT(); | ||
echo PHP_EOL; | ||
|
||
// add row | ||
$nums->addRow(["", 0, 0, 0]); | ||
echo $nums->formatTXT(); | ||
echo PHP_EOL; | ||
|
||
// add column | ||
$nums->addColumn(0); | ||
echo $nums->formatTXT(); | ||
echo PHP_EOL; | ||
|
||
// fill function | ||
$nums->fillVertical(0, 0, $nums->getTotalRows() - 1, fn() => date("Y-m-d")); | ||
echo $nums->formatTXT(); | ||
echo PHP_EOL; | ||
|
||
// add row on top | ||
$nums->addRow(["Date\t", "Value1", "Value2", "Value3", "Value4"], onTop: true); | ||
echo $nums->formatTXT(true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
include __DIR__ . "/../src/matrix.php"; | ||
|
||
use divengine\matrix; | ||
|
||
$F_AMOUNT = fn($row, $col, $matrix) => $matrix->get($row, $col - 2) * $matrix->get($row, $col - 1); | ||
$F_TOTAL = fn($row, $col, $matrix) => array_sum($matrix->vertical($col, 1, $row - 1)); | ||
$F_AVG = fn($row, $col, $matrix) => number_format($F_TOTAL($row, $col, $matrix) / ($row - 1), 1); | ||
$F_BOTTOM_RIGHT = fn(matrix $matrix) => $matrix->get($matrix->getTotalRows() - 1, $matrix->getTotalColumns() - 1); | ||
|
||
// sheet of products | ||
$products = new matrix([ | ||
/* 0 1 2 3 */ | ||
/* 0 */ ["Product", "Price", "Count", "Amount"], | ||
//---------------------------------------- | ||
/* 1 */ ["Apple", 5, 2, $F_AMOUNT], | ||
/* 2 */ ["Banana", 6, 3, $F_AMOUNT], | ||
/* 3 */ ["Orange", 6, 10, $F_AMOUNT], | ||
//----------------------------------------- | ||
/* 4 */ ["Totals", $F_AVG, $F_TOTAL, $F_TOTAL] | ||
]); | ||
|
||
// sheet of services | ||
$services = new matrix([ | ||
/* 0 1 2 3 */ | ||
/* 0 */ ["Service", "Price", "Count", "Amount"], | ||
//----------------------------------------- | ||
/* 1 */ ["Clean", 10, 2, $F_AMOUNT], | ||
/* 2 */ ["Paint", 35, 3, $F_AMOUNT], | ||
/* 3 */ ["Repair", 6, 10, $F_AMOUNT], | ||
//------------------------------------------ | ||
/* 4 */ ["Totals", $F_AVG, $F_TOTAL, $F_TOTAL] | ||
]); | ||
|
||
$T_PRODUCTS = fn() => $F_BOTTOM_RIGHT($products); | ||
$T_SERVICES = fn() => $F_BOTTOM_RIGHT($services); | ||
|
||
// sheet of earnings | ||
$earnings = new matrix([ | ||
["\t", "Amount"], | ||
//------------------------ | ||
["Products", $T_PRODUCTS], | ||
["Services", $T_SERVICES], | ||
//------------------------ | ||
["Earnings", $F_TOTAL] | ||
]); | ||
|
||
echo $products->formatTXT(true); | ||
echo "\n"; | ||
echo $services->formatTXT(true); | ||
echo "\n"; | ||
echo $earnings->formatTXT(true); | ||
echo "\n"; | ||
|
||
$products->set(1, 1, 10); // $earnings will be updated automatically | ||
|
||
echo $products->formatTXT(true); | ||
echo "\n"; | ||
echo $earnings->formatTXT(true); | ||
echo "\n"; |
Oops, something went wrong.