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

Source for file Statistical.php

Documentation is available at Statistical.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. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/trend/trendClass.php';
  40.  
  41.  
  42. /** LOG_GAMMA_X_MAX_VALUE */
  43. define('LOG_GAMMA_X_MAX_VALUE'2.55e305);
  44.  
  45. /** XMININ */
  46. define('XMININ'2.23e-308);
  47.  
  48. /** EPS */
  49. define('EPS'2.22e-16);
  50.  
  51. /** SQRT2PI */
  52. define('SQRT2PI'2.5066282746310005024157652848110452530069867406099);
  53.  
  54.  
  55. /**
  56.  * PHPExcel_Calculation_Statistical
  57.  *
  58.  * @category    PHPExcel
  59.  * @package        PHPExcel_Calculation
  60.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  61.  */
  62.  
  63.  
  64.     private static function _checkTrendArrays(&$array1,&$array2{
  65.         if (!is_array($array1)) $array1 array($array1)}
  66.         if (!is_array($array2)) $array2 array($array2)}
  67.  
  68.         $array1 PHPExcel_Calculation_Functions::flattenArray($array1);
  69.         $array2 PHPExcel_Calculation_Functions::flattenArray($array2);
  70.         foreach($array1 as $key => $value{
  71.             if ((is_bool($value)) || (is_string($value)) || (is_null($value))) {
  72.                 unset($array1[$key]);
  73.                 unset($array2[$key]);
  74.             }
  75.         }
  76.         foreach($array2 as $key => $value{
  77.             if ((is_bool($value)) || (is_string($value)) || (is_null($value))) {
  78.                 unset($array1[$key]);
  79.                 unset($array2[$key]);
  80.             }
  81.         }
  82.         $array1 array_merge($array1);
  83.         $array2 array_merge($array2);
  84.  
  85.         return True;
  86.     }    //    function _checkTrendArrays()
  87.  
  88.  
  89.     /**
  90.      * Beta function.
  91.      *
  92.      * @author Jaco van Kooten
  93.      *
  94.      * @param require p>0
  95.      * @param require q>0
  96.      * @return if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow
  97.      */
  98.     private static function _beta($p$q{
  99.         if ($p <= 0.0 || $q <= 0.0 || ($p $qLOG_GAMMA_X_MAX_VALUE{
  100.             return 0.0;
  101.         else {
  102.             return exp(self::_logBeta($p$q));
  103.         }
  104.     }    //    function _beta()
  105.  
  106.  
  107.     /**
  108.      * Incomplete beta function
  109.      *
  110.      * @author Jaco van Kooten
  111.      * @author Paul Meagher
  112.      *
  113.      *  The computation is based on formulas from Numerical Recipes, Chapter 6.4 (W.H. Press et al, 1992).
  114.      * @param require 0<=x<=1
  115.      * @param require p>0
  116.      * @param require q>0
  117.      * @return if x<0, p<=0, q<=0 or p+q>2.55E305 and 1 if x>1 to avoid errors and over/underflow
  118.      */
  119.     private static function _incompleteBeta($x$p$q{
  120.         if ($x <= 0.0{
  121.             return 0.0;
  122.         elseif ($x >= 1.0{
  123.             return 1.0;
  124.         elseif (($p <= 0.0|| ($q <= 0.0|| (($p $qLOG_GAMMA_X_MAX_VALUE)) {
  125.             return 0.0;
  126.         }
  127.         $beta_gam exp((self::_logBeta($p$q)) $p log($x$q log(1.0 $x));
  128.         if ($x ($p 1.0($p $q 2.0)) {
  129.             return $beta_gam self::_betaFraction($x$p$q$p;
  130.         else {
  131.             return 1.0 ($beta_gam self::_betaFraction($x$q$p$q);
  132.         }
  133.     }    //    function _incompleteBeta()
  134.  
  135.  
  136.     // Function cache for _logBeta function
  137.     private static $_logBetaCache_p            0.0;
  138.     private static $_logBetaCache_q            0.0;
  139.     private static $_logBetaCache_result    0.0;
  140.  
  141.     /**
  142.      *    The natural logarithm of the beta function.
  143.      *    @param require p>0
  144.      *    @param require q>0
  145.      *    @return if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow
  146.      *    @author Jaco van Kooten
  147.      */
  148.     private static function _logBeta($p$q{
  149.         if ($p != self::$_logBetaCache_p || $q != self::$_logBetaCache_q{
  150.             self::$_logBetaCache_p $p;
  151.             self::$_logBetaCache_q $q;
  152.             if (($p <= 0.0|| ($q <= 0.0|| (($p $qLOG_GAMMA_X_MAX_VALUE)) {
  153.                 self::$_logBetaCache_result 0.0;
  154.             else {
  155.                 self::$_logBetaCache_result self::_logGamma($pself::_logGamma($qself::_logGamma($p $q);
  156.             }
  157.         }
  158.         return self::$_logBetaCache_result;
  159.     }    //    function _logBeta()
  160.  
  161.  
  162.     /**
  163.      *    Evaluates of continued fraction part of incomplete beta function.
  164.      *    Based on an idea from Numerical Recipes (W.H. Press et al, 1992).
  165.      *    @author Jaco van Kooten
  166.      */
  167.     private static function _betaFraction($x$p$q{
  168.         $c 1.0;
  169.         $sum_pq $p $q;
  170.         $p_plus $p 1.0;
  171.         $p_minus $p 1.0;
  172.         $h 1.0 $sum_pq $x $p_plus;
  173.         if (abs($hXMININ{
  174.             $h XMININ;
  175.         }
  176.         $h 1.0 $h;
  177.         $frac $h;
  178.         $m     1;
  179.         $delta 0.0;
  180.         while ($m <= MAX_ITERATIONS && abs($delta-1.0PRECISION {
  181.             $m2 $m;
  182.             // even index for d
  183.             $d $m ($q $m$x ( ($p_minus $m2($p $m2));
  184.             $h 1.0 $d $h;
  185.             if (abs($hXMININ{
  186.                 $h XMININ;
  187.             }
  188.             $h 1.0 $h;
  189.             $c 1.0 $d $c;
  190.             if (abs($cXMININ{
  191.                 $c XMININ;
  192.             }
  193.             $frac *= $h $c;
  194.             // odd index for d
  195.             $d = -($p $m($sum_pq $m$x (($p $m2($p_plus $m2));
  196.             $h 1.0 $d $h;
  197.             if (abs($hXMININ{
  198.                 $h XMININ;
  199.             }
  200.             $h 1.0 $h;
  201.             $c 1.0 $d $c;
  202.             if (abs($cXMININ{
  203.                 $c XMININ;
  204.             }
  205.             $delta $h $c;
  206.             $frac *= $delta;
  207.             ++$m;
  208.         }
  209.         return $frac;
  210.     }    //    function _betaFraction()
  211.  
  212.  
  213.     /**
  214.      * logGamma function
  215.      *
  216.      * @version 1.1
  217.      * @author Jaco van Kooten
  218.      *
  219.      *  Original author was Jaco van Kooten. Ported to PHP by Paul Meagher.
  220.      *
  221.      *  The natural logarithm of the gamma function. <br />
  222.      *  Based on public domain NETLIB (Fortran) code by W. J. Cody and L. Stoltz <br />
  223.      *  Applied Mathematics Division <br />
  224.      *  Argonne National Laboratory <br />
  225.      *  Argonne, IL 60439 <br />
  226.      *  <p>
  227.      *  References:
  228.      *  <ol>
  229.      *  <li>W. J. Cody and K. E. Hillstrom, 'Chebyshev Approximations for the Natural
  230.      *      Logarithm of the Gamma Function,' Math. Comp. 21, 1967, pp. 198-203.</li>
  231.      *  <li>K. E. Hillstrom, ANL/AMD Program ANLC366S, DGAMMA/DLGAMA, May, 1969.</li>
  232.      *  <li>Hart, Et. Al., Computer Approximations, Wiley and sons, New York, 1968.</li>
  233.      *  </ol>
  234.      *  </p>
  235.      *  <p>
  236.      *  From the original documentation:
  237.      *  </p>
  238.      *  <p>
  239.      *  This routine calculates the LOG(GAMMA) function for a positive real argument X.
  240.      *  Computation is based on an algorithm outlined in references 1 and 2.
  241.      *  The program uses rational functions that theoretically approximate LOG(GAMMA)
  242.      *  to at least 18 significant decimal digits. The approximation for X > 12 is from
  243.      *  reference 3, while approximations for X < 12.0 are similar to those in reference
  244.      *  1, but are unpublished. The accuracy achieved depends on the arithmetic system,
  245.      *  the compiler, the intrinsic functions, and proper selection of the
  246.      *  machine-dependent constants.
  247.      *  </p>
  248.      *  <p>
  249.      *  Error returns: <br />
  250.      *  The program returns the value XINF for X .LE. 0.0 or when overflow would occur.
  251.      *  The computation is believed to be free of underflow and overflow.
  252.      *  </p>
  253.      * @return MAX_VALUE for x < 0.0 or when overflow would occur, i.e. x > 2.55E305
  254.      */
  255.  
  256.     // Function cache for logGamma
  257.     private static $_logGammaCache_result    0.0;
  258.     private static $_logGammaCache_x        0.0;
  259.  
  260.     private static function _logGamma($x{
  261.         // Log Gamma related constants
  262.         static $lg_d1 = -0.5772156649015328605195174;
  263.         static $lg_d2 0.4227843350984671393993777;
  264.         static $lg_d4 1.791759469228055000094023;
  265.  
  266.         static $lg_p1 array(    4.945235359296727046734888,
  267.                                 201.8112620856775083915565,
  268.                                 2290.838373831346393026739,
  269.                                 11319.67205903380828685045,
  270.                                 28557.24635671635335736389,
  271.                                 38484.96228443793359990269,
  272.                                 26377.48787624195437963534,
  273.                                 7225.813979700288197698961 );
  274.         static $lg_p2 array(    4.974607845568932035012064,
  275.                                 542.4138599891070494101986,
  276.                                 15506.93864978364947665077,
  277.                                 184793.2904445632425417223,
  278.                                 1088204.76946882876749847,
  279.                                 3338152.967987029735917223,
  280.                                 5106661.678927352456275255,
  281.                                 3074109.054850539556250927 );
  282.         static $lg_p4 array(    14745.02166059939948905062,
  283.                                 2426813.369486704502836312,
  284.                                 121475557.4045093227939592,
  285.                                 2663432449.630976949898078,
  286.                                 29403789566.34553899906876,
  287.                                 170266573776.5398868392998,
  288.                                 492612579337.743088758812,
  289.                                 560625185622.3951465078242 );
  290.  
  291.         static $lg_q1 array(    67.48212550303777196073036,
  292.                                 1113.332393857199323513008,
  293.                                 7738.757056935398733233834,
  294.                                 27639.87074403340708898585,
  295.                                 54993.10206226157329794414,
  296.                                 61611.22180066002127833352,
  297.                                 36351.27591501940507276287,
  298.                                 8785.536302431013170870835 );
  299.         static $lg_q2 array(    183.0328399370592604055942,
  300.                                 7765.049321445005871323047,
  301.                                 133190.3827966074194402448,
  302.                                 1136705.821321969608938755,
  303.                                 5267964.117437946917577538,
  304.                                 13467014.54311101692290052,
  305.                                 17827365.30353274213975932,
  306.                                 9533095.591844353613395747 );
  307.         static $lg_q4 array(    2690.530175870899333379843,
  308.                                 639388.5654300092398984238,
  309.                                 41355999.30241388052042842,
  310.                                 1120872109.61614794137657,
  311.                                 14886137286.78813811542398,
  312.                                 101680358627.2438228077304,
  313.                                 341747634550.7377132798597,
  314.                                 446315818741.9713286462081 );
  315.  
  316.         static $lg_c  array(    -0.001910444077728,
  317.                                 8.4171387781295e-4,
  318.                                 -5.952379913043012e-4,
  319.                                 7.93650793500350248e-4,
  320.                                 -0.002777777777777681622553,
  321.                                 0.08333333333333333331554247,
  322.                                 0.0057083835261 );
  323.  
  324.     // Rough estimate of the fourth root of logGamma_xBig
  325.     static $lg_frtbig 2.25e76;
  326.     static $pnt68     0.6796875;
  327.  
  328.  
  329.     if ($x == self::$_logGammaCache_x{
  330.         return self::$_logGammaCache_result;
  331.     }
  332.     $y $x;
  333.     if ($y 0.0 && $y <= LOG_GAMMA_X_MAX_VALUE{
  334.         if ($y <= EPS{
  335.             $res = -log(y);
  336.         elseif ($y <= 1.5{
  337.             // ---------------------
  338.             //    EPS .LT. X .LE. 1.5
  339.             // ---------------------
  340.             if ($y $pnt68{
  341.                 $corr = -log($y);
  342.                 $xm1 $y;
  343.             else {
  344.                 $corr 0.0;
  345.                 $xm1 $y 1.0;
  346.             }
  347.             if ($y <= 0.5 || $y >= $pnt68{
  348.                 $xden 1.0;
  349.                 $xnum 0.0;
  350.                 for ($i 0$i 8++$i{
  351.                     $xnum $xnum $xm1 $lg_p1[$i];
  352.                     $xden $xden $xm1 $lg_q1[$i];
  353.                 }
  354.                 $res $corr $xm1 ($lg_d1 $xm1 ($xnum $xden));
  355.             else {
  356.                 $xm2 $y 1.0;
  357.                 $xden 1.0;
  358.                 $xnum 0.0;
  359.                 for ($i 0$i 8++$i{
  360.                     $xnum $xnum $xm2 $lg_p2[$i];
  361.                     $xden $xden $xm2 $lg_q2[$i];
  362.                 }
  363.                 $res $corr $xm2 ($lg_d2 $xm2 ($xnum $xden));
  364.             }
  365.         elseif ($y <= 4.0{
  366.             // ---------------------
  367.             //    1.5 .LT. X .LE. 4.0
  368.             // ---------------------
  369.             $xm2 $y 2.0;
  370.             $xden 1.0;
  371.             $xnum 0.0;
  372.             for ($i 0$i 8++$i{
  373.                 $xnum $xnum $xm2 $lg_p2[$i];
  374.                 $xden $xden $xm2 $lg_q2[$i];
  375.             }
  376.             $res $xm2 ($lg_d2 $xm2 ($xnum $xden));
  377.         elseif ($y <= 12.0{
  378.             // ----------------------
  379.             //    4.0 .LT. X .LE. 12.0
  380.             // ----------------------
  381.             $xm4 $y 4.0;
  382.             $xden = -1.0;
  383.             $xnum 0.0;
  384.             for ($i 0$i 8++$i{
  385.                 $xnum $xnum $xm4 $lg_p4[$i];
  386.                 $xden $xden $xm4 $lg_q4[$i];
  387.             }
  388.             $res $lg_d4 $xm4 ($xnum $xden);
  389.         else {
  390.             // ---------------------------------
  391.             //    Evaluate for argument .GE. 12.0
  392.             // ---------------------------------
  393.             $res 0.0;
  394.             if ($y <= $lg_frtbig{
  395.                 $res $lg_c[6];
  396.                 $ysq $y $y;
  397.                 for ($i 0$i 6++$i)
  398.                     $res $res $ysq $lg_c[$i];
  399.                 }
  400.                 $res /= $y;
  401.                 $corr log($y);
  402.                 $res $res log(SQRT2PI0.5 $corr;
  403.                 $res += $y ($corr 1.0);
  404.             }
  405.         else {
  406.             // --------------------------
  407.             //    Return for bad arguments
  408.             // --------------------------
  409.             $res MAX_VALUE;
  410.         }
  411.         // ------------------------------
  412.         //    Final adjustments and return
  413.         // ------------------------------
  414.         self::$_logGammaCache_x $x;
  415.         self::$_logGammaCache_result $res;
  416.         return $res;
  417.     }    //    function _logGamma()
  418.  
  419.  
  420.     //
  421.     //    Private implementation of the incomplete Gamma function
  422.     //
  423.     private static function _incompleteGamma($a,$x{
  424.         static $max 32;
  425.         $summer 0;
  426.         for ($n=0$n<=$max++$n{
  427.             $divisor $a;
  428.             for ($i=1$i<=$n++$i{
  429.                 $divisor *= ($a $i);
  430.             }
  431.             $summer += (pow($x,$n$divisor);
  432.         }
  433.         return pow($x,$aexp(0-$x$summer;
  434.     }    //    function _incompleteGamma()
  435.  
  436.  
  437.     //
  438.     //    Private implementation of the Gamma function
  439.     //
  440.     private static function _gamma($data{
  441.         if ($data == 0.0return 0;
  442.  
  443.         static $p0 1.000000000190015;
  444.         static $p array => 76.18009172947146,
  445.                             => -86.50532032941677,
  446.                             => 24.01409824083091,
  447.                             => -1.231739572450155,
  448.                             => 1.208650973866179e-3,
  449.                             => -5.395239384953e-6
  450.                           );
  451.  
  452.         $y $x $data;
  453.         $tmp $x 5.5;
  454.         $tmp -= ($x 0.5log($tmp);
  455.  
  456.         $summer $p0;
  457.         for ($j=1;$j<=6;++$j{
  458.             $summer += ($p[$j/ ++$y);
  459.         }
  460.         return exp($tmp log(SQRT2PI $summer $x));
  461.     }    //    function _gamma()
  462.  
  463.  
  464.     /***************************************************************************
  465.      *                                inverse_ncdf.php
  466.      *                            -------------------
  467.      *    begin                : Friday, January 16, 2004
  468.      *    copyright            : (C) 2004 Michael Nickerson
  469.      *    email                : nickersonm@yahoo.com
  470.      *
  471.      ***************************************************************************/
  472.     private static function _inverse_ncdf($p{
  473.         //    Inverse ncdf approximation by Peter J. Acklam, implementation adapted to
  474.         //    PHP by Michael Nickerson, using Dr. Thomas Ziegler's C implementation as
  475.         //    a guide. http://home.online.no/~pjacklam/notes/invnorm/index.html
  476.         //    I have not checked the accuracy of this implementation. Be aware that PHP
  477.         //    will truncate the coeficcients to 14 digits.
  478.  
  479.         //    You have permission to use and distribute this function freely for
  480.         //    whatever purpose you want, but please show common courtesy and give credit
  481.         //    where credit is due.
  482.  
  483.         //    Input paramater is $p - probability - where 0 < p < 1.
  484.  
  485.         //    Coefficients in rational approximations
  486.         static $a array(    => -3.969683028665376e+01,
  487.                             => 2.209460984245205e+02,
  488.                             => -2.759285104469687e+02,
  489.                             => 1.383577518672690e+02,
  490.                             => -3.066479806614716e+01,
  491.                             => 2.506628277459239e+00
  492.                          );
  493.  
  494.         static $b array(    => -5.447609879822406e+01,
  495.                             => 1.615858368580409e+02,
  496.                             => -1.556989798598866e+02,
  497.                             => 6.680131188771972e+01,
  498.                             => -1.328068155288572e+01
  499.                          );
  500.  
  501.         static $c array(    => -7.784894002430293e-03,
  502.                             => -3.223964580411365e-01,
  503.                             => -2.400758277161838e+00,
  504.                             => -2.549732539343734e+00,
  505.                             => 4.374664141464968e+00,
  506.                             => 2.938163982698783e+00
  507.                          );
  508.  
  509.         static $d array(    => 7.784695709041462e-03,
  510.                             => 3.224671290700398e-01,
  511.                             => 2.445134137142996e+00,
  512.                             => 3.754408661907416e+00
  513.                          );
  514.  
  515.         //    Define lower and upper region break-points.
  516.         $p_low 0.02425;            //Use lower region approx. below this
  517.         $p_high $p_low;        //Use upper region approx. above this
  518.  
  519.         if ($p && $p $p_low{
  520.             //    Rational approximation for lower region.
  521.             $q sqrt(-log($p));
  522.             return ((((($c[1$q $c[2]$q $c[3]$q $c[4]$q $c[5]$q $c[6]/
  523.                     (((($d[1$q $d[2]$q $d[3]$q $d[4]$q 1);
  524.         elseif ($p_low <= $p && $p <= $p_high{
  525.             //    Rational approximation for central region.
  526.             $q $p 0.5;
  527.             $r $q $q;
  528.             return ((((($a[1$r $a[2]$r $a[3]$r $a[4]$r $a[5]$r $a[6]$q /
  529.                    ((((($b[1$r $b[2]$r $b[3]$r $b[4]$r $b[5]$r 1);
  530.         elseif ($p_high $p && $p 1{
  531.             //    Rational approximation for upper region.
  532.             $q sqrt(-log($p));
  533.             return -((((($c[1$q $c[2]$q $c[3]$q $c[4]$q $c[5]$q $c[6]/
  534.                      (((($d[1$q $d[2]$q $d[3]$q $d[4]$q 1);
  535.         }
  536.         //    If 0 < p < 1, return a null value
  537.         return PHPExcel_Calculation_Functions::NULL();
  538.     }    //    function _inverse_ncdf()
  539.  
  540.  
  541.     private static function _inverse_ncdf2($prob{
  542.         //    Approximation of inverse standard normal CDF developed by
  543.         //    B. Moro, "The Full Monte," Risk 8(2), Feb 1995, 57-58.
  544.  
  545.         $a1 2.50662823884;
  546.         $a2 = -18.61500062529;
  547.         $a3 41.39119773534;
  548.         $a4 = -25.44106049637;
  549.  
  550.         $b1 = -8.4735109309;
  551.         $b2 23.08336743743;
  552.         $b3 = -21.06224101826;
  553.         $b4 3.13082909833;
  554.  
  555.         $c1 0.337475482272615;
  556.         $c2 0.976169019091719;
  557.         $c3 0.160797971491821;
  558.         $c4 2.76438810333863E-02;
  559.         $c5 3.8405729373609E-03;
  560.         $c6 3.951896511919E-04;
  561.         $c7 3.21767881768E-05;
  562.         $c8 2.888167364E-07;
  563.         $c9 3.960315187E-07;
  564.  
  565.         $y $prob 0.5;
  566.         if (abs($y0.42{
  567.             $z ($y $y);
  568.             $z $y ((($a4 $z $a3$z $a2$z $a1(((($b4 $z $b3$z $b2$z $b1$z 1);
  569.         else {
  570.             if ($y 0{
  571.                 $z log(-log($prob));
  572.             else {
  573.                 $z log(-log($prob));
  574.             }
  575.             $z $c1 $z ($c2 $z ($c3 $z ($c4 $z ($c5 $z ($c6 $z ($c7 $z ($c8 $z $c9)))))));
  576.             if ($y 0{
  577.                 $z = -$z;
  578.             }
  579.         }
  580.         return $z;
  581.     }    //    function _inverse_ncdf2()
  582.  
  583.  
  584.     private static function _inverse_ncdf3($p{
  585.         //    ALGORITHM AS241 APPL. STATIST. (1988) VOL. 37, NO. 3.
  586.         //    Produces the normal deviate Z corresponding to a given lower
  587.         //    tail area of P; Z is accurate to about 1 part in 10**16.
  588.         //
  589.         //    This is a PHP version of the original FORTRAN code that can
  590.         //    be found at http://lib.stat.cmu.edu/apstat/
  591.         $split1 0.425;
  592.         $split2 5;
  593.         $const1 0.180625;
  594.         $const2 1.6;
  595.  
  596.         //    coefficients for p close to 0.5
  597.         $a0 3.3871328727963666080;
  598.         $a1 1.3314166789178437745E+2;
  599.         $a2 1.9715909503065514427E+3;
  600.         $a3 1.3731693765509461125E+4;
  601.         $a4 4.5921953931549871457E+4;
  602.         $a5 6.7265770927008700853E+4;
  603.         $a6 3.3430575583588128105E+4;
  604.         $a7 2.5090809287301226727E+3;
  605.  
  606.         $b1 4.2313330701600911252E+1;
  607.         $b2 6.8718700749205790830E+2;
  608.         $b3 5.3941960214247511077E+3;
  609.         $b4 2.1213794301586595867E+4;
  610.         $b5 3.9307895800092710610E+4;
  611.         $b6 2.8729085735721942674E+4;
  612.         $b7 5.2264952788528545610E+3;
  613.  
  614.         //    coefficients for p not close to 0, 0.5 or 1.
  615.         $c0 1.42343711074968357734;
  616.         $c1 4.63033784615654529590;
  617.         $c2 5.76949722146069140550;
  618.         $c3 3.64784832476320460504;
  619.         $c4 1.27045825245236838258;
  620.         $c5 2.41780725177450611770E-1;
  621.         $c6 2.27238449892691845833E-2;
  622.         $c7 7.74545014278341407640E-4;
  623.  
  624.         $d1 2.05319162663775882187;
  625.         $d2 1.67638483018380384940;
  626.         $d3 6.89767334985100004550E-1;
  627.         $d4 1.48103976427480074590E-1;
  628.         $d5 1.51986665636164571966E-2;
  629.         $d6 5.47593808499534494600E-4;
  630.         $d7 1.05075007164441684324E-9;
  631.  
  632.         //    coefficients for p near 0 or 1.
  633.         $e0 6.65790464350110377720;
  634.         $e1 5.46378491116411436990;
  635.         $e2 1.78482653991729133580;
  636.         $e3 2.96560571828504891230E-1;
  637.         $e4 2.65321895265761230930E-2;
  638.         $e5 1.24266094738807843860E-3;
  639.         $e6 2.71155556874348757815E-5;
  640.         $e7 2.01033439929228813265E-7;
  641.  
  642.         $f1 5.99832206555887937690E-1;
  643.         $f2 1.36929880922735805310E-1;
  644.         $f3 1.48753612908506148525E-2;
  645.         $f4 7.86869131145613259100E-4;
  646.         $f5 1.84631831751005468180E-5;
  647.         $f6 1.42151175831644588870E-7;
  648.         $f7 2.04426310338993978564E-15;
  649.  
  650.         $q $p 0.5;
  651.  
  652.         //    computation for p close to 0.5
  653.         if (abs($q<= split1{
  654.             $R $const1 $q $q;
  655.             $z $q ((((((($a7 $R $a6$R $a5$R $a4$R $a3$R $a2$R $a1$R $a0/
  656.                       ((((((($b7 $R $b6$R $b5$R $b4$R $b3$R $b2$R $b1$R 1);
  657.         else {
  658.             if ($q 0{
  659.                 $R $p;
  660.             else {
  661.                 $R $p;
  662.             }
  663.             $R pow(-log($R),2);
  664.  
  665.             //    computation for p not close to 0, 0.5 or 1.
  666.             If ($R <= $split2{
  667.                 $R $R $const2;
  668.                 $z ((((((($c7 $R $c6$R $c5$R $c4$R $c3$R $c2$R $c1$R $c0/
  669.                      ((((((($d7 $R $d6$R $d5$R $d4$R $d3$R $d2$R $d1$R 1);
  670.             else {
  671.             //    computation for p near 0 or 1.
  672.                 $R $R $split2;
  673.                 $z ((((((($e7 $R $e6$R $e5$R $e4$R $e3$R $e2$R $e1$R $e0/
  674.                      ((((((($f7 $R $f6$R $f5$R $f4$R $f3$R $f2$R $f1$R 1);
  675.             }
  676.             if ($q 0{
  677.                 $z = -$z;
  678.             }
  679.         }
  680.         return $z;
  681.     }    //    function _inverse_ncdf3()
  682.  
  683.  
  684.     /**
  685.      *    AVEDEV
  686.      *
  687.      *    Returns the average of the absolute deviations of data points from their mean.
  688.      *    AVEDEV is a measure of the variability in a data set.
  689.      *
  690.      *    Excel Function:
  691.      *        AVEDEV(value1[,value2[, ...]])
  692.      *
  693.      *    @access    public
  694.      *    @category Statistical Functions
  695.      *    @param    mixed        $arg,...        Data values
  696.      *    @return    float 
  697.      */
  698.     public static function AVEDEV({
  699.  
  700.         // Return value
  701.         $returnValue null;
  702.  
  703.         $aMean self::AVERAGE($aArgs);
  704.         if ($aMean != PHPExcel_Calculation_Functions::DIV0()) {
  705.             $aCount 0;
  706.             foreach ($aArgs as $k => $arg{
  707.                 if ((is_bool($arg)) &&
  708.                     ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  709.                     $arg = (integer) $arg;
  710.                 }
  711.                 // Is it a numeric value?
  712.                 if ((is_numeric($arg)) && (!is_string($arg))) {
  713.                     if (is_null($returnValue)) {
  714.                         $returnValue abs($arg $aMean);
  715.                     else {
  716.                         $returnValue += abs($arg $aMean);
  717.                     }
  718.                     ++$aCount;
  719.                 }
  720.             }
  721.  
  722.             // Return
  723.             if ($aCount == 0{
  724.                 return PHPExcel_Calculation_Functions::DIV0();
  725.             }
  726.             return $returnValue $aCount;
  727.         }
  728.         return PHPExcel_Calculation_Functions::NaN();
  729.     }    //    function AVEDEV()
  730.  
  731.  
  732.     /**
  733.      *    AVERAGE
  734.      *
  735.      *    Returns the average (arithmetic mean) of the arguments
  736.      *
  737.      *    Excel Function:
  738.      *        AVERAGE(value1[,value2[, ...]])
  739.      *
  740.      *    @access    public
  741.      *    @category Statistical Functions
  742.      *    @param    mixed        $arg,...        Data values
  743.      *    @return    float 
  744.      */
  745.     public static function AVERAGE({
  746.         $returnValue $aCount 0;
  747.  
  748.         // Loop through arguments
  749.         foreach (PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()) as $k => $arg{
  750.             if ((is_bool($arg)) &&
  751.                 ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  752.                 $arg = (integer) $arg;
  753.             }
  754.             // Is it a numeric value?
  755.             if ((is_numeric($arg)) && (!is_string($arg))) {
  756.                 if (is_null($returnValue)) {
  757.                     $returnValue $arg;
  758.                 else {
  759.                     $returnValue += $arg;
  760.                 }
  761.                 ++$aCount;
  762.             }
  763.         }
  764.  
  765.         // Return
  766.         if ($aCount 0{
  767.             return $returnValue $aCount;
  768.         else {
  769.             return PHPExcel_Calculation_Functions::DIV0();
  770.         }
  771.     }    //    function AVERAGE()
  772.  
  773.  
  774.     /**
  775.      *    AVERAGEA
  776.      *
  777.      *    Returns the average of its arguments, including numbers, text, and logical values
  778.      *
  779.      *    Excel Function:
  780.      *        AVERAGEA(value1[,value2[, ...]])
  781.      *
  782.      *    @access    public
  783.      *    @category Statistical Functions
  784.      *    @param    mixed        $arg,...        Data values
  785.      *    @return    float 
  786.      */
  787.     public static function AVERAGEA({
  788.         // Return value
  789.         $returnValue null;
  790.  
  791.         $aCount 0;
  792.         // Loop through arguments
  793.         foreach (PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()) as $k => $arg{
  794.             if ((is_bool($arg)) &&
  795.                 (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  796.             else {
  797.                 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg&& ($arg != '')))) {
  798.                     if (is_bool($arg)) {
  799.                         $arg = (integer) $arg;
  800.                     elseif (is_string($arg)) {
  801.                         $arg 0;
  802.                     }
  803.                     if (is_null($returnValue)) {
  804.                         $returnValue $arg;
  805.                     else {
  806.                         $returnValue += $arg;
  807.                     }
  808.                     ++$aCount;
  809.                 }
  810.             }
  811.         }
  812.  
  813.         // Return
  814.         if ($aCount 0{
  815.             return $returnValue $aCount;
  816.         else {
  817.             return PHPExcel_Calculation_Functions::DIV0();
  818.         }
  819.     }    //    function AVERAGEA()
  820.  
  821.  
  822.     /**
  823.      *    AVERAGEIF
  824.      *
  825.      *    Returns the average value from a range of cells that contain numbers within the list of arguments
  826.      *
  827.      *    Excel Function:
  828.      *        AVERAGEIF(value1[,value2[, ...]],condition)
  829.      *
  830.      *    @access    public
  831.      *    @category Mathematical and Trigonometric Functions
  832.      *    @param    mixed        $arg,...        Data values
  833.      *    @param    string        $condition        The criteria that defines which cells will be checked.
  834.      *    @return    float 
  835.      */
  836.     public static function AVERAGEIF($aArgs,$condition,$averageArgs array()) {
  837.         // Return value
  838.         $returnValue 0;
  839.  
  840.         $aArgs PHPExcel_Calculation_Functions::flattenArray($aArgs);
  841.         $averageArgs PHPExcel_Calculation_Functions::flattenArray($averageArgs);
  842.         if (count($averageArgs== 0{
  843.             $averageArgs $aArgs;
  844.         }
  845.         $condition PHPExcel_Calculation_Functions::_ifCondition($condition);
  846.         // Loop through arguments
  847.         $aCount 0;
  848.         foreach ($aArgs as $key => $arg{
  849.             if (!is_numeric($arg)) $arg PHPExcel_Calculation::_wrapResult(strtoupper($arg))}
  850.             $testCondition '='.$arg.$condition;
  851.             if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  852.                 if ((is_null($returnValue)) || ($arg $returnValue)) {
  853.                     $returnValue += $arg;
  854.                     ++$aCount;
  855.                 }
  856.             }
  857.         }
  858.  
  859.         // Return
  860.         if ($aCount 0{
  861.             return $returnValue $aCount;
  862.         else {
  863.             return PHPExcel_Calculation_Functions::DIV0();
  864.         }
  865.     }    //    function AVERAGEIF()
  866.  
  867.  
  868.     /**
  869.      * BETADIST
  870.      *
  871.      * Returns the beta distribution.
  872.      *
  873.      * @param    float        $value            Value at which you want to evaluate the distribution
  874.      * @param    float        $alpha            Parameter to the distribution
  875.      * @param    float        $beta            Parameter to the distribution
  876.      * @param    boolean        $cumulative 
  877.      * @return    float 
  878.      *
  879.      */
  880.     public static function BETADIST($value,$alpha,$beta,$rMin=0,$rMax=1{
  881.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  882.         $alpha    PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  883.         $beta    PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  884.         $rMin    PHPExcel_Calculation_Functions::flattenSingleValue($rMin);
  885.         $rMax    PHPExcel_Calculation_Functions::flattenSingleValue($rMax);
  886.  
  887.         if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
  888.             if (($value $rMin|| ($value $rMax|| ($alpha <= 0|| ($beta <= 0|| ($rMin == $rMax)) {
  889.                 return PHPExcel_Calculation_Functions::NaN();
  890.             }
  891.             if ($rMin $rMax{
  892.                 $tmp $rMin;
  893.                 $rMin $rMax;
  894.                 $rMax $tmp;
  895.             }
  896.             $value -= $rMin;
  897.             $value /= ($rMax $rMin);
  898.             return self::_incompleteBeta($value,$alpha,$beta);
  899.         }
  900.         return PHPExcel_Calculation_Functions::VALUE();
  901.     }    //    function BETADIST()
  902.  
  903.  
  904.     /**
  905.      * BETAINV
  906.      *
  907.      * Returns the inverse of the beta distribution.
  908.      *
  909.      * @param    float        $probability    Probability at which you want to evaluate the distribution
  910.      * @param    float        $alpha            Parameter to the distribution
  911.      * @param    float        $beta            Parameter to the distribution
  912.      * @param    boolean        $cumulative 
  913.      * @return    float 
  914.      *
  915.      */
  916.     public static function BETAINV($probability,$alpha,$beta,$rMin=0,$rMax=1{
  917.         $probability    PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  918.         $alpha            PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  919.         $beta            PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  920.         $rMin            PHPExcel_Calculation_Functions::flattenSingleValue($rMin);
  921.         $rMax            PHPExcel_Calculation_Functions::flattenSingleValue($rMax);
  922.  
  923.         if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
  924.             if (($alpha <= 0|| ($beta <= 0|| ($rMin == $rMax|| ($probability <= 0|| ($probability 1)) {
  925.                 return PHPExcel_Calculation_Functions::NaN();
  926.             }
  927.             if ($rMin $rMax{
  928.                 $tmp $rMin;
  929.                 $rMin $rMax;
  930.                 $rMax $tmp;
  931.             }
  932.             $a 0;
  933.             $b 2;
  934.  
  935.             $i 0;
  936.             while ((($b $aPRECISION&& ($i++ < MAX_ITERATIONS)) {
  937.                 $guess ($a $b2;
  938.                 $result self::BETADIST($guess$alpha$beta);
  939.                 if (($result == $probability|| ($result == 0)) {
  940.                     $b $a;
  941.                 elseif ($result $probability{
  942.                     $b $guess;
  943.                 else {
  944.                     $a $guess;
  945.                 }
  946.             }
  947.             if ($i == MAX_ITERATIONS{
  948.                 return PHPExcel_Calculation_Functions::NA();
  949.             }
  950.             return round($rMin $guess ($rMax $rMin),12);
  951.         }
  952.         return PHPExcel_Calculation_Functions::VALUE();
  953.     }    //    function BETAINV()
  954.  
  955.  
  956.     /**
  957.      *    BINOMDIST
  958.      *
  959.      *    Returns the individual term binomial distribution probability. Use BINOMDIST in problems with
  960.      *    a fixed number of tests or trials, when the outcomes of any trial are only success or failure,
  961.      *    when trials are independent, and when the probability of success is constant throughout the
  962.      *    experiment. For example, BINOMDIST can calculate the probability that two of the next three
  963.      *    babies born are male.
  964.      *
  965.      *    @param    float        $value            Number of successes in trials
  966.      *    @param    float        $trials            Number of trials
  967.      *    @param    float        $probability    Probability of success on each trial
  968.      *    @param    boolean        $cumulative 
  969.      *    @return    float 
  970.      *
  971.      *    @todo    Cumulative distribution function
  972.      *
  973.      */
  974.     public static function BINOMDIST($value$trials$probability$cumulative{
  975.         $value            floor(PHPExcel_Calculation_Functions::flattenSingleValue($value));
  976.         $trials            floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials));
  977.         $probability    PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  978.  
  979.         if ((is_numeric($value)) && (is_numeric($trials)) && (is_numeric($probability))) {
  980.             if (($value 0|| ($value $trials)) {
  981.                 return PHPExcel_Calculation_Functions::NaN();
  982.             }
  983.             if (($probability 0|| ($probability 1)) {
  984.                 return PHPExcel_Calculation_Functions::NaN();
  985.             }
  986.             if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  987.                 if ($cumulative{
  988.                     $summer 0;
  989.                     for ($i 0$i <= $value++$i{
  990.                         $summer += PHPExcel_Calculation_MathTrig::COMBIN($trials,$ipow($probability,$ipow($probability,$trials $i);
  991.                     }
  992.                     return $summer;
  993.                 else {
  994.                     return PHPExcel_Calculation_MathTrig::COMBIN($trials,$valuepow($probability,$valuepow($probability,$trials $value;
  995.                 }
  996.             }
  997.         }
  998.         return PHPExcel_Calculation_Functions::VALUE();
  999.     }    //    function BINOMDIST()
  1000.  
  1001.  
  1002.     /**
  1003.      *    CHIDIST
  1004.      *
  1005.      *    Returns the one-tailed probability of the chi-squared distribution.
  1006.      *
  1007.      *    @param    float        $value            Value for the function
  1008.      *    @param    float        $degrees        degrees of freedom
  1009.      *    @return    float 
  1010.      */
  1011.     public static function CHIDIST($value$degrees{
  1012.         $value        PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1013.         $degrees    floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  1014.  
  1015.         if ((is_numeric($value)) && (is_numeric($degrees))) {
  1016.             if ($degrees 1{
  1017.                 return PHPExcel_Calculation_Functions::NaN();
  1018.             }
  1019.             if ($value 0{
  1020.                 if (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC{
  1021.                     return 1;
  1022.                 }
  1023.                 return PHPExcel_Calculation_Functions::NaN();
  1024.             }
  1025.             return (self::_incompleteGamma($degrees/2,$value/2self::_gamma($degrees/2));
  1026.         }
  1027.         return PHPExcel_Calculation_Functions::VALUE();
  1028.     }    //    function CHIDIST()
  1029.  
  1030.  
  1031.     /**
  1032.      *    CHIINV
  1033.      *
  1034.      *    Returns the one-tailed probability of the chi-squared distribution.
  1035.      *
  1036.      *    @param    float        $probability    Probability for the function
  1037.      *    @param    float        $degrees        degrees of freedom
  1038.      *    @return    float 
  1039.      */
  1040.     public static function CHIINV($probability$degrees{
  1041.         $probability    PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  1042.         $degrees        floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  1043.  
  1044.         if ((is_numeric($probability)) && (is_numeric($degrees))) {
  1045.  
  1046.             $xLo 100;
  1047.             $xHi 0;
  1048.  
  1049.             $x $xNew 1;
  1050.             $dx    1;
  1051.             $i 0;
  1052.  
  1053.             while ((abs($dxPRECISION&& ($i++ < MAX_ITERATIONS)) {
  1054.                 // Apply Newton-Raphson step
  1055.                 $result self::CHIDIST($x$degrees);
  1056.                 $error $result $probability;
  1057.                 if ($error == 0.0{
  1058.                     $dx 0;
  1059.                 elseif ($error 0.0{
  1060.                     $xLo $x;
  1061.                 else {
  1062.                     $xHi $x;
  1063.                 }
  1064.                 // Avoid division by zero
  1065.                 if ($result != 0.0{
  1066.                     $dx $error $result;
  1067.                     $xNew $x $dx;
  1068.                 }
  1069.                 // If the NR fails to converge (which for example may be the
  1070.                 // case if the initial guess is too rough) we apply a bisection
  1071.                 // step to determine a more narrow interval around the root.
  1072.                 if (($xNew $xLo|| ($xNew $xHi|| ($result == 0.0)) {
  1073.                     $xNew ($xLo $xHi2;
  1074.                     $dx $xNew $x;
  1075.                 }
  1076.                 $x $xNew;
  1077.             }
  1078.             if ($i == MAX_ITERATIONS{
  1079.                 return PHPExcel_Calculation_Functions::NA();
  1080.             }
  1081.             return round($x,12);
  1082.         }
  1083.         return PHPExcel_Calculation_Functions::VALUE();
  1084.     }    //    function CHIINV()
  1085.  
  1086.  
  1087.     /**
  1088.      * CONFIDENCE
  1089.      *
  1090.      * Returns the confidence interval for a population mean
  1091.      *
  1092.      * @param    float        $alpha 
  1093.      * @param    float        $stdDev        Standard Deviation
  1094.      * @param    float        $size 
  1095.      * @return    float 
  1096.      *
  1097.      */
  1098.     public static function CONFIDENCE($alpha,$stdDev,$size{
  1099.         $alpha    PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  1100.         $stdDev    PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  1101.         $size    floor(PHPExcel_Calculation_Functions::flattenSingleValue($size));
  1102.  
  1103.         if ((is_numeric($alpha)) && (is_numeric($stdDev)) && (is_numeric($size))) {
  1104.             if (($alpha <= 0|| ($alpha >= 1)) {
  1105.                 return PHPExcel_Calculation_Functions::NaN();
  1106.             }
  1107.             if (($stdDev <= 0|| ($size 1)) {
  1108.                 return PHPExcel_Calculation_Functions::NaN();
  1109.             }
  1110.             return self::NORMSINV($alpha 2$stdDev sqrt($size);
  1111.         }
  1112.         return PHPExcel_Calculation_Functions::VALUE();
  1113.     }    //    function CONFIDENCE()
  1114.  
  1115.  
  1116.     /**
  1117.      *    CORREL
  1118.      *
  1119.      *    Returns covariance, the average of the products of deviations for each data point pair.
  1120.      *
  1121.      *    @param    array of mixed        Data Series Y
  1122.      *    @param    array of mixed        Data Series X
  1123.      *    @return    float 
  1124.      */
  1125.     public static function CORREL($yValues,$xValues=null{
  1126.         if ((is_null($xValues)) || (!is_array($yValues)) || (!is_array($xValues))) {
  1127.             return PHPExcel_Calculation_Functions::VALUE();
  1128.         }
  1129.         if (!self::_checkTrendArrays($yValues,$xValues)) {
  1130.             return PHPExcel_Calculation_Functions::VALUE();
  1131.         }
  1132.         $yValueCount count($yValues);
  1133.         $xValueCount count($xValues);
  1134.  
  1135.         if (($yValueCount == 0|| ($yValueCount != $xValueCount)) {
  1136.             return PHPExcel_Calculation_Functions::NA();
  1137.         elseif ($yValueCount == 1{
  1138.             return PHPExcel_Calculation_Functions::DIV0();
  1139.         }
  1140.  
  1141.         $bestFitLinear trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1142.         return $bestFitLinear->getCorrelation();
  1143.     }    //    function CORREL()
  1144.  
  1145.  
  1146.     /**
  1147.      *    COUNT
  1148.      *
  1149.      *    Counts the number of cells that contain numbers within the list of arguments
  1150.      *
  1151.      *    Excel Function:
  1152.      *        COUNT(value1[,value2[, ...]])
  1153.      *
  1154.      *    @access    public
  1155.      *    @category Statistical Functions
  1156.      *    @param    mixed        $arg,...        Data values
  1157.      *    @return    int 
  1158.      */
  1159.     public static function COUNT({
  1160.         // Return value
  1161.         $returnValue 0;
  1162.  
  1163.         // Loop through arguments
  1164.         foreach ($aArgs as $k => $arg{
  1165.             if ((is_bool($arg)) &&
  1166.                 ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  1167.                 $arg = (integer) $arg;
  1168.             }
  1169.             // Is it a numeric value?
  1170.             if ((is_numeric($arg)) && (!is_string($arg))) {
  1171.                 ++$returnValue;
  1172.             }
  1173.         }
  1174.  
  1175.         // Return
  1176.         return $returnValue;
  1177.     }    //    function COUNT()
  1178.  
  1179.  
  1180.     /**
  1181.      *    COUNTA
  1182.      *
  1183.      *    Counts the number of cells that are not empty within the list of arguments
  1184.      *
  1185.      *    Excel Function:
  1186.      *        COUNTA(value1[,value2[, ...]])
  1187.      *
  1188.      *    @access    public
  1189.      *    @category Statistical Functions
  1190.      *    @param    mixed        $arg,...        Data values
  1191.      *    @return    int 
  1192.      */
  1193.     public static function COUNTA({
  1194.         // Return value
  1195.         $returnValue 0;
  1196.  
  1197.         // Loop through arguments
  1198.         foreach ($aArgs as $arg{
  1199.             // Is it a numeric, boolean or string value?
  1200.             if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg&& ($arg != '')))) {
  1201.                 ++$returnValue;
  1202.             }
  1203.         }
  1204.  
  1205.         // Return
  1206.         return $returnValue;
  1207.     }    //    function COUNTA()
  1208.  
  1209.  
  1210.     /**
  1211.      *    COUNTBLANK
  1212.      *
  1213.      *    Counts the number of empty cells within the list of arguments
  1214.      *
  1215.      *    Excel Function:
  1216.      *        COUNTBLANK(value1[,value2[, ...]])
  1217.      *
  1218.      *    @access    public
  1219.      *    @category Statistical Functions
  1220.      *    @param    mixed        $arg,...        Data values
  1221.      *    @return    int 
  1222.      */
  1223.     public static function COUNTBLANK({
  1224.         // Return value
  1225.         $returnValue 0;
  1226.  
  1227.         // Loop through arguments
  1228.         foreach ($aArgs as $arg{
  1229.             // Is it a blank cell?
  1230.             if ((is_null($arg)) || ((is_string($arg)) && ($arg == ''))) {
  1231.                 ++$returnValue;
  1232.             }
  1233.         }
  1234.  
  1235.         // Return
  1236.         return $returnValue;
  1237.     }    //    function COUNTBLANK()
  1238.  
  1239.  
  1240.     /**
  1241.      *    COUNTIF
  1242.      *
  1243.      *    Counts the number of cells that contain numbers within the list of arguments
  1244.      *
  1245.      *    Excel Function:
  1246.      *        COUNTIF(value1[,value2[, ...]],condition)
  1247.      *
  1248.      *    @access    public
  1249.      *    @category Statistical Functions
  1250.      *    @param    mixed        $arg,...        Data values
  1251.      *    @param    string        $condition        The criteria that defines which cells will be counted.
  1252.      *    @return    int 
  1253.      */
  1254.     public static function COUNTIF($aArgs,$condition{
  1255.         // Return value
  1256.         $returnValue 0;
  1257.  
  1258.         $aArgs PHPExcel_Calculation_Functions::flattenArray($aArgs);
  1259.         $condition PHPExcel_Calculation_Functions::_ifCondition($condition);
  1260.         // Loop through arguments
  1261.         foreach ($aArgs as $arg{
  1262.             if (!is_numeric($arg)) $arg PHPExcel_Calculation::_wrapResult(strtoupper($arg))}
  1263.             $testCondition '='.$arg.$condition;
  1264.             if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1265.                 // Is it a value within our criteria
  1266.                 ++$returnValue;
  1267.             }
  1268.         }
  1269.  
  1270.         // Return
  1271.         return $returnValue;
  1272.     }    //    function COUNTIF()
  1273.  
  1274.  
  1275.     /**
  1276.      *    COVAR
  1277.      *
  1278.      *    Returns covariance, the average of the products of deviations for each data point pair.
  1279.      *
  1280.      *    @param    array of mixed        Data Series Y
  1281.      *    @param    array of mixed        Data Series X
  1282.      *    @return    float 
  1283.      */
  1284.     public static function COVAR($yValues,$xValues{
  1285.         if (!self::_checkTrendArrays($yValues,$xValues)) {
  1286.             return PHPExcel_Calculation_Functions::VALUE();
  1287.         }
  1288.         $yValueCount count($yValues);
  1289.         $xValueCount count($xValues);
  1290.  
  1291.         if (($yValueCount == 0|| ($yValueCount != $xValueCount)) {
  1292.             return PHPExcel_Calculation_Functions::NA();
  1293.         elseif ($yValueCount == 1{
  1294.             return PHPExcel_Calculation_Functions::DIV0();
  1295.         }
  1296.  
  1297.         $bestFitLinear trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1298.         return $bestFitLinear->getCovariance();
  1299.     }    //    function COVAR()
  1300.  
  1301.  
  1302.     /**
  1303.      *    CRITBINOM
  1304.      *
  1305.      *    Returns the smallest value for which the cumulative binomial distribution is greater
  1306.      *    than or equal to a criterion value
  1307.      *
  1308.      *    See http://support.microsoft.com/kb/828117/ for details of the algorithm used
  1309.      *
  1310.      *    @param    float        $trials            number of Bernoulli trials
  1311.      *    @param    float        $probability    probability of a success on each trial
  1312.      *    @param    float        $alpha            criterion value
  1313.      *    @return    int 
  1314.      *
  1315.      *    @todo    Warning. This implementation differs from the algorithm detailed on the MS
  1316.      *             web site in that $CumPGuessMinus1 = $CumPGuess - 1 rather than $CumPGuess - $PGuess
  1317.      *             This eliminates a potential endless loop error, but may have an adverse affect on the
  1318.      *             accuracy of the function (although all my tests have so far returned correct results).
  1319.      *
  1320.      */
  1321.     public static function CRITBINOM($trials$probability$alpha{
  1322.         $trials            floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials));
  1323.         $probability    PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  1324.         $alpha            PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  1325.  
  1326.         if ((is_numeric($trials)) && (is_numeric($probability)) && (is_numeric($alpha))) {
  1327.             if ($trials 0{
  1328.                 return PHPExcel_Calculation_Functions::NaN();
  1329.             }
  1330.             if (($probability 0|| ($probability 1)) {
  1331.                 return PHPExcel_Calculation_Functions::NaN();
  1332.             }
  1333.             if (($alpha 0|| ($alpha 1)) {
  1334.                 return PHPExcel_Calculation_Functions::NaN();
  1335.             }
  1336.             if ($alpha <= 0.5{
  1337.                 $t sqrt(log(($alpha $alpha)));
  1338.                 $trialsApprox ($t (2.515517 0.802853 $t 0.010328 $t $t(1.432788 $t 0.189269 $t $t 0.001308 $t $t $t));
  1339.             else {
  1340.                 $t sqrt(log(pow($alpha,2)));
  1341.                 $trialsApprox $t (2.515517 0.802853 $t 0.010328 $t $t(1.432788 $t 0.189269 $t $t 0.001308 $t $t $t);
  1342.             }
  1343.             $Guess floor($trials $probability $trialsApprox sqrt($trials $probability ($probability)));
  1344.             if ($Guess 0{
  1345.                 $Guess 0;
  1346.             elseif ($Guess $trials{
  1347.                 $Guess $trials;
  1348.             }
  1349.  
  1350.             $TotalUnscaledProbability $UnscaledPGuess $UnscaledCumPGuess 0.0;
  1351.             $EssentiallyZero 10e-12;
  1352.  
  1353.             $m floor($trials $probability);
  1354.             ++$TotalUnscaledProbability;
  1355.             if ($m == $Guess++$UnscaledPGuess}
  1356.             if ($m <= $Guess++$UnscaledCumPGuess}
  1357.  
  1358.             $PreviousValue 1;
  1359.             $Done False;
  1360.             $k $m 1;
  1361.             while ((!$Done&& ($k <= $trials)) {
  1362.                 $CurrentValue $PreviousValue ($trials $k 1$probability ($k ($probability));
  1363.                 $TotalUnscaledProbability += $CurrentValue;
  1364.                 if ($k == $Guess$UnscaledPGuess += $CurrentValue}
  1365.                 if ($k <= $Guess$UnscaledCumPGuess += $CurrentValue}
  1366.                 if ($CurrentValue <= $EssentiallyZero$Done True}
  1367.                 $PreviousValue $CurrentValue;
  1368.                 ++$k;
  1369.             }
  1370.  
  1371.             $PreviousValue 1;
  1372.             $Done False;
  1373.             $k $m 1;
  1374.             while ((!$Done&& ($k >= 0)) {
  1375.                 $CurrentValue $PreviousValue $k ($probability(($trials $k$probability);
  1376.                 $TotalUnscaledProbability += $CurrentValue;
  1377.                 if ($k == $Guess$UnscaledPGuess += $CurrentValue}
  1378.                 if ($k <= $Guess$UnscaledCumPGuess += $CurrentValue}
  1379.                 if ($CurrentValue <= $EssentiallyZero$Done True}
  1380.                 $PreviousValue $CurrentValue;
  1381.                 --$k;
  1382.             }
  1383.  
  1384.             $PGuess $UnscaledPGuess $TotalUnscaledProbability;
  1385.             $CumPGuess $UnscaledCumPGuess $TotalUnscaledProbability;
  1386.  
  1387. //            $CumPGuessMinus1 = $CumPGuess - $PGuess;
  1388.             $CumPGuessMinus1 $CumPGuess 1;
  1389.  
  1390.             while (True{
  1391.                 if (($CumPGuessMinus1 $alpha&& ($CumPGuess >= $alpha)) {
  1392.                     return $Guess;
  1393.                 elseif (($CumPGuessMinus1 $alpha&& ($CumPGuess $alpha)) {
  1394.                     $PGuessPlus1 $PGuess ($trials $Guess$probability $Guess ($probability);
  1395.                     $CumPGuessMinus1 $CumPGuess;
  1396.                     $CumPGuess $CumPGuess $PGuessPlus1;
  1397.                     $PGuess $PGuessPlus1;
  1398.                     ++$Guess;
  1399.                 elseif (($CumPGuessMinus1 >= $alpha&& ($CumPGuess >= $alpha)) {
  1400.                     $PGuessMinus1 $PGuess $Guess ($probability($trials $Guess 1$probability;
  1401.                     $CumPGuess $CumPGuessMinus1;
  1402.                     $CumPGuessMinus1 $CumPGuessMinus1 $PGuess;
  1403.                     $PGuess $PGuessMinus1;
  1404.                     --$Guess;
  1405.                 }
  1406.             }
  1407.         }
  1408.         return PHPExcel_Calculation_Functions::VALUE();
  1409.     }    //    function CRITBINOM()
  1410.  
  1411.  
  1412.     /**
  1413.      *    DEVSQ
  1414.      *
  1415.      *    Returns the sum of squares of deviations of data points from their sample mean.
  1416.      *
  1417.      *    Excel Function:
  1418.      *        DEVSQ(value1[,value2[, ...]])
  1419.      *
  1420.      *    @access    public
  1421.      *    @category Statistical Functions
  1422.      *    @param    mixed        $arg,...        Data values
  1423.      *    @return    float 
  1424.      */
  1425.     public static function DEVSQ({
  1426.  
  1427.         // Return value
  1428.         $returnValue null;
  1429.  
  1430.         $aMean self::AVERAGE($aArgs);
  1431.         if ($aMean != PHPExcel_Calculation_Functions::DIV0()) {
  1432.             $aCount = -1;
  1433.             foreach ($aArgs as $k => $arg{
  1434.                 // Is it a numeric value?
  1435.                 if ((is_bool($arg)) &&
  1436.                     ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  1437.                     $arg = (integer) $arg;
  1438.                 }
  1439.                 if ((is_numeric($arg)) && (!is_string($arg))) {
  1440.                     if (is_null($returnValue)) {
  1441.                         $returnValue pow(($arg $aMean),2);
  1442.                     else {
  1443.                         $returnValue += pow(($arg $aMean),2);
  1444.                     }
  1445.                     ++$aCount;
  1446.                 }
  1447.             }
  1448.  
  1449.             // Return
  1450.             if (is_null($returnValue)) {
  1451.                 return PHPExcel_Calculation_Functions::NaN();
  1452.             else {
  1453.                 return $returnValue;
  1454.             }
  1455.         }
  1456.         return self::NA();
  1457.     }    //    function DEVSQ()
  1458.  
  1459.  
  1460.     /**
  1461.      *    EXPONDIST
  1462.      *
  1463.      *    Returns the exponential distribution. Use EXPONDIST to model the time between events,
  1464.      *    such as how long an automated bank teller takes to deliver cash. For example, you can
  1465.      *    use EXPONDIST to determine the probability that the process takes at most 1 minute.
  1466.      *
  1467.      *    @param    float        $value            Value of the function
  1468.      *    @param    float        $lambda            The parameter value
  1469.      *    @param    boolean        $cumulative 
  1470.      *    @return    float 
  1471.      */
  1472.     public static function EXPONDIST($value$lambda$cumulative{
  1473.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1474.         $lambda    PHPExcel_Calculation_Functions::flattenSingleValue($lambda);
  1475.         $cumulative    PHPExcel_Calculation_Functions::flattenSingleValue($cumulative);
  1476.  
  1477.         if ((is_numeric($value)) && (is_numeric($lambda))) {
  1478.             if (($value 0|| ($lambda 0)) {
  1479.                 return PHPExcel_Calculation_Functions::NaN();
  1480.             }
  1481.             if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  1482.                 if ($cumulative{
  1483.                     return exp(0-$value*$lambda);
  1484.                 else {
  1485.                     return $lambda exp(0-$value*$lambda);
  1486.                 }
  1487.             }
  1488.         }
  1489.         return PHPExcel_Calculation_Functions::VALUE();
  1490.     }    //    function EXPONDIST()
  1491.  
  1492.  
  1493.     /**
  1494.      *    FISHER
  1495.      *
  1496.      *    Returns the Fisher transformation at x. This transformation produces a function that
  1497.      *    is normally distributed rather than skewed. Use this function to perform hypothesis
  1498.      *    testing on the correlation coefficient.
  1499.      *
  1500.      *    @param    float        $value 
  1501.      *    @return    float 
  1502.      */
  1503.     public static function FISHER($value{
  1504.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1505.  
  1506.         if (is_numeric($value)) {
  1507.             if (($value <= -1|| ($value >= 1)) {
  1508.                 return PHPExcel_Calculation_Functions::NaN();
  1509.             }
  1510.             return 0.5 log((1+$value)/(1-$value));
  1511.         }
  1512.         return PHPExcel_Calculation_Functions::VALUE();
  1513.     }    //    function FISHER()
  1514.  
  1515.  
  1516.     /**
  1517.      *    FISHERINV
  1518.      *
  1519.      *    Returns the inverse of the Fisher transformation. Use this transformation when
  1520.      *    analyzing correlations between ranges or arrays of data. If y = FISHER(x), then
  1521.      *    FISHERINV(y) = x.
  1522.      *
  1523.      *    @param    float        $value 
  1524.      *    @return    float 
  1525.      */
  1526.     public static function FISHERINV($value{
  1527.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1528.  
  1529.         if (is_numeric($value)) {
  1530.             return (exp($value1(exp($value1);
  1531.         }
  1532.         return PHPExcel_Calculation_Functions::VALUE();
  1533.     }    //    function FISHERINV()
  1534.  
  1535.  
  1536.     /**
  1537.      *    FORECAST
  1538.      *
  1539.      *    Calculates, or predicts, a future value by using existing values. The predicted value is a y-value for a given x-value.
  1540.      *
  1541.      *    @param    float                Value of X for which we want to find Y
  1542.      *    @param    array of mixed        Data Series Y
  1543.      *    @param    array of mixed        Data Series X
  1544.      *    @return    float 
  1545.      */
  1546.     public static function FORECAST($xValue,$yValues,$xValues{
  1547.         $xValue    PHPExcel_Calculation_Functions::flattenSingleValue($xValue);
  1548.         if (!is_numeric($xValue)) {
  1549.             return PHPExcel_Calculation_Functions::VALUE();
  1550.         }
  1551.  
  1552.         if (!self::_checkTrendArrays($yValues,$xValues)) {
  1553.             return PHPExcel_Calculation_Functions::VALUE();
  1554.         }
  1555.         $yValueCount count($yValues);
  1556.         $xValueCount count($xValues);
  1557.  
  1558.         if (($yValueCount == 0|| ($yValueCount != $xValueCount)) {
  1559.             return PHPExcel_Calculation_Functions::NA();
  1560.         elseif ($yValueCount == 1{
  1561.             return PHPExcel_Calculation_Functions::DIV0();
  1562.         }
  1563.  
  1564.         $bestFitLinear trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1565.         return $bestFitLinear->getValueOfYForX($xValue);
  1566.     }    //    function FORECAST()
  1567.  
  1568.  
  1569.     /**
  1570.      * GAMMADIST
  1571.      *
  1572.      * Returns the gamma distribution.
  1573.      *
  1574.      * @param    float        $value            Value at which you want to evaluate the distribution
  1575.      * @param    float        $a                Parameter to the distribution
  1576.      * @param    float        $b                Parameter to the distribution
  1577.      * @param    boolean        $cumulative 
  1578.      * @return    float 
  1579.      *
  1580.      */
  1581.     public static function GAMMADIST($value,$a,$b,$cumulative{
  1582.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1583.         $a        PHPExcel_Calculation_Functions::flattenSingleValue($a);
  1584.         $b        PHPExcel_Calculation_Functions::flattenSingleValue($b);
  1585.  
  1586.         if ((is_numeric($value)) && (is_numeric($a)) && (is_numeric($b))) {
  1587.             if (($value 0|| ($a <= 0|| ($b <= 0)) {
  1588.                 return PHPExcel_Calculation_Functions::NaN();
  1589.             }
  1590.             if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  1591.                 if ($cumulative{
  1592.                     return self::_incompleteGamma($a,$value $bself::_gamma($a);
  1593.                 else {
  1594.                     return ((pow($b,$aself::_gamma($a))) pow($value,$a-1exp(0-($value $b));
  1595.                 }
  1596.             }
  1597.         }
  1598.         return PHPExcel_Calculation_Functions::VALUE();
  1599.     }    //    function GAMMADIST()
  1600.  
  1601.  
  1602.     /**
  1603.      * GAMMAINV
  1604.      *
  1605.      * Returns the inverse of the beta distribution.
  1606.      *
  1607.      * @param    float        $probability    Probability at which you want to evaluate the distribution
  1608.      * @param    float        $alpha            Parameter to the distribution
  1609.      * @param    float        $beta            Parameter to the distribution
  1610.      * @return    float 
  1611.      *
  1612.      */
  1613.     public static function GAMMAINV($probability,$alpha,$beta{
  1614.         $probability    PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  1615.         $alpha            PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  1616.         $beta            PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  1617.  
  1618.         if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta))) {
  1619.             if (($alpha <= 0|| ($beta <= 0|| ($probability 0|| ($probability 1)) {
  1620.                 return PHPExcel_Calculation_Functions::NaN();
  1621.             }
  1622.  
  1623.             $xLo 0;
  1624.             $xHi $alpha $beta 5;
  1625.  
  1626.             $x $xNew 1;
  1627.             $error $pdf 0;
  1628.             $dx    1024;
  1629.             $i 0;
  1630.  
  1631.             while ((abs($dxPRECISION&& ($i++ < MAX_ITERATIONS)) {
  1632.                 // Apply Newton-Raphson step
  1633.                 $error self::GAMMADIST($x$alpha$betaTrue$probability;
  1634.                 if ($error 0.0{
  1635.                     $xLo $x;
  1636.                 else {
  1637.                     $xHi $x;
  1638.                 }
  1639.                 $pdf self::GAMMADIST($x$alpha$betaFalse);
  1640.                 // Avoid division by zero
  1641.                 if ($pdf != 0.0{
  1642.                     $dx $error $pdf;
  1643.                     $xNew $x $dx;
  1644.                 }
  1645.                 // If the NR fails to converge (which for example may be the
  1646.                 // case if the initial guess is too rough) we apply a bisection
  1647.                 // step to determine a more narrow interval around the root.
  1648.                 if (($xNew $xLo|| ($xNew $xHi|| ($pdf == 0.0)) {
  1649.                     $xNew ($xLo $xHi2;
  1650.                     $dx $xNew $x;
  1651.                 }
  1652.                 $x $xNew;
  1653.             }
  1654.             if ($i == MAX_ITERATIONS{
  1655.                 return PHPExcel_Calculation_Functions::NA();
  1656.             }
  1657.             return $x;
  1658.         }
  1659.         return PHPExcel_Calculation_Functions::VALUE();
  1660.     }    //    function GAMMAINV()
  1661.  
  1662.  
  1663.     /**
  1664.      * GAMMALN
  1665.      *
  1666.      * Returns the natural logarithm of the gamma function.
  1667.      *
  1668.      * @param    float        $value 
  1669.      * @return    float 
  1670.      */
  1671.     public static function GAMMALN($value{
  1672.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1673.  
  1674.         if (is_numeric($value)) {
  1675.             if ($value <= 0{
  1676.                 return PHPExcel_Calculation_Functions::NaN();
  1677.             }
  1678.             return log(self::_gamma($value));
  1679.         }
  1680.         return PHPExcel_Calculation_Functions::VALUE();
  1681.     }    //    function GAMMALN()
  1682.  
  1683.  
  1684.     /**
  1685.      *    GEOMEAN
  1686.      *
  1687.      *    Returns the geometric mean of an array or range of positive data. For example, you
  1688.      *        can use GEOMEAN to calculate average growth rate given compound interest with
  1689.      *        variable rates.
  1690.      *
  1691.      *    Excel Function:
  1692.      *        GEOMEAN(value1[,value2[, ...]])
  1693.      *
  1694.      *    @access    public
  1695.      *    @category Statistical Functions
  1696.      *    @param    mixed        $arg,...        Data values
  1697.      *    @return    float 
  1698.      */
  1699.     public static function GEOMEAN({
  1700.  
  1701.         $aMean PHPExcel_Calculation_MathTrig::PRODUCT($aArgs);
  1702.         if (is_numeric($aMean&& ($aMean 0)) {
  1703.             $aCount self::COUNT($aArgs;
  1704.             if (self::MIN($aArgs0{
  1705.                 return pow($aMean($aCount));
  1706.             }
  1707.         }
  1708.         return PHPExcel_Calculation_Functions::NaN();
  1709.     }    //    GEOMEAN()
  1710.  
  1711.  
  1712.     /**
  1713.      *    GROWTH
  1714.      *
  1715.      *    Returns values along a predicted emponential trend
  1716.      *
  1717.      *    @param    array of mixed        Data Series Y
  1718.      *    @param    array of mixed        Data Series X
  1719.      *    @param    array of mixed        Values of X for which we want to find Y
  1720.      *    @param    boolean                A logical value specifying whether to force the intersect to equal 0.
  1721.      *    @return    array of float
  1722.      */
  1723.     public static function GROWTH($yValues,$xValues=array(),$newValues=array(),$const=True{
  1724.         $yValues PHPExcel_Calculation_Functions::flattenArray($yValues);
  1725.         $xValues PHPExcel_Calculation_Functions::flattenArray($xValues);
  1726.         $newValues PHPExcel_Calculation_Functions::flattenArray($newValues);
  1727.         $const    (is_null($const))    True :    (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const);
  1728.  
  1729.         $bestFitExponential trendClass::calculate(trendClass::TREND_EXPONENTIAL,$yValues,$xValues,$const);
  1730.         if (count($newValues== 0{
  1731.             $newValues $bestFitExponential->getXValues();
  1732.         }
  1733.  
  1734.         $returnArray array();
  1735.         foreach($newValues as $xValue{
  1736.             $returnArray[0][$bestFitExponential->getValueOfYForX($xValue);
  1737.         }
  1738.  
  1739.         return $returnArray;
  1740.     }    //    function GROWTH()
  1741.  
  1742.  
  1743.     /**
  1744.      *    HARMEAN
  1745.      *
  1746.      *    Returns the harmonic mean of a data set. The harmonic mean is the reciprocal of the
  1747.      *        arithmetic mean of reciprocals.
  1748.      *
  1749.      *    Excel Function:
  1750.      *        HARMEAN(value1[,value2[, ...]])
  1751.      *
  1752.      *    @access    public
  1753.      *    @category Statistical Functions
  1754.      *    @param    mixed        $arg,...        Data values
  1755.      *    @return    float 
  1756.      */
  1757.     public static function HARMEAN({
  1758.         // Return value
  1759.         $returnValue PHPExcel_Calculation_Functions::NA();
  1760.  
  1761.         // Loop through arguments
  1762.         if (self::MIN($aArgs0{
  1763.             return PHPExcel_Calculation_Functions::NaN();
  1764.         }
  1765.         $aCount 0;
  1766.         foreach ($aArgs as $arg{
  1767.             // Is it a numeric value?
  1768.             if ((is_numeric($arg)) && (!is_string($arg))) {
  1769.                 if ($arg <= 0{
  1770.                     return PHPExcel_Calculation_Functions::NaN();
  1771.                 }
  1772.                 if (is_null($returnValue)) {
  1773.                     $returnValue ($arg);
  1774.                 else {
  1775.                     $returnValue += ($arg);
  1776.                 }
  1777.                 ++$aCount;
  1778.             }
  1779.         }
  1780.  
  1781.         // Return
  1782.         if ($aCount 0{
  1783.             return ($returnValue $aCount);
  1784.         else {
  1785.             return $returnValue;
  1786.         }
  1787.     }    //    function HARMEAN()
  1788.  
  1789.  
  1790.     /**
  1791.      * HYPGEOMDIST
  1792.      *
  1793.      * Returns the hypergeometric distribution. HYPGEOMDIST returns the probability of a given number of
  1794.      * sample successes, given the sample size, population successes, and population size.
  1795.      *
  1796.      * @param    float        $sampleSuccesses        Number of successes in the sample
  1797.      * @param    float        $sampleNumber            Size of the sample
  1798.      * @param    float        $populationSuccesses    Number of successes in the population
  1799.      * @param    float        $populationNumber        Population size
  1800.      * @return    float 
  1801.      *
  1802.      */
  1803.     public static function HYPGEOMDIST($sampleSuccesses$sampleNumber$populationSuccesses$populationNumber{
  1804.         $sampleSuccesses        floor(PHPExcel_Calculation_Functions::flattenSingleValue($sampleSuccesses));
  1805.         $sampleNumber            floor(PHPExcel_Calculation_Functions::flattenSingleValue($sampleNumber));
  1806.         $populationSuccesses    floor(PHPExcel_Calculation_Functions::flattenSingleValue($populationSuccesses));
  1807.         $populationNumber        floor(PHPExcel_Calculation_Functions::flattenSingleValue($populationNumber));
  1808.  
  1809.         if ((is_numeric($sampleSuccesses)) && (is_numeric($sampleNumber)) && (is_numeric($populationSuccesses)) && (is_numeric($populationNumber))) {
  1810.             if (($sampleSuccesses 0|| ($sampleSuccesses $sampleNumber|| ($sampleSuccesses $populationSuccesses)) {
  1811.                 return PHPExcel_Calculation_Functions::NaN();
  1812.             }
  1813.             if (($sampleNumber <= 0|| ($sampleNumber $populationNumber)) {
  1814.                 return PHPExcel_Calculation_Functions::NaN();
  1815.             }
  1816.             if (($populationSuccesses <= 0|| ($populationSuccesses $populationNumber)) {
  1817.                 return PHPExcel_Calculation_Functions::NaN();
  1818.             }
  1819.             return PHPExcel_Calculation_MathTrig::COMBIN($populationSuccesses,$sampleSuccesses*
  1820.                    PHPExcel_Calculation_MathTrig::COMBIN($populationNumber $populationSuccesses,$sampleNumber $sampleSuccesses/
  1821.                    PHPExcel_Calculation_MathTrig::COMBIN($populationNumber,$sampleNumber);
  1822.         }
  1823.         return PHPExcel_Calculation_Functions::VALUE();
  1824.     }    //    function HYPGEOMDIST()
  1825.  
  1826.  
  1827.     /**
  1828.      *    INTERCEPT
  1829.      *
  1830.      *    Calculates the point at which a line will intersect the y-axis by using existing x-values and y-values.
  1831.      *
  1832.      *    @param    array of mixed        Data Series Y
  1833.      *    @param    array of mixed        Data Series X
  1834.      *    @return    float 
  1835.      */
  1836.     public static function INTERCEPT($yValues,$xValues{
  1837.         if (!self::_checkTrendArrays($yValues,$xValues)) {
  1838.             return PHPExcel_Calculation_Functions::VALUE();
  1839.         }
  1840.         $yValueCount count($yValues);
  1841.         $xValueCount count($xValues);
  1842.  
  1843.         if (($yValueCount == 0|| ($yValueCount != $xValueCount)) {
  1844.             return PHPExcel_Calculation_Functions::NA();
  1845.         elseif ($yValueCount == 1{
  1846.             return PHPExcel_Calculation_Functions::DIV0();
  1847.         }
  1848.  
  1849.         $bestFitLinear trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1850.         return $bestFitLinear->getIntersect();
  1851.     }    //    function INTERCEPT()
  1852.  
  1853.  
  1854.     /**
  1855.      * KURT
  1856.      *
  1857.      * Returns the kurtosis of a data set. Kurtosis characterizes the relative peakedness
  1858.      * or flatness of a distribution compared with the normal distribution. Positive
  1859.      * kurtosis indicates a relatively peaked distribution. Negative kurtosis indicates a
  1860.      * relatively flat distribution.
  1861.      *
  1862.      * @param    array    Data Series
  1863.      * @return    float 
  1864.      */
  1865.     public static function KURT({
  1866.         $mean self::AVERAGE($aArgs);
  1867.         $stdDev self::STDEV($aArgs);
  1868.  
  1869.         if ($stdDev 0{
  1870.             $count $summer 0;
  1871.             // Loop through arguments
  1872.             foreach ($aArgs as $k => $arg{
  1873.                 if ((is_bool($arg)) &&
  1874.                     (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  1875.                 else {
  1876.                     // Is it a numeric value?
  1877.                     if ((is_numeric($arg)) && (!is_string($arg))) {
  1878.                         $summer += pow((($arg $mean$stdDev),4;
  1879.                         ++$count;
  1880.                     }
  1881.                 }
  1882.             }
  1883.  
  1884.             // Return
  1885.             if ($count 3{
  1886.                 return $summer ($count ($count+1(($count-1($count-2($count-3))) (pow($count-1,2(($count-2($count-3)));
  1887.             }
  1888.         }
  1889.         return PHPExcel_Calculation_Functions::DIV0();
  1890.     }    //    function KURT()
  1891.  
  1892.  
  1893.     /**
  1894.      *    LARGE
  1895.      *
  1896.      *    Returns the nth largest value in a data set. You can use this function to
  1897.      *        select a value based on its relative standing.
  1898.      *
  1899.      *    Excel Function:
  1900.      *        LARGE(value1[,value2[, ...]],entry)
  1901.      *
  1902.      *    @access    public
  1903.      *    @category Statistical Functions
  1904.      *    @param    mixed        $arg,...        Data values
  1905.      *    @param    int            $entry            Position (ordered from the largest) in the array or range of data to return
  1906.      *    @return    float 
  1907.      *
  1908.      */
  1909.     public static function LARGE({
  1910.  
  1911.         // Calculate
  1912.         $entry floor(array_pop($aArgs));
  1913.  
  1914.         if ((is_numeric($entry)) && (!is_string($entry))) {
  1915.             $mArgs array();
  1916.             foreach ($aArgs as $arg{
  1917.                 // Is it a numeric value?
  1918.                 if ((is_numeric($arg)) && (!is_string($arg))) {
  1919.                     $mArgs[$arg;
  1920.                 }
  1921.             }
  1922.             $count self::COUNT($mArgs);
  1923.             $entry floor(--$entry);
  1924.             if (($entry 0|| ($entry >= $count|| ($count == 0)) {
  1925.                 return PHPExcel_Calculation_Functions::NaN();
  1926.             }
  1927.             rsort($mArgs);
  1928.             return $mArgs[$entry];
  1929.         }
  1930.         return PHPExcel_Calculation_Functions::VALUE();
  1931.     }    //    function LARGE()
  1932.  
  1933.  
  1934.     /**
  1935.      *    LINEST
  1936.      *
  1937.      *    Calculates the statistics for a line by using the "least squares" method to calculate a straight line that best fits your data,
  1938.      *        and then returns an array that describes the line.
  1939.      *
  1940.      *    @param    array of mixed        Data Series Y
  1941.      *    @param    array of mixed        Data Series X
  1942.      *    @param    boolean                A logical value specifying whether to force the intersect to equal 0.
  1943.      *    @param    boolean                A logical value specifying whether to return additional regression statistics.
  1944.      *    @return    array 
  1945.      */
  1946.     public static function LINEST($yValues,$xValues=null,$const=True,$stats=False{
  1947.         $const    (is_null($const))    True :    (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const);
  1948.         $stats    (is_null($stats))    False :    (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($stats);
  1949.         if (is_null($xValues)) $xValues range(1,count(PHPExcel_Calculation_Functions::flattenArray($yValues)));
  1950.  
  1951.         if (!self::_checkTrendArrays($yValues,$xValues)) {
  1952.             return PHPExcel_Calculation_Functions::VALUE();
  1953.         }
  1954.         $yValueCount count($yValues);
  1955.         $xValueCount count($xValues);
  1956.  
  1957.  
  1958.         if (($yValueCount == 0|| ($yValueCount != $xValueCount)) {
  1959.             return PHPExcel_Calculation_Functions::NA();
  1960.         elseif ($yValueCount == 1{
  1961.             return 0;
  1962.         }
  1963.  
  1964.         $bestFitLinear trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues,$const);
  1965.         if ($stats{
  1966.             return arrayarray$bestFitLinear->getSlope(),
  1967.                                   $bestFitLinear->getSlopeSE(),
  1968.                                   $bestFitLinear->getGoodnessOfFit(),
  1969.                                   $bestFitLinear->getF(),
  1970.                                   $bestFitLinear->getSSRegression(),
  1971.                                ),
  1972.                           array$bestFitLinear->getIntersect(),
  1973.                                  $bestFitLinear->getIntersectSE(),
  1974.                                  $bestFitLinear->getStdevOfResiduals(),
  1975.                                  $bestFitLinear->getDFResiduals(),
  1976.                                  $bestFitLinear->getSSResiduals()
  1977.                                )
  1978.                         );
  1979.         else {
  1980.             return array$bestFitLinear->getSlope(),
  1981.                           $bestFitLinear->getIntersect()
  1982.                         );
  1983.         }
  1984.     }    //    function LINEST()
  1985.  
  1986.  
  1987.     /**
  1988.      *    LOGEST
  1989.      *
  1990.      *    Calculates an exponential curve that best fits the X and Y data series,
  1991.      *        and then returns an array that describes the line.
  1992.      *
  1993.      *    @param    array of mixed        Data Series Y
  1994.      *    @param    array of mixed        Data Series X
  1995.      *    @param    boolean                A logical value specifying whether to force the intersect to equal 0.
  1996.      *    @param    boolean                A logical value specifying whether to return additional regression statistics.
  1997.      *    @return    array 
  1998.      */
  1999.     public static function LOGEST($yValues,$xValues=null,$const=True,$stats=False{
  2000.         $const    (is_null($const))    True :    (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const);
  2001.         $stats    (is_null($stats))    False :    (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($stats);
  2002.         if (is_null($xValues)) $xValues range(1,count(PHPExcel_Calculation_Functions::flattenArray($yValues)));
  2003.  
  2004.         if (!self::_checkTrendArrays($yValues,$xValues)) {
  2005.             return PHPExcel_Calculation_Functions::VALUE();
  2006.         }
  2007.         $yValueCount count($yValues);
  2008.         $xValueCount count($xValues);
  2009.  
  2010.         foreach($yValues as $value{
  2011.             if ($value <= 0.0{
  2012.                 return PHPExcel_Calculation_Functions::NaN();
  2013.             }
  2014.         }
  2015.  
  2016.  
  2017.         if (($yValueCount == 0|| ($yValueCount != $xValueCount)) {
  2018.             return PHPExcel_Calculation_Functions::NA();
  2019.         elseif ($yValueCount == 1{
  2020.             return 1;
  2021.         }
  2022.  
  2023.         $bestFitExponential trendClass::calculate(trendClass::TREND_EXPONENTIAL,$yValues,$xValues,$const);
  2024.         if ($stats{
  2025.             return arrayarray$bestFitExponential->getSlope(),
  2026.                                   $bestFitExponential->getSlopeSE(),
  2027.                                   $bestFitExponential->getGoodnessOfFit(),
  2028.                                   $bestFitExponential->getF(),
  2029.                                   $bestFitExponential->getSSRegression(),
  2030.                                ),
  2031.                           array$bestFitExponential->getIntersect(),
  2032.                                  $bestFitExponential->getIntersectSE(),
  2033.                                  $bestFitExponential->getStdevOfResiduals(),
  2034.                                  $bestFitExponential->getDFResiduals(),
  2035.                                  $bestFitExponential->getSSResiduals()
  2036.                                )
  2037.                         );
  2038.         else {
  2039.             return array$bestFitExponential->getSlope(),
  2040.                           $bestFitExponential->getIntersect()
  2041.                         );
  2042.         }
  2043.     }    //    function LOGEST()
  2044.  
  2045.  
  2046.     /**
  2047.      * LOGINV
  2048.      *
  2049.      * Returns the inverse of the normal cumulative distribution
  2050.      *
  2051.      * @param    float        $value 
  2052.      * @return    float 
  2053.      *
  2054.      * @todo    Try implementing P J Acklam's refinement algorithm for greater
  2055.      *             accuracy if I can get my head round the mathematics
  2056.      *             (as described at) http://home.online.no/~pjacklam/notes/invnorm/
  2057.      */
  2058.     public static function LOGINV($probability$mean$stdDev{
  2059.         $probability    PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  2060.         $mean            PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  2061.         $stdDev            PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  2062.  
  2063.         if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
  2064.             if (($probability 0|| ($probability 1|| ($stdDev <= 0)) {
  2065.                 return PHPExcel_Calculation_Functions::NaN();
  2066.             }
  2067.             return exp($mean $stdDev self::NORMSINV($probability));
  2068.         }
  2069.         return PHPExcel_Calculation_Functions::VALUE();
  2070.     }    //    function LOGINV()
  2071.  
  2072.  
  2073.     /**
  2074.      * LOGNORMDIST
  2075.      *
  2076.      * Returns the cumulative lognormal distribution of x, where ln(x) is normally distributed
  2077.      * with parameters mean and standard_dev.
  2078.      *
  2079.      * @param    float        $value 
  2080.      * @return    float 
  2081.      */
  2082.     public static function LOGNORMDIST($value$mean$stdDev{
  2083.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2084.         $mean    PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  2085.         $stdDev    PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  2086.  
  2087.         if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
  2088.             if (($value <= 0|| ($stdDev <= 0)) {
  2089.                 return PHPExcel_Calculation_Functions::NaN();
  2090.             }
  2091.             return self::NORMSDIST((log($value$mean$stdDev);
  2092.         }
  2093.         return PHPExcel_Calculation_Functions::VALUE();
  2094.     }    //    function LOGNORMDIST()
  2095.  
  2096.  
  2097.     /**
  2098.      *    MAX
  2099.      *
  2100.      *    MAX returns the value of the element of the values passed that has the highest value,
  2101.      *        with negative numbers considered smaller than positive numbers.
  2102.      *
  2103.      *    Excel Function:
  2104.      *        MAX(value1[,value2[, ...]])
  2105.      *
  2106.      *    @access    public
  2107.      *    @category Statistical Functions
  2108.      *    @param    mixed        $arg,...        Data values
  2109.      *    @return    float 
  2110.      */
  2111.     public static function MAX({
  2112.         // Return value
  2113.         $returnValue null;
  2114.  
  2115.         // Loop through arguments
  2116.         foreach ($aArgs as $arg{
  2117.             // Is it a numeric value?
  2118.             if ((is_numeric($arg)) && (!is_string($arg))) {
  2119.                 if ((is_null($returnValue)) || ($arg $returnValue)) {
  2120.                     $returnValue $arg;
  2121.                 }
  2122.             }
  2123.         }
  2124.  
  2125.         // Return
  2126.         if(is_null($returnValue)) {
  2127.             return 0;
  2128.         }
  2129.         return $returnValue;
  2130.     }    //    function MAX()
  2131.  
  2132.  
  2133.     /**
  2134.      *    MAXA
  2135.      *
  2136.      *    Returns the greatest value in a list of arguments, including numbers, text, and logical values
  2137.      *
  2138.      *    Excel Function:
  2139.      *        MAXA(value1[,value2[, ...]])
  2140.      *
  2141.      *    @access    public
  2142.      *    @category Statistical Functions
  2143.      *    @param    mixed        $arg,...        Data values
  2144.      *    @return    float 
  2145.      */
  2146.     public static function MAXA({
  2147.         // Return value
  2148.         $returnValue null;
  2149.  
  2150.         // Loop through arguments
  2151.         foreach ($aArgs as $arg{
  2152.             // Is it a numeric value?
  2153.             if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg&& ($arg != '')))) {
  2154.                 if (is_bool($arg)) {
  2155.                     $arg = (integer) $arg;
  2156.                 elseif (is_string($arg)) {
  2157.                     $arg 0;
  2158.                 }
  2159.                 if ((is_null($returnValue)) || ($arg $returnValue)) {
  2160.                     $returnValue $arg;
  2161.                 }
  2162.             }
  2163.         }
  2164.  
  2165.         // Return
  2166.         if(is_null($returnValue)) {
  2167.             return 0;
  2168.         }
  2169.         return $returnValue;
  2170.     }    //    function MAXA()
  2171.  
  2172.  
  2173.     /**
  2174.      *    MAXIF
  2175.      *
  2176.      *    Counts the maximum value within a range of cells that contain numbers within the list of arguments
  2177.      *
  2178.      *    Excel Function:
  2179.      *        MAXIF(value1[,value2[, ...]],condition)
  2180.      *
  2181.      *    @access    public
  2182.      *    @category Mathematical and Trigonometric Functions
  2183.      *    @param    mixed        $arg,...        Data values
  2184.      *    @param    string        $condition        The criteria that defines which cells will be checked.
  2185.      *    @return    float 
  2186.      */
  2187.     public static function MAXIF($aArgs,$condition,$sumArgs array()) {
  2188.         // Return value
  2189.         $returnValue null;
  2190.  
  2191.         $aArgs PHPExcel_Calculation_Functions::flattenArray($aArgs);
  2192.         $sumArgs PHPExcel_Calculation_Functions::flattenArray($sumArgs);
  2193.         if (count($sumArgs== 0{
  2194.             $sumArgs $aArgs;
  2195.         }
  2196.         $condition PHPExcel_Calculation_Functions::_ifCondition($condition);
  2197.         // Loop through arguments
  2198.         foreach ($aArgs as $key => $arg{
  2199.             if (!is_numeric($arg)) $arg PHPExcel_Calculation::_wrapResult(strtoupper($arg))}
  2200.             $testCondition '='.$arg.$condition;
  2201.             if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  2202.                 if ((is_null($returnValue)) || ($arg $returnValue)) {
  2203.                     $returnValue $arg;
  2204.                 }
  2205.             }
  2206.         }
  2207.  
  2208.         // Return
  2209.         return $returnValue;
  2210.     }    //    function MAXIF()
  2211.  
  2212.  
  2213.     /**
  2214.      *    MEDIAN
  2215.      *
  2216.      *    Returns the median of the given numbers. The median is the number in the middle of a set of numbers.
  2217.      *
  2218.      *    Excel Function:
  2219.      *        MEDIAN(value1[,value2[, ...]])
  2220.      *
  2221.      *    @access    public
  2222.      *    @category Statistical Functions
  2223.      *    @param    mixed        $arg,...        Data values
  2224.      *    @return    float 
  2225.      */
  2226.     public static function MEDIAN({
  2227.         // Return value
  2228.         $returnValue PHPExcel_Calculation_Functions::NaN();
  2229.  
  2230.         $mArgs array();
  2231.         // Loop through arguments
  2232.         foreach ($aArgs as $arg{
  2233.             // Is it a numeric value?
  2234.             if ((is_numeric($arg)) && (!is_string($arg))) {
  2235.                 $mArgs[$arg;
  2236.             }
  2237.         }
  2238.  
  2239.         $mValueCount count($mArgs);
  2240.         if ($mValueCount 0{
  2241.             sort($mArgs,SORT_NUMERIC);
  2242.             $mValueCount $mValueCount 2;
  2243.             if ($mValueCount == floor($mValueCount)) {
  2244.                 $returnValue ($mArgs[$mValueCount--$mArgs[$mValueCount]2;
  2245.             else {
  2246.                 $mValueCount == floor($mValueCount);
  2247.                 $returnValue $mArgs[$mValueCount];
  2248.             }
  2249.         }
  2250.  
  2251.         // Return
  2252.         return $returnValue;
  2253.     }    //    function MEDIAN()
  2254.  
  2255.  
  2256.     /**
  2257.      *    MIN
  2258.      *
  2259.      *    MIN returns the value of the element of the values passed that has the smallest value,
  2260.      *        with negative numbers considered smaller than positive numbers.
  2261.      *
  2262.      *    Excel Function:
  2263.      *        MIN(value1[,value2[, ...]])
  2264.      *
  2265.      *    @access    public
  2266.      *    @category Statistical Functions
  2267.      *    @param    mixed        $arg,...        Data values
  2268.      *    @return    float 
  2269.      */
  2270.     public static function MIN({
  2271.         // Return value
  2272.         $returnValue null;
  2273.  
  2274.         // Loop through arguments
  2275.         foreach ($aArgs as $arg{
  2276.             // Is it a numeric value?
  2277.             if ((is_numeric($arg)) && (!is_string($arg))) {
  2278.                 if ((is_null($returnValue)) || ($arg $returnValue)) {
  2279.                     $returnValue $arg;
  2280.                 }
  2281.             }
  2282.         }
  2283.  
  2284.         // Return
  2285.         if(is_null($returnValue)) {
  2286.             return 0;
  2287.         }
  2288.         return $returnValue;
  2289.     }    //    function MIN()
  2290.  
  2291.  
  2292.     /**
  2293.      *    MINA
  2294.      *
  2295.      *    Returns the smallest value in a list of arguments, including numbers, text, and logical values
  2296.      *
  2297.      *    Excel Function:
  2298.      *        MINA(value1[,value2[, ...]])
  2299.      *
  2300.      *    @access    public
  2301.      *    @category Statistical Functions
  2302.      *    @param    mixed        $arg,...        Data values
  2303.      *    @return    float 
  2304.      */
  2305.     public static function MINA({
  2306.         // Return value
  2307.         $returnValue null;
  2308.  
  2309.         // Loop through arguments
  2310.         foreach ($aArgs as $arg{
  2311.             // Is it a numeric value?
  2312.             if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg&& ($arg != '')))) {
  2313.                 if (is_bool($arg)) {
  2314.                     $arg = (integer) $arg;
  2315.                 elseif (is_string($arg)) {
  2316.                     $arg 0;
  2317.                 }
  2318.                 if ((is_null($returnValue)) || ($arg $returnValue)) {
  2319.                     $returnValue $arg;
  2320.                 }
  2321.             }
  2322.         }
  2323.  
  2324.         // Return
  2325.         if(is_null($returnValue)) {
  2326.             return 0;
  2327.         }
  2328.         return $returnValue;
  2329.     }    //    function MINA()
  2330.  
  2331.  
  2332.     /**
  2333.      *    MINIF
  2334.      *
  2335.      *    Returns the minimum value within a range of cells that contain numbers within the list of arguments
  2336.      *
  2337.      *    Excel Function:
  2338.      *        MINIF(value1[,value2[, ...]],condition)
  2339.      *
  2340.      *    @access    public
  2341.      *    @category Mathematical and Trigonometric Functions
  2342.      *    @param    mixed        $arg,...        Data values
  2343.      *    @param    string        $condition        The criteria that defines which cells will be checked.
  2344.      *    @return    float 
  2345.      */
  2346.     public static function MINIF($aArgs,$condition,$sumArgs array()) {
  2347.         // Return value
  2348.         $returnValue null;
  2349.  
  2350.         $aArgs PHPExcel_Calculation_Functions::flattenArray($aArgs);
  2351.         $sumArgs PHPExcel_Calculation_Functions::flattenArray($sumArgs);
  2352.         if (count($sumArgs== 0{
  2353.             $sumArgs $aArgs;
  2354.         }
  2355.         $condition PHPExcel_Calculation_Functions::_ifCondition($condition);
  2356.         // Loop through arguments
  2357.         foreach ($aArgs as $key => $arg{
  2358.             if (!is_numeric($arg)) $arg PHPExcel_Calculation::_wrapResult(strtoupper($arg))}
  2359.             $testCondition '='.$arg.$condition;
  2360.             if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  2361.                 if ((is_null($returnValue)) || ($arg $returnValue)) {
  2362.                     $returnValue $arg;
  2363.                 }
  2364.             }
  2365.         }
  2366.  
  2367.         // Return
  2368.         return $returnValue;
  2369.     }    //    function MINIF()
  2370.  
  2371.  
  2372.     //
  2373.     //    Special variant of array_count_values that isn't limited to strings and integers,
  2374.     //        but can work with floating point numbers as values
  2375.     //
  2376.     private static function _modeCalc($data{
  2377.         $frequencyArray array();
  2378.         foreach($data as $datum{
  2379.             $found False;
  2380.             foreach($frequencyArray as $key => $value{
  2381.                 if ((string) $value['value'== (string) $datum{
  2382.                     ++$frequencyArray[$key]['frequency'];
  2383.                     $found True;
  2384.                     break;
  2385.                 }
  2386.             }
  2387.             if (!$found{
  2388.                 $frequencyArray[array('value'        => $datum,
  2389.                                           'frequency'    =>    );
  2390.             }
  2391.         }
  2392.  
  2393.         foreach($frequencyArray as $key => $value{
  2394.             $frequencyList[$key$value['frequency'];
  2395.             $valueList[$key$value['value'];
  2396.         }
  2397.         array_multisort($frequencyListSORT_DESC$valueListSORT_ASCSORT_NUMERIC$frequencyArray);
  2398.  
  2399.         if ($frequencyArray[0]['frequency'== 1{
  2400.             return PHPExcel_Calculation_Functions::NA();
  2401.         }
  2402.         return $frequencyArray[0]['value'];
  2403.     }    //    function _modeCalc()
  2404.  
  2405.  
  2406.     /**
  2407.      *    MODE
  2408.      *
  2409.      *    Returns the most frequently occurring, or repetitive, value in an array or range of data
  2410.      *
  2411.      *    Excel Function:
  2412.      *        MODE(value1[,value2[, ...]])
  2413.      *
  2414.      *    @access    public
  2415.      *    @category Statistical Functions
  2416.      *    @param    mixed        $arg,...        Data values
  2417.      *    @return    float 
  2418.      */
  2419.     public static function MODE({
  2420.         // Return value
  2421.         $returnValue PHPExcel_Calculation_Functions::NA();
  2422.  
  2423.         // Loop through arguments
  2424.  
  2425.         $mArgs array();
  2426.         foreach ($aArgs as $arg{
  2427.             // Is it a numeric value?
  2428.             if ((is_numeric($arg)) && (!is_string($arg))) {
  2429.                 $mArgs[$arg;
  2430.             }
  2431.         }
  2432.  
  2433.         if (count($mArgs0{
  2434.             return self::_modeCalc($mArgs);
  2435.         }
  2436.  
  2437.         // Return
  2438.         return $returnValue;
  2439.     }    //    function MODE()
  2440.  
  2441.  
  2442.     /**
  2443.      *    NEGBINOMDIST
  2444.      *
  2445.      *    Returns the negative binomial distribution. NEGBINOMDIST returns the probability that
  2446.      *    there will be number_f failures before the number_s-th success, when the constant
  2447.      *    probability of a success is probability_s. This function is similar to the binomial
  2448.      *    distribution, except that the number of successes is fixed, and the number of trials is
  2449.      *    variable. Like the binomial, trials are assumed to be independent.
  2450.      *
  2451.      *    @param    float        $failures        Number of Failures
  2452.      *    @param    float        $successes        Threshold number of Successes
  2453.      *    @param    float        $probability    Probability of success on each trial
  2454.      *    @return    float 
  2455.      *
  2456.      */
  2457.     public static function NEGBINOMDIST($failures$successes$probability{
  2458.         $failures        floor(PHPExcel_Calculation_Functions::flattenSingleValue($failures));
  2459.         $successes        floor(PHPExcel_Calculation_Functions::flattenSingleValue($successes));
  2460.         $probability    PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  2461.  
  2462.         if ((is_numeric($failures)) && (is_numeric($successes)) && (is_numeric($probability))) {
  2463.             if (($failures 0|| ($successes 1)) {
  2464.                 return PHPExcel_Calculation_Functions::NaN();
  2465.             }
  2466.             if (($probability 0|| ($probability 1)) {
  2467.                 return PHPExcel_Calculation_Functions::NaN();
  2468.             }
  2469.             if (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC{
  2470.                 if (($failures $successes 1<= 0{
  2471.                     return PHPExcel_Calculation_Functions::NaN();
  2472.                 }
  2473.             }
  2474.             return (PHPExcel_Calculation_MathTrig::COMBIN($failures $successes 1,$successes 1)) (pow($probability,$successes)) (pow($probability,$failures)) ;
  2475.         }
  2476.         return PHPExcel_Calculation_Functions::VALUE();
  2477.     }    //    function NEGBINOMDIST()
  2478.  
  2479.  
  2480.     /**
  2481.      * NORMDIST
  2482.      *
  2483.      * Returns the normal distribution for the specified mean and standard deviation. This
  2484.      * function has a very wide range of applications in statistics, including hypothesis
  2485.      * testing.
  2486.      *
  2487.      * @param    float        $value 
  2488.      * @param    float        $mean        Mean Value
  2489.      * @param    float        $stdDev        Standard Deviation
  2490.      * @param    boolean        $cumulative 
  2491.      * @return    float 
  2492.      *
  2493.      */
  2494.     public static function NORMDIST($value$mean$stdDev$cumulative{
  2495.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2496.         $mean    PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  2497.         $stdDev    PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  2498.  
  2499.         if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
  2500.             if ($stdDev 0{
  2501.                 return PHPExcel_Calculation_Functions::NaN();
  2502.             }
  2503.             if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  2504.                 if ($cumulative{
  2505.                     return 0.5 (PHPExcel_Calculation_Engineering::_erfVal(($value $mean($stdDev sqrt(2))));
  2506.                 else {
  2507.                     return ((SQRT2PI $stdDev)) exp((pow($value $mean,2(($stdDev $stdDev))));
  2508.                 }
  2509.             }
  2510.         }
  2511.         return PHPExcel_Calculation_Functions::VALUE();
  2512.     }    //    function NORMDIST()
  2513.  
  2514.  
  2515.     /**
  2516.      * NORMINV
  2517.      *
  2518.      * Returns the inverse of the normal cumulative distribution for the specified mean and standard deviation.
  2519.      *
  2520.      * @param    float        $value 
  2521.      * @param    float        $mean        Mean Value
  2522.      * @param    float        $stdDev        Standard Deviation
  2523.      * @return    float 
  2524.      *
  2525.      */
  2526.     public static function NORMINV($probability,$mean,$stdDev{
  2527.         $probability    PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  2528.         $mean            PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  2529.         $stdDev            PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  2530.  
  2531.         if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
  2532.             if (($probability 0|| ($probability 1)) {
  2533.                 return PHPExcel_Calculation_Functions::NaN();
  2534.             }
  2535.             if ($stdDev 0{
  2536.                 return PHPExcel_Calculation_Functions::NaN();
  2537.             }
  2538.             return (self::_inverse_ncdf($probability$stdDev$mean;
  2539.         }
  2540.         return PHPExcel_Calculation_Functions::VALUE();
  2541.     }    //    function NORMINV()
  2542.  
  2543.  
  2544.     /**
  2545.      * NORMSDIST
  2546.      *
  2547.      * Returns the standard normal cumulative distribution function. The distribution has
  2548.      * a mean of 0 (zero) and a standard deviation of one. Use this function in place of a
  2549.      * table of standard normal curve areas.
  2550.      *
  2551.      * @param    float        $value 
  2552.      * @return    float 
  2553.      */
  2554.     public static function NORMSDIST($value{
  2555.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2556.  
  2557.         return self::NORMDIST($value01True);
  2558.     }    //    function NORMSDIST()
  2559.  
  2560.  
  2561.     /**
  2562.      * NORMSINV
  2563.      *
  2564.      * Returns the inverse of the standard normal cumulative distribution
  2565.      *
  2566.      * @param    float        $value 
  2567.      * @return    float 
  2568.      */
  2569.     public static function NORMSINV($value{
  2570.         return self::NORMINV($value01);
  2571.     }    //    function NORMSINV()
  2572.  
  2573.  
  2574.     /**
  2575.      *    PERCENTILE
  2576.      *
  2577.      *    Returns the nth percentile of values in a range..
  2578.      *
  2579.      *    Excel Function:
  2580.      *        PERCENTILE(value1[,value2[, ...]],entry)
  2581.      *
  2582.      *    @access    public
  2583.      *    @category Statistical Functions
  2584.      *    @param    mixed        $arg,...        Data values
  2585.      *    @param    float        $entry            Percentile value in the range 0..1, inclusive.
  2586.      *    @return    float 
  2587.      */
  2588.     public static function PERCENTILE({
  2589.  
  2590.         // Calculate
  2591.         $entry array_pop($aArgs);
  2592.  
  2593.         if ((is_numeric($entry)) && (!is_string($entry))) {
  2594.             if (($entry 0|| ($entry 1)) {
  2595.                 return PHPExcel_Calculation_Functions::NaN();
  2596.             }
  2597.             $mArgs array();
  2598.             foreach ($aArgs as $arg{
  2599.                 // Is it a numeric value?
  2600.                 if ((is_numeric($arg)) && (!is_string($arg))) {
  2601.                     $mArgs[$arg;
  2602.                 }
  2603.             }
  2604.             $mValueCount count($mArgs);
  2605.             if ($mValueCount 0{
  2606.                 sort($mArgs);
  2607.                 $count self::COUNT($mArgs);
  2608.                 $index $entry ($count-1);
  2609.                 $iBase floor($index);
  2610.                 if ($index == $iBase{
  2611.                     return $mArgs[$index];
  2612.                 else {
  2613.                     $iNext $iBase 1;
  2614.                     $iProportion $index $iBase;
  2615.                     return $mArgs[$iBase(($mArgs[$iNext$mArgs[$iBase]$iProportion;
  2616.                 }
  2617.             }
  2618.         }
  2619.         return PHPExcel_Calculation_Functions::VALUE();
  2620.     }    //    function PERCENTILE()
  2621.  
  2622.  
  2623.     /**
  2624.      *    PERCENTRANK
  2625.      *
  2626.      *    Returns the rank of a value in a data set as a percentage of the data set.
  2627.      *
  2628.      *    @param    array of number        An array of, or a reference to, a list of numbers.
  2629.      *    @param    number                The number whose rank you want to find.
  2630.      *    @param    number                The number of significant digits for the returned percentage value.
  2631.      *    @return    float 
  2632.      */
  2633.     public static function PERCENTRANK($valueSet,$value,$significance=3{
  2634.         $valueSet    PHPExcel_Calculation_Functions::flattenArray($valueSet);
  2635.         $value        PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2636.         $significance    (is_null($significance))    :    (integer) PHPExcel_Calculation_Functions::flattenSingleValue($significance);
  2637.  
  2638.         foreach($valueSet as $key => $valueEntry{
  2639.             if (!is_numeric($valueEntry)) {
  2640.                 unset($valueSet[$key]);
  2641.             }
  2642.         }
  2643.         sort($valueSet,SORT_NUMERIC);
  2644.         $valueCount count($valueSet);
  2645.         if ($valueCount == 0{
  2646.             return PHPExcel_Calculation_Functions::NaN();
  2647.         }
  2648.  
  2649.         $valueAdjustor $valueCount 1;
  2650.         if (($value $valueSet[0]|| ($value $valueSet[$valueAdjustor])) {
  2651.             return PHPExcel_Calculation_Functions::NA();
  2652.         }
  2653.  
  2654.         $pos array_search($value,$valueSet);
  2655.         if ($pos === False{
  2656.             $pos 0;
  2657.             $testValue $valueSet[0];
  2658.             while ($testValue $value{
  2659.                 $testValue $valueSet[++$pos];
  2660.             }
  2661.             --$pos;
  2662.             $pos += (($value $valueSet[$pos]($testValue $valueSet[$pos]));
  2663.         }
  2664.  
  2665.         return round($pos $valueAdjustor,$significance);
  2666.     }    //    function PERCENTRANK()
  2667.  
  2668.  
  2669.     /**
  2670.      *    PERMUT
  2671.      *
  2672.      *    Returns the number of permutations for a given number of objects that can be
  2673.      *    selected from number objects. A permutation is any set or subset of objects or
  2674.      *    events where internal order is significant. Permutations are different from
  2675.      *    combinations, for which the internal order is not significant. Use this function
  2676.      *    for lottery-style probability calculations.
  2677.      *
  2678.      *    @param    int        $numObjs    Number of different objects
  2679.      *    @param    int        $numInSet    Number of objects in each permutation
  2680.      *    @return    int        Number of permutations
  2681.      */
  2682.     public static function PERMUT($numObjs,$numInSet{
  2683.         $numObjs    PHPExcel_Calculation_Functions::flattenSingleValue($numObjs);
  2684.         $numInSet    PHPExcel_Calculation_Functions::flattenSingleValue($numInSet);
  2685.  
  2686.         if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
  2687.             $numInSet floor($numInSet);
  2688.             if ($numObjs $numInSet{
  2689.                 return PHPExcel_Calculation_Functions::NaN();
  2690.             }
  2691.             return round(PHPExcel_Calculation_MathTrig::FACT($numObjsPHPExcel_Calculation_MathTrig::FACT($numObjs $numInSet));
  2692.         }
  2693.         return PHPExcel_Calculation_Functions::VALUE();
  2694.     }    //    function PERMUT()
  2695.  
  2696.  
  2697.     /**
  2698.      * POISSON
  2699.      *
  2700.      * Returns the Poisson distribution. A common application of the Poisson distribution
  2701.      * is predicting the number of events over a specific time, such as the number of
  2702.      * cars arriving at a toll plaza in 1 minute.
  2703.      *
  2704.      * @param    float        $value 
  2705.      * @param    float        $mean        Mean Value
  2706.      * @param    boolean        $cumulative 
  2707.      * @return    float 
  2708.      *
  2709.      */
  2710.     public static function POISSON($value$mean$cumulative{
  2711.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2712.         $mean    PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  2713.  
  2714.         if ((is_numeric($value)) && (is_numeric($mean))) {
  2715.             if (($value <= 0|| ($mean <= 0)) {
  2716.                 return PHPExcel_Calculation_Functions::NaN();
  2717.             }
  2718.             if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  2719.                 if ($cumulative{
  2720.                     $summer 0;
  2721.                     for ($i 0$i <= floor($value)++$i{
  2722.                         $summer += pow($mean,$iPHPExcel_Calculation_MathTrig::FACT($i);
  2723.                     }
  2724.                     return exp(0-$mean$summer;
  2725.                 else {
  2726.                     return (exp(0-$meanpow($mean,$value)) PHPExcel_Calculation_MathTrig::FACT($value);
  2727.                 }
  2728.             }
  2729.         }
  2730.         return PHPExcel_Calculation_Functions::VALUE();
  2731.     }    //    function POISSON()
  2732.  
  2733.  
  2734.     /**
  2735.      *    QUARTILE
  2736.      *
  2737.      *    Returns the quartile of a data set.
  2738.      *
  2739.      *    Excel Function:
  2740.      *        QUARTILE(value1[,value2[, ...]],entry)
  2741.      *
  2742.      *    @access    public
  2743.      *    @category Statistical Functions
  2744.      *    @param    mixed        $arg,...        Data values
  2745.      *    @param    int            $entry            Quartile value in the range 1..3, inclusive.
  2746.      *    @return    float 
  2747.      */
  2748.     public static function QUARTILE({
  2749.  
  2750.         // Calculate
  2751.         $entry floor(array_pop($aArgs));
  2752.  
  2753.         if ((is_numeric($entry)) && (!is_string($entry))) {
  2754.             $entry /= 4;
  2755.             if (($entry 0|| ($entry 1)) {
  2756.                 return PHPExcel_Calculation_Functions::NaN();
  2757.             }
  2758.             return self::PERCENTILE($aArgs,$entry);
  2759.         }
  2760.         return PHPExcel_Calculation_Functions::VALUE();
  2761.     }    //    function QUARTILE()
  2762.  
  2763.  
  2764.     /**
  2765.      *    RANK
  2766.      *
  2767.      *    Returns the rank of a number in a list of numbers.
  2768.      *
  2769.      *    @param    number                The number whose rank you want to find.
  2770.      *    @param    array of number        An array of, or a reference to, a list of numbers.
  2771.      *    @param    mixed                Order to sort the values in the value set
  2772.      *    @return    float 
  2773.      */
  2774.     public static function RANK($value,$valueSet,$order=0{
  2775.         $value PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2776.         $valueSet PHPExcel_Calculation_Functions::flattenArray($valueSet);
  2777.         $order    (is_null($order))    :    (integer) PHPExcel_Calculation_Functions::flattenSingleValue($order);
  2778.  
  2779.         foreach($valueSet as $key => $valueEntry{
  2780.             if (!is_numeric($valueEntry)) {
  2781.                 unset($valueSet[$key]);
  2782.             }
  2783.         }
  2784.  
  2785.         if ($order == 0{
  2786.             rsort($valueSet,SORT_NUMERIC);
  2787.         else {
  2788.             sort($valueSet,SORT_NUMERIC);
  2789.         }
  2790.         $pos array_search($value,$valueSet);
  2791.         if ($pos === False{
  2792.             return PHPExcel_Calculation_Functions::NA();
  2793.         }
  2794.  
  2795.         return ++$pos;
  2796.     }    //    function RANK()
  2797.  
  2798.  
  2799.     /**
  2800.      *    RSQ
  2801.      *
  2802.      *    Returns the square of the Pearson product moment correlation coefficient through data points in known_y's and known_x's.
  2803.      *
  2804.      *    @param    array of mixed        Data Series Y
  2805.      *    @param    array of mixed        Data Series X
  2806.      *    @return    float 
  2807.      */
  2808.     public static function RSQ($yValues,$xValues{
  2809.         if (!self::_checkTrendArrays($yValues,$xValues)) {
  2810.             return PHPExcel_Calculation_Functions::VALUE();
  2811.         }
  2812.         $yValueCount count($yValues);
  2813.         $xValueCount count($xValues);
  2814.  
  2815.         if (($yValueCount == 0|| ($yValueCount != $xValueCount)) {
  2816.             return PHPExcel_Calculation_Functions::NA();
  2817.         elseif ($yValueCount == 1{
  2818.             return PHPExcel_Calculation_Functions::DIV0();
  2819.         }
  2820.  
  2821.         $bestFitLinear trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  2822.         return $bestFitLinear->getGoodnessOfFit();
  2823.     }    //    function RSQ()
  2824.  
  2825.  
  2826.     /**
  2827.      * SKEW
  2828.      *
  2829.      * Returns the skewness of a distribution. Skewness characterizes the degree of asymmetry
  2830.      * of a distribution around its mean. Positive skewness indicates a distribution with an
  2831.      * asymmetric tail extending toward more positive values. Negative skewness indicates a
  2832.      * distribution with an asymmetric tail extending toward more negative values.
  2833.      *
  2834.      * @param    array    Data Series
  2835.      * @return    float 
  2836.      */
  2837.     public static function SKEW({
  2838.         $mean self::AVERAGE($aArgs);
  2839.         $stdDev self::STDEV($aArgs);
  2840.  
  2841.         $count $summer 0;
  2842.         // Loop through arguments
  2843.         foreach ($aArgs as $k => $arg{
  2844.             if ((is_bool($arg)) &&
  2845.                 (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  2846.             else {
  2847.                 // Is it a numeric value?
  2848.                 if ((is_numeric($arg)) && (!is_string($arg))) {
  2849.                     $summer += pow((($arg $mean$stdDev),3;
  2850.                     ++$count;
  2851.                 }
  2852.             }
  2853.         }
  2854.  
  2855.         // Return
  2856.         if ($count 2{
  2857.             return $summer ($count (($count-1($count-2)));
  2858.         }
  2859.         return PHPExcel_Calculation_Functions::DIV0();
  2860.     }    //    function SKEW()
  2861.  
  2862.  
  2863.     /**
  2864.      *    SLOPE
  2865.      *
  2866.      *    Returns the slope of the linear regression line through data points in known_y's and known_x's.
  2867.      *
  2868.      *    @param    array of mixed        Data Series Y
  2869.      *    @param    array of mixed        Data Series X
  2870.      *    @return    float 
  2871.      */
  2872.     public static function SLOPE($yValues,$xValues{
  2873.         if (!self::_checkTrendArrays($yValues,$xValues)) {
  2874.             return PHPExcel_Calculation_Functions::VALUE();
  2875.         }
  2876.         $yValueCount count($yValues);
  2877.         $xValueCount count($xValues);
  2878.  
  2879.         if (($yValueCount == 0|| ($yValueCount != $xValueCount)) {
  2880.             return PHPExcel_Calculation_Functions::NA();
  2881.         elseif ($yValueCount == 1{
  2882.             return PHPExcel_Calculation_Functions::DIV0();
  2883.         }
  2884.  
  2885.         $bestFitLinear trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  2886.         return $bestFitLinear->getSlope();
  2887.     }    //    function SLOPE()
  2888.  
  2889.  
  2890.     /**
  2891.      *    SMALL
  2892.      *
  2893.      *    Returns the nth smallest value in a data set. You can use this function to
  2894.      *        select a value based on its relative standing.
  2895.      *
  2896.      *    Excel Function:
  2897.      *        SMALL(value1[,value2[, ...]],entry)
  2898.      *
  2899.      *    @access    public
  2900.      *    @category Statistical Functions
  2901.      *    @param    mixed        $arg,...        Data values
  2902.      *    @param    int            $entry            Position (ordered from the smallest) in the array or range of data to return
  2903.      *    @return    float 
  2904.      */
  2905.     public static function SMALL({
  2906.  
  2907.         // Calculate
  2908.         $entry array_pop($aArgs);
  2909.  
  2910.         if ((is_numeric($entry)) && (!is_string($entry))) {
  2911.             $mArgs array();
  2912.             foreach ($aArgs as $arg{
  2913.                 // Is it a numeric value?
  2914.                 if ((is_numeric($arg)) && (!is_string($arg))) {
  2915.                     $mArgs[$arg;
  2916.                 }
  2917.             }
  2918.             $count self::COUNT($mArgs);
  2919.             $entry floor(--$entry);
  2920.             if (($entry 0|| ($entry >= $count|| ($count == 0)) {
  2921.                 return PHPExcel_Calculation_Functions::NaN();
  2922.             }
  2923.             sort($mArgs);
  2924.             return $mArgs[$entry];
  2925.         }
  2926.         return PHPExcel_Calculation_Functions::VALUE();
  2927.     }    //    function SMALL()
  2928.  
  2929.  
  2930.     /**
  2931.      *    STANDARDIZE
  2932.      *
  2933.      *    Returns a normalized value from a distribution characterized by mean and standard_dev.
  2934.      *
  2935.      *    @param    float    $value        Value to normalize
  2936.      *    @param    float    $mean        Mean Value
  2937.      *    @param    float    $stdDev        Standard Deviation
  2938.      *    @return    float    Standardized value
  2939.      */
  2940.     public static function STANDARDIZE($value,$mean,$stdDev{
  2941.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2942.         $mean    PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  2943.         $stdDev    PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  2944.  
  2945.         if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
  2946.             if ($stdDev <= 0{
  2947.                 return PHPExcel_Calculation_Functions::NaN();
  2948.             }
  2949.             return ($value $mean$stdDev ;
  2950.         }
  2951.         return PHPExcel_Calculation_Functions::VALUE();
  2952.     }    //    function STANDARDIZE()
  2953.  
  2954.  
  2955.     /**
  2956.      *    STDEV
  2957.      *
  2958.      *    Estimates standard deviation based on a sample. The standard deviation is a measure of how
  2959.      *    widely values are dispersed from the average value (the mean).
  2960.      *
  2961.      *    Excel Function:
  2962.      *        STDEV(value1[,value2[, ...]])
  2963.      *
  2964.      *    @access    public
  2965.      *    @category Statistical Functions
  2966.      *    @param    mixed        $arg,...        Data values
  2967.      *    @return    float 
  2968.      */
  2969.     public static function STDEV({
  2970.  
  2971.         // Return value
  2972.         $returnValue null;
  2973.  
  2974.         $aMean self::AVERAGE($aArgs);
  2975.         if (!is_null($aMean)) {
  2976.             $aCount = -1;
  2977.             foreach ($aArgs as $k => $arg{
  2978.                 if ((is_bool($arg)) &&
  2979.                     ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  2980.                     $arg = (integer) $arg;
  2981.                 }
  2982.                 // Is it a numeric value?
  2983.                 if ((is_numeric($arg)) && (!is_string($arg))) {
  2984.                     if (is_null($returnValue)) {
  2985.                         $returnValue pow(($arg $aMean),2);
  2986.                     else {
  2987.                         $returnValue += pow(($arg $aMean),2);
  2988.                     }
  2989.                     ++$aCount;
  2990.                 }
  2991.             }
  2992.  
  2993.             // Return
  2994.             if (($aCount 0&& ($returnValue >= 0)) {
  2995.                 return sqrt($returnValue $aCount);
  2996.             }
  2997.         }
  2998.         return PHPExcel_Calculation_Functions::DIV0();
  2999.     }    //    function STDEV()
  3000.  
  3001.  
  3002.     /**
  3003.      *    STDEVA
  3004.      *
  3005.      *    Estimates standard deviation based on a sample, including numbers, text, and logical values
  3006.      *
  3007.      *    Excel Function:
  3008.      *        STDEVA(value1[,value2[, ...]])
  3009.      *
  3010.      *    @access    public
  3011.      *    @category Statistical Functions
  3012.      *    @param    mixed        $arg,...        Data values
  3013.      *    @return    float 
  3014.      */
  3015.     public static function STDEVA({
  3016.  
  3017.         // Return value
  3018.         $returnValue null;
  3019.  
  3020.         $aMean self::AVERAGEA($aArgs);
  3021.         if (!is_null($aMean)) {
  3022.             $aCount = -1;
  3023.             foreach ($aArgs as $k => $arg{
  3024.                 if ((is_bool($arg)) &&
  3025.                     (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  3026.                 else {
  3027.                     // Is it a numeric value?
  3028.                     if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg($arg != '')))) {
  3029.                         if (is_bool($arg)) {
  3030.                             $arg = (integer) $arg;
  3031.                         elseif (is_string($arg)) {
  3032.                             $arg 0;
  3033.                         }
  3034.                         if (is_null($returnValue)) {
  3035.                             $returnValue pow(($arg $aMean),2);
  3036.                         else {
  3037.                             $returnValue += pow(($arg $aMean),2);
  3038.                         }
  3039.                         ++$aCount;
  3040.                     }
  3041.                 }
  3042.             }
  3043.  
  3044.             // Return
  3045.             if (($aCount 0&& ($returnValue >= 0)) {
  3046.                 return sqrt($returnValue $aCount);
  3047.             }
  3048.         }
  3049.         return PHPExcel_Calculation_Functions::DIV0();
  3050.     }    //    function STDEVA()
  3051.  
  3052.  
  3053.     /**
  3054.      *    STDEVP
  3055.      *
  3056.      *    Calculates standard deviation based on the entire population
  3057.      *
  3058.      *    Excel Function:
  3059.      *        STDEVP(value1[,value2[, ...]])
  3060.      *
  3061.      *    @access    public
  3062.      *    @category Statistical Functions
  3063.      *    @param    mixed        $arg,...        Data values
  3064.      *    @return    float 
  3065.      */
  3066.     public static function STDEVP({
  3067.  
  3068.         // Return value
  3069.         $returnValue null;
  3070.  
  3071.         $aMean self::AVERAGE($aArgs);
  3072.         if (!is_null($aMean)) {
  3073.             $aCount 0;
  3074.             foreach ($aArgs as $k => $arg{
  3075.                 if ((is_bool($arg)) &&
  3076.                     ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  3077.                     $arg = (integer) $arg;
  3078.                 }
  3079.                 // Is it a numeric value?
  3080.                 if ((is_numeric($arg)) && (!is_string($arg))) {
  3081.                     if (is_null($returnValue)) {
  3082.                         $returnValue pow(($arg $aMean),2);
  3083.                     else {
  3084.                         $returnValue += pow(($arg $aMean),2);
  3085.                     }
  3086.                     ++$aCount;
  3087.                 }
  3088.             }
  3089.  
  3090.             // Return
  3091.             if (($aCount 0&& ($returnValue >= 0)) {
  3092.                 return sqrt($returnValue $aCount);
  3093.             }
  3094.         }
  3095.         return PHPExcel_Calculation_Functions::DIV0();
  3096.     }    //    function STDEVP()
  3097.  
  3098.  
  3099.     /**
  3100.      *    STDEVPA
  3101.      *
  3102.      *    Calculates standard deviation based on the entire population, including numbers, text, and logical values
  3103.      *
  3104.      *    Excel Function:
  3105.      *        STDEVPA(value1[,value2[, ...]])
  3106.      *
  3107.      *    @access    public
  3108.      *    @category Statistical Functions
  3109.      *    @param    mixed        $arg,...        Data values
  3110.      *    @return    float 
  3111.      */
  3112.     public static function STDEVPA({
  3113.  
  3114.         // Return value
  3115.         $returnValue null;
  3116.  
  3117.         $aMean self::AVERAGEA($aArgs);
  3118.         if (!is_null($aMean)) {
  3119.             $aCount 0;
  3120.             foreach ($aArgs as $k => $arg{
  3121.                 if ((is_bool($arg)) &&
  3122.                     (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  3123.                 else {
  3124.                     // Is it a numeric value?
  3125.                     if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg($arg != '')))) {
  3126.                         if (is_bool($arg)) {
  3127.                             $arg = (integer) $arg;
  3128.                         elseif (is_string($arg)) {
  3129.                             $arg 0;
  3130.                         }
  3131.                         if (is_null($returnValue)) {
  3132.                             $returnValue pow(($arg $aMean),2);
  3133.                         else {
  3134.                             $returnValue += pow(($arg $aMean),2);
  3135.                         }
  3136.                         ++$aCount;
  3137.                     }
  3138.                 }
  3139.             }
  3140.  
  3141.             // Return
  3142.             if (($aCount 0&& ($returnValue >= 0)) {
  3143.                 return sqrt($returnValue $aCount);
  3144.             }
  3145.         }
  3146.         return PHPExcel_Calculation_Functions::DIV0();
  3147.     }    //    function STDEVPA()
  3148.  
  3149.  
  3150.     /**
  3151.      *    STEYX
  3152.      *
  3153.      *    Returns the standard error of the predicted y-value for each x in the regression.
  3154.      *
  3155.      *    @param    array of mixed        Data Series Y
  3156.      *    @param    array of mixed        Data Series X
  3157.      *    @return    float 
  3158.      */
  3159.     public static function STEYX($yValues,$xValues{
  3160.         if (!self::_checkTrendArrays($yValues,$xValues)) {
  3161.             return PHPExcel_Calculation_Functions::VALUE();
  3162.         }
  3163.         $yValueCount count($yValues);
  3164.         $xValueCount count($xValues);
  3165.  
  3166.         if (($yValueCount == 0|| ($yValueCount != $xValueCount)) {
  3167.             return PHPExcel_Calculation_Functions::NA();
  3168.         elseif ($yValueCount == 1{
  3169.             return PHPExcel_Calculation_Functions::DIV0();
  3170.         }
  3171.  
  3172.         $bestFitLinear trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  3173.         return $bestFitLinear->getStdevOfResiduals();
  3174.     }    //    function STEYX()
  3175.  
  3176.  
  3177.     /**
  3178.      * TDIST
  3179.      *
  3180.      * Returns the probability of Student's T distribution.
  3181.      *
  3182.      * @param    float        $value            Value for the function
  3183.      * @param    float        $degrees        degrees of freedom
  3184.      * @param    float        $tails            number of tails (1 or 2)
  3185.      * @return    float 
  3186.      */
  3187.     public static function TDIST($value$degrees$tails{
  3188.         $value        PHPExcel_Calculation_Functions::flattenSingleValue($value);
  3189.         $degrees    floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  3190.         $tails        floor(PHPExcel_Calculation_Functions::flattenSingleValue($tails));
  3191.  
  3192.         if ((is_numeric($value)) && (is_numeric($degrees)) && (is_numeric($tails))) {
  3193.             if (($value 0|| ($degrees 1|| ($tails 1|| ($tails 2)) {
  3194.                 return PHPExcel_Calculation_Functions::NaN();
  3195.             }
  3196.             //    tdist, which finds the probability that corresponds to a given value
  3197.             //    of t with k degrees of freedom. This algorithm is translated from a
  3198.             //    pascal function on p81 of "Statistical Computing in Pascal" by D
  3199.             //    Cooke, A H Craven & G M Clark (1985: Edward Arnold (Pubs.) Ltd:
  3200.             //    London). The above Pascal algorithm is itself a translation of the
  3201.             //    fortran algoritm "AS 3" by B E Cooper of the Atlas Computer
  3202.             //    Laboratory as reported in (among other places) "Applied Statistics
  3203.             //    Algorithms", editied by P Griffiths and I D Hill (1985; Ellis
  3204.             //    Horwood Ltd.; W. Sussex, England).
  3205.             $tterm $degrees;
  3206.             $ttheta atan2($value,sqrt($tterm));
  3207.             $tc cos($ttheta);
  3208.             $ts sin($ttheta);
  3209.             $tsum 0;
  3210.  
  3211.             if (($degrees 2== 1{
  3212.                 $ti 3;
  3213.                 $tterm $tc;
  3214.             else {
  3215.                 $ti 2;
  3216.                 $tterm 1;
  3217.             }
  3218.  
  3219.             $tsum $tterm;
  3220.             while ($ti $degrees{
  3221.                 $tterm *= $tc $tc ($ti 1$ti;
  3222.                 $tsum += $tterm;
  3223.                 $ti += 2;
  3224.             }
  3225.             $tsum *= $ts;
  3226.             if (($degrees 2== 1$tsum M_2DIVPI ($tsum $ttheta)}
  3227.             $tValue 0.5 ($tsum);
  3228.             if ($tails == 1{
  3229.                 return abs($tValue);
  3230.             else {
  3231.                 return abs(($tValue$tValue);
  3232.             }
  3233.         }
  3234.         return PHPExcel_Calculation_Functions::VALUE();
  3235.     }    //    function TDIST()
  3236.  
  3237.  
  3238.     /**
  3239.      * TINV
  3240.      *
  3241.      * Returns the one-tailed probability of the chi-squared distribution.
  3242.      *
  3243.      * @param    float        $probability    Probability for the function
  3244.      * @param    float        $degrees        degrees of freedom
  3245.      * @return    float 
  3246.      */
  3247.     public static function TINV($probability$degrees{
  3248.         $probability    PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  3249.         $degrees        floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  3250.  
  3251.         if ((is_numeric($probability)) && (is_numeric($degrees))) {
  3252.             $xLo 100;
  3253.             $xHi 0;
  3254.  
  3255.             $x $xNew 1;
  3256.             $dx    1;
  3257.             $i 0;
  3258.  
  3259.             while ((abs($dxPRECISION&& ($i++ < MAX_ITERATIONS)) {
  3260.                 // Apply Newton-Raphson step
  3261.                 $result self::TDIST($x$degrees2);
  3262.                 $error $result $probability;
  3263.                 if ($error == 0.0{
  3264.                     $dx 0;
  3265.                 elseif ($error 0.0{
  3266.                     $xLo $x;
  3267.                 else {
  3268.                     $xHi $x;
  3269.                 }
  3270.                 // Avoid division by zero
  3271.                 if ($result != 0.0{
  3272.                     $dx $error $result;
  3273.                     $xNew $x $dx;
  3274.                 }
  3275.                 // If the NR fails to converge (which for example may be the
  3276.                 // case if the initial guess is too rough) we apply a bisection
  3277.                 // step to determine a more narrow interval around the root.
  3278.                 if (($xNew $xLo|| ($xNew $xHi|| ($result == 0.0)) {
  3279.                     $xNew ($xLo $xHi2;
  3280.                     $dx $xNew $x;
  3281.                 }
  3282.                 $x $xNew;
  3283.             }
  3284.             if ($i == MAX_ITERATIONS{
  3285.                 return PHPExcel_Calculation_Functions::NA();
  3286.             }
  3287.             return round($x,12);
  3288.         }
  3289.         return PHPExcel_Calculation_Functions::VALUE();
  3290.     }    //    function TINV()
  3291.  
  3292.  
  3293.     /**
  3294.      *    TREND
  3295.      *
  3296.      *    Returns values along a linear trend
  3297.      *
  3298.      *    @param    array of mixed        Data Series Y
  3299.      *    @param    array of mixed        Data Series X
  3300.      *    @param    array of mixed        Values of X for which we want to find Y
  3301.      *    @param    boolean                A logical value specifying whether to force the intersect to equal 0.
  3302.      *    @return    array of float
  3303.      */
  3304.     public static function TREND($yValues,$xValues=array(),$newValues=array(),$const=True{
  3305.         $yValues PHPExcel_Calculation_Functions::flattenArray($yValues);
  3306.         $xValues PHPExcel_Calculation_Functions::flattenArray($xValues);
  3307.         $newValues PHPExcel_Calculation_Functions::flattenArray($newValues);
  3308.         $const    (is_null($const))    True :    (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const);
  3309.  
  3310.         $bestFitLinear trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues,$const);
  3311.         if (count($newValues== 0{
  3312.             $newValues $bestFitLinear->getXValues();
  3313.         }
  3314.  
  3315.         $returnArray array();
  3316.         foreach($newValues as $xValue{
  3317.             $returnArray[0][$bestFitLinear->getValueOfYForX($xValue);
  3318.         }
  3319.  
  3320.         return $returnArray;
  3321.     }    //    function TREND()
  3322.  
  3323.  
  3324.     /**
  3325.      *    TRIMMEAN
  3326.      *
  3327.      *    Returns the mean of the interior of a data set. TRIMMEAN calculates the mean
  3328.      *    taken by excluding a percentage of data points from the top and bottom tails
  3329.      *    of a data set.
  3330.      *
  3331.      *    Excel Function:
  3332.      *        TRIMEAN(value1[,value2[, ...]],$discard)
  3333.      *
  3334.      *    @access    public
  3335.      *    @category Statistical Functions
  3336.      *    @param    mixed        $arg,...        Data values
  3337.      *    @param    float        $discard        Percentage to discard
  3338.      *    @return    float 
  3339.      */
  3340.     public static function TRIMMEAN({
  3341.  
  3342.         // Calculate
  3343.         $percent array_pop($aArgs);
  3344.  
  3345.         if ((is_numeric($percent)) && (!is_string($percent))) {
  3346.             if (($percent 0|| ($percent 1)) {
  3347.                 return PHPExcel_Calculation_Functions::NaN();
  3348.             }
  3349.             $mArgs array();
  3350.             foreach ($aArgs as $arg{
  3351.                 // Is it a numeric value?
  3352.                 if ((is_numeric($arg)) && (!is_string($arg))) {
  3353.                     $mArgs[$arg;
  3354.                 }
  3355.             }
  3356.             $discard floor(self::COUNT($mArgs$percent 2);
  3357.             sort($mArgs);
  3358.             for ($i=0$i $discard++$i{
  3359.                 array_pop($mArgs);
  3360.                 array_shift($mArgs);
  3361.             }
  3362.             return self::AVERAGE($mArgs);
  3363.         }
  3364.         return PHPExcel_Calculation_Functions::VALUE();
  3365.     }    //    function TRIMMEAN()
  3366.  
  3367.  
  3368.     /**
  3369.      *    VARFunc
  3370.      *
  3371.      *    Estimates variance based on a sample.
  3372.      *
  3373.      *    Excel Function:
  3374.      *        VAR(value1[,value2[, ...]])
  3375.      *
  3376.      *    @access    public
  3377.      *    @category Statistical Functions
  3378.      *    @param    mixed        $arg,...        Data values
  3379.      *    @return    float 
  3380.      */
  3381.     public static function VARFunc({
  3382.         // Return value
  3383.         $returnValue PHPExcel_Calculation_Functions::DIV0();
  3384.  
  3385.         $summerA $summerB 0;
  3386.  
  3387.         // Loop through arguments
  3388.         $aCount 0;
  3389.         foreach ($aArgs as $arg{
  3390.             if (is_bool($arg)) $arg = (integer) $arg}
  3391.             // Is it a numeric value?
  3392.             if ((is_numeric($arg)) && (!is_string($arg))) {
  3393.                 $summerA += ($arg $arg);
  3394.                 $summerB += $arg;
  3395.                 ++$aCount;
  3396.             }
  3397.         }
  3398.  
  3399.         // Return
  3400.         if ($aCount 1{
  3401.             $summerA *= $aCount;
  3402.             $summerB *= $summerB;
  3403.             $returnValue ($summerA $summerB($aCount ($aCount 1));
  3404.         }
  3405.         return $returnValue;
  3406.     }    //    function VARFunc()
  3407.  
  3408.  
  3409.     /**
  3410.      *    VARA
  3411.      *
  3412.      *    Estimates variance based on a sample, including numbers, text, and logical values
  3413.      *
  3414.      *    Excel Function:
  3415.      *        VARA(value1[,value2[, ...]])
  3416.      *
  3417.      *    @access    public
  3418.      *    @category Statistical Functions
  3419.      *    @param    mixed        $arg,...        Data values
  3420.      *    @return    float 
  3421.      */
  3422.     public static function VARA({
  3423.         // Return value
  3424.         $returnValue PHPExcel_Calculation_Functions::DIV0();
  3425.  
  3426.         $summerA $summerB 0;
  3427.  
  3428.         // Loop through arguments
  3429.         $aCount 0;
  3430.         foreach ($aArgs as $k => $arg{
  3431.             if ((is_string($arg)) &&
  3432.                 (PHPExcel_Calculation_Functions::isValue($k))) {
  3433.                 return PHPExcel_Calculation_Functions::VALUE();
  3434.             elseif ((is_string($arg)) &&
  3435.                 (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  3436.             else {
  3437.                 // Is it a numeric value?
  3438.                 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg($arg != '')))) {
  3439.                     if (is_bool($arg)) {
  3440.                         $arg = (integer) $arg;
  3441.                     elseif (is_string($arg)) {
  3442.                         $arg 0;
  3443.                     }
  3444.                     $summerA += ($arg $arg);
  3445.                     $summerB += $arg;
  3446.                     ++$aCount;
  3447.                 }
  3448.             }
  3449.         }
  3450.  
  3451.         // Return
  3452.         if ($aCount 1{
  3453.             $summerA *= $aCount;
  3454.             $summerB *= $summerB;
  3455.             $returnValue ($summerA $summerB($aCount ($aCount 1));
  3456.         }
  3457.         return $returnValue;
  3458.     }    //    function VARA()
  3459.  
  3460.  
  3461.     /**
  3462.      *    VARP
  3463.      *
  3464.      *    Calculates variance based on the entire population
  3465.      *
  3466.      *    Excel Function:
  3467.      *        VARP(value1[,value2[, ...]])
  3468.      *
  3469.      *    @access    public
  3470.      *    @category Statistical Functions
  3471.      *    @param    mixed        $arg,...        Data values
  3472.      *    @return    float 
  3473.      */
  3474.     public static function VARP({
  3475.         // Return value
  3476.         $returnValue PHPExcel_Calculation_Functions::DIV0();
  3477.  
  3478.         $summerA $summerB 0;
  3479.  
  3480.         // Loop through arguments
  3481.         $aCount 0;
  3482.         foreach ($aArgs as $arg{
  3483.             if (is_bool($arg)) $arg = (integer) $arg}
  3484.             // Is it a numeric value?
  3485.             if ((is_numeric($arg)) && (!is_string($arg))) {
  3486.                 $summerA += ($arg $arg);
  3487.                 $summerB += $arg;
  3488.                 ++$aCount;
  3489.             }
  3490.         }
  3491.  
  3492.         // Return
  3493.         if ($aCount 0{
  3494.             $summerA *= $aCount;
  3495.             $summerB *= $summerB;
  3496.             $returnValue ($summerA $summerB($aCount $aCount);
  3497.         }
  3498.         return $returnValue;
  3499.     }    //    function VARP()
  3500.  
  3501.  
  3502.     /**
  3503.      *    VARPA
  3504.      *
  3505.      *    Calculates variance based on the entire population, including numbers, text, and logical values
  3506.      *
  3507.      *    Excel Function:
  3508.      *        VARPA(value1[,value2[, ...]])
  3509.      *
  3510.      *    @access    public
  3511.      *    @category Statistical Functions
  3512.      *    @param    mixed        $arg,...        Data values
  3513.      *    @return    float 
  3514.      */
  3515.     public static function VARPA({
  3516.         // Return value
  3517.         $returnValue PHPExcel_Calculation_Functions::DIV0();
  3518.  
  3519.         $summerA $summerB 0;
  3520.  
  3521.         // Loop through arguments
  3522.         $aCount 0;
  3523.         foreach ($aArgs as $k => $arg{
  3524.             if ((is_string($arg)) &&
  3525.                 (PHPExcel_Calculation_Functions::isValue($k))) {
  3526.                 return PHPExcel_Calculation_Functions::VALUE();
  3527.             elseif ((is_string($arg)) &&
  3528.                 (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  3529.             else {
  3530.                 // Is it a numeric value?
  3531.                 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg($arg != '')))) {
  3532.                     if (is_bool($arg)) {
  3533.                         $arg = (integer) $arg;
  3534.                     elseif (is_string($arg)) {
  3535.                         $arg 0;
  3536.                     }
  3537.                     $summerA += ($arg $arg);
  3538.                     $summerB += $arg;
  3539.                     ++$aCount;
  3540.                 }
  3541.             }
  3542.         }
  3543.  
  3544.         // Return
  3545.         if ($aCount 0{
  3546.             $summerA *= $aCount;
  3547.             $summerB *= $summerB;
  3548.             $returnValue ($summerA $summerB($aCount $aCount);
  3549.         }
  3550.         return $returnValue;
  3551.     }    //    function VARPA()
  3552.  
  3553.  
  3554.     /**
  3555.      * WEIBULL
  3556.      *
  3557.      * Returns the Weibull distribution. Use this distribution in reliability
  3558.      * analysis, such as calculating a device's mean time to failure.
  3559.      *
  3560.      * @param    float        $value 
  3561.      * @param    float        $alpha        Alpha Parameter
  3562.      * @param    float        $beta        Beta Parameter
  3563.      * @param    boolean        $cumulative 
  3564.      * @return    float 
  3565.      *
  3566.      */
  3567.     public static function WEIBULL($value$alpha$beta$cumulative{
  3568.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  3569.         $alpha    PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  3570.         $beta    PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  3571.  
  3572.         if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta))) {
  3573.             if (($value 0|| ($alpha <= 0|| ($beta <= 0)) {
  3574.                 return PHPExcel_Calculation_Functions::NaN();
  3575.             }
  3576.             if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  3577.                 if ($cumulative{
  3578.                     return exp(pow($value $beta,$alpha));
  3579.                 else {
  3580.                     return ($alpha pow($beta,$alpha)) pow($value,$alpha 1exp(pow($value $beta,$alpha));
  3581.                 }
  3582.             }
  3583.         }
  3584.         return PHPExcel_Calculation_Functions::VALUE();
  3585.     }    //    function WEIBULL()
  3586.  
  3587.  
  3588.     /**
  3589.      * ZTEST
  3590.      *
  3591.      * Returns the Weibull distribution. Use this distribution in reliability
  3592.      * analysis, such as calculating a device's mean time to failure.
  3593.      *
  3594.      * @param    float        $value 
  3595.      * @param    float        $alpha        Alpha Parameter
  3596.      * @param    float        $beta        Beta Parameter
  3597.      * @param    boolean        $cumulative 
  3598.      * @return    float 
  3599.      *
  3600.      */
  3601.     public static function ZTEST($dataSet$m0$sigma=null{
  3602.         $dataSet    PHPExcel_Calculation_Functions::flattenArrayIndexed($dataSet);
  3603.         $m0            PHPExcel_Calculation_Functions::flattenSingleValue($m0);
  3604.         $sigma        PHPExcel_Calculation_Functions::flattenSingleValue($sigma);
  3605.  
  3606.         if (is_null($sigma)) {
  3607.             $sigma self::STDEV($dataSet);
  3608.         }
  3609.         $n count($dataSet);
  3610.  
  3611.         return self::NORMSDIST((self::AVERAGE($dataSet$m0)/($sigma/SQRT($n)));
  3612.     }    //    function ZTEST()
  3613.  
  3614. }    //    class PHPExcel_Calculation_Statistical

Documentation generated on Sun, 27 Feb 2011 16:34:05 -0800 by phpDocumentor 1.4.3