MathTrig.php 49 KB

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