6 define(
'RAND_MAX', mt_getrandmax());
10 if (!defined(
'PHPEXCEL_ROOT')) {
14 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../../');
17 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/JAMA/utils/Error.php';
18 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/JAMA/utils/Maths.php';
19 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/JAMA/CholeskyDecomposition.php';
20 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/JAMA/LUDecomposition.php';
21 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/JAMA/QRDecomposition.php';
22 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/JAMA/EigenvalueDecomposition.php';
23 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/JAMA/SingularValueDecomposition.php';
69 if (func_num_args() > 0) {
70 $args = func_get_args();
71 $match = implode(
",", array_map(
'gettype', $args));
78 $this->A = array_fill(0, $this->m, array_fill(0, $this->
n, 0));
81 case 'integer,integer':
84 $this->A = array_fill(0, $this->m, array_fill(0, $this->
n, 0));
87 case 'integer,integer,integer':
90 $this->A = array_fill(0, $this->m, array_fill(0, $this->
n, $args[2]));
93 case 'integer,integer,double':
96 $this->A = array_fill(0, $this->m, array_fill(0, $this->
n, $args[2]));
100 $this->m = count($args[0]);
101 $this->
n = count($args[0][0]);
105 case 'array,integer,integer':
111 case 'array,integer':
118 if (($this->m * $this->
n) == count($args[0])) {
121 $this->A[$i][$j] = $args[0][$i + $j *
$this->m];
166 $this->m = count(
$A);
167 $this->
n = count(
$A[0]);
168 $newCopyMatrix =
new Matrix($this->m, $this->
n);
170 if (count(
$A[$i]) != $this->
n) {
174 $newCopyMatrix->A[$i][$j] =
$A[$i][$j];
177 return $newCopyMatrix;
191 array_push($P, $this->A[$j][$i]);
208 array_push($P, $this->A[$i][$j]);
243 public function get($i = null, $j = null) {
244 return $this->A[$i][$j];
259 if (func_num_args() > 0) {
260 $args = func_get_args();
261 $match = implode(
",", array_map(
'gettype', $args));
265 case 'integer,integer':
266 list($i0, $j0) = $args;
270 for($i = $i0; $i <
$this->m; ++$i) {
271 for($j = $j0; $j <
$this->n; ++$j) {
272 $R->set($i, $j, $this->A[$i][$j]);
278 case 'integer,integer,integer,integer':
279 list($i0, $iF, $j0, $jF) = $args;
283 for($i = $i0; $i <= $iF; ++$i) {
284 for($j = $j0; $j <= $jF; ++$j) {
285 $R->set($i - $i0, $j - $j0, $this->A[$i][$j]);
292 list($RL, $CL) = $args;
296 for($i = 0; $i <
$m; ++$i) {
297 for($j = 0; $j <
$n; ++$j) {
298 $R->set($i - $i0, $j - $j0, $this->A[$RL[$i]][$CL[$j]]);
305 list($RL, $CL) = $args;
309 for($i = 0; $i <
$m; ++$i) {
310 for($j = 0; $j <
$n; ++$j) {
311 $R->set($i, $j, $this->A[$RL[$i]][$CL[$j]]);
317 case 'integer,integer,array':
318 list($i0, $iF, $CL) = $args;
322 for($i = $i0; $i < $iF; ++$i) {
323 for($j = 0; $j <
$n; ++$j) {
324 $R->set($i - $i0, $j, $this->A[$RL[$i]][$j]);
330 case 'array,integer,integer':
331 list($RL, $j0, $jF) = $args;
335 for($i = 0; $i <
$m; ++$i) {
336 for($j = $j0; $j <= $jF; ++$j) {
337 $R->set($i, $j - $j0, $this->A[$RL[$i]][$j]);
363 if (func_num_args() > 0) {
364 $args = func_get_args();
365 $match = implode(
",", array_map(
'gettype', $args));
368 case 'integer,integer,object':
372 for($i = $i0; $i < $i0 + $M->m; ++$i) {
373 for($j = $j0; $j < $j0 + $M->n; ++$j) {
374 $this->A[$i][$j] = $M->get($i - $i0, $j - $j0);
378 case 'integer,integer,array':
379 $M =
new Matrix($args[2]);
382 for($i = $i0; $i < $i0 + $M->m; ++$i) {
383 for($j = $j0; $j < $j0 + $M->n; ++$j) {
384 $this->A[$i][$j] = $M->get($i - $i0, $j - $j0);
406 if ($B instanceof
Matrix) {
407 if (($this->m == $B->getRowDimension()) && ($this->
n == $B->getColumnDimension())) {
428 public function set($i = null, $j = null, $c = null) {
430 $this->A[$i][$j] = $c;
470 for($i = 0; $i <
$m; ++$i) {
487 if (is_int(
$m) && is_int(
$n) && is_numeric($c)) {
504 if (is_int(
$m) && is_int(
$n) && is_numeric($a) && is_numeric($b)) {
506 for($i = 0; $i <
$m; ++$i) {
507 for($j = 0; $j <
$n; ++$j) {
508 $R->set($i, $j, mt_rand($a, $b));
525 return $this->getRowPacked();
540 return $this->
getMatrix($i0, 0, $iF + 1, $this->
n);
542 return $this->
getMatrix($i0, 0, $i0 + 1, $this->
n);
561 return $this->
getMatrix(0, $j0, $this->m, $jF + 1);
563 return $this->
getMatrix(0, $j0, $this->m, $j0 + 1);
578 $R =
new Matrix($this->
n, $this->m);
581 $R->set($j, $i, $this->A[$i][$j]);
599 $s += abs($this->A[$i][$j]);
601 $r = ($r > $s) ? $r : $s;
628 $s += abs($this->A[$i][$j]);
630 $r = ($r > $s) ? $r : $s;
683 $n = min($this->m, $this->
n);
684 for($i = 0; $i <
$n; ++$i) {
685 $s += $this->A[$i][$i];
709 if (func_num_args() > 0) {
710 $args = func_get_args();
711 $match = implode(
",", array_map(
'gettype', $args));
718 $M =
new Matrix($args[0]);
727 $M->set($i, $j, $M->get($i, $j) + $this->A[$i][$j]);
745 if (func_num_args() > 0) {
746 $args = func_get_args();
747 $match = implode(
",", array_map(
'gettype', $args));
754 $M =
new Matrix($args[0]);
763 $this->A[$i][$j] += $M->get($i, $j);
781 if (func_num_args() > 0) {
782 $args = func_get_args();
783 $match = implode(
",", array_map(
'gettype', $args));
790 $M =
new Matrix($args[0]);
799 $M->set($i, $j, $M->get($i, $j) - $this->A[$i][$j]);
817 if (func_num_args() > 0) {
818 $args = func_get_args();
819 $match = implode(
",", array_map(
'gettype', $args));
826 $M =
new Matrix($args[0]);
835 $this->A[$i][$j] -= $M->get($i, $j);
854 if (func_num_args() > 0) {
855 $args = func_get_args();
856 $match = implode(
",", array_map(
'gettype', $args));
863 $M =
new Matrix($args[0]);
872 $M->set($i, $j, $M->get($i, $j) * $this->A[$i][$j]);
891 if (func_num_args() > 0) {
892 $args = func_get_args();
893 $match = implode(
",", array_map(
'gettype', $args));
900 $M =
new Matrix($args[0]);
909 $this->A[$i][$j] *= $M->get($i, $j);
928 if (func_num_args() > 0) {
929 $args = func_get_args();
930 $match = implode(
",", array_map(
'gettype', $args));
937 $M =
new Matrix($args[0]);
946 $M->set($i, $j, $this->A[$i][$j] / $M->get($i, $j));
965 if (func_num_args() > 0) {
966 $args = func_get_args();
967 $match = implode(
",", array_map(
'gettype', $args));
974 $M =
new Matrix($args[0]);
983 $this->A[$i][$j] = $this->A[$i][$j] / $M->get($i, $j);
1002 if (func_num_args() > 0) {
1003 $args = func_get_args();
1004 $match = implode(
",", array_map(
'gettype', $args));
1011 $M =
new Matrix($args[0]);
1020 $M->set($i, $j, $M->get($i, $j) / $this->A[$i][$j]);
1039 if (func_num_args() > 0) {
1040 $args = func_get_args();
1041 $match = implode(
",", array_map(
'gettype', $args));
1048 $M =
new Matrix($args[0]);
1057 $this->A[$i][$j] = $M->get($i, $j) / $this->A[$i][$j];
1075 if (func_num_args() > 0) {
1076 $args = func_get_args();
1077 $match = implode(
",", array_map(
'gettype', $args));
1082 if ($this->
n == $B->m) {
1083 $C =
new Matrix($this->m, $B->n);
1084 for($j = 0; $j < $B->n; ++$j) {
1085 for ($k = 0; $k <
$this->n; ++$k) {
1086 $Bcolj[$k] = $B->A[$k][$j];
1089 $Arowi = $this->A[$i];
1092 $s += $Arowi[$k] * $Bcolj[$k];
1103 $B =
new Matrix($args[0]);
1104 if ($this->
n == $B->m) {
1105 $C =
new Matrix($this->m, $B->n);
1106 for($i = 0; $i < $C->m; ++$i) {
1107 for($j = 0; $j < $C->n; ++$j) {
1109 for($k = 0; $k < $C->n; ++$k) {
1110 $s += $this->A[$i][$k] * $B->A[$k][$j];
1122 $C =
new Matrix($this->A);
1123 for($i = 0; $i < $C->m; ++$i) {
1124 for($j = 0; $j < $C->n; ++$j) {
1125 $C->A[$i][$j] *= $args[0];
1131 $C =
new Matrix($this->m, $this->
n);
1132 for($i = 0; $i < $C->m; ++$i) {
1133 for($j = 0; $j < $C->n; ++$j) {
1134 $C->A[$i][$j] = $args[0] * $this->A[$i][$j];
1140 $C =
new Matrix($this->A);
1141 for($i = 0; $i < $C->m; ++$i) {
1142 for($j = 0; $j < $C->n; ++$j) {
1143 $C->A[$i][$j] *= $args[0];
1166 if (func_num_args() > 0) {
1167 $args = func_get_args();
1168 $match = implode(
",", array_map(
'gettype', $args));
1175 $M =
new Matrix($args[0]);
1184 $this->A[$i][$j] = pow($this->A[$i][$j],$M->get($i, $j));
1202 if (func_num_args() > 0) {
1203 $args = func_get_args();
1204 $match = implode(
",", array_map(
'gettype', $args));
1210 $M =
new Matrix($args[0]);
1220 $this->A[$i][$j] = trim($this->A[$i][$j],
'"').trim($M->get($i, $j),
'"');
1292 if ($this->m == $this->
n) {
1294 return $LU->solve($B);
1297 return $QR->solve($B);
1329 public function mprint(
$A, $format=
"%01.2f", $width=2) {
1332 $spacing = str_repeat(
' ',$width);
1334 for ($i = 0; $i <
$m; ++$i) {
1335 for ($j = 0; $j <
$n; ++$j) {
1336 $formatted = sprintf($format,
$A[$i][$j]);
1337 echo $formatted.$spacing;
1350 print(
'<table style="background-color:#eee;">');
1354 print(
'<td style="background-color:#fff;border:1px solid #000;padding:2px;text-align:center;vertical-align:middle;">' . $this->A[$i][$j] .
'</td>');