/lib/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Engineering/ComplexFunctions.php

https://github.com/sbourget/moodle · PHP · 513 lines · 218 code · 65 blank · 230 comment · 18 complexity · fa0977723d8f9da47b7c0fbe11f168e1 MD5 · raw file

  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
  3. use Complex\Complex as ComplexObject;
  4. use Complex\Exception as ComplexException;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  6. class ComplexFunctions
  7. {
  8. /**
  9. * IMABS.
  10. *
  11. * Returns the absolute value (modulus) of a complex number in x + yi or x + yj text format.
  12. *
  13. * Excel Function:
  14. * IMABS(complexNumber)
  15. *
  16. * @param string $complexNumber the complex number for which you want the absolute value
  17. *
  18. * @return float|string
  19. */
  20. public static function IMABS($complexNumber)
  21. {
  22. $complexNumber = Functions::flattenSingleValue($complexNumber);
  23. try {
  24. $complex = new ComplexObject($complexNumber);
  25. } catch (ComplexException $e) {
  26. return Functions::NAN();
  27. }
  28. return $complex->abs();
  29. }
  30. /**
  31. * IMARGUMENT.
  32. *
  33. * Returns the argument theta of a complex number, i.e. the angle in radians from the real
  34. * axis to the representation of the number in polar coordinates.
  35. *
  36. * Excel Function:
  37. * IMARGUMENT(complexNumber)
  38. *
  39. * @param string $complexNumber the complex number for which you want the argument theta
  40. *
  41. * @return float|string
  42. */
  43. public static function IMARGUMENT($complexNumber)
  44. {
  45. $complexNumber = Functions::flattenSingleValue($complexNumber);
  46. try {
  47. $complex = new ComplexObject($complexNumber);
  48. } catch (ComplexException $e) {
  49. return Functions::NAN();
  50. }
  51. if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
  52. return Functions::DIV0();
  53. }
  54. return $complex->argument();
  55. }
  56. /**
  57. * IMCONJUGATE.
  58. *
  59. * Returns the complex conjugate of a complex number in x + yi or x + yj text format.
  60. *
  61. * Excel Function:
  62. * IMCONJUGATE(complexNumber)
  63. *
  64. * @param string $complexNumber the complex number for which you want the conjugate
  65. *
  66. * @return string
  67. */
  68. public static function IMCONJUGATE($complexNumber)
  69. {
  70. $complexNumber = Functions::flattenSingleValue($complexNumber);
  71. try {
  72. $complex = new ComplexObject($complexNumber);
  73. } catch (ComplexException $e) {
  74. return Functions::NAN();
  75. }
  76. return (string) $complex->conjugate();
  77. }
  78. /**
  79. * IMCOS.
  80. *
  81. * Returns the cosine of a complex number in x + yi or x + yj text format.
  82. *
  83. * Excel Function:
  84. * IMCOS(complexNumber)
  85. *
  86. * @param string $complexNumber the complex number for which you want the cosine
  87. *
  88. * @return float|string
  89. */
  90. public static function IMCOS($complexNumber)
  91. {
  92. $complexNumber = Functions::flattenSingleValue($complexNumber);
  93. try {
  94. $complex = new ComplexObject($complexNumber);
  95. } catch (ComplexException $e) {
  96. return Functions::NAN();
  97. }
  98. return (string) $complex->cos();
  99. }
  100. /**
  101. * IMCOSH.
  102. *
  103. * Returns the hyperbolic cosine of a complex number in x + yi or x + yj text format.
  104. *
  105. * Excel Function:
  106. * IMCOSH(complexNumber)
  107. *
  108. * @param string $complexNumber the complex number for which you want the hyperbolic cosine
  109. *
  110. * @return float|string
  111. */
  112. public static function IMCOSH($complexNumber)
  113. {
  114. $complexNumber = Functions::flattenSingleValue($complexNumber);
  115. try {
  116. $complex = new ComplexObject($complexNumber);
  117. } catch (ComplexException $e) {
  118. return Functions::NAN();
  119. }
  120. return (string) $complex->cosh();
  121. }
  122. /**
  123. * IMCOT.
  124. *
  125. * Returns the cotangent of a complex number in x + yi or x + yj text format.
  126. *
  127. * Excel Function:
  128. * IMCOT(complexNumber)
  129. *
  130. * @param string $complexNumber the complex number for which you want the cotangent
  131. *
  132. * @return float|string
  133. */
  134. public static function IMCOT($complexNumber)
  135. {
  136. $complexNumber = Functions::flattenSingleValue($complexNumber);
  137. try {
  138. $complex = new ComplexObject($complexNumber);
  139. } catch (ComplexException $e) {
  140. return Functions::NAN();
  141. }
  142. return (string) $complex->cot();
  143. }
  144. /**
  145. * IMCSC.
  146. *
  147. * Returns the cosecant of a complex number in x + yi or x + yj text format.
  148. *
  149. * Excel Function:
  150. * IMCSC(complexNumber)
  151. *
  152. * @param string $complexNumber the complex number for which you want the cosecant
  153. *
  154. * @return float|string
  155. */
  156. public static function IMCSC($complexNumber)
  157. {
  158. $complexNumber = Functions::flattenSingleValue($complexNumber);
  159. try {
  160. $complex = new ComplexObject($complexNumber);
  161. } catch (ComplexException $e) {
  162. return Functions::NAN();
  163. }
  164. return (string) $complex->csc();
  165. }
  166. /**
  167. * IMCSCH.
  168. *
  169. * Returns the hyperbolic cosecant of a complex number in x + yi or x + yj text format.
  170. *
  171. * Excel Function:
  172. * IMCSCH(complexNumber)
  173. *
  174. * @param string $complexNumber the complex number for which you want the hyperbolic cosecant
  175. *
  176. * @return float|string
  177. */
  178. public static function IMCSCH($complexNumber)
  179. {
  180. $complexNumber = Functions::flattenSingleValue($complexNumber);
  181. try {
  182. $complex = new ComplexObject($complexNumber);
  183. } catch (ComplexException $e) {
  184. return Functions::NAN();
  185. }
  186. return (string) $complex->csch();
  187. }
  188. /**
  189. * IMSIN.
  190. *
  191. * Returns the sine of a complex number in x + yi or x + yj text format.
  192. *
  193. * Excel Function:
  194. * IMSIN(complexNumber)
  195. *
  196. * @param string $complexNumber the complex number for which you want the sine
  197. *
  198. * @return float|string
  199. */
  200. public static function IMSIN($complexNumber)
  201. {
  202. $complexNumber = Functions::flattenSingleValue($complexNumber);
  203. try {
  204. $complex = new ComplexObject($complexNumber);
  205. } catch (ComplexException $e) {
  206. return Functions::NAN();
  207. }
  208. return (string) $complex->sin();
  209. }
  210. /**
  211. * IMSINH.
  212. *
  213. * Returns the hyperbolic sine of a complex number in x + yi or x + yj text format.
  214. *
  215. * Excel Function:
  216. * IMSINH(complexNumber)
  217. *
  218. * @param string $complexNumber the complex number for which you want the hyperbolic sine
  219. *
  220. * @return float|string
  221. */
  222. public static function IMSINH($complexNumber)
  223. {
  224. $complexNumber = Functions::flattenSingleValue($complexNumber);
  225. try {
  226. $complex = new ComplexObject($complexNumber);
  227. } catch (ComplexException $e) {
  228. return Functions::NAN();
  229. }
  230. return (string) $complex->sinh();
  231. }
  232. /**
  233. * IMSEC.
  234. *
  235. * Returns the secant of a complex number in x + yi or x + yj text format.
  236. *
  237. * Excel Function:
  238. * IMSEC(complexNumber)
  239. *
  240. * @param string $complexNumber the complex number for which you want the secant
  241. *
  242. * @return float|string
  243. */
  244. public static function IMSEC($complexNumber)
  245. {
  246. $complexNumber = Functions::flattenSingleValue($complexNumber);
  247. try {
  248. $complex = new ComplexObject($complexNumber);
  249. } catch (ComplexException $e) {
  250. return Functions::NAN();
  251. }
  252. return (string) $complex->sec();
  253. }
  254. /**
  255. * IMSECH.
  256. *
  257. * Returns the hyperbolic secant of a complex number in x + yi or x + yj text format.
  258. *
  259. * Excel Function:
  260. * IMSECH(complexNumber)
  261. *
  262. * @param string $complexNumber the complex number for which you want the hyperbolic secant
  263. *
  264. * @return float|string
  265. */
  266. public static function IMSECH($complexNumber)
  267. {
  268. $complexNumber = Functions::flattenSingleValue($complexNumber);
  269. try {
  270. $complex = new ComplexObject($complexNumber);
  271. } catch (ComplexException $e) {
  272. return Functions::NAN();
  273. }
  274. return (string) $complex->sech();
  275. }
  276. /**
  277. * IMTAN.
  278. *
  279. * Returns the tangent of a complex number in x + yi or x + yj text format.
  280. *
  281. * Excel Function:
  282. * IMTAN(complexNumber)
  283. *
  284. * @param string $complexNumber the complex number for which you want the tangent
  285. *
  286. * @return float|string
  287. */
  288. public static function IMTAN($complexNumber)
  289. {
  290. $complexNumber = Functions::flattenSingleValue($complexNumber);
  291. try {
  292. $complex = new ComplexObject($complexNumber);
  293. } catch (ComplexException $e) {
  294. return Functions::NAN();
  295. }
  296. return (string) $complex->tan();
  297. }
  298. /**
  299. * IMSQRT.
  300. *
  301. * Returns the square root of a complex number in x + yi or x + yj text format.
  302. *
  303. * Excel Function:
  304. * IMSQRT(complexNumber)
  305. *
  306. * @param string $complexNumber the complex number for which you want the square root
  307. *
  308. * @return string
  309. */
  310. public static function IMSQRT($complexNumber)
  311. {
  312. $complexNumber = Functions::flattenSingleValue($complexNumber);
  313. try {
  314. $complex = new ComplexObject($complexNumber);
  315. } catch (ComplexException $e) {
  316. return Functions::NAN();
  317. }
  318. $theta = self::IMARGUMENT($complexNumber);
  319. if ($theta === Functions::DIV0()) {
  320. return '0';
  321. }
  322. return (string) $complex->sqrt();
  323. }
  324. /**
  325. * IMLN.
  326. *
  327. * Returns the natural logarithm of a complex number in x + yi or x + yj text format.
  328. *
  329. * Excel Function:
  330. * IMLN(complexNumber)
  331. *
  332. * @param string $complexNumber the complex number for which you want the natural logarithm
  333. *
  334. * @return string
  335. */
  336. public static function IMLN($complexNumber)
  337. {
  338. $complexNumber = Functions::flattenSingleValue($complexNumber);
  339. try {
  340. $complex = new ComplexObject($complexNumber);
  341. } catch (ComplexException $e) {
  342. return Functions::NAN();
  343. }
  344. if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
  345. return Functions::NAN();
  346. }
  347. return (string) $complex->ln();
  348. }
  349. /**
  350. * IMLOG10.
  351. *
  352. * Returns the common logarithm (base 10) of a complex number in x + yi or x + yj text format.
  353. *
  354. * Excel Function:
  355. * IMLOG10(complexNumber)
  356. *
  357. * @param string $complexNumber the complex number for which you want the common logarithm
  358. *
  359. * @return string
  360. */
  361. public static function IMLOG10($complexNumber)
  362. {
  363. $complexNumber = Functions::flattenSingleValue($complexNumber);
  364. try {
  365. $complex = new ComplexObject($complexNumber);
  366. } catch (ComplexException $e) {
  367. return Functions::NAN();
  368. }
  369. if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
  370. return Functions::NAN();
  371. }
  372. return (string) $complex->log10();
  373. }
  374. /**
  375. * IMLOG2.
  376. *
  377. * Returns the base-2 logarithm of a complex number in x + yi or x + yj text format.
  378. *
  379. * Excel Function:
  380. * IMLOG2(complexNumber)
  381. *
  382. * @param string $complexNumber the complex number for which you want the base-2 logarithm
  383. *
  384. * @return string
  385. */
  386. public static function IMLOG2($complexNumber)
  387. {
  388. $complexNumber = Functions::flattenSingleValue($complexNumber);
  389. try {
  390. $complex = new ComplexObject($complexNumber);
  391. } catch (ComplexException $e) {
  392. return Functions::NAN();
  393. }
  394. if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
  395. return Functions::NAN();
  396. }
  397. return (string) $complex->log2();
  398. }
  399. /**
  400. * IMEXP.
  401. *
  402. * Returns the exponential of a complex number in x + yi or x + yj text format.
  403. *
  404. * Excel Function:
  405. * IMEXP(complexNumber)
  406. *
  407. * @param string $complexNumber the complex number for which you want the exponential
  408. *
  409. * @return string
  410. */
  411. public static function IMEXP($complexNumber)
  412. {
  413. $complexNumber = Functions::flattenSingleValue($complexNumber);
  414. try {
  415. $complex = new ComplexObject($complexNumber);
  416. } catch (ComplexException $e) {
  417. return Functions::NAN();
  418. }
  419. return (string) $complex->exp();
  420. }
  421. /**
  422. * IMPOWER.
  423. *
  424. * Returns a complex number in x + yi or x + yj text format raised to a power.
  425. *
  426. * Excel Function:
  427. * IMPOWER(complexNumber,realNumber)
  428. *
  429. * @param string $complexNumber the complex number you want to raise to a power
  430. * @param float $realNumber the power to which you want to raise the complex number
  431. *
  432. * @return string
  433. */
  434. public static function IMPOWER($complexNumber, $realNumber)
  435. {
  436. $complexNumber = Functions::flattenSingleValue($complexNumber);
  437. $realNumber = Functions::flattenSingleValue($realNumber);
  438. try {
  439. $complex = new ComplexObject($complexNumber);
  440. } catch (ComplexException $e) {
  441. return Functions::NAN();
  442. }
  443. if (!is_numeric($realNumber)) {
  444. return Functions::VALUE();
  445. }
  446. return (string) $complex->pow($realNumber);
  447. }
  448. }