1: <?php
2: 3: 4:
5:
6:
7: if (!defined('PHPEXCEL_ROOT')) {
8: 9: 10:
11: define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
12: require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
13: }
14:
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: class PHPExcel_Shared_JAMA_Matrix {
28:
29:
30: const PolymorphicArgumentException = "Invalid argument pattern for polymorphic function.";
31: const ArgumentTypeException = "Invalid argument type.";
32: const ArgumentBoundsException = "Invalid argument range.";
33: const MatrixDimensionException = "Matrix dimensions are not equal.";
34: const ArrayLengthException = "Array length must be a multiple of m.";
35:
36: 37: 38: 39: 40: 41:
42: public $A = array();
43:
44: 45: 46: 47: 48: 49:
50: private $m;
51:
52: 53: 54: 55: 56: 57:
58: private $n;
59:
60:
61: 62: 63: 64: 65:
66: public function __construct() {
67: if (func_num_args() > 0) {
68: $args = func_get_args();
69: $match = implode(",", array_map('gettype', $args));
70:
71: switch($match) {
72:
73: case 'array':
74: $this->m = count($args[0]);
75: $this->n = count($args[0][0]);
76: $this->A = $args[0];
77: break;
78:
79: case 'integer':
80: $this->m = $args[0];
81: $this->n = $args[0];
82: $this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0));
83: break;
84:
85: case 'integer,integer':
86: $this->m = $args[0];
87: $this->n = $args[1];
88: $this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0));
89: break;
90:
91: case 'array,integer':
92: $this->m = $args[1];
93: if ($this->m != 0) {
94: $this->n = count($args[0]) / $this->m;
95: } else {
96: $this->n = 0;
97: }
98: if (($this->m * $this->n) == count($args[0])) {
99: for($i = 0; $i < $this->m; ++$i) {
100: for($j = 0; $j < $this->n; ++$j) {
101: $this->A[$i][$j] = $args[0][$i + $j * $this->m];
102: }
103: }
104: } else {
105: throw new PHPExcel_Calculation_Exception(self::ArrayLengthException);
106: }
107: break;
108: default:
109: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
110: break;
111: }
112: } else {
113: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
114: }
115: }
116:
117:
118: 119: 120: 121: 122:
123: public function getArray() {
124: return $this->A;
125: }
126:
127:
128: 129: 130: 131: 132:
133: public function getRowDimension() {
134: return $this->m;
135: }
136:
137:
138: 139: 140: 141: 142:
143: public function getColumnDimension() {
144: return $this->n;
145: }
146:
147:
148: 149: 150: 151: 152: 153: 154: 155:
156: public function get($i = null, $j = null) {
157: return $this->A[$i][$j];
158: }
159:
160:
161: 162: 163: 164: 165: 166: 167: 168: 169: 170:
171: public function getMatrix() {
172: if (func_num_args() > 0) {
173: $args = func_get_args();
174: $match = implode(",", array_map('gettype', $args));
175:
176: switch($match) {
177:
178: case 'integer,integer':
179: list($i0, $j0) = $args;
180: if ($i0 >= 0) { $m = $this->m - $i0; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
181: if ($j0 >= 0) { $n = $this->n - $j0; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
182: $R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
183: for($i = $i0; $i < $this->m; ++$i) {
184: for($j = $j0; $j < $this->n; ++$j) {
185: $R->set($i, $j, $this->A[$i][$j]);
186: }
187: }
188: return $R;
189: break;
190:
191: case 'integer,integer,integer,integer':
192: list($i0, $iF, $j0, $jF) = $args;
193: if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) { $m = $iF - $i0; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
194: if (($jF > $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
195: $R = new PHPExcel_Shared_JAMA_Matrix($m+1, $n+1);
196: for($i = $i0; $i <= $iF; ++$i) {
197: for($j = $j0; $j <= $jF; ++$j) {
198: $R->set($i - $i0, $j - $j0, $this->A[$i][$j]);
199: }
200: }
201: return $R;
202: break;
203:
204: case 'array,array':
205: list($RL, $CL) = $args;
206: if (count($RL) > 0) { $m = count($RL); } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
207: if (count($CL) > 0) { $n = count($CL); } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
208: $R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
209: for($i = 0; $i < $m; ++$i) {
210: for($j = 0; $j < $n; ++$j) {
211: $R->set($i - $i0, $j - $j0, $this->A[$RL[$i]][$CL[$j]]);
212: }
213: }
214: return $R;
215: break;
216:
217: case 'array,array':
218: list($RL, $CL) = $args;
219: if (count($RL) > 0) { $m = count($RL); } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
220: if (count($CL) > 0) { $n = count($CL); } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
221: $R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
222: for($i = 0; $i < $m; ++$i) {
223: for($j = 0; $j < $n; ++$j) {
224: $R->set($i, $j, $this->A[$RL[$i]][$CL[$j]]);
225: }
226: }
227: return $R;
228: break;
229:
230: case 'integer,integer,array':
231: list($i0, $iF, $CL) = $args;
232: if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) { $m = $iF - $i0; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
233: if (count($CL) > 0) { $n = count($CL); } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
234: $R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
235: for($i = $i0; $i < $iF; ++$i) {
236: for($j = 0; $j < $n; ++$j) {
237: $R->set($i - $i0, $j, $this->A[$RL[$i]][$j]);
238: }
239: }
240: return $R;
241: break;
242:
243: case 'array,integer,integer':
244: list($RL, $j0, $jF) = $args;
245: if (count($RL) > 0) { $m = count($RL); } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
246: if (($jF >= $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentBoundsException); }
247: $R = new PHPExcel_Shared_JAMA_Matrix($m, $n+1);
248: for($i = 0; $i < $m; ++$i) {
249: for($j = $j0; $j <= $jF; ++$j) {
250: $R->set($i, $j - $j0, $this->A[$RL[$i]][$j]);
251: }
252: }
253: return $R;
254: break;
255: default:
256: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
257: break;
258: }
259: } else {
260: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
261: }
262: }
263:
264:
265: 266: 267: 268: 269: 270: 271:
272: public function checkMatrixDimensions($B = null) {
273: if ($B instanceof PHPExcel_Shared_JAMA_Matrix) {
274: if (($this->m == $B->getRowDimension()) && ($this->n == $B->getColumnDimension())) {
275: return true;
276: } else {
277: throw new PHPExcel_Calculation_Exception(self::MatrixDimensionException);
278: }
279: } else {
280: throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
281: }
282: }
283:
284:
285:
286: 287: 288: 289: 290: 291: 292: 293: 294:
295: public function set($i = null, $j = null, $c = null) {
296:
297: $this->A[$i][$j] = $c;
298: }
299:
300:
301: 302: 303: 304: 305: 306: 307: 308:
309: public function identity($m = null, $n = null) {
310: return $this->diagonal($m, $n, 1);
311: }
312:
313:
314: 315: 316: 317: 318: 319: 320: 321: 322:
323: public function diagonal($m = null, $n = null, $c = 1) {
324: $R = new PHPExcel_Shared_JAMA_Matrix($m, $n);
325: for($i = 0; $i < $m; ++$i) {
326: $R->set($i, $i, $c);
327: }
328: return $R;
329: }
330:
331:
332: 333: 334: 335: 336: 337: 338: 339:
340: public function getMatrixByRow($i0 = null, $iF = null) {
341: if (is_int($i0)) {
342: if (is_int($iF)) {
343: return $this->getMatrix($i0, 0, $iF + 1, $this->n);
344: } else {
345: return $this->getMatrix($i0, 0, $i0 + 1, $this->n);
346: }
347: } else {
348: throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
349: }
350: }
351:
352:
353: 354: 355: 356: 357: 358: 359: 360:
361: public function getMatrixByCol($j0 = null, $jF = null) {
362: if (is_int($j0)) {
363: if (is_int($jF)) {
364: return $this->getMatrix(0, $j0, $this->m, $jF + 1);
365: } else {
366: return $this->getMatrix(0, $j0, $this->m, $j0 + 1);
367: }
368: } else {
369: throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException);
370: }
371: }
372:
373:
374: 375: 376: 377: 378: 379:
380: public function transpose() {
381: $R = new PHPExcel_Shared_JAMA_Matrix($this->n, $this->m);
382: for($i = 0; $i < $this->m; ++$i) {
383: for($j = 0; $j < $this->n; ++$j) {
384: $R->set($j, $i, $this->A[$i][$j]);
385: }
386: }
387: return $R;
388: }
389:
390:
391: 392: 393: 394: 395: 396:
397: public function trace() {
398: $s = 0;
399: $n = min($this->m, $this->n);
400: for($i = 0; $i < $n; ++$i) {
401: $s += $this->A[$i][$i];
402: }
403: return $s;
404: }
405:
406:
407: 408: 409: 410: 411: 412:
413: public function uminus() {
414: }
415:
416:
417: 418: 419: 420: 421: 422: 423:
424: public function plus() {
425: if (func_num_args() > 0) {
426: $args = func_get_args();
427: $match = implode(",", array_map('gettype', $args));
428:
429: switch($match) {
430: case 'object':
431: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
432: break;
433: case 'array':
434: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
435: break;
436: default:
437: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
438: break;
439: }
440: $this->checkMatrixDimensions($M);
441: for($i = 0; $i < $this->m; ++$i) {
442: for($j = 0; $j < $this->n; ++$j) {
443: $M->set($i, $j, $M->get($i, $j) + $this->A[$i][$j]);
444: }
445: }
446: return $M;
447: } else {
448: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
449: }
450: }
451:
452:
453: 454: 455: 456: 457: 458: 459:
460: public function plusEquals() {
461: if (func_num_args() > 0) {
462: $args = func_get_args();
463: $match = implode(",", array_map('gettype', $args));
464:
465: switch($match) {
466: case 'object':
467: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
468: break;
469: case 'array':
470: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
471: break;
472: default:
473: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
474: break;
475: }
476: $this->checkMatrixDimensions($M);
477: for($i = 0; $i < $this->m; ++$i) {
478: for($j = 0; $j < $this->n; ++$j) {
479: $validValues = True;
480: $value = $M->get($i, $j);
481: if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
482: $this->A[$i][$j] = trim($this->A[$i][$j],'"');
483: $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
484: }
485: if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
486: $value = trim($value,'"');
487: $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
488: }
489: if ($validValues) {
490: $this->A[$i][$j] += $value;
491: } else {
492: $this->A[$i][$j] = PHPExcel_Calculation_Functions::NaN();
493: }
494: }
495: }
496: return $this;
497: } else {
498: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
499: }
500: }
501:
502:
503: 504: 505: 506: 507: 508: 509:
510: public function minus() {
511: if (func_num_args() > 0) {
512: $args = func_get_args();
513: $match = implode(",", array_map('gettype', $args));
514:
515: switch($match) {
516: case 'object':
517: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
518: break;
519: case 'array':
520: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
521: break;
522: default:
523: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
524: break;
525: }
526: $this->checkMatrixDimensions($M);
527: for($i = 0; $i < $this->m; ++$i) {
528: for($j = 0; $j < $this->n; ++$j) {
529: $M->set($i, $j, $M->get($i, $j) - $this->A[$i][$j]);
530: }
531: }
532: return $M;
533: } else {
534: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
535: }
536: }
537:
538:
539: 540: 541: 542: 543: 544: 545:
546: public function minusEquals() {
547: if (func_num_args() > 0) {
548: $args = func_get_args();
549: $match = implode(",", array_map('gettype', $args));
550:
551: switch($match) {
552: case 'object':
553: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
554: break;
555: case 'array':
556: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
557: break;
558: default:
559: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
560: break;
561: }
562: $this->checkMatrixDimensions($M);
563: for($i = 0; $i < $this->m; ++$i) {
564: for($j = 0; $j < $this->n; ++$j) {
565: $validValues = True;
566: $value = $M->get($i, $j);
567: if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
568: $this->A[$i][$j] = trim($this->A[$i][$j],'"');
569: $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
570: }
571: if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
572: $value = trim($value,'"');
573: $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
574: }
575: if ($validValues) {
576: $this->A[$i][$j] -= $value;
577: } else {
578: $this->A[$i][$j] = PHPExcel_Calculation_Functions::NaN();
579: }
580: }
581: }
582: return $this;
583: } else {
584: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
585: }
586: }
587:
588:
589: 590: 591: 592: 593: 594: 595: 596:
597: public function arrayTimes() {
598: if (func_num_args() > 0) {
599: $args = func_get_args();
600: $match = implode(",", array_map('gettype', $args));
601:
602: switch($match) {
603: case 'object':
604: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
605: break;
606: case 'array':
607: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
608: break;
609: default:
610: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
611: break;
612: }
613: $this->checkMatrixDimensions($M);
614: for($i = 0; $i < $this->m; ++$i) {
615: for($j = 0; $j < $this->n; ++$j) {
616: $M->set($i, $j, $M->get($i, $j) * $this->A[$i][$j]);
617: }
618: }
619: return $M;
620: } else {
621: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
622: }
623: }
624:
625:
626: 627: 628: 629: 630: 631: 632: 633:
634: public function arrayTimesEquals() {
635: if (func_num_args() > 0) {
636: $args = func_get_args();
637: $match = implode(",", array_map('gettype', $args));
638:
639: switch($match) {
640: case 'object':
641: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
642: break;
643: case 'array':
644: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
645: break;
646: default:
647: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
648: break;
649: }
650: $this->checkMatrixDimensions($M);
651: for($i = 0; $i < $this->m; ++$i) {
652: for($j = 0; $j < $this->n; ++$j) {
653: $validValues = True;
654: $value = $M->get($i, $j);
655: if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
656: $this->A[$i][$j] = trim($this->A[$i][$j],'"');
657: $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
658: }
659: if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
660: $value = trim($value,'"');
661: $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
662: }
663: if ($validValues) {
664: $this->A[$i][$j] *= $value;
665: } else {
666: $this->A[$i][$j] = PHPExcel_Calculation_Functions::NaN();
667: }
668: }
669: }
670: return $this;
671: } else {
672: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
673: }
674: }
675:
676:
677: 678: 679: 680: 681: 682: 683: 684:
685: public function arrayRightDivide() {
686: if (func_num_args() > 0) {
687: $args = func_get_args();
688: $match = implode(",", array_map('gettype', $args));
689:
690: switch($match) {
691: case 'object':
692: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
693: break;
694: case 'array':
695: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
696: break;
697: default:
698: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
699: break;
700: }
701: $this->checkMatrixDimensions($M);
702: for($i = 0; $i < $this->m; ++$i) {
703: for($j = 0; $j < $this->n; ++$j) {
704: $validValues = True;
705: $value = $M->get($i, $j);
706: if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
707: $this->A[$i][$j] = trim($this->A[$i][$j],'"');
708: $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
709: }
710: if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
711: $value = trim($value,'"');
712: $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
713: }
714: if ($validValues) {
715: if ($value == 0) {
716:
717: $M->set($i, $j, '#DIV/0!');
718: } else {
719: $M->set($i, $j, $this->A[$i][$j] / $value);
720: }
721: } else {
722: $M->set($i, $j, PHPExcel_Calculation_Functions::NaN());
723: }
724: }
725: }
726: return $M;
727: } else {
728: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
729: }
730: }
731:
732:
733: 734: 735: 736: 737: 738: 739: 740:
741: public function arrayRightDivideEquals() {
742: if (func_num_args() > 0) {
743: $args = func_get_args();
744: $match = implode(",", array_map('gettype', $args));
745:
746: switch($match) {
747: case 'object':
748: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
749: break;
750: case 'array':
751: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
752: break;
753: default:
754: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
755: break;
756: }
757: $this->checkMatrixDimensions($M);
758: for($i = 0; $i < $this->m; ++$i) {
759: for($j = 0; $j < $this->n; ++$j) {
760: $this->A[$i][$j] = $this->A[$i][$j] / $M->get($i, $j);
761: }
762: }
763: return $M;
764: } else {
765: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
766: }
767: }
768:
769:
770: 771: 772: 773: 774: 775: 776: 777:
778: public function arrayLeftDivide() {
779: if (func_num_args() > 0) {
780: $args = func_get_args();
781: $match = implode(",", array_map('gettype', $args));
782:
783: switch($match) {
784: case 'object':
785: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
786: break;
787: case 'array':
788: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
789: break;
790: default:
791: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
792: break;
793: }
794: $this->checkMatrixDimensions($M);
795: for($i = 0; $i < $this->m; ++$i) {
796: for($j = 0; $j < $this->n; ++$j) {
797: $M->set($i, $j, $M->get($i, $j) / $this->A[$i][$j]);
798: }
799: }
800: return $M;
801: } else {
802: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
803: }
804: }
805:
806:
807: 808: 809: 810: 811: 812: 813: 814:
815: public function arrayLeftDivideEquals() {
816: if (func_num_args() > 0) {
817: $args = func_get_args();
818: $match = implode(",", array_map('gettype', $args));
819:
820: switch($match) {
821: case 'object':
822: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
823: break;
824: case 'array':
825: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
826: break;
827: default:
828: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
829: break;
830: }
831: $this->checkMatrixDimensions($M);
832: for($i = 0; $i < $this->m; ++$i) {
833: for($j = 0; $j < $this->n; ++$j) {
834: $this->A[$i][$j] = $M->get($i, $j) / $this->A[$i][$j];
835: }
836: }
837: return $M;
838: } else {
839: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
840: }
841: }
842:
843:
844: 845: 846: 847: 848: 849: 850:
851: public function times() {
852: if (func_num_args() > 0) {
853: $args = func_get_args();
854: $match = implode(",", array_map('gettype', $args));
855:
856: switch($match) {
857: case 'object':
858: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $B = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
859: if ($this->n == $B->m) {
860: $C = new PHPExcel_Shared_JAMA_Matrix($this->m, $B->n);
861: for($j = 0; $j < $B->n; ++$j) {
862: for ($k = 0; $k < $this->n; ++$k) {
863: $Bcolj[$k] = $B->A[$k][$j];
864: }
865: for($i = 0; $i < $this->m; ++$i) {
866: $Arowi = $this->A[$i];
867: $s = 0;
868: for($k = 0; $k < $this->n; ++$k) {
869: $s += $Arowi[$k] * $Bcolj[$k];
870: }
871: $C->A[$i][$j] = $s;
872: }
873: }
874: return $C;
875: } else {
876: throw new PHPExcel_Calculation_Exception(JAMAError(MatrixDimensionMismatch));
877: }
878: break;
879: case 'array':
880: $B = new PHPExcel_Shared_JAMA_Matrix($args[0]);
881: if ($this->n == $B->m) {
882: $C = new PHPExcel_Shared_JAMA_Matrix($this->m, $B->n);
883: for($i = 0; $i < $C->m; ++$i) {
884: for($j = 0; $j < $C->n; ++$j) {
885: $s = "0";
886: for($k = 0; $k < $C->n; ++$k) {
887: $s += $this->A[$i][$k] * $B->A[$k][$j];
888: }
889: $C->A[$i][$j] = $s;
890: }
891: }
892: return $C;
893: } else {
894: throw new PHPExcel_Calculation_Exception(JAMAError(MatrixDimensionMismatch));
895: }
896: return $M;
897: break;
898: case 'integer':
899: $C = new PHPExcel_Shared_JAMA_Matrix($this->A);
900: for($i = 0; $i < $C->m; ++$i) {
901: for($j = 0; $j < $C->n; ++$j) {
902: $C->A[$i][$j] *= $args[0];
903: }
904: }
905: return $C;
906: break;
907: case 'double':
908: $C = new PHPExcel_Shared_JAMA_Matrix($this->m, $this->n);
909: for($i = 0; $i < $C->m; ++$i) {
910: for($j = 0; $j < $C->n; ++$j) {
911: $C->A[$i][$j] = $args[0] * $this->A[$i][$j];
912: }
913: }
914: return $C;
915: break;
916: case 'float':
917: $C = new PHPExcel_Shared_JAMA_Matrix($this->A);
918: for($i = 0; $i < $C->m; ++$i) {
919: for($j = 0; $j < $C->n; ++$j) {
920: $C->A[$i][$j] *= $args[0];
921: }
922: }
923: return $C;
924: break;
925: default:
926: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
927: break;
928: }
929: } else {
930: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
931: }
932: }
933:
934:
935: 936: 937: 938: 939: 940: 941:
942: public function power() {
943: if (func_num_args() > 0) {
944: $args = func_get_args();
945: $match = implode(",", array_map('gettype', $args));
946:
947: switch($match) {
948: case 'object':
949: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
950: break;
951: case 'array':
952: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
953: break;
954: default:
955: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
956: break;
957: }
958: $this->checkMatrixDimensions($M);
959: for($i = 0; $i < $this->m; ++$i) {
960: for($j = 0; $j < $this->n; ++$j) {
961: $validValues = True;
962: $value = $M->get($i, $j);
963: if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
964: $this->A[$i][$j] = trim($this->A[$i][$j],'"');
965: $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
966: }
967: if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
968: $value = trim($value,'"');
969: $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
970: }
971: if ($validValues) {
972: $this->A[$i][$j] = pow($this->A[$i][$j],$value);
973: } else {
974: $this->A[$i][$j] = PHPExcel_Calculation_Functions::NaN();
975: }
976: }
977: }
978: return $this;
979: } else {
980: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
981: }
982: }
983:
984:
985: 986: 987: 988: 989: 990: 991:
992: public function concat() {
993: if (func_num_args() > 0) {
994: $args = func_get_args();
995: $match = implode(",", array_map('gettype', $args));
996:
997: switch($match) {
998: case 'object':
999: if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ArgumentTypeException); }
1000: case 'array':
1001: $M = new PHPExcel_Shared_JAMA_Matrix($args[0]);
1002: break;
1003: default:
1004: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
1005: break;
1006: }
1007: $this->checkMatrixDimensions($M);
1008: for($i = 0; $i < $this->m; ++$i) {
1009: for($j = 0; $j < $this->n; ++$j) {
1010: $this->A[$i][$j] = trim($this->A[$i][$j],'"').trim($M->get($i, $j),'"');
1011: }
1012: }
1013: return $this;
1014: } else {
1015: throw new PHPExcel_Calculation_Exception(self::PolymorphicArgumentException);
1016: }
1017: }
1018:
1019:
1020: 1021: 1022: 1023: 1024: 1025:
1026: public function solve($B) {
1027: if ($this->m == $this->n) {
1028: $LU = new PHPExcel_Shared_JAMA_LUDecomposition($this);
1029: return $LU->solve($B);
1030: } else {
1031: $QR = new QRDecomposition($this);
1032: return $QR->solve($B);
1033: }
1034: }
1035:
1036:
1037: 1038: 1039: 1040: 1041:
1042: public function inverse() {
1043: return $this->solve($this->identity($this->m, $this->m));
1044: }
1045:
1046:
1047: 1048: 1049: 1050: 1051: 1052:
1053: public function det() {
1054: $L = new PHPExcel_Shared_JAMA_LUDecomposition($this);
1055: return $L->det();
1056: }
1057:
1058:
1059: }
1060: