/branches/v1.5.5/Classes/PHPExcel/Calculation/Functions.php
PHP | 5602 lines | 3313 code | 482 blank | 1807 comment | 1019 complexity | b09c41e0ddc50a051cd384fc103b5078 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * PHPExcel
- *
- * Copyright (c) 2006 - 2007 PHPExcel
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * @category PHPExcel
- * @package PHPExcel_Calculation
- * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
- * @license http://www.gnu.org/licenses/lgpl.txt LGPL
- * @version ##VERSION##, ##DATE##
- */
-
-
- define('EPS', 2.22e-16);
- define('MAX_VALUE', 1.2e308);
- define('LOG_GAMMA_X_MAX_VALUE', 2.55e305);
- define('SQRT2PI', 2.5066282746310005024157652848110452530069867406099);
- define('XMININ', 2.23e-308);
- define('MAX_ITERATIONS', 150);
- define('PRECISION', 8.88E-016);
-
-
- /** PHPExcel_Cell */
- require_once 'PHPExcel/Cell.php';
-
- /** PHPExcel_Cell_DataType */
- require_once 'PHPExcel/Cell/DataType.php';
-
- /** PHPExcel_Shared_Date */
- require_once 'PHPExcel/Shared/Date.php';
-
-
- /**
- * PHPExcel_Calculation_Functions
- *
- * @category PHPExcel
- * @package PHPExcel_Calculation
- * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
- */
- class PHPExcel_Calculation_Functions {
-
- /** constants */
- const COMPATIBILITY_EXCEL = 'Excel';
- const COMPATIBILITY_GNUMERIC = 'Gnumeric';
- const COMPATIBILITY_OPENOFFICE = 'OpenOfficeCalc';
-
-
- /**
- * Compatibility mode to use for error checking and responses
- *
- * @var string
- */
- private static $compatibilityMode = self::COMPATIBILITY_EXCEL;
-
- /**
- * List of error codes
- *
- * @var array
- */
- private static $_errorCodes = array( 'null' => '#NULL!',
- 'divisionbyzero' => '#DIV/0!',
- 'value' => '#VALUE!',
- 'reference' => '#REF!',
- 'name' => '#NAME?',
- 'num' => '#NUM!',
- 'na' => '#N/A'
- );
-
-
- /**
- * Set the Compatibility Mode
- *
- * @param string $compatibilityMode Compatibility Mode
- * @return boolean (Success or Failure)
- */
- public static function setCompatibilityMode($compatibilityMode) {
- if (($compatibilityMode == self::COMPATIBILITY_EXCEL) ||
- ($compatibilityMode == self::COMPATIBILITY_GNUMERIC) ||
- ($compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
- self::$compatibilityMode = $compatibilityMode;
- return True;
- }
- return False;
- }
-
- /**
- * Return the Compatibility Mode
- *
- * @return string $compatibilityMode Compatibility Mode
- */
- public static function getCompatibilityMode() {
- return self::$compatibilityMode;
- }
-
- /**
- * DUMMY
- *
- * @return string #NAME?
- */
- public static function DUMMY() {
- return self::$_errorCodes['name'];
- }
-
- /**
- * NA
- *
- * @return string #N/A!
- */
- public static function NA() {
- return self::$_errorCodes['na'];
- }
-
- /**
- * LOGICAL_AND
- *
- * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.
- *
- * Booleans arguments are treated as True or False as appropriate
- * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
- * If any argument value is a string, or a Null, it is ignored
- *
- * Quirk of Excel:
- * String values passed directly to the function rather than through a cell reference
- * e.g.=AND(1,"A",1)
- * will return a #VALUE! error, _not_ ignoring the string.
- * This behaviour is not replicated
- *
- * @param array of mixed Data Series
- * @return boolean
- */
- public static function LOGICAL_AND() {
- // Return value
- $returnValue = True;
-
- // Loop through the arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- $argCount = 0;
- foreach ($aArgs as $arg) {
- // Is it a boolean value?
- if (is_bool($arg)) {
- $returnValue = $returnValue && $arg;
- $argCount++;
- } elseif ((is_numeric($arg)) && (!is_string($arg))) {
- $returnValue = $returnValue && ($arg != 0);
- $argCount++;
- }
- }
-
- // Return
- if ($argCount == 0) {
- return self::$_errorCodes['value'];
- }
- return $returnValue;
- }
-
- /**
- * LOGICAL_OR
- *
- * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
- *
- * Booleans arguments are treated as True or False as appropriate
- * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
- * If any argument value is a string, or a Null, it is ignored
- *
- * @param array of mixed Data Series
- * @return boolean
- */
- public static function LOGICAL_OR() {
- // Return value
- $returnValue = False;
-
- // Loop through the arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- $argCount = 0;
- foreach ($aArgs as $arg) {
- // Is it a boolean value?
- if (is_bool($arg)) {
- $returnValue = $returnValue || $arg;
- $argCount++;
- } elseif ((is_numeric($arg)) && (!is_string($arg))) {
- $returnValue = $returnValue || ($arg != 0);
- $argCount++;
- }
- }
-
- // Return
- if ($argCount == 0) {
- return self::$_errorCodes['value'];
- }
- return $returnValue;
- }
-
- /**
- * LOGICAL_FALSE
- *
- * Returns FALSE.
- *
- * @return boolean
- */
- public static function LOGICAL_FALSE() {
- return False;
- }
-
- /**
- * LOGICAL_TRUE
- *
- * Returns TRUE.
- *
- * @return boolean
- */
- public static function LOGICAL_TRUE() {
- return True;
- }
-
- /**
- * SUM
- *
- * SUM computes the sum of all the values and cells referenced in the argument list.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function SUM() {
- // Return value
- $returnValue = 0;
-
- // Loop through the arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $returnValue += $arg;
- }
- }
-
- // Return
- return $returnValue;
- }
-
- /**
- * SUMSQ
- *
- * Returns the sum of the squares of the arguments
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function SUMSQ() {
- // Return value
- $returnValue = 0;
-
- // Loop trough arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $returnValue += pow($arg,2);
- }
- }
-
- // Return
- return $returnValue;
- }
-
- /**
- * PRODUCT
- *
- * PRODUCT returns the product of all the values and cells referenced in the argument list.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function PRODUCT() {
- // Return value
- $returnValue = null;
-
- // Loop trough arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- if (is_null($returnValue)) {
- $returnValue = $arg;
- } else {
- $returnValue *= $arg;
- }
- }
- }
-
- // Return
- if (is_null($returnValue)) {
- return 0;
- }
- return $returnValue;
- }
-
- /**
- * QUOTIENT
- *
- * QUOTIENT function returns the integer portion of a division.numerator is the divided number
- * and denominator is the divisor.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function QUOTIENT() {
- // Return value
- $returnValue = null;
-
- // Loop trough arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- if (is_null($returnValue)) {
- if (($returnValue == 0) || ($arg == 0)) {
- $returnValue = 0;
- } else {
- $returnValue = $arg;
- }
- } else {
- if (($returnValue == 0) || ($arg == 0)) {
- $returnValue = 0;
- } else {
- $returnValue /= $arg;
- }
- }
- }
- }
-
- // Return
- return intval($returnValue);
- }
-
- /**
- * MIN
- *
- * MIN returns the value of the element of the values passed that has the smallest value,
- * with negative numbers considered smaller than positive numbers.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function MIN() {
- // Return value
- $returnValue = null;
-
- // Loop trough arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- if ((is_null($returnValue)) || ($arg < $returnValue)) {
- $returnValue = $arg;
- }
- }
- }
-
- // Return
- if(is_null($returnValue)) {
- return 0;
- }
- return $returnValue;
- }
-
- /**
- * MINA
- *
- * Returns the smallest value in a list of arguments, including numbers, text, and logical values
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function MINA() {
- // Return value
- $returnValue = null;
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
- if (is_bool($arg)) {
- $arg = (integer) $arg;
- } elseif (is_string($arg)) {
- $arg = 0;
- }
- if ((is_null($returnValue)) || ($arg < $returnValue)) {
- $returnValue = $arg;
- }
- }
- }
-
- // Return
- if(is_null($returnValue)) {
- return 0;
- }
- return $returnValue;
- }
-
- /**
- * SMALL
- *
- * Returns the nth smallest value in a data set. You can use this function to
- * select a value based on its relative standing.
- *
- * @param array of mixed Data Series
- * @param float Entry in the series to return
- * @return float
- */
- public static function SMALL() {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- // Calculate
- $n = array_pop($aArgs);
-
- if ((is_numeric($n)) && (!is_string($n))) {
- $mArgs = array();
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $mArgs[] = $arg;
- }
- }
- $count = self::COUNT($mArgs);
- $n = floor(--$n);
- if (($n < 0) || ($n >= $count) || ($count == 0)) {
- return self::$_errorCodes['num'];
- }
- sort($mArgs);
- return $mArgs[$n];
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * MAX
- *
- * MAX returns the value of the element of the values passed that has the highest value,
- * with negative numbers considered smaller than positive numbers.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function MAX() {
- // Return value
- $returnValue = null;
-
- // Loop trough arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- if ((is_null($returnValue)) || ($arg > $returnValue)) {
- $returnValue = $arg;
- }
- }
- }
-
- // Return
- if(is_null($returnValue)) {
- return 0;
- }
- return $returnValue;
- }
-
- /**
- * MAXA
- *
- * Returns the greatest value in a list of arguments, including numbers, text, and logical values
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function MAXA() {
- // Return value
- $returnValue = null;
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
- if (is_bool($arg)) {
- $arg = (integer) $arg;
- } elseif (is_string($arg)) {
- $arg = 0;
- }
- if ((is_null($returnValue)) || ($arg > $returnValue)) {
- $returnValue = $arg;
- }
- }
- }
-
- // Return
- if(is_null($returnValue)) {
- return 0;
- }
- return $returnValue;
- }
-
- /**
- * LARGE
- *
- * Returns the nth largest value in a data set. You can use this function to
- * select a value based on its relative standing.
- *
- * @param array of mixed Data Series
- * @param float Entry in the series to return
- * @return float
- *
- */
- public static function LARGE() {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- // Calculate
- $n = floor(array_pop($aArgs));
-
- if ((is_numeric($n)) && (!is_string($n))) {
- $mArgs = array();
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $mArgs[] = $arg;
- }
- }
- $count = self::COUNT($mArgs);
- $n = floor(--$n);
- if (($n < 0) || ($n >= $count) || ($count == 0)) {
- return self::$_errorCodes['num'];
- }
- rsort($mArgs);
- return $mArgs[$n];
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * PERCENTILE
- *
- * Returns the nth percentile of values in a range..
- *
- * @param array of mixed Data Series
- * @param float $entry Entry in the series to return
- * @return float
- */
- public static function PERCENTILE() {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- // Calculate
- $entry = array_pop($aArgs);
-
- if ((is_numeric($entry)) && (!is_string($entry))) {
- if (($entry < 0) || ($entry > 1)) {
- return self::$_errorCodes['num'];
- }
- $mArgs = array();
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $mArgs[] = $arg;
- }
- }
- $mValueCount = count($mArgs);
- if ($mValueCount > 0) {
- sort($mArgs);
- $count = self::COUNT($mArgs);
- $index = $entry * ($count-1);
- $iBase = floor($index);
- if ($index == $iBase) {
- return $mArgs[$index];
- } else {
- $iNext = $iBase + 1;
- $iProportion = $index - $iBase;
- return $mArgs[$iBase] + (($mArgs[$iNext] - $mArgs[$iBase]) * $iProportion) ;
- }
- }
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * QUARTILE
- *
- * Returns the quartile of a data set.
- *
- * @param array of mixed Data Series
- * @param float $entry Entry in the series to return
- * @return float
- */
- public static function QUARTILE() {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- // Calculate
- $entry = floor(array_pop($aArgs));
-
- if ((is_numeric($entry)) && (!is_string($entry))) {
- $entry /= 4;
- if (($entry < 0) || ($entry > 1)) {
- return self::$_errorCodes['num'];
- }
- return self::PERCENTILE($aArgs,$entry);
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * COUNT
- *
- * Counts the number of cells that contain numbers within the list of arguments
- *
- * @param array of mixed Data Series
- * @return int
- */
- public static function COUNT() {
- // Return value
- $returnValue = 0;
-
- // Loop trough arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
- $arg = (int) $arg;
- }
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- ++$returnValue;
- }
- }
-
- // Return
- return $returnValue;
- }
-
- /**
- * COUNTBLANK
- *
- * Counts the number of empty cells within the list of arguments
- *
- * @param array of mixed Data Series
- * @return int
- */
- public static function COUNTBLANK() {
- // Return value
- $returnValue = 0;
-
- // Loop trough arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a blank cell?
- if ((is_null($arg)) || ((is_string($arg)) && ($arg == ''))) {
- ++$returnValue;
- }
- }
-
- // Return
- return $returnValue;
- }
-
- /**
- * COUNTA
- *
- * Counts the number of cells that are not empty within the list of arguments
- *
- * @param array of mixed Data Series
- * @return int
- */
- public static function COUNTA() {
- // Return value
- $returnValue = 0;
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a numeric, boolean or string value?
- if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
- ++$returnValue;
- }
- }
-
- // Return
- return $returnValue;
- }
-
- /**
- * AVERAGE
- *
- * Returns the average (arithmetic mean) of the arguments
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function AVERAGE() {
- // Return value
- $returnValue = 0;
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- $aCount = 0;
- foreach ($aArgs as $arg) {
- if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
- $arg = (integer) $arg;
- }
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- if (is_null($returnValue)) {
- $returnValue = $arg;
- } else {
- $returnValue += $arg;
- }
- ++$aCount;
- }
- }
-
- // Return
- if ($aCount > 0) {
- return $returnValue / $aCount;
- } else {
- return self::$_errorCodes['divisionbyzero'];
- }
- }
-
- /**
- * AVERAGEA
- *
- * Returns the average of its arguments, including numbers, text, and logical values
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function AVERAGEA() {
- // Return value
- $returnValue = null;
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- $aCount = 0;
- foreach ($aArgs as $arg) {
- if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
- if (is_bool($arg)) {
- $arg = (integer) $arg;
- } elseif (is_string($arg)) {
- $arg = 0;
- }
- if (is_null($returnValue)) {
- $returnValue = $arg;
- } else {
- $returnValue += $arg;
- }
- ++$aCount;
- }
- }
-
- // Return
- if ($aCount > 0) {
- return $returnValue / $aCount;
- } else {
- return self::$_errorCodes['divisionbyzero'];
- }
- }
-
- /**
- * MEDIAN
- *
- * Returns the median of the given numbers. The median is the number in the middle of a set of numbers.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function MEDIAN() {
- // Return value
- $returnValue = self::$_errorCodes['num'];
-
- $mArgs = array();
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $mArgs[] = $arg;
- }
- }
-
- $mValueCount = count($mArgs);
- if ($mValueCount > 0) {
- sort($mArgs,SORT_NUMERIC);
- $mValueCount = $mValueCount / 2;
- if ($mValueCount == floor($mValueCount)) {
- $returnValue = ($mArgs[$mValueCount--] + $mArgs[$mValueCount]) / 2;
- } else {
- $mValueCount == floor($mValueCount);
- $returnValue = $mArgs[$mValueCount];
- }
- }
-
- // Return
- return $returnValue;
- }
-
- //
- // Special variant of array_count_values that isn't limited to strings and integers,
- // but can work with floating point numbers as values
- //
- private static function modeCalc($data) {
- $frequencyArray = array();
- foreach($data as $datum) {
- $found = False;
- foreach($frequencyArray as $key => $value) {
- if ((string)$value['value'] == (string)$datum) {
- ++$frequencyArray[$key]['frequency'];
- $found = True;
- break;
- }
- }
- if (!$found) {
- $frequencyArray[] = array('value' => $datum,
- 'frequency' => 1 );
- }
- }
-
- foreach($frequencyArray as $key => $value) {
- $frequencyList[$key] = $value['frequency'];
- $valueList[$key] = $value['value'];
- }
- array_multisort($frequencyList, SORT_DESC, $valueList, SORT_ASC, SORT_NUMERIC, $frequencyArray);
-
- if ($frequencyArray[0]['frequency'] == 1) {
- return self::NA();
- }
- return $frequencyArray[0]['value'];
- }
-
- /**
- * MODE
- *
- * Returns the most frequently occurring, or repetitive, value in an array or range of data
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function MODE() {
- // Return value
- $returnValue = PHPExcel_Calculation_Functions::NA();
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- $mArgs = array();
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $mArgs[] = $arg;
- }
- }
-
- if (count($mArgs) > 0) {
- return self::modeCalc($mArgs);
- }
-
- // Return
- return $returnValue;
- }
-
- /**
- * DEVSQ
- *
- * Returns the sum of squares of deviations of data points from their sample mean.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function DEVSQ() {
- // Return value
- $returnValue = null;
-
- $aMean = PHPExcel_Calculation_Functions::AVERAGE(func_get_args());
- if (!is_null($aMean)) {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- $aCount = -1;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
- $arg = (int) $arg;
- }
- if ((is_numeric($arg)) && (!is_string($arg))) {
- if (is_null($returnValue)) {
- $returnValue = pow(($arg - $aMean),2);
- } else {
- $returnValue += pow(($arg - $aMean),2);
- }
- ++$aCount;
- }
- }
-
- // Return
- if (is_null($returnValue)) {
- return self::$_errorCodes['num'];
- } else {
- return $returnValue;
- }
- }
- return self::NA();
- }
-
- /**
- * AVEDEV
- *
- * Returns the average of the absolute deviations of data points from their mean.
- * AVEDEV is a measure of the variability in a data set.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function AVEDEV() {
- // Return value
- $returnValue = null;
-
- $aMean = PHPExcel_Calculation_Functions::AVERAGE(func_get_args());
- if ($aMean != self::$_errorCodes['divisionbyzero']) {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- $aCount = 0;
- foreach ($aArgs as $arg) {
- if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
- $arg = (integer) $arg;
- }
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- if (is_null($returnValue)) {
- $returnValue = abs($arg - $aMean);
- } else {
- $returnValue += abs($arg - $aMean);
- }
- ++$aCount;
- }
- }
-
- // Return
- return $returnValue / $aCount ;
- }
- return self::$_errorCodes['num'];
- }
-
- /**
- * GEOMEAN
- *
- * Returns the geometric mean of an array or range of positive data. For example, you
- * can use GEOMEAN to calculate average growth rate given compound interest with
- * variable rates.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function GEOMEAN() {
- $aMean = PHPExcel_Calculation_Functions::PRODUCT(func_get_args());
- if (is_numeric($aMean) && ($aMean > 0)) {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- $aCount = PHPExcel_Calculation_Functions::COUNT($aArgs) ;
- if (PHPExcel_Calculation_Functions::MIN($aArgs) > 0) {
- return pow($aMean, (1 / $aCount));
- }
- }
- return self::$_errorCodes['num'];
- }
-
- /**
- * HARMEAN
- *
- * Returns the harmonic mean of a data set. The harmonic mean is the reciprocal of the
- * arithmetic mean of reciprocals.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function HARMEAN() {
- // Return value
- $returnValue = self::NA();
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- if (PHPExcel_Calculation_Functions::MIN($aArgs) < 0) {
- return self::$_errorCodes['num'];
- }
- $aCount = 0;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- if ($arg <= 0) {
- return self::$_errorCodes['num'];
- }
- if (is_null($returnValue)) {
- $returnValue = (1 / $arg);
- } else {
- $returnValue += (1 / $arg);
- }
- ++$aCount;
- }
- }
-
- // Return
- if ($aCount > 0) {
- return 1 / ($returnValue / $aCount);
- } else {
- return $returnValue;
- }
- }
-
- /**
- * TRIMMEAN
- *
- * Returns the mean of the interior of a data set. TRIMMEAN calculates the mean
- * taken by excluding a percentage of data points from the top and bottom tails
- * of a data set.
- *
- * @param array of mixed Data Series
- * @param float Percentage to discard
- * @return float
- */
- public static function TRIMMEAN() {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- // Calculate
- $percent = array_pop($aArgs);
-
- if ((is_numeric($percent)) && (!is_string($percent))) {
- if (($percent < 0) || ($percent > 1)) {
- return self::$_errorCodes['num'];
- }
- $mArgs = array();
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $mArgs[] = $arg;
- }
- }
- $discard = floor(self::COUNT($mArgs) * $percent / 2);
- sort($mArgs);
- for ($i=0; $i < $discard; ++$i) {
- array_pop($mArgs);
- array_shift($mArgs);
- }
- return self::AVERAGE($mArgs);
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * STDEV
- *
- * Estimates standard deviation based on a sample. The standard deviation is a measure of how
- * widely values are dispersed from the average value (the mean).
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function STDEV() {
- // Return value
- $returnValue = null;
-
- $aMean = PHPExcel_Calculation_Functions::AVERAGE(func_get_args());
- if (!is_null($aMean)) {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- $aCount = -1;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- if (is_null($returnValue)) {
- $returnValue = pow(($arg - $aMean),2);
- } else {
- $returnValue += pow(($arg - $aMean),2);
- }
- ++$aCount;
- }
- }
-
- // Return
- if (($aCount > 0) && ($returnValue > 0)) {
- return sqrt($returnValue / $aCount);
- }
- }
- return self::$_errorCodes['divisionbyzero'];
- }
-
- /**
- * STDEVA
- *
- * Estimates standard deviation based on a sample, including numbers, text, and logical values
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function STDEVA() {
- // Return value
- $returnValue = null;
-
- $aMean = PHPExcel_Calculation_Functions::AVERAGEA(func_get_args());
- if (!is_null($aMean)) {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- $aCount = -1;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
- if (is_bool($arg)) {
- $arg = (integer) $arg;
- } elseif (is_string($arg)) {
- $arg = 0;
- }
- if (is_null($returnValue)) {
- $returnValue = pow(($arg - $aMean),2);
- } else {
- $returnValue += pow(($arg - $aMean),2);
- }
- ++$aCount;
- }
- }
-
- // Return
- if (($aCount > 0) && ($returnValue > 0)) {
- return sqrt($returnValue / $aCount);
- }
- }
- return self::$_errorCodes['divisionbyzero'];
- }
-
- /**
- * STDEVP
- *
- * Calculates standard deviation based on the entire population
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function STDEVP() {
- // Return value
- $returnValue = null;
-
- $aMean = PHPExcel_Calculation_Functions::AVERAGE(func_get_args());
- if (!is_null($aMean)) {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- $aCount = 0;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- if (is_null($returnValue)) {
- $returnValue = pow(($arg - $aMean),2);
- } else {
- $returnValue += pow(($arg - $aMean),2);
- }
- ++$aCount;
- }
- }
-
- // Return
- if (($aCount > 0) && ($returnValue > 0)) {
- return sqrt($returnValue / $aCount);
- }
- }
- return self::$_errorCodes['divisionbyzero'];
- }
-
- /**
- * STDEVPA
- *
- * Calculates standard deviation based on the entire population, including numbers, text, and logical values
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function STDEVPA() {
- // Return value
- $returnValue = null;
-
- $aMean = PHPExcel_Calculation_Functions::AVERAGEA(func_get_args());
- if (!is_null($aMean)) {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- $aCount = 0;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
- if (is_bool($arg)) {
- $arg = (integer) $arg;
- } elseif (is_string($arg)) {
- $arg = 0;
- }
- if (is_null($returnValue)) {
- $returnValue = pow(($arg - $aMean),2);
- } else {
- $returnValue += pow(($arg - $aMean),2);
- }
- ++$aCount;
- }
- }
-
- // Return
- if (($aCount > 0) && ($returnValue > 0)) {
- return sqrt($returnValue / $aCount);
- }
- }
- return self::$_errorCodes['divisionbyzero'];
- }
-
- /**
- * VARFunc
- *
- * Estimates variance based on a sample.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function VARFunc() {
- // Return value
- $returnValue = self::$_errorCodes['divisionbyzero'];
-
- $summerA = $summerB = 0;
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- $aCount = 0;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $summerA += ($arg * $arg);
- $summerB += $arg;
- ++$aCount;
- }
- }
-
- // Return
- if ($aCount > 1) {
- $summerA = $summerA * $aCount;
- $summerB = ($summerB * $summerB);
- $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1));
- }
- return $returnValue;
- }
-
- /**
- * VARA
- *
- * Estimates variance based on a sample, including numbers, text, and logical values
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function VARA() {
- // Return value
- $returnValue = self::$_errorCodes['divisionbyzero'];
-
- $summerA = $summerB = 0;
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- $aCount = 0;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
- if (is_bool($arg)) {
- $arg = (integer) $arg;
- } elseif (is_string($arg)) {
- $arg = 0;
- }
- $summerA += ($arg * $arg);
- $summerB += $arg;
- ++$aCount;
- }
- }
-
- // Return
- if ($aCount > 1) {
- $summerA = $summerA * $aCount;
- $summerB = ($summerB * $summerB);
- $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1));
- }
- return $returnValue;
- }
-
- /**
- * VARP
- *
- * Calculates variance based on the entire population
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function VARP() {
- // Return value
- $returnValue = self::$_errorCodes['divisionbyzero'];
-
- $summerA = $summerB = 0;
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- $aCount = 0;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $summerA += ($arg * $arg);
- $summerB += $arg;
- ++$aCount;
- }
- }
-
- // Return
- if ($aCount > 0) {
- $summerA = $summerA * $aCount;
- $summerB = ($summerB * $summerB);
- $returnValue = ($summerA - $summerB) / ($aCount * $aCount);
- }
- return $returnValue;
- }
-
- /**
- * VARPA
- *
- * Calculates variance based on the entire population, including numbers, text, and logical values
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function VARPA() {
- // Return value
- $returnValue = self::$_errorCodes['divisionbyzero'];
-
- $summerA = $summerB = 0;
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- $aCount = 0;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
- if (is_bool($arg)) {
- $arg = (integer) $arg;
- } elseif (is_string($arg)) {
- $arg = 0;
- }
- $summerA += ($arg * $arg);
- $summerB += $arg;
- ++$aCount;
- }
- }
-
- // Return
- if ($aCount > 0) {
- $summerA = $summerA * $aCount;
- $summerB = ($summerB * $summerB);
- $returnValue = ($summerA - $summerB) / ($aCount * $aCount);
- }
- return $returnValue;
- }
-
- /**
- * SUBTOTAL
- *
- * Returns a subtotal in a list or database.
- *
- * @param int the number 1 to 11 that specifies which function to
- * use in calculating subtotals within a list.
- * @param array of mixed Data Series
- * @return float
- */
- public static function SUBTOTAL() {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- // Calculate
- $subtotal = array_shift($aArgs);
-
- if ((is_numeric($subtotal)) && (!is_string($subtotal))) {
- switch($subtotal) {
- case 1 :
- return self::AVERAGE($aArgs);
- break;
- case 2 :
- return self::COUNT($aArgs);
- break;
- case 3 :
- return self::COUNTA($aArgs);
- break;
- case 4 :
- return self::MAX($aArgs);
- break;
- case 5 :
- return self::MIN($aArgs);
- break;
- case 6 :
- return self::PRODUCT($aArgs);
- break;
- case 7 :
- return self::STDEV($aArgs);
- break;
- case 8 :
- return self::STDEVP($aArgs);
- break;
- case 9 :
- return self::SUM($aArgs);
- break;
- case 10 :
- return self::VARFunc($aArgs);
- break;
- case 11 :
- return self::VARP($aArgs);
- break;
- }
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * SQRTPI
- *
- * Returns the square root of (number * pi).
- *
- * @param float $number Number
- * @return float Square Root of Number * Pi
- */
- public static function SQRTPI($number) {
- $number = self::flattenSingleValue($number);
-
- if (is_numeric($number)) {
- if ($number < 0) {
- return self::$_errorCodes['num'];
- }
- return sqrt($number * pi()) ;
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * FACT
- *
- * Returns the factorial of a number.
- *
- * @param float $factVal Factorial Value
- * @return int Factorial
- */
- public static function FACT($factVal) {
- $factVal = self::flattenSingleValue($factVal);
-
- if (is_numeric($factVal)) {
- if ($factVal < 0) {
- return self::$_errorCodes['num'];
- }
- $factLoop = floor($factVal);
- if (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC) {
- if ($factVal > $factLoop) {
- return self::$_errorCodes['num'];
- }
- }
- $factorial = 1;
- while ($factLoop > 1) {
- $factorial *= $factLoop--;
- }
- return $factorial ;
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * FACTDOUBLE
- *
- * Returns the double factorial of a number.
- *
- * @param float $factVal Factorial Value
- * @return int Double Factorial
- */
- public static function FACTDOUBLE($factVal) {
- $factLoop = floor(self::flattenSingleValue($factVal));
-
- if (is_numeric($factLoop)) {
- if ($factVal < 0) {
- return self::$_errorCodes['num'];
- }
- $factorial = 1;
- while ($factLoop > 1) {
- $factorial *= $factLoop--;
- --$factLoop;
- }
- return $factorial ;
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * MULTINOMIAL
- *
- * Returns the ratio of the factorial of a sum of values to the product of factorials.
- *
- * @param array of mixed Data Series
- * @return float
- */
- public static function MULTINOMIAL() {
-
- // Loop through arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- $summer = 0;
- $divisor = 1;
- foreach ($aArgs as $arg) {
- // Is it a numeric value?
- if (is_numeric($arg)) {
- if ($arg < 1) {
- return self::$_errorCodes['num'];
- }
- $summer += floor($arg);
- $divisor *= self::FACT($arg);
- } else {
- return self::$_errorCodes['value'];
- }
- }
-
- // Return
- if ($summer > 0) {
- $summer = self::FACT($summer);
- return $summer / $divisor;
- }
- return 0;
- }
-
- /**
- * CEILING
- *
- * Returns number rounded up, away from zero, to the nearest multiple of significance.
- *
- * @param float $number Number to round
- * @param float $significance Significance
- * @return float Rounded Number
- */
- public static function CEILING($number,$significance) {
- $number = self::flattenSingleValue($number);
- $significance = self::flattenSingleValue($significance);
-
- if ((is_numeric($number)) && (is_numeric($significance))) {
- if ($significance == 0.0) {
- return 0;
- }
- return ceil($number / $significance) * $significance;
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * EVEN
- *
- * Returns number rounded up to the nearest even integer.
- *
- * @param float $number Number to round
- * @return int Rounded Number
- */
- public static function EVEN($number) {
- $number = self::flattenSingleValue($number);
-
- if (is_numeric($number)) {
- $significance = 2 * self::SIGN($number);
- return self::CEILING($number,$significance);
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * ODD
- *
- * Returns number rounded up to the nearest odd integer.
- *
- * @param float $number Number to round
- * @return int Rounded Number
- */
- public static function ODD($number) {
- $number = self::flattenSingleValue($number);
-
- if (is_numeric($number)) {
- $significance = self::SIGN($number);
- if ($significance == 0) {
- return 1;
- }
- $result = self::CEILING($number,$significance);
- if (self::IS_EVEN($result)) {
- $result += $significance;
- }
- return $result;
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * ROUNDUP
- *
- * Rounds a number up to a specified number of decimal places
- *
- * @param float $number Number to round
- * @param int $digits Number of digits to which you want to round $number
- * @return float Rounded Number
- */
- public static function ROUNDUP($number,$digits) {
- $number = self::flattenSingleValue($number);
- $digits = self::flattenSingleValue($digits);
-
- if (is_numeric($number)) {
- if ((is_numeric($digits)) && ($digits >= 0)) {
- $significance = pow(10,$digits);
- return ceil($number * $significance) / $significance;
- }
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * ROUNDDOWN
- *
- * Rounds a number down to a specified number of decimal places
- *
- * @param float $number Number to round
- * @param int $digits Number of digits to which you want to round $number
- * @return float Rounded Number
- */
- public static function ROUNDDOWN($number,$digits) {
- $number = self::flattenSingleValue($number);
- $digits = self::flattenSingleValue($digits);
-
- if (is_numeric($number)) {
- if ((is_numeric($digits)) && ($digits >= 0)) {
- $significance = pow(10,$digits);
- return floor($number * $significance) / $significance;
- }
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * MROUND
- *
- * Rounds a number to the nearest multiple of a specified value
- *
- * @param float $number Number to round
- * @param int $multiple Multiple to which you want to round $number
- * @return float Rounded Number
- */
- public static function MROUND($number,$multiple) {
- $number = self::flattenSingleValue($number);
- $multiple = self::flattenSingleValue($multiple);
-
- if ((is_numeric($number)) && (is_numeric($multiple))) {
- if ((self::SIGN($number)) == (self::SIGN($multiple))) {
- $lowerVal = floor($number / $multiple) * $multiple;
- $upperVal = ceil($number / $multiple) * $multiple;
- $adjustUp = abs($number - $upperVal);
- $adjustDown = abs($number - $lowerVal) + PRECISION;
- if ($adjustDown < $adjustUp) {
- return $lowerVal;
- }
- return $upperVal;
- }
- return self::$_errorCodes['num'];
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * SIGN
- *
- * Determines the sign of a number. Returns 1 if the number is positive, zero (0)
- * if the number is 0, and -1 if the number is negative.
- *
- * @param float $number Number to round
- * @return int sign value
- */
- public static function SIGN($number) {
- $number = self::flattenSingleValue($number);
-
- if (is_numeric($number)) {
- if ($number == 0.0) {
- return 0;
- }
- return $number / abs($number);
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * FLOOR
- *
- * Rounds number down, toward zero, to the nearest multiple of significance.
- *
- * @param float $number Number to round
- * @param float $significance Significance
- * @return float Rounded Number
- */
- public static function FLOOR($number,$significance) {
- $number = self::flattenSingleValue($number);
- $significance = self::flattenSingleValue($significance);
-
- if ((is_numeric($number)) && (is_numeric($significance))) {
- return floor($number / $significance) * $significance;
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * PERMUT
- *
- * Returns the number of permutations for a given number of objects that can be
- * selected from number objects. A permutation is any set or subset of objects or
- * events where internal order is significant. Permutations are different from
- * combinations, for which the internal order is not significant. Use this function
- * for lottery-style probability calculations.
- *
- * @param int $numObjs Number of different objects
- * @param int $numInSet Number of objects in each permutation
- * @return int Number of permutations
- */
- public static function PERMUT($numObjs,$numInSet) {
- $numObjs = self::flattenSingleValue($numObjs);
- $numInSet = self::flattenSingleValue($numInSet);
-
- if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
- if ($numObjs < $numInSet) {
- return self::$_errorCodes['num'];
- }
- return self::FACT($numObjs) / self::FACT($numObjs - $numInSet);
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * COMBIN
- *
- * Returns the number of combinations for a given number of items. Use COMBIN to
- * determine the total possible number of groups for a given number of items.
- *
- * @param int $numObjs Number of different objects
- * @param int $numInSet Number of objects in each combination
- * @return int Number of combinations
- */
- public static function COMBIN($numObjs,$numInSet) {
- $numObjs = self::flattenSingleValue($numObjs);
- $numInSet = self::flattenSingleValue($numInSet);
-
- if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
- if ($numObjs < $numInSet) {
- return self::$_errorCodes['num'];
- }
- return (self::FACT($numObjs) / self::FACT($numObjs - $numInSet)) / self::FACT($numInSet);
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * SERIESSUM
- *
- * Returns the sum of a power series
- *
- * @param float $x Input value to the power series
- * @param float $n Initial power to which you want to raise $x
- * @param float $m Step by which to increase $n for each term in the series
- * @param array of mixed Data Series
- * @return float
- */
- public static function SERIESSUM() {
- // Return value
- $returnValue = 0;
-
- // Loop trough arguments
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- $x = array_shift($aArgs);
- $n = array_shift($aArgs);
- $m = array_shift($aArgs);
-
- if ((is_numeric($x)) && (is_numeric($n)) && (is_numeric($m))) {
- // Calculate
- $i = 0;
- foreach($aArgs as $arg) {
- // Is it a numeric value?
- if ((is_numeric($arg)) && (!is_string($arg))) {
- $returnValue += $arg * pow($x,$n + ($m * $i++));
- } else {
- return self::$_errorCodes['value'];
- }
- }
- // Return
- return $returnValue;
- }
- return self::$_errorCodes['value'];
- }
-
- /**
- * STANDARDIZE
- *
- * Returns a normalized value from a distribution characterized by mean and standard_dev.
- *
- * @param float $value Value to normalize
- * @param float $mean Mean Value
- * @param float $stdDev Standard Deviation
- * @return float Standardized value
- */
- public static function STANDARDIZE($value,$mean,$stdDev) {
- $value = self::flattenSingleValue($value);
- $mean = self::flattenSingleValue($mean);
- $stdDev = self::flattenSingleValue($stdDev);
-
- if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
- if ($stdDev <= 0) {
- return self::$_errorCodes['num'];
- }
- return ($value - $mean) / $stdDev ;
- }
- return self::$_errorCodes['value'];
- }
-
- //
- // Private method to return an array of the factors of the input value
- //
- private static function factors($value) {
- $startVal = floor($value/2);
-
- $factorArray = array();
- for($i=$startVal; $i>1; --$i) {
- if (($value/$i) == floor($value/$i)) {
- $subFactors = self::factors($i);
- if ($i == sqrt($value)) {
- $factorArray = array_merge($factorArray,$subFactors,$subFactors);
- } else {
- $value /= $i;
- $factorArray = array_merge($factorArray,$subFactors);
- }
- }
- }
- if (count($factorArray) > 0) {
- return $factorArray;
- } else {
- return array((integer)$value);
- }
- }
-
- /**
- * LCM
- *
- * Returns the lowest common multiplier of a series of numbers
- *
- * @param $array Values to calculate the Lowest Common Multiplier
- * @return int Lowest Common Multiplier
- */
- public static function LCM() {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- $returnValue = 1;
- $allPoweredFactors = array();
- foreach($aArgs as $value) {
- if (!is_numeric($value)) {
- return self::$_errorCodes['value'];
- }
- if ($value < 1) {
- return self::$_errorCodes['num'];
- }
- $myFactors = self::factors(floor($value));
- $myCountedFactors = array_count_values($myFactors);
- $myPoweredFactors = array();
- foreach($myCountedFactors as $myCountedFactor => $myCountedPower) {
- $myPoweredFactors[$myCountedFactor] = pow($myCountedFactor,$myCountedPower);
- }
- foreach($myPoweredFactors as $myPoweredValue => $myPoweredFactor) {
- if (array_key_exists($myPoweredValue,$allPoweredFactors)) {
- if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) {
- $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
- }
- } else {
- $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
- }
- }
- }
- foreach($allPoweredFactors as $allPoweredFactor) {
- $returnValue *= (integer) $allPoweredFactor;
- }
- return $returnValue;
- }
-
- /**
- * GCD
- *
- * Returns the greatest common divisor of a series of numbers
- *
- * @param $array Values to calculate the Greatest Common Divisor
- * @return int Greatest Common Divisor
- */
- public static function GCD() {
- $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
-
- $returnValue = 1;
- $allPoweredFactors = array();
- foreach($aArgs as $value) {
- if ($value == 0) {
- return 0;
- }
- $myFactors = self::factors($value);
- $myCountedFactors = array_count_values($myFactors);
- $allValuesFactors[] = $myCountedFactors;
- }
- $allValuesCount = count($allValuesFactors);
- $mergedArray = $allValuesFactors[0];
- for ($i=1;$i < $allValuesCount; ++$i) {
- $mergedArray = array_intersect_key($mergedArray,$allValuesFactors[$i]);
- }
- $mergedArrayValues = count($mergedArray);
- if ($mergedArrayValues == 0) {
- return $returnValue;
- } elseif ($mergedArrayValues > 1) {
- foreach($mergedArray as $mergedKey => $mergedValue) {
- foreach($allValuesFactors as $highestPowerTest) {
- foreach($highestPowerTest as $testKey => $testValue) {
- if (($testKey == $mergedKey) && ($testValue < $mergedValue)) {
- $mergedArray[$mergedKey] = $testValue;
- $mergedValue = $testValue;
- }
- }
- }
- }
-
- $returnValue = 1;
- foreach($mergedArray as $key => $value) {
- $returnValue *= pow($key,$value);
- }
- return $returnValue;
- } else {
- $keys = array_keys($mergedArray);
- $key = $keys[0];
- $value = $mergedArray[$key];
- foreach($allValuesFactors a…
Large files files are truncated, but you can click here to view the full file