PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 1ms

/common/libraries/plugin/phpexcel/PHPExcel/Calculation/Statistical.php

https://bitbucket.org/ywarnier/chamilo-dev
PHP | 4183 lines | 2565 code | 384 blank | 1234 comment | 616 complexity | da7daf734cb14eab3ea98c886bc3f5ab MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT

Large files files are truncated, but you can click here to view the full file

  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. /** PHPExcel root directory */
  28. if (! defined('PHPEXCEL_ROOT'))
  29. {
  30. /**
  31. * @ignore
  32. */
  33. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  34. require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  35. }
  36. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/trendClass.php';
  37. /** LOG_GAMMA_X_MAX_VALUE */
  38. define('LOG_GAMMA_X_MAX_VALUE', 2.55e305);
  39. /** XMININ */
  40. define('XMININ', 2.23e-308);
  41. /** EPS */
  42. define('EPS', 2.22e-16);
  43. /** SQRT2PI */
  44. define('SQRT2PI', 2.5066282746310005024157652848110452530069867406099);
  45. /**
  46. * PHPExcel_Calculation_Statistical
  47. *
  48. * @category PHPExcel
  49. * @package PHPExcel_Calculation
  50. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  51. */
  52. class PHPExcel_Calculation_Statistical
  53. {
  54. private static function _checkTrendArrays(&$array1, &$array2)
  55. {
  56. if (! is_array($array1))
  57. {
  58. $array1 = array($array1);
  59. }
  60. if (! is_array($array2))
  61. {
  62. $array2 = array($array2);
  63. }
  64. $array1 = PHPExcel_Calculation_Functions :: flattenArray($array1);
  65. $array2 = PHPExcel_Calculation_Functions :: flattenArray($array2);
  66. foreach ($array1 as $key => $value)
  67. {
  68. if ((is_bool($value)) || (is_string($value)) || (is_null($value)))
  69. {
  70. unset($array1[$key]);
  71. unset($array2[$key]);
  72. }
  73. }
  74. foreach ($array2 as $key => $value)
  75. {
  76. if ((is_bool($value)) || (is_string($value)) || (is_null($value)))
  77. {
  78. unset($array1[$key]);
  79. unset($array2[$key]);
  80. }
  81. }
  82. $array1 = array_merge($array1);
  83. $array2 = array_merge($array2);
  84. return True;
  85. } // function _checkTrendArrays()
  86. /**
  87. * Beta function.
  88. *
  89. * @author Jaco van Kooten
  90. *
  91. * @param p require p>0
  92. * @param q require q>0
  93. * @return 0 if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow
  94. */
  95. private static function _beta($p, $q)
  96. {
  97. if ($p <= 0.0 || $q <= 0.0 || ($p + $q) > LOG_GAMMA_X_MAX_VALUE)
  98. {
  99. return 0.0;
  100. }
  101. else
  102. {
  103. return exp(self :: _logBeta($p, $q));
  104. }
  105. } // function _beta()
  106. /**
  107. * Incomplete beta function
  108. *
  109. * @author Jaco van Kooten
  110. * @author Paul Meagher
  111. *
  112. * The computation is based on formulas from Numerical Recipes, Chapter 6.4 (W.H. Press et al, 1992).
  113. * @param x require 0<=x<=1
  114. * @param p require p>0
  115. * @param q require q>0
  116. * @return 0 if x<0, p<=0, q<=0 or p+q>2.55E305 and 1 if x>1 to avoid errors and over/underflow
  117. */
  118. private static function _incompleteBeta($x, $p, $q)
  119. {
  120. if ($x <= 0.0)
  121. {
  122. return 0.0;
  123. }
  124. elseif ($x >= 1.0)
  125. {
  126. return 1.0;
  127. }
  128. elseif (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > LOG_GAMMA_X_MAX_VALUE))
  129. {
  130. return 0.0;
  131. }
  132. $beta_gam = exp((0 - self :: _logBeta($p, $q)) + $p * log($x) + $q * log(1.0 - $x));
  133. if ($x < ($p + 1.0) / ($p + $q + 2.0))
  134. {
  135. return $beta_gam * self :: _betaFraction($x, $p, $q) / $p;
  136. }
  137. else
  138. {
  139. return 1.0 - ($beta_gam * self :: _betaFraction(1 - $x, $q, $p) / $q);
  140. }
  141. } // function _incompleteBeta()
  142. // Function cache for _logBeta function
  143. private static $_logBetaCache_p = 0.0;
  144. private static $_logBetaCache_q = 0.0;
  145. private static $_logBetaCache_result = 0.0;
  146. /**
  147. * The natural logarithm of the beta function.
  148. * @param p require p>0
  149. * @param q require q>0
  150. * @return 0 if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow
  151. * @author Jaco van Kooten
  152. */
  153. private static function _logBeta($p, $q)
  154. {
  155. if ($p != self :: $_logBetaCache_p || $q != self :: $_logBetaCache_q)
  156. {
  157. self :: $_logBetaCache_p = $p;
  158. self :: $_logBetaCache_q = $q;
  159. if (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > LOG_GAMMA_X_MAX_VALUE))
  160. {
  161. self :: $_logBetaCache_result = 0.0;
  162. }
  163. else
  164. {
  165. self :: $_logBetaCache_result = self :: _logGamma($p) + self :: _logGamma($q) - self :: _logGamma($p + $q);
  166. }
  167. }
  168. return self :: $_logBetaCache_result;
  169. } // function _logBeta()
  170. /**
  171. * Evaluates of continued fraction part of incomplete beta function.
  172. * Based on an idea from Numerical Recipes (W.H. Press et al, 1992).
  173. * @author Jaco van Kooten
  174. */
  175. private static function _betaFraction($x, $p, $q)
  176. {
  177. $c = 1.0;
  178. $sum_pq = $p + $q;
  179. $p_plus = $p + 1.0;
  180. $p_minus = $p - 1.0;
  181. $h = 1.0 - $sum_pq * $x / $p_plus;
  182. if (abs($h) < XMININ)
  183. {
  184. $h = XMININ;
  185. }
  186. $h = 1.0 / $h;
  187. $frac = $h;
  188. $m = 1;
  189. $delta = 0.0;
  190. while ($m <= MAX_ITERATIONS && abs($delta - 1.0) > PRECISION)
  191. {
  192. $m2 = 2 * $m;
  193. // even index for d
  194. $d = $m * ($q - $m) * $x / (($p_minus + $m2) * ($p + $m2));
  195. $h = 1.0 + $d * $h;
  196. if (abs($h) < XMININ)
  197. {
  198. $h = XMININ;
  199. }
  200. $h = 1.0 / $h;
  201. $c = 1.0 + $d / $c;
  202. if (abs($c) < XMININ)
  203. {
  204. $c = XMININ;
  205. }
  206. $frac *= $h * $c;
  207. // odd index for d
  208. $d = - ($p + $m) * ($sum_pq + $m) * $x / (($p + $m2) * ($p_plus + $m2));
  209. $h = 1.0 + $d * $h;
  210. if (abs($h) < XMININ)
  211. {
  212. $h = XMININ;
  213. }
  214. $h = 1.0 / $h;
  215. $c = 1.0 + $d / $c;
  216. if (abs($c) < XMININ)
  217. {
  218. $c = XMININ;
  219. }
  220. $delta = $h * $c;
  221. $frac *= $delta;
  222. ++ $m;
  223. }
  224. return $frac;
  225. } // function _betaFraction()
  226. /**
  227. * logGamma function
  228. *
  229. * @version 1.1
  230. * @author Jaco van Kooten
  231. *
  232. * Original author was Jaco van Kooten. Ported to PHP by Paul Meagher.
  233. *
  234. * The natural logarithm of the gamma function. <br />
  235. * Based on public domain NETLIB (Fortran) code by W. J. Cody and L. Stoltz <br />
  236. * Applied Mathematics Division <br />
  237. * Argonne National Laboratory <br />
  238. * Argonne, IL 60439 <br />
  239. * <p>
  240. * References:
  241. * <ol>
  242. * <li>W. J. Cody and K. E. Hillstrom, 'Chebyshev Approximations for the Natural
  243. * Logarithm of the Gamma Function,' Math. Comp. 21, 1967, pp. 198-203.</li>
  244. * <li>K. E. Hillstrom, ANL/AMD Program ANLC366S, DGAMMA/DLGAMA, May, 1969.</li>
  245. * <li>Hart, Et. Al., Computer Approximations, Wiley and sons, New York, 1968.</li>
  246. * </ol>
  247. * </p>
  248. * <p>
  249. * From the original documentation:
  250. * </p>
  251. * <p>
  252. * This routine calculates the LOG(GAMMA) function for a positive real argument X.
  253. * Computation is based on an algorithm outlined in references 1 and 2.
  254. * The program uses rational functions that theoretically approximate LOG(GAMMA)
  255. * to at least 18 significant decimal digits. The approximation for X > 12 is from
  256. * reference 3, while approximations for X < 12.0 are similar to those in reference
  257. * 1, but are unpublished. The accuracy achieved depends on the arithmetic system,
  258. * the compiler, the intrinsic functions, and proper selection of the
  259. * machine-dependent constants.
  260. * </p>
  261. * <p>
  262. * Error returns: <br />
  263. * The program returns the value XINF for X .LE. 0.0 or when overflow would occur.
  264. * The computation is believed to be free of underflow and overflow.
  265. * </p>
  266. * @return MAX_VALUE for x < 0.0 or when overflow would occur, i.e. x > 2.55E305
  267. */
  268. // Function cache for logGamma
  269. private static $_logGammaCache_result = 0.0;
  270. private static $_logGammaCache_x = 0.0;
  271. private static function _logGamma($x)
  272. {
  273. // Log Gamma related constants
  274. static $lg_d1 = - 0.5772156649015328605195174;
  275. static $lg_d2 = 0.4227843350984671393993777;
  276. static $lg_d4 = 1.791759469228055000094023;
  277. static $lg_p1 = array(4.945235359296727046734888, 201.8112620856775083915565, 2290.838373831346393026739,
  278. 11319.67205903380828685045, 28557.24635671635335736389, 38484.96228443793359990269,
  279. 26377.48787624195437963534, 7225.813979700288197698961);
  280. static $lg_p2 = array(4.974607845568932035012064, 542.4138599891070494101986, 15506.93864978364947665077,
  281. 184793.2904445632425417223, 1088204.76946882876749847, 3338152.967987029735917223,
  282. 5106661.678927352456275255, 3074109.054850539556250927);
  283. static $lg_p4 = array(14745.02166059939948905062, 2426813.369486704502836312, 121475557.4045093227939592,
  284. 2663432449.630976949898078, 29403789566.34553899906876, 170266573776.5398868392998,
  285. 492612579337.743088758812, 560625185622.3951465078242);
  286. static $lg_q1 = array(67.48212550303777196073036, 1113.332393857199323513008, 7738.757056935398733233834,
  287. 27639.87074403340708898585, 54993.10206226157329794414, 61611.22180066002127833352,
  288. 36351.27591501940507276287, 8785.536302431013170870835);
  289. static $lg_q2 = array(183.0328399370592604055942, 7765.049321445005871323047, 133190.3827966074194402448,
  290. 1136705.821321969608938755, 5267964.117437946917577538, 13467014.54311101692290052,
  291. 17827365.30353274213975932, 9533095.591844353613395747);
  292. static $lg_q4 = array(2690.530175870899333379843, 639388.5654300092398984238, 41355999.30241388052042842,
  293. 1120872109.61614794137657, 14886137286.78813811542398, 101680358627.2438228077304,
  294. 341747634550.7377132798597, 446315818741.9713286462081);
  295. static $lg_c = array(- 0.001910444077728, 8.4171387781295e-4, - 5.952379913043012e-4, 7.93650793500350248e-4,
  296. - 0.002777777777777681622553, 0.08333333333333333331554247, 0.0057083835261);
  297. // Rough estimate of the fourth root of logGamma_xBig
  298. static $lg_frtbig = 2.25e76;
  299. static $pnt68 = 0.6796875;
  300. if ($x == self :: $_logGammaCache_x)
  301. {
  302. return self :: $_logGammaCache_result;
  303. }
  304. $y = $x;
  305. if ($y > 0.0 && $y <= LOG_GAMMA_X_MAX_VALUE)
  306. {
  307. if ($y <= EPS)
  308. {
  309. $res = - log(y);
  310. }
  311. elseif ($y <= 1.5)
  312. {
  313. // ---------------------
  314. // EPS .LT. X .LE. 1.5
  315. // ---------------------
  316. if ($y < $pnt68)
  317. {
  318. $corr = - log($y);
  319. $xm1 = $y;
  320. }
  321. else
  322. {
  323. $corr = 0.0;
  324. $xm1 = $y - 1.0;
  325. }
  326. if ($y <= 0.5 || $y >= $pnt68)
  327. {
  328. $xden = 1.0;
  329. $xnum = 0.0;
  330. for($i = 0; $i < 8; ++ $i)
  331. {
  332. $xnum = $xnum * $xm1 + $lg_p1[$i];
  333. $xden = $xden * $xm1 + $lg_q1[$i];
  334. }
  335. $res = $corr + $xm1 * ($lg_d1 + $xm1 * ($xnum / $xden));
  336. }
  337. else
  338. {
  339. $xm2 = $y - 1.0;
  340. $xden = 1.0;
  341. $xnum = 0.0;
  342. for($i = 0; $i < 8; ++ $i)
  343. {
  344. $xnum = $xnum * $xm2 + $lg_p2[$i];
  345. $xden = $xden * $xm2 + $lg_q2[$i];
  346. }
  347. $res = $corr + $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden));
  348. }
  349. }
  350. elseif ($y <= 4.0)
  351. {
  352. // ---------------------
  353. // 1.5 .LT. X .LE. 4.0
  354. // ---------------------
  355. $xm2 = $y - 2.0;
  356. $xden = 1.0;
  357. $xnum = 0.0;
  358. for($i = 0; $i < 8; ++ $i)
  359. {
  360. $xnum = $xnum * $xm2 + $lg_p2[$i];
  361. $xden = $xden * $xm2 + $lg_q2[$i];
  362. }
  363. $res = $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden));
  364. }
  365. elseif ($y <= 12.0)
  366. {
  367. // ----------------------
  368. // 4.0 .LT. X .LE. 12.0
  369. // ----------------------
  370. $xm4 = $y - 4.0;
  371. $xden = - 1.0;
  372. $xnum = 0.0;
  373. for($i = 0; $i < 8; ++ $i)
  374. {
  375. $xnum = $xnum * $xm4 + $lg_p4[$i];
  376. $xden = $xden * $xm4 + $lg_q4[$i];
  377. }
  378. $res = $lg_d4 + $xm4 * ($xnum / $xden);
  379. }
  380. else
  381. {
  382. // ---------------------------------
  383. // Evaluate for argument .GE. 12.0
  384. // ---------------------------------
  385. $res = 0.0;
  386. if ($y <= $lg_frtbig)
  387. {
  388. $res = $lg_c[6];
  389. $ysq = $y * $y;
  390. for($i = 0; $i < 6; ++ $i)
  391. $res = $res / $ysq + $lg_c[$i];
  392. }
  393. $res /= $y;
  394. $corr = log($y);
  395. $res = $res + log(SQRT2PI) - 0.5 * $corr;
  396. $res += $y * ($corr - 1.0);
  397. }
  398. }
  399. else
  400. {
  401. // --------------------------
  402. // Return for bad arguments
  403. // --------------------------
  404. $res = MAX_VALUE;
  405. }
  406. // ------------------------------
  407. // Final adjustments and return
  408. // ------------------------------
  409. self :: $_logGammaCache_x = $x;
  410. self :: $_logGammaCache_result = $res;
  411. return $res;
  412. } // function _logGamma()
  413. //
  414. // Private implementation of the incomplete Gamma function
  415. //
  416. private static function _incompleteGamma($a, $x)
  417. {
  418. static $max = 32;
  419. $summer = 0;
  420. for($n = 0; $n <= $max; ++ $n)
  421. {
  422. $divisor = $a;
  423. for($i = 1; $i <= $n; ++ $i)
  424. {
  425. $divisor *= ($a + $i);
  426. }
  427. $summer += (pow($x, $n) / $divisor);
  428. }
  429. return pow($x, $a) * exp(0 - $x) * $summer;
  430. } // function _incompleteGamma()
  431. //
  432. // Private implementation of the Gamma function
  433. //
  434. private static function _gamma($data)
  435. {
  436. if ($data == 0.0)
  437. return 0;
  438. static $p0 = 1.000000000190015;
  439. static $p = array(1 => 76.18009172947146, 2 => - 86.50532032941677, 3 => 24.01409824083091,
  440. 4 => - 1.231739572450155, 5 => 1.208650973866179e-3, 6 => - 5.395239384953e-6);
  441. $y = $x = $data;
  442. $tmp = $x + 5.5;
  443. $tmp -= ($x + 0.5) * log($tmp);
  444. $summer = $p0;
  445. for($j = 1; $j <= 6; ++ $j)
  446. {
  447. $summer += ($p[$j] / ++ $y);
  448. }
  449. return exp(0 - $tmp + log(SQRT2PI * $summer / $x));
  450. } // function _gamma()
  451. /***************************************************************************
  452. * inverse_ncdf.php
  453. * -------------------
  454. * begin : Friday, January 16, 2004
  455. * copyright : (C) 2004 Michael Nickerson
  456. * email : nickersonm@yahoo.com
  457. *
  458. ***************************************************************************/
  459. private static function _inverse_ncdf($p)
  460. {
  461. // Inverse ncdf approximation by Peter J. Acklam, implementation adapted to
  462. // PHP by Michael Nickerson, using Dr. Thomas Ziegler's C implementation as
  463. // a guide. http://home.online.no/~pjacklam/notes/invnorm/index.html
  464. // I have not checked the accuracy of this implementation. Be aware that PHP
  465. // will truncate the coeficcients to 14 digits.
  466. // You have permission to use and distribute this function freely for
  467. // whatever purpose you want, but please show common courtesy and give credit
  468. // where credit is due.
  469. // Input paramater is $p - probability - where 0 < p < 1.
  470. // Coefficients in rational approximations
  471. static $a = array(1 => - 3.969683028665376e+01, 2 => 2.209460984245205e+02, 3 => - 2.759285104469687e+02,
  472. 4 => 1.383577518672690e+02, 5 => - 3.066479806614716e+01, 6 => 2.506628277459239e+00);
  473. static $b = array(1 => - 5.447609879822406e+01, 2 => 1.615858368580409e+02, 3 => - 1.556989798598866e+02,
  474. 4 => 6.680131188771972e+01, 5 => - 1.328068155288572e+01);
  475. static $c = array(1 => - 7.784894002430293e-03, 2 => - 3.223964580411365e-01, 3 => - 2.400758277161838e+00,
  476. 4 => - 2.549732539343734e+00, 5 => 4.374664141464968e+00, 6 => 2.938163982698783e+00);
  477. static $d = array(1 => 7.784695709041462e-03, 2 => 3.224671290700398e-01, 3 => 2.445134137142996e+00,
  478. 4 => 3.754408661907416e+00);
  479. // Define lower and upper region break-points.
  480. $p_low = 0.02425; //Use lower region approx. below this
  481. $p_high = 1 - $p_low; //Use upper region approx. above this
  482. if (0 < $p && $p < $p_low)
  483. {
  484. // Rational approximation for lower region.
  485. $q = sqrt(- 2 * log($p));
  486. return ((((($c[1] * $q + $c[2]) * $q + $c[3]) * $q + $c[4]) * $q + $c[5]) * $q + $c[6]) / (((($d[1] * $q + $d[2]) * $q + $d[3]) * $q + $d[4]) * $q + 1);
  487. }
  488. elseif ($p_low <= $p && $p <= $p_high)
  489. {
  490. // Rational approximation for central region.
  491. $q = $p - 0.5;
  492. $r = $q * $q;
  493. return ((((($a[1] * $r + $a[2]) * $r + $a[3]) * $r + $a[4]) * $r + $a[5]) * $r + $a[6]) * $q / ((((($b[1] * $r + $b[2]) * $r + $b[3]) * $r + $b[4]) * $r + $b[5]) * $r + 1);
  494. }
  495. elseif ($p_high < $p && $p < 1)
  496. {
  497. // Rational approximation for upper region.
  498. $q = sqrt(- 2 * log(1 - $p));
  499. return - ((((($c[1] * $q + $c[2]) * $q + $c[3]) * $q + $c[4]) * $q + $c[5]) * $q + $c[6]) / (((($d[1] * $q + $d[2]) * $q + $d[3]) * $q + $d[4]) * $q + 1);
  500. }
  501. // If 0 < p < 1, return a null value
  502. return PHPExcel_Calculation_Functions :: NULL();
  503. } // function _inverse_ncdf()
  504. private static function _inverse_ncdf2($prob)
  505. {
  506. // Approximation of inverse standard normal CDF developed by
  507. // B. Moro, "The Full Monte," Risk 8(2), Feb 1995, 57-58.
  508. $a1 = 2.50662823884;
  509. $a2 = - 18.61500062529;
  510. $a3 = 41.39119773534;
  511. $a4 = - 25.44106049637;
  512. $b1 = - 8.4735109309;
  513. $b2 = 23.08336743743;
  514. $b3 = - 21.06224101826;
  515. $b4 = 3.13082909833;
  516. $c1 = 0.337475482272615;
  517. $c2 = 0.976169019091719;
  518. $c3 = 0.160797971491821;
  519. $c4 = 2.76438810333863E-02;
  520. $c5 = 3.8405729373609E-03;
  521. $c6 = 3.951896511919E-04;
  522. $c7 = 3.21767881768E-05;
  523. $c8 = 2.888167364E-07;
  524. $c9 = 3.960315187E-07;
  525. $y = $prob - 0.5;
  526. if (abs($y) < 0.42)
  527. {
  528. $z = ($y * $y);
  529. $z = $y * ((($a4 * $z + $a3) * $z + $a2) * $z + $a1) / (((($b4 * $z + $b3) * $z + $b2) * $z + $b1) * $z + 1);
  530. }
  531. else
  532. {
  533. if ($y > 0)
  534. {
  535. $z = log(- log(1 - $prob));
  536. }
  537. else
  538. {
  539. $z = log(- log($prob));
  540. }
  541. $z = $c1 + $z * ($c2 + $z * ($c3 + $z * ($c4 + $z * ($c5 + $z * ($c6 + $z * ($c7 + $z * ($c8 + $z * $c9)))))));
  542. if ($y < 0)
  543. {
  544. $z = - $z;
  545. }
  546. }
  547. return $z;
  548. } // function _inverse_ncdf2()
  549. private static function _inverse_ncdf3($p)
  550. {
  551. // ALGORITHM AS241 APPL. STATIST. (1988) VOL. 37, NO. 3.
  552. // Produces the normal deviate Z corresponding to a given lower
  553. // tail area of P; Z is accurate to about 1 part in 10**16.
  554. //
  555. // This is a PHP version of the original FORTRAN code that can
  556. // be found at http://lib.stat.cmu.edu/apstat/
  557. $split1 = 0.425;
  558. $split2 = 5;
  559. $const1 = 0.180625;
  560. $const2 = 1.6;
  561. // coefficients for p close to 0.5
  562. $a0 = 3.3871328727963666080;
  563. $a1 = 1.3314166789178437745E+2;
  564. $a2 = 1.9715909503065514427E+3;
  565. $a3 = 1.3731693765509461125E+4;
  566. $a4 = 4.5921953931549871457E+4;
  567. $a5 = 6.7265770927008700853E+4;
  568. $a6 = 3.3430575583588128105E+4;
  569. $a7 = 2.5090809287301226727E+3;
  570. $b1 = 4.2313330701600911252E+1;
  571. $b2 = 6.8718700749205790830E+2;
  572. $b3 = 5.3941960214247511077E+3;
  573. $b4 = 2.1213794301586595867E+4;
  574. $b5 = 3.9307895800092710610E+4;
  575. $b6 = 2.8729085735721942674E+4;
  576. $b7 = 5.2264952788528545610E+3;
  577. // coefficients for p not close to 0, 0.5 or 1.
  578. $c0 = 1.42343711074968357734;
  579. $c1 = 4.63033784615654529590;
  580. $c2 = 5.76949722146069140550;
  581. $c3 = 3.64784832476320460504;
  582. $c4 = 1.27045825245236838258;
  583. $c5 = 2.41780725177450611770E-1;
  584. $c6 = 2.27238449892691845833E-2;
  585. $c7 = 7.74545014278341407640E-4;
  586. $d1 = 2.05319162663775882187;
  587. $d2 = 1.67638483018380384940;
  588. $d3 = 6.89767334985100004550E-1;
  589. $d4 = 1.48103976427480074590E-1;
  590. $d5 = 1.51986665636164571966E-2;
  591. $d6 = 5.47593808499534494600E-4;
  592. $d7 = 1.05075007164441684324E-9;
  593. // coefficients for p near 0 or 1.
  594. $e0 = 6.65790464350110377720;
  595. $e1 = 5.46378491116411436990;
  596. $e2 = 1.78482653991729133580;
  597. $e3 = 2.96560571828504891230E-1;
  598. $e4 = 2.65321895265761230930E-2;
  599. $e5 = 1.24266094738807843860E-3;
  600. $e6 = 2.71155556874348757815E-5;
  601. $e7 = 2.01033439929228813265E-7;
  602. $f1 = 5.99832206555887937690E-1;
  603. $f2 = 1.36929880922735805310E-1;
  604. $f3 = 1.48753612908506148525E-2;
  605. $f4 = 7.86869131145613259100E-4;
  606. $f5 = 1.84631831751005468180E-5;
  607. $f6 = 1.42151175831644588870E-7;
  608. $f7 = 2.04426310338993978564E-15;
  609. $q = $p - 0.5;
  610. // computation for p close to 0.5
  611. if (abs($q) <= split1)
  612. {
  613. $R = $const1 - $q * $q;
  614. $z = $q * ((((((($a7 * $R + $a6) * $R + $a5) * $R + $a4) * $R + $a3) * $R + $a2) * $R + $a1) * $R + $a0) / ((((((($b7 * $R + $b6) * $R + $b5) * $R + $b4) * $R + $b3) * $R + $b2) * $R + $b1) * $R + 1);
  615. }
  616. else
  617. {
  618. if ($q < 0)
  619. {
  620. $R = $p;
  621. }
  622. else
  623. {
  624. $R = 1 - $p;
  625. }
  626. $R = pow(- log($R), 2);
  627. // computation for p not close to 0, 0.5 or 1.
  628. If ($R <= $split2)
  629. {
  630. $R = $R - $const2;
  631. $z = ((((((($c7 * $R + $c6) * $R + $c5) * $R + $c4) * $R + $c3) * $R + $c2) * $R + $c1) * $R + $c0) / ((((((($d7 * $R + $d6) * $R + $d5) * $R + $d4) * $R + $d3) * $R + $d2) * $R + $d1) * $R + 1);
  632. }
  633. else
  634. {
  635. // computation for p near 0 or 1.
  636. $R = $R - $split2;
  637. $z = ((((((($e7 * $R + $e6) * $R + $e5) * $R + $e4) * $R + $e3) * $R + $e2) * $R + $e1) * $R + $e0) / ((((((($f7 * $R + $f6) * $R + $f5) * $R + $f4) * $R + $f3) * $R + $f2) * $R + $f1) * $R + 1);
  638. }
  639. if ($q < 0)
  640. {
  641. $z = - $z;
  642. }
  643. }
  644. return $z;
  645. } // function _inverse_ncdf3()
  646. /**
  647. * AVEDEV
  648. *
  649. * Returns the average of the absolute deviations of data points from their mean.
  650. * AVEDEV is a measure of the variability in a data set.
  651. *
  652. * Excel Function:
  653. * AVEDEV(value1[,value2[, ...]])
  654. *
  655. * @access public
  656. * @category Statistical Functions
  657. * @param mixed $arg,... Data values
  658. * @return float
  659. */
  660. public static function AVEDEV()
  661. {
  662. $aArgs = PHPExcel_Calculation_Functions :: flattenArrayIndexed(func_get_args());
  663. // Return value
  664. $returnValue = null;
  665. $aMean = self :: AVERAGE($aArgs);
  666. if ($aMean != PHPExcel_Calculation_Functions :: DIV0())
  667. {
  668. $aCount = 0;
  669. foreach ($aArgs as $k => $arg)
  670. {
  671. if ((is_bool($arg)) && ((! PHPExcel_Calculation_Functions :: isCellValue($k)) || (PHPExcel_Calculation_Functions :: getCompatibilityMode() == PHPExcel_Calculation_Functions :: COMPATIBILITY_OPENOFFICE)))
  672. {
  673. $arg = (integer) $arg;
  674. }
  675. // Is it a numeric value?
  676. if ((is_numeric($arg)) && (! is_string($arg)))
  677. {
  678. if (is_null($returnValue))
  679. {
  680. $returnValue = abs($arg - $aMean);
  681. }
  682. else
  683. {
  684. $returnValue += abs($arg - $aMean);
  685. }
  686. ++ $aCount;
  687. }
  688. }
  689. // Return
  690. if ($aCount == 0)
  691. {
  692. return PHPExcel_Calculation_Functions :: DIV0();
  693. }
  694. return $returnValue / $aCount;
  695. }
  696. return PHPExcel_Calculation_Functions :: NaN();
  697. } // function AVEDEV()
  698. /**
  699. * AVERAGE
  700. *
  701. * Returns the average (arithmetic mean) of the arguments
  702. *
  703. * Excel Function:
  704. * AVERAGE(value1[,value2[, ...]])
  705. *
  706. * @access public
  707. * @category Statistical Functions
  708. * @param mixed $arg,... Data values
  709. * @return float
  710. */
  711. public static function AVERAGE()
  712. {
  713. $returnValue = $aCount = 0;
  714. // Loop through arguments
  715. foreach (PHPExcel_Calculation_Functions :: flattenArrayIndexed(func_get_args()) as $k => $arg)
  716. {
  717. if ((is_bool($arg)) && ((! PHPExcel_Calculation_Functions :: isCellValue($k)) || (PHPExcel_Calculation_Functions :: getCompatibilityMode() == PHPExcel_Calculation_Functions :: COMPATIBILITY_OPENOFFICE)))
  718. {
  719. $arg = (integer) $arg;
  720. }
  721. // Is it a numeric value?
  722. if ((is_numeric($arg)) && (! is_string($arg)))
  723. {
  724. if (is_null($returnValue))
  725. {
  726. $returnValue = $arg;
  727. }
  728. else
  729. {
  730. $returnValue += $arg;
  731. }
  732. ++ $aCount;
  733. }
  734. }
  735. // Return
  736. if ($aCount > 0)
  737. {
  738. return $returnValue / $aCount;
  739. }
  740. else
  741. {
  742. return PHPExcel_Calculation_Functions :: DIV0();
  743. }
  744. } // function AVERAGE()
  745. /**
  746. * AVERAGEA
  747. *
  748. * Returns the average of its arguments, including numbers, text, and logical values
  749. *
  750. * Excel Function:
  751. * AVERAGEA(value1[,value2[, ...]])
  752. *
  753. * @access public
  754. * @category Statistical Functions
  755. * @param mixed $arg,... Data values
  756. * @return float
  757. */
  758. public static function AVERAGEA()
  759. {
  760. // Return value
  761. $returnValue = null;
  762. $aCount = 0;
  763. // Loop through arguments
  764. foreach (PHPExcel_Calculation_Functions :: flattenArrayIndexed(func_get_args()) as $k => $arg)
  765. {
  766. if ((is_bool($arg)) && (! PHPExcel_Calculation_Functions :: isMatrixValue($k)))
  767. {
  768. }
  769. else
  770. {
  771. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != ''))))
  772. {
  773. if (is_bool($arg))
  774. {
  775. $arg = (integer) $arg;
  776. }
  777. elseif (is_string($arg))
  778. {
  779. $arg = 0;
  780. }
  781. if (is_null($returnValue))
  782. {
  783. $returnValue = $arg;
  784. }
  785. else
  786. {
  787. $returnValue += $arg;
  788. }
  789. ++ $aCount;
  790. }
  791. }
  792. }
  793. // Return
  794. if ($aCount > 0)
  795. {
  796. return $returnValue / $aCount;
  797. }
  798. else
  799. {
  800. return PHPExcel_Calculation_Functions :: DIV0();
  801. }
  802. } // function AVERAGEA()
  803. /**
  804. * AVERAGEIF
  805. *
  806. * Returns the average value from a range of cells that contain numbers within the list of arguments
  807. *
  808. * Excel Function:
  809. * AVERAGEIF(value1[,value2[, ...]],condition)
  810. *
  811. * @access public
  812. * @category Mathematical and Trigonometric Functions
  813. * @param mixed $arg,... Data values
  814. * @param string $condition The criteria that defines which cells will be checked.
  815. * @return float
  816. */
  817. public static function AVERAGEIF($aArgs, $condition, $averageArgs = array())
  818. {
  819. // Return value
  820. $returnValue = 0;
  821. $aArgs = PHPExcel_Calculation_Functions :: flattenArray($aArgs);
  822. $averageArgs = PHPExcel_Calculation_Functions :: flattenArray($averageArgs);
  823. if (count($averageArgs) == 0)
  824. {
  825. $averageArgs = $aArgs;
  826. }
  827. $condition = PHPExcel_Calculation_Functions :: _ifCondition($condition);
  828. // Loop through arguments
  829. $aCount = 0;
  830. foreach ($aArgs as $key => $arg)
  831. {
  832. if (! is_numeric($arg))
  833. {
  834. $arg = PHPExcel_Calculation :: _wrapResult(strtoupper($arg));
  835. }
  836. $testCondition = '=' . $arg . $condition;
  837. if (PHPExcel_Calculation :: getInstance()->_calculateFormulaValue($testCondition))
  838. {
  839. if ((is_null($returnValue)) || ($arg > $returnValue))
  840. {
  841. $returnValue += $arg;
  842. ++ $aCount;
  843. }
  844. }
  845. }
  846. // Return
  847. if ($aCount > 0)
  848. {
  849. return $returnValue / $aCount;
  850. }
  851. else
  852. {
  853. return PHPExcel_Calculation_Functions :: DIV0();
  854. }
  855. } // function AVERAGEIF()
  856. /**
  857. * BETADIST
  858. *
  859. * Returns the beta distribution.
  860. *
  861. * @param float $value Value at which you want to evaluate the distribution
  862. * @param float $alpha Parameter to the distribution
  863. * @param float $beta Parameter to the distribution
  864. * @param boolean $cumulative
  865. * @return float
  866. *
  867. */
  868. public static function BETADIST($value, $alpha, $beta, $rMin = 0, $rMax = 1)
  869. {
  870. $value = PHPExcel_Calculation_Functions :: flattenSingleValue($value);
  871. $alpha = PHPExcel_Calculation_Functions :: flattenSingleValue($alpha);
  872. $beta = PHPExcel_Calculation_Functions :: flattenSingleValue($beta);
  873. $rMin = PHPExcel_Calculation_Functions :: flattenSingleValue($rMin);
  874. $rMax = PHPExcel_Calculation_Functions :: flattenSingleValue($rMax);
  875. if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax)))
  876. {
  877. if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax))
  878. {
  879. return PHPExcel_Calculation_Functions :: NaN();
  880. }
  881. if ($rMin > $rMax)
  882. {
  883. $tmp = $rMin;
  884. $rMin = $rMax;
  885. $rMax = $tmp;
  886. }
  887. $value -= $rMin;
  888. $value /= ($rMax - $rMin);
  889. return self :: _incompleteBeta($value, $alpha, $beta);
  890. }
  891. return PHPExcel_Calculation_Functions :: VALUE();
  892. } // function BETADIST()
  893. /**
  894. * BETAINV
  895. *
  896. * Returns the inverse of the beta distribution.
  897. *
  898. * @param float $probability Probability at which you want to evaluate the distribution
  899. * @param float $alpha Parameter to the distribution
  900. * @param float $beta Parameter to the distribution
  901. * @param boolean $cumulative
  902. * @return float
  903. *
  904. */
  905. public static function BETAINV($probability, $alpha, $beta, $rMin = 0, $rMax = 1)
  906. {
  907. $probability = PHPExcel_Calculation_Functions :: flattenSingleValue($probability);
  908. $alpha = PHPExcel_Calculation_Functions :: flattenSingleValue($alpha);
  909. $beta = PHPExcel_Calculation_Functions :: flattenSingleValue($beta);
  910. $rMin = PHPExcel_Calculation_Functions :: flattenSingleValue($rMin);
  911. $rMax = PHPExcel_Calculation_Functions :: flattenSingleValue($rMax);
  912. if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax)))
  913. {
  914. if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0) || ($probability > 1))
  915. {
  916. return PHPExcel_Calculation_Functions :: NaN();
  917. }
  918. if ($rMin > $rMax)
  919. {
  920. $tmp = $rMin;
  921. $rMin = $rMax;
  922. $rMax = $tmp;
  923. }
  924. $a = 0;
  925. $b = 2;
  926. $i = 0;
  927. while ((($b - $a) > PRECISION) && ($i ++ < MAX_ITERATIONS))
  928. {
  929. $guess = ($a + $b) / 2;
  930. $result = self :: BETADIST($guess, $alpha, $beta);
  931. if (($result == $probability) || ($result == 0))
  932. {
  933. $b = $a;
  934. }
  935. elseif ($result > $probability)
  936. {
  937. $b = $guess;
  938. }
  939. else
  940. {
  941. $a = $guess;
  942. }
  943. }
  944. if ($i == MAX_ITERATIONS)
  945. {
  946. return PHPExcel_Calculation_Functions :: NA();
  947. }
  948. return round($rMin + $guess * ($rMax - $rMin), 12);
  949. }
  950. return PHPExcel_Calculation_Functions :: VALUE();
  951. } // function BETAINV()
  952. /**
  953. * BINOMDIST
  954. *
  955. * Returns the individual term binomial distribution probability. Use BINOMDIST in problems with
  956. * a fixed number of tests or trials, when the outcomes of any trial are only success or failure,
  957. * when trials are independent, and when the probability of success is constant throughout the
  958. * experiment. For example, BINOMDIST can calculate the probability that two of the next three
  959. * babies born are male.
  960. *
  961. * @param float $value Number of successes in trials
  962. * @param float $trials Number of trials
  963. * @param float $probability Probability of success on each trial
  964. * @param boolean $cumulative
  965. * @return float
  966. *
  967. * @todo Cumulative distribution function
  968. *
  969. */
  970. public static function BINOMDIST($value, $trials, $probability, $cumulative)
  971. {
  972. $value = floor(PHPExcel_Calculation_Functions :: flattenSingleValue($value));
  973. $trials = floor(PHPExcel_Calculation_Functions :: flattenSingleValue($trials));
  974. $probability = PHPExcel_Calculation_Functions :: flattenSingleValue($probability);
  975. if ((is_numeric($value)) && (is_numeric($trials)) && (is_numeric($probability)))
  976. {
  977. if (($value < 0) || ($value > $trials))
  978. {
  979. return PHPExcel_Calculation_Functions :: NaN();
  980. }
  981. if (($probability < 0) || ($probability > 1))
  982. {
  983. return PHPExcel_Calculation_Functions :: NaN();
  984. }
  985. if ((is_numeric($cumulative)) || (is_bool($cumulative)))
  986. {
  987. if ($cumulative)
  988. {
  989. $summer = 0;
  990. for($i = 0; $i <= $value; ++ $i)
  991. {
  992. $summer += PHPExcel_Calculation_MathTrig :: COMBIN($trials, $i) * pow($probability, $i) * pow(1 - $probability, $trials - $i);
  993. }
  994. return $summer;
  995. }
  996. else
  997. {
  998. return PHPExcel_Calculation_MathTrig :: COMBIN($trials, $value) * pow($probability, $value) * pow(1 - $probability, $trials - $value);
  999. }
  1000. }
  1001. }
  1002. return PHPExcel_Calculation_Functions :: VALUE();
  1003. } // function BINOMDIST()
  1004. /**
  1005. * CHIDIST
  1006. *
  1007. * Returns the one-tailed probability of the chi-squared distribution.
  1008. *
  1009. * @param float $value Value for the function
  1010. * @param float $degrees degrees of freedom
  1011. * @return float
  1012. */
  1013. public static function CHIDIST($value, $degrees)
  1014. {
  1015. $value = PHPExcel_Calculation_Functions :: flattenSingleValue($value);
  1016. $degrees = floor(PHPExcel_Calculation_Functions :: flattenSingleValue($degrees));
  1017. if ((is_numeric($value)) && (is_numeric($degrees)))
  1018. {
  1019. if ($degrees < 1)
  1020. {
  1021. return PHPExcel_Calculation_Functions :: NaN();
  1022. }
  1023. if ($value < 0)
  1024. {
  1025. if (PHPExcel_Calculation_Functions :: getCompatibilityMode() == PHPExcel_Calculation_Functions :: COMPATIBILITY_GNUMERIC)
  1026. {
  1027. return 1;
  1028. }
  1029. return PHPExcel_Calculation_Functions :: NaN();
  1030. }
  1031. return 1 - (self :: _incompleteGamma($degrees / 2, $value / 2) / self :: _gamma($degrees / 2));
  1032. }
  1033. return PHPExcel_Calculation_Functions :: VALUE();
  1034. } // function CHIDIST()
  1035. /**
  1036. * CHIINV
  1037. *
  1038. * Returns the one-tailed probability of the chi-squared distribution.
  1039. *
  1040. * @param float $probability Probability for the function
  1041. * @param float $degrees degrees of freedom
  1042. * @return float
  1043. */
  1044. public static function CHIINV($probability, $degrees)
  1045. {
  1046. $probability = PHPExcel_Calculation_Functions :: flattenSingleValue($probability);
  1047. $degrees = floor(PHPExcel_Calculation_Functions :: flattenSingleValue($degrees));
  1048. if ((is_numeric($probability)) && (is_numeric($degrees)))
  1049. {
  1050. $xLo = 100;
  1051. $xHi = 0;
  1052. $x = $xNew = 1;
  1053. $dx = 1;
  1054. $i = 0;
  1055. while ((abs($dx) > PRECISION) && ($i ++ < MAX_ITERATIONS))
  1056. {
  1057. // Apply Newton-Raphson step
  1058. $result = self :: CHIDIST($x, $degrees);
  1059. $error = $result - $probability;
  1060. if ($error == 0.0)
  1061. {
  1062. $dx = 0;
  1063. }
  1064. elseif ($error < 0.0)
  1065. {
  1066. $xLo = $x;
  1067. }
  1068. else
  1069. {
  1070. $xHi = $x;
  1071. }
  1072. // Avoid division by zero
  1073. if ($result != 0.0)
  1074. {
  1075. $dx = $error / $result;
  1076. $xNew = $x - $dx;
  1077. }
  1078. // If the NR fails to converge (which for example may be the
  1079. // case if the initial guess is too rough) we apply a bisection
  1080. // step to determine a more narrow interval around the root.
  1081. if (($xNew < $xLo) || ($xNew > $xHi) || ($result == 0.0))
  1082. {
  1083. $xNew = ($xLo + $xHi) / 2;
  1084. $dx = $xNew - $x;
  1085. }
  1086. $x = $xNew;
  1087. }
  1088. if ($i == MAX_ITERATIONS)
  1089. {
  1090. return PHPExcel_Calculation_Functions :: NA();
  1091. }
  1092. return round($x, 12);
  1093. }
  1094. return PHPExcel_Calculation_Functions :: VALUE();
  1095. } // function CHIINV()
  1096. /**
  1097. * CONFIDENCE
  1098. *
  1099. * Returns the confidence interval for a population mean
  1100. *
  1101. * @param float $alpha
  1102. * @param float $stdDev Standard Deviation
  1103. * @param float $size
  1104. * @return float
  1105. *
  1106. */
  1107. public static function CONFIDENCE($alpha, $stdDev, $size)
  1108. {
  1109. $alpha = PHPExcel_Calculation_Functions :: flattenSingleValue($alpha);
  1110. $stdDev = PHPExcel_Calculation_Functions :: flattenSingleValue($stdDev);
  1111. $size = floor(PHPExcel_Calculation_Functions :: flattenSingleValue($size));
  1112. if ((is_numeric($alpha)) && (is_numeric($stdDev)) && (is_numeric($size)))
  1113. {
  1114. if (($alpha <= 0) || ($alpha >= 1))
  1115. {
  1116. return PHPExcel_Calculation_Functions :: NaN();
  1117. }
  1118. if (($stdDev <= 0) || ($size < 1))
  1119. {
  1120. return PHPExcel_Calculation_Functions :: NaN();
  1121. }
  1122. return self :: NORMSINV(1 - $alpha / 2) * $stdDev / sqrt($size);
  1123. }
  1124. return PHPExcel_Calculation_Functions :: VALUE();
  1125. } // function CONFIDENCE()
  1126. /**
  1127. * CORREL
  1128. *
  1129. * Returns covariance, the average of the products of deviations for each data point pair.
  1130. *
  1131. * @param array of mixed Data Series Y
  1132. * @param array of mixed Data Series X
  1133. * @return float
  1134. */
  1135. public static function CORREL($yValues, $xValues = null)
  1136. {
  1137. if ((is_null($xValues)) || (! is_array($yValues)) || (! is_array($xValues)))
  1138. {
  1139. return PHPExcel_Calculation_Functions :: VALUE();
  1140. }
  1141. if (! self :: _checkTrendArrays($yValues, $xValues))
  1142. {
  1143. return PHPExcel_Calculation_Functions :: VALUE();
  1144. }
  1145. $yValueCount = count($yValues);
  1146. $xValueCount = count($xValues);
  1147. if (($yValueCount == 0) || ($yValueCount != $xValueCount))
  1148. {
  1149. return PHPExcel_Calculation_Functions :: NA();
  1150. }
  1151. elseif ($yValueCount == 1)
  1152. {
  1153. return PHPExcel_Calculation_Functions :: DIV0();
  1154. }
  1155. $bestFitLinear = trendClass :: calculate(trendClass :: TREND_LINEAR, $yValues, $xValues);
  1156. return $bestFitLinear->getCorrelation();
  1157. } // function CORREL()
  1158. /**
  1159. * COUNT
  1160. *
  1161. * Counts the number of cells that contain numbers within the list of arguments
  1162. *
  1163. * Excel Function:
  1164. * COUNT(value1[,value2[, ...]])
  1165. *
  1166. * @access public
  1167. * @category Statistical Functions
  1168. * @param mixed $arg,... Data values
  1169. * @return int
  1170. */
  1171. public static function COUNT()
  1172. {
  1173. // Return value
  1174. $returnValue = 0;
  1175. // Loop through arguments
  1176. $aArgs = PHPExcel_Calculation_Functions :: flattenArrayIndexed(func_get_args());
  1177. foreach ($aArgs as $k => $arg)
  1178. {
  1179. if ((is_bool($arg)) && ((! PHPExcel_Calculation_Functions :: isCellValue($k)) || (PHPExcel_Calculation_Functions :: getCompatibilityMode() == PHPExcel_Calculation_Functions :: COMPATIBILITY_OPENOFFICE)))
  1180. {
  1181. $arg = (integer) $arg;
  1182. }
  1183. // Is it a numeric value?
  1184. if ((is_numeric($arg)) && (! is_string($arg)))
  1185. {
  1186. ++ $returnValue;
  1187. }
  1188. }
  1189. // Return
  1190. return $returnValue;
  1191. } // function COUNT()
  1192. /**
  1193. * COUNTA
  1194. *
  1195. * Counts the number of cells that are not empty within the list of arguments
  1196. *
  1197. * Excel Function:
  1198. * COUNTA(value1[,value2[, ...]])
  1199. *
  1200. * @access public
  1201. * @category Statistical Functions
  1202. * @param mixed $arg,... Data values
  1203. * @return int
  1204. */
  1205. public static function COUNTA()
  1206. {
  1207. // Return value
  1208. $returnValue = 0;
  1209. // Loop through arguments
  1210. $aArgs = PHPExcel_Calculation_Functions :: flattenArray(func_get_args());
  1211. foreach ($aArgs as $arg)
  1212. {
  1213. // Is it a numeric, boolean or string value?
  1214. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != ''))))
  1215. {
  1216. ++ $returnValue;
  1217. }
  1218. }
  1219. // Return
  1220. return $returnValue;
  1221. } // function COUNTA()
  1222. /**
  1223. * COUNTBLANK
  1224. *
  1225. * Counts the number of empty cells within the list of arguments
  1226. *
  1227. * Excel Function:
  1228. * COUNTBLANK(value1[,value2[, ...]])
  1229. *
  1230. * @access public
  1231. * @category Statistical Functions
  1232. * @param mixed $arg,... Data values
  1233. * @return int
  1234. */
  1235. public static function COUNTBLANK()
  1236. {
  1237. // Return value
  1238. $returnValue = 0;
  1239. // Loop through arguments
  1240. $aArgs = PHPExcel_Calculation_Functions :: flattenArray(func_get_args());
  1241. foreach ($aArgs as $arg)
  1242. {
  1243. // Is it a blank cell?
  1244. if ((is_null($arg)) || ((is_string($arg)) && ($arg == '')))
  1245. {
  1246. ++ $returnValue;
  1247. }
  1248. }
  1249. // Return
  1250. return $returnValue;
  1251. } // function COUNTBLANK()
  1252. /**
  1253. * COUNTIF
  1254. *
  1255. * Counts the number of cells that contain numbers within the list of arguments
  1256. *
  1257. * Excel Function:
  1258. * COUNTIF(value1[,value2[, ...]],condition)
  1259. *
  1260. * @access public
  1261. * @category Statistical Functions
  1262. * @param mixed $arg,... Data values
  1263. * @param string $condition The criteria that defines which cells will be counted.
  1264. * @return int
  1265. */
  1266. public static function COUNTIF($aArgs, $condition)
  1267. {
  1268. // Return value
  1269. $returnValue = 0;
  1270. $aArgs = PHPExcel_Calculation_Functions :: flattenArray($aArgs);
  1271. $condition = PHPExcel_Calculation_Functions :: _ifCondition($condition);
  1272. // Loop through arguments
  1273. foreach ($aArgs as $arg)
  1274. {
  1275. if (! is_numeric($arg))
  1276. {
  1277. $arg = PHPExcel_Calculation :: _wrapResult(strtoupper($arg));
  1278. }
  1279. $testCondition = '=' . $arg . $condition;
  1280. if (PHPExcel_Calculation :: getInstance()->_calculateFormulaValue($testCondition))
  1281. {
  1282. // Is it a value within our criteria
  1283. ++ $returnValue;
  1284. }
  1285. }
  1286. // Return
  1287. return $returnValue;
  1288. } // function COUNTIF()
  1289. /**
  1290. * COVAR
  1291. *
  1292. * Returns covariance, the average of the products of deviations for each data point pair.
  1293. *
  1294. * @param array of mixed Data Series Y
  1295. * @param array of mixed Data Series X
  1296. * @return float
  1297. */
  1298. public static function COVAR($yValues, $xValues)
  1299. {
  1300. if (! self :: _checkTrendArrays($yValues, $xValues))
  1301. {
  1302. return PHPExcel_Calculation_Functions :: VALUE();
  1303. }
  1304. $yValueCount = count($yValues);
  1305. $xValueCount = count($xValues);
  1306. if (($yValueCount == 0) || ($yValueCount != $xValueCount))
  1307. {
  1308. return PHPExcel_Calculation_Functions :: NA();
  1309. }
  1310. elseif ($yValueCount == 1)
  1311. {
  1312. return PHPExcel_Calculation_Functions :: DIV0();
  1313. }
  1314. $bestFitLinear = trendClass :: calculate(trendClass :: TREND_LINEAR, $yValues, $xValues);
  1315. re

Large files files are truncated, but you can click here to view the full file