PHP code example of trivialmatters / square-matrix

1. Go to this page and download the library: Download trivialmatters/square-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/ */

    

trivialmatters / square-matrix example snippets




 = <<<EOF
3
11 2 4
4 5 6
10 8 -12
EOF;

// or create your own data provider
$data = new Gkirtsou\DataProvider\StringDataProvider($data);
$data->calculateRowsAndColumns();

/**
 * [
 *   [11, 4, 10]
 *   [2, 5, 8]
 *   [4, 6, -12]
 * ]
 */
$data->getColumns();

/**
 * [
 *   [11, 2, 4]
 *   [4, 5, 6]
 *   [10, 8, -12]
 * ]
 */
$data->getRows();

$squareMatrix = new Gkirtsou\SquareMatrix($data);

// get numbers from first column (don't forget PHP starts counting from zero!)
$squareMatrix->getNumbersByColumn(0);

// get numbers from first row (don't forget PHP starts counting from zero!)
$squareMatrix->getNumbersByRow(0);

// diagonal difference
$diagonalDifference = new \Gkirtsou\Diagonal\DiagonalDifference($squareMatrix);

// find the diagonal difference
$diagonalDifference->findDiagonalDifference(); // returns '15'

// find primary diagonal (left to right): returns [11, 5, -12]
$diagonalDifference->findDiagonal(\Gkirtsou\Diagonal\DiagonalDifference::PRIMARY_TYPE);

// find secondary diagonal (right to left): returns [4, 5, 10]
$diagonalDifference->findDiagonal(\Gkirtsou\Diagonal\DiagonalDifference::SECONDARY_TYPE);