30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
35 require(PHPEXCEL_ROOT .
'PHPExcel/Autoloader.php');
52 $startVal = floor(sqrt($value));
54 $factorArray = array();
55 for ($i = $startVal; $i > 1; --$i) {
56 if (($value % $i) == 0) {
57 $factorArray = array_merge($factorArray,self::_factors($value / $i));
58 $factorArray = array_merge($factorArray,self::_factors($i));
59 if ($i <= sqrt($value)) {
64 if (count($factorArray) > 0) {
68 return array((integer) $value);
74 return ($num - ($num % $n ) ) / $n;
100 public static function ATAN2($xCoordinate, $yCoordinate) {
104 if (($xCoordinate == 0) && ($yCoordinate == 0)) {
108 return atan2($yCoordinate, $xCoordinate);
121 public static function CEILING($number,$significance=null) {
126 $significance = $number/abs($number);
129 if ((is_numeric($number)) && (is_numeric($significance))) {
130 if (self::SIGN($number) == self::SIGN($significance)) {
131 if ($significance == 0.0) {
134 return ceil($number / $significance) * $significance;
153 public static function COMBIN($numObjs,$numInSet) {
157 if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
158 if ($numObjs < $numInSet) {
160 } elseif ($numInSet < 0) {
163 return round(self::FACT($numObjs) / self::FACT($numObjs - $numInSet)) /
self::FACT($numInSet);
177 public static function EVEN($number) {
180 if (is_null($number)) {
182 } elseif (is_numeric($number)) {
198 public static function FACT($factVal) {
201 if (is_numeric($factVal)) {
205 $factLoop = floor($factVal);
207 if ($factVal > $factLoop) {
213 while ($factLoop > 1) {
214 $factorial *= $factLoop--;
233 if (is_numeric($factLoop)) {
238 while ($factLoop > 1) {
239 $factorial *= $factLoop--;
257 public static function FLOOR($number,$significance=null) {
262 $significance = $number/abs($number);
265 if ((is_numeric($number)) && (is_numeric($significance))) {
266 if ((
float) $significance == 0.0) {
269 if (self::SIGN($number) == self::SIGN($significance)) {
270 return floor($number / $significance) * $significance;
287 public static function GCD() {
289 $allPoweredFactors = array();
296 $myCountedFactors = array_count_values($myFactors);
297 $allValuesFactors[] = $myCountedFactors;
299 $allValuesCount = count($allValuesFactors);
300 $mergedArray = $allValuesFactors[0];
301 for ($i=1;$i < $allValuesCount; ++$i) {
302 $mergedArray = array_intersect_key($mergedArray,$allValuesFactors[$i]);
304 $mergedArrayValues = count($mergedArray);
305 if ($mergedArrayValues == 0) {
307 } elseif ($mergedArrayValues > 1) {
308 foreach($mergedArray as $mergedKey => $mergedValue) {
309 foreach($allValuesFactors as $highestPowerTest) {
310 foreach($highestPowerTest as $testKey => $testValue) {
311 if (($testKey == $mergedKey) && ($testValue < $mergedValue)) {
312 $mergedArray[$mergedKey] = $testValue;
313 $mergedValue = $testValue;
320 foreach($mergedArray as $key => $value) {
321 $returnValue *= pow($key,$value);
325 $keys = array_keys($mergedArray);
327 $value = $mergedArray[$key];
328 foreach($allValuesFactors as $testValue) {
329 foreach($testValue as $mergedKey => $mergedValue) {
330 if (($mergedKey == $key) && ($mergedValue < $value)) {
331 $value = $mergedValue;
335 return pow($key,$value);
348 public static function INT($number) {
351 if (is_numeric($number)) {
352 return (
int) floor($number);
366 public static function LCM() {
368 $allPoweredFactors = array();
371 if (!is_numeric($value)) {
376 } elseif ($value < 0) {
380 $myCountedFactors = array_count_values($myFactors);
381 $myPoweredFactors = array();
382 foreach($myCountedFactors as $myCountedFactor => $myCountedPower) {
383 $myPoweredFactors[$myCountedFactor] = pow($myCountedFactor,$myCountedPower);
385 foreach($myPoweredFactors as $myPoweredValue => $myPoweredFactor) {
386 if (array_key_exists($myPoweredValue,$allPoweredFactors)) {
387 if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) {
388 $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
391 $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
395 foreach($allPoweredFactors as $allPoweredFactor) {
396 $returnValue *= (integer) $allPoweredFactor;
416 public static function LOG_BASE($number, $base=10) {
420 return log($number, $base);
430 public static function MDETERM($matrixValues) {
431 $matrixData = array();
432 if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); }
434 $row = $maxColumn = 0;
435 foreach($matrixValues as $matrixRow) {
437 foreach($matrixRow as $matrixCell) {
438 if ((is_string($matrixCell)) || ($matrixCell === null)) {
441 $matrixData[$column][
$row] = $matrixCell;
444 if ($column > $maxColumn) { $maxColumn = $column; }
451 return $matrix->det();
465 $matrixData = array();
466 if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); }
468 $row = $maxColumn = 0;
469 foreach($matrixValues as $matrixRow) {
471 foreach($matrixRow as $matrixCell) {
472 if ((is_string($matrixCell)) || ($matrixCell === null)) {
475 $matrixData[$column][
$row] = $matrixCell;
478 if ($column > $maxColumn) { $maxColumn = $column; }
485 return $matrix->inverse()->getArray();
499 public static function MMULT($matrixData1,$matrixData2) {
500 $matrixAData = $matrixBData = array();
501 if (!is_array($matrixData1)) { $matrixData1 = array(array($matrixData1)); }
502 if (!is_array($matrixData2)) { $matrixData2 = array(array($matrixData2)); }
505 foreach($matrixData1 as $matrixRow) {
507 foreach($matrixRow as $matrixCell) {
508 if ((is_string($matrixCell)) || ($matrixCell === null)) {
511 $matrixAData[$rowA][$columnA] = $matrixCell;
519 foreach($matrixData2 as $matrixRow) {
521 foreach($matrixRow as $matrixCell) {
522 if ((is_string($matrixCell)) || ($matrixCell === null)) {
525 $matrixBData[$rowB][$columnB] = $matrixCell;
532 if (($rowA != $columnB) || ($rowB != $columnA)) {
536 return $matrixA->times($matrixB)->getArray();
550 public static function MOD($a = 1, $b = 1) {
556 } elseif (($a < 0.0) && ($b > 0.0)) {
557 return $b - fmod(abs($a),$b);
558 } elseif (($a > 0.0) && ($b < 0.0)) {
559 return $b + fmod($a,abs($b));
575 public static function MROUND($number,$multiple) {
579 if ((is_numeric($number)) && (is_numeric($multiple))) {
580 if ($multiple == 0) {
583 if ((self::SIGN($number)) == (self::SIGN($multiple))) {
584 $multiplier = 1 / $multiple;
585 return round($number * $multiplier) / $multiplier;
607 if (is_numeric($arg)) {
611 $summer += floor($arg);
621 return $summer / $divisor;
635 public static function ODD($number) {
638 if (is_null($number)) {
640 } elseif (is_numeric($number)) {
642 if ($significance == 0) {
647 if ($result == self::EVEN($result)) {
648 $result += $significance;
651 return (
int) $result;
671 if (
$x == 0 &&
$y <= 0) {
700 if ((is_numeric($arg)) && (!is_string($arg))) {
701 if (is_null($returnValue)) {
704 $returnValue *=
$arg;
710 if (is_null($returnValue)) {
738 if ((is_numeric($arg)) && (!is_string($arg))) {
739 if (is_null($returnValue)) {
740 $returnValue = ($arg == 0) ? 0 : $arg;
742 if (($returnValue == 0) || ($arg == 0)) {
745 $returnValue /=
$arg;
752 return intval($returnValue);
763 public static function RAND($min = 0, $max = 0) {
767 if ($min == 0 && $max == 0) {
768 return (rand(0,10000000)) / 10000000;
770 return rand($min, $max);
775 public static function ROMAN($aValue, $style=0) {
778 if ((!is_numeric($aValue)) || ($aValue < 0) || ($aValue >= 4000)) {
785 $mill = Array(
'',
'M',
'MM',
'MMM',
'MMMM',
'MMMMM');
786 $cent = Array(
'',
'C',
'CC',
'CCC',
'CD',
'D',
'DC',
'DCC',
'DCCC',
'CM');
787 $tens = Array(
'',
'X',
'XX',
'XXX',
'XL',
'L',
'LX',
'LXX',
'LXXX',
'XC');
788 $ones = Array(
'',
'I',
'II',
'III',
'IV',
'V',
'VI',
'VII',
'VIII',
'IX');
791 while ($aValue > 5999) {
799 return $roman.$mill[$m].$cent[$c].$tens[
$t].$ones[$aValue];
812 public static function ROUNDUP($number,$digits) {
816 if ((is_numeric($number)) && (is_numeric($digits))) {
817 $significance = pow(10,$digits);
819 return floor($number * $significance) / $significance;
821 return ceil($number * $significance) / $significance;
841 if ((is_numeric($number)) && (is_numeric($digits))) {
842 $significance = pow(10,$digits);
844 return ceil($number * $significance) / $significance;
846 return floor($number * $significance) / $significance;
871 $x = array_shift($aArgs);
872 $n = array_shift($aArgs);
873 $m = array_shift($aArgs);
875 if ((is_numeric(
$x)) && (is_numeric($n)) && (is_numeric($m))) {
878 foreach($aArgs as
$arg) {
880 if ((is_numeric($arg)) && (!is_string($arg))) {
881 $returnValue += $arg * pow(
$x,$n + ($m * $i++));
902 public static function SIGN($number) {
905 if (is_numeric($number)) {
906 if ($number == 0.0) {
909 return $number / abs($number);
926 if (is_numeric($number)) {
930 return sqrt($number * M_PI) ;
950 $subtotal = array_shift($aArgs);
952 if ((is_numeric($subtotal)) && (!is_string($subtotal))) {
1006 public static function SUM() {
1013 if ((is_numeric($arg)) && (!is_string($arg))) {
1014 $returnValue +=
$arg;
1019 return $returnValue;
1037 public static function SUMIF($aArgs,$condition,$sumArgs = array()) {
1043 if (count($sumArgs) == 0) {
1048 foreach ($aArgs as $key =>
$arg) {
1050 $testCondition =
'='.$arg.$condition;
1053 $returnValue += $sumArgs[$key];
1058 return $returnValue;
1069 $arrayList = func_get_args();
1072 $wrkCellCount = count($wrkArray);
1074 foreach($arrayList as $matrixData) {
1076 $count = count($array2);
1077 if ($wrkCellCount != $count) {
1081 foreach ($array2 as $i => $val) {
1082 if (((is_numeric($wrkArray[$i])) && (!is_string($wrkArray[$i]))) &&
1083 ((is_numeric($val)) && (!is_string($val)))) {
1084 $wrkArray[$i] *= $val;
1089 return array_sum($wrkArray);
1113 if ((is_numeric($arg)) && (!is_string($arg))) {
1114 $returnValue += ($arg *
$arg);
1119 return $returnValue;
1129 public static function SUMX2MY2($matrixData1,$matrixData2) {
1132 $count1 = count($array1);
1133 $count2 = count($array2);
1134 if ($count1 < $count2) {
1141 for ($i = 0; $i < $count; ++$i) {
1142 if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
1143 ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
1144 $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
1158 public static function SUMX2PY2($matrixData1,$matrixData2) {
1161 $count1 = count($array1);
1162 $count2 = count($array2);
1163 if ($count1 < $count2) {
1170 for ($i = 0; $i < $count; ++$i) {
1171 if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
1172 ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
1173 $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
1187 public static function SUMXMY2($matrixData1,$matrixData2) {
1190 $count1 = count($array1);
1191 $count2 = count($array2);
1192 if ($count1 < $count2) {
1199 for ($i = 0; $i < $count; ++$i) {
1200 if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
1201 ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
1202 $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
1219 public static function TRUNC($value = 0, $number_digits = 0) {
1224 if ($number_digits < 0) {
1229 if ($number_digits > 0) {
1230 $value = $value * pow(10, $number_digits);
1232 $value = intval($value);
1233 if ($number_digits > 0) {
1234 $value = $value / pow(10, $number_digits);