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

Source for file MathTrig.php

Documentation is available at MathTrig.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20.  *
  21.  * @category    PHPExcel
  22.  * @package        PHPExcel_Calculation
  23.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version        1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35.     require(PHPEXCEL_ROOT 'PHPExcel/Autoloader.php');
  36. }
  37.  
  38.  
  39. /**
  40.  * PHPExcel_Calculation_MathTrig
  41.  *
  42.  * @category    PHPExcel
  43.  * @package        PHPExcel_Calculation
  44.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  45.  */
  46.  
  47.     //
  48.     //    Private method to return an array of the factors of the input value
  49.     //
  50.     private static function _factors($value{
  51.         $startVal floor(sqrt($value));
  52.  
  53.         $factorArray array();
  54.         for ($i $startVal$i 1--$i{
  55.             if (($value $i== 0{
  56.                 $factorArray array_merge($factorArray,self::_factors($value $i));
  57.                 $factorArray array_merge($factorArray,self::_factors($i));
  58.                 if ($i <= sqrt($value)) {
  59.                     break;
  60.                 }
  61.             }
  62.         }
  63.         if (count($factorArray0{
  64.             rsort($factorArray);
  65.             return $factorArray;
  66.         else {
  67.             return array((integer) $value);
  68.         }
  69.     }    //    function _factors()
  70.  
  71.  
  72.     private static function _romanCut($num$n{
  73.         return ($num ($num $n ) ) $n;
  74.     }    //    function _romanCut()
  75.  
  76.  
  77.     /**
  78.      *    ATAN2
  79.      *
  80.      *    This function calculates the arc tangent of the two variables x and y. It is similar to
  81.      *        calculating the arc tangent of y ÷ x, except that the signs of both arguments are used
  82.      *        to determine the quadrant of the result.
  83.      *    The arctangent is the angle from the x-axis to a line containing the origin (0, 0) and a
  84.      *        point with coordinates (xCoordinate, yCoordinate). The angle is given in radians between
  85.      *        -pi and pi, excluding -pi.
  86.      *
  87.      *    Note that the Excel ATAN2() function accepts its arguments in the reverse order to the standard
  88.      *        PHP atan2() function, so we need to reverse them here before calling the PHP atan() function.
  89.      *
  90.      *    Excel Function:
  91.      *        ATAN2(xCoordinate,yCoordinate)
  92.      *
  93.      *    @access    public
  94.      *    @category Mathematical and Trigonometric Functions
  95.      *    @param    float    $xCoordinate        The x-coordinate of the point.
  96.      *    @param    float    $yCoordinate        The y-coordinate of the point.
  97.      *    @return    float    The inverse tangent of the specified x- and y-coordinates.
  98.      */
  99.     public static function ATAN2($xCoordinate$yCoordinate{
  100.         $xCoordinate    = (float) PHPExcel_Calculation_Functions::flattenSingleValue($xCoordinate);
  101.         $yCoordinate    = (float) PHPExcel_Calculation_Functions::flattenSingleValue($yCoordinate);
  102.  
  103.         if (($xCoordinate == 0&& ($yCoordinate == 0)) {
  104.             return PHPExcel_Calculation_Functions::DIV0();
  105.         }
  106.  
  107.         return atan2($yCoordinate$xCoordinate);
  108.     }    //    function REVERSE_ATAN2()
  109.  
  110.  
  111.     /**
  112.      *    CEILING
  113.      *
  114.      *    Returns number rounded up, away from zero, to the nearest multiple of significance.
  115.      *
  116.      *    @param    float    $number            Number to round
  117.      *    @param    float    $significance    Significance
  118.      *    @return    float    Rounded Number
  119.      */
  120.     public static function CEILING($number,$significance=null{
  121.         $number            PHPExcel_Calculation_Functions::flattenSingleValue($number);
  122.         $significance    PHPExcel_Calculation_Functions::flattenSingleValue($significance);
  123.  
  124.         if ((is_null($significance)) && (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) {
  125.             $significance $number/abs($number);
  126.         }
  127.  
  128.         if ((is_numeric($number)) && (is_numeric($significance))) {
  129.             if (self::SIGN($number== self::SIGN($significance)) {
  130.                 if ($significance == 0.0{
  131.                     return 0;
  132.                 }
  133.                 return ceil($number $significance$significance;
  134.             else {
  135.                 return PHPExcel_Calculation_Functions::NaN();
  136.             }
  137.         }
  138.         return PHPExcel_Calculation_Functions::VALUE();
  139.     }    //    function CEILING()
  140.  
  141.  
  142.     /**
  143.      *    COMBIN
  144.      *
  145.      *    Returns the number of combinations for a given number of items. Use COMBIN to
  146.      *    determine the total possible number of groups for a given number of items.
  147.      *
  148.      *    @param    int        $numObjs    Number of different objects
  149.      *    @param    int        $numInSet    Number of objects in each combination
  150.      *    @return    int        Number of combinations
  151.      */
  152.     public static function COMBIN($numObjs,$numInSet{
  153.         $numObjs    PHPExcel_Calculation_Functions::flattenSingleValue($numObjs);
  154.         $numInSet    PHPExcel_Calculation_Functions::flattenSingleValue($numInSet);
  155.  
  156.         if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
  157.             if ($numObjs $numInSet{
  158.                 return PHPExcel_Calculation_Functions::NaN();
  159.             elseif ($numInSet 0{
  160.                 return PHPExcel_Calculation_Functions::NaN();
  161.             }
  162.             return round(self::FACT($numObjsself::FACT($numObjs $numInSet)) self::FACT($numInSet);
  163.         }
  164.         return PHPExcel_Calculation_Functions::VALUE();
  165.     }    //    function COMBIN()
  166.  
  167.  
  168.     /**
  169.      *    EVEN
  170.      *
  171.      *    Returns number rounded up to the nearest even integer.
  172.      *
  173.      *    @param    float    $number            Number to round
  174.      *    @return    int        Rounded Number
  175.      */
  176.     public static function EVEN($number{
  177.         $number    PHPExcel_Calculation_Functions::flattenSingleValue($number);
  178.  
  179.         if (is_null($number)) {
  180.             return 0;
  181.         elseif (is_numeric($number)) {
  182.             $significance self::SIGN($number);
  183.             return (int) self::CEILING($number,$significance);
  184.         }
  185.         return PHPExcel_Calculation_Functions::VALUE();
  186.     }    //    function EVEN()
  187.  
  188.  
  189.     /**
  190.      *    FACT
  191.      *
  192.      *    Returns the factorial of a number.
  193.      *
  194.      *    @param    float    $factVal    Factorial Value
  195.      *    @return    int        Factorial
  196.      */
  197.     public static function FACT($factVal{
  198.         $factVal    PHPExcel_Calculation_Functions::flattenSingleValue($factVal);
  199.  
  200.         if (is_numeric($factVal)) {
  201.             if ($factVal 0{
  202.                 return PHPExcel_Calculation_Functions::NaN();
  203.             }
  204.             $factLoop floor($factVal);
  205.             if (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC{
  206.                 if ($factVal $factLoop{
  207.                     return PHPExcel_Calculation_Functions::NaN();
  208.                 }
  209.             }
  210.  
  211.             $factorial 1;
  212.             while ($factLoop 1{
  213.                 $factorial *= $factLoop--;
  214.             }
  215.             return $factorial ;
  216.         }
  217.         return PHPExcel_Calculation_Functions::VALUE();
  218.     }    //    function FACT()
  219.  
  220.  
  221.     /**
  222.      *    FACTDOUBLE
  223.      *
  224.      *    Returns the double factorial of a number.
  225.      *
  226.      *    @param    float    $factVal    Factorial Value
  227.      *    @return    int        Double Factorial
  228.      */
  229.     public static function FACTDOUBLE($factVal{
  230.         $factLoop    floor(PHPExcel_Calculation_Functions::flattenSingleValue($factVal));
  231.  
  232.         if (is_numeric($factLoop)) {
  233.             if ($factVal 0{
  234.                 return PHPExcel_Calculation_Functions::NaN();
  235.             }
  236.             $factorial 1;
  237.             while ($factLoop 1{
  238.                 $factorial *= $factLoop--;
  239.                 --$factLoop;
  240.             }
  241.             return $factorial ;
  242.         }
  243.         return PHPExcel_Calculation_Functions::VALUE();
  244.     }    //    function FACTDOUBLE()
  245.  
  246.  
  247.     /**
  248.      *    FLOOR
  249.      *
  250.      *    Rounds number down, toward zero, to the nearest multiple of significance.
  251.      *
  252.      *    @param    float    $number            Number to round
  253.      *    @param    float    $significance    Significance
  254.      *    @return    float    Rounded Number
  255.      */
  256.     public static function FLOOR($number,$significance=null{
  257.         $number            PHPExcel_Calculation_Functions::flattenSingleValue($number);
  258.         $significance    PHPExcel_Calculation_Functions::flattenSingleValue($significance);
  259.  
  260.         if ((is_null($significance)) && (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) {
  261.             $significance $number/abs($number);
  262.         }
  263.  
  264.         if ((is_numeric($number)) && (is_numeric($significance))) {
  265.             if ((float) $significance == 0.0{
  266.                 return PHPExcel_Calculation_Functions::DIV0();
  267.             }
  268.             if (self::SIGN($number== self::SIGN($significance)) {
  269.                 return floor($number $significance$significance;
  270.             else {
  271.                 return PHPExcel_Calculation_Functions::NaN();
  272.             }
  273.         }
  274.         return PHPExcel_Calculation_Functions::VALUE();
  275.     }    //    function FLOOR()
  276.  
  277.  
  278.     /**
  279.      *    GCD
  280.      *
  281.      *    Returns the greatest common divisor of a series of numbers
  282.      *
  283.      *    @param    $array    Values to calculate the Greatest Common Divisor
  284.      *    @return    int        Greatest Common Divisor
  285.      */
  286.     public static function GCD({
  287.         $returnValue 1;
  288.         $allPoweredFactors array();
  289.         // Loop through arguments
  290.         foreach(PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value{
  291.             if ($value == 0{
  292.                 break;
  293.             }
  294.             $myFactors self::_factors($value);
  295.             $myCountedFactors array_count_values($myFactors);
  296.             $allValuesFactors[$myCountedFactors;
  297.         }
  298.         $allValuesCount count($allValuesFactors);
  299.         $mergedArray $allValuesFactors[0];
  300.         for ($i=1;$i $allValuesCount++$i{
  301.             $mergedArray array_intersect_key($mergedArray,$allValuesFactors[$i]);
  302.         }
  303.         $mergedArrayValues count($mergedArray);
  304.         if ($mergedArrayValues == 0{
  305.             return $returnValue;
  306.         elseif ($mergedArrayValues 1{
  307.             foreach($mergedArray as $mergedKey => $mergedValue{
  308.                 foreach($allValuesFactors as $highestPowerTest{
  309.                     foreach($highestPowerTest as $testKey => $testValue{
  310.                         if (($testKey == $mergedKey&& ($testValue $mergedValue)) {
  311.                             $mergedArray[$mergedKey$testValue;
  312.                             $mergedValue $testValue;
  313.                         }
  314.                     }
  315.                 }
  316.             }
  317.  
  318.             $returnValue 1;
  319.             foreach($mergedArray as $key => $value{
  320.                 $returnValue *= pow($key,$value);
  321.             }
  322.             return $returnValue;
  323.         else {
  324.             $keys array_keys($mergedArray);
  325.             $key $keys[0];
  326.             $value $mergedArray[$key];
  327.             foreach($allValuesFactors as $testValue{
  328.                 foreach($testValue as $mergedKey => $mergedValue{
  329.                     if (($mergedKey == $key&& ($mergedValue $value)) {
  330.                         $value $mergedValue;
  331.                     }
  332.                 }
  333.             }
  334.             return pow($key,$value);
  335.         }
  336.     }    //    function GCD()
  337.  
  338.  
  339.     /**
  340.      *    INT
  341.      *
  342.      *    Casts a floating point value to an integer
  343.      *
  344.      *    @param    float    $number            Number to cast to an integer
  345.      *    @return    integer    Integer value
  346.      */
  347.     public static function INT($number{
  348.         $number    PHPExcel_Calculation_Functions::flattenSingleValue($number);
  349.  
  350.         if (is_numeric($number)) {
  351.             return (int) floor($number);
  352.         }
  353.         return PHPExcel_Calculation_Functions::VALUE();
  354.     }    //    function INT()
  355.  
  356.  
  357.     /**
  358.      *    LCM
  359.      *
  360.      *    Returns the lowest common multiplier of a series of numbers
  361.      *
  362.      *    @param    $array    Values to calculate the Lowest Common Multiplier
  363.      *    @return    int        Lowest Common Multiplier
  364.      */
  365.     public static function LCM({
  366.         $returnValue 1;
  367.         $allPoweredFactors array();
  368.         // Loop through arguments
  369.         foreach(PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value{
  370.             if (!is_numeric($value)) {
  371.                 return PHPExcel_Calculation_Functions::VALUE();
  372.             }
  373.             if ($value == 0{
  374.                 return 0;
  375.             elseif ($value 0{
  376.                 return PHPExcel_Calculation_Functions::NaN();
  377.             }
  378.             $myFactors self::_factors(floor($value));
  379.             $myCountedFactors array_count_values($myFactors);
  380.             $myPoweredFactors array();
  381.             foreach($myCountedFactors as $myCountedFactor => $myCountedPower{
  382.                 $myPoweredFactors[$myCountedFactorpow($myCountedFactor,$myCountedPower);
  383.             }
  384.             foreach($myPoweredFactors as $myPoweredValue => $myPoweredFactor{
  385.                 if (array_key_exists($myPoweredValue,$allPoweredFactors)) {
  386.                     if ($allPoweredFactors[$myPoweredValue$myPoweredFactor{
  387.                         $allPoweredFactors[$myPoweredValue$myPoweredFactor;
  388.                     }
  389.                 else {
  390.                     $allPoweredFactors[$myPoweredValue$myPoweredFactor;
  391.                 }
  392.             }
  393.         }
  394.         foreach($allPoweredFactors as $allPoweredFactor{
  395.             $returnValue *= (integer) $allPoweredFactor;
  396.         }
  397.         return $returnValue;
  398.     }    //    function LCM()
  399.  
  400.  
  401.     /**
  402.      *    LOG_BASE
  403.      *
  404.      *    Returns the logarithm of a number to a specified base. The default base is 10.
  405.      *
  406.      *    Excel Function:
  407.      *        LOG(number[,base])
  408.      *
  409.      *    @access    public
  410.      *    @category Mathematical and Trigonometric Functions
  411.      *    @param    float    $value        The positive real number for which you want the logarithm
  412.      *    @param    float    $base        The base of the logarithm. If base is omitted, it is assumed to be 10.
  413.      *    @return    float 
  414.      */
  415.     public static function LOG_BASE($number$base=10{
  416.         $number    PHPExcel_Calculation_Functions::flattenSingleValue($number);
  417.         $base    (is_null($base))    10 :    (float) PHPExcel_Calculation_Functions::flattenSingleValue($base);
  418.  
  419.         return log($number$base);
  420.     }    //    function LOG_BASE()
  421.  
  422.  
  423.     /**
  424.      * MDETERM
  425.      *
  426.      * @param    array    $matrixValues    A matrix of values
  427.      * @return    float 
  428.      */
  429.     public static function MDETERM($matrixValues{
  430.         $matrixData array();
  431.         if (!is_array($matrixValues)) $matrixValues array(array($matrixValues))}
  432.  
  433.         $row $maxColumn 0;
  434.         foreach($matrixValues as $matrixRow{
  435.             $column 0;
  436.             foreach($matrixRow as $matrixCell{
  437.                 if ((is_string($matrixCell)) || ($matrixCell === null)) {
  438.                     return PHPExcel_Calculation_Functions::VALUE();
  439.                 }
  440.                 $matrixData[$column][$row$matrixCell;
  441.                 ++$column;
  442.             }
  443.             if ($column $maxColumn$maxColumn $column}
  444.             ++$row;
  445.         }
  446.         if ($row != $maxColumnreturn PHPExcel_Calculation_Functions::VALUE()}
  447.  
  448.         try {
  449.             $matrix new PHPExcel_Shared_JAMA_Matrix($matrixData);
  450.             return $matrix->det();
  451.         catch (Exception $ex{
  452.             return PHPExcel_Calculation_Functions::VALUE();
  453.         }
  454.     }    //    function MDETERM()
  455.  
  456.  
  457.     /**
  458.      * MINVERSE
  459.      *
  460.      * @param    array    $matrixValues    A matrix of values
  461.      * @return    array 
  462.      */
  463.     public static function MINVERSE($matrixValues{
  464.         $matrixData array();
  465.         if (!is_array($matrixValues)) $matrixValues array(array($matrixValues))}
  466.  
  467.         $row $maxColumn 0;
  468.         foreach($matrixValues as $matrixRow{
  469.             $column 0;
  470.             foreach($matrixRow as $matrixCell{
  471.                 if ((is_string($matrixCell)) || ($matrixCell === null)) {
  472.                     return PHPExcel_Calculation_Functions::VALUE();
  473.                 }
  474.                 $matrixData[$column][$row$matrixCell;
  475.                 ++$column;
  476.             }
  477.             if ($column $maxColumn$maxColumn $column}
  478.             ++$row;
  479.         }
  480.         if ($row != $maxColumnreturn PHPExcel_Calculation_Functions::VALUE()}
  481.  
  482.         try {
  483.             $matrix new PHPExcel_Shared_JAMA_Matrix($matrixData);
  484.             return $matrix->inverse()->getArray();
  485.         catch (Exception $ex{
  486.             return PHPExcel_Calculation_Functions::VALUE();
  487.         }
  488.     }    //    function MINVERSE()
  489.  
  490.  
  491.     /**
  492.      * MMULT
  493.      *
  494.      * @param    array    $matrixData1    A matrix of values
  495.      * @param    array    $matrixData2    A matrix of values
  496.      * @return    array 
  497.      */
  498.     public static function MMULT($matrixData1,$matrixData2{
  499.         $matrixAData $matrixBData array();
  500.         if (!is_array($matrixData1)) $matrixData1 array(array($matrixData1))}
  501.         if (!is_array($matrixData2)) $matrixData2 array(array($matrixData2))}
  502.  
  503.         $rowA 0;
  504.         foreach($matrixData1 as $matrixRow{
  505.             $columnA 0;
  506.             foreach($matrixRow as $matrixCell{
  507.                 if ((is_string($matrixCell)) || ($matrixCell === null)) {
  508.                     return PHPExcel_Calculation_Functions::VALUE();
  509.                 }
  510.                 $matrixAData[$rowA][$columnA$matrixCell;
  511.                 ++$columnA;
  512.             }
  513.             ++$rowA;
  514.         }
  515.         try {
  516.             $matrixA new PHPExcel_Shared_JAMA_Matrix($matrixAData);
  517.             $rowB 0;
  518.             foreach($matrixData2 as $matrixRow{
  519.                 $columnB 0;
  520.                 foreach($matrixRow as $matrixCell{
  521.                     if ((is_string($matrixCell)) || ($matrixCell === null)) {
  522.                         return PHPExcel_Calculation_Functions::VALUE();
  523.                     }
  524.                     $matrixBData[$rowB][$columnB$matrixCell;
  525.                     ++$columnB;
  526.                 }
  527.                 ++$rowB;
  528.             }
  529.             $matrixB new PHPExcel_Shared_JAMA_Matrix($matrixBData);
  530.  
  531.             if (($rowA != $columnB|| ($rowB != $columnA)) {
  532.                 return PHPExcel_Calculation_Functions::VALUE();
  533.             }
  534.  
  535.             return $matrixA->times($matrixB)->getArray();
  536.         catch (Exception $ex{
  537.             return PHPExcel_Calculation_Functions::VALUE();
  538.         }
  539.     }    //    function MMULT()
  540.  
  541.  
  542.     /**
  543.      * MOD
  544.      *
  545.      * @param    int        $a        Dividend
  546.      * @param    int        $b        Divisor
  547.      * @return    int        Remainder
  548.      */
  549.     public static function MOD($a 1$b 1{
  550.         $a        PHPExcel_Calculation_Functions::flattenSingleValue($a);
  551.         $b        PHPExcel_Calculation_Functions::flattenSingleValue($b);
  552.  
  553.         if ($b == 0.0{
  554.             return PHPExcel_Calculation_Functions::DIV0();
  555.         elseif (($a 0.0&& ($b 0.0)) {
  556.             return $b fmod(abs($a),$b);
  557.         elseif (($a 0.0&& ($b 0.0)) {
  558.             return $b fmod($a,abs($b));
  559.         }
  560.  
  561.         return fmod($a,$b);
  562.     }    //    function MOD()
  563.  
  564.  
  565.     /**
  566.      *    MROUND
  567.      *
  568.      *    Rounds a number to the nearest multiple of a specified value
  569.      *
  570.      *    @param    float    $number            Number to round
  571.      *    @param    int        $multiple        Multiple to which you want to round $number
  572.      *    @return    float    Rounded Number
  573.      */
  574.     public static function MROUND($number,$multiple{
  575.         $number        PHPExcel_Calculation_Functions::flattenSingleValue($number);
  576.         $multiple    PHPExcel_Calculation_Functions::flattenSingleValue($multiple);
  577.  
  578.         if ((is_numeric($number)) && (is_numeric($multiple))) {
  579.             if ($multiple == 0{
  580.                 return 0;
  581.             }
  582.             if ((self::SIGN($number)) == (self::SIGN($multiple))) {
  583.                 $multiplier $multiple;
  584.                 return round($number $multiplier$multiplier;
  585.             }
  586.             return PHPExcel_Calculation_Functions::NaN();
  587.         }
  588.         return PHPExcel_Calculation_Functions::VALUE();
  589.     }    //    function MROUND()
  590.  
  591.  
  592.     /**
  593.      *    MULTINOMIAL
  594.      *
  595.      *    Returns the ratio of the factorial of a sum of values to the product of factorials.
  596.      *
  597.      *    @param    array of mixed        Data Series
  598.      *    @return    float 
  599.      */
  600.     public static function MULTINOMIAL({
  601.         $summer 0;
  602.         $divisor 1;
  603.         // Loop through arguments
  604.         foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg{
  605.             // Is it a numeric value?
  606.             if (is_numeric($arg)) {
  607.                 if ($arg 1{
  608.                     return PHPExcel_Calculation_Functions::NaN();
  609.                 }
  610.                 $summer += floor($arg);
  611.                 $divisor *= self::FACT($arg);
  612.             else {
  613.                 return PHPExcel_Calculation_Functions::VALUE();
  614.             }
  615.         }
  616.  
  617.         // Return
  618.         if ($summer 0{
  619.             $summer self::FACT($summer);
  620.             return $summer $divisor;
  621.         }
  622.         return 0;
  623.     }    //    function MULTINOMIAL()
  624.  
  625.  
  626.     /**
  627.      *    ODD
  628.      *
  629.      *    Returns number rounded up to the nearest odd integer.
  630.      *
  631.      *    @param    float    $number            Number to round
  632.      *    @return    int        Rounded Number
  633.      */
  634.     public static function ODD($number{
  635.         $number    PHPExcel_Calculation_Functions::flattenSingleValue($number);
  636.  
  637.         if (is_null($number)) {
  638.             return 1;
  639.         elseif (is_numeric($number)) {
  640.             $significance self::SIGN($number);
  641.             if ($significance == 0{
  642.                 return 1;
  643.             }
  644.  
  645.             $result self::CEILING($number,$significance);
  646.             if ($result == self::EVEN($result)) {
  647.                 $result += $significance;
  648.             }
  649.  
  650.             return (int) $result;
  651.         }
  652.         return PHPExcel_Calculation_Functions::VALUE();
  653.     }    //    function ODD()
  654.  
  655.  
  656.     /**
  657.      *    POWER
  658.      *
  659.      *    Computes x raised to the power y.
  660.      *
  661.      *    @param    float        $x 
  662.      *    @param    float        $y 
  663.      *    @return    float 
  664.      */
  665.     public static function POWER($x 0$y 2{
  666.         $x    PHPExcel_Calculation_Functions::flattenSingleValue($x);
  667.         $y    PHPExcel_Calculation_Functions::flattenSingleValue($y);
  668.  
  669.         // Validate parameters
  670.         if ($x == && $y <= 0{
  671.             return PHPExcel_Calculation_Functions::DIV0();
  672.         }
  673.  
  674.         // Return
  675.         return pow($x$y);
  676.     }    //    function POWER()
  677.  
  678.  
  679.     /**
  680.      *    PRODUCT
  681.      *
  682.      *    PRODUCT returns the product of all the values and cells referenced in the argument list.
  683.      *
  684.      *    Excel Function:
  685.      *        PRODUCT(value1[,value2[, ...]])
  686.      *
  687.      *    @access    public
  688.      *    @category Mathematical and Trigonometric Functions
  689.      *    @param    mixed        $arg,...        Data values
  690.      *    @return    float 
  691.      */
  692.     public static function PRODUCT({
  693.         // Return value
  694.         $returnValue null;
  695.  
  696.         // Loop through arguments
  697.         foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg{
  698.             // Is it a numeric value?
  699.             if ((is_numeric($arg)) && (!is_string($arg))) {
  700.                 if (is_null($returnValue)) {
  701.                     $returnValue $arg;
  702.                 else {
  703.                     $returnValue *= $arg;
  704.                 }
  705.             }
  706.         }
  707.  
  708.         // Return
  709.         if (is_null($returnValue)) {
  710.             return 0;
  711.         }
  712.         return $returnValue;
  713.     }    //    function PRODUCT()
  714.  
  715.  
  716.     /**
  717.      *    QUOTIENT
  718.      *
  719.      *    QUOTIENT function returns the integer portion of a division. Numerator is the divided number
  720.      *        and denominator is the divisor.
  721.      *
  722.      *    Excel Function:
  723.      *        QUOTIENT(value1[,value2[, ...]])
  724.      *
  725.      *    @access    public
  726.      *    @category Mathematical and Trigonometric Functions
  727.      *    @param    mixed        $arg,...        Data values
  728.      *    @return    float 
  729.      */
  730.     public static function QUOTIENT({
  731.         // Return value
  732.         $returnValue null;
  733.  
  734.         // Loop through arguments
  735.         foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg{
  736.             // Is it a numeric value?
  737.             if ((is_numeric($arg)) && (!is_string($arg))) {
  738.                 if (is_null($returnValue)) {
  739.                     $returnValue ($arg == 0$arg;
  740.                 else {
  741.                     if (($returnValue == 0|| ($arg == 0)) {
  742.                         $returnValue 0;
  743.                     else {
  744.                         $returnValue /= $arg;
  745.                     }
  746.                 }
  747.             }
  748.         }
  749.  
  750.         // Return
  751.         return intval($returnValue);
  752.     }    //    function QUOTIENT()
  753.  
  754.  
  755.     /**
  756.      * RAND
  757.      *
  758.      * @param    int        $min    Minimal value
  759.      * @param    int        $max    Maximal value
  760.      * @return    int        Random number
  761.      */
  762.     public static function RAND($min 0$max 0{
  763.         $min        PHPExcel_Calculation_Functions::flattenSingleValue($min);
  764.         $max        PHPExcel_Calculation_Functions::flattenSingleValue($max);
  765.  
  766.         if ($min == && $max == 0{
  767.             return (rand(0,10000000)) 10000000;
  768.         else {
  769.             return rand($min$max);
  770.         }
  771.     }    //    function RAND()
  772.  
  773.  
  774.     public static function ROMAN($aValue$style=0{
  775.         $aValue    = (integer) PHPExcel_Calculation_Functions::flattenSingleValue($aValue);
  776.         $style    (is_null($style))    :    (integer) PHPExcel_Calculation_Functions::flattenSingleValue($style);
  777.         if ((!is_numeric($aValue)) || ($aValue 0|| ($aValue >= 4000)) {
  778.             return PHPExcel_Calculation_Functions::VALUE();
  779.         }
  780.         if ($aValue == 0{
  781.             return '';
  782.         }
  783.  
  784.         $mill Array('''M''MM''MMM''MMMM''MMMMM');
  785.         $cent Array('''C''CC''CCC''CD''D''DC''DCC''DCCC''CM');
  786.         $tens Array('''X''XX''XXX''XL''L''LX''LXX''LXXX''XC');
  787.         $ones Array('''I''II''III''IV''V''VI''VII''VIII''IX');
  788.  
  789.         $roman '';
  790.         while ($aValue 5999{
  791.             $roman .= 'M';
  792.             $aValue -= 1000;
  793.         }
  794.         $m self::_romanCut($aValue1000);    $aValue %= 1000;
  795.         $c self::_romanCut($aValue100);        $aValue %= 100;
  796.         $t self::_romanCut($aValue10);        $aValue %= 10;
  797.  
  798.         return $roman.$mill[$m].$cent[$c].$tens[$t].$ones[$aValue];
  799.     }    //    function ROMAN()
  800.  
  801.  
  802.     /**
  803.      *    ROUNDUP
  804.      *
  805.      *    Rounds a number up to a specified number of decimal places
  806.      *
  807.      *    @param    float    $number            Number to round
  808.      *    @param    int        $digits            Number of digits to which you want to round $number
  809.      *    @return    float    Rounded Number
  810.      */
  811.     public static function ROUNDUP($number,$digits{
  812.         $number    PHPExcel_Calculation_Functions::flattenSingleValue($number);
  813.         $digits    PHPExcel_Calculation_Functions::flattenSingleValue($digits);
  814.  
  815.         if ((is_numeric($number)) && (is_numeric($digits))) {
  816.             $significance pow(10,$digits);
  817.             if ($number 0.0{
  818.                 return floor($number $significance$significance;
  819.             else {
  820.                 return ceil($number $significance$significance;
  821.             }
  822.         }
  823.         return PHPExcel_Calculation_Functions::VALUE();
  824.     }    //    function ROUNDUP()
  825.  
  826.  
  827.     /**
  828.      *    ROUNDDOWN
  829.      *
  830.      *    Rounds a number down to a specified number of decimal places
  831.      *
  832.      *    @param    float    $number            Number to round
  833.      *    @param    int        $digits            Number of digits to which you want to round $number
  834.      *    @return    float    Rounded Number
  835.      */
  836.     public static function ROUNDDOWN($number,$digits{
  837.         $number    PHPExcel_Calculation_Functions::flattenSingleValue($number);
  838.         $digits    PHPExcel_Calculation_Functions::flattenSingleValue($digits);
  839.  
  840.         if ((is_numeric($number)) && (is_numeric($digits))) {
  841.             $significance pow(10,$digits);
  842.             if ($number 0.0{
  843.                 return ceil($number $significance$significance;
  844.             else {
  845.                 return floor($number $significance$significance;
  846.             }
  847.         }
  848.         return PHPExcel_Calculation_Functions::VALUE();
  849.     }    //    function ROUNDDOWN()
  850.  
  851.  
  852.     /**
  853.      *    SERIESSUM
  854.      *
  855.      *    Returns the sum of a power series
  856.      *
  857.      *    @param    float            $x    Input value to the power series
  858.      *    @param    float            $n    Initial power to which you want to raise $x
  859.      *    @param    float            $m    Step by which to increase $n for each term in the series
  860.      *    @param    array of mixed        Data Series
  861.      *    @return    float 
  862.      */
  863.     public static function SERIESSUM({
  864.         // Return value
  865.         $returnValue 0;
  866.  
  867.         // Loop through arguments
  868.  
  869.         $x array_shift($aArgs);
  870.         $n array_shift($aArgs);
  871.         $m array_shift($aArgs);
  872.  
  873.         if ((is_numeric($x)) && (is_numeric($n)) && (is_numeric($m))) {
  874.             // Calculate
  875.             $i 0;
  876.             foreach($aArgs as $arg{
  877.                 // Is it a numeric value?
  878.                 if ((is_numeric($arg)) && (!is_string($arg))) {
  879.                     $returnValue += $arg pow($x,$n ($m $i++));
  880.                 else {
  881.                     return PHPExcel_Calculation_Functions::VALUE();
  882.                 }
  883.             }
  884.             // Return
  885.             return $returnValue;
  886.         }
  887.         return PHPExcel_Calculation_Functions::VALUE();
  888.     }    //    function SERIESSUM()
  889.  
  890.  
  891.     /**
  892.      *    SIGN
  893.      *
  894.      *    Determines the sign of a number. Returns 1 if the number is positive, zero (0)
  895.      *    if the number is 0, and -1 if the number is negative.
  896.      *
  897.      *    @param    float    $number            Number to round
  898.      *    @return    int        sign value
  899.      */
  900.     public static function SIGN($number{
  901.         $number    PHPExcel_Calculation_Functions::flattenSingleValue($number);
  902.  
  903.         if (is_numeric($number)) {
  904.             if ($number == 0.0{
  905.                 return 0;
  906.             }
  907.             return $number abs($number);
  908.         }
  909.         return PHPExcel_Calculation_Functions::VALUE();
  910.     }    //    function SIGN()
  911.  
  912.  
  913.     /**
  914.      *    SQRTPI
  915.      *
  916.      *    Returns the square root of (number * pi).
  917.      *
  918.      *    @param    float    $number        Number
  919.      *    @return    float    Square Root of Number * Pi
  920.      */
  921.     public static function SQRTPI($number{
  922.         $number    PHPExcel_Calculation_Functions::flattenSingleValue($number);
  923.  
  924.         if (is_numeric($number)) {
  925.             if ($number 0{
  926.                 return PHPExcel_Calculation_Functions::NaN();
  927.             }
  928.             return sqrt($number M_PI;
  929.         }
  930.         return PHPExcel_Calculation_Functions::VALUE();
  931.     }    //    function SQRTPI()
  932.  
  933.  
  934.     /**
  935.      *    SUBTOTAL
  936.      *
  937.      *    Returns a subtotal in a list or database.
  938.      *
  939.      *    @param    int        the number 1 to 11 that specifies which function to
  940.      *                     use in calculating subtotals within a list.
  941.      *    @param    array of mixed        Data Series
  942.      *    @return    float 
  943.      */
  944.     public static function SUBTOTAL({
  945.  
  946.         // Calculate
  947.         $subtotal array_shift($aArgs);
  948.  
  949.         if ((is_numeric($subtotal)) && (!is_string($subtotal))) {
  950.             switch($subtotal{
  951.                 case 1    :
  952.                     return PHPExcel_Calculation_Statistical::AVERAGE($aArgs);
  953.                     break;
  954.                 case 2    :
  955.                     return PHPExcel_Calculation_Statistical::COUNT($aArgs);
  956.                     break;
  957.                 case 3    :
  958.                     return PHPExcel_Calculation_Statistical::COUNTA($aArgs);
  959.                     break;
  960.                 case 4    :
  961.                     return PHPExcel_Calculation_Statistical::MAX($aArgs);
  962.                     break;
  963.                 case 5    :
  964.                     return PHPExcel_Calculation_Statistical::MIN($aArgs);
  965.                     break;
  966.                 case 6    :
  967.                     return self::PRODUCT($aArgs);
  968.                     break;
  969.                 case 7    :
  970.                     return PHPExcel_Calculation_Statistical::STDEV($aArgs);
  971.                     break;
  972.                 case 8    :
  973.                     return PHPExcel_Calculation_Statistical::STDEVP($aArgs);
  974.                     break;
  975.                 case 9    :
  976.                     return self::SUM($aArgs);
  977.                     break;
  978.                 case 10    :
  979.                     return PHPExcel_Calculation_Statistical::VARFunc($aArgs);
  980.                     break;
  981.                 case 11    :
  982.                     return PHPExcel_Calculation_Statistical::VARP($aArgs);
  983.                     break;
  984.             }
  985.         }
  986.         return PHPExcel_Calculation_Functions::VALUE();
  987.     }    //    function SUBTOTAL()
  988.  
  989.  
  990.     /**
  991.      *    SUM
  992.      *
  993.      *    SUM computes the sum of all the values and cells referenced in the argument list.
  994.      *
  995.      *    Excel Function:
  996.      *        SUM(value1[,value2[, ...]])
  997.      *
  998.      *    @access    public
  999.      *    @category Mathematical and Trigonometric Functions
  1000.      *    @param    mixed        $arg,...        Data values
  1001.      *    @return    float 
  1002.      */
  1003.     public static function SUM({
  1004.         // Return value
  1005.         $returnValue 0;
  1006.  
  1007.         // Loop through the arguments
  1008.         foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg{
  1009.             // Is it a numeric value?
  1010.             if ((is_numeric($arg)) && (!is_string($arg))) {
  1011.                 $returnValue += $arg;
  1012.             }
  1013.         }
  1014.  
  1015.         // Return
  1016.         return $returnValue;
  1017.     }    //    function SUM()
  1018.  
  1019.  
  1020.     /**
  1021.      *    SUMIF
  1022.      *
  1023.      *    Counts the number of cells that contain numbers within the list of arguments
  1024.      *
  1025.      *    Excel Function:
  1026.      *        SUMIF(value1[,value2[, ...]],condition)
  1027.      *
  1028.      *    @access    public
  1029.      *    @category Mathematical and Trigonometric Functions
  1030.      *    @param    mixed        $arg,...        Data values
  1031.      *    @param    string        $condition        The criteria that defines which cells will be summed.
  1032.      *    @return    float 
  1033.      */
  1034.     public static function SUMIF($aArgs,$condition,$sumArgs array()) {
  1035.         // Return value
  1036.         $returnValue 0;
  1037.  
  1038.         $aArgs PHPExcel_Calculation_Functions::flattenArray($aArgs);
  1039.         $sumArgs PHPExcel_Calculation_Functions::flattenArray($sumArgs);
  1040.         if (count($sumArgs== 0{
  1041.             $sumArgs $aArgs;
  1042.         }
  1043.         $condition PHPExcel_Calculation_Functions::_ifCondition($condition);
  1044.         // Loop through arguments
  1045.         foreach ($aArgs as $key => $arg{
  1046.             if (!is_numeric($arg)) $arg PHPExcel_Calculation::_wrapResult(strtoupper($arg))}
  1047.             $testCondition '='.$arg.$condition;
  1048.             if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1049.                 // Is it a value within our criteria
  1050.                 $returnValue += $sumArgs[$key];
  1051.             }
  1052.         }
  1053.  
  1054.         // Return
  1055.         return $returnValue;
  1056.     }    //    function SUMIF()
  1057.  
  1058.  
  1059.     /**
  1060.      * SUMPRODUCT
  1061.      *
  1062.      * @param    mixed    $value    Value to check
  1063.      * @return    float 
  1064.      */
  1065.     public static function SUMPRODUCT({
  1066.         $arrayList func_get_args();
  1067.  
  1068.         $wrkArray PHPExcel_Calculation_Functions::flattenArray(array_shift($arrayList));
  1069.         $wrkCellCount count($wrkArray);
  1070.  
  1071.         foreach($arrayList as $matrixData{
  1072.             $array2 PHPExcel_Calculation_Functions::flattenArray($matrixData);
  1073.             $count count($array2);
  1074.             if ($wrkCellCount != $count{
  1075.                 return PHPExcel_Calculation_Functions::VALUE();
  1076.             }
  1077.  
  1078.             foreach ($array2 as $i => $val{
  1079.                 if (((is_numeric($wrkArray[$i])) && (!is_string($wrkArray[$i]))) &&
  1080.                     ((is_numeric($val)) && (!is_string($val)))) {
  1081.                     $wrkArray[$i*= $val;
  1082.                 }
  1083.             }
  1084.         }
  1085.  
  1086.         return array_sum($wrkArray);
  1087.     }    //    function SUMPRODUCT()
  1088.  
  1089.  
  1090.     /**
  1091.      *    SUMSQ
  1092.      *
  1093.      *    SUMSQ returns the sum of the squares of the arguments
  1094.      *
  1095.      *    Excel Function:
  1096.      *        SUMSQ(value1[,value2[, ...]])
  1097.      *
  1098.      *    @access    public
  1099.      *    @category Mathematical and Trigonometric Functions
  1100.      *    @param    mixed        $arg,...        Data values
  1101.      *    @return    float 
  1102.      */
  1103.     public static function SUMSQ({
  1104.         // Return value
  1105.         $returnValue 0;
  1106.  
  1107.         // Loop through arguments
  1108.         foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg{
  1109.             // Is it a numeric value?
  1110.             if ((is_numeric($arg)) && (!is_string($arg))) {
  1111.                 $returnValue += ($arg $arg);
  1112.             }
  1113.         }
  1114.  
  1115.         // Return
  1116.         return $returnValue;
  1117.     }    //    function SUMSQ()
  1118.  
  1119.  
  1120.     /**
  1121.      * SUMX2MY2
  1122.      *
  1123.      * @param    mixed    $value    Value to check
  1124.      * @return    float 
  1125.      */
  1126.     public static function SUMX2MY2($matrixData1,$matrixData2{
  1127.         $array1 PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1128.         $array2 PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1129.         $count1 count($array1);
  1130.         $count2 count($array2);
  1131.         if ($count1 $count2{
  1132.             $count $count1;
  1133.         else {
  1134.             $count $count2;
  1135.         }
  1136.  
  1137.         $result 0;
  1138.         for ($i 0$i $count++$i{
  1139.             if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1140.                 ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1141.                 $result += ($array1[$i$array1[$i]($array2[$i$array2[$i]);
  1142.             }
  1143.         }
  1144.  
  1145.         return $result;
  1146.     }    //    function SUMX2MY2()
  1147.  
  1148.  
  1149.     /**
  1150.      * SUMX2PY2
  1151.      *
  1152.      * @param    mixed    $value    Value to check
  1153.      * @return    float 
  1154.      */
  1155.     public static function SUMX2PY2($matrixData1,$matrixData2{
  1156.         $array1 PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1157.         $array2 PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1158.         $count1 count($array1);
  1159.         $count2 count($array2);
  1160.         if ($count1 $count2{
  1161.             $count $count1;
  1162.         else {
  1163.             $count $count2;
  1164.         }
  1165.  
  1166.         $result 0;
  1167.         for ($i 0$i $count++$i{
  1168.             if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1169.                 ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1170.                 $result += ($array1[$i$array1[$i]($array2[$i$array2[$i]);
  1171.             }
  1172.         }
  1173.  
  1174.         return $result;
  1175.     }    //    function SUMX2PY2()
  1176.  
  1177.  
  1178.     /**
  1179.      * SUMXMY2
  1180.      *
  1181.      * @param    mixed    $value    Value to check
  1182.      * @return    float 
  1183.      */
  1184.     public static function SUMXMY2($matrixData1,$matrixData2{
  1185.         $array1 PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1186.         $array2 PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1187.         $count1 count($array1);
  1188.         $count2 count($array2);
  1189.         if ($count1 $count2{
  1190.             $count $count1;
  1191.         else {
  1192.             $count $count2;
  1193.         }
  1194.  
  1195.         $result 0;
  1196.         for ($i 0$i $count++$i{
  1197.             if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1198.                 ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1199.                 $result += ($array1[$i$array2[$i]($array1[$i$array2[$i]);
  1200.             }
  1201.         }
  1202.  
  1203.         return $result;
  1204.     }    //    function SUMXMY2()
  1205.  
  1206.  
  1207.     /**
  1208.      *    TRUNC
  1209.      *
  1210.      *    Truncates value to the number of fractional digits by number_digits.
  1211.      *
  1212.      *    @param    float        $value 
  1213.      *    @param    int            $number_digits 
  1214.      *    @return    float        Truncated value
  1215.      */
  1216.     public static function TRUNC($value 0$number_digits 0{
  1217.         $value            PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1218.         $number_digits    PHPExcel_Calculation_Functions::flattenSingleValue($number_digits);
  1219.  
  1220.         // Validate parameters
  1221.         if ($number_digits 0{
  1222.             return PHPExcel_Calculation_Functions::VALUE();
  1223.         }
  1224.  
  1225.         // Truncate
  1226.         if ($number_digits 0{
  1227.             $value $value pow(10$number_digits);
  1228.         }
  1229.         $value intval($value);
  1230.         if ($number_digits 0{
  1231.             $value $value pow(10$number_digits);
  1232.         }
  1233.  
  1234.         // Return
  1235.         return $value;
  1236.     }    //    function TRUNC()
  1237.  
  1238. }    //    class PHPExcel_Calculation_MathTrig

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