JAMA
[ class tree: JAMA ] [ index: JAMA ] [ all elements ]

Source for file Matrix.php

Documentation is available at Matrix.php

  1. <?php
  2. /**
  3.  * @package JAMA
  4.  */
  5.  
  6. define('RAND_MAX'mt_getrandmax());
  7. define('RAND_MIN'0);
  8.  
  9. /** PHPExcel root directory */
  10. if (!defined('PHPEXCEL_ROOT')) {
  11.     /**
  12.      * @ignore
  13.      */
  14.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../../');
  15. }
  16.  
  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';
  24. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/String.php';
  25. require_once PHPEXCEL_ROOT 'PHPExcel/Calculation/Functions.php';
  26.  
  27. /*
  28.  *    Matrix class
  29.  *
  30.  *    @author Paul Meagher
  31.  *    @author Michael Bommarito
  32.  *    @author Lukasz Karapuda
  33.  *    @author Bartek Matosiuk
  34.  *    @version 1.8
  35.  *    @license PHP v3.0
  36.  *    @see http://math.nist.gov/javanumerics/jama/
  37.  */
  38. class Matrix {
  39.  
  40.     /**
  41.      *    Matrix storage
  42.      *
  43.      *    @var array 
  44.      *    @access public
  45.      */
  46.     public $A = array();
  47.  
  48.     /**
  49.      *    Matrix row dimension
  50.      *
  51.      *    @var int 
  52.      *    @access private
  53.      */
  54.     private $m;
  55.  
  56.     /**
  57.      *    Matrix column dimension
  58.      *
  59.      *    @var int 
  60.      *    @access private
  61.      */
  62.     private $n;
  63.  
  64.  
  65.     /**
  66.      *    Polymorphic constructor
  67.      *
  68.      *    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.
  69.      */
  70.     public function __construct({
  71.         if (func_num_args(0{
  72.             $args func_get_args();
  73.             $match implode(","array_map('gettype'$args));
  74.  
  75.             switch($match{
  76.                 //Square matrix - n x n
  77.                 case 'integer':
  78.                         $this->m = $args[0];
  79.                         $this->n = $args[0];
  80.                         $this->A = array_fill(0$this->marray_fill(0$this->n0));
  81.                         break;
  82.                 //Rectangular matrix - m x n
  83.                 case 'integer,integer':
  84.                         $this->m = $args[0];
  85.                         $this->n = $args[1];
  86.                         $this->A = array_fill(0$this->marray_fill(0$this->n0));
  87.                         break;
  88.                 //Rectangular matrix constant-filled - m x n filled with c
  89.                 case 'integer,integer,integer':
  90.                         $this->m = $args[0];
  91.                         $this->n = $args[1];
  92.                         $this->A = array_fill(0$this->marray_fill(0$this->n$args[2]));
  93.                         break;
  94.                 //Rectangular matrix constant-filled - m x n filled with c
  95.                 case 'integer,integer,double':
  96.                         $this->m = $args[0];
  97.                         $this->n = $args[1];
  98.                         $this->A = array_fill(0$this->marray_fill(0$this->n$args[2]));
  99.                         break;
  100.                 //Rectangular matrix - m x n initialized from 2D array
  101.                 case 'array':
  102.                         $this->m = count($args[0]);
  103.                         $this->n = count($args[0][0]);
  104.                         $this->A = $args[0];
  105.                         break;
  106.                 //Rectangular matrix - m x n initialized from 2D array
  107.                 case 'array,integer,integer':
  108.                         $this->m = $args[1];
  109.                         $this->n = $args[2];
  110.                         $this->A = $args[0];
  111.                         break;
  112.                 //Rectangular matrix - m x n initialized from packed array
  113.                 case 'array,integer':
  114.                         $this->m = $args[1];
  115.                         if ($this->m != 0{
  116.                             $this->n = count($args[0]$this->m;
  117.                         else {
  118.                             $this->n = 0;
  119.                         }
  120.                         if (($this->m * $this->n== count($args[0])) {
  121.                             for($i 0$i $this->m++$i{
  122.                                 for($j 0$j $this->n++$j{
  123.                                     $this->A[$i][$j$args[0][$i $j $this->m];
  124.                                 }
  125.                             }
  126.                         else {
  127.                             throw new Exception(JAMAError(ArrayLengthException));
  128.                         }
  129.                         break;
  130.                 default:
  131.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  132.                         break;
  133.             }
  134.         else {
  135.             throw new Exception(JAMAError(PolymorphicArgumentException));
  136.         }
  137.     }    //    function __construct()
  138.  
  139.  
  140.     /**
  141.      *    getArray
  142.      *
  143.      *    @return array Matrix array
  144.      */
  145.     public function getArray({
  146.         return $this->A;
  147.     }    //    function getArray()
  148.  
  149.  
  150.     /**
  151.      *    getArrayCopy
  152.      *
  153.      *    @return array Matrix array copy
  154.      */
  155.     public function getArrayCopy({
  156.         return $this->A;
  157.     }    //    function getArrayCopy()
  158.  
  159.  
  160.     /**
  161.      *    constructWithCopy
  162.      *    Construct a matrix from a copy of a 2-D array.
  163.      *
  164.      *    @param double A[][]        Two-dimensional array of doubles.
  165.      *    @exception    IllegalArgumentException All rows must have the same length
  166.      */
  167.     public function constructWithCopy($A{
  168.         $this->m = count($A);
  169.         $this->n = count($A[0]);
  170.         $newCopyMatrix new Matrix($this->m$this->n);
  171.         for ($i 0$i $this->m++$i{
  172.             if (count($A[$i]!= $this->n{
  173.                 throw new Exception(JAMAError(RowLengthException));
  174.             }
  175.             for ($j 0$j $this->n++$j{
  176.                 $newCopyMatrix->A[$i][$j$A[$i][$j];
  177.             }
  178.         }
  179.         return $newCopyMatrix;
  180.     }    //    function constructWithCopy()
  181.  
  182.  
  183.     /**
  184.      *    getColumnPackedCopy
  185.      *
  186.      *    Get a column-packed array
  187.      *    @return array Column-packed matrix array
  188.      */
  189.     public function getColumnPackedCopy({
  190.         $P array();
  191.         for($i 0$i $this->m++$i{
  192.             for($j 0$j $this->n++$j{
  193.                 array_push($P$this->A[$j][$i]);
  194.             }
  195.         }
  196.         return $P;
  197.     }    //    function getColumnPackedCopy()
  198.  
  199.  
  200.     /**
  201.      *    getRowPackedCopy
  202.      *
  203.      *    Get a row-packed array
  204.      *    @return array Row-packed matrix array
  205.      */
  206.     public function getRowPackedCopy({
  207.         $P array();
  208.         for($i 0$i $this->m++$i{
  209.             for($j 0$j $this->n++$j{
  210.                 array_push($P$this->A[$i][$j]);
  211.             }
  212.         }
  213.         return $P;
  214.     }    //    function getRowPackedCopy()
  215.  
  216.  
  217.     /**
  218.      *    getRowDimension
  219.      *
  220.      *    @return int Row dimension
  221.      */
  222.     public function getRowDimension({
  223.         return $this->m;
  224.     }    //    function getRowDimension()
  225.  
  226.  
  227.     /**
  228.      *    getColumnDimension
  229.      *
  230.      *    @return int Column dimension
  231.      */
  232.     public function getColumnDimension({
  233.         return $this->n;
  234.     }    //    function getColumnDimension()
  235.  
  236.  
  237.     /**
  238.      *    get
  239.      *
  240.      *    Get the i,j-th element of the matrix.
  241.      *    @param int $i Row position
  242.      *    @param int $j Column position
  243.      *    @return mixed Element (int/float/double)
  244.      */
  245.     public function get($i null$j null{
  246.         return $this->A[$i][$j];
  247.     }    //    function get()
  248.  
  249.  
  250.     /**
  251.      *    getMatrix
  252.      *
  253.      *    Get a submatrix
  254.      *    @param int $i0 Initial row index
  255.      *    @param int $iF Final row index
  256.      *    @param int $j0 Initial column index
  257.      *    @param int $jF Final column index
  258.      *    @return Matrix Submatrix
  259.      */
  260.     public function getMatrix({
  261.         if (func_num_args(0{
  262.             $args func_get_args();
  263.             $match implode(","array_map('gettype'$args));
  264.  
  265.             switch($match{
  266.                 //A($i0...; $j0...)
  267.                 case 'integer,integer':
  268.                         list($i0$j0$args;
  269.                         if ($i0 >= 0$m $this->m - $i0else throw new Exception(JAMAError(ArgumentBoundsException))}
  270.                         if ($j0 >= 0$n $this->n - $j0else throw new Exception(JAMAError(ArgumentBoundsException))}
  271.                         $R new Matrix($m$n);
  272.                         for($i $i0$i $this->m++$i{
  273.                             for($j $j0$j $this->n++$j{
  274.                                 $R->set($i$j$this->A[$i][$j]);
  275.                             }
  276.                         }
  277.                         return $R;
  278.                         break;
  279.                 //A($i0...$iF; $j0...$jF)
  280.                 case 'integer,integer,integer,integer':
  281.                         list($i0$iF$j0$jF$args;
  282.                         if (($iF $i0&& ($this->m >= $iF&& ($i0 >= 0)) $m $iF $i0else throw new Exception(JAMAError(ArgumentBoundsException))}
  283.                         if (($jF $j0&& ($this->n >= $jF&& ($j0 >= 0)) $n $jF $j0else throw new Exception(JAMAError(ArgumentBoundsException))}
  284.                         $R new Matrix($m+1$n+1);
  285.                         for($i $i0$i <= $iF++$i{
  286.                             for($j $j0$j <= $jF++$j{
  287.                                 $R->set($i $i0$j $j0$this->A[$i][$j]);
  288.                             }
  289.                         }
  290.                         return $R;
  291.                         break;
  292.                 //$R = array of row indices; $C = array of column indices
  293.                 case 'array,array':
  294.                         list($RL$CL$args;
  295.                         if (count($RL0$m count($RL)else throw new Exception(JAMAError(ArgumentBoundsException))}
  296.                         if (count($CL0$n count($CL)else throw new Exception(JAMAError(ArgumentBoundsException))}
  297.                         $R new Matrix($m$n);
  298.                         for($i 0$i $m++$i{
  299.                             for($j 0$j $n++$j{
  300.                                 $R->set($i $i0$j $j0$this->A[$RL[$i]][$CL[$j]]);
  301.                             }
  302.                         }
  303.                         return $R;
  304.                         break;
  305.                 //$RL = array of row indices; $CL = array of column indices
  306.                 case 'array,array':
  307.                         list($RL$CL$args;
  308.                         if (count($RL0$m count($RL)else throw new Exception(JAMAError(ArgumentBoundsException))}
  309.                         if (count($CL0$n count($CL)else throw new Exception(JAMAError(ArgumentBoundsException))}
  310.                         $R new Matrix($m$n);
  311.                         for($i 0$i $m++$i{
  312.                             for($j 0$j $n++$j{
  313.                                 $R->set($i$j$this->A[$RL[$i]][$CL[$j]]);
  314.                             }
  315.                         }
  316.                         return $R;
  317.                         break;
  318.                 //A($i0...$iF); $CL = array of column indices
  319.                 case 'integer,integer,array':
  320.                         list($i0$iF$CL$args;
  321.                         if (($iF $i0&& ($this->m >= $iF&& ($i0 >= 0)) $m $iF $i0else throw new Exception(JAMAError(ArgumentBoundsException))}
  322.                         if (count($CL0$n count($CL)else throw new Exception(JAMAError(ArgumentBoundsException))}
  323.                         $R new Matrix($m$n);
  324.                         for($i $i0$i $iF++$i{
  325.                             for($j 0$j $n++$j{
  326.                                 $R->set($i $i0$j$this->A[$RL[$i]][$j]);
  327.                             }
  328.                         }
  329.                         return $R;
  330.                         break;
  331.                 //$RL = array of row indices
  332.                 case 'array,integer,integer':
  333.                         list($RL$j0$jF$args;
  334.                         if (count($RL0$m count($RL)else throw new Exception(JAMAError(ArgumentBoundsException))}
  335.                         if (($jF >= $j0&& ($this->n >= $jF&& ($j0 >= 0)) $n $jF $j0else throw new Exception(JAMAError(ArgumentBoundsException))}
  336.                         $R new Matrix($m$n+1);
  337.                         for($i 0$i $m++$i{
  338.                             for($j $j0$j <= $jF++$j{
  339.                                 $R->set($i$j $j0$this->A[$RL[$i]][$j]);
  340.                             }
  341.                         }
  342.                         return $R;
  343.                         break;
  344.                 default:
  345.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  346.                         break;
  347.             }
  348.         else {
  349.             throw new Exception(JAMAError(PolymorphicArgumentException));
  350.         }
  351.     }    //    function getMatrix()
  352.  
  353.  
  354.     /**
  355.      *    setMatrix
  356.      *
  357.      *    Set a submatrix
  358.      *    @param int $i0 Initial row index
  359.      *    @param int $j0 Initial column index
  360.      *    @param mixed $S Matrix/Array submatrix
  361.      *     ($i0, $j0, $S) $S = Matrix
  362.      *     ($i0, $j0, $S) $S = Array
  363.      */
  364.     public function setMatrix({
  365.         if (func_num_args(0{
  366.             $args func_get_args();
  367.             $match implode(","array_map('gettype'$args));
  368.  
  369.             switch($match{
  370.                 case 'integer,integer,object':
  371.                         if ($args[2instanceof Matrix$M $args[2]else throw new Exception(JAMAError(ArgumentTypeException))}
  372.                         if (($args[0$M->m<= $this->m$i0 $args[0]else throw new Exception(JAMAError(ArgumentBoundsException))}
  373.                         if (($args[1$M->n<= $this->n$j0 $args[1]else throw new Exception(JAMAError(ArgumentBoundsException))}
  374.                         for($i $i0$i $i0 $M->m++$i{
  375.                             for($j $j0$j $j0 $M->n++$j{
  376.                                 $this->A[$i][$j$M->get($i $i0$j $j0);
  377.                             }
  378.                         }
  379.                         break;
  380.                 case 'integer,integer,array':
  381.                         $M new Matrix($args[2]);
  382.                         if (($args[0$M->m<= $this->m$i0 $args[0]else throw new Exception(JAMAError(ArgumentBoundsException))}
  383.                         if (($args[1$M->n<= $this->n$j0 $args[1]else throw new Exception(JAMAError(ArgumentBoundsException))}
  384.                         for($i $i0$i $i0 $M->m++$i{
  385.                             for($j $j0$j $j0 $M->n++$j{
  386.                                 $this->A[$i][$j$M->get($i $i0$j $j0);
  387.                             }
  388.                         }
  389.                         break;
  390.                 default:
  391.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  392.                         break;
  393.             }
  394.         else {
  395.             throw new Exception(JAMAError(PolymorphicArgumentException));
  396.         }
  397.     }    //    function setMatrix()
  398.  
  399.  
  400.     /**
  401.      *    checkMatrixDimensions
  402.      *
  403.      *    Is matrix B the same size?
  404.      *    @param Matrix $B Matrix B
  405.      *    @return boolean 
  406.      */
  407.     public function checkMatrixDimensions($B null{
  408.         if ($B instanceof Matrix{
  409.             if (($this->m == $B->getRowDimension()) && ($this->n == $B->getColumnDimension())) {
  410.                 return true;
  411.             else {
  412.                 throw new Exception(JAMAError(MatrixDimensionException));
  413.             }
  414.         else {
  415.             throw new Exception(JAMAError(ArgumentTypeException));
  416.         }
  417.     }    //    function checkMatrixDimensions()
  418.  
  419.  
  420.  
  421.     /**
  422.      *    set
  423.      *
  424.      *    Set the i,j-th element of the matrix.
  425.      *    @param int $i Row position
  426.      *    @param int $j Column position
  427.      *    @param mixed $c Int/float/double value
  428.      *    @return mixed Element (int/float/double)
  429.      */
  430.     public function set($i null$j null$c null{
  431.         // Optimized set version just has this
  432.         $this->A[$i][$j$c;
  433.         /*
  434.         if (is_int($i) && is_int($j) && is_numeric($c)) {
  435.             if (($i < $this->m) && ($j < $this->n)) {
  436.                 $this->A[$i][$j] = $c;
  437.             } else {
  438.                 echo "A[$i][$j] = $c<br />";
  439.                 throw new Exception(JAMAError(ArgumentBoundsException));
  440.             }
  441.         } else {
  442.             throw new Exception(JAMAError(ArgumentTypeException));
  443.         }
  444.         */
  445.     }    //    function set()
  446.  
  447.  
  448.     /**
  449.      *    identity
  450.      *
  451.      *    Generate an identity matrix.
  452.      *    @param int $m Row dimension
  453.      *    @param int $n Column dimension
  454.      *    @return Matrix Identity matrix
  455.      */
  456.     public function identity($m null$n null{
  457.         return $this->diagonal($m$n1);
  458.     }    //    function identity()
  459.  
  460.  
  461.     /**
  462.      *    diagonal
  463.      *
  464.      *    Generate a diagonal matrix
  465.      *    @param int $m Row dimension
  466.      *    @param int $n Column dimension
  467.      *    @param mixed $c Diagonal value
  468.      *    @return Matrix Diagonal matrix
  469.      */
  470.     public function diagonal($m null$n null$c 1{
  471.         $R new Matrix($m$n);
  472.         for($i 0$i $m++$i{
  473.             $R->set($i$i$c);
  474.         }
  475.         return $R;
  476.     }    //    function diagonal()
  477.  
  478.  
  479.     /**
  480.      *    filled
  481.      *
  482.      *    Generate a filled matrix
  483.      *    @param int $m Row dimension
  484.      *    @param int $n Column dimension
  485.      *    @param int $c Fill constant
  486.      *    @return Matrix Filled matrix
  487.      */
  488.     public function filled($m null$n null$c 0{
  489.         if (is_int($m&& is_int($n&& is_numeric($c)) {
  490.             $R new Matrix($m$n$c);
  491.             return $R;
  492.         else {
  493.             throw new Exception(JAMAError(ArgumentTypeException));
  494.         }
  495.     }    //    function filled()
  496.  
  497.     /**
  498.      *    random
  499.      *
  500.      *    Generate a random matrix
  501.      *    @param int $m Row dimension
  502.      *    @param int $n Column dimension
  503.      *    @return Matrix Random matrix
  504.      */
  505.     public function random($m null$n null$a RAND_MIN$b RAND_MAX{
  506.         if (is_int($m&& is_int($n&& is_numeric($a&& is_numeric($b)) {
  507.             $R new Matrix($m$n);
  508.             for($i 0$i $m++$i{
  509.                 for($j 0$j $n++$j{
  510.                     $R->set($i$jmt_rand($a$b));
  511.                 }
  512.             }
  513.             return $R;
  514.         else {
  515.             throw new Exception(JAMAError(ArgumentTypeException));
  516.         }
  517.     }    //    function random()
  518.  
  519.  
  520.     /**
  521.      *    packed
  522.      *
  523.      *    Alias for getRowPacked
  524.      *    @return array Packed array
  525.      */
  526.     public function packed({
  527.         return $this->getRowPacked();
  528.     }    //    function packed()
  529.  
  530.  
  531.     /**
  532.      *    getMatrixByRow
  533.      *
  534.      *    Get a submatrix by row index/range
  535.      *    @param int $i0 Initial row index
  536.      *    @param int $iF Final row index
  537.      *    @return Matrix Submatrix
  538.      */
  539.     public function getMatrixByRow($i0 null$iF null{
  540.         if (is_int($i0)) {
  541.             if (is_int($iF)) {
  542.                 return $this->getMatrix($i00$iF 1$this->n);
  543.             else {
  544.                 return $this->getMatrix($i00$i0 1$this->n);
  545.             }
  546.         else {
  547.             throw new Exception(JAMAError(ArgumentTypeException));
  548.         }
  549.     }    //    function getMatrixByRow()
  550.  
  551.  
  552.     /**
  553.      *    getMatrixByCol
  554.      *
  555.      *    Get a submatrix by column index/range
  556.      *    @param int $i0 Initial column index
  557.      *    @param int $iF Final column index
  558.      *    @return Matrix Submatrix
  559.      */
  560.     public function getMatrixByCol($j0 null$jF null{
  561.         if (is_int($j0)) {
  562.             if (is_int($jF)) {
  563.                 return $this->getMatrix(0$j0$this->m$jF 1);
  564.             else {
  565.                 return $this->getMatrix(0$j0$this->m$j0 1);
  566.             }
  567.         else {
  568.             throw new Exception(JAMAError(ArgumentTypeException));
  569.         }
  570.     }    //    function getMatrixByCol()
  571.  
  572.  
  573.     /**
  574.      *    transpose
  575.      *
  576.      *    Tranpose matrix
  577.      *    @return Matrix Transposed matrix
  578.      */
  579.     public function transpose({
  580.         $R new Matrix($this->n$this->m);
  581.         for($i 0$i $this->m++$i{
  582.             for($j 0$j $this->n++$j{
  583.                 $R->set($j$i$this->A[$i][$j]);
  584.             }
  585.         }
  586.         return $R;
  587.     }    //    function transpose()
  588.  
  589.  
  590.     /**
  591.      *    norm1
  592.      *
  593.      *    One norm
  594.      *    @return float Maximum column sum
  595.      */
  596.     public function norm1({
  597.         $r 0;
  598.         for($j 0$j $this->n++$j{
  599.             $s 0;
  600.             for($i 0$i $this->m++$i{
  601.                 $s += abs($this->A[$i][$j]);
  602.             }
  603.             $r ($r $s$r $s;
  604.         }
  605.         return $r;
  606.     }    //    function norm1()
  607.  
  608.  
  609.     /**
  610.      *    norm2
  611.      *
  612.      *    Maximum singular value
  613.      *    @return float Maximum singular value
  614.      */
  615.     public function norm2({
  616.     }    //    function norm2()
  617.  
  618.  
  619.     /**
  620.      *    normInf
  621.      *
  622.      *    Infinite norm
  623.      *    @return float Maximum row sum
  624.      */
  625.     public function normInf({
  626.         $r 0;
  627.         for($i 0$i $this->m++$i{
  628.             $s 0;
  629.             for($j 0$j $this->n++$j{
  630.                 $s += abs($this->A[$i][$j]);
  631.             }
  632.             $r ($r $s$r $s;
  633.         }
  634.         return $r;
  635.     }    //    function normInf()
  636.  
  637.  
  638.     /**
  639.      *    normF
  640.      *
  641.      *    Frobenius norm
  642.      *    @return float Square root of the sum of all elements squared
  643.      */
  644.     public function normF({
  645.         $f 0;
  646.         for ($i 0$i $this->m++$i{
  647.             for ($j 0$j $this->n++$j{
  648.                 $f hypo($f,$this->A[$i][$j]);
  649.             }
  650.         }
  651.         return $f;
  652.     }    //    function normF()
  653.  
  654.  
  655.     /**
  656.      *    Matrix rank
  657.      *
  658.      *    @return effective numerical rank, obtained from SVD.
  659.      */
  660.     public function rank ({
  661.         $svd new SingularValueDecomposition($this);
  662.         return $svd->rank();
  663.     }    //    function rank ()
  664.  
  665.  
  666.     /**
  667.      *    Matrix condition (2 norm)
  668.      *
  669.      *    @return ratio of largest to smallest singular value.
  670.      */
  671.     public function cond ({
  672.         $svd new SingularValueDecomposition($this);
  673.         return $svd->cond();
  674.     }    //    function cond ()
  675.  
  676.  
  677.     /**
  678.      *    trace
  679.      *
  680.      *    Sum of diagonal elements
  681.      *    @return float Sum of diagonal elements
  682.      */
  683.     public function trace({
  684.         $s 0;
  685.         $n min($this->m$this->n);
  686.         for($i 0$i $n++$i{
  687.             $s += $this->A[$i][$i];
  688.         }
  689.         return $s;
  690.     }    //    function trace()
  691.  
  692.  
  693.     /**
  694.      *    uminus
  695.      *
  696.      *    Unary minus matrix -A
  697.      *    @return Matrix Unary minus matrix
  698.      */
  699.     public function uminus({
  700.     }    //    function uminus()
  701.  
  702.  
  703.     /**
  704.      *    plus
  705.      *
  706.      *    A + B
  707.      *    @param mixed $B Matrix/Array
  708.      *    @return Matrix Sum
  709.      */
  710.     public function plus({
  711.         if (func_num_args(0{
  712.             $args func_get_args();
  713.             $match implode(","array_map('gettype'$args));
  714.  
  715.             switch($match{
  716.                 case 'object':
  717.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  718.                         break;
  719.                 case 'array':
  720.                         $M new Matrix($args[0]);
  721.                         break;
  722.                 default:
  723.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  724.                         break;
  725.             }
  726.             $this->checkMatrixDimensions($M);
  727.             for($i 0$i $this->m++$i{
  728.                 for($j 0$j $this->n++$j{
  729.                     $M->set($i$j$M->get($i$j$this->A[$i][$j]);
  730.                 }
  731.             }
  732.             return $M;
  733.         else {
  734.             throw new Exception(JAMAError(PolymorphicArgumentException));
  735.         }
  736.     }    //    function plus()
  737.  
  738.  
  739.     /**
  740.      *    plusEquals
  741.      *
  742.      *    A = A + B
  743.      *    @param mixed $B Matrix/Array
  744.      *    @return Matrix Sum
  745.      */
  746.     public function plusEquals({
  747.         if (func_num_args(0{
  748.             $args func_get_args();
  749.             $match implode(","array_map('gettype'$args));
  750.  
  751.             switch($match{
  752.                 case 'object':
  753.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  754.                         break;
  755.                 case 'array':
  756.                         $M new Matrix($args[0]);
  757.                         break;
  758.                 default:
  759.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  760.                         break;
  761.             }
  762.             $this->checkMatrixDimensions($M);
  763.             for($i 0$i $this->m++$i{
  764.                 for($j 0$j $this->n++$j{
  765.                     $validValues True;
  766.                     $value $M->get($i$j);
  767.                     if ((is_string($this->A[$i][$j])) && (!is_numeric($this->A[$i][$j]))) {
  768.                         $this->A[$i][$jtrim($this->A[$i][$j],'"');
  769.                         $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
  770.                     }
  771.                     if ((is_string($value)) && (!is_numeric($value))) {
  772.                         $value trim($value,'"');
  773.                         $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
  774.                     }
  775.                     if ($validValues{
  776.                         $this->A[$i][$j+= $value;
  777.                     else {
  778.                         $this->A[$i][$jPHPExcel_Calculation_Functions::NaN();
  779.                     }
  780.                 }
  781.             }
  782.             return $this;
  783.         else {
  784.             throw new Exception(JAMAError(PolymorphicArgumentException));
  785.         }
  786.     }    //    function plusEquals()
  787.  
  788.  
  789.     /**
  790.      *    minus
  791.      *
  792.      *    A - B
  793.      *    @param mixed $B Matrix/Array
  794.      *    @return Matrix Sum
  795.      */
  796.     public function minus({
  797.         if (func_num_args(0{
  798.             $args func_get_args();
  799.             $match implode(","array_map('gettype'$args));
  800.  
  801.             switch($match{
  802.                 case 'object':
  803.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  804.                         break;
  805.                 case 'array':
  806.                         $M new Matrix($args[0]);
  807.                         break;
  808.                 default:
  809.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  810.                         break;
  811.             }
  812.             $this->checkMatrixDimensions($M);
  813.             for($i 0$i $this->m++$i{
  814.                 for($j 0$j $this->n++$j{
  815.                     $M->set($i$j$M->get($i$j$this->A[$i][$j]);
  816.                 }
  817.             }
  818.             return $M;
  819.         else {
  820.             throw new Exception(JAMAError(PolymorphicArgumentException));
  821.         }
  822.     }    //    function minus()
  823.  
  824.  
  825.     /**
  826.      *    minusEquals
  827.      *
  828.      *    A = A - B
  829.      *    @param mixed $B Matrix/Array
  830.      *    @return Matrix Sum
  831.      */
  832.     public function minusEquals({
  833.         if (func_num_args(0{
  834.             $args func_get_args();
  835.             $match implode(","array_map('gettype'$args));
  836.  
  837.             switch($match{
  838.                 case 'object':
  839.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  840.                         break;
  841.                 case 'array':
  842.                         $M new Matrix($args[0]);
  843.                         break;
  844.                 default:
  845.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  846.                         break;
  847.             }
  848.             $this->checkMatrixDimensions($M);
  849.             for($i 0$i $this->m++$i{
  850.                 for($j 0$j $this->n++$j{
  851.                     $validValues True;
  852.                     $value $M->get($i$j);
  853.                     if ((is_string($this->A[$i][$j])) && (!is_numeric($this->A[$i][$j]))) {
  854.                         $this->A[$i][$jtrim($this->A[$i][$j],'"');
  855.                         $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
  856.                     }
  857.                     if ((is_string($value)) && (!is_numeric($value))) {
  858.                         $value trim($value,'"');
  859.                         $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
  860.                     }
  861.                     if ($validValues{
  862.                         $this->A[$i][$j-= $value;
  863.                     else {
  864.                         $this->A[$i][$jPHPExcel_Calculation_Functions::NaN();
  865.                     }
  866.                 }
  867.             }
  868.             return $this;
  869.         else {
  870.             throw new Exception(JAMAError(PolymorphicArgumentException));
  871.         }
  872.     }    //    function minusEquals()
  873.  
  874.  
  875.     /**
  876.      *    arrayTimes
  877.      *
  878.      *    Element-by-element multiplication
  879.      *    Cij = Aij * Bij
  880.      *    @param mixed $B Matrix/Array
  881.      *    @return Matrix Matrix Cij
  882.      */
  883.     public function arrayTimes({
  884.         if (func_num_args(0{
  885.             $args func_get_args();
  886.             $match implode(","array_map('gettype'$args));
  887.  
  888.             switch($match{
  889.                 case 'object':
  890.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  891.                         break;
  892.                 case 'array':
  893.                         $M new Matrix($args[0]);
  894.                         break;
  895.                 default:
  896.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  897.                         break;
  898.             }
  899.             $this->checkMatrixDimensions($M);
  900.             for($i 0$i $this->m++$i{
  901.                 for($j 0$j $this->n++$j{
  902.                     $M->set($i$j$M->get($i$j$this->A[$i][$j]);
  903.                 }
  904.             }
  905.             return $M;
  906.         else {
  907.             throw new Exception(JAMAError(PolymorphicArgumentException));
  908.         }
  909.     }    //    function arrayTimes()
  910.  
  911.  
  912.     /**
  913.      *    arrayTimesEquals
  914.      *
  915.      *    Element-by-element multiplication
  916.      *    Aij = Aij * Bij
  917.      *    @param mixed $B Matrix/Array
  918.      *    @return Matrix Matrix Aij
  919.      */
  920.     public function arrayTimesEquals({
  921.         if (func_num_args(0{
  922.             $args func_get_args();
  923.             $match implode(","array_map('gettype'$args));
  924.  
  925.             switch($match{
  926.                 case 'object':
  927.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  928.                         break;
  929.                 case 'array':
  930.                         $M new Matrix($args[0]);
  931.                         break;
  932.                 default:
  933.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  934.                         break;
  935.             }
  936.             $this->checkMatrixDimensions($M);
  937.             for($i 0$i $this->m++$i{
  938.                 for($j 0$j $this->n++$j{
  939.                     $validValues True;
  940.                     $value $M->get($i$j);
  941.                     if ((is_string($this->A[$i][$j])) && (!is_numeric($this->A[$i][$j]))) {
  942.                         $this->A[$i][$jtrim($this->A[$i][$j],'"');
  943.                         $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
  944.                     }
  945.                     if ((is_string($value)) && (!is_numeric($value))) {
  946.                         $value trim($value,'"');
  947.                         $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
  948.                     }
  949.                     if ($validValues{
  950.                         $this->A[$i][$j*= $value;
  951.                     else {
  952.                         $this->A[$i][$jPHPExcel_Calculation_Functions::NaN();
  953.                     }
  954.                 }
  955.             }
  956.             return $this;
  957.         else {
  958.             throw new Exception(JAMAError(PolymorphicArgumentException));
  959.         }
  960.     }    //    function arrayTimesEquals()
  961.  
  962.  
  963.     /**
  964.      *    arrayRightDivide
  965.      *
  966.      *    Element-by-element right division
  967.      *    A / B
  968.      *    @param Matrix $B Matrix B
  969.      *    @return Matrix Division result
  970.      */
  971.     public function arrayRightDivide({
  972.         if (func_num_args(0{
  973.             $args func_get_args();
  974.             $match implode(","array_map('gettype'$args));
  975.  
  976.             switch($match{
  977.                 case 'object':
  978.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  979.                         break;
  980.                 case 'array':
  981.                         $M new Matrix($args[0]);
  982.                         break;
  983.                 default:
  984.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  985.                         break;
  986.             }
  987.             $this->checkMatrixDimensions($M);
  988.             for($i 0$i $this->m++$i{
  989.                 for($j 0$j $this->n++$j{
  990.                     $validValues True;
  991.                     $value $M->get($i$j);
  992.                     if ((is_string($this->A[$i][$j])) && (!is_numeric($this->A[$i][$j]))) {
  993.                         $this->A[$i][$jtrim($this->A[$i][$j],'"');
  994.                         $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
  995.                     }
  996.                     if ((is_string($value)) && (!is_numeric($value))) {
  997.                         $value trim($value,'"');
  998.                         $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
  999.                     }
  1000.                     if ($validValues{
  1001.                         if ($value == 0{
  1002.                             //    Trap for Divide by Zero error
  1003.                             $M->set($i$j'#DIV/0!');
  1004.                         else {
  1005.                             $M->set($i$j$this->A[$i][$j$value);
  1006.                         }
  1007.                     else {
  1008.                         $this->A[$i][$jPHPExcel_Calculation_Functions::NaN();
  1009.                     }
  1010.                 }
  1011.             }
  1012.             return $M;
  1013.         else {
  1014.             throw new Exception(JAMAError(PolymorphicArgumentException));
  1015.         }
  1016.     }    //    function arrayRightDivide()
  1017.  
  1018.  
  1019.     /**
  1020.      *    arrayRightDivideEquals
  1021.      *
  1022.      *    Element-by-element right division
  1023.      *    Aij = Aij / Bij
  1024.      *    @param mixed $B Matrix/Array
  1025.      *    @return Matrix Matrix Aij
  1026.      */
  1027.     public function arrayRightDivideEquals({
  1028.         if (func_num_args(0{
  1029.             $args func_get_args();
  1030.             $match implode(","array_map('gettype'$args));
  1031.  
  1032.             switch($match{
  1033.                 case 'object':
  1034.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  1035.                         break;
  1036.                 case 'array':
  1037.                         $M new Matrix($args[0]);
  1038.                         break;
  1039.                 default:
  1040.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  1041.                         break;
  1042.             }
  1043.             $this->checkMatrixDimensions($M);
  1044.             for($i 0$i $this->m++$i{
  1045.                 for($j 0$j $this->n++$j{
  1046.                     $this->A[$i][$j$this->A[$i][$j$M->get($i$j);
  1047.                 }
  1048.             }
  1049.             return $M;
  1050.         else {
  1051.             throw new Exception(JAMAError(PolymorphicArgumentException));
  1052.         }
  1053.     }    //    function arrayRightDivideEquals()
  1054.  
  1055.  
  1056.     /**
  1057.      *    arrayLeftDivide
  1058.      *
  1059.      *    Element-by-element Left division
  1060.      *    A / B
  1061.      *    @param Matrix $B Matrix B
  1062.      *    @return Matrix Division result
  1063.      */
  1064.     public function arrayLeftDivide({
  1065.         if (func_num_args(0{
  1066.             $args func_get_args();
  1067.             $match implode(","array_map('gettype'$args));
  1068.  
  1069.             switch($match{
  1070.                 case 'object':
  1071.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  1072.                         break;
  1073.                 case 'array':
  1074.                         $M new Matrix($args[0]);
  1075.                         break;
  1076.                 default:
  1077.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  1078.                         break;
  1079.             }
  1080.             $this->checkMatrixDimensions($M);
  1081.             for($i 0$i $this->m++$i{
  1082.                 for($j 0$j $this->n++$j{
  1083.                     $M->set($i$j$M->get($i$j$this->A[$i][$j]);
  1084.                 }
  1085.             }
  1086.             return $M;
  1087.         else {
  1088.             throw new Exception(JAMAError(PolymorphicArgumentException));
  1089.         }
  1090.     }    //    function arrayLeftDivide()
  1091.  
  1092.  
  1093.     /**
  1094.      *    arrayLeftDivideEquals
  1095.      *
  1096.      *    Element-by-element Left division
  1097.      *    Aij = Aij / Bij
  1098.      *    @param mixed $B Matrix/Array
  1099.      *    @return Matrix Matrix Aij
  1100.      */
  1101.     public function arrayLeftDivideEquals({
  1102.         if (func_num_args(0{
  1103.             $args func_get_args();
  1104.             $match implode(","array_map('gettype'$args));
  1105.  
  1106.             switch($match{
  1107.                 case 'object':
  1108.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  1109.                         break;
  1110.                 case 'array':
  1111.                         $M new Matrix($args[0]);
  1112.                         break;
  1113.                 default:
  1114.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  1115.                         break;
  1116.             }
  1117.             $this->checkMatrixDimensions($M);
  1118.             for($i 0$i $this->m++$i{
  1119.                 for($j 0$j $this->n++$j{
  1120.                     $this->A[$i][$j$M->get($i$j$this->A[$i][$j];
  1121.                 }
  1122.             }
  1123.             return $M;
  1124.         else {
  1125.             throw new Exception(JAMAError(PolymorphicArgumentException));
  1126.         }
  1127.     }    //    function arrayLeftDivideEquals()
  1128.  
  1129.  
  1130.     /**
  1131.      *    times
  1132.      *
  1133.      *    Matrix multiplication
  1134.      *    @param mixed $n Matrix/Array/Scalar
  1135.      *    @return Matrix Product
  1136.      */
  1137.     public function times({
  1138.         if (func_num_args(0{
  1139.             $args  func_get_args();
  1140.             $match implode(","array_map('gettype'$args));
  1141.  
  1142.             switch($match{
  1143.                 case 'object':
  1144.                         if ($args[0instanceof Matrix$B $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  1145.                         if ($this->n == $B->m{
  1146.                             $C new Matrix($this->m$B->n);
  1147.                             for($j 0$j $B->n++$j{
  1148.                                 for ($k 0$k $this->n++$k{
  1149.                                     $Bcolj[$k$B->A[$k][$j];
  1150.                                 }
  1151.                                 for($i 0$i $this->m++$i{
  1152.                                     $Arowi $this->A[$i];
  1153.                                     $s 0;
  1154.                                     for($k 0$k $this->n++$k{
  1155.                                         $s += $Arowi[$k$Bcolj[$k];
  1156.                                     }
  1157.                                     $C->A[$i][$j$s;
  1158.                                 }
  1159.                             }
  1160.                             return $C;
  1161.                         else {
  1162.                             throw new Exception(JAMAError(MatrixDimensionMismatch));
  1163.                         }
  1164.                         break;
  1165.                 case 'array':
  1166.                         $B new Matrix($args[0]);
  1167.                         if ($this->n == $B->m{
  1168.                             $C new Matrix($this->m$B->n);
  1169.                             for($i 0$i $C->m++$i{
  1170.                                 for($j 0$j $C->n++$j{
  1171.                                     $s "0";
  1172.                                     for($k 0$k $C->n++$k{
  1173.                                         $s += $this->A[$i][$k$B->A[$k][$j];
  1174.                                     }
  1175.                                     $C->A[$i][$j$s;
  1176.                                 }
  1177.                             }
  1178.                             return $C;
  1179.                         else {
  1180.                             throw new Exception(JAMAError(MatrixDimensionMismatch));
  1181.                         }
  1182.                         return $M;
  1183.                         break;
  1184.                 case 'integer':
  1185.                         $C new Matrix($this->A);
  1186.                         for($i 0$i $C->m++$i{
  1187.                             for($j 0$j $C->n++$j{
  1188.                                 $C->A[$i][$j*= $args[0];
  1189.                             }
  1190.                         }
  1191.                         return $C;
  1192.                         break;
  1193.                 case 'double':
  1194.                         $C new Matrix($this->m$this->n);
  1195.                         for($i 0$i $C->m++$i{
  1196.                             for($j 0$j $C->n++$j{
  1197.                                 $C->A[$i][$j$args[0$this->A[$i][$j];
  1198.                             }
  1199.                         }
  1200.                         return $C;
  1201.                         break;
  1202.                 case 'float':
  1203.                         $C new Matrix($this->A);
  1204.                         for($i 0$i $C->m++$i{
  1205.                             for($j 0$j $C->n++$j{
  1206.                                 $C->A[$i][$j*= $args[0];
  1207.                             }
  1208.                         }
  1209.                         return $C;
  1210.                         break;
  1211.                 default:
  1212.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  1213.                         break;
  1214.             }
  1215.         else {
  1216.             throw new Exception(PolymorphicArgumentException);
  1217.         }
  1218.     }    //    function times()
  1219.  
  1220.  
  1221.     /**
  1222.      *    power
  1223.      *
  1224.      *    A = A ^ B
  1225.      *    @param mixed $B Matrix/Array
  1226.      *    @return Matrix Sum
  1227.      */
  1228.     public function power({
  1229.         if (func_num_args(0{
  1230.             $args func_get_args();
  1231.             $match implode(","array_map('gettype'$args));
  1232.  
  1233.             switch($match{
  1234.                 case 'object':
  1235.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  1236.                         break;
  1237.                 case 'array':
  1238.                         $M new Matrix($args[0]);
  1239.                         break;
  1240.                 default:
  1241.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  1242.                         break;
  1243.             }
  1244.             $this->checkMatrixDimensions($M);
  1245.             for($i 0$i $this->m++$i{
  1246.                 for($j 0$j $this->n++$j{
  1247.                     $validValues True;
  1248.                     $value $M->get($i$j);
  1249.                     if ((is_string($this->A[$i][$j])) && (!is_numeric($this->A[$i][$j]))) {
  1250.                         $this->A[$i][$jtrim($this->A[$i][$j],'"');
  1251.                         $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
  1252.                     }
  1253.                     if ((is_string($value)) && (!is_numeric($value))) {
  1254.                         $value trim($value,'"');
  1255.                         $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value);
  1256.                     }
  1257.                     if ($validValues{
  1258.                         $this->A[$i][$jpow($this->A[$i][$j],$value);
  1259.                     else {
  1260.                         $this->A[$i][$jPHPExcel_Calculation_Functions::NaN();
  1261.                     }
  1262.                 }
  1263.             }
  1264.             return $this;
  1265.         else {
  1266.             throw new Exception(JAMAError(PolymorphicArgumentException));
  1267.         }
  1268.     }    //    function power()
  1269.  
  1270.  
  1271.     /**
  1272.      *    concat
  1273.      *
  1274.      *    A = A & B
  1275.      *    @param mixed $B Matrix/Array
  1276.      *    @return Matrix Sum
  1277.      */
  1278.     public function concat({
  1279.         if (func_num_args(0{
  1280.             $args func_get_args();
  1281.             $match implode(","array_map('gettype'$args));
  1282.  
  1283.             switch($match{
  1284.                 case 'object':
  1285.                         if ($args[0instanceof Matrix$M $args[0]else throw new Exception(JAMAError(ArgumentTypeException))}
  1286.                 case 'array':
  1287.                         $M new Matrix($args[0]);
  1288.                         break;
  1289.                 default:
  1290.                         throw new Exception(JAMAError(PolymorphicArgumentException));
  1291.                         break;
  1292.             }
  1293.             $this->checkMatrixDimensions($M);
  1294.             for($i 0$i $this->m++$i{
  1295.                 for($j 0$j $this->n++$j{
  1296. //                    $this->A[$i][$j] = '"'.trim($this->A[$i][$j],'"').trim($M->get($i, $j),'"').'"';
  1297.                     $this->A[$i][$jtrim($this->A[$i][$j],'"').trim($M->get($i$j),'"');
  1298.                 }
  1299.             }
  1300.             return $this;
  1301.         else {
  1302.             throw new Exception(JAMAError(PolymorphicArgumentException));
  1303.         }
  1304.     }    //    function concat()
  1305.  
  1306.  
  1307.     /**
  1308.      *    chol
  1309.      *
  1310.      *    Cholesky decomposition
  1311.      *    @return Matrix Cholesky decomposition
  1312.      */
  1313.     public function chol({
  1314.         return new CholeskyDecomposition($this);
  1315.     }    //    function chol()
  1316.  
  1317.  
  1318.     /**
  1319.      *    lu
  1320.      *
  1321.      *    LU decomposition
  1322.      *    @return Matrix LU decomposition
  1323.      */
  1324.     public function lu({
  1325.         return new LUDecomposition($this);
  1326.     }    //    function lu()
  1327.  
  1328.  
  1329.     /**
  1330.      *    qr
  1331.      *
  1332.      *    QR decomposition
  1333.      *    @return Matrix QR decomposition
  1334.      */
  1335.     public function qr({
  1336.         return new QRDecomposition($this);
  1337.     }    //    function qr()
  1338.  
  1339.  
  1340.     /**
  1341.      *    eig
  1342.      *
  1343.      *    Eigenvalue decomposition
  1344.      *    @return Matrix Eigenvalue decomposition
  1345.      */
  1346.     public function eig({
  1347.         return new EigenvalueDecomposition($this);
  1348.     }    //    function eig()
  1349.  
  1350.  
  1351.     /**
  1352.      *    svd
  1353.      *
  1354.      *    Singular value decomposition
  1355.      *    @return Singular value decomposition
  1356.      */
  1357.     public function svd({
  1358.         return new SingularValueDecomposition($this);
  1359.     }    //    function svd()
  1360.  
  1361.  
  1362.     /**
  1363.      *    Solve A*X = B.
  1364.      *
  1365.      *    @param Matrix $B Right hand side
  1366.      *    @return Matrix ... Solution if A is square, least squares solution otherwise
  1367.      */
  1368.     public function solve($B{
  1369.         if ($this->m == $this->n{
  1370.             $LU new LUDecomposition($this);
  1371.             return $LU->solve($B);
  1372.         else {
  1373.             $QR new QRDecomposition($this);
  1374.             return $QR->solve($B);
  1375.         }
  1376.     }    //    function solve()
  1377.  
  1378.  
  1379.     /**
  1380.      *    Matrix inverse or pseudoinverse.
  1381.      *
  1382.      *    @return Matrix ... Inverse(A) if A is square, pseudoinverse otherwise.
  1383.      */
  1384.     public function inverse({
  1385.         return $this->solve($this->identity($this->m$this->m));
  1386.     }    //    function inverse()
  1387.  
  1388.  
  1389.     /**
  1390.      *    det
  1391.      *
  1392.      *    Calculate determinant
  1393.      *    @return float Determinant
  1394.      */
  1395.     public function det({
  1396.         $L new LUDecomposition($this);
  1397.         return $L->det();
  1398.     }    //    function det()
  1399.  
  1400.  
  1401.     /**
  1402.      *    Older debugging utility for backwards compatability.
  1403.      *
  1404.      *    @return html version of matrix
  1405.      */
  1406.     public function mprint($A$format="%01.2f"$width=2{
  1407.         $m count($A);
  1408.         $n count($A[0]);
  1409.         $spacing str_repeat('&nbsp;',$width);
  1410.  
  1411.         for ($i 0$i $m++$i{
  1412.             for ($j 0$j $n++$j{
  1413.                 $formatted sprintf($format$A[$i][$j]);
  1414.                 echo $formatted.$spacing;
  1415.             }
  1416.             echo "<br />";
  1417.         }
  1418.     }    //    function mprint()
  1419.  
  1420.  
  1421.     /**
  1422.      *    Debugging utility.
  1423.      *
  1424.      *    @return Output HTML representation of matrix
  1425.      */
  1426.     public function toHTML($width=2{
  1427.         print('<table style="background-color:#eee;">');
  1428.         for($i 0$i $this->m++$i{
  1429.             print('<tr>');
  1430.             for($j 0$j $this->n++$j{
  1431.                 print('<td style="background-color:#fff;border:1px solid #000;padding:2px;text-align:center;vertical-align:middle;">' $this->A[$i][$j'</td>');
  1432.             }
  1433.             print('</tr>');
  1434.         }
  1435.         print('</table>');
  1436.     }    //    function toHTML()
  1437.  
  1438. }    //    class Matrix

Documentation generated on Mon, 11 Jan 2010 08:12:05 +0100 by phpDocumentor 1.4.1