PHP code example of lisachenko / native-php-matrix

1. Go to this page and download the library: Download lisachenko/native-php-matrix library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lisachenko / native-php-matrix example snippets



declare(strict_types=1);

use Lisachenko\NativePhpMatrix\Matrix;

);

$value = $first * 2 + $second; // Matrix([[22, 44, 66]])

$a = new Matrix([[1, 2, 3]]);
$b = new Matrix([[4], [5], [6]]);

$product = $a * $b;
var_dump($product->toArray()); // [[32]]  — a 1×3 times a 3×1 gives a 1×1

$m = new Matrix([[1, 2, 3]]);

var_dump(($m ** 2)->toArray()); // [[1, 4, 9]]   — element-wise exponentiation

$even = new Matrix([[2, 4, 6]]);
var_dump(($even / 2)->toArray()); // [[1, 2, 3]]

$a = new Matrix([[1, 2], [3, 4]]);
$b = new Matrix([[1, 2], [3, 4]]);
$c = new Matrix([[1, 2], [3, 5]]);

var_dump($a == $b); // bool(true)
var_dump($a == $c); // bool(false)
var_dump($a != $c); // bool(true)

$m = new Matrix([[1, 2], [3, 4]]);

var_dump((array) $m);  // [[1, 2], [3, 4]] — the rows, not the object's internals

echo (string) $m;      // [1, 2]
                       // [3, 4]

var_dump((bool) $m);   // bool(true) — a valid matrix is never empty by construction

$m = new Matrix([[1, 2], [3, 4]]);

$m->getRows();    // 2
$m->getColumns(); // 2
$m->isSquare();   // true
$m->toArray();    // [[1, 2], [3, 4]]
bash
composer