Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
rafageist committed Jan 17, 2024
1 parent 08a2327 commit b658f04
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 100 deletions.
104 changes: 4 additions & 100 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,106 +26,10 @@ This package is designed to offer flexibility in matrix manipulation, enabling d
composer require divengine/matrix
```

## Basic usage
## Documentation

```php
<?php
https://divengine.org/docs/div-php-matrix

// Create a matrix
$table = new matrix([
["Product", "Price", "Count"],
["Apple", 10, 2],
["Banana", 35, 3],
["Orange", 6, 10],
]);

// Show the matrix
echo $table->format('txt', true);

$table->addColumn();
$table->set(0, 3, "Amount");

// Fill the column with the product of the previous two columns
$table->fillVertical(3, 1, 3,
fn ($r, $c, matrix $m)
=> $m->get($r, $c - 1) * $m->get($r, $c - 2));

// Add a row with the sum of the previous rows
$table->addRow(["Total", "", "",
fn ($r, $c, matrix $m)
=> array_sum($m->vertical($c, 1, $r - 1))]);
echo $table->format('txt', true);

// Add a column with the sum of the previous columns
$table->addColumn();
$table->set(0, 4, "Cumul");

// Fill the column with the sum of the previous column
$table->fillVertical(4, 1, 3,
fn ($r, $c, matrix $m)
=> $r == 1 ? $m->get($r, $c - 1)
: $m->get($r - 1, $c) + $m->get($r, $c - 1));

echo $table->format('txt', true);

// Change a value of the second row
$table->set(1, 1, 20);
echo $table->format('txt', true);

// Remove the second row
$table->removeRow(1);
echo $table->format('txt', true);

// Show ranges
print_r($table->vertical(1, 1, 2));
print_r($table->horizontal(1, 1, 2));

// Serialize
echo $table->format('serialize');
```

Ouput:

```shell
Product Price Count
Apple 10 2
Banana 35 3
Orange 6 10

Product Price Count Amount
Apple 10 2 20
Banana 35 3 105
Orange 6 10 60
Total 185

Product Price Count Amount Cumul
Apple 10 2 20 20
Banana 35 3 105 125
Orange 6 10 60 185
Total 185

Product Price Count Amount Cumul
Apple 20 2 40 40
Banana 35 3 105 145
Orange 6 10 60 205
Total 205

Product Price Count Amount Cumul
Banana 35 3 105 105
Orange 6 10 60 165
Total 165

Array
(
[0] => 35
[1] => 6
)

Array
(
[0] => 35
[1] => 3
)

```
## Examples

See "examples" folder.
57 changes: 57 additions & 0 deletions examples/calculations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

include __DIR__ . "/../src/matrix.php";

use divengine\matrix;

// Create a matrix
$table = new matrix([
["Product", "Price", "Count"],
["Apple", 10, 2],
["Banana", 35, 3],
["Orange", 6, 10],
]);

// Show the matrix
echo $table->format('txt', true);

$table->addColumn();
$table->set(0, 3, "Amount");

// Fill the column with the product of the previous two columns
$table->fillVertical(3, 1, 3,
fn ($r, $c, matrix $m)
=> $m->get($r, $c - 1) * $m->get($r, $c - 2));

// Add a row with the sum of the previous rows
$table->addRow(["Total", "", "",
fn ($r, $c, matrix $m)
=> array_sum($m->vertical($c, 1, $r - 1))]);
echo $table->format('txt', true);

// Add a column with the sum of the previous columns
$table->addColumn();
$table->set(0, 4, "Cumul");

// Fill the column with the sum of the previous column
$table->fillVertical(4, 1, 3,
fn ($r, $c, matrix $m)
=> $r == 1 ? $m->get($r, $c - 1)
: $m->get($r - 1, $c) + $m->get($r, $c - 1));

echo $table->format('txt', true);

// Change a value of the second row
$table->set(1, 1, 20);
echo $table->format('txt', true);

// Remove the second row
$table->removeRow(1);
echo $table->format('txt', true);

// Show ranges
print_r($table->vertical(1, 1, 2));
print_r($table->horizontal(1, 1, 2));

// Serialize
echo $table->format('serialize');

0 comments on commit b658f04

Please sign in to comment.