MathTrig.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  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 - 2014 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 $number 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 (PHPExcel_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 (PHPExcel_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 (PHPExcel_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)) {
  1043. $arg = str_replace('"', '""', $arg);
  1044. $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg));
  1045. }
  1046. $testCondition = '='.$arg.$condition;
  1047. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1048. // Is it a value within our criteria
  1049. $returnValue += $sumArgs[$key];
  1050. }
  1051. }
  1052. // Return
  1053. return $returnValue;
  1054. } // function SUMIF()
  1055. /**
  1056. * SUMPRODUCT
  1057. *
  1058. * Excel Function:
  1059. * SUMPRODUCT(value1[,value2[, ...]])
  1060. *
  1061. * @access public
  1062. * @category Mathematical and Trigonometric Functions
  1063. * @param mixed $arg,... Data values
  1064. * @return float
  1065. */
  1066. public static function SUMPRODUCT() {
  1067. $arrayList = func_get_args();
  1068. $wrkArray = PHPExcel_Calculation_Functions::flattenArray(array_shift($arrayList));
  1069. $wrkCellCount = count($wrkArray);
  1070. for ($i=0; $i< $wrkCellCount; ++$i) {
  1071. if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) {
  1072. $wrkArray[$i] = 0;
  1073. }
  1074. }
  1075. foreach($arrayList as $matrixData) {
  1076. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData);
  1077. $count = count($array2);
  1078. if ($wrkCellCount != $count) {
  1079. return PHPExcel_Calculation_Functions::VALUE();
  1080. }
  1081. foreach ($array2 as $i => $val) {
  1082. if ((!is_numeric($val)) || (is_string($val))) {
  1083. $val = 0;
  1084. }
  1085. $wrkArray[$i] *= $val;
  1086. }
  1087. }
  1088. return array_sum($wrkArray);
  1089. } // function SUMPRODUCT()
  1090. /**
  1091. * SUMSQ
  1092. *
  1093. * SUMSQ returns the sum of the squares of the arguments
  1094. *
  1095. * Excel Function:
  1096. * SUMSQ(value1[,value2[, ...]])
  1097. *
  1098. * @access public
  1099. * @category Mathematical and Trigonometric Functions
  1100. * @param mixed $arg,... Data values
  1101. * @return float
  1102. */
  1103. public static function SUMSQ() {
  1104. // Return value
  1105. $returnValue = 0;
  1106. // Loop through arguments
  1107. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  1108. // Is it a numeric value?
  1109. if ((is_numeric($arg)) && (!is_string($arg))) {
  1110. $returnValue += ($arg * $arg);
  1111. }
  1112. }
  1113. // Return
  1114. return $returnValue;
  1115. } // function SUMSQ()
  1116. /**
  1117. * SUMX2MY2
  1118. *
  1119. * @param mixed[] $matrixData1 Matrix #1
  1120. * @param mixed[] $matrixData2 Matrix #2
  1121. * @return float
  1122. */
  1123. public static function SUMX2MY2($matrixData1,$matrixData2) {
  1124. $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1125. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1126. $count1 = count($array1);
  1127. $count2 = count($array2);
  1128. if ($count1 < $count2) {
  1129. $count = $count1;
  1130. } else {
  1131. $count = $count2;
  1132. }
  1133. $result = 0;
  1134. for ($i = 0; $i < $count; ++$i) {
  1135. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1136. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1137. $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
  1138. }
  1139. }
  1140. return $result;
  1141. } // function SUMX2MY2()
  1142. /**
  1143. * SUMX2PY2
  1144. *
  1145. * @param mixed[] $matrixData1 Matrix #1
  1146. * @param mixed[] $matrixData2 Matrix #2
  1147. * @return float
  1148. */
  1149. public static function SUMX2PY2($matrixData1,$matrixData2) {
  1150. $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1151. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1152. $count1 = count($array1);
  1153. $count2 = count($array2);
  1154. if ($count1 < $count2) {
  1155. $count = $count1;
  1156. } else {
  1157. $count = $count2;
  1158. }
  1159. $result = 0;
  1160. for ($i = 0; $i < $count; ++$i) {
  1161. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1162. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1163. $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
  1164. }
  1165. }
  1166. return $result;
  1167. } // function SUMX2PY2()
  1168. /**
  1169. * SUMXMY2
  1170. *
  1171. * @param mixed[] $matrixData1 Matrix #1
  1172. * @param mixed[] $matrixData2 Matrix #2
  1173. * @return float
  1174. */
  1175. public static function SUMXMY2($matrixData1,$matrixData2) {
  1176. $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1177. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1178. $count1 = count($array1);
  1179. $count2 = count($array2);
  1180. if ($count1 < $count2) {
  1181. $count = $count1;
  1182. } else {
  1183. $count = $count2;
  1184. }
  1185. $result = 0;
  1186. for ($i = 0; $i < $count; ++$i) {
  1187. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1188. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1189. $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
  1190. }
  1191. }
  1192. return $result;
  1193. } // function SUMXMY2()
  1194. /**
  1195. * TRUNC
  1196. *
  1197. * Truncates value to the number of fractional digits by number_digits.
  1198. *
  1199. * @param float $value
  1200. * @param int $digits
  1201. * @return float Truncated value
  1202. */
  1203. public static function TRUNC($value = 0, $digits = 0) {
  1204. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1205. $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits);
  1206. // Validate parameters
  1207. if ((!is_numeric($value)) || (!is_numeric($digits)))
  1208. return PHPExcel_Calculation_Functions::VALUE();
  1209. $digits = floor($digits);
  1210. // Truncate
  1211. $adjust = pow(10, $digits);
  1212. if (($digits > 0) && (rtrim(intval((abs($value) - abs(intval($value))) * $adjust),'0') < $adjust/10))
  1213. return $value;
  1214. return (intval($value * $adjust)) / $adjust;
  1215. } // function TRUNC()
  1216. } // class PHPExcel_Calculation_MathTrig