1160 lines
40 KiB
PHP
Executable File
1160 lines
40 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* @package JAMA
|
|
*/
|
|
|
|
/** PHPExcel root directory */
|
|
if (!defined('PHPEXCEL_ROOT')) {
|
|
/**
|
|
* @ignore
|
|
*/
|
|
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
|
|
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
|
}
|
|
|
|
|
|
/*
|
|
* Matrix class
|
|
*
|
|
* @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/
|
|
*/
|
|
class PHPExcel_Shared_JAMA_Matrix
|
|
{
|
|
const POLYMORPHIC_ARGUMENT_EXCEPTION = "Invalid argument pattern for polymorphic function.";
|
|
const ARGUMENT_TYPE_EXCEPTION = "Invalid argument type.";
|
|
const ARGUMENT_BOUNDS_EXCEPTION = "Invalid argument range.";
|
|
const MATRIX_DIMENSION_EXCEPTION = "Matrix dimensions are not equal.";
|
|
const ARRAY_LENGTH_EXCEPTION = "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.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
if (func_num_args() > 0) {
|
|
$args = func_get_args();
|
|
$match = implode(",", array_map('gettype', $args));
|
|
|
|
switch ($match) {
|
|
//Rectangular matrix - m x n initialized from 2D array
|
|
case 'array':
|
|
$this->m = count($args[0]);
|
|
$this->n = count($args[0][0]);
|
|
$this->A = $args[0];
|
|
break;
|
|
//Square matrix - n x n
|
|
case 'integer':
|
|
$this->m = $args[0];
|
|
$this->n = $args[0];
|
|
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0));
|
|
break;
|
|
//Rectangular matrix - m x n
|
|
case 'integer,integer':
|
|
$this->m = $args[0];
|
|
$this->n = $args[1];
|
|
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0));
|
|
break;
|
|
//Rectangular matrix - m x n initialized from packed array
|
|
case 'array,integer':
|
|
$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])) {
|
|
for ($i = 0; $i < $this->m; ++$i) {
|
|
for ($j = 0; $j < $this->n; ++$j) {
|
|
$this->A[$i][$j] = $args[0][$i + $j * $this->m];
|
|
}
|
|
}
|
|
} else {
|
|
throw new PHPExcel_Calculation_Exception(self::ARRAY_LENGTH_EXCEPTION);
|
|
}
|
|
break;
|
|
default:
|
|
throw new PHPExcel_Calculation_Exception(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
|
|
break;
|
|
}
|
|
} else {
|
|
throw new PHPExcel_Calculation_Exception(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* getArray
|
|
*
|
|
* @return array Matrix array
|
|
*/
|
|
public function getArray()
|
|