Source for file Matrix.php
Documentation is available at Matrix.php
define('RAND_MAX', mt_getrandmax());
/** PHPExcel root directory */
define('PHPEXCEL_ROOT', dirname(__FILE__ ) . '/../../../');
require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
// check mbstring.func_overload
if (ini_get('mbstring.func_overload') & 2) {
throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/utils/Error.php';
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/utils/Maths.php';
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/CholeskyDecomposition.php';
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/LUDecomposition.php';
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/QRDecomposition.php';
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/EigenvalueDecomposition.php';
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/SingularValueDecomposition.php';
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/String.php';
require_once PHPEXCEL_ROOT . 'PHPExcel/Calculation/Functions.php';
* @author Michael Bommarito
* @author Lukasz Karapuda
* @author Bartek Matosiuk
* @see http://math.nist.gov/javanumerics/jama/
* Matrix column dimension
* Polymorphic constructor
* As PHP has no support for polymorphic constructors, we hack our own sort of polymorphism using func_num_args, func_get_arg, and gettype. In essence, we're just implementing a simple RTTI filter and calling the appropriate constructor.
//Rectangular matrix - m x n
//Rectangular matrix constant-filled - m x n filled with c
case 'integer,integer,integer':
//Rectangular matrix constant-filled - m x n filled with c
case 'integer,integer,double':
//Rectangular matrix - m x n initialized from 2D array
$this->m = count($args[0]);
$this->n = count($args[0][0]);
//Rectangular matrix - m x n initialized from 2D array
case 'array,integer,integer':
//Rectangular matrix - m x n initialized from packed array
$this->n = count($args[0]) / $this->m;
if (($this->m * $this->n) == count($args[0])) {
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$this->A[$i][$j] = $args[0][$i + $j * $this->m];
} // function __construct()
* @return array Matrix array
* @return array Matrix array copy
} // function getArrayCopy()
* Construct a matrix from a copy of a 2-D array.
* @param double A[][] Two-dimensional array of doubles.
* @exception IllegalArgumentException All rows must have the same length
$newCopyMatrix = new Matrix($this->m, $this->n);
for ($i = 0; $i < $this->m; ++ $i) {
if (count($A[$i]) != $this->n) {
for ($j = 0; $j < $this->n; ++ $j) {
$newCopyMatrix->A[$i][$j] = $A[$i][$j];
} // function constructWithCopy()
* Get a column-packed array
* @return array Column-packed matrix array
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
} // function getColumnPackedCopy()
* @return array Row-packed matrix array
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
} // function getRowPackedCopy()
* @return int Row dimension
} // function getRowDimension()
* @return int Column dimension
} // function getColumnDimension()
* Get the i,j-th element of the matrix.
* @param int $i Row position
* @param int $j Column position
* @return mixed Element (int/float/double)
public function get($i = null, $j = null) {
* @param int $i0 Initial row index
* @param int $iF Final row index
* @param int $j0 Initial column index
* @param int $jF Final column index
* @return Matrix Submatrix
for($i = $i0; $i < $this->m; ++ $i) {
for($j = $j0; $j < $this->n; ++ $j) {
$R->set($i, $j, $this->A[$i][$j]);
//A($i0...$iF; $j0...$jF)
case 'integer,integer,integer,integer':
list ($i0, $iF, $j0, $jF) = $args;
for($i = $i0; $i <= $iF; ++ $i) {
for($j = $j0; $j <= $jF; ++ $j) {
$R->set($i - $i0, $j - $j0, $this->A[$i][$j]);
//$R = array of row indices; $C = array of column indices
for($i = 0; $i < $m; ++ $i) {
for($j = 0; $j < $n; ++ $j) {
$R->set($i - $i0, $j - $j0, $this->A[$RL[$i]][$CL[$j]]);
//$RL = array of row indices; $CL = array of column indices
for($i = 0; $i < $m; ++ $i) {
for($j = 0; $j < $n; ++ $j) {
$R->set($i, $j, $this->A[$RL[$i]][$CL[$j]]);
//A($i0...$iF); $CL = array of column indices
case 'integer,integer,array':
list ($i0, $iF, $CL) = $args;
for($i = $i0; $i < $iF; ++ $i) {
for($j = 0; $j < $n; ++ $j) {
$R->set($i - $i0, $j, $this->A[$RL[$i]][$j]);
//$RL = array of row indices
case 'array,integer,integer':
list ($RL, $j0, $jF) = $args;
for($i = 0; $i < $m; ++ $i) {
for($j = $j0; $j <= $jF; ++ $j) {
$R->set($i, $j - $j0, $this->A[$RL[$i]][$j]);
} // function getMatrix()
* @param int $i0 Initial row index
* @param int $j0 Initial column index
* @param mixed $S Matrix/Array submatrix
* ($i0, $j0, $S) $S = Matrix
* ($i0, $j0, $S) $S = Array
case 'integer,integer,object':
for($i = $i0; $i < $i0 + $M->m; ++ $i) {
for($j = $j0; $j < $j0 + $M->n; ++ $j) {
$this->A[$i][$j] = $M->get($i - $i0, $j - $j0);
case 'integer,integer,array':
for($i = $i0; $i < $i0 + $M->m; ++ $i) {
for($j = $j0; $j < $j0 + $M->n; ++ $j) {
$this->A[$i][$j] = $M->get($i - $i0, $j - $j0);
} // function setMatrix()
* Is matrix B the same size?
* @param Matrix $B Matrix B
if (($this->m == $B->getRowDimension()) && ($this->n == $B->getColumnDimension())) {
} // function checkMatrixDimensions()
* Set the i,j-th element of the matrix.
* @param int $i Row position
* @param int $j Column position
* @param mixed $c Int/float/double value
* @return mixed Element (int/float/double)
public function set($i = null, $j = null, $c = null) {
// Optimized set version just has this
if (is_int($i) && is_int($j) && is_numeric($c)) {
if (($i < $this->m) && ($j < $this->n)) {
echo "A[$i][$j] = $c<br />";
throw new Exception(JAMAError(ArgumentBoundsException));
throw new Exception(JAMAError(ArgumentTypeException));
* Generate an identity matrix.
* @param int $m Row dimension
* @param int $n Column dimension
* @return Matrix Identity matrix
public function identity($m = null, $n = null) {
* Generate a diagonal matrix
* @param int $m Row dimension
* @param int $n Column dimension
* @param mixed $c Diagonal value
* @return Matrix Diagonal matrix
public function diagonal($m = null, $n = null, $c = 1) {
for($i = 0; $i < $m; ++ $i) {
* Generate a filled matrix
* @param int $m Row dimension
* @param int $n Column dimension
* @param int $c Fill constant
* @return Matrix Filled matrix
public function filled($m = null, $n = null, $c = 0) {
* Generate a random matrix
* @param int $m Row dimension
* @param int $n Column dimension
* @return Matrix Random matrix
public function random($m = null, $n = null, $a = RAND_MIN, $b = RAND_MAX) {
for($i = 0; $i < $m; ++ $i) {
for($j = 0; $j < $n; ++ $j) {
* @return array Packed array
return $this->getRowPacked();
* Get a submatrix by row index/range
* @param int $i0 Initial row index
* @param int $iF Final row index
* @return Matrix Submatrix
return $this->getMatrix($i0, 0, $iF + 1, $this->n);
return $this->getMatrix($i0, 0, $i0 + 1, $this->n);
} // function getMatrixByRow()
* Get a submatrix by column index/range
* @param int $i0 Initial column index
* @param int $iF Final column index
* @return Matrix Submatrix
return $this->getMatrix(0, $j0, $this->m, $jF + 1);
return $this->getMatrix(0, $j0, $this->m, $j0 + 1);
} // function getMatrixByCol()
* @return Matrix Transposed matrix
$R = new Matrix($this->n, $this->m);
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$R->set($j, $i, $this->A[$i][$j]);
} // function transpose()
* @return float Maximum column sum
public function norm1() {
for($j = 0; $j < $this->n; ++ $j) {
for($i = 0; $i < $this->m; ++ $i) {
$s += abs($this->A[$i][$j]);
$r = ($r > $s) ? $r : $s;
* @return float Maximum singular value
public function norm2() {
* @return float Maximum row sum
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$s += abs($this->A[$i][$j]);
$r = ($r > $s) ? $r : $s;
* @return float Square root of the sum of all elements squared
public function normF() {
for ($i = 0; $i < $this->m; ++ $i) {
for ($j = 0; $j < $this->n; ++ $j) {
$f = hypo($f,$this->A[$i][$j]);
* @return effective numerical rank, obtained from SVD.
public function rank () {
* Matrix condition (2 norm)
* @return ratio of largest to smallest singular value.
public function cond () {
* Sum of diagonal elements
* @return float Sum of diagonal elements
public function trace() {
$n = min($this->m, $this->n);
for($i = 0; $i < $n; ++ $i) {
* @return Matrix Unary minus matrix
* @param mixed $B Matrix/Array
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$M->set($i, $j, $M->get($i, $j) + $this->A[$i][$j]);
* @param mixed $B Matrix/Array
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$value = $M->get($i, $j);
$this->A[$i][$j] = trim($this->A[$i][$j],'"');
$value = trim($value,'"');
$this->A[$i][$j] += $value;
} // function plusEquals()
* @param mixed $B Matrix/Array
public function minus() {
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$M->set($i, $j, $M->get($i, $j) - $this->A[$i][$j]);
* @param mixed $B Matrix/Array
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$value = $M->get($i, $j);
$this->A[$i][$j] = trim($this->A[$i][$j],'"');
$value = trim($value,'"');
$this->A[$i][$j] -= $value;
} // function minusEquals()
* Element-by-element multiplication
* @param mixed $B Matrix/Array
* @return Matrix Matrix Cij
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$M->set($i, $j, $M->get($i, $j) * $this->A[$i][$j]);
} // function arrayTimes()
* Element-by-element multiplication
* @param mixed $B Matrix/Array
* @return Matrix Matrix Aij
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$value = $M->get($i, $j);
$this->A[$i][$j] = trim($this->A[$i][$j],'"');
$value = trim($value,'"');
$this->A[$i][$j] *= $value;
} // function arrayTimesEquals()
* Element-by-element right division
* @param Matrix $B Matrix B
* @return Matrix Division result
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$value = $M->get($i, $j);
$this->A[$i][$j] = trim($this->A[$i][$j],'"');
$value = trim($value,'"');
// Trap for Divide by Zero error
$M->set($i, $j, '#DIV/0!');
$M->set($i, $j, $this->A[$i][$j] / $value);
} // function arrayRightDivide()
* Element-by-element right division
* @param mixed $B Matrix/Array
* @return Matrix Matrix Aij
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$this->A[$i][$j] = $this->A[$i][$j] / $M->get($i, $j);
} // function arrayRightDivideEquals()
* Element-by-element Left division
* @param Matrix $B Matrix B
* @return Matrix Division result
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$M->set($i, $j, $M->get($i, $j) / $this->A[$i][$j]);
} // function arrayLeftDivide()
* Element-by-element Left division
* @param mixed $B Matrix/Array
* @return Matrix Matrix Aij
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$this->A[$i][$j] = $M->get($i, $j) / $this->A[$i][$j];
} // function arrayLeftDivideEquals()
* @param mixed $n Matrix/Array/Scalar
public function times() {
$C = new Matrix($this->m, $B->n);
for($j = 0; $j < $B->n; ++ $j) {
for ($k = 0; $k < $this->n; ++ $k) {
$Bcolj[$k] = $B->A[$k][$j];
for($i = 0; $i < $this->m; ++ $i) {
for($k = 0; $k < $this->n; ++ $k) {
$s += $Arowi[$k] * $Bcolj[$k];
throw new Exception(JAMAError(MatrixDimensionMismatch));
$C = new Matrix($this->m, $B->n);
for($i = 0; $i < $C->m; ++ $i) {
for($j = 0; $j < $C->n; ++ $j) {
for($k = 0; $k < $C->n; ++ $k) {
$s += $this->A[$i][$k] * $B->A[$k][$j];
throw new Exception(JAMAError(MatrixDimensionMismatch));
for($i = 0; $i < $C->m; ++ $i) {
for($j = 0; $j < $C->n; ++ $j) {
$C->A[$i][$j] *= $args[0];
$C = new Matrix($this->m, $this->n);
for($i = 0; $i < $C->m; ++ $i) {
for($j = 0; $j < $C->n; ++ $j) {
$C->A[$i][$j] = $args[0] * $this->A[$i][$j];
for($i = 0; $i < $C->m; ++ $i) {
for($j = 0; $j < $C->n; ++ $j) {
$C->A[$i][$j] *= $args[0];
* @param mixed $B Matrix/Array
public function power() {
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$value = $M->get($i, $j);
$this->A[$i][$j] = trim($this->A[$i][$j],'"');
$value = trim($value,'"');
$this->A[$i][$j] = pow($this->A[$i][$j],$value);
* @param mixed $B Matrix/Array
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
// $this->A[$i][$j] = '"'.trim($this->A[$i][$j],'"').trim($M->get($i, $j),'"').'"';
$this->A[$i][$j] = trim($this->A[$i][$j],'"'). trim($M->get($i, $j),'"');
* @return Matrix Cholesky decomposition
* @return Matrix LU decomposition
* @return Matrix QR decomposition
* Eigenvalue decomposition
* @return Matrix Eigenvalue decomposition
* Singular value decomposition
* @return Singular value decomposition
* @param Matrix $B Right hand side
* @return Matrix ... Solution if A is square, least squares solution otherwise
public function solve($B) {
if ($this->m == $this->n) {
* Matrix inverse or pseudoinverse.
* @return Matrix ... Inverse(A) if A is square, pseudoinverse otherwise.
* @return float Determinant
* Older debugging utility for backwards compatability.
* @return html version of matrix
public function mprint($A, $format= "%01.2f", $width= 2) {
for ($i = 0; $i < $m; ++ $i) {
for ($j = 0; $j < $n; ++ $j) {
$formatted = sprintf($format, $A[$i][$j]);
echo $formatted. $spacing;
* @return Output HTML representation of matrix
public function toHTML($width= 2) {
print ('<table style="background-color:#eee;">');
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
print ('<td style="background-color:#fff;border:1px solid #000;padding:2px;text-align:center;vertical-align:middle;">' . $this->A[$i][$j] . '</td>');
|