1b0f7defc6d0802e04d9d0aa0e9f90e6d61b793d.svn-base 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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 ##VERSION##, ##DATE##
  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 (($number == 0.0 ) || ($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 ($significance == 0.0) {
  284. return PHPExcel_Calculation_Functions::DIV0();
  285. } elseif ($number == 0.0) {
  286. return 0.0;
  287. } elseif (self::SIGN($number) == self::SIGN($significance)) {
  288. return floor($number / $significance) * $significance;
  289. } else {
  290. return PHPExcel_Calculation_Functions::NaN();
  291. }
  292. } else
  293. return PHPExcel_Calculation_Functions::VALUE();
  294. } // function FLOOR()
  295. /**
  296. * GCD
  297. *
  298. * Returns the greatest common divisor of a series of numbers.
  299. * The greatest common divisor is the largest integer that divides both
  300. * number1 and number2 without a remainder.
  301. *
  302. * Excel Function:
  303. * GCD(number1[,number2[, ...]])
  304. *
  305. * @access public
  306. * @category Mathematical and Trigonometric Functions
  307. * @param mixed $arg,... Data values
  308. * @return integer Greatest Common Divisor
  309. */
  310. public static function GCD() {
  311. $returnValue = 1;
  312. $allValuesFactors = array();
  313. // Loop through arguments
  314. foreach(PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) {
  315. if (!is_numeric($value)) {
  316. return PHPExcel_Calculation_Functions::VALUE();
  317. } elseif ($value == 0) {
  318. continue;
  319. } elseif($value < 0) {
  320. return PHPExcel_Calculation_Functions::NaN();
  321. }
  322. $myFactors = self::_factors($value);
  323. $myCountedFactors = array_count_values($myFactors);
  324. $allValuesFactors[] = $myCountedFactors;
  325. }
  326. $allValuesCount = count($allValuesFactors);
  327. if ($allValuesCount == 0) {
  328. return 0;
  329. }
  330. $mergedArray = $allValuesFactors[0];
  331. for ($i=1;$i < $allValuesCount; ++$i) {
  332. $mergedArray = array_intersect_key($mergedArray,$allValuesFactors[$i]);
  333. }
  334. $mergedArrayValues = count($mergedArray);
  335. if ($mergedArrayValues == 0) {
  336. return $returnValue;
  337. } elseif ($mergedArrayValues > 1) {
  338. foreach($mergedArray as $mergedKey => $mergedValue) {
  339. foreach($allValuesFactors as $highestPowerTest) {
  340. foreach($highestPowerTest as $testKey => $testValue) {
  341. if (($testKey == $mergedKey) && ($testValue < $mergedValue)) {
  342. $mergedArray[$mergedKey] = $testValue;
  343. $mergedValue = $testValue;
  344. }
  345. }
  346. }
  347. }
  348. $returnValue = 1;
  349. foreach($mergedArray as $key => $value) {
  350. $returnValue *= pow($key,$value);
  351. }
  352. return $returnValue;
  353. } else {
  354. $keys = array_keys($mergedArray);
  355. $key = $keys[0];
  356. $value = $mergedArray[$key];
  357. foreach($allValuesFactors as $testValue) {
  358. foreach($testValue as $mergedKey => $mergedValue) {
  359. if (($mergedKey == $key) && ($mergedValue < $value)) {
  360. $value = $mergedValue;
  361. }
  362. }
  363. }
  364. return pow($key,$value);
  365. }
  366. } // function GCD()
  367. /**
  368. * INT
  369. *
  370. * Casts a floating point value to an integer
  371. *
  372. * Excel Function:
  373. * INT(number)
  374. *
  375. * @access public
  376. * @category Mathematical and Trigonometric Functions
  377. * @param float $number Number to cast to an integer
  378. * @return integer Integer value
  379. */
  380. public static function INT($number) {
  381. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  382. if (is_null($number)) {
  383. return 0;
  384. } elseif (is_bool($number)) {
  385. return (int) $number;
  386. }
  387. if (is_numeric($number)) {
  388. return (int) floor($number);
  389. }
  390. return PHPExcel_Calculation_Functions::VALUE();
  391. } // function INT()
  392. /**
  393. * LCM
  394. *
  395. * Returns the lowest common multiplier of a series of numbers
  396. * The least common multiple is the smallest positive integer that is a multiple
  397. * of all integer arguments number1, number2, and so on. Use LCM to add fractions
  398. * with different denominators.
  399. *
  400. * Excel Function:
  401. * LCM(number1[,number2[, ...]])
  402. *
  403. * @access public
  404. * @category Mathematical and Trigonometric Functions
  405. * @param mixed $arg,... Data values
  406. * @return int Lowest Common Multiplier
  407. */
  408. public static function LCM() {
  409. $returnValue = 1;
  410. $allPoweredFactors = array();
  411. // Loop through arguments
  412. foreach(PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) {
  413. if (!is_numeric($value)) {
  414. return PHPExcel_Calculation_Functions::VALUE();
  415. }
  416. if ($value == 0) {
  417. return 0;
  418. } elseif ($value < 0) {
  419. return PHPExcel_Calculation_Functions::NaN();
  420. }
  421. $myFactors = self::_factors(floor($value));
  422. $myCountedFactors = array_count_values($myFactors);
  423. $myPoweredFactors = array();
  424. foreach($myCountedFactors as $myCountedFactor => $myCountedPower) {
  425. $myPoweredFactors[$myCountedFactor] = pow($myCountedFactor,$myCountedPower);
  426. }
  427. foreach($myPoweredFactors as $myPoweredValue => $myPoweredFactor) {
  428. if (array_key_exists($myPoweredValue,$allPoweredFactors)) {
  429. if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) {
  430. $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
  431. }
  432. } else {
  433. $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
  434. }
  435. }
  436. }
  437. foreach($allPoweredFactors as $allPoweredFactor) {
  438. $returnValue *= (integer) $allPoweredFactor;
  439. }
  440. return $returnValue;
  441. } // function LCM()
  442. /**
  443. * LOG_BASE
  444. *
  445. * Returns the logarithm of a number to a specified base. The default base is 10.
  446. *
  447. * Excel Function:
  448. * LOG(number[,base])
  449. *
  450. * @access public
  451. * @category Mathematical and Trigonometric Functions
  452. * @param float $number The positive real number for which you want the logarithm
  453. * @param float $base The base of the logarithm. If base is omitted, it is assumed to be 10.
  454. * @return float
  455. */
  456. public static function LOG_BASE($number = NULL, $base = 10) {
  457. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  458. $base = (is_null($base)) ? 10 : (float) PHPExcel_Calculation_Functions::flattenSingleValue($base);
  459. if ((!is_numeric($base)) || (!is_numeric($number)))
  460. return PHPExcel_Calculation_Functions::VALUE();
  461. if (($base <= 0) || ($number <= 0))
  462. return PHPExcel_Calculation_Functions::NaN();
  463. return log($number, $base);
  464. } // function LOG_BASE()
  465. /**
  466. * MDETERM
  467. *
  468. * Returns the matrix determinant of an array.
  469. *
  470. * Excel Function:
  471. * MDETERM(array)
  472. *
  473. * @access public
  474. * @category Mathematical and Trigonometric Functions
  475. * @param array $matrixValues A matrix of values
  476. * @return float
  477. */
  478. public static function MDETERM($matrixValues) {
  479. $matrixData = array();
  480. if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); }
  481. $row = $maxColumn = 0;
  482. foreach($matrixValues as $matrixRow) {
  483. if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); }
  484. $column = 0;
  485. foreach($matrixRow as $matrixCell) {
  486. if ((is_string($matrixCell)) || ($matrixCell === null)) {
  487. return PHPExcel_Calculation_Functions::VALUE();
  488. }
  489. $matrixData[$column][$row] = $matrixCell;
  490. ++$column;
  491. }
  492. if ($column > $maxColumn) { $maxColumn = $column; }
  493. ++$row;
  494. }
  495. if ($row != $maxColumn) { return PHPExcel_Calculation_Functions::VALUE(); }
  496. try {
  497. $matrix = new PHPExcel_Shared_JAMA_Matrix($matrixData);
  498. return $matrix->det();
  499. } catch (PHPExcel_Exception $ex) {
  500. return PHPExcel_Calculation_Functions::VALUE();
  501. }
  502. } // function MDETERM()
  503. /**
  504. * MINVERSE
  505. *
  506. * Returns the inverse matrix for the matrix stored in an array.
  507. *
  508. * Excel Function:
  509. * MINVERSE(array)
  510. *
  511. * @access public
  512. * @category Mathematical and Trigonometric Functions
  513. * @param array $matrixValues A matrix of values
  514. * @return array
  515. */
  516. public static function MINVERSE($matrixValues) {
  517. $matrixData = array();
  518. if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); }
  519. $row = $maxColumn = 0;
  520. foreach($matrixValues as $matrixRow) {
  521. if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); }
  522. $column = 0;
  523. foreach($matrixRow as $matrixCell) {
  524. if ((is_string($matrixCell)) || ($matrixCell === null)) {
  525. return PHPExcel_Calculation_Functions::VALUE();
  526. }
  527. $matrixData[$column][$row] = $matrixCell;
  528. ++$column;
  529. }
  530. if ($column > $maxColumn) { $maxColumn = $column; }
  531. ++$row;
  532. }
  533. if ($row != $maxColumn) { return PHPExcel_Calculation_Functions::VALUE(); }
  534. try {
  535. $matrix = new PHPExcel_Shared_JAMA_Matrix($matrixData);
  536. return $matrix->inverse()->getArray();
  537. } catch (PHPExcel_Exception $ex) {
  538. return PHPExcel_Calculation_Functions::VALUE();
  539. }
  540. } // function MINVERSE()
  541. /**
  542. * MMULT
  543. *
  544. * @param array $matrixData1 A matrix of values
  545. * @param array $matrixData2 A matrix of values
  546. * @return array
  547. */
  548. public static function MMULT($matrixData1,$matrixData2) {
  549. $matrixAData = $matrixBData = array();
  550. if (!is_array($matrixData1)) { $matrixData1 = array(array($matrixData1)); }
  551. if (!is_array($matrixData2)) { $matrixData2 = array(array($matrixData2)); }
  552. try {
  553. $rowA = 0;
  554. foreach($matrixData1 as $matrixRow) {
  555. if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); }
  556. $columnA = 0;
  557. foreach($matrixRow as $matrixCell) {
  558. if ((!is_numeric($matrixCell)) || ($matrixCell === null)) {
  559. return PHPExcel_Calculation_Functions::VALUE();
  560. }
  561. $matrixAData[$rowA][$columnA] = $matrixCell;
  562. ++$columnA;
  563. }
  564. ++$rowA;
  565. }
  566. $matrixA = new PHPExcel_Shared_JAMA_Matrix($matrixAData);
  567. $rowB = 0;
  568. foreach($matrixData2 as $matrixRow) {
  569. if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); }
  570. $columnB = 0;
  571. foreach($matrixRow as $matrixCell) {
  572. if ((!is_numeric($matrixCell)) || ($matrixCell === null)) {
  573. return PHPExcel_Calculation_Functions::VALUE();
  574. }
  575. $matrixBData[$rowB][$columnB] = $matrixCell;
  576. ++$columnB;
  577. }
  578. ++$rowB;
  579. }
  580. $matrixB = new PHPExcel_Shared_JAMA_Matrix($matrixBData);
  581. if ($columnA != $rowB) {
  582. return PHPExcel_Calculation_Functions::VALUE();
  583. }
  584. return $matrixA->times($matrixB)->getArray();
  585. } catch (PHPExcel_Exception $ex) {
  586. var_dump($ex->getMessage());
  587. return PHPExcel_Calculation_Functions::VALUE();
  588. }
  589. } // function MMULT()
  590. /**
  591. * MOD
  592. *
  593. * @param int $a Dividend
  594. * @param int $b Divisor
  595. * @return int Remainder
  596. */
  597. public static function MOD($a = 1, $b = 1) {
  598. $a = PHPExcel_Calculation_Functions::flattenSingleValue($a);
  599. $b = PHPExcel_Calculation_Functions::flattenSingleValue($b);
  600. if ($b == 0.0) {
  601. return PHPExcel_Calculation_Functions::DIV0();
  602. } elseif (($a < 0.0) && ($b > 0.0)) {
  603. return $b - fmod(abs($a),$b);
  604. } elseif (($a > 0.0) && ($b < 0.0)) {
  605. return $b + fmod($a,abs($b));
  606. }
  607. return fmod($a,$b);
  608. } // function MOD()
  609. /**
  610. * MROUND
  611. *
  612. * Rounds a number to the nearest multiple of a specified value
  613. *
  614. * @param float $number Number to round
  615. * @param int $multiple Multiple to which you want to round $number
  616. * @return float Rounded Number
  617. */
  618. public static function MROUND($number,$multiple) {
  619. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  620. $multiple = PHPExcel_Calculation_Functions::flattenSingleValue($multiple);
  621. if ((is_numeric($number)) && (is_numeric($multiple))) {
  622. if ($multiple == 0) {
  623. return 0;
  624. }
  625. if ((self::SIGN($number)) == (self::SIGN($multiple))) {
  626. $multiplier = 1 / $multiple;
  627. return round($number * $multiplier) / $multiplier;
  628. }
  629. return PHPExcel_Calculation_Functions::NaN();
  630. }
  631. return PHPExcel_Calculation_Functions::VALUE();
  632. } // function MROUND()
  633. /**
  634. * MULTINOMIAL
  635. *
  636. * Returns the ratio of the factorial of a sum of values to the product of factorials.
  637. *
  638. * @param array of mixed Data Series
  639. * @return float
  640. */
  641. public static function MULTINOMIAL() {
  642. $summer = 0;
  643. $divisor = 1;
  644. // Loop through arguments
  645. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  646. // Is it a numeric value?
  647. if (is_numeric($arg)) {
  648. if ($arg < 1) {
  649. return PHPExcel_Calculation_Functions::NaN();
  650. }
  651. $summer += floor($arg);
  652. $divisor *= self::FACT($arg);
  653. } else {
  654. return PHPExcel_Calculation_Functions::VALUE();
  655. }
  656. }
  657. // Return
  658. if ($summer > 0) {
  659. $summer = self::FACT($summer);
  660. return $summer / $divisor;
  661. }
  662. return 0;
  663. } // function MULTINOMIAL()
  664. /**
  665. * ODD
  666. *
  667. * Returns number rounded up to the nearest odd integer.
  668. *
  669. * @param float $number Number to round
  670. * @return int Rounded Number
  671. */
  672. public static function ODD($number) {
  673. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  674. if (is_null($number)) {
  675. return 1;
  676. } elseif (is_bool($number)) {
  677. $number = (int) $number;
  678. }
  679. if (is_numeric($number)) {
  680. $significance = self::SIGN($number);
  681. if ($significance == 0) {
  682. return 1;
  683. }
  684. $result = self::CEILING($number,$significance);
  685. if ($result == self::EVEN($result)) {
  686. $result += $significance;
  687. }
  688. return (int) $result;
  689. }
  690. return PHPExcel_Calculation_Functions::VALUE();
  691. } // function ODD()
  692. /**
  693. * POWER
  694. *
  695. * Computes x raised to the power y.
  696. *
  697. * @param float $x
  698. * @param float $y
  699. * @return float
  700. */
  701. public static function POWER($x = 0, $y = 2) {
  702. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  703. $y = PHPExcel_Calculation_Functions::flattenSingleValue($y);
  704. // Validate parameters
  705. if ($x == 0.0 && $y == 0.0) {
  706. return PHPExcel_Calculation_Functions::NaN();
  707. } elseif ($x == 0.0 && $y < 0.0) {
  708. return PHPExcel_Calculation_Functions::DIV0();
  709. }
  710. // Return
  711. $result = pow($x, $y);
  712. return (!is_nan($result) && !is_infinite($result)) ? $result : PHPExcel_Calculation_Functions::NaN();
  713. } // function POWER()
  714. /**
  715. * PRODUCT
  716. *
  717. * PRODUCT returns the product of all the values and cells referenced in the argument list.
  718. *
  719. * Excel Function:
  720. * PRODUCT(value1[,value2[, ...]])
  721. *
  722. * @access public
  723. * @category Mathematical and Trigonometric Functions
  724. * @param mixed $arg,... Data values
  725. * @return float
  726. */
  727. public static function PRODUCT() {
  728. // Return value
  729. $returnValue = null;
  730. // Loop through arguments
  731. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  732. // Is it a numeric value?
  733. if ((is_numeric($arg)) && (!is_string($arg))) {
  734. if (is_null($returnValue)) {
  735. $returnValue = $arg;
  736. } else {
  737. $returnValue *= $arg;
  738. }
  739. }
  740. }
  741. // Return
  742. if (is_null($returnValue)) {
  743. return 0;
  744. }
  745. return $returnValue;
  746. } // function PRODUCT()
  747. /**
  748. * QUOTIENT
  749. *
  750. * QUOTIENT function returns the integer portion of a division. Numerator is the divided number
  751. * and denominator is the divisor.
  752. *
  753. * Excel Function:
  754. * QUOTIENT(value1[,value2[, ...]])
  755. *
  756. * @access public
  757. * @category Mathematical and Trigonometric Functions
  758. * @param mixed $arg,... Data values
  759. * @return float
  760. */
  761. public static function QUOTIENT() {
  762. // Return value
  763. $returnValue = null;
  764. // Loop through arguments
  765. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  766. // Is it a numeric value?
  767. if ((is_numeric($arg)) && (!is_string($arg))) {
  768. if (is_null($returnValue)) {
  769. $returnValue = ($arg == 0) ? 0 : $arg;
  770. } else {
  771. if (($returnValue == 0) || ($arg == 0)) {
  772. $returnValue = 0;
  773. } else {
  774. $returnValue /= $arg;
  775. }
  776. }
  777. }
  778. }
  779. // Return
  780. return intval($returnValue);
  781. } // function QUOTIENT()
  782. /**
  783. * RAND
  784. *
  785. * @param int $min Minimal value
  786. * @param int $max Maximal value
  787. * @return int Random number
  788. */
  789. public static function RAND($min = 0, $max = 0) {
  790. $min = PHPExcel_Calculation_Functions::flattenSingleValue($min);
  791. $max = PHPExcel_Calculation_Functions::flattenSingleValue($max);
  792. if ($min == 0 && $max == 0) {
  793. return (mt_rand(0,10000000)) / 10000000;
  794. } else {
  795. return mt_rand($min, $max);
  796. }
  797. } // function RAND()
  798. public static function ROMAN($aValue, $style=0) {
  799. $aValue = PHPExcel_Calculation_Functions::flattenSingleValue($aValue);
  800. $style = (is_null($style)) ? 0 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($style);
  801. if ((!is_numeric($aValue)) || ($aValue < 0) || ($aValue >= 4000)) {
  802. return PHPExcel_Calculation_Functions::VALUE();
  803. }
  804. $aValue = (integer) $aValue;
  805. if ($aValue == 0) {
  806. return '';
  807. }
  808. $mill = Array('', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM');
  809. $cent = Array('', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM');
  810. $tens = Array('', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC');
  811. $ones = Array('', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX');
  812. $roman = '';
  813. while ($aValue > 5999) {
  814. $roman .= 'M';
  815. $aValue -= 1000;
  816. }
  817. $m = self::_romanCut($aValue, 1000); $aValue %= 1000;
  818. $c = self::_romanCut($aValue, 100); $aValue %= 100;
  819. $t = self::_romanCut($aValue, 10); $aValue %= 10;
  820. return $roman.$mill[$m].$cent[$c].$tens[$t].$ones[$aValue];
  821. } // function ROMAN()
  822. /**
  823. * ROUNDUP
  824. *
  825. * Rounds a number up to a specified number of decimal places
  826. *
  827. * @param float $number Number to round
  828. * @param int $digits Number of digits to which you want to round $number
  829. * @return float Rounded Number
  830. */
  831. public static function ROUNDUP($number,$digits) {
  832. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  833. $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits);
  834. if ((is_numeric($number)) && (is_numeric($digits))) {
  835. $significance = pow(10,(int) $digits);
  836. if ($number < 0.0) {
  837. return floor($number * $significance) / $significance;
  838. } else {
  839. return ceil($number * $significance) / $significance;
  840. }
  841. }
  842. return PHPExcel_Calculation_Functions::VALUE();
  843. } // function ROUNDUP()
  844. /**
  845. * ROUNDDOWN
  846. *
  847. * Rounds a number down to a specified number of decimal places
  848. *
  849. * @param float $number Number to round
  850. * @param int $digits Number of digits to which you want to round $number
  851. * @return float Rounded Number
  852. */
  853. public static function ROUNDDOWN($number,$digits) {
  854. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  855. $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits);
  856. if ((is_numeric($number)) && (is_numeric($digits))) {
  857. $significance = pow(10,(int) $digits);
  858. if ($number < 0.0) {
  859. return ceil($number * $significance) / $significance;
  860. } else {
  861. return floor($number * $significance) / $significance;
  862. }
  863. }
  864. return PHPExcel_Calculation_Functions::VALUE();
  865. } // function ROUNDDOWN()
  866. /**
  867. * SERIESSUM
  868. *
  869. * Returns the sum of a power series
  870. *
  871. * @param float $x Input value to the power series
  872. * @param float $n Initial power to which you want to raise $x
  873. * @param float $m Step by which to increase $n for each term in the series
  874. * @param array of mixed Data Series
  875. * @return float
  876. */
  877. public static function SERIESSUM() {
  878. // Return value
  879. $returnValue = 0;
  880. // Loop through arguments
  881. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  882. $x = array_shift($aArgs);
  883. $n = array_shift($aArgs);
  884. $m = array_shift($aArgs);
  885. if ((is_numeric($x)) && (is_numeric($n)) && (is_numeric($m))) {
  886. // Calculate
  887. $i = 0;
  888. foreach($aArgs as $arg) {
  889. // Is it a numeric value?
  890. if ((is_numeric($arg)) && (!is_string($arg))) {
  891. $returnValue += $arg * pow($x,$n + ($m * $i++));
  892. } else {
  893. return PHPExcel_Calculation_Functions::VALUE();
  894. }
  895. }
  896. // Return
  897. return $returnValue;
  898. }
  899. return PHPExcel_Calculation_Functions::VALUE();
  900. } // function SERIESSUM()
  901. /**
  902. * SIGN
  903. *
  904. * Determines the sign of a number. Returns 1 if the number is positive, zero (0)
  905. * if the number is 0, and -1 if the number is negative.
  906. *
  907. * @param float $number Number to round
  908. * @return int sign value
  909. */
  910. public static function SIGN($number) {
  911. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  912. if (is_bool($number))
  913. return (int) $number;
  914. if (is_numeric($number)) {
  915. if ($number == 0.0) {
  916. return 0;
  917. }
  918. return $number / abs($number);
  919. }
  920. return PHPExcel_Calculation_Functions::VALUE();
  921. } // function SIGN()
  922. /**
  923. * SQRTPI
  924. *
  925. * Returns the square root of (number * pi).
  926. *
  927. * @param float $number Number
  928. * @return float Square Root of Number * Pi
  929. */
  930. public static function SQRTPI($number) {
  931. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  932. if (is_numeric($number)) {
  933. if ($number < 0) {
  934. return PHPExcel_Calculation_Functions::NaN();
  935. }
  936. return sqrt($number * M_PI) ;
  937. }
  938. return PHPExcel_Calculation_Functions::VALUE();
  939. } // function SQRTPI()
  940. /**
  941. * SUBTOTAL
  942. *
  943. * Returns a subtotal in a list or database.
  944. *
  945. * @param int the number 1 to 11 that specifies which function to
  946. * use in calculating subtotals within a list.
  947. * @param array of mixed Data Series
  948. * @return float
  949. */
  950. public static function SUBTOTAL() {
  951. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  952. // Calculate
  953. $subtotal = array_shift($aArgs);
  954. if ((is_numeric($subtotal)) && (!is_string($subtotal))) {
  955. switch($subtotal) {
  956. case 1 :
  957. return PHPExcel_Calculation_Statistical::AVERAGE($aArgs);
  958. break;
  959. case 2 :
  960. return PHPExcel_Calculation_Statistical::COUNT($aArgs);
  961. break;
  962. case 3 :
  963. return PHPExcel_Calculation_Statistical::COUNTA($aArgs);
  964. break;
  965. case 4 :
  966. return PHPExcel_Calculation_Statistical::MAX($aArgs);
  967. break;
  968. case 5 :
  969. return PHPExcel_Calculation_Statistical::MIN($aArgs);
  970. break;
  971. case 6 :
  972. return self::PRODUCT($aArgs);
  973. break;
  974. case 7 :
  975. return PHPExcel_Calculation_Statistical::STDEV($aArgs);
  976. break;
  977. case 8 :
  978. return PHPExcel_Calculation_Statistical::STDEVP($aArgs);
  979. break;
  980. case 9 :
  981. return self::SUM($aArgs);
  982. break;
  983. case 10 :
  984. return PHPExcel_Calculation_Statistical::VARFunc($aArgs);
  985. break;
  986. case 11 :
  987. return PHPExcel_Calculation_Statistical::VARP($aArgs);
  988. break;
  989. }
  990. }
  991. return PHPExcel_Calculation_Functions::VALUE();
  992. } // function SUBTOTAL()
  993. /**
  994. * SUM
  995. *
  996. * SUM computes the sum of all the values and cells referenced in the argument list.
  997. *
  998. * Excel Function:
  999. * SUM(value1[,value2[, ...]])
  1000. *
  1001. * @access public
  1002. * @category Mathematical and Trigonometric Functions
  1003. * @param mixed $arg,... Data values
  1004. * @return float
  1005. */
  1006. public static function SUM() {
  1007. // Return value
  1008. $returnValue = 0;
  1009. // Loop through the arguments
  1010. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  1011. // Is it a numeric value?
  1012. if ((is_numeric($arg)) && (!is_string($arg))) {
  1013. $returnValue += $arg;
  1014. }
  1015. }
  1016. // Return
  1017. return $returnValue;
  1018. } // function SUM()
  1019. /**
  1020. * SUMIF
  1021. *
  1022. * Counts the number of cells that contain numbers within the list of arguments
  1023. *
  1024. * Excel Function:
  1025. * SUMIF(value1[,value2[, ...]],condition)
  1026. *
  1027. * @access public
  1028. * @category Mathematical and Trigonometric Functions
  1029. * @param mixed $arg,... Data values
  1030. * @param string $condition The criteria that defines which cells will be summed.
  1031. * @return float
  1032. */
  1033. public static function SUMIF($aArgs,$condition,$sumArgs = array()) {
  1034. // Return value
  1035. $returnValue = 0;
  1036. $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs);
  1037. $sumArgs = PHPExcel_Calculation_Functions::flattenArray($sumArgs);
  1038. if (empty($sumArgs)) {
  1039. $sumArgs = $aArgs;
  1040. }
  1041. $condition = PHPExcel_Calculation_Functions::_ifCondition($condition);
  1042. // Loop through arguments
  1043. foreach ($aArgs as $key => $arg) {
  1044. if (!is_numeric($arg)) {
  1045. $arg = str_replace('"', '""', $arg);
  1046. $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg));
  1047. }
  1048. $testCondition = '='.$arg.$condition;
  1049. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1050. // Is it a value within our criteria
  1051. $returnValue += $sumArgs[$key];
  1052. }
  1053. }
  1054. // Return
  1055. return $returnValue;
  1056. } // function SUMIF()
  1057. /**
  1058. * SUMPRODUCT
  1059. *
  1060. * Excel Function:
  1061. * SUMPRODUCT(value1[,value2[, ...]])
  1062. *
  1063. * @access public
  1064. * @category Mathematical and Trigonometric Functions
  1065. * @param mixed $arg,... Data values
  1066. * @return float
  1067. */
  1068. public static function SUMPRODUCT() {
  1069. $arrayList = func_get_args();
  1070. $wrkArray = PHPExcel_Calculation_Functions::flattenArray(array_shift($arrayList));
  1071. $wrkCellCount = count($wrkArray);
  1072. for ($i=0; $i< $wrkCellCount; ++$i) {
  1073. if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) {
  1074. $wrkArray[$i] = 0;
  1075. }
  1076. }
  1077. foreach($arrayList as $matrixData) {
  1078. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData);
  1079. $count = count($array2);
  1080. if ($wrkCellCount != $count) {
  1081. return PHPExcel_Calculation_Functions::VALUE();
  1082. }
  1083. foreach ($array2 as $i => $val) {
  1084. if ((!is_numeric($val)) || (is_string($val))) {
  1085. $val = 0;
  1086. }
  1087. $wrkArray[$i] *= $val;
  1088. }
  1089. }
  1090. return array_sum($wrkArray);
  1091. } // function SUMPRODUCT()
  1092. /**
  1093. * SUMSQ
  1094. *
  1095. * SUMSQ returns the sum of the squares of the arguments
  1096. *
  1097. * Excel Function:
  1098. * SUMSQ(value1[,value2[, ...]])
  1099. *
  1100. * @access public
  1101. * @category Mathematical and Trigonometric Functions
  1102. * @param mixed $arg,... Data values
  1103. * @return float
  1104. */
  1105. public static function SUMSQ() {
  1106. // Return value
  1107. $returnValue = 0;
  1108. // Loop through arguments
  1109. foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $arg) {
  1110. // Is it a numeric value?
  1111. if ((is_numeric($arg)) && (!is_string($arg))) {
  1112. $returnValue += ($arg * $arg);
  1113. }
  1114. }
  1115. // Return
  1116. return $returnValue;
  1117. } // function SUMSQ()
  1118. /**
  1119. * SUMX2MY2
  1120. *
  1121. * @param mixed[] $matrixData1 Matrix #1
  1122. * @param mixed[] $matrixData2 Matrix #2
  1123. * @return float
  1124. */
  1125. public static function SUMX2MY2($matrixData1,$matrixData2) {
  1126. $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1127. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1128. $count1 = count($array1);
  1129. $count2 = count($array2);
  1130. if ($count1 < $count2) {
  1131. $count = $count1;
  1132. } else {
  1133. $count = $count2;
  1134. }
  1135. $result = 0;
  1136. for ($i = 0; $i < $count; ++$i) {
  1137. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1138. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1139. $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
  1140. }
  1141. }
  1142. return $result;
  1143. } // function SUMX2MY2()
  1144. /**
  1145. * SUMX2PY2
  1146. *
  1147. * @param mixed[] $matrixData1 Matrix #1
  1148. * @param mixed[] $matrixData2 Matrix #2
  1149. * @return float
  1150. */
  1151. public static function SUMX2PY2($matrixData1,$matrixData2) {
  1152. $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1153. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1154. $count1 = count($array1);
  1155. $count2 = count($array2);
  1156. if ($count1 < $count2) {
  1157. $count = $count1;
  1158. } else {
  1159. $count = $count2;
  1160. }
  1161. $result = 0;
  1162. for ($i = 0; $i < $count; ++$i) {
  1163. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1164. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1165. $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
  1166. }
  1167. }
  1168. return $result;
  1169. } // function SUMX2PY2()
  1170. /**
  1171. * SUMXMY2
  1172. *
  1173. * @param mixed[] $matrixData1 Matrix #1
  1174. * @param mixed[] $matrixData2 Matrix #2
  1175. * @return float
  1176. */
  1177. public static function SUMXMY2($matrixData1,$matrixData2) {
  1178. $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1);
  1179. $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2);
  1180. $count1 = count($array1);
  1181. $count2 = count($array2);
  1182. if ($count1 < $count2) {
  1183. $count = $count1;
  1184. } else {
  1185. $count = $count2;
  1186. }
  1187. $result = 0;
  1188. for ($i = 0; $i < $count; ++$i) {
  1189. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1190. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1191. $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
  1192. }
  1193. }
  1194. return $result;
  1195. } // function SUMXMY2()
  1196. /**
  1197. * TRUNC
  1198. *
  1199. * Truncates value to the number of fractional digits by number_digits.
  1200. *
  1201. * @param float $value
  1202. * @param int $digits
  1203. * @return float Truncated value
  1204. */
  1205. public static function TRUNC($value = 0, $digits = 0) {
  1206. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1207. $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits);
  1208. // Validate parameters
  1209. if ((!is_numeric($value)) || (!is_numeric($digits)))
  1210. return PHPExcel_Calculation_Functions::VALUE();
  1211. $digits = floor($digits);
  1212. // Truncate
  1213. $adjust = pow(10, $digits);
  1214. if (($digits > 0) && (rtrim(intval((abs($value) - abs(intval($value))) * $adjust),'0') < $adjust/10))
  1215. return $value;
  1216. return (intval($value * $adjust)) / $adjust;
  1217. } // function TRUNC()
  1218. } // class PHPExcel_Calculation_MathTrig