PhpSpreadsheet/Classes/PHPExcel/Shared/JAMA/Matrix.php

1160 lines
40 KiB
PHP
Raw Normal View History

<?php
/**
* @package JAMA
*/
/** PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) {
2015-05-12 09:22:06 +00:00
/**
* @ignore
*/
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}
/*
2015-05-12 09:22:06 +00:00
* Matrix class
*
2015-05-12 09:22:06 +00:00
* @author Paul Meagher
* @author Michael Bommarito
* @author Lukasz Karapuda
* @author Bartek Matosiuk
* @version 1.8
* @license PHP v3.0
* @see http://math.nist.gov/javanumerics/jama/
*/
2015-05-17 12:16:53 +00:00
class PHPExcel_Shared_JAMA_Matrix
{
2015-05-12 09:22:06 +00:00
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
*
* @var array
* @access public
*/
public $A = array();
/**
* Matrix row dimension
*
* @var int
* @access private
*/
private $m;
/**
* Matrix column dimension
*
* @var int
* @access private
*/
private $n;
/**
* 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.
*/
2015-05-17 12:16:53 +00:00
public function __construct()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
//Rectangular matrix - m x n initialized from 2D array
case 'array':
2015-05-17 12:16:53 +00:00
$this->m = count($args[0]);
$this->n = count($args[0][0]);
$this->A = $args[0];
break;
2015-05-12 09:22:06 +00:00
//Square matrix - n x n
case 'integer':
2015-05-17 12:16:53 +00:00
$this->m = $args[0];
$this->n = $args[0];
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0));
break;
2015-05-12 09:22:06 +00:00
//Rectangular matrix - m x n
case 'integer,integer':
2015-05-17 12:16:53 +00:00
$this->m = $args[0];
$this->n = $args[1];
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0));
break;
2015-05-12 09:22:06 +00:00
//Rectangular matrix - m x n initialized from packed array
case 'array,integer':
2015-05-17 12:16:53 +00:00
$this->m = $args[1];
if ($this->m != 0) {
$this->n = count($args[0]) / $this->m;
} else {
$this->n = 0;
}
if (($this->m * $this->n) == count($args[0])) {
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-17 12:16:53 +00:00
$this->A[$i][$j] = $args[0][$i + $j * $this->m];
2015-05-12 09:22:06 +00:00
}
}
2015-05-17 12:16:53 +00:00
} else {
throw new PHPExcel_Calculation_Exception(self::ArrayLengthException);
}
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* getArray
*
* @return array Matrix array
*/
2015-05-17 12:16:53 +00:00
public function getArray()
{
2015-05-12 09:22:06 +00:00
return $this->A;
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* getRowDimension
*
* @return int Row dimension
*/
2015-05-17 12:16:53 +00:00
public function getRowDimension()
{
2015-05-12 09:22:06 +00:00
return $this->m;
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* getColumnDimension
*
* @return int Column dimension
*/
2015-05-17 12:16:53 +00:00
public function getColumnDimension()
{
2015-05-12 09:22:06 +00:00
return $this->n;
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* get
*
* 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)
*/
2015-05-17 12:16:53 +00:00
public function get($i = null, $j = null)
{
2015-05-12 09:22:06 +00:00
return $this->A[$i][$j];
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* getMatrix
*
* Get a submatrix
* @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
*/
2015-05-17 12:16:53 +00:00
public function getMatrix()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
//A($i0...; $j0...)
case 'integer,integer':
2015-05-17 12:16:53 +00:00
list($i0, $j0) = $args;
2015-05-17 13:00:02 +00:00
if ($i0 >= 0) {
$m = $this->m - $i0;
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
}
if ($j0 >= 0) {
$n = $this->n - $j0;
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
}
2015-05-17 12:16:53 +00:00
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
2015-05-17 13:00:02 +00:00
for ($i = $i0; $i < $this->m; ++$i) {
for ($j = $j0; $j < $this->n; ++$j) {
2015-05-17 12:16:53 +00:00
$R->set($i, $j, $this->A[$i][$j]);
2015-05-12 09:22:06 +00:00
}
2015-05-17 12:16:53 +00:00
}
return $R;
break;
2015-05-12 09:22:06 +00:00
//A($i0...$iF; $j0...$jF)
case 'integer,integer,integer,integer':
2015-05-17 12:16:53 +00:00
list($i0, $iF, $j0, $jF) = $args;
2015-05-17 13:00:02 +00:00
if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) {
2015-05-17 16:10:35 +00:00
$m = $iF - $i0;
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
2015-05-17 13:00:02 +00:00
}
if (($jF > $j0) && ($this->n >= $jF) && ($j0 >= 0)) {
2015-05-17 16:10:35 +00:00
$n = $jF - $j0;
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
2015-05-17 13:00:02 +00:00
}
2015-05-17 12:16:53 +00:00
$R = new PHPExcel_Shared_JAMA_Matrix($m+1, $n+1);
2015-05-17 13:00:02 +00:00
for ($i = $i0; $i <= $iF; ++$i) {
for ($j = $j0; $j <= $jF; ++$j) {
2015-05-17 12:16:53 +00:00
$R->set($i - $i0, $j - $j0, $this->A[$i][$j]);
2015-05-12 09:22:06 +00:00
}
2015-05-17 12:16:53 +00:00
}
return $R;
break;
2015-05-12 09:22:06 +00:00
//$R = array of row indices; $C = array of column indices
case 'array,array':
2015-05-17 12:16:53 +00:00
list($RL, $CL) = $args;
2015-05-17 13:00:02 +00:00
if (count($RL) > 0) {
$m = count($RL);
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
}
if (count($CL) > 0) {
$n = count($CL);
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
}
2015-05-17 12:16:53 +00:00
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $m; ++$i) {
for ($j = 0; $j < $n; ++$j) {
2015-05-17 12:16:53 +00:00
$R->set($i - $i0, $j - $j0, $this->A[$RL[$i]][$CL[$j]]);
2015-05-12 09:22:06 +00:00
}
2015-05-17 12:16:53 +00:00
}
return $R;
break;
2015-05-12 09:22:06 +00:00
//$RL = array of row indices; $CL = array of column indices
case 'array,array':
2015-05-17 12:16:53 +00:00
list($RL, $CL) = $args;
2015-05-17 13:00:02 +00:00
if (count($RL) > 0) {
$m = count($RL);
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
}
if (count($CL) > 0) {
$n = count($CL);
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
}
2015-05-17 12:16:53 +00:00
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $m; ++$i) {
for ($j = 0; $j < $n; ++$j) {
2015-05-17 12:16:53 +00:00
$R->set($i, $j, $this->A[$RL[$i]][$CL[$j]]);
2015-05-12 09:22:06 +00:00
}
2015-05-17 12:16:53 +00:00
}
return $R;
break;
2015-05-12 09:22:06 +00:00
//A($i0...$iF); $CL = array of column indices
case 'integer,integer,array':
2015-05-17 12:16:53 +00:00
list($i0, $iF, $CL) = $args;
2015-05-17 13:00:02 +00:00
if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) {
$m = $iF - $i0;
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
}
if (count($CL) > 0) {
$n = count($CL);
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
}
2015-05-17 12:16:53 +00:00
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
2015-05-17 13:00:02 +00:00
for ($i = $i0; $i < $iF; ++$i) {
for ($j = 0; $j < $n; ++$j) {
2015-05-17 12:16:53 +00:00
$R->set($i - $i0, $j, $this->A[$RL[$i]][$j]);
2015-05-12 09:22:06 +00:00
}
2015-05-17 12:16:53 +00:00
}
return $R;
break;
2015-05-12 09:22:06 +00:00
//$RL = array of row indices
case 'array,integer,integer':
2015-05-17 12:16:53 +00:00
list($RL, $j0, $jF) = $args;
2015-05-17 13:00:02 +00:00
if (count($RL) > 0) {
$m = count($RL);
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
}
if (($jF >= $j0) && ($this->n >= $jF) && ($j0 >= 0)) {
$n = $jF - $j0;
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException);
}
2015-05-17 12:16:53 +00:00
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n+1);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $m; ++$i) {
for ($j = $j0; $j <= $jF; ++$j) {
2015-05-17 12:16:53 +00:00
$R->set($i, $j - $j0, $this->A[$RL[$i]][$j]);
2015-05-12 09:22:06 +00:00
}
2015-05-17 12:16:53 +00:00
}
return $R;
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* checkMatrixDimensions
*
* Is matrix B the same size?
* @param Matrix $B Matrix B
* @return boolean
*/
2015-05-17 12:16:53 +00:00
public function checkMatrixDimensions($B = null)
{
2015-05-12 09:22:06 +00:00
if ($B instanceof PHPExcel_Shared_JAMA_Matrix) {
if (($this->m == $B->getRowDimension()) && ($this->n == $B->getColumnDimension())) {
return true;
} else {
throw new PHPExcel_Calculation_Exception(self::MatrixDimensionException);
}
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
} // function checkMatrixDimensions()
/**
* set
*
* 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)
*/
2015-05-17 12:16:53 +00:00
public function set($i = null, $j = null, $c = null)
{
2015-05-12 09:22:06 +00:00
// Optimized set version just has this
$this->A[$i][$j] = $c;
} // function set()
/**
* identity
*
* Generate an identity matrix.
* @param int $m Row dimension
* @param int $n Column dimension
* @return Matrix Identity matrix
*/
2015-05-17 12:16:53 +00:00
public function identity($m = null, $n = null)
{
2015-05-12 09:22:06 +00:00
return $this->diagonal($m, $n, 1);
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* diagonal
*
* Generate a diagonal matrix
* @param int $m Row dimension
* @param int $n Column dimension
* @param mixed $c Diagonal value
* @return Matrix Diagonal matrix
*/
2015-05-17 12:16:53 +00:00
public function diagonal($m = null, $n = null, $c = 1)
{
2015-05-12 09:22:06 +00:00
$R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $m; ++$i) {
2015-05-12 09:22:06 +00:00
$R->set($i, $i, $c);
}
return $R;
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* getMatrixByRow
*
* Get a submatrix by row index/range
* @param int $i0 Initial row index
* @param int $iF Final row index
* @return Matrix Submatrix
*/
2015-05-17 12:16:53 +00:00
public function getMatrixByRow($i0 = null, $iF = null)
{
2015-05-12 09:22:06 +00:00
if (is_int($i0)) {
if (is_int($iF)) {
return $this->getMatrix($i0, 0, $iF + 1, $this->n);
} else {
return $this->getMatrix($i0, 0, $i0 + 1, $this->n);
}
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* getMatrixByCol
*
* Get a submatrix by column index/range
* @param int $i0 Initial column index
* @param int $iF Final column index
* @return Matrix Submatrix
*/
2015-05-17 12:16:53 +00:00
public function getMatrixByCol($j0 = null, $jF = null)
{
2015-05-12 09:22:06 +00:00
if (is_int($j0)) {
if (is_int($jF)) {
return $this->getMatrix(0, $j0, $this->m, $jF + 1);
} else {
return $this->getMatrix(0, $j0, $this->m, $j0 + 1);
}
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* transpose
*
* Tranpose matrix
* @return Matrix Transposed matrix
*/
2015-05-17 12:16:53 +00:00
public function transpose()
{
2015-05-12 09:22:06 +00:00
$R = new PHPExcel_Shared_JAMA_Matrix($this->n, $this->m);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-12 09:22:06 +00:00
$R->set($j, $i, $this->A[$i][$j]);
}
}
return $R;
} // function transpose()
/**
* trace
*
* Sum of diagonal elements
* @return float Sum of diagonal elements
*/
2015-05-17 12:16:53 +00:00
public function trace()
{
2015-05-12 09:22:06 +00:00
$s = 0;
$n = min($this->m, $this->n);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $n; ++$i) {
2015-05-12 09:22:06 +00:00
$s += $this->A[$i][$i];
}
return $s;
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* uminus
*
* Unary minus matrix -A
* @return Matrix Unary minus matrix
*/
2015-05-17 12:16:53 +00:00
public function uminus()
{
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* plus
*
* A + B
* @param mixed $B Matrix/Array
* @return Matrix Sum
*/
2015-05-17 12:16:53 +00:00
public function plus()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-12 09:22:06 +00:00
$M->set($i, $j, $M->get($i, $j) + $this->A[$i][$j]);
}
}
return $M;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* plusEquals
*
* A = A + B
* @param mixed $B Matrix/Array
* @return Matrix Sum
*/
2015-05-17 12:16:53 +00:00
public function plusEquals()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-17 12:16:53 +00:00
$validValues = true;
2015-05-12 09:22:06 +00:00
$value = $M->get($i, $j);
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
2015-05-17 13:00:02 +00:00
$this->A[$i][$j] = trim($this->A[$i][$j], '"');
2015-05-12 09:22:06 +00:00
$validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
}
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
2015-05-17 13:00:02 +00:00
$value = trim($value, '"');
2015-05-12 09:22:06 +00:00
$validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
}
if ($validValues) {
$this->A[$i][$j] += $value;
} else {
$this->A[$i][$j] = PHPExcel_Calculation_Functions::NaN();
}
}
}
return $this;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* minus
*
* A - B
* @param mixed $B Matrix/Array
* @return Matrix Sum
*/
2015-05-17 12:16:53 +00:00
public function minus()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-12 09:22:06 +00:00
$M->set($i, $j, $M->get($i, $j) - $this->A[$i][$j]);
}
}
return $M;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* minusEquals
*
* A = A - B
* @param mixed $B Matrix/Array
* @return Matrix Sum
*/
2015-05-17 12:16:53 +00:00
public function minusEquals()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-17 12:16:53 +00:00
$validValues = true;
2015-05-12 09:22:06 +00:00
$value = $M->get($i, $j);
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
2015-05-17 13:00:02 +00:00
$this->A[$i][$j] = trim($this->A[$i][$j], '"');
2015-05-12 09:22:06 +00:00
$validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
}
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
2015-05-17 13:00:02 +00:00
$value = trim($value, '"');
2015-05-12 09:22:06 +00:00
$validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
}
if ($validValues) {
$this->A[$i][$j] -= $value;
} else {
$this->A[$i][$j] = PHPExcel_Calculation_Functions::NaN();
}
}
}
return $this;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* arrayTimes
*
* Element-by-element multiplication
* Cij = Aij * Bij
* @param mixed $B Matrix/Array
* @return Matrix Matrix Cij
*/
2015-05-17 12:16:53 +00:00
public function arrayTimes()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-12 09:22:06 +00:00
$M->set($i, $j, $M->get($i, $j) * $this->A[$i][$j]);
}
}
return $M;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* arrayTimesEquals
*
* Element-by-element multiplication
* Aij = Aij * Bij
* @param mixed $B Matrix/Array
* @return Matrix Matrix Aij
*/
2015-05-17 12:16:53 +00:00
public function arrayTimesEquals()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-17 12:16:53 +00:00
$validValues = true;
2015-05-12 09:22:06 +00:00
$value = $M->get($i, $j);
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
2015-05-17 13:00:02 +00:00
$this->A[$i][$j] = trim($this->A[$i][$j], '"');
2015-05-12 09:22:06 +00:00
$validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
}
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
2015-05-17 13:00:02 +00:00
$value = trim($value, '"');
2015-05-12 09:22:06 +00:00
$validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
}
if ($validValues) {
$this->A[$i][$j] *= $value;
} else {
$this->A[$i][$j] = PHPExcel_Calculation_Functions::NaN();
}
}
}
return $this;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* arrayRightDivide
*
* Element-by-element right division
* A / B
* @param Matrix $B Matrix B
* @return Matrix Division result
*/
2015-05-17 12:16:53 +00:00
public function arrayRightDivide()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-17 12:16:53 +00:00
$validValues = true;
2015-05-12 09:22:06 +00:00
$value = $M->get($i, $j);
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
2015-05-17 13:00:02 +00:00
$this->A[$i][$j] = trim($this->A[$i][$j], '"');
2015-05-12 09:22:06 +00:00
$validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
}
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
2015-05-17 13:00:02 +00:00
$value = trim($value, '"');
2015-05-12 09:22:06 +00:00
$validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
}
if ($validValues) {
if ($value == 0) {
// Trap for Divide by Zero error
$M->set($i, $j, '#DIV/0!');
} else {
$M->set($i, $j, $this->A[$i][$j] / $value);
}
} else {
$M->set($i, $j, PHPExcel_Calculation_Functions::NaN());
}
}
}
return $M;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* arrayRightDivideEquals
*
* Element-by-element right division
* Aij = Aij / Bij
* @param mixed $B Matrix/Array
* @return Matrix Matrix Aij
*/
2015-05-17 12:16:53 +00:00
public function arrayRightDivideEquals()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-12 09:22:06 +00:00
$this->A[$i][$j] = $this->A[$i][$j] / $M->get($i, $j);
}
}
return $M;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* arrayLeftDivide
*
* Element-by-element Left division
* A / B
* @param Matrix $B Matrix B
* @return Matrix Division result
*/
2015-05-17 12:16:53 +00:00
public function arrayLeftDivide()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-12 09:22:06 +00:00
$M->set($i, $j, $M->get($i, $j) / $this->A[$i][$j]);
}
}
return $M;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* arrayLeftDivideEquals
*
* Element-by-element Left division
* Aij = Aij / Bij
* @param mixed $B Matrix/Array
* @return Matrix Matrix Aij
*/
2015-05-17 12:16:53 +00:00
public function arrayLeftDivideEquals()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-12 09:22:06 +00:00
$this->A[$i][$j] = $M->get($i, $j) / $this->A[$i][$j];
}
}
return $M;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* times
*
* Matrix multiplication
* @param mixed $n Matrix/Array/Scalar
* @return Matrix Product
*/
2015-05-17 12:16:53 +00:00
public function times()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$B = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
if ($this->n == $B->m) {
$C = new PHPExcel_Shared_JAMA_Matrix($this->m, $B->n);
2015-05-17 13:00:02 +00:00
for ($j = 0; $j < $B->n; ++$j) {
2015-05-17 12:16:53 +00:00
for ($k = 0; $k < $this->n; ++$k) {
$Bcolj[$k] = $B->A[$k][$j];
2015-05-12 09:22:06 +00:00
}
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
2015-05-17 12:16:53 +00:00
$Arowi = $this->A[$i];
$s = 0;
2015-05-17 13:00:02 +00:00
for ($k = 0; $k < $this->n; ++$k) {
2015-05-17 12:16:53 +00:00
$s += $Arowi[$k] * $Bcolj[$k];
2015-05-12 09:22:06 +00:00
}
2015-05-17 12:16:53 +00:00
$C->A[$i][$j] = $s;
2015-05-12 09:22:06 +00:00
}
}
2015-05-17 12:16:53 +00:00
return $C;
} else {
throw new PHPExcel_Calculation_Exception(JAMAError(MatrixDimensionMismatch));
}
break;
case 'array':
$B = new PHPExcel_Shared_JAMA_Matrix($args[0]);
if ($this->n == $B->m) {
$C = new PHPExcel_Shared_JAMA_Matrix($this->m, $B->n);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $C->m; ++$i) {
for ($j = 0; $j < $C->n; ++$j) {
2015-05-17 12:16:53 +00:00
$s = "0";
2015-05-17 13:00:02 +00:00
for ($k = 0; $k < $C->n; ++$k) {
2015-05-17 12:16:53 +00:00
$s += $this->A[$i][$k] * $B->A[$k][$j];
}
$C->A[$i][$j] = $s;
2015-05-12 09:22:06 +00:00
}
}
return $C;
2015-05-17 12:16:53 +00:00
} else {
throw new PHPExcel_Calculation_Exception(JAMAError(MatrixDimensionMismatch));
}
return $M;
break;
case 'integer':
$C = new PHPExcel_Shared_JAMA_Matrix($this->A);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $C->m; ++$i) {
for ($j = 0; $j < $C->n; ++$j) {
2015-05-17 12:16:53 +00:00
$C->A[$i][$j] *= $args[0];
}
}
return $C;
break;
2015-05-12 09:22:06 +00:00
case 'double':
2015-05-17 12:16:53 +00:00
$C = new PHPExcel_Shared_JAMA_Matrix($this->m, $this->n);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $C->m; ++$i) {
for ($j = 0; $j < $C->n; ++$j) {
2015-05-17 12:16:53 +00:00
$C->A[$i][$j] = $args[0] * $this->A[$i][$j];
2015-05-12 09:22:06 +00:00
}
2015-05-17 12:16:53 +00:00
}
return $C;
break;
2015-05-12 09:22:06 +00:00
case 'float':
2015-05-17 12:16:53 +00:00
$C = new PHPExcel_Shared_JAMA_Matrix($this->A);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $C->m; ++$i) {
for ($j = 0; $j < $C->n; ++$j) {
2015-05-17 12:16:53 +00:00
$C->A[$i][$j] *= $args[0];
2015-05-12 09:22:06 +00:00
}
2015-05-17 12:16:53 +00:00
}
return $C;
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* power
*
* A = A ^ B
* @param mixed $B Matrix/Array
* @return Matrix Sum
*/
2015-05-17 12:16:53 +00:00
public function power()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
break;
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
2015-05-17 12:16:53 +00:00
$validValues = true;
2015-05-12 09:22:06 +00:00
$value = $M->get($i, $j);
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
2015-05-17 13:00:02 +00:00
$this->A[$i][$j] = trim($this->A[$i][$j], '"');
2015-05-12 09:22:06 +00:00
$validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
}
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
2015-05-17 13:00:02 +00:00
$value = trim($value, '"');
2015-05-12 09:22:06 +00:00
$validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
}
if ($validValues) {
2015-05-13 10:27:01 +00:00
$this->A[$i][$j] = pow($this->A[$i][$j], $value);
2015-05-12 09:22:06 +00:00
} else {
$this->A[$i][$j] = PHPExcel_Calculation_Functions::NaN();
}
}
}
return $this;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* concat
*
* A = A & B
* @param mixed $B Matrix/Array
* @return Matrix Sum
*/
2015-05-17 12:16:53 +00:00
public function concat()
{
2015-05-12 09:22:06 +00:00
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
2015-05-13 10:27:01 +00:00
switch ($match) {
2015-05-12 09:22:06 +00:00
case 'object':
2015-05-17 12:16:53 +00:00
if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) {
$M = $args[0];
} else {
throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
}
2015-05-12 09:22:06 +00:00
case 'array':
2015-05-17 12:16:53 +00:00
$M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
break;
2015-05-12 09:22:06 +00:00
default:
2015-05-17 12:16:53 +00:00
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
break;
2015-05-12 09:22:06 +00:00
}
$this->checkMatrixDimensions($M);
2015-05-17 13:00:02 +00:00
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), '"');
2015-05-12 09:22:06 +00:00
}
}
return $this;
} else {
throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* Solve A*X = B.
*
* @param Matrix $B Right hand side
* @return Matrix ... Solution if A is square, least squares solution otherwise
*/
2015-05-17 12:16:53 +00:00
public function solve($B)
{
2015-05-12 09:22:06 +00:00
if ($this->m == $this->n) {
$LU = new PHPExcel_Shared_JAMA_LUDecomposition($this);
return $LU->solve($B);
} else {
$QR = new PHPExcel_Shared_JAMA_QRDecomposition($this);
return $QR->solve($B);
}
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* Matrix inverse or pseudoinverse.
*
* @return Matrix ... Inverse(A) if A is square, pseudoinverse otherwise.
*/
2015-05-17 12:16:53 +00:00
public function inverse()
{
2015-05-12 09:22:06 +00:00
return $this->solve($this->identity($this->m, $this->m));
2015-05-20 22:19:33 +00:00
}
2015-05-12 09:22:06 +00:00
/**
* det
*
* Calculate determinant
* @return float Determinant
*/
2015-05-17 12:16:53 +00:00
public function det()
{
2015-05-12 09:22:06 +00:00
$L = new PHPExcel_Shared_JAMA_LUDecomposition($this);
return $L->det();
2015-05-20 22:19:33 +00:00
}
}