Memory and Performance Improvements

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@64204 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker
2010-11-23 17:24:29 +00:00
parent 4da85678d4
commit 9641c16df0
19 changed files with 141 additions and 647 deletions
+95 -510
View File
@@ -3,9 +3,6 @@
* @package JAMA
*/
define('RAND_MAX', mt_getrandmax());
define('RAND_MIN', 0);
/** PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) {
/**
@@ -13,23 +10,8 @@ if (!defined('PHPEXCEL_ROOT')) {
*/
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
PHPExcel_Autoloader::Register();
PHPExcel_Shared_ZipStreamWrapper::register();
// 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';
/*
* Matrix class
@@ -42,7 +24,14 @@ require_once PHPEXCEL_ROOT . 'PHPExcel/Calculation/Functions.php';
* @license PHP v3.0
* @see http://math.nist.gov/javanumerics/jama/
*/
class Matrix {
class PHPExcel_Shared_JAMA_Matrix {
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 storage
@@ -98,24 +87,6 @@ class Matrix {
$this->n = $args[1];
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0));
break;
//Rectangular matrix constant-filled - m x n filled with c
case 'integer,integer,integer':
$this->m = $args[0];
$this->n = $args[1];
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, $args[2]));
break;
//Rectangular matrix constant-filled - m x n filled with c
case 'integer,integer,double':
$this->m = $args[0];
$this->n = $args[1];
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, $args[2]));
break;
//Rectangular matrix - m x n initialized from 2D array
case 'array,integer,integer':
$this->m = $args[1];
$this->n = $args[2];
$this->A = $args[0];
break;
//Rectangular matrix - m x n initialized from packed array
case 'array,integer':
$this->m = $args[1];
@@ -131,15 +102,15 @@ class Matrix {
}
}
} else {
throw new Exception(JAMAError(ArrayLengthException));
throw new Exception(self::ArrayLengthException);
}
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function __construct()
@@ -154,73 +125,6 @@ class Matrix {
} // function getArray()
/**
* getArrayCopy
*
* @return array Matrix array copy
*/
public function getArrayCopy() {
return $this->A;
} // function getArrayCopy()
/**
* constructWithCopy
* 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
*/
public function constructWithCopy($A) {
$this->m = count($A);
$this->n = count($A[0]);
$newCopyMatrix = new Matrix($this->m, $this->n);
for ($i = 0; $i < $this->m; ++$i) {
if (count($A[$i]) != $this->n) {
throw new Exception(JAMAError(RowLengthException));
}
for ($j = 0; $j < $this->n; ++$j) {
$newCopyMatrix->A[$i][$j] = $A[$i][$j];
}
}
return $newCopyMatrix;
} // function constructWithCopy()
/**
* getColumnPackedCopy
*
* Get a column-packed array
* @return array Column-packed matrix array
*/
public function getColumnPackedCopy() {
$P = array();
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
array_push($P, $this->A[$j][$i]);
}
}
return $P;
} // function getColumnPackedCopy()
/**
* getRowPackedCopy
*
* Get a row-packed array
* @return array Row-packed matrix array
*/
public function getRowPackedCopy() {
$P = array();
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
array_push($P, $this->A[$i][$j]);
}
}
return $P;
} // function getRowPackedCopy()
/**
* getRowDimension
*
@@ -273,9 +177,9 @@ class Matrix {
//A($i0...; $j0...)
case 'integer,integer':
list($i0, $j0) = $args;
if ($i0 >= 0) { $m = $this->m - $i0; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if ($j0 >= 0) { $n = $this->n - $j0; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m, $n);
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); }
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
for($i = $i0; $i < $this->m; ++$i) {
for($j = $j0; $j < $this->n; ++$j) {
$R->set($i, $j, $this->A[$i][$j]);
@@ -286,9 +190,9 @@ class Matrix {
//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(JAMAError(ArgumentBoundsException)); }
if (($jF > $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m+1, $n+1);
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); }
$R = new PHPExcel_Shared_JAMA_Matrix($m+1, $n+1);
for($i = $i0; $i <= $iF; ++$i) {
for($j = $j0; $j <= $jF; ++$j) {
$R->set($i - $i0, $j - $j0, $this->A[$i][$j]);
@@ -299,9 +203,9 @@ class Matrix {
//$R = array of row indices; $C = array of column indices
case 'array,array':
list($RL, $CL) = $args;
if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m, $n);
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); }
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
for($i = 0; $i < $m; ++$i) {
for($j = 0; $j < $n; ++$j) {
$R->set($i - $i0, $j - $j0, $this->A[$RL[$i]][$CL[$j]]);
@@ -312,9 +216,9 @@ class Matrix {
//$RL = array of row indices; $CL = array of column indices
case 'array,array':
list($RL, $CL) = $args;
if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m, $n);
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); }
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
for($i = 0; $i < $m; ++$i) {
for($j = 0; $j < $n; ++$j) {
$R->set($i, $j, $this->A[$RL[$i]][$CL[$j]]);
@@ -325,9 +229,9 @@ class Matrix {
//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(JAMAError(ArgumentBoundsException)); }
if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m, $n);
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); }
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
for($i = $i0; $i < $iF; ++$i) {
for($j = 0; $j < $n; ++$j) {
$R->set($i - $i0, $j, $this->A[$RL[$i]][$j]);
@@ -338,9 +242,9 @@ class Matrix {
//$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(JAMAError(ArgumentBoundsException)); }
if (($jF >= $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m, $n+1);
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); }
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n+1);
for($i = 0; $i < $m; ++$i) {
for($j = $j0; $j <= $jF; ++$j) {
$R->set($i, $j - $j0, $this->A[$RL[$i]][$j]);
@@ -349,61 +253,15 @@ class Matrix {
return $R;
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function getMatrix()
/**
* setMatrix
*
* Set a submatrix
* @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
*/
public function setMatrix() {
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'integer,integer,object':
if ($args[2] instanceof Matrix) { $M = $args[2]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if (($args[0] + $M->m) <= $this->m) { $i0 = $args[0]; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (($args[1] + $M->n) <= $this->n) { $j0 = $args[1]; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
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);
}
}
break;
case 'integer,integer,array':
$M = new Matrix($args[2]);
if (($args[0] + $M->m) <= $this->m) { $i0 = $args[0]; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (($args[1] + $M->n) <= $this->n) { $j0 = $args[1]; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
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);
}
}
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
break;
}
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
}
} // function setMatrix()
/**
* checkMatrixDimensions
*
@@ -412,14 +270,14 @@ class Matrix {
* @return boolean
*/
public function checkMatrixDimensions($B = null) {
if ($B instanceof Matrix) {
if ($B instanceof PHPExcel_Shared_JAMA_Matrix) {
if (($this->m == $B->getRowDimension()) && ($this->n == $B->getColumnDimension())) {
return true;
} else {
throw new Exception(JAMAError(MatrixDimensionException));
throw new Exception(self::MatrixDimensionException);
}
} else {
throw new Exception(JAMAError(ArgumentTypeException));
throw new Exception(self::ArgumentTypeException);
}
} // function checkMatrixDimensions()
@@ -437,18 +295,6 @@ class Matrix {
public function set($i = null, $j = null, $c = null) {
// Optimized set version just has this
$this->A[$i][$j] = $c;
/*
if (is_int($i) && is_int($j) && is_numeric($c)) {
if (($i < $this->m) && ($j < $this->n)) {
$this->A[$i][$j] = $c;
} else {
echo "A[$i][$j] = $c<br />";
throw new Exception(JAMAError(ArgumentBoundsException));
}
} else {
throw new Exception(JAMAError(ArgumentTypeException));
}
*/
} // function set()
@@ -475,7 +321,7 @@ class Matrix {
* @return Matrix Diagonal matrix
*/
public function diagonal($m = null, $n = null, $c = 1) {
$R = new Matrix($m, $n);
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
for($i = 0; $i < $m; ++$i) {
$R->set($i, $i, $c);
}
@@ -483,58 +329,6 @@ class Matrix {
} // function diagonal()
/**
* filled
*
* 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) {
if (is_int($m) && is_int($n) && is_numeric($c)) {
$R = new Matrix($m, $n, $c);
return $R;
} else {
throw new Exception(JAMAError(ArgumentTypeException));
}
} // function filled()
/**
* random
*
* 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) {
if (is_int($m) && is_int($n) && is_numeric($a) && is_numeric($b)) {
$R = new Matrix($m, $n);
for($i = 0; $i < $m; ++$i) {
for($j = 0; $j < $n; ++$j) {
$R->set($i, $j, mt_rand($a, $b));
}
}
return $R;
} else {
throw new Exception(JAMAError(ArgumentTypeException));
}
} // function random()
/**
* packed
*
* Alias for getRowPacked
* @return array Packed array
*/
public function packed() {
return $this->getRowPacked();
} // function packed()
/**
* getMatrixByRow
*
@@ -551,7 +345,7 @@ class Matrix {
return $this->getMatrix($i0, 0, $i0 + 1, $this->n);
}
} else {
throw new Exception(JAMAError(ArgumentTypeException));
throw new Exception(self::ArgumentTypeException);
}
} // function getMatrixByRow()
@@ -572,7 +366,7 @@ class Matrix {
return $this->getMatrix(0, $j0, $this->m, $j0 + 1);
}
} else {
throw new Exception(JAMAError(ArgumentTypeException));
throw new Exception(self::ArgumentTypeException);
}
} // function getMatrixByCol()
@@ -584,7 +378,7 @@ class Matrix {
* @return Matrix Transposed matrix
*/
public function transpose() {
$R = new Matrix($this->n, $this->m);
$R = new PHPExcel_Shared_JAMA_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]);
@@ -594,93 +388,6 @@ class Matrix {
} // function transpose()
/**
* norm1
*
* One norm
* @return float Maximum column sum
*/
public function norm1() {
$r = 0;
for($j = 0; $j < $this->n; ++$j) {
$s = 0;
for($i = 0; $i < $this->m; ++$i) {
$s += abs($this->A[$i][$j]);
}
$r = ($r > $s) ? $r : $s;
}
return $r;
} // function norm1()
/**
* norm2
*
* Maximum singular value
* @return float Maximum singular value
*/
public function norm2() {
} // function norm2()
/**
* normInf
*
* Infinite norm
* @return float Maximum row sum
*/
public function normInf() {
$r = 0;
for($i = 0; $i < $this->m; ++$i) {
$s = 0;
for($j = 0; $j < $this->n; ++$j) {
$s += abs($this->A[$i][$j]);
}
$r = ($r > $s) ? $r : $s;
}
return $r;
} // function normInf()
/**
* normF
*
* Frobenius norm
* @return float Square root of the sum of all elements squared
*/
public function normF() {
$f = 0;
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
$f = hypo($f,$this->A[$i][$j]);
}
}
return $f;
} // function normF()
/**
* Matrix rank
*
* @return effective numerical rank, obtained from SVD.
*/
public function rank () {
$svd = new SingularValueDecomposition($this);
return $svd->rank();
} // function rank ()
/**
* Matrix condition (2 norm)
*
* @return ratio of largest to smallest singular value.
*/
public function cond () {
$svd = new SingularValueDecomposition($this);
return $svd->cond();
} // function cond ()
/**
* trace
*
@@ -721,13 +428,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -738,7 +445,7 @@ class Matrix {
}
return $M;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function plus()
@@ -757,13 +464,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -788,7 +495,7 @@ class Matrix {
}
return $this;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function plusEquals()
@@ -807,13 +514,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -824,7 +531,7 @@ class Matrix {
}
return $M;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function minus()
@@ -843,13 +550,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -874,7 +581,7 @@ class Matrix {
}
return $this;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function minusEquals()
@@ -894,13 +601,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -911,7 +618,7 @@ class Matrix {
}
return $M;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function arrayTimes()
@@ -931,13 +638,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -962,7 +669,7 @@ class Matrix {
}
return $this;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function arrayTimesEquals()
@@ -982,13 +689,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -1018,7 +725,7 @@ class Matrix {
}
return $M;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function arrayRightDivide()
@@ -1038,13 +745,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -1055,7 +762,7 @@ class Matrix {
}
return $M;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function arrayRightDivideEquals()
@@ -1075,13 +782,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -1092,7 +799,7 @@ class Matrix {
}
return $M;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function arrayLeftDivide()
@@ -1112,13 +819,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -1129,7 +836,7 @@ class Matrix {
}
return $M;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function arrayLeftDivideEquals()
@@ -1148,9 +855,9 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $B = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $B = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
if ($this->n == $B->m) {
$C = new Matrix($this->m, $B->n);
$C = new PHPExcel_Shared_JAMA_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];
@@ -1170,9 +877,9 @@ class Matrix {
}
break;
case 'array':
$B = new Matrix($args[0]);
$B = new PHPExcel_Shared_JAMA_Matrix($args[0]);
if ($this->n == $B->m) {
$C = new Matrix($this->m, $B->n);
$C = new PHPExcel_Shared_JAMA_Matrix($this->m, $B->n);
for($i = 0; $i < $C->m; ++$i) {
for($j = 0; $j < $C->n; ++$j) {
$s = "0";
@@ -1189,7 +896,7 @@ class Matrix {
return $M;
break;
case 'integer':
$C = new Matrix($this->A);
$C = new PHPExcel_Shared_JAMA_Matrix($this->A);
for($i = 0; $i < $C->m; ++$i) {
for($j = 0; $j < $C->n; ++$j) {
$C->A[$i][$j] *= $args[0];
@@ -1198,7 +905,7 @@ class Matrix {
return $C;
break;
case 'double':
$C = new Matrix($this->m, $this->n);
$C = new PHPExcel_Shared_JAMA_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];
@@ -1207,7 +914,7 @@ class Matrix {
return $C;
break;
case 'float':
$C = new Matrix($this->A);
$C = new PHPExcel_Shared_JAMA_Matrix($this->A);
for($i = 0; $i < $C->m; ++$i) {
for($j = 0; $j < $C->n; ++$j) {
$C->A[$i][$j] *= $args[0];
@@ -1216,11 +923,11 @@ class Matrix {
return $C;
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
} else {
throw new Exception(PolymorphicArgumentException);
throw new Exception(self::PolymorphicArgumentException);
}
} // function times()
@@ -1239,13 +946,13 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
break;
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
@@ -1270,7 +977,7 @@ class Matrix {
}
return $this;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function power()
@@ -1289,100 +996,27 @@ class Matrix {
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); }
case 'array':
$M = new Matrix($args[0]);
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
default:
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
break;
}
$this->checkMatrixDimensions($M);
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 $this;
} else {
throw new Exception(JAMAError(PolymorphicArgumentException));
throw new Exception(self::PolymorphicArgumentException);
}
} // function concat()
/**
* chol
*
* Cholesky decomposition
* @return Matrix Cholesky decomposition
*/
public function chol() {
return new CholeskyDecomposition($this);
} // function chol()
/**
* lu
*
* LU decomposition
* @return Matrix LU decomposition
*/
public function lu() {
return new LUDecomposition($this);
} // function lu()
/**
* qr
*
* QR decomposition
* @return Matrix QR decomposition
*/
public function qr() {
return new QRDecomposition($this);
} // function qr()
/**
* eig
*
* Eigenvalue decomposition
* @return Matrix Eigenvalue decomposition
*/
public function eig() {
return new EigenvalueDecomposition($this);
} // function eig()
/**
* svd
*
* Singular value decomposition
* @return Singular value decomposition
*/
public function svd() {
return new SingularValueDecomposition($this);
} // function svd()
/**
* Solve A*X = B.
*
* @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) {
$LU = new LUDecomposition($this);
return $LU->solve($B);
} else {
$QR = new QRDecomposition($this);
return $QR->solve($B);
}
} // function solve()
/**
* Matrix inverse or pseudoinverse.
*
@@ -1393,53 +1027,4 @@ class Matrix {
} // function inverse()
/**
* det
*
* Calculate determinant
* @return float Determinant
*/
public function det() {
$L = new LUDecomposition($this);
return $L->det();
} // function det()
/**
* Older debugging utility for backwards compatability.
*
* @return html version of matrix
*/
public function mprint($A, $format="%01.2f", $width=2) {
$m = count($A);
$n = count($A[0]);
$spacing = str_repeat('&nbsp;',$width);
for ($i = 0; $i < $m; ++$i) {
for ($j = 0; $j < $n; ++$j) {
$formatted = sprintf($format, $A[$i][$j]);
echo $formatted.$spacing;
}
echo "<br />";
}
} // function mprint()
/**
* Debugging utility.
*
* @return Output HTML representation of matrix
*/
public function toHTML($width=2) {
print('<table style="background-color:#eee;">');
for($i = 0; $i < $this->m; ++$i) {
print('<tr>');
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>');
}
print('</tr>');
}
print('</table>');
} // function toHTML()
} // class Matrix