Source for file Matrix.php
Documentation is available at Matrix.php
/** PHPExcel root directory */
define('PHPEXCEL_ROOT', dirname(__FILE__ ) . '/../../../');
require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
* @author Michael Bommarito
* @author Lukasz Karapuda
* @author Bartek Matosiuk
* @see http://math.nist.gov/javanumerics/jama/
const PolymorphicArgumentException = "Invalid argument pattern for polymorphic function.";
const ArgumentTypeException = "Invalid argument type.";
const ArgumentBoundsException = "Invalid argument range.";
const MatrixDimensionException = "Matrix dimensions are not equal.";
const ArrayLengthException = "Array length must be a multiple of m.";
* 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 initialized from 2D array
$this->m = count($args[0]);
$this->n = count($args[0][0]);
//Rectangular matrix - m x n
//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];
throw new Exception(self::ArrayLengthException);
throw new Exception(self::PolymorphicArgumentException);
throw new Exception(self::PolymorphicArgumentException);
} // function __construct()
* @return array Matrix array
* @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
if ($i0 >= 0) { $m = $this->m - $i0; } else { throw new Exception(self::ArgumentBoundsException); }
if ($j0 >= 0) { $n = $this->n - $j0; } else { throw new Exception(self::ArgumentBoundsException); }
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;
if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) { $m = $iF - $i0; } else { throw new Exception(self::ArgumentBoundsException); }
if (($jF > $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new Exception(self::ArgumentBoundsException); }
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
if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(self::ArgumentBoundsException); }
if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(self::ArgumentBoundsException); }
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
if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(self::ArgumentBoundsException); }
if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(self::ArgumentBoundsException); }
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;
if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) { $m = $iF - $i0; } else { throw new Exception(self::ArgumentBoundsException); }
if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(self::ArgumentBoundsException); }
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;
if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(self::ArgumentBoundsException); }
if (($jF >= $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new Exception(self::ArgumentBoundsException); }
for($i = 0; $i < $m; ++ $i) {
for($j = $j0; $j <= $jF; ++ $j) {
$R->set($i, $j - $j0, $this->A[$RL[$i]][$j]);
throw new Exception(self::PolymorphicArgumentException);
throw new Exception(self::PolymorphicArgumentException);
} // function getMatrix()
* Is matrix B the same size?
* @param Matrix $B Matrix B
if (($this->m == $B->getRowDimension()) && ($this->n == $B->getColumnDimension())) {
throw new Exception(self::MatrixDimensionException);
throw new Exception(self::ArgumentTypeException);
} // 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
* 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) {
* 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);
throw new Exception(self::ArgumentTypeException);
} // 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);
throw new Exception(self::ArgumentTypeException);
} // function getMatrixByCol()
* @return Matrix Transposed matrix
for($i = 0; $i < $this->m; ++ $i) {
for($j = 0; $j < $this->n; ++ $j) {
$R->set($j, $i, $this->A[$i][$j]);
} // function transpose()
* 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
throw new Exception(self::PolymorphicArgumentException);
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]);
throw new Exception(self::PolymorphicArgumentException);
* @param mixed $B Matrix/Array
throw new Exception(self::PolymorphicArgumentException);
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;
throw new Exception(self::PolymorphicArgumentException);
} // function plusEquals()
* @param mixed $B Matrix/Array
public function minus() {
throw new Exception(self::PolymorphicArgumentException);
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]);
throw new Exception(self::PolymorphicArgumentException);
* @param mixed $B Matrix/Array
throw new Exception(self::PolymorphicArgumentException);
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;
throw new Exception(self::PolymorphicArgumentException);
} // function minusEquals()
* Element-by-element multiplication
* @param mixed $B Matrix/Array
* @return Matrix Matrix Cij
throw new Exception(self::PolymorphicArgumentException);
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]);
throw new Exception(self::PolymorphicArgumentException);
} // function arrayTimes()
* Element-by-element multiplication
* @param mixed $B Matrix/Array
* @return Matrix Matrix Aij
throw new Exception(self::PolymorphicArgumentException);
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;
throw new Exception(self::PolymorphicArgumentException);
} // function arrayTimesEquals()
* Element-by-element right division
* @param Matrix $B Matrix B
* @return Matrix Division result
throw new Exception(self::PolymorphicArgumentException);
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);
throw new Exception(self::PolymorphicArgumentException);
} // function arrayRightDivide()
* Element-by-element right division
* @param mixed $B Matrix/Array
* @return Matrix Matrix Aij
throw new Exception(self::PolymorphicArgumentException);
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);
throw new Exception(self::PolymorphicArgumentException);
} // function arrayRightDivideEquals()
* Element-by-element Left division
* @param Matrix $B Matrix B
* @return Matrix Division result
throw new Exception(self::PolymorphicArgumentException);
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]);
throw new Exception(self::PolymorphicArgumentException);
} // function arrayLeftDivide()
* Element-by-element Left division
* @param mixed $B Matrix/Array
* @return Matrix Matrix Aij
throw new Exception(self::PolymorphicArgumentException);
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];
throw new Exception(self::PolymorphicArgumentException);
} // function arrayLeftDivideEquals()
* @param mixed $n Matrix/Array/Scalar
public function times() {
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));
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];
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];
throw new Exception(self::PolymorphicArgumentException);
throw new Exception(self::PolymorphicArgumentException);
* @param mixed $B Matrix/Array
public function power() {
throw new Exception(self::PolymorphicArgumentException);
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);
throw new Exception(self::PolymorphicArgumentException);
* @param mixed $B Matrix/Array
throw new Exception(self::PolymorphicArgumentException);
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),'"');
throw new Exception(self::PolymorphicArgumentException);
* @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) {
$QR = new QRDecomposition($this);
* Matrix inverse or pseudoinverse.
* @return Matrix ... Inverse(A) if A is square, pseudoinverse otherwise.
* @return float Determinant
} // class PHPExcel_Shared_JAMA_Matrix
|