PageRenderTime 62ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/PHPExcel/Calculation/MathTrig.php

https://bitbucket.org/amorello/changeover
PHP | 1366 lines | 724 code | 156 blank | 486 comment | 209 complexity | 182b8c7bc4a360aa646b717c8748cffc MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.8, 2012-10-12
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  34. }
  35. /**
  36. * PHPExcel_Calculation_MathTrig
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Calculation
  40. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Calculation_MathTrig {
  43. //
  44. // Private method to return an array of the factors of the input value
  45. //
  46. private static function _factors($value) {
  47. $startVal = floor(sqrt($value));
  48. $factorArray = array();
  49. for ($i = $startVal; $i > 1; --$i) {
  50. if (($value % $i) == 0) {
  51. $factorArray = array_merge($factorArray,self::_factors($value / $i));
  52. $factorArray = array_merge($factorArray,self::_factors($i));
  53. if ($i <= sqrt($value)) {
  54. break;
  55. }
  56. }
  57. }
  58. if (!empty($factorArray)) {
  59. rsort($factorArray);
  60. return $factorArray;
  61. } else {
  62. return array((integer) $value);
  63. }
  64. } // function _factors()
  65. private static function _romanCut($num, $n) {
  66. return ($num - ($num % $n ) ) / $n;
  67. } // function _romanCut()
  68. /**
  69. * ATAN2
  70. *
  71. * This function calculates the arc tangent of the two variables x and y. It is similar to
  72. * calculating the arc tangent of y ÷ x, except that the signs of both arguments are used
  73. * to determine the quadrant of the result.
  74. * The arctangent is the angle from the x-axis to a line containing the origin (0, 0) and a
  75. * point with coordinates (xCoordinate, yCoordinate). The angle is given in radians between
  76. * -pi and pi, excluding -pi.
  77. *
  78. * Note that the Excel ATAN2() function accepts its arguments in the reverse order to the standard
  79. * PHP atan2() function, so we need to reverse them here before calling the PHP atan() function.
  80. *
  81. * Excel Function:
  82. * ATAN2(xCoordinate,yCoordinate)
  83. *
  84. * @access public
  85. * @category Mathematical and Trigonometric Functions
  86. * @param float $xCoordinate The x-coordinate of the point.
  87. * @param float $yCoordinate The y-coordinate of the point.
  88. * @return float The inverse tangent of the specified x- and y-coordinates.
  89. */
  90. public static function ATAN2($xCoordinate = NULL, $yCoordinate = NULL) {
  91. $xCoordinate = PHPExcel_Calculation_Functions::flattenSingleValue($xCoordinate);
  92. $yCoordinate = PHPExcel_Calculation_Functions::flattenSingleValue($yCoordinate);
  93. $xCoordinate = ($xCoordinate !== NULL) ? $xCoordinate : 0.0;
  94. $yCoordinate = ($yCoordinate !== NULL) ? $yCoordinate : 0.0;
  95. if (((is_numeric($xCoordinate)) || (is_bool($xCoordinate))) &&
  96. ((is_numeric($yCoordinate))) || (is_bool($yCoordinate))) {
  97. $xCoordinate = (float) $xCoordinate;
  98. $yCoordinate = (float) $yCoordinate;
  99. if (($xCoordinate == 0) && ($yCoordinate == 0)) {
  100. return PHPExcel_Calculation_Functions::DIV0();
  101. }
  102. return atan2($yCoordinate, $xCoordinate);
  103. }
  104. return PHPExcel_Calculation_Functions::VALUE();
  105. } // function ATAN2()
  106. /**
  107. * CEILING
  108. *
  109. * Returns number rounded up, away from zero, to the nearest multiple of significance.
  110. * For example, if you want to avoid using pennies in your prices and your product is
  111. * priced at $4.42, use the formula =CEILING(4.42,0.05) to round prices up to the
  112. * nearest nickel.
  113. *
  114. * Excel Function:
  115. * CEILING(number[,significance])
  116. *
  117. * @access public
  118. * @category Mathematical and Trigonometric Functions
  119. * @param float $number The number you want to round.
  120. * @param float $significance The multiple to which you want to round.
  121. * @return float Rounded Number
  122. */
  123. public static function CEILING($number, $significance = NULL) {
  124. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  125. $significance = PHPExcel_Calculation_Functions::flattenSingleValue($significance);
  126. if ((is_null($significance)) &&
  127. (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) {
  128. $significance = $number/abs($number);
  129. }
  130. if ((is_numeric($number)) && (is_numeric($significance))) {
  131. if ($significance == 0.0) {
  132. return 0.0;
  133. } elseif (self::SIGN($number) == self::SIGN($significance)) {
  134. return ceil($number / $significance) * $significance;
  135. } else {
  136. return PHPExcel_Calculation_Functions::NaN();
  137. }
  138. }
  139. return PHPExcel_Calculation_Functions::VALUE();
  140. } // function CEILING()
  141. /**
  142. * COMBIN
  143. *
  144. * Returns the number of combinations for a given number of items. Use COMBIN to
  145. * determine the total possible number of groups for a given number of items.
  146. *
  147. * Excel Function:
  148. * COMBIN(numObjs,numInSet)
  149. *
  150. * @access public
  151. * @category Mathematical and Trigonometric Functions
  152. * @param int $numObjs Number of different objects
  153. * @param int $numInSet Number of objects in each combination
  154. * @return int Number of combinations
  155. */
  156. public static function COMBIN($numObjs, $numInSet) {
  157. $numObjs = PHPExcel_Calculation_Functions::flattenSingleValue($numObjs);
  158. $numInSet = PHPExcel_Calculation_Functions::flattenSingleValue($numInSet);
  159. if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
  160. if ($numObjs < $numInSet) {
  161. return PHPExcel_Calculation_Functions::NaN();
  162. } elseif ($numInSet < 0) {
  163. return PHPExcel_Calculation_Functions::NaN();
  164. }
  165. return round(self::FACT($numObjs) / self::FACT($numObjs - $numInSet)) / self::FACT($numInSet);
  166. }
  167. return PHPExcel_Calculation_Functions::VALUE();
  168. } // function COMBIN()
  169. /**
  170. * EVEN
  171. *
  172. * Returns number rounded up to the nearest even integer.
  173. * You can use this function for processing items that come in twos. For example,
  174. * a packing crate accepts rows of one or two items. The crate is full when
  175. * the number of items, rounded up to the nearest two, matches the crate's
  176. * capacity.
  177. *
  178. * Excel Function:
  179. * EVEN(number)
  180. *
  181. * @access public
  182. * @category Mathematical and Trigonometric Functions
  183. * @param float $number Number to round
  184. * @return int Rounded Number
  185. */
  186. public static function EVEN($number) {
  187. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  188. if (is_null($number)) {
  189. return 0;
  190. } elseif (is_bool($number)) {
  191. $number = (int) $number;
  192. }
  193. if (is_numeric($number)) {
  194. $significance = 2 * self::SIGN($number);
  195. return (int) self::CEILING($number,$significance);
  196. }
  197. return PHPExcel_Calculation_Functions::VALUE();
  198. } // function EVEN()
  199. /**
  200. * FACT
  201. *
  202. * Returns the factorial of a number.
  203. * The factorial of a number is equal to 1*2*3*...* number.
  204. *
  205. * Excel Function:
  206. * FACT(factVal)
  207. *
  208. * @access public
  209. * @category Mathematical and Trigonometric Functions
  210. * @param float $factVal Factorial Value
  211. * @return int Factorial
  212. */
  213. public static function FACT($factVal) {
  214. $factVal = PHPExcel_Calculation_Functions::flattenSingleValue($factVal);
  215. if (is_numeric($factVal)) {
  216. if ($factVal < 0) {
  217. return PHPExcel_Calculation_Functions::NaN();
  218. }
  219. $factLoop = floor($factVal);
  220. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  221. if ($factVal > $factLoop) {
  222. return PHPExcel_Calculation_Functions::NaN();
  223. }
  224. }
  225. $factorial = 1;
  226. while ($factLoop > 1) {
  227. $factorial *= $factLoop--;
  228. }
  229. return $factorial ;
  230. }
  231. return PHPExcel_Calculation_Functions::VALUE();
  232. } // function FACT()
  233. /**
  234. * FACTDOUBLE
  235. *
  236. * Returns the double factorial of a number.
  237. *
  238. * Excel Function:
  239. * FACTDOUBLE(factVal)
  240. *
  241. * @access public
  242. * @category Mathematical and Trigonometric Functions
  243. * @param float $factVal Factorial Value
  244. * @return int Double Factorial
  245. */
  246. public static function FACTDOUBLE($factVal) {
  247. $factLoop = PHPExcel_Calculation_Functions::flattenSingleValue($factVal);
  248. if (is_numeric($factLoop)) {
  249. $factLoop = floor($factLoop);
  250. if ($factVal < 0) {
  251. return PHPExcel_Calculation_Functions::NaN();
  252. }
  253. $factorial = 1;
  254. while ($factLoop > 1) {
  255. $factorial *= $factLoop--;
  256. --$factLoop;
  257. }
  258. return $factorial ;
  259. }
  260. return PHPExcel_Calculation_Functions::VALUE();
  261. } // function FACTDOUBLE()
  262. /**
  263. * FLOOR
  264. *
  265. * Rounds number down, toward zero, to the nearest multiple of significance.
  266. *
  267. * Excel Function:
  268. * FLOOR(number[,significance])
  269. *
  270. * @access public
  271. * @category Mathematical and Trigonometric Functions
  272. * @param float $number Number to round
  273. * @param float $significance Significance
  274. * @return float Rounded Number
  275. */
  276. public static function FLOOR($number, $significance = NULL) {
  277. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  278. $significance = PHPExcel_Calculation_Functions::flattenSingleValue($significance);
  279. if ((is_null($significance)) && (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) {
  280. $significance = $number/abs($number);
  281. }
  282. if ((is_numeric($number)) && (is_numeric($significance))) {
  283. if ((float) $significance == 0.0) {
  284. return PHPExcel_Calculation_Functions::DIV0();
  285. }
  286. if (self::SIGN($number) == self::SIGN($significance)) {
  287. return floor($number / $significance) * $significance;
  288. } else {
  289. return PHPExcel_Calculation_Functions::NaN();
  290. }
  291. }
  292. return PHPExcel_Calculation_Functions::VALUE();
  293. } // function FLOOR()
  294. /**
  295. * GCD
  296. *
  297. * Returns the greatest common divisor of a series of numbers.
  298. * The greatest common divisor is the largest integer that divides both
  299. * number1 and number2 without a remainder.
  300. *
  301. * Excel Function:
  302. * GCD(number1[,number2[, ...]])
  303. *
  304. * @access public
  305. * @category Mathematical and Trigonometric Functions
  306. * @param mixed $arg,... Data values
  307. * @return integer Greatest Common Divisor
  308. */
  309. public static function GCD() {
  310. $returnValue = 1;
  311. $allValuesFactors = array();
  312. // Loop through arguments
  313. foreach(PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) {
  314. if (!is_numeric($value)) {
  315. return PHPExcel_Calculation_Functions::VALUE();
  316. } elseif ($value == 0) {
  317. continue;
  318. } elseif($value < 0) {
  319. return PHPExcel_Calculation_Functions::NaN();
  320. }
  321. $myFactors = self::_factors($value);
  322. $myCountedFactors = array_count_values($myFactors);
  323. $allValuesFactors[] = $myCountedFactors;
  324. }
  325. $allValuesCount = count($allValuesFactors);
  326. if ($allValuesCount == 0) {
  327. return 0;
  328. }
  329. $mergedArray = $allValuesFactors[0];
  330. for ($i=1;$i < $allValuesCount; ++$i) {
  331. $mergedArray = array_intersect_key($mergedArray,$allValuesFactors[$i]);
  332. }
  333. $mergedArrayValues = count($mergedArray);
  334. if ($mergedArrayValues == 0) {
  335. return $returnValue;
  336. } elseif ($mergedArrayValues > 1) {
  337. foreach($mergedArray as $mergedKey => $mergedValue) {
  338. foreach($allValuesFactors as $highestPowerTest) {
  339. foreach($highestPowerTest as $testKey => $testValue) {
  340. if (($testKey == $mergedKey) && ($testValue < $mergedValue)) {
  341. $mergedArray[$mergedKey] = $testValue;
  342. $mergedValue = $testValue;
  343. }
  344. }
  345. }
  346. }
  347. $returnValue = 1;
  348. foreach($mergedArray as $key => $value) {
  349. $returnValue *= pow($key,$value);
  350. }
  351. return $returnValue;
  352. } else {
  353. $keys = array_keys($mergedArray);
  354. $key = $keys[0];
  355. $value = $mergedArray[$key];
  356. foreach($allValuesFactors as $testValue) {
  357. foreach($testValue as $mergedKey => $mergedValue) {
  358. if (($mergedKey == $key) && ($mergedValue < $value)) {
  359. $value = $mergedValue;
  360. }
  361. }
  362. }
  363. return pow($key,$value);
  364. }
  365. } // function GCD()
  366. /**
  367. * INT
  368. *
  369. * Casts a floating point value to an integer
  370. *
  371. * Excel Function:
  372. * INT(number)
  373. *
  374. * @access public
  375. * @category Mathematical and Trigonometric Functions
  376. * @param float $number Number to cast to an integer
  377. * @return integer Integer value
  378. */
  379. public static function INT($number) {
  380. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  381. if (is_null($number)) {
  382. return 0;
  383. } elseif (is_bool($number)) {
  384. return (int) $number;
  385. }
  386. if (is_numeric($number)) {
  387. return (int) floor($number);
  388. }
  389. return PHPExcel_Calculation_Functions::VALUE();
  390. } // function INT()
  391. /**
  392. * LCM
  393. *
  394. * Returns the lowest common multiplier of a series of numbers
  395. * The least common multiple is the smallest positive integer that is a multiple
  396. * of all integer arguments number1, number2, and so on. Use LCM to add fractions
  397. * with different denominators.
  398. *
  399. * Excel Function:
  400. * LCM(number1[,number2[, ...]])
  401. *
  402. * @access public
  403. * @category Mathematical and Trigonometric Functions
  404. * @param mixed $arg,... Data values
  405. * @return int Lowest Common Multiplier
  406. */
  407. public static function LCM() {
  408. $returnValue = 1;
  409. $allPoweredFactors = array();
  410. // Loop through arguments
  411. foreach(PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) {
  412. if (!is_numeric($value)) {
  413. return PHPExcel_Calculation_Functions::VALUE();
  414. }
  415. if ($value == 0) {
  416. return 0;
  417. } elseif ($value < 0) {
  418. return PHPExcel_Calculation_Functions::NaN();
  419. }
  420. $myFactors = self::_factors(floor($value));
  421. $myCountedFactors = array_count_values($myFactors);
  422. $myPoweredFactors = array();
  423. foreach($myCountedFactors as $myCountedFactor => $myCountedPower) {
  424. $myPoweredFactors[$myCountedFactor] = pow($myCountedFactor,$myCountedPower);
  425. }
  426. foreach($myPoweredFactors as $myPoweredValue => $myPoweredFactor) {
  427. if (array_key_exists($myPoweredValue,$allPoweredFactors)) {
  428. if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) {
  429. $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
  430. }
  431. } else {
  432. $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
  433. }
  434. }
  435. }
  436. foreach($allPoweredFactors as $allPoweredFactor) {
  437. $returnValue *= (integer) $allPoweredFactor;
  438. }
  439. return $returnValue;
  440. } // function LCM()
  441. /**
  442. * LOG_BASE
  443. *
  444. * Returns the logarithm of a number to a specified base. The default base is 10.
  445. *
  446. * Excel Function:
  447. * LOG(number[,base])
  448. *
  449. * @access public
  450. * @category Mathematical and Trigonometric Functions
  451. * @param float $value The positive real number for which you want the logarithm
  452. * @param float $base The base of the logarithm. If base is omitted, it is assumed to be 10.
  453. * @return float
  454. */
  455. public static function LOG_BASE($number = NULL, $base = 10) {
  456. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  457. $base = (is_null($base)) ? 10 : (float) PHPExcel_Calculation_Functions::flattenSingleValue($base);
  458. if ((!is_numeric($base)) || (!is_numeric($number)))
  459. return PHPExcel_Calculation_Functions::VALUE();
  460. if (($base <= 0) || ($number <= 0))
  461. return PHPExcel_Calculation_Functions::NaN();
  462. return log($number, $base);
  463. } // function LOG_BASE()
  464. /**
  465. * MDETERM
  466. *
  467. * Returns the matrix determinant of an array.
  468. *
  469. * Excel Function:
  470. * MDETERM(array)
  471. *
  472. * @access public
  473. * @category Mathematical and Trigonometric Functions
  474. * @param array $matrixValues A matrix of values
  475. * @return float
  476. */
  477. public static function MDETERM($matrixValues) {
  478. $matrixData = array();
  479. if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); }
  480. $row = $maxColumn = 0;
  481. foreach($matrixValues as $matrixRow) {
  482. if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); }
  483. $column = 0;
  484. foreach($matrixRow as $matrixCell) {
  485. if ((is_string($matrixCell)) || ($matrixCell === null)) {
  486. return PHPExcel_Calculation_Functions::VALUE();
  487. }
  488. $matrixData[$column][$row] = $matrixCell;
  489. ++$column;
  490. }
  491. if ($column > $maxColumn) { $maxColumn = $column; }
  492. ++$row;
  493. }
  494. if ($row != $maxColumn) { return PHPExcel_Calculation_Functions::VALUE(); }
  495. try {
  496. $matrix = new PHPExcel_Shared_JAMA_Matrix($matrixData);
  497. return $matrix->det();
  498. } catch (Exception $ex) {
  499. return PHPExcel_Calculation_Functions::VALUE();
  500. }
  501. } // function MDETERM()
  502. /**
  503. * MINVERSE
  504. *
  505. * Returns the inverse matrix for the matrix stored in an array.
  506. *
  507. * Excel Function:
  508. * MINVERSE(array)
  509. *
  510. * @access public
  511. * @category Mathematical and Trigonometric Functions
  512. * @param array $matrixValues A matrix of values
  513. * @return array
  514. */
  515. public static function MINVERSE($matrixValues) {
  516. $matrixData = array();
  517. if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); }
  518. $row = $maxColumn = 0;
  519. foreach($matrixValues as $matrixRow) {
  520. if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); }
  521. $column = 0;
  522. foreach($matrixRow as $matrixCell) {
  523. if ((is_string($matrixCell)) || ($matrixCell === null)) {
  524. return PHPExcel_Calculation_Functions::VALUE();
  525. }
  526. $matrixData[$column][$row] = $matrixCell;
  527. ++$column;
  528. }
  529. if ($column > $maxColumn) { $maxColumn = $column; }
  530. ++$row;
  531. }
  532. if ($row != $maxColumn) { return PHPExcel_Calculation_Functions::VALUE(); }
  533. try {
  534. $matrix = new PHPExcel_Shared_JAMA_Matrix($matrixData);
  535. return $matrix->inverse()->getArray();
  536. } catch (Exception $ex) {
  537. return PHPExcel_Calculation_Functions::VALUE();
  538. }
  539. } // function MINVERSE()
  540. /**
  541. * MMULT
  542. *
  543. * @param array $matrixData1 A matrix of values
  544. * @param array $matrixData2 A matrix of values
  545. * @return array
  546. */
  547. public static function MMULT($matrixData1,$matrixData2) {
  548. $matrixAData = $matrixBData = array();
  549. if (!is_array($matrixData1)) { $matrixData1 = array(array($matrixData1)); }
  550. if (!is_array($matrixData2)) { $matrixData2 = array(array($matrixData2)); }
  551. $rowA = 0;
  552. foreach($matrixData1 as $matrixRow) {
  553. if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); }
  554. $columnA = 0;
  555. foreach($matrixRow as $matrixCell) {
  556. if ((is_string($matrixCell)) || ($matrixCell === null)) {
  557. return PHPExcel_Calculation_Functions::VALUE();
  558. }
  559. $matrixAData[$rowA][$columnA] = $matrixCell;
  560. ++$columnA;
  561. }
  562. ++$rowA;
  563. }
  564. try {
  565. $matrixA = new PHPExcel_Shared_JAMA_Matrix($matrixAData);
  566. $rowB = 0;
  567. foreach($matrixData2 as $matrixRow) {
  568. if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); }
  569. $columnB = 0;
  570. foreach($matrixRow as $matrixCell) {
  571. if ((is_string($matrixCell)) || ($matrixCell === null)) {
  572. return PHPExcel_Calculation_Functions::VALUE();
  573. }
  574. $matrixBData[$rowB][$columnB] = $matrixCell;
  575. ++$columnB;
  576. }
  577. ++$rowB;
  578. }
  579. $matrixB = new PHPExcel_Shared_JAMA_Matrix($matrixBData);
  580. if (($rowA != $columnB) || ($rowB != $columnA)) {
  581. return PHPExcel_Calculation_Functions::VALUE();
  582. }
  583. return $matrixA->times($matrixB)->getArray();
  584. } catch (Exception $ex) {
  585. return PHPExcel_Calculation_Functions::VALUE();
  586. }
  587. } // function MMULT()
  588. /**
  589. * MOD
  590. *
  591. * @param int $a Dividend
  592. * @param int $b Divisor
  593. * @return int Remainder
  594. */
  595. public static function MOD($a = 1, $b = 1) {
  596. $a = PHPExcel_Calculation_Functions::flattenSingleValue($a);
  597. $b = PHPExcel_Calculation_Functions::flattenSingleValue($b);
  598. if ($b == 0.0) {
  599. return PHPExcel_Calculation_Functions::DIV0();
  600. } elseif (($a < 0.0) && ($b > 0.0)) {
  601. return $b - fmod(abs($a),$b);
  602. } elseif (($a > 0.0) && ($b < 0.0)) {
  603. return $b + fmod($a,abs($b));
  604. }
  605. return fmod($a,$b);
  606. } // function MOD()
  607. /**
  608. * MROUND
  609. *
  610. * Rounds a number to the nearest multiple of a specified value
  611. *
  612. * @param float $number Number to round
  613. * @param int $multiple Multiple to which you want to round $number
  614. * @return float Rounded Number
  615. */
  616. public static function MROUND($number,$multiple) {
  617. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  618. $multiple = PHPExcel_Calculation_Functions::flattenSingleValue($multiple);
  619. if ((is_numeric($number)) && (is_numeric($multiple))) {
  620. if ($multiple == 0) {
  621. return 0;
  622. }
  623. if ((self::SIGN($number)) == (self::SIGN($multiple))) {
  624. $multiplier = 1 / $multiple;
  625. return round($number * $multiplier) / $multiplier;
  626. }
  627. return PHPExcel_Calculation_Functions::NaN();
  628. }
  629. return PHPExcel_Calculation_Functions::VALUE();
  630. } // function MROUND()
  631. /**
  632. * MULTINOMIAL
  633. *
  634. * Returns the ratio of the factorial of a sum of values to the product of factorials.
  635. *
  636. * @param array of mixed Data Series
  637. * @return float
  638. */
  639. public static function MULTINOMIAL() {
  640. $summer = 0;
  641. $divisor = 1;
  642. // Loop through arguments
  643. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  644. // Is it a numeric value?
  645. if (is_numeric($arg)) {
  646. if ($arg < 1) {
  647. return PHPExcel_Calculation_Functions::NaN();
  648. }
  649. $summer += floor($arg);
  650. $divisor *= self::FACT($arg);
  651. } else {
  652. return PHPExcel_Calculation_Functions::VALUE();
  653. }
  654. }
  655. // Return
  656. if ($summer > 0) {
  657. $summer = self::FACT($summer);
  658. return $summer / $divisor;
  659. }
  660. return 0;
  661. } // function MULTINOMIAL()
  662. /**
  663. * ODD
  664. *
  665. * Returns number rounded up to the nearest odd integer.
  666. *
  667. * @param float $number Number to round
  668. * @return int Rounded Number
  669. */
  670. public static function ODD($number) {
  671. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  672. if (is_null($number)) {
  673. return 1;
  674. } elseif (is_bool($number)) {
  675. $number = (int) $number;
  676. }
  677. if (is_numeric($number)) {
  678. $significance = self::SIGN($number);
  679. if ($significance == 0) {
  680. return 1;
  681. }
  682. $result = self::CEILING($number,$significance);
  683. if ($result == self::EVEN($result)) {
  684. $result += $significance;
  685. }
  686. return (int) $result;
  687. }
  688. return PHPExcel_Calculation_Functions::VALUE();
  689. } // function ODD()
  690. /**
  691. * POWER
  692. *
  693. * Computes x raised to the power y.
  694. *
  695. * @param float $x
  696. * @param float $y
  697. * @return float
  698. */
  699. public static function POWER($x = 0, $y = 2) {
  700. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  701. $y = PHPExcel_Calculation_Functions::flattenSingleValue($y);
  702. // Validate parameters
  703. if ($x == 0.0 && $y == 0.0) {
  704. return PHPExcel_Calculation_Functions::NaN();
  705. } elseif ($x == 0.0 && $y < 0.0) {
  706. return PHPExcel_Calculation_Functions::DIV0();
  707. }
  708. // Return
  709. $result = pow($x, $y);
  710. return (!is_nan($result) && !is_infinite($result)) ? $result : PHPExcel_Calculation_Functions::NaN();
  711. } // function POWER()
  712. /**
  713. * PRODUCT
  714. *
  715. * PRODUCT returns the product of all the values and cells referenced in the argument list.
  716. *
  717. * Excel Function:
  718. * PRODUCT(value1[,value2[, ...]])
  719. *
  720. * @access public
  721. * @category Mathematical and Trigonometric Functions
  722. * @param mixed $arg,... Data values
  723. * @return float
  724. */
  725. public static function PRODUCT() {
  726. // Return value
  727. $returnValue = null;
  728. // Loop through arguments
  729. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  730. // Is it a numeric value?
  731. if ((is_numeric($arg)) && (!is_string($arg))) {
  732. if (is_null($returnValue)) {
  733. $returnValue = $arg;
  734. } else {
  735. $returnValue *= $arg;
  736. }
  737. }
  738. }
  739. // Return
  740. if (is_null($returnValue)) {
  741. return 0;
  742. }
  743. return $returnValue;
  744. } // function PRODUCT()
  745. /**
  746. * QUOTIENT
  747. *
  748. * QUOTIENT function returns the integer portion of a division. Numerator is the divided number
  749. * and denominator is the divisor.
  750. *
  751. * Excel Function:
  752. * QUOTIENT(value1[,value2[, ...]])
  753. *
  754. * @access public
  755. * @category Mathematical and Trigonometric Functions
  756. * @param mixed $arg,... Data values
  757. * @return float
  758. */
  759. public static function QUOTIENT() {
  760. // Return value
  761. $returnValue = null;
  762. // Loop through arguments
  763. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  764. // Is it a numeric value?
  765. if ((is_numeric($arg)) && (!is_string($arg))) {
  766. if (is_null($returnValue)) {
  767. $returnValue = ($arg == 0) ? 0 : $arg;
  768. } else {
  769. if (($returnValue == 0) || ($arg == 0)) {
  770. $returnValue = 0;
  771. } else {
  772. $returnValue /= $arg;
  773. }
  774. }
  775. }
  776. }
  777. // Return
  778. return intval($returnValue);
  779. } // function QUOTIENT()
  780. /**
  781. * RAND
  782. *
  783. * @param int $min Minimal value
  784. * @param int $max Maximal value
  785. * @return int Random number
  786. */
  787. public static function RAND($min = 0, $max = 0) {
  788. $min = PHPExcel_Calculation_Functions::flattenSingleValue($min);
  789. $max = PHPExcel_Calculation_Functions::flattenSingleValue($max);
  790. if ($min == 0 && $max == 0) {
  791. return (rand(0,10000000)) / 10000000;
  792. } else {
  793. return rand($min, $max);
  794. }
  795. } // function RAND()
  796. public static function ROMAN($aValue, $style=0) {
  797. $aValue = PHPExcel_Calculation_Functions::flattenSingleValue($aValue);
  798. $style = (is_null($style)) ? 0 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($style);
  799. if ((!is_numeric($aValue)) || ($aValue < 0) || ($aValue >= 4000)) {
  800. return PHPExcel_Calculation_Functions::VALUE();
  801. }
  802. $aValue = (integer) $aValue;
  803. if ($aValue == 0) {
  804. return '';
  805. }
  806. $mill = Array('', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM');
  807. $cent = Array('', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM');
  808. $tens = Array('', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC');
  809. $ones = Array('', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX');
  810. $roman = '';
  811. while ($aValue > 5999) {
  812. $roman .= 'M';
  813. $aValue -= 1000;
  814. }
  815. $m = self::_romanCut($aValue, 1000); $aValue %= 1000;
  816. $c = self::_romanCut($aValue, 100); $aValue %= 100;
  817. $t = self::_romanCut($aValue, 10); $aValue %= 10;
  818. return $roman.$mill[$m].$cent[$c].$tens[$t].$ones[$aValue];
  819. } // function ROMAN()
  820. /**
  821. * ROUNDUP
  822. *
  823. * Rounds a number up to a specified number of decimal places
  824. *
  825. * @param float $number Number to round
  826. * @param int $digits Number of digits to which you want to round $number
  827. * @return float Rounded Number
  828. */
  829. public static function ROUNDUP($number,$digits) {
  830. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  831. $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits);
  832. if ((is_numeric($number)) && (is_numeric($digits))) {
  833. $significance = pow(10,(int) $digits);
  834. if ($number < 0.0) {
  835. return floor($number * $significance) / $significance;
  836. } else {
  837. return ceil($number * $significance) / $significance;
  838. }
  839. }
  840. return PHPExcel_Calculation_Functions::VALUE();
  841. } // function ROUNDUP()
  842. /**
  843. * ROUNDDOWN
  844. *
  845. * Rounds a number down to a specified number of decimal places
  846. *
  847. * @param float $number Number to round
  848. * @param int $digits Number of digits to which you want to round $number
  849. * @return float Rounded Number
  850. */
  851. public static function ROUNDDOWN($number,$digits) {
  852. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  853. $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits);
  854. if ((is_numeric($number)) && (is_numeric($digits))) {
  855. $significance = pow(10,(int) $digits);
  856. if ($number < 0.0) {
  857. return ceil($number * $significance) / $significance;
  858. } else {
  859. return floor($number * $significance) / $significance;
  860. }
  861. }
  862. return PHPExcel_Calculation_Functions::VALUE();
  863. } // function ROUNDDOWN()
  864. /**
  865. * SERIESSUM
  866. *
  867. * Returns the sum of a power series
  868. *
  869. * @param float $x Input value to the power series
  870. * @param float $n Initial power to which you want to raise $x
  871. * @param float $m Step by which to increase $n for each term in the series
  872. * @param array of mixed Data Series
  873. * @return float
  874. */
  875. public static function SERIESSUM() {
  876. // Return value
  877. $returnValue = 0;
  878. // Loop through arguments
  879. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  880. $x = array_shift($aArgs);
  881. $n = array_shift($aArgs);
  882. $m = array_shift($aArgs);
  883. if ((is_numeric($x)) && (is_numeric($n)) && (is_numeric($m))) {
  884. // Calculate
  885. $i = 0;
  886. foreach($aArgs as $arg) {
  887. // Is it a numeric value?
  888. if ((is_numeric($arg)) && (!is_string($arg))) {
  889. $returnValue += $arg * pow($x,$n + ($m * $i++));
  890. } else {
  891. return PHPExcel_Calculation_Functions::VALUE();
  892. }
  893. }
  894. // Return
  895. return $returnValue;
  896. }
  897. return PHPExcel_Calculation_Functions::VALUE();
  898. } // function SERIESSUM()
  899. /**
  900. * SIGN
  901. *
  902. * Determines the sign of a number. Returns 1 if the number is positive, zero (0)
  903. * if the number is 0, and -1 if the number is negative.
  904. *
  905. * @param float $number Number to round
  906. * @return int sign value
  907. */
  908. public static function SIGN($number) {
  909. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  910. if (is_bool($number))
  911. return (int) $number;
  912. if (is_numeric($number)) {
  913. if ($number == 0.0) {
  914. return 0;
  915. }
  916. return $number / abs($number);
  917. }
  918. return PHPExcel_Calculation_Functions::VALUE();
  919. } // function SIGN()
  920. /**
  921. * SQRTPI
  922. *
  923. * Returns the square root of (number * pi).
  924. *
  925. * @param float $number Number
  926. * @return float Square Root of Number * Pi
  927. */
  928. public static function SQRTPI($number) {
  929. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  930. if (is_numeric($number)) {
  931. if ($number < 0) {
  932. return PHPExcel_Calculation_Functions::NaN();
  933. }
  934. return sqrt($number * M_PI) ;
  935. }
  936. return PHPExcel_Calculation_Functions::VALUE();
  937. } // function SQRTPI()
  938. /**
  939. * SUBTOTAL
  940. *
  941. * Returns a subtotal in a list or database.
  942. *
  943. * @param int the number 1 to 11 that specifies which function to
  944. * use in calculating subtotals within a list.
  945. * @param array of mixed Data Series
  946. * @return float
  947. */
  948. public static function SUBTOTAL() {
  949. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  950. // Calculate
  951. $subtotal = array_shift($aArgs);
  952. if ((is_numeric($subtotal)) && (!is_string($subtotal))) {
  953. switch($subtotal) {
  954. case 1 :
  955. return PHPExcel_Calculation_Statistical::AVERAGE($aArgs);
  956. break;
  957. case 2 :
  958. return PHPExcel_Calculation_Statistical::COUNT($aArgs);
  959. break;
  960. case 3 :
  961. return PHPExcel_Calculation_Statistical::COUNTA($aArgs);
  962. break;
  963. case 4 :
  964. return PHPExcel_Calculation_Statistical::MAX($aArgs);
  965. break;
  966. case 5 :
  967. return PHPExcel_Calculation_Statistical::MIN($aArgs);
  968. break;
  969. case 6 :
  970. return self::PRODUCT($aArgs);
  971. break;
  972. case 7 :
  973. return PHPExcel_Calculation_Statistical::STDEV($aArgs);
  974. break;
  975. case 8 :
  976. return PHPExcel_Calculation_Statistical::STDEVP($aArgs);
  977. break;
  978. case 9 :
  979. return self::SUM($aArgs);
  980. break;
  981. case 10 :
  982. return PHPExcel_Calculation_Statistical::VARFunc($aArgs);
  983. break;
  984. case 11 :
  985. return PHPExcel_Calculation_Statistical::VARP($aArgs);
  986. break;
  987. }
  988. }
  989. return PHPExcel_Calculation_Functions::VALUE();
  990. } // function SUBTOTAL()
  991. /**
  992. * SUM
  993. *
  994. * SUM computes the sum of all the values and cells referenced in the argument list.
  995. *
  996. * Excel Function:
  997. * SUM(value1[,value2[, ...]])
  998. *
  999. * @access public
  1000. * @category Mathematical and Trigonometric Functions
  1001. * @param mixed $arg,... Data values
  1002. * @return float
  1003. */
  1004. public static function SUM() {
  1005. // Return value
  1006. $returnValue = 0;
  1007. // Loop through the arguments
  1008. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  1009. // Is it a numeric value?
  1010. if ((is_numeric($arg)) && (!is_string($arg))) {
  1011. $returnValue += $arg;
  1012. }
  1013. }
  1014. // Return
  1015. return $returnValue;
  1016. } // function SUM()
  1017. /**
  1018. * SUMIF
  1019. *
  1020. * Counts the number of cells that contain numbers within the list of arguments
  1021. *
  1022. * Excel Function:
  1023. * SUMIF(value1[,value2[, ...]],condition)
  1024. *
  1025. * @access public
  1026. * @category Mathematical and Trigonometric Functions
  1027. * @param mixed $arg,... Data values
  1028. * @param string $condition The criteria that defines which cells will be summed.
  1029. * @return float
  1030. */
  1031. public static function SUMIF($aArgs,$condition,$sumArgs = array()) {
  1032. // Return value
  1033. $returnValue = 0;
  1034. $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs);
  1035. $sumArgs = PHPExcel_Calculation_Functions::flattenArray($sumArgs);
  1036. if (empty($sumArgs)) {
  1037. $sumArgs = $aArgs;
  1038. }
  1039. $condition = PHPExcel_Calculation_Functions::_ifCondition($condition);
  1040. // Loop through arguments
  1041. foreach ($aArgs as $key => $arg) {
  1042. if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); }
  1043. $testCondition = '='.$arg.$condition;
  1044. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1045. // Is it a value within our criteria
  1046. $returnValue += $sumArgs[$key];
  1047. }
  1048. }
  1049. // Return
  1050. return $returnValue;
  1051. } // function SUMIF()
  1052. /**
  1053. * SUMPRODUCT
  1054. *
  1055. * Excel Function:
  1056. * SUMPRODUCT(value1[,value2[, ...]])
  1057. *
  1058. * @access public
  1059. * @category Mathematical and Trigonometric Functions
  1060. * @param mixed $arg,... Data values
  1061. * @return float
  1062. */
  1063. public static function SUMPRODUCT() {
  1064. $arrayList = func_get_args();
  1065. $wrkArray = PHPExcel_Calculation_Functions::flattenArray(array_shift($arrayList));
  1066. $wrkCellCount = count($wrkArray);
  1067. for ($i=0; $i< $wrkCellCount; ++$i) {
  1068. if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) {
  1069. $wrkArray[$i] = 0;
  1070. }
  1071. }
  1072. foreach($arrayList as $matrixData) {
  1073. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData);
  1074. $count = count($array2);
  1075. if ($wrkCellCount != $count) {
  1076. return PHPExcel_Calculation_Functions::VALUE();
  1077. }
  1078. foreach ($array2 as $i => $val) {
  1079. if ((!is_numeric($val)) || (is_string($val))) {
  1080. $val = 0;
  1081. }
  1082. $wrkArray[$i] *= $val;
  1083. }
  1084. }
  1085. return array_sum($wrkArray);
  1086. } // function SUMPRODUCT()
  1087. /**
  1088. * SUMSQ
  1089. *
  1090. * SUMSQ returns the sum of the squares of the arguments
  1091. *
  1092. * Excel Function:
  1093. * SUMSQ(value1[,value2[, ...]])
  1094. *
  1095. * @access public
  1096. * @category Mathematical and Trigonometric Functions
  1097. * @param mixed $arg,... Data values
  1098. * @return float
  1099. */
  1100. public static function SUMSQ() {
  1101. // Return value
  1102. $returnValue = 0;
  1103. // Loop through arguments
  1104. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  1105. // Is it a numeric value?
  1106. if ((is_numeric($arg)) && (!is_string($arg))) {
  1107. $returnValue += ($arg * $arg);
  1108. }
  1109. }
  1110. // Return
  1111. return $returnValue;
  1112. } // function SUMSQ()
  1113. /**
  1114. * SUMX2MY2
  1115. *
  1116. * @param mixed $value Value to check
  1117. * @return float
  1118. */
  1119. public static function SUMX2MY2($matrixData1,$matrixData2) {
  1120. $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1121. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1122. $count1 = count($array1);
  1123. $count2 = count($array2);
  1124. if ($count1 < $count2) {
  1125. $count = $count1;
  1126. } else {
  1127. $count = $count2;
  1128. }
  1129. $result = 0;
  1130. for ($i = 0; $i < $count; ++$i) {
  1131. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1132. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1133. $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
  1134. }
  1135. }
  1136. return $result;
  1137. } // function SUMX2MY2()
  1138. /**
  1139. * SUMX2PY2
  1140. *
  1141. * @param mixed $value Value to check
  1142. * @return float
  1143. */
  1144. public static function SUMX2PY2($matrixData1,$matrixData2) {
  1145. $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1146. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1147. $count1 = count($array1);
  1148. $count2 = count($array2);
  1149. if ($count1 < $count2) {
  1150. $count = $count1;
  1151. } else {
  1152. $count = $count2;
  1153. }
  1154. $result = 0;
  1155. for ($i = 0; $i < $count; ++$i) {
  1156. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1157. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1158. $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
  1159. }
  1160. }
  1161. return $result;
  1162. } // function SUMX2PY2()
  1163. /**
  1164. * SUMXMY2
  1165. *
  1166. * @param mixed $value Value to check
  1167. * @return float
  1168. */
  1169. public static function SUMXMY2($matrixData1,$matrixData2) {
  1170. $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1171. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1172. $count1 = count($array1);
  1173. $count2 = count($array2);
  1174. if ($count1 < $count2) {
  1175. $count = $count1;
  1176. } else {
  1177. $count = $count2;
  1178. }
  1179. $result = 0;
  1180. for ($i = 0; $i < $count; ++$i) {
  1181. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1182. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1183. $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
  1184. }
  1185. }
  1186. return $result;
  1187. } // function SUMXMY2()
  1188. /**
  1189. * TRUNC
  1190. *
  1191. * Truncates value to the number of fractional digits by number_digits.
  1192. *
  1193. * @param float $value
  1194. * @param int $digits
  1195. * @return float Truncated value
  1196. */
  1197. public static function TRUNC($value = 0, $digits = 0) {
  1198. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1199. $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits);
  1200. // Validate parameters
  1201. if ((!is_numeric($value)) || (!is_numeric($digits)))
  1202. return PHPExcel_Calculation_Functions::VALUE();
  1203. $digits = floor($digits);
  1204. // Truncate
  1205. $adjust = pow(10, $digits);
  1206. if (($digits > 0) && (rtrim(intval((abs($value) - abs(intval($value))) * $adjust),'0') < $adjust/10))
  1207. return $value;
  1208. return (intval($value * $adjust)) / $adjust;
  1209. } // function TRUNC()
  1210. } // class PHPExcel_Calculation_MathTrig