PageRenderTime 76ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 2ms

/libs/phpexcel/PHPExcel/Calculation/Functions.php

https://github.com/vykintasv/psiprogresas
PHP | 10505 lines | 6179 code | 1177 blank | 3149 comment | 1740 complexity | d7547c97c8a3cafe206bd8cfa285027c MD5 | raw file

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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.0, 2009-08-10
  26. */
  27. /** EPS */
  28. define('EPS', 2.22e-16);
  29. /** MAX_VALUE */
  30. define('MAX_VALUE', 1.2e308);
  31. /** LOG_GAMMA_X_MAX_VALUE */
  32. define('LOG_GAMMA_X_MAX_VALUE', 2.55e305);
  33. /** SQRT2PI */
  34. define('SQRT2PI', 2.5066282746310005024157652848110452530069867406099);
  35. /** XMININ */
  36. define('XMININ', 2.23e-308);
  37. /** MAX_ITERATIONS */
  38. define('MAX_ITERATIONS', 150);
  39. /** PRECISION */
  40. define('PRECISION', 8.88E-016);
  41. /** EULER */
  42. define('EULER', 2.71828182845904523536);
  43. $savedPrecision = ini_get('precision');
  44. if ($savedPrecision < 16) {
  45. ini_set('precision',16);
  46. }
  47. /** PHPExcel root directory */
  48. if (!defined('PHPEXCEL_ROOT')) {
  49. /**
  50. * @ignore
  51. */
  52. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  53. }
  54. /** PHPExcel_Cell */
  55. require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
  56. /** PHPExcel_Cell_DataType */
  57. require_once PHPEXCEL_ROOT . 'PHPExcel/Cell/DataType.php';
  58. /** PHPExcel_Style_NumberFormat */
  59. require_once PHPEXCEL_ROOT . 'PHPExcel/Style/NumberFormat.php';
  60. /** PHPExcel_Shared_Date */
  61. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/Date.php';
  62. /** Matrix */
  63. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/Matrix.php';
  64. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/trendClass.php';
  65. /**
  66. * PHPExcel_Calculation_Functions
  67. *
  68. * @category PHPExcel
  69. * @package PHPExcel_Calculation
  70. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  71. */
  72. class PHPExcel_Calculation_Functions {
  73. /** constants */
  74. const COMPATIBILITY_EXCEL = 'Excel';
  75. const COMPATIBILITY_GNUMERIC = 'Gnumeric';
  76. const COMPATIBILITY_OPENOFFICE = 'OpenOfficeCalc';
  77. const RETURNDATE_PHP_NUMERIC = 'P';
  78. const RETURNDATE_PHP_OBJECT = 'O';
  79. const RETURNDATE_EXCEL = 'E';
  80. /**
  81. * Compatibility mode to use for error checking and responses
  82. *
  83. * @access private
  84. * @var string
  85. */
  86. private static $compatibilityMode = self::COMPATIBILITY_EXCEL;
  87. /**
  88. * Data Type to use when returning date values
  89. *
  90. * @access private
  91. * @var integer
  92. */
  93. private static $ReturnDateType = self::RETURNDATE_EXCEL;
  94. /**
  95. * List of error codes
  96. *
  97. * @access private
  98. * @var array
  99. */
  100. private static $_errorCodes = array( 'null' => '#NULL!',
  101. 'divisionbyzero' => '#DIV/0!',
  102. 'value' => '#VALUE!',
  103. 'reference' => '#REF!',
  104. 'name' => '#NAME?',
  105. 'num' => '#NUM!',
  106. 'na' => '#N/A',
  107. 'gettingdata' => '#GETTING_DATA'
  108. );
  109. /**
  110. * Set the Compatibility Mode
  111. *
  112. * @access public
  113. * @category Function Configuration
  114. * @param string $compatibilityMode Compatibility Mode
  115. * Permitted values are:
  116. * PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL 'Excel'
  117. * PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC 'Gnumeric'
  118. * PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc'
  119. * @return boolean (Success or Failure)
  120. */
  121. public static function setCompatibilityMode($compatibilityMode) {
  122. if (($compatibilityMode == self::COMPATIBILITY_EXCEL) ||
  123. ($compatibilityMode == self::COMPATIBILITY_GNUMERIC) ||
  124. ($compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
  125. self::$compatibilityMode = $compatibilityMode;
  126. return True;
  127. }
  128. return False;
  129. } // function setCompatibilityMode()
  130. /**
  131. * Return the current Compatibility Mode
  132. *
  133. * @access public
  134. * @category Function Configuration
  135. * @return string Compatibility Mode
  136. * Possible Return values are:
  137. * PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL 'Excel'
  138. * PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC 'Gnumeric'
  139. * PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc'
  140. */
  141. public static function getCompatibilityMode() {
  142. return self::$compatibilityMode;
  143. } // function getCompatibilityMode()
  144. /**
  145. * Set the Return Date Format used by functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object)
  146. *
  147. * @access public
  148. * @category Function Configuration
  149. * @param string $returnDateType Return Date Format
  150. * Permitted values are:
  151. * PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC 'P'
  152. * PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT 'O'
  153. * PHPExcel_Calculation_Functions::RETURNDATE_EXCEL 'E'
  154. * @return boolean Success or failure
  155. */
  156. public static function setReturnDateType($returnDateType) {
  157. if (($returnDateType == self::RETURNDATE_PHP_NUMERIC) ||
  158. ($returnDateType == self::RETURNDATE_PHP_OBJECT) ||
  159. ($returnDateType == self::RETURNDATE_EXCEL)) {
  160. self::$ReturnDateType = $returnDateType;
  161. return True;
  162. }
  163. return False;
  164. } // function setReturnDateType()
  165. /**
  166. * Return the current Return Date Format for functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object)
  167. *
  168. * @access public
  169. * @category Function Configuration
  170. * @return string Return Date Format
  171. * Possible Return values are:
  172. * PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC 'P'
  173. * PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT 'O'
  174. * PHPExcel_Calculation_Functions::RETURNDATE_EXCEL 'E'
  175. */
  176. public static function getReturnDateType() {
  177. return self::$ReturnDateType;
  178. } // function getReturnDateType()
  179. /**
  180. * DUMMY
  181. *
  182. * @access public
  183. * @category Error Returns
  184. * @return string #Not Yet Implemented
  185. */
  186. public static function DUMMY() {
  187. return '#Not Yet Implemented';
  188. } // function DUMMY()
  189. /**
  190. * NA
  191. *
  192. * @access public
  193. * @category Error Returns
  194. * @return string #N/A!
  195. */
  196. public static function NA() {
  197. return self::$_errorCodes['na'];
  198. } // function NA()
  199. /**
  200. * NAN
  201. *
  202. * @access public
  203. * @category Error Returns
  204. * @return string #NUM!
  205. */
  206. public static function NaN() {
  207. return self::$_errorCodes['num'];
  208. } // function NAN()
  209. /**
  210. * NAME
  211. *
  212. * @access public
  213. * @category Error Returns
  214. * @return string #NAME!
  215. */
  216. public static function NAME() {
  217. return self::$_errorCodes['name'];
  218. } // function NAME()
  219. /**
  220. * REF
  221. *
  222. * @access public
  223. * @category Error Returns
  224. * @return string #REF!
  225. */
  226. public static function REF() {
  227. return self::$_errorCodes['reference'];
  228. } // function REF()
  229. /**
  230. * LOGICAL_AND
  231. *
  232. * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.
  233. *
  234. * Excel Function:
  235. * AND(logical1[,logical2[, ...]])
  236. *
  237. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  238. * or references that contain logical values.
  239. *
  240. * Booleans arguments are treated as True or False as appropriate
  241. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  242. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds the value TRUE or FALSE,
  243. * holds the value TRUE or FALSE, in which case it is evaluated as a boolean
  244. *
  245. * @access public
  246. * @category Logical Functions
  247. * @param mixed $arg,... Data values
  248. * @return boolean The logical AND of the arguments.
  249. */
  250. public static function LOGICAL_AND() {
  251. // Return value
  252. $returnValue = True;
  253. // Loop through the arguments
  254. $aArgs = self::flattenArray(func_get_args());
  255. $argCount = 0;
  256. foreach ($aArgs as $arg) {
  257. // Is it a boolean value?
  258. if (is_bool($arg)) {
  259. $returnValue = $returnValue && $arg;
  260. ++$argCount;
  261. } elseif ((is_numeric($arg)) && (!is_string($arg))) {
  262. $returnValue = $returnValue && ($arg != 0);
  263. ++$argCount;
  264. } elseif (is_string($arg)) {
  265. $arg = strtoupper($arg);
  266. if ($arg == 'TRUE') {
  267. $arg = 1;
  268. } elseif ($arg == 'FALSE') {
  269. $arg = 0;
  270. } else {
  271. return self::$_errorCodes['value'];
  272. }
  273. $returnValue = $returnValue && ($arg != 0);
  274. ++$argCount;
  275. }
  276. }
  277. // Return
  278. if ($argCount == 0) {
  279. return self::$_errorCodes['value'];
  280. }
  281. return $returnValue;
  282. } // function LOGICAL_AND()
  283. /**
  284. * LOGICAL_OR
  285. *
  286. * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
  287. *
  288. * Excel Function:
  289. * OR(logical1[,logical2[, ...]])
  290. *
  291. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  292. * or references that contain logical values.
  293. *
  294. * Booleans arguments are treated as True or False as appropriate
  295. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  296. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string
  297. * holds the value TRUE or FALSE, in which case it is evaluated as a boolean
  298. *
  299. * @access public
  300. * @category Logical Functions
  301. * @param mixed $arg,... Data values
  302. * @return boolean The logical OR of the arguments.
  303. */
  304. public static function LOGICAL_OR() {
  305. // Return value
  306. $returnValue = False;
  307. // Loop through the arguments
  308. $aArgs = self::flattenArray(func_get_args());
  309. $argCount = 0;
  310. foreach ($aArgs as $arg) {
  311. // Is it a boolean value?
  312. if (is_bool($arg)) {
  313. $returnValue = $returnValue || $arg;
  314. ++$argCount;
  315. } elseif ((is_numeric($arg)) && (!is_string($arg))) {
  316. $returnValue = $returnValue || ($arg != 0);
  317. ++$argCount;
  318. } elseif (is_string($arg)) {
  319. $arg = strtoupper($arg);
  320. if ($arg == 'TRUE') {
  321. $arg = 1;
  322. } elseif ($arg == 'FALSE') {
  323. $arg = 0;
  324. } else {
  325. return self::$_errorCodes['value'];
  326. }
  327. $returnValue = $returnValue || ($arg != 0);
  328. ++$argCount;
  329. }
  330. }
  331. // Return
  332. if ($argCount == 0) {
  333. return self::$_errorCodes['value'];
  334. }
  335. return $returnValue;
  336. } // function LOGICAL_OR()
  337. /**
  338. * LOGICAL_FALSE
  339. *
  340. * Returns the boolean FALSE.
  341. *
  342. * Excel Function:
  343. * FALSE()
  344. *
  345. * @access public
  346. * @category Logical Functions
  347. * @return boolean False
  348. */
  349. public static function LOGICAL_FALSE() {
  350. return False;
  351. } // function LOGICAL_FALSE()
  352. /**
  353. * LOGICAL_TRUE
  354. *
  355. * Returns the boolean TRUE.
  356. *
  357. * Excel Function:
  358. * TRUE()
  359. *
  360. * @access public
  361. * @category Logical Functions
  362. * @return boolean True
  363. */
  364. public static function LOGICAL_TRUE() {
  365. return True;
  366. } // function LOGICAL_TRUE()
  367. /**
  368. * LOGICAL_NOT
  369. *
  370. * Returns the boolean inverse of the argument.
  371. *
  372. * Excel Function:
  373. * NOT(logical)
  374. *
  375. * @access public
  376. * @category Logical Functions
  377. * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE
  378. * @return boolean The boolean inverse of the argument.
  379. */
  380. public static function LOGICAL_NOT($logical) {
  381. return !$logical;
  382. } // function LOGICAL_NOT()
  383. /**
  384. * STATEMENT_IF
  385. *
  386. * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.
  387. *
  388. * Excel Function:
  389. * IF(condition[,returnIfTrue[,returnIfFalse]])
  390. *
  391. * Condition is any value or expression that can be evaluated to TRUE or FALSE.
  392. * For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,
  393. * the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.
  394. * This argument can use any comparison calculation operator.
  395. * ReturnIfTrue is the value that is returned if condition evaluates to TRUE.
  396. * For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE,
  397. * then the IF function returns the text "Within budget"
  398. * If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use
  399. * the logical value TRUE for this argument.
  400. * ReturnIfTrue can be another formula.
  401. * ReturnIfFalse is the value that is returned if condition evaluates to FALSE.
  402. * For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE,
  403. * then the IF function returns the text "Over budget".
  404. * If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned.
  405. * If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned.
  406. * ReturnIfFalse can be another formula.
  407. *
  408. * @param mixed $condition Condition to evaluate
  409. * @param mixed $returnIfTrue Value to return when condition is true
  410. * @param mixed $returnIfFalse Value to return when condition is false
  411. * @return mixed The value of returnIfTrue or returnIfFalse determined by condition
  412. */
  413. public static function STATEMENT_IF($condition = true, $returnIfTrue = 0, $returnIfFalse = False) {
  414. $condition = self::flattenSingleValue($condition);
  415. $returnIfTrue = self::flattenSingleValue($returnIfTrue);
  416. $returnIfFalse = self::flattenSingleValue($returnIfFalse);
  417. if (is_null($returnIfTrue)) { $returnIfTrue = 0; }
  418. if (is_null($returnIfFalse)) { $returnIfFalse = 0; }
  419. return ($condition ? $returnIfTrue : $returnIfFalse);
  420. } // function STATEMENT_IF()
  421. /**
  422. * STATEMENT_IFERROR
  423. *
  424. * @param mixed $value Value to check , is also value when no error
  425. * @param mixed $errorpart Value when error
  426. * @return mixed
  427. */
  428. public static function STATEMENT_IFERROR($value = '', $errorpart = '') {
  429. return self::STATEMENT_IF(self::IS_ERROR($value), $errorpart, $value);
  430. } // function STATEMENT_IFERROR()
  431. /**
  432. * ATAN2
  433. *
  434. * This function calculates the arc tangent of the two variables x and y. It is similar to
  435. * calculating the arc tangent of y ÷ x, except that the signs of both arguments are used
  436. * to determine the quadrant of the result.
  437. * The arctangent is the angle from the x-axis to a line containing the origin (0, 0) and a
  438. * point with coordinates (xCoordinate, yCoordinate). The angle is given in radians between
  439. * -pi and pi, excluding -pi.
  440. *
  441. * Note that the Excel ATAN2() function accepts its arguments in the reverse order to the standard
  442. * PHP atan2() function, so we need to reverse them here before calling the PHP atan() function.
  443. *
  444. * Excel Function:
  445. * ATAN2(xCoordinate,yCoordinate)
  446. *
  447. * @access public
  448. * @category Mathematical and Trigonometric Functions
  449. * @param float $xCoordinate The x-coordinate of the point.
  450. * @param float $yCoordinate The y-coordinate of the point.
  451. * @return float The inverse tangent of the specified x- and y-coordinates.
  452. */
  453. public static function REVERSE_ATAN2($xCoordinate, $yCoordinate) {
  454. $xCoordinate = (float) self::flattenSingleValue($xCoordinate);
  455. $yCoordinate = (float) self::flattenSingleValue($yCoordinate);
  456. if (($xCoordinate == 0) && ($yCoordinate == 0)) {
  457. return self::$_errorCodes['divisionbyzero'];
  458. }
  459. return atan2($yCoordinate, $xCoordinate);
  460. } // function REVERSE_ATAN2()
  461. /**
  462. * LOG_BASE
  463. *
  464. * Returns the logarithm of a number to a specified base. The default base is 10.
  465. *
  466. * Excel Function:
  467. * LOG(number[,base])
  468. *
  469. * @access public
  470. * @category Mathematical and Trigonometric Functions
  471. * @param float $value The positive real number for which you want the logarithm
  472. * @param float $base The base of the logarithm. If base is omitted, it is assumed to be 10.
  473. * @return float
  474. */
  475. public static function LOG_BASE($number, $base=10) {
  476. $number = self::flattenSingleValue($number);
  477. $base = self::flattenSingleValue($base);
  478. return log($number, $base);
  479. } // function LOG_BASE()
  480. /**
  481. * SUM
  482. *
  483. * SUM computes the sum of all the values and cells referenced in the argument list.
  484. *
  485. * Excel Function:
  486. * SUM(value1[,value2[, ...]])
  487. *
  488. * @access public
  489. * @category Mathematical and Trigonometric Functions
  490. * @param mixed $arg,... Data values
  491. * @return float
  492. */
  493. public static function SUM() {
  494. // Return value
  495. $returnValue = 0;
  496. // Loop through the arguments
  497. $aArgs = self::flattenArray(func_get_args());
  498. foreach ($aArgs as $arg) {
  499. // Is it a numeric value?
  500. if ((is_numeric($arg)) && (!is_string($arg))) {
  501. $returnValue += $arg;
  502. }
  503. }
  504. // Return
  505. return $returnValue;
  506. } // function SUM()
  507. /**
  508. * SUMSQ
  509. *
  510. * SUMSQ returns the sum of the squares of the arguments
  511. *
  512. * Excel Function:
  513. * SUMSQ(value1[,value2[, ...]])
  514. *
  515. * @access public
  516. * @category Mathematical and Trigonometric Functions
  517. * @param mixed $arg,... Data values
  518. * @return float
  519. */
  520. public static function SUMSQ() {
  521. // Return value
  522. $returnValue = 0;
  523. // Loop trough arguments
  524. $aArgs = self::flattenArray(func_get_args());
  525. foreach ($aArgs as $arg) {
  526. // Is it a numeric value?
  527. if ((is_numeric($arg)) && (!is_string($arg))) {
  528. $returnValue += pow($arg,2);
  529. }
  530. }
  531. // Return
  532. return $returnValue;
  533. } // function SUMSQ()
  534. /**
  535. * PRODUCT
  536. *
  537. * PRODUCT returns the product of all the values and cells referenced in the argument list.
  538. *
  539. * Excel Function:
  540. * PRODUCT(value1[,value2[, ...]])
  541. *
  542. * @access public
  543. * @category Mathematical and Trigonometric Functions
  544. * @param mixed $arg,... Data values
  545. * @return float
  546. */
  547. public static function PRODUCT() {
  548. // Return value
  549. $returnValue = null;
  550. // Loop trough arguments
  551. $aArgs = self::flattenArray(func_get_args());
  552. foreach ($aArgs as $arg) {
  553. // Is it a numeric value?
  554. if ((is_numeric($arg)) && (!is_string($arg))) {
  555. if (is_null($returnValue)) {
  556. $returnValue = $arg;
  557. } else {
  558. $returnValue *= $arg;
  559. }
  560. }
  561. }
  562. // Return
  563. if (is_null($returnValue)) {
  564. return 0;
  565. }
  566. return $returnValue;
  567. } // function PRODUCT()
  568. /**
  569. * QUOTIENT
  570. *
  571. * QUOTIENT function returns the integer portion of a division. Numerator is the divided number
  572. * and denominator is the divisor.
  573. *
  574. * Excel Function:
  575. * QUOTIENT(value1[,value2[, ...]])
  576. *
  577. * @access public
  578. * @category Mathematical and Trigonometric Functions
  579. * @param mixed $arg,... Data values
  580. * @return float
  581. */
  582. public static function QUOTIENT() {
  583. // Return value
  584. $returnValue = null;
  585. // Loop trough arguments
  586. $aArgs = self::flattenArray(func_get_args());
  587. foreach ($aArgs as $arg) {
  588. // Is it a numeric value?
  589. if ((is_numeric($arg)) && (!is_string($arg))) {
  590. if (is_null($returnValue)) {
  591. $returnValue = ($arg == 0) ? 0 : $arg;
  592. } else {
  593. if (($returnValue == 0) || ($arg == 0)) {
  594. $returnValue = 0;
  595. } else {
  596. $returnValue /= $arg;
  597. }
  598. }
  599. }
  600. }
  601. // Return
  602. return intval($returnValue);
  603. } // function QUOTIENT()
  604. /**
  605. * MIN
  606. *
  607. * MIN returns the value of the element of the values passed that has the smallest value,
  608. * with negative numbers considered smaller than positive numbers.
  609. *
  610. * Excel Function:
  611. * MIN(value1[,value2[, ...]])
  612. *
  613. * @access public
  614. * @category Statistical Functions
  615. * @param mixed $arg,... Data values
  616. * @return float
  617. */
  618. public static function MIN() {
  619. // Return value
  620. $returnValue = null;
  621. // Loop trough arguments
  622. $aArgs = self::flattenArray(func_get_args());
  623. foreach ($aArgs as $arg) {
  624. // Is it a numeric value?
  625. if ((is_numeric($arg)) && (!is_string($arg))) {
  626. if ((is_null($returnValue)) || ($arg < $returnValue)) {
  627. $returnValue = $arg;
  628. }
  629. }
  630. }
  631. // Return
  632. if(is_null($returnValue)) {
  633. return 0;
  634. }
  635. return $returnValue;
  636. } // function MIN()
  637. /**
  638. * MINA
  639. *
  640. * Returns the smallest value in a list of arguments, including numbers, text, and logical values
  641. *
  642. * Excel Function:
  643. * MINA(value1[,value2[, ...]])
  644. *
  645. * @access public
  646. * @category Statistical Functions
  647. * @param mixed $arg,... Data values
  648. * @return float
  649. */
  650. public static function MINA() {
  651. // Return value
  652. $returnValue = null;
  653. // Loop through arguments
  654. $aArgs = self::flattenArray(func_get_args());
  655. foreach ($aArgs as $arg) {
  656. // Is it a numeric value?
  657. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
  658. if (is_bool($arg)) {
  659. $arg = (integer) $arg;
  660. } elseif (is_string($arg)) {
  661. $arg = 0;
  662. }
  663. if ((is_null($returnValue)) || ($arg < $returnValue)) {
  664. $returnValue = $arg;
  665. }
  666. }
  667. }
  668. // Return
  669. if(is_null($returnValue)) {
  670. return 0;
  671. }
  672. return $returnValue;
  673. } // function MINA()
  674. /**
  675. * SMALL
  676. *
  677. * Returns the nth smallest value in a data set. You can use this function to
  678. * select a value based on its relative standing.
  679. *
  680. * Excel Function:
  681. * SMALL(value1[,value2[, ...]],entry)
  682. *
  683. * @access public
  684. * @category Statistical Functions
  685. * @param mixed $arg,... Data values
  686. * @param int $entry Position (ordered from the smallest) in the array or range of data to return
  687. * @return float
  688. */
  689. public static function SMALL() {
  690. $aArgs = self::flattenArray(func_get_args());
  691. // Calculate
  692. $n = array_pop($aArgs);
  693. if ((is_numeric($n)) && (!is_string($n))) {
  694. $mArgs = array();
  695. foreach ($aArgs as $arg) {
  696. // Is it a numeric value?
  697. if ((is_numeric($arg)) && (!is_string($arg))) {
  698. $mArgs[] = $arg;
  699. }
  700. }
  701. $count = self::COUNT($mArgs);
  702. $n = floor(--$n);
  703. if (($n < 0) || ($n >= $count) || ($count == 0)) {
  704. return self::$_errorCodes['num'];
  705. }
  706. sort($mArgs);
  707. return $mArgs[$n];
  708. }
  709. return self::$_errorCodes['value'];
  710. } // function SMALL()
  711. /**
  712. * MAX
  713. *
  714. * MAX returns the value of the element of the values passed that has the highest value,
  715. * with negative numbers considered smaller than positive numbers.
  716. *
  717. * Excel Function:
  718. * MAX(value1[,value2[, ...]])
  719. *
  720. * @access public
  721. * @category Statistical Functions
  722. * @param mixed $arg,... Data values
  723. * @return float
  724. */
  725. public static function MAX() {
  726. // Return value
  727. $returnValue = null;
  728. // Loop trough arguments
  729. $aArgs = self::flattenArray(func_get_args());
  730. foreach ($aArgs as $arg) {
  731. // Is it a numeric value?
  732. if ((is_numeric($arg)) && (!is_string($arg))) {
  733. if ((is_null($returnValue)) || ($arg > $returnValue)) {
  734. $returnValue = $arg;
  735. }
  736. }
  737. }
  738. // Return
  739. if(is_null($returnValue)) {
  740. return 0;
  741. }
  742. return $returnValue;
  743. } // function MAX()
  744. /**
  745. * MAXA
  746. *
  747. * Returns the greatest value in a list of arguments, including numbers, text, and logical values
  748. *
  749. * Excel Function:
  750. * MAXA(value1[,value2[, ...]])
  751. *
  752. * @access public
  753. * @category Statistical Functions
  754. * @param mixed $arg,... Data values
  755. * @return float
  756. */
  757. public static function MAXA() {
  758. // Return value
  759. $returnValue = null;
  760. // Loop through arguments
  761. $aArgs = self::flattenArray(func_get_args());
  762. foreach ($aArgs as $arg) {
  763. // Is it a numeric value?
  764. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
  765. if (is_bool($arg)) {
  766. $arg = (integer) $arg;
  767. } elseif (is_string($arg)) {
  768. $arg = 0;
  769. }
  770. if ((is_null($returnValue)) || ($arg > $returnValue)) {
  771. $returnValue = $arg;
  772. }
  773. }
  774. }
  775. // Return
  776. if(is_null($returnValue)) {
  777. return 0;
  778. }
  779. return $returnValue;
  780. } // function MAXA()
  781. /**
  782. * LARGE
  783. *
  784. * Returns the nth largest value in a data set. You can use this function to
  785. * select a value based on its relative standing.
  786. *
  787. * Excel Function:
  788. * LARGE(value1[,value2[, ...]],entry)
  789. *
  790. * @access public
  791. * @category Statistical Functions
  792. * @param mixed $arg,... Data values
  793. * @param int $entry Position (ordered from the largest) in the array or range of data to return
  794. * @return float
  795. *
  796. */
  797. public static function LARGE() {
  798. $aArgs = self::flattenArray(func_get_args());
  799. // Calculate
  800. $n = floor(array_pop($aArgs));
  801. if ((is_numeric($n)) && (!is_string($n))) {
  802. $mArgs = array();
  803. foreach ($aArgs as $arg) {
  804. // Is it a numeric value?
  805. if ((is_numeric($arg)) && (!is_string($arg))) {
  806. $mArgs[] = $arg;
  807. }
  808. }
  809. $count = self::COUNT($mArgs);
  810. $n = floor(--$n);
  811. if (($n < 0) || ($n >= $count) || ($count == 0)) {
  812. return self::$_errorCodes['num'];
  813. }
  814. rsort($mArgs);
  815. return $mArgs[$n];
  816. }
  817. return self::$_errorCodes['value'];
  818. } // function LARGE()
  819. /**
  820. * PERCENTILE
  821. *
  822. * Returns the nth percentile of values in a range..
  823. *
  824. * Excel Function:
  825. * PERCENTILE(value1[,value2[, ...]],entry)
  826. *
  827. * @access public
  828. * @category Statistical Functions
  829. * @param mixed $arg,... Data values
  830. * @param float $entry Percentile value in the range 0..1, inclusive.
  831. * @return float
  832. */
  833. public static function PERCENTILE() {
  834. $aArgs = self::flattenArray(func_get_args());
  835. // Calculate
  836. $entry = array_pop($aArgs);
  837. if ((is_numeric($entry)) && (!is_string($entry))) {
  838. if (($entry < 0) || ($entry > 1)) {
  839. return self::$_errorCodes['num'];
  840. }
  841. $mArgs = array();
  842. foreach ($aArgs as $arg) {
  843. // Is it a numeric value?
  844. if ((is_numeric($arg)) && (!is_string($arg))) {
  845. $mArgs[] = $arg;
  846. }
  847. }
  848. $mValueCount = count($mArgs);
  849. if ($mValueCount > 0) {
  850. sort($mArgs);
  851. $count = self::COUNT($mArgs);
  852. $index = $entry * ($count-1);
  853. $iBase = floor($index);
  854. if ($index == $iBase) {
  855. return $mArgs[$index];
  856. } else {
  857. $iNext = $iBase + 1;
  858. $iProportion = $index - $iBase;
  859. return $mArgs[$iBase] + (($mArgs[$iNext] - $mArgs[$iBase]) * $iProportion) ;
  860. }
  861. }
  862. }
  863. return self::$_errorCodes['value'];
  864. } // function PERCENTILE()
  865. /**
  866. * QUARTILE
  867. *
  868. * Returns the quartile of a data set.
  869. *
  870. * Excel Function:
  871. * QUARTILE(value1[,value2[, ...]],entry)
  872. *
  873. * @access public
  874. * @category Statistical Functions
  875. * @param mixed $arg,... Data values
  876. * @param int $entry Quartile value in the range 1..3, inclusive.
  877. * @return float
  878. */
  879. public static function QUARTILE() {
  880. $aArgs = self::flattenArray(func_get_args());
  881. // Calculate
  882. $entry = floor(array_pop($aArgs));
  883. if ((is_numeric($entry)) && (!is_string($entry))) {
  884. $entry /= 4;
  885. if (($entry < 0) || ($entry > 1)) {
  886. return self::$_errorCodes['num'];
  887. }
  888. return self::PERCENTILE($aArgs,$entry);
  889. }
  890. return self::$_errorCodes['value'];
  891. } // function QUARTILE()
  892. /**
  893. * COUNT
  894. *
  895. * Counts the number of cells that contain numbers within the list of arguments
  896. *
  897. * Excel Function:
  898. * COUNT(value1[,value2[, ...]])
  899. *
  900. * @access public
  901. * @category Statistical Functions
  902. * @param mixed $arg,... Data values
  903. * @return int
  904. */
  905. public static function COUNT() {
  906. // Return value
  907. $returnValue = 0;
  908. // Loop trough arguments
  909. $aArgs = self::flattenArray(func_get_args());
  910. foreach ($aArgs as $arg) {
  911. if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
  912. $arg = (int) $arg;
  913. }
  914. // Is it a numeric value?
  915. if ((is_numeric($arg)) && (!is_string($arg))) {
  916. ++$returnValue;
  917. }
  918. }
  919. // Return
  920. return $returnValue;
  921. } // function COUNT()
  922. /**
  923. * COUNTBLANK
  924. *
  925. * Counts the number of empty cells within the list of arguments
  926. *
  927. * Excel Function:
  928. * COUNTBLANK(value1[,value2[, ...]])
  929. *
  930. * @access public
  931. * @category Statistical Functions
  932. * @param mixed $arg,... Data values
  933. * @return int
  934. */
  935. public static function COUNTBLANK() {
  936. // Return value
  937. $returnValue = 0;
  938. // Loop trough arguments
  939. $aArgs = self::flattenArray(func_get_args());
  940. foreach ($aArgs as $arg) {
  941. // Is it a blank cell?
  942. if ((is_null($arg)) || ((is_string($arg)) && ($arg == ''))) {
  943. ++$returnValue;
  944. }
  945. }
  946. // Return
  947. return $returnValue;
  948. } // function COUNTBLANK()
  949. /**
  950. * COUNTA
  951. *
  952. * Counts the number of cells that are not empty within the list of arguments
  953. *
  954. * Excel Function:
  955. * COUNTA(value1[,value2[, ...]])
  956. *
  957. * @access public
  958. * @category Statistical Functions
  959. * @param mixed $arg,... Data values
  960. * @return int
  961. */
  962. public static function COUNTA() {
  963. // Return value
  964. $returnValue = 0;
  965. // Loop through arguments
  966. $aArgs = self::flattenArray(func_get_args());
  967. foreach ($aArgs as $arg) {
  968. // Is it a numeric, boolean or string value?
  969. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
  970. ++$returnValue;
  971. }
  972. }
  973. // Return
  974. return $returnValue;
  975. } // function COUNTA()
  976. /**
  977. * COUNTIF
  978. *
  979. * Counts the number of cells that contain numbers within the list of arguments
  980. *
  981. * Excel Function:
  982. * COUNTIF(value1[,value2[, ...]],condition)
  983. *
  984. * @access public
  985. * @category Statistical Functions
  986. * @param mixed $arg,... Data values
  987. * @param string $condition The criteria that defines which cells will be counted.
  988. * @return int
  989. */
  990. public static function COUNTIF($aArgs,$condition) {
  991. // Return value
  992. $returnValue = 0;
  993. $aArgs = self::flattenArray($aArgs);
  994. if (!in_array($condition{0},array('>', '<', '='))) {
  995. if (!is_numeric($condition)) { $condition = PHPExcel_Calculation::_wrapResult(strtoupper($condition)); }
  996. $condition = '='.$condition;
  997. }
  998. // Loop through arguments
  999. foreach ($aArgs as $arg) {
  1000. if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); }
  1001. $testCondition = '='.$arg.$condition;
  1002. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1003. // Is it a value within our criteria
  1004. ++$returnValue;
  1005. }
  1006. }
  1007. // Return
  1008. return $returnValue;
  1009. } // function COUNTIF()
  1010. /**
  1011. * SUMIF
  1012. *
  1013. * Counts the number of cells that contain numbers within the list of arguments
  1014. *
  1015. * Excel Function:
  1016. * SUMIF(value1[,value2[, ...]],condition)
  1017. *
  1018. * @access public
  1019. * @category Mathematical and Trigonometric Functions
  1020. * @param mixed $arg,... Data values
  1021. * @param string $condition The criteria that defines which cells will be summed.
  1022. * @return float
  1023. */
  1024. public static function SUMIF($aArgs,$condition,$sumArgs = array()) {
  1025. // Return value
  1026. $returnValue = 0;
  1027. $aArgs = self::flattenArray($aArgs);
  1028. $sumArgs = self::flattenArray($sumArgs);
  1029. if (count($sumArgs) == 0) {
  1030. $sumArgs = $aArgs;
  1031. }
  1032. if (!in_array($condition{0},array('>', '<', '='))) {
  1033. if (!is_numeric($condition)) { $condition = PHPExcel_Calculation::_wrapResult(strtoupper($condition)); }
  1034. $condition = '='.$condition;
  1035. }
  1036. // Loop through arguments
  1037. foreach ($aArgs as $key => $arg) {
  1038. if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); }
  1039. $testCondition = '='.$arg.$condition;
  1040. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1041. // Is it a value within our criteria
  1042. $returnValue += $sumArgs[$key];
  1043. }
  1044. }
  1045. // Return
  1046. return $returnValue;
  1047. } // function SUMIF()
  1048. /**
  1049. * AVERAGE
  1050. *
  1051. * Returns the average (arithmetic mean) of the arguments
  1052. *
  1053. * Excel Function:
  1054. * AVERAGE(value1[,value2[, ...]])
  1055. *
  1056. * @access public
  1057. * @category Statistical Functions
  1058. * @param mixed $arg,... Data values
  1059. * @return float
  1060. */
  1061. public static function AVERAGE() {
  1062. // Return value
  1063. $returnValue = 0;
  1064. // Loop through arguments
  1065. $aArgs = self::flattenArray(func_get_args());
  1066. $aCount = 0;
  1067. foreach ($aArgs as $arg) {
  1068. if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
  1069. $arg = (integer) $arg;
  1070. }
  1071. // Is it a numeric value?
  1072. if ((is_numeric($arg)) && (!is_string($arg))) {
  1073. if (is_null($returnValue)) {
  1074. $returnValue = $arg;
  1075. } else {
  1076. $returnValue += $arg;
  1077. }
  1078. ++$aCount;
  1079. }
  1080. }
  1081. // Return
  1082. if ($aCount > 0) {
  1083. return $returnValue / $aCount;
  1084. } else {
  1085. return self::$_errorCodes['divisionbyzero'];
  1086. }
  1087. } // function AVERAGE()
  1088. /**
  1089. * AVERAGEA
  1090. *
  1091. * Returns the average of its arguments, including numbers, text, and logical values
  1092. *
  1093. * Excel Function:
  1094. * AVERAGEA(value1[,value2[, ...]])
  1095. *
  1096. * @access public
  1097. * @category Statistical Functions
  1098. * @param mixed $arg,... Data values
  1099. * @return float
  1100. */
  1101. public static function AVERAGEA() {
  1102. // Return value
  1103. $returnValue = null;
  1104. // Loop through arguments
  1105. $aArgs = self::flattenArray(func_get_args());
  1106. $aCount = 0;
  1107. foreach ($aArgs as $arg) {
  1108. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
  1109. if (is_bool($arg)) {
  1110. $arg = (integer) $arg;
  1111. } elseif (is_string($arg)) {
  1112. $arg = 0;
  1113. }
  1114. if (is_null($returnValue)) {
  1115. $returnValue = $arg;
  1116. } else {
  1117. $returnValue += $arg;
  1118. }
  1119. ++$aCount;
  1120. }
  1121. }
  1122. // Return
  1123. if ($aCount > 0) {
  1124. return $returnValue / $aCount;
  1125. } else {
  1126. return self::$_errorCodes['divisionbyzero'];
  1127. }
  1128. } // function AVERAGEA()
  1129. /**
  1130. * MEDIAN
  1131. *
  1132. * Returns the median of the given numbers. The median is the number in the middle of a set of numbers.
  1133. *
  1134. * Excel Function:
  1135. * MEDIAN(value1[,value2[, ...]])
  1136. *
  1137. * @access public
  1138. * @category Statistical Functions
  1139. * @param mixed $arg,... Data values
  1140. * @return float
  1141. */
  1142. public static function MEDIAN() {
  1143. // Return value
  1144. $returnValue = self::$_errorCodes['num'];
  1145. $mArgs = array();
  1146. // Loop through arguments
  1147. $aArgs = self::flattenArray(func_get_args());
  1148. foreach ($aArgs as $arg) {
  1149. // Is it a numeric value?
  1150. if ((is_numeric($arg)) && (!is_string($arg))) {
  1151. $mArgs[] = $arg;
  1152. }
  1153. }
  1154. $mValueCount = count($mArgs);
  1155. if ($mValueCount > 0) {
  1156. sort($mArgs,SORT_NUMERIC);
  1157. $mValueCount = $mValueCount / 2;
  1158. if ($mValueCount == floor($mValueCount)) {
  1159. $returnValue = ($mArgs[$mValueCount--] + $mArgs[$mValueCount]) / 2;
  1160. } else {
  1161. $mValueCount == floor($mValueCount);
  1162. $returnValue = $mArgs[$mValueCount];
  1163. }
  1164. }
  1165. // Return
  1166. return $returnValue;
  1167. } // function MEDIAN()
  1168. //
  1169. // Special variant of array_count_values that isn't limited to strings and integers,
  1170. // but can work with floating point numbers as values
  1171. //
  1172. private static function _modeCalc($data) {
  1173. $frequencyArray = array();
  1174. foreach($data as $datum) {
  1175. $found = False;
  1176. foreach($frequencyArray as $key => $value) {
  1177. if ((string) $value['value'] == (string) $datum) {
  1178. ++$frequencyArray[$key]['frequency'];
  1179. $found = True;
  1180. break;
  1181. }
  1182. }
  1183. if (!$found) {
  1184. $frequencyArray[] = array('value' => $datum,
  1185. 'frequency' => 1 );
  1186. }
  1187. }
  1188. foreach($frequencyArray as $key => $value) {
  1189. $frequencyList[$key] = $value['frequency'];
  1190. $valueList[$key] = $value['value'];
  1191. }
  1192. array_multisort($frequencyList, SORT_DESC, $valueList, SORT_ASC, SORT_NUMERIC, $frequencyArray);
  1193. if ($frequencyArray[0]['frequency'] == 1) {
  1194. return self::NA();
  1195. }
  1196. return $frequencyArray[0]['value'];
  1197. } // function _modeCalc()
  1198. /**
  1199. * MODE
  1200. *
  1201. * Returns the most frequently occurring, or repetitive, value in an array or range of data
  1202. *
  1203. * Excel Function:
  1204. * MODE(value1[,value2[, ...]])
  1205. *
  1206. * @access public
  1207. * @category Statistical Functions
  1208. * @param mixed $arg,... Data values
  1209. * @return float
  1210. */
  1211. public static function MODE() {
  1212. // Return value
  1213. $returnValue = self::NA();
  1214. // Loop through arguments
  1215. $aArgs = self::flattenArray(func_get_args());
  1216. $mArgs = array();
  1217. foreach ($aArgs as $arg) {
  1218. // Is it a numeric value?
  1219. if ((is_numeric($arg)) && (!is_string($arg))) {
  1220. $mArgs[] = $arg;
  1221. }
  1222. }
  1223. if (count($mArgs) > 0) {
  1224. return self::_modeCalc($mArgs);
  1225. }
  1226. // Return
  1227. return $returnValue;
  1228. } // function MODE()
  1229. /**
  1230. * DEVSQ
  1231. *
  1232. * Returns the sum of squares of deviations of data points from their sample mean.
  1233. *
  1234. * Excel Function:
  1235. * DEVSQ(value1[,value2[, ...]])
  1236. *
  1237. * @access public
  1238. * @category Statistical Functions
  1239. * @param mixed $arg,... Data values
  1240. * @return float
  1241. */
  1242. public static function DEVSQ() {
  1243. // Return value
  1244. $returnValue = null;
  1245. $aMean = self::AVERAGE(func_get_args());
  1246. if (!is_null($aMean)) {
  1247. $aArgs = self::flattenArray(func_get_args());
  1248. $aCount = -1;
  1249. foreach ($aArgs as $arg) {
  1250. // Is it a numeric value?
  1251. if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
  1252. $arg = (int) $arg;
  1253. }
  1254. if ((is_numeric($arg)) && (!is_string($arg))) {
  1255. if (is_null($returnValue)) {
  1256. $returnValue = pow(($arg - $aMean),2);
  1257. } else {
  1258. $returnValue += pow(($arg - $aMean),2);
  1259. }
  1260. ++$aCount;
  1261. }
  1262. }
  1263. // Return
  1264. if (is_null($returnValue)) {
  1265. return self::$_errorCodes['num'];
  1266. } else {
  1267. return $returnValue;
  1268. }
  1269. }
  1270. return self::NA();
  1271. } // function DEVSQ()
  1272. /**
  1273. * AVEDEV
  1274. *
  1275. * Returns the average of the absolute deviations of data points from their mean.
  1276. * AVEDEV is a measure of the variability in a data set.
  1277. *
  1278. * Excel Function:
  1279. * AVEDEV(value1[,value2[, ...]])
  1280. *
  1281. * @access public
  1282. * @category Statistical Functions
  1283. * @param mixed $arg,... Data values
  1284. * @return float
  1285. */
  1286. public static function AVEDEV() {
  1287. $aArgs = self::flattenArray(func_get_args());
  1288. // Return value
  1289. $returnValue = null;
  1290. $aMean = self::AVERAGE($aArgs);
  1291. if ($aMean != self::$_errorCodes['divisionbyzero']) {
  1292. $aCount = 0;
  1293. foreach ($aArgs as $arg) {
  1294. if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
  1295. $arg = (integer) $arg;
  1296. }
  1297. // Is it a numeric value?
  1298. if ((is_numeric($arg)) && (!is_string($arg))) {
  1299. if (is_null($returnValue)) {
  1300. $returnValue = abs($arg - $aMean);
  1301. } else {
  1302. $returnValue += abs($arg - $aMean);
  1303. }
  1304. ++$aCount;
  1305. }
  1306. }
  1307. // Return
  1308. return $returnValue / $aCount ;
  1309. }
  1310. return self::$_errorCodes['num'];
  1311. } // function AVEDEV()
  1312. /**
  1313. * GEOMEAN
  1314. *
  1315. * Returns the geometric mean of an array or range of positive data. For example, you
  1316. * can use GEOMEAN to calculate average growth rate given compound interest with
  1317. * variable rates.
  1318. *
  1319. * Excel Function:
  1320. * GEOMEAN(value1[,value2[, ...]])
  1321. *
  1322. * @access public
  1323. * @category Statistical Functions
  1324. * @param mixed $arg,... Data values
  1325. * @return float
  1326. */
  1327. public static function GEOMEAN() {
  1328. $aMean = self::PRODUCT(func_get_args());
  1329. if (is_numeric($aMean) && ($aMean > 0)) {
  1330. $aArgs = self::flattenArray(func_get_args());
  1331. $aCount = self::COUNT($aArgs) ;
  1332. if (self::MIN($aArgs) > 0) {
  1333. return pow($aMean, (1 / $aCount));
  1334. }
  1335. }
  1336. return self::$_errorCodes['num'];
  1337. } // GEOMEAN()
  1338. /**
  1339. * HARMEAN
  1340. *
  1341. * Returns the harmonic mean of a data set. The harmonic mean is the reciprocal of the
  1342. * arithmetic mean of reciprocals.
  1343. *
  1344. * Excel Function:
  1345. * HARMEAN(value1[,value2[, ...]])
  1346. *
  1347. * @access public
  1348. * @category Statistical Functions
  1349. * @param mixed $arg,... Data values
  1350. * @return float
  1351. */
  1352. public static function HARMEAN() {
  1353. // Return value
  1354. $returnValue = self::NA();
  1355. // Loop through arguments
  1356. $aArgs = self::flattenArray(func_get_args());
  1357. if (self::MIN($aArgs) < 0) {
  1358. return self::$_errorCodes['num'];
  1359. }
  1360. $aCount = 0;
  1361. foreach ($aArgs as $arg) {
  1362. // Is it a numeric value?
  1363. if ((is_numeric($arg)) && (!is_string($arg))) {
  1364. if ($arg <= 0) {
  1365. return self::$_errorCodes['num'];
  1366. }
  1367. if (is_null($returnValue)) {
  1368. $returnValue = (1 / $arg);
  1369. } else {
  1370. $returnValue += (1 / $arg);
  1371. }
  1372. ++$aCount;
  1373. }
  1374. }
  1375. // Return
  1376. if ($aCount > 0) {
  1377. return 1 / ($returnValue / $aCount);
  1378. } else {
  1379. return $returnValue;
  1380. }
  1381. } // function HARMEAN()
  1382. /**
  1383. * TRIMMEAN
  1384. *
  1385. * Returns the mean of the interior of a data set. TRIMMEAN calculates the mean
  1386. * taken by excluding a percentage of data points from the top and bottom tails
  1387. * of a data set.
  1388. *
  1389. * Excel Function:
  1390. * TRIMEAN(value1[,value2[, ...]],$discard)
  1391. *
  1392. * @access public
  1393. * @category Statistical Functions
  1394. * @param mixed $arg,... Data values
  1395. * @param float $discard Percentage to discard
  1396. * @return float
  1397. */
  1398. public static function TRIMMEAN() {
  1399. $aArgs = self::flattenArray(func_get_args());
  1400. // Calculate
  1401. $percent = array_pop($aArgs);
  1402. if ((is_numeric($percent)) && (!is_string($percent))) {
  1403. if (($percent < 0) || ($percent > 1)) {
  1404. return self::$_errorCodes['num'];
  1405. }
  1406. $mArgs = array();
  1407. foreach ($aArgs as $arg) {
  1408. // Is it a numeric value?
  1409. if ((is_numeric($arg)) && (!is_string($arg))) {
  1410. $mArgs[] = $arg;
  1411. }
  1412. }
  1413. $discard = floor(self::COUNT($mArgs) * $percent / 2);
  1414. sort($mArgs);
  1415. for ($i=0; $i < $discard; ++$i) {
  1416. array_pop($mArgs);
  1417. array_shift($mArgs);
  1418. }
  1419. return self::AVERAGE($mArgs);
  1420. }
  1421. return self::$_errorCodes['value'];
  1422. } // function TRIMMEAN()
  1423. /**
  1424. * STDEV
  1425. *
  1426. * Estimates standard deviation based on a sample. The standard deviation is a measure of how
  1427. * widely values are dispersed from the average value (the mean).
  1428. *
  1429. * Excel Function:
  1430. * STDEV(value1[,value2[, ...]])
  1431. *
  1432. * @access public
  1433. * @category Statistical Functions
  1434. * @param mixed $arg,... Data values
  1435. * @return float
  1436. */
  1437. public static function STDEV() {
  1438. // Return value
  1439. $returnValue = null;
  1440. $aMean = self::AVERAGE(func_get_args());
  1441. if (!is_null($aMean)) {
  1442. $aArgs = self::flattenArray(func_get_args());
  1443. $aCount = -1;
  1444. foreach ($aArgs as $arg) {
  1445. // Is it a numeric value?
  1446. if ((is_numeric($arg)) && (!is_string($arg))) {
  1447. if (is_null($returnValue)) {
  1448. $returnValue = pow(($arg - $aMean),2);
  1449. } else {
  1450. $returnValue += pow(($arg - $aMean),2);
  1451. }
  1452. ++$aCount;
  1453. }
  1454. }
  1455. // Return
  1456. if (($aCount > 0) && ($returnValue > 0)) {
  1457. return sqrt($returnValue / $aCount);
  1458. }
  1459. }
  1460. return self::$_errorCodes['divisionbyzero'];
  1461. } // function STDEV()
  1462. /**
  1463. * STDEVA
  1464. *
  1465. * Estimates standard deviation based on a sample, including numbers, text, and logical values
  1466. *
  1467. * Excel Function:
  1468. * STDEVA(value1[,value2[, ...]])
  1469. *
  1470. * @access public
  1471. * @category Statistical Functions
  1472. * @param mixed $arg,... Data values
  1473. * @return float
  1474. */
  1475. public static function STDEVA() {
  1476. // Return value
  1477. $returnValue = null;
  1478. $aMean = self::AVERAGEA(func_get_args());
  1479. if (!is_null($aMean)) {
  1480. $aArgs = self::flattenArray(func_get_args());
  1481. $aCount = -1;
  1482. foreach ($aArgs as $arg) {
  1483. // Is it a numeric value?
  1484. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
  1485. if (is_bool($arg)) {
  1486. $arg = (integer) $arg;
  1487. } elseif (is_string($arg)) {
  1488. $arg = 0;
  1489. }
  1490. if (is_null($returnValue)) {
  1491. $returnValue = pow(($arg - $aMean),2);
  1492. } else {
  1493. $returnValue += pow(($arg - $aMean),2);
  1494. }
  1495. ++$aCount;
  1496. }
  1497. }
  1498. // Return
  1499. if (($aCount > 0) && ($returnValue > 0)) {
  1500. return sqrt($returnValue / $aCount);
  1501. }
  1502. }
  1503. return self::$_errorCodes['divisionbyzero'];
  1504. } // function STDEVA()
  1505. /**
  1506. * STDEVP
  1507. *
  1508. * Calculates standard deviation based on the entire population
  1509. *
  1510. * Excel Function:
  1511. * STDEVP(value1[,value2[, ...]])
  1512. *
  1513. * @access public
  1514. * @category Statistical Functions
  1515. * @param mixed $arg,... Data values
  1516. * @return float
  1517. */
  1518. public static function STDEVP() {
  1519. // Return value
  1520. $returnValue = null;
  1521. $aMean = self::AVERAGE(func_get_args());
  1522. if (!is_null($aMean)) {
  1523. $aArgs = self::flattenArray(func_get_args());
  1524. $aCount = 0;
  1525. foreach ($aArgs as $arg) {
  1526. // Is it a numeric value?
  1527. if ((is_numeric($arg)) && (!is_string($arg))) {
  1528. if (is_null($returnValue)) {
  1529. $returnValue = pow(($arg - $aMean),2);
  1530. } else {
  1531. $returnValue += pow(($arg - $aMean),2);
  1532. }
  1533. ++$aCount;
  1534. }
  1535. }
  1536. // Return
  1537. if (($aCount > 0) && ($returnValue > 0)) {
  1538. return sqrt($returnValue / $aCount);
  1539. }
  1540. }
  1541. return self::$_errorCodes['divisionbyzero'];
  1542. } // function STDEVP()
  1543. /**
  1544. * STDEVPA
  1545. *
  1546. * Calculates standard deviation based on the entire population, including numbers, text, and logical values
  1547. *
  1548. * Excel Function:
  1549. * STDEVPA(value1[,value2[, ...]])
  1550. *
  1551. * @access public
  1552. * @category Statistical Functions
  1553. * @param mixed $arg,... Data values
  1554. * @return float
  1555. */
  1556. public static function STDEVPA() {
  1557. // Return value
  1558. $returnValue = null;
  1559. $aMean = self::AVERAGEA(func_get_args());
  1560. if (!is_null($aMean)) {
  1561. $aArgs = self::flattenArray(func_get_args());
  1562. $aCount = 0;
  1563. foreach ($aArgs as $arg) {
  1564. // Is it a numeric value?
  1565. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
  1566. if (is_bool($arg)) {
  1567. $arg = (integer) $arg;
  1568. } elseif (is_string($arg)) {
  1569. $arg = 0;
  1570. }
  1571. if (is_null($returnValue)) {
  1572. $returnValue = pow(($arg - $aMean),2);
  1573. } else {
  1574. $returnValue += pow(($arg - $aMean),2);
  1575. }
  1576. ++$aCount;
  1577. }
  1578. }
  1579. // Return
  1580. if (($aCount > 0) && ($returnValue > 0)) {
  1581. return sqrt($returnValue / $aCount);
  1582. }
  1583. }
  1584. return self::$_errorCodes['divisionbyzero'];
  1585. } // function STDEVPA()
  1586. /**
  1587. * VARFunc
  1588. *
  1589. * Estimates variance based on a sample.
  1590. *
  1591. * Excel Function:
  1592. * VAR(value1[,value2[, ...]])
  1593. *
  1594. * @access public
  1595. * @category Statistical Functions
  1596. * @param mixed $arg,... Data values
  1597. * @return float
  1598. */
  1599. public static function VARFunc() {
  1600. // Return value
  1601. $returnValue = self::$_errorCodes['divisionbyzero'];
  1602. $summerA = $summerB = 0;
  1603. // Loop through arguments
  1604. $aArgs = self::flattenArray(func_get_args());
  1605. $aCount = 0;
  1606. foreach ($aArgs as $arg) {
  1607. // Is it a numeric value?
  1608. if ((is_numeric($arg)) && (!is_string($arg))) {
  1609. $summerA += ($arg * $arg);
  1610. $summerB += $arg;
  1611. ++$aCount;
  1612. }
  1613. }
  1614. // Return
  1615. if ($aCount > 1) {
  1616. $summerA = $summerA * $aCount;
  1617. $summerB = ($summerB * $summerB);
  1618. $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1));
  1619. }
  1620. return $returnValue;
  1621. } // function VARFunc()
  1622. /**
  1623. * VARA
  1624. *
  1625. * Estimates variance based on a sample, including numbers, text, and logical values
  1626. *
  1627. * Excel Function:
  1628. * VARA(value1[,value2[, ...]])
  1629. *
  1630. * @access public
  1631. * @category Statistical Functions
  1632. * @param mixed $arg,... Data values
  1633. * @return float
  1634. */
  1635. public static function VARA() {
  1636. // Return value
  1637. $returnValue = self::$_errorCodes['divisionbyzero'];
  1638. $summerA = $summerB = 0;
  1639. // Loop through arguments
  1640. $aArgs = self::flattenArray(func_get_args());
  1641. $aCount = 0;
  1642. foreach ($aArgs as $arg) {
  1643. // Is it a numeric value?
  1644. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
  1645. if (is_bool($arg)) {
  1646. $arg = (integer) $arg;
  1647. } elseif (is_string($arg)) {
  1648. $arg = 0;
  1649. }
  1650. $summerA += ($arg * $arg);
  1651. $summerB += $arg;
  1652. ++$aCount;
  1653. }
  1654. }
  1655. // Return
  1656. if ($aCount > 1) {
  1657. $summerA = $summerA * $aCount;
  1658. $summerB = ($summerB * $summerB);
  1659. $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1));
  1660. }
  1661. return $returnValue;
  1662. } // function VARA()
  1663. /**
  1664. * VARP
  1665. *
  1666. * Calculates variance based on the entire population
  1667. *
  1668. * Excel Function:
  1669. * VARP(value1[,value2[, ...]])
  1670. *
  1671. * @access public
  1672. * @category Statistical Functions
  1673. * @param mixed $arg,... Data values
  1674. * @return float
  1675. */
  1676. public static function VARP() {
  1677. // Return value
  1678. $returnValue = self::$_errorCodes['divisionbyzero'];
  1679. $summerA = $summerB = 0;
  1680. // Loop through arguments
  1681. $aArgs = self::flattenArray(func_get_args());
  1682. $aCount = 0;
  1683. foreach ($aArgs as $arg) {
  1684. // Is it a numeric value?
  1685. if ((is_numeric($arg)) && (!is_string($arg))) {
  1686. $summerA += ($arg * $arg);
  1687. $summerB += $arg;
  1688. ++$aCount;
  1689. }
  1690. }
  1691. // Return
  1692. if ($aCount > 0) {
  1693. $summerA = $summerA * $aCount;
  1694. $summerB = ($summerB * $summerB);
  1695. $returnValue = ($summerA - $summerB) / ($aCount * $aCount);
  1696. }
  1697. return $returnValue;
  1698. } // function VARP()
  1699. /**
  1700. * VARPA
  1701. *
  1702. * Calculates variance based on the entire population, including numbers, text, and logical values
  1703. *
  1704. * Excel Function:
  1705. * VARPA(value1[,value2[, ...]])
  1706. *
  1707. * @access public
  1708. * @category Statistical Functions
  1709. * @param mixed $arg,... Data values
  1710. * @return float
  1711. */
  1712. public static function VARPA() {
  1713. // Return value
  1714. $returnValue = self::$_errorCodes['divisionbyzero'];
  1715. $summerA = $summerB = 0;
  1716. // Loop through arguments
  1717. $aArgs = self::flattenArray(func_get_args());
  1718. $aCount = 0;
  1719. foreach ($aArgs as $arg) {
  1720. // Is it a numeric value?
  1721. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
  1722. if (is_bool($arg)) {
  1723. $arg = (integer) $arg;
  1724. } elseif (is_string($arg)) {
  1725. $arg = 0;
  1726. }
  1727. $summerA += ($arg * $arg);
  1728. $summerB += $arg;
  1729. ++$aCount;
  1730. }
  1731. }
  1732. // Return
  1733. if ($aCount > 0) {
  1734. $summerA = $summerA * $aCount;
  1735. $summerB = ($summerB * $summerB);
  1736. $returnValue = ($summerA - $summerB) / ($aCount * $aCount);
  1737. }
  1738. return $returnValue;
  1739. } // function VARPA()
  1740. /**
  1741. * RANK
  1742. *
  1743. * Returns the rank of a number in a list of numbers.
  1744. *
  1745. * @param number The number whose rank you want to find.
  1746. * @param array of number An array of, or a reference to, a list of numbers.
  1747. * @param mixed Order to sort the values in the value set
  1748. * @return float
  1749. */
  1750. public static function RANK($value,$valueSet,$order=0) {
  1751. $value = self::flattenSingleValue($value);
  1752. $valueSet = self::flattenArray($valueSet);
  1753. $order = self::flattenSingleValue($order);
  1754. foreach($valueSet as $key => $valueEntry) {
  1755. if (!is_numeric($valueEntry)) {
  1756. unset($valueSet[$key]);
  1757. }
  1758. }
  1759. if ($order == 0) {
  1760. rsort($valueSet,SORT_NUMERIC);
  1761. } else {
  1762. sort($valueSet,SORT_NUMERIC);
  1763. }
  1764. $pos…

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