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

Documentation generated on Sun, 27 Feb 2011 16:32:44 -0800 by phpDocumentor 1.4.3