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

Documentation generated on Tue, 01 Jun 2010 17:05:12 +0200 by phpDocumentor 1.4.3