MathTrig.php 38 KB

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