| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233 | 
							- <?php
 
- namespace PhpOffice\PhpSpreadsheet\Shared\JAMA;
 
- use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
 
- use PhpOffice\PhpSpreadsheet\Calculation\Functions;
 
- use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
 
- /**
 
-  * Matrix class.
 
-  *
 
-  * @author Paul Meagher
 
-  * @author Michael Bommarito
 
-  * @author Lukasz Karapuda
 
-  * @author Bartek Matosiuk
 
-  *
 
-  * @version 1.8
 
-  *
 
-  * @see https://math.nist.gov/javanumerics/jama/
 
-  */
 
- class 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.';
 
-     const MATRIX_SPD_EXCEPTION = 'Can only perform operation on symmetric positive definite matrix.';
 
-     /**
 
-      * Matrix storage.
 
-      *
 
-      * @var array
 
-      */
 
-     public $A = [];
 
-     /**
 
-      * Matrix row dimension.
 
-      *
 
-      * @var int
 
-      */
 
-     private $m;
 
-     /**
 
-      * Matrix column dimension.
 
-      *
 
-      * @var int
 
-      */
 
-     private $n;
 
-     /**
 
-      * Polymorphic constructor.
 
-      *
 
-      * As PHP has no support for polymorphic constructors, we use tricks to make 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(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $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 CalculationException(self::ARRAY_LENGTH_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-         } else {
 
-             throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-         }
 
-     }
 
-     /**
 
-      * getArray.
 
-      *
 
-      * @return array Matrix array
 
-      */
 
-     public function getArray()
 
-     {
 
-         return $this->A;
 
-     }
 
-     /**
 
-      * getRowDimension.
 
-      *
 
-      * @return int Row dimension
 
-      */
 
-     public function getRowDimension()
 
-     {
 
-         return $this->m;
 
-     }
 
-     /**
 
-      * getColumnDimension.
 
-      *
 
-      * @return int Column dimension
 
-      */
 
-     public function getColumnDimension()
 
-     {
 
-         return $this->n;
 
-     }
 
-     /**
 
-      * 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)
 
-      */
 
-     public function get($i = null, $j = null)
 
-     {
 
-         return $this->A[$i][$j];
 
-     }
 
-     /**
 
-      * 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
 
-      */
 
-     public function getMatrix(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 //A($i0...; $j0...)
 
-                 case 'integer,integer':
 
-                     list($i0, $j0) = $args;
 
-                     if ($i0 >= 0) {
 
-                         $m = $this->m - $i0;
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION);
 
-                     }
 
-                     if ($j0 >= 0) {
 
-                         $n = $this->n - $j0;
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION);
 
-                     }
 
-                     $R = new self($m, $n);
 
-                     for ($i = $i0; $i < $this->m; ++$i) {
 
-                         for ($j = $j0; $j < $this->n; ++$j) {
 
-                             $R->set($i, $j, $this->A[$i][$j]);
 
-                         }
 
-                     }
 
-                     return $R;
 
-                     break;
 
-                 //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 CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION);
 
-                     }
 
-                     if (($jF > $j0) && ($this->n >= $jF) && ($j0 >= 0)) {
 
-                         $n = $jF - $j0;
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION);
 
-                     }
 
-                     $R = new self($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]);
 
-                         }
 
-                     }
 
-                     return $R;
 
-                     break;
 
-                 //$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 CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION);
 
-                     }
 
-                     if (count($CL) > 0) {
 
-                         $n = count($CL);
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION);
 
-                     }
 
-                     $R = new self($m, $n);
 
-                     for ($i = 0; $i < $m; ++$i) {
 
-                         for ($j = 0; $j < $n; ++$j) {
 
-                             $R->set($i, $j, $this->A[$RL[$i]][$CL[$j]]);
 
-                         }
 
-                     }
 
-                     return $R;
 
-                     break;
 
-                 //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 CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION);
 
-                     }
 
-                     if (count($CL) > 0) {
 
-                         $n = count($CL);
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION);
 
-                     }
 
-                     $R = new self($m, $n);
 
-                     for ($i = $i0; $i < $iF; ++$i) {
 
-                         for ($j = 0; $j < $n; ++$j) {
 
-                             $R->set($i - $i0, $j, $this->A[$i][$CL[$j]]);
 
-                         }
 
-                     }
 
-                     return $R;
 
-                     break;
 
-                 //$RL = array of row indices
 
-                 case 'array,integer,integer':
 
-                     list($RL, $j0, $jF) = $args;
 
-                     if (count($RL) > 0) {
 
-                         $m = count($RL);
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION);
 
-                     }
 
-                     if (($jF >= $j0) && ($this->n >= $jF) && ($j0 >= 0)) {
 
-                         $n = $jF - $j0;
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION);
 
-                     }
 
-                     $R = new self($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]);
 
-                         }
 
-                     }
 
-                     return $R;
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-         } else {
 
-             throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-         }
 
-     }
 
-     /**
 
-      * checkMatrixDimensions.
 
-      *
 
-      *    Is matrix B the same size?
 
-      *
 
-      * @param Matrix $B Matrix B
 
-      *
 
-      * @return bool
 
-      */
 
-     public function checkMatrixDimensions($B = null)
 
-     {
 
-         if ($B instanceof self) {
 
-             if (($this->m == $B->getRowDimension()) && ($this->n == $B->getColumnDimension())) {
 
-                 return true;
 
-             }
 
-             throw new CalculationException(self::MATRIX_DIMENSION_EXCEPTION);
 
-         }
 
-         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-     }
 
-     //    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)
 
-      */
 
-     public function set($i = null, $j = null, $c = null)
 
-     {
 
-         // 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
 
-      */
 
-     public function identity($m = null, $n = null)
 
-     {
 
-         return $this->diagonal($m, $n, 1);
 
-     }
 
-     /**
 
-      * diagonal.
 
-      *
 
-      *    Generate a diagonal matrix
 
-      *
 
-      * @param int $m Row dimension
 
-      * @param int $n Column dimension
 
-      * @param mixed $c Diagonal value
 
-      *
 
-      * @return Matrix Diagonal matrix
 
-      */
 
-     public function diagonal($m = null, $n = null, $c = 1)
 
-     {
 
-         $R = new self($m, $n);
 
-         for ($i = 0; $i < $m; ++$i) {
 
-             $R->set($i, $i, $c);
 
-         }
 
-         return $R;
 
-     }
 
-     /**
 
-      * getMatrixByRow.
 
-      *
 
-      *    Get a submatrix by row index/range
 
-      *
 
-      * @param int $i0 Initial row index
 
-      * @param int $iF Final row index
 
-      *
 
-      * @return Matrix Submatrix
 
-      */
 
-     public function getMatrixByRow($i0 = null, $iF = null)
 
-     {
 
-         if (is_int($i0)) {
 
-             if (is_int($iF)) {
 
-                 return $this->getMatrix($i0, 0, $iF + 1, $this->n);
 
-             }
 
-             return $this->getMatrix($i0, 0, $i0 + 1, $this->n);
 
-         }
 
-         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-     }
 
-     /**
 
-      * getMatrixByCol.
 
-      *
 
-      *    Get a submatrix by column index/range
 
-      *
 
-      * @param int $j0 Initial column index
 
-      * @param int $jF Final column index
 
-      *
 
-      * @return Matrix Submatrix
 
-      */
 
-     public function getMatrixByCol($j0 = null, $jF = null)
 
-     {
 
-         if (is_int($j0)) {
 
-             if (is_int($jF)) {
 
-                 return $this->getMatrix(0, $j0, $this->m, $jF + 1);
 
-             }
 
-             return $this->getMatrix(0, $j0, $this->m, $j0 + 1);
 
-         }
 
-         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-     }
 
-     /**
 
-      * transpose.
 
-      *
 
-      *    Tranpose matrix
 
-      *
 
-      * @return Matrix Transposed matrix
 
-      */
 
-     public function transpose()
 
-     {
 
-         $R = new self($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]);
 
-             }
 
-         }
 
-         return $R;
 
-     }
 
-     //    function transpose()
 
-     /**
 
-      * trace.
 
-      *
 
-      *    Sum of diagonal elements
 
-      *
 
-      * @return float Sum of diagonal elements
 
-      */
 
-     public function trace()
 
-     {
 
-         $s = 0;
 
-         $n = min($this->m, $this->n);
 
-         for ($i = 0; $i < $n; ++$i) {
 
-             $s += $this->A[$i][$i];
 
-         }
 
-         return $s;
 
-     }
 
-     /**
 
-      * uminus.
 
-      *
 
-      *    Unary minus matrix -A
 
-      *
 
-      * @return Matrix Unary minus matrix
 
-      */
 
-     public function uminus()
 
-     {
 
-     }
 
-     /**
 
-      * plus.
 
-      *
 
-      *    A + B
 
-      *
 
-      * @param mixed $B Matrix/Array
 
-      *
 
-      * @return Matrix Sum
 
-      */
 
-     public function plus(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $M->set($i, $j, $M->get($i, $j) + $this->A[$i][$j]);
 
-                 }
 
-             }
 
-             return $M;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * plusEquals.
 
-      *
 
-      *    A = A + B
 
-      *
 
-      * @param mixed $B Matrix/Array
 
-      *
 
-      * @return Matrix Sum
 
-      */
 
-     public function plusEquals(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $validValues = true;
 
-                     $value = $M->get($i, $j);
 
-                     if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
 
-                         $this->A[$i][$j] = trim($this->A[$i][$j], '"');
 
-                         $validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
 
-                     }
 
-                     if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
 
-                         $value = trim($value, '"');
 
-                         $validValues &= StringHelper::convertToNumberIfFraction($value);
 
-                     }
 
-                     if ($validValues) {
 
-                         $this->A[$i][$j] += $value;
 
-                     } else {
 
-                         $this->A[$i][$j] = Functions::NAN();
 
-                     }
 
-                 }
 
-             }
 
-             return $this;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * minus.
 
-      *
 
-      *    A - B
 
-      *
 
-      * @param mixed $B Matrix/Array
 
-      *
 
-      * @return Matrix Sum
 
-      */
 
-     public function minus(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $M->set($i, $j, $M->get($i, $j) - $this->A[$i][$j]);
 
-                 }
 
-             }
 
-             return $M;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * minusEquals.
 
-      *
 
-      *    A = A - B
 
-      *
 
-      * @param mixed $B Matrix/Array
 
-      *
 
-      * @return Matrix Sum
 
-      */
 
-     public function minusEquals(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $validValues = true;
 
-                     $value = $M->get($i, $j);
 
-                     if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
 
-                         $this->A[$i][$j] = trim($this->A[$i][$j], '"');
 
-                         $validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
 
-                     }
 
-                     if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
 
-                         $value = trim($value, '"');
 
-                         $validValues &= StringHelper::convertToNumberIfFraction($value);
 
-                     }
 
-                     if ($validValues) {
 
-                         $this->A[$i][$j] -= $value;
 
-                     } else {
 
-                         $this->A[$i][$j] = Functions::NAN();
 
-                     }
 
-                 }
 
-             }
 
-             return $this;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * arrayTimes.
 
-      *
 
-      *    Element-by-element multiplication
 
-      *    Cij = Aij * Bij
 
-      *
 
-      * @param mixed $B Matrix/Array
 
-      *
 
-      * @return Matrix Matrix Cij
 
-      */
 
-     public function arrayTimes(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $M->set($i, $j, $M->get($i, $j) * $this->A[$i][$j]);
 
-                 }
 
-             }
 
-             return $M;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * arrayTimesEquals.
 
-      *
 
-      *    Element-by-element multiplication
 
-      *    Aij = Aij * Bij
 
-      *
 
-      * @param mixed $B Matrix/Array
 
-      *
 
-      * @return Matrix Matrix Aij
 
-      */
 
-     public function arrayTimesEquals(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $validValues = true;
 
-                     $value = $M->get($i, $j);
 
-                     if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
 
-                         $this->A[$i][$j] = trim($this->A[$i][$j], '"');
 
-                         $validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
 
-                     }
 
-                     if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
 
-                         $value = trim($value, '"');
 
-                         $validValues &= StringHelper::convertToNumberIfFraction($value);
 
-                     }
 
-                     if ($validValues) {
 
-                         $this->A[$i][$j] *= $value;
 
-                     } else {
 
-                         $this->A[$i][$j] = Functions::NAN();
 
-                     }
 
-                 }
 
-             }
 
-             return $this;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * arrayRightDivide.
 
-      *
 
-      *    Element-by-element right division
 
-      *    A / B
 
-      *
 
-      * @param Matrix $B Matrix B
 
-      *
 
-      * @return Matrix Division result
 
-      */
 
-     public function arrayRightDivide(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $validValues = true;
 
-                     $value = $M->get($i, $j);
 
-                     if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
 
-                         $this->A[$i][$j] = trim($this->A[$i][$j], '"');
 
-                         $validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
 
-                     }
 
-                     if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
 
-                         $value = trim($value, '"');
 
-                         $validValues &= StringHelper::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, Functions::NAN());
 
-                     }
 
-                 }
 
-             }
 
-             return $M;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * arrayRightDivideEquals.
 
-      *
 
-      *    Element-by-element right division
 
-      *    Aij = Aij / Bij
 
-      *
 
-      * @param mixed $B Matrix/Array
 
-      *
 
-      * @return Matrix Matrix Aij
 
-      */
 
-     public function arrayRightDivideEquals(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $this->A[$i][$j] = $this->A[$i][$j] / $M->get($i, $j);
 
-                 }
 
-             }
 
-             return $M;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * arrayLeftDivide.
 
-      *
 
-      *    Element-by-element Left division
 
-      *    A / B
 
-      *
 
-      * @param Matrix $B Matrix B
 
-      *
 
-      * @return Matrix Division result
 
-      */
 
-     public function arrayLeftDivide(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $M->set($i, $j, $M->get($i, $j) / $this->A[$i][$j]);
 
-                 }
 
-             }
 
-             return $M;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * arrayLeftDivideEquals.
 
-      *
 
-      *    Element-by-element Left division
 
-      *    Aij = Aij / Bij
 
-      *
 
-      * @param mixed $B Matrix/Array
 
-      *
 
-      * @return Matrix Matrix Aij
 
-      */
 
-     public function arrayLeftDivideEquals(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $this->A[$i][$j] = $M->get($i, $j) / $this->A[$i][$j];
 
-                 }
 
-             }
 
-             return $M;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * times.
 
-      *
 
-      *    Matrix multiplication
 
-      *
 
-      * @param mixed $n Matrix/Array/Scalar
 
-      *
 
-      * @return Matrix Product
 
-      */
 
-     public function times(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $B = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     if ($this->n == $B->m) {
 
-                         $C = new self($this->m, $B->n);
 
-                         for ($j = 0; $j < $B->n; ++$j) {
 
-                             $Bcolj = [];
 
-                             for ($k = 0; $k < $this->n; ++$k) {
 
-                                 $Bcolj[$k] = $B->A[$k][$j];
 
-                             }
 
-                             for ($i = 0; $i < $this->m; ++$i) {
 
-                                 $Arowi = $this->A[$i];
 
-                                 $s = 0;
 
-                                 for ($k = 0; $k < $this->n; ++$k) {
 
-                                     $s += $Arowi[$k] * $Bcolj[$k];
 
-                                 }
 
-                                 $C->A[$i][$j] = $s;
 
-                             }
 
-                         }
 
-                         return $C;
 
-                     }
 
-                     throw new CalculationException(self::MATRIX_DIMENSION_EXCEPTION);
 
-                 case 'array':
 
-                     $B = new self($args[0]);
 
-                     if ($this->n == $B->m) {
 
-                         $C = new self($this->m, $B->n);
 
-                         for ($i = 0; $i < $C->m; ++$i) {
 
-                             for ($j = 0; $j < $C->n; ++$j) {
 
-                                 $s = '0';
 
-                                 for ($k = 0; $k < $C->n; ++$k) {
 
-                                     $s += $this->A[$i][$k] * $B->A[$k][$j];
 
-                                 }
 
-                                 $C->A[$i][$j] = $s;
 
-                             }
 
-                         }
 
-                         return $C;
 
-                     }
 
-                     throw new CalculationException(self::MATRIX_DIMENSION_EXCEPTION);
 
-                 case 'integer':
 
-                     $C = new self($this->A);
 
-                     for ($i = 0; $i < $C->m; ++$i) {
 
-                         for ($j = 0; $j < $C->n; ++$j) {
 
-                             $C->A[$i][$j] *= $args[0];
 
-                         }
 
-                     }
 
-                     return $C;
 
-                 case 'double':
 
-                     $C = new self($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];
 
-                         }
 
-                     }
 
-                     return $C;
 
-                 case 'float':
 
-                     $C = new self($this->A);
 
-                     for ($i = 0; $i < $C->m; ++$i) {
 
-                         for ($j = 0; $j < $C->n; ++$j) {
 
-                             $C->A[$i][$j] *= $args[0];
 
-                         }
 
-                     }
 
-                     return $C;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-             }
 
-         } else {
 
-             throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-         }
 
-     }
 
-     /**
 
-      * power.
 
-      *
 
-      *    A = A ^ B
 
-      *
 
-      * @param mixed $B Matrix/Array
 
-      *
 
-      * @return Matrix Sum
 
-      */
 
-     public function power(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     break;
 
-             }
 
-             $this->checkMatrixDimensions($M);
 
-             for ($i = 0; $i < $this->m; ++$i) {
 
-                 for ($j = 0; $j < $this->n; ++$j) {
 
-                     $validValues = true;
 
-                     $value = $M->get($i, $j);
 
-                     if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
 
-                         $this->A[$i][$j] = trim($this->A[$i][$j], '"');
 
-                         $validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
 
-                     }
 
-                     if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
 
-                         $value = trim($value, '"');
 
-                         $validValues &= StringHelper::convertToNumberIfFraction($value);
 
-                     }
 
-                     if ($validValues) {
 
-                         $this->A[$i][$j] = pow($this->A[$i][$j], $value);
 
-                     } else {
 
-                         $this->A[$i][$j] = Functions::NAN();
 
-                     }
 
-                 }
 
-             }
 
-             return $this;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * concat.
 
-      *
 
-      *    A = A & B
 
-      *
 
-      * @param mixed $B Matrix/Array
 
-      *
 
-      * @return Matrix Sum
 
-      */
 
-     public function concat(...$args)
 
-     {
 
-         if (count($args) > 0) {
 
-             $match = implode(',', array_map('gettype', $args));
 
-             switch ($match) {
 
-                 case 'object':
 
-                     if ($args[0] instanceof self) {
 
-                         $M = $args[0];
 
-                     } else {
 
-                         throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION);
 
-                     }
 
-                     break;
 
-                 case 'array':
 
-                     $M = new self($args[0]);
 
-                     break;
 
-                 default:
 
-                     throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-                     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), '"');
 
-                 }
 
-             }
 
-             return $this;
 
-         }
 
-         throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION);
 
-     }
 
-     /**
 
-      * 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);
 
-         }
 
-         $QR = new QRDecomposition($this);
 
-         return $QR->solve($B);
 
-     }
 
-     /**
 
-      * Matrix inverse or pseudoinverse.
 
-      *
 
-      * @return Matrix ... Inverse(A) if A is square, pseudoinverse otherwise.
 
-      */
 
-     public function inverse()
 
-     {
 
-         return $this->solve($this->identity($this->m, $this->m));
 
-     }
 
-     /**
 
-      * det.
 
-      *
 
-      *    Calculate determinant
 
-      *
 
-      * @return float Determinant
 
-      */
 
-     public function det()
 
-     {
 
-         $L = new LUDecomposition($this);
 
-         return $L->det();
 
-     }
 
- }
 
 
  |