MathTrig.php 41 KB

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