complex.test.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. var assert = require("assert");
  2. var Complex = require("../complex.js");
  3. var functionTests = [{
  4. set: Complex.I,
  5. fn: "mul",
  6. param: Complex(Math.PI).exp(),
  7. expect: "23.140692632779267i"
  8. }, {
  9. set: new Complex(1, 4),
  10. fn: "mul",
  11. param: 3,
  12. expect: "3 + 12i"
  13. }, {
  14. set: "4 + 3i",
  15. fn: "add",
  16. param: "-3 - 2i",
  17. expect: "1 + i"
  18. }, {
  19. set: "3i",
  20. fn: "add",
  21. param: "-2i",
  22. expect: "i"
  23. }, {
  24. set: "4",
  25. fn: "add",
  26. param: "-3",
  27. expect: "1"
  28. }, {
  29. set: 9,
  30. fn: "sqrt",
  31. expect: "3"
  32. }, {
  33. set: -9,
  34. fn: "sqrt",
  35. expect: "3i"
  36. }, {
  37. set: "-36",
  38. fn: "sqrt",
  39. expect: "6i"
  40. }, {
  41. set: "36i",
  42. fn: "sqrt",
  43. expect: "4.242640687119285 + 4.242640687119285i"
  44. }, {
  45. set: Infinity,
  46. fn: "mul",
  47. param: "i",
  48. expect: "Infinity"
  49. }, {
  50. set: "-36i",
  51. fn: "sqrt",
  52. expect: "4.242640687119285 - 4.242640687119285i"
  53. }, {
  54. set: "4 + 2i",
  55. fn: "div",
  56. param: "0",
  57. expect: "Infinity"
  58. }, {
  59. set: "0",
  60. fn: "div",
  61. param: Infinity,
  62. expect: "0"
  63. }, {
  64. set: -Infinity,
  65. fn: "div",
  66. param: 0,
  67. expect: "Infinity"
  68. }, {
  69. set: Infinity,
  70. fn: "div",
  71. param: Infinity,
  72. expect: "NaN"
  73. }, {
  74. set: 0,
  75. fn: "div",
  76. param: 0,
  77. expect: "NaN"
  78. }, {
  79. set: "4 + 2i",
  80. fn: "div",
  81. param: "1 + i",
  82. expect: "3 - i"
  83. }, {
  84. set: "25",
  85. fn: "div",
  86. param: "3 - 4i",
  87. expect: "3 + 4i"
  88. }, {
  89. set: "3 - 2i",
  90. fn: "div",
  91. param: "i",
  92. expect: "-2 - 3i"
  93. }, {
  94. set: "4i",
  95. fn: "mul",
  96. param: "-5i",
  97. expect: "20"
  98. }, {
  99. set: "3 - 6i",
  100. fn: "mul",
  101. param: "i",
  102. expect: "6 + 3i"
  103. }, {
  104. set: Infinity,
  105. fn: "mul",
  106. param: 0,
  107. expect: "NaN"
  108. }, {
  109. set: "3 + 4i",
  110. fn: "add",
  111. param: "5 - 7i",
  112. expect: "8 - 3i"
  113. }, {
  114. set: Infinity,
  115. fn: "add",
  116. param: Infinity,
  117. expect: "NaN"
  118. }, {
  119. set: -Infinity,
  120. fn: "sub",
  121. param: -Infinity,
  122. expect: "NaN"
  123. }, {
  124. set: "6i",
  125. fn: "div",
  126. param: "3 - 12i",
  127. expect: "-0.47058823529411764 + 0.11764705882352941i"
  128. }, {
  129. set: "36 + 36i",
  130. fn: "sqrt",
  131. expect: "6.59210468080686 + 2.730539163373364i"
  132. }, {
  133. set: "36 - 36i",
  134. fn: "sqrt",
  135. expect: "6.59210468080686 - 2.730539163373364i"
  136. }, {
  137. set: "-36 + 36i",
  138. fn: "sqrt",
  139. expect: "2.730539163373364 + 6.59210468080686i"
  140. }, {
  141. set: "-36 - 36i",
  142. fn: "sqrt",
  143. expect: "2.730539163373364 - 6.59210468080686i"
  144. }, {
  145. set: "0",
  146. fn: "sqrt",
  147. expect: "0"
  148. }, {
  149. set: Math.E,
  150. fn: "log",
  151. expect: "1"
  152. }, {
  153. set: 0,
  154. fn: "log",
  155. expect: "Infinity"
  156. }, {
  157. set: Infinity,
  158. fn: "mul",
  159. param: 3,
  160. expect: "Infinity"
  161. }, {
  162. set: "-1",
  163. fn: "log",
  164. expect: Math.PI + "i"
  165. }, {
  166. set: "i",
  167. fn: "log",
  168. expect: (Math.PI / 2) + "i"
  169. }, {
  170. set: "3 + 2i",
  171. fn: "log",
  172. expect: Math.log(13) / 2 + " + " + Math.atan2(2, 3) + "i"
  173. }, {
  174. set: "3 - 2i",
  175. fn: "log",
  176. expect: Math.log(13) / 2 + " - " + Math.atan2(2, 3) + "i"
  177. }, {
  178. set: 1,
  179. fn: "exp",
  180. expect: "" + Math.E
  181. }, {
  182. set: "i",
  183. fn: "exp",
  184. expect: Math.cos(1) + " + " + Math.sin(1) + "i"
  185. }, {
  186. set: "i",
  187. fn: "mul",
  188. param: "i",
  189. expect: "-1"
  190. }, {
  191. set: "3 + 2i",
  192. fn: "exp",
  193. expect: "-8.358532650935372 + 18.263727040666765i"
  194. }, {
  195. set: "3 - 2i",
  196. fn: "exp",
  197. expect: "-8.358532650935372 - 18.263727040666765i"
  198. }, {
  199. set: "3 - 2i",
  200. fn: "expm1",
  201. expect: "-9.358532650935372 - 18.263727040666765i"
  202. }, {
  203. set: "0",
  204. fn: "expm1",
  205. expect: "0"
  206. }, {
  207. set: "1e-6",
  208. fn: "expm1",
  209. expect: "0.0000010000005000001665"
  210. }, {
  211. set: "1e-5 + 5i",
  212. fn: "expm1",
  213. expect: "-0.716334977900736 - 0.9589338639538314i"
  214. }, {
  215. set: "1.2e-7 - 2e-6i",
  216. fn: "expm1",
  217. expect: "1.1999800719976027e-7 - 0.000002000000239998681i"
  218. }, {
  219. set: "3",
  220. fn: "pow",
  221. param: "3",
  222. expect: "27"
  223. }, {
  224. set: -2,
  225. fn: "pow",
  226. param: 1.5,
  227. expect: "-2.82842712474619i"
  228. }, {
  229. set: -8,
  230. fn: "pow",
  231. param: 1 / 3,
  232. expect: "1 + 1.732050807568877i"
  233. }, {
  234. set: -25,
  235. fn: "sqrt",
  236. expect: "5i"
  237. }, {
  238. set: -25,
  239. fn: "pow",
  240. param: 0.5,
  241. expect: "5i"
  242. }, {
  243. set: "0",
  244. fn: "pow",
  245. param: "1+i",
  246. expect: "0"
  247. }, {
  248. set: "i",
  249. fn: "pow",
  250. param: "0",
  251. expect: "1"
  252. }, {
  253. set: "87",
  254. fn: "pow",
  255. param: "3",
  256. expect: "658503"
  257. }, {
  258. set: "i",
  259. fn: "pow",
  260. param: "1",
  261. expect: "i"
  262. }, {
  263. set: "i",
  264. fn: "pow",
  265. param: "2",
  266. expect: "-1"
  267. }, {
  268. set: "i",
  269. fn: "pow",
  270. param: "3",
  271. expect: "-i"
  272. }, {
  273. set: "i",
  274. fn: "pow",
  275. param: "4",
  276. expect: "1"
  277. }, {
  278. set: "i",
  279. fn: "pow",
  280. param: "5",
  281. expect: "i"
  282. }, {
  283. set: 7,
  284. fn: "pow",
  285. param: 2,
  286. expect: '49'
  287. }, {
  288. set: 0,
  289. fn: "pow",
  290. param: 2,
  291. expect: '0'
  292. }, {
  293. set: "3i",
  294. fn: "pow",
  295. param: "3i",
  296. expect: "-0.008876640735623678 - 0.0013801328997494863i"
  297. }, {
  298. set: { re: 3, im: 4 },
  299. fn: "abs",
  300. expect: "5"
  301. }, {
  302. set: { re: 10, im: 24 },
  303. fn: "abs",
  304. expect: "26"
  305. }, {
  306. set: "+++++--+1 + 4i",
  307. fn: "mul",
  308. param: "3 + 2i",
  309. expect: "-5 + 14i"
  310. }, {
  311. set: "4 + 16i",
  312. fn: "div",
  313. param: "4.0000",
  314. expect: "1 + 4i"
  315. }, {
  316. set: { re: -7.1, im: 2.5 },
  317. fn: "neg",
  318. expect: "7.1 - 2.5i"
  319. }, {
  320. set: { re: 1, im: 1 },
  321. fn: "div",
  322. param: { re: 3, im: 4 },
  323. expect: 7 / 25 + " - " + 1 / 25 + "i"
  324. }, {
  325. set: new Complex(-7.1, 2.5),
  326. fn: "neg",
  327. expect: "7.1 - 2.5i"
  328. }, {
  329. set: { re: 1, im: 1 },
  330. fn: "arg",
  331. expect: "" + Math.PI / 4
  332. }, {
  333. set: { re: -1, im: -1 },
  334. fn: "arg",
  335. expect: "" + -3 * Math.PI / 4
  336. }, {
  337. set: { re: 0, im: 1 },
  338. fn: "arg",
  339. expect: "" + Math.PI / 2
  340. }, {
  341. set: { re: 1, im: 0.5 * Math.sqrt(4 / 3) },
  342. fn: "arg",
  343. expect: "" + Math.PI / 6
  344. }, {
  345. set: "3 + 4i",
  346. fn: "conjugate",
  347. expect: "3 - 4i"
  348. }, {
  349. set: { re: 99, im: 50 },
  350. fn: "conjugate",
  351. expect: "99 - 50i"
  352. }, {
  353. set: { re: 0, im: 0 },
  354. fn: "conjugate",
  355. expect: "0"
  356. }, {
  357. set: { re: 1, im: 23 },
  358. fn: "conjugate",
  359. expect: "1 - 23i"
  360. }, {
  361. set: "2 + 8i",
  362. fn: "div",
  363. param: new Complex(1, 2),
  364. expect: "3.6 + 0.8i"
  365. }, {
  366. set: "2 + 8i",
  367. fn: "div",
  368. param: "2 + 8i",
  369. expect: "1"
  370. }, {
  371. set: -Infinity,
  372. fn: "div",
  373. param: 3,
  374. expect: "Infinity"
  375. }, {
  376. set: "3+4i",
  377. fn: "add",
  378. param: "5 - i",
  379. expect: "8 + 3i"
  380. }, {
  381. set: { re: 1, im: 2 },
  382. fn: "add",
  383. param: "4 + 6i",
  384. expect: "5 + 8i"
  385. }, {
  386. set: { re: 5, im: 8 },
  387. fn: "sub",
  388. param: "4 + 6i",
  389. expect: "1 + 2i"
  390. }, {
  391. set: "3 + 4i",
  392. fn: "sub",
  393. param: "2 - 5i",
  394. expect: "1 + 9i"
  395. }, {
  396. set: "1 + 2i",
  397. fn: "pow",
  398. param: "2",
  399. expect: "-2.999999999999999 + 4.000000000000001i"
  400. }, {
  401. set: "1 + 2i",
  402. fn: "pow",
  403. param: "1 + 2i",
  404. expect: "-0.22251715680177267 + 0.10070913113607541i"
  405. }, {
  406. set: { re: 1, im: 2 },
  407. fn: "pow",
  408. param: new Complex(3, 4),
  409. expect: "0.1290095940744669 + 0.03392409290517001i"
  410. }, {
  411. fn: "abs",
  412. set: new Complex(3, 4),
  413. expect: "5"
  414. }, {
  415. param: 2,
  416. fn: "pow",
  417. set: new Complex(1, 2),
  418. expect: "-2.999999999999999 + 4.000000000000001i"
  419. }, {
  420. set: "i",
  421. fn: "pow",
  422. param: 7,
  423. expect: "-i"
  424. }, {
  425. set: "2+3i",
  426. fn: "mul",
  427. param: "4+5i",
  428. expect: "-7 + 22i"
  429. }, {
  430. set: "3 + 4i",
  431. fn: "mul",
  432. param: "2 - 5i",
  433. expect: "26 - 7i"
  434. }, {
  435. set: "i",
  436. fn: "pow",
  437. param: 4,
  438. expect: "1"
  439. }, {
  440. set: "i",
  441. fn: "pow",
  442. param: 5,
  443. expect: "i"
  444. }, {
  445. set: "0-0i",
  446. fn: "pow",
  447. param: 2,
  448. expect: "0"
  449. }, {
  450. set: "0-0i",
  451. fn: "pow",
  452. param: 0,
  453. expect: "1"
  454. }, {
  455. set: "1 + 4i",
  456. fn: "sqrt",
  457. expect: "1.600485180440241 + 1.2496210676876531i"
  458. }, {
  459. set: { re: -3, im: 4 },
  460. fn: "sqrt",
  461. expect: "1 + 2i"
  462. }, {
  463. set: { re: 3, im: -4 },
  464. fn: "sqrt",
  465. expect: "2 - i"
  466. }, {
  467. set: { re: -3, im: -4 },
  468. fn: "sqrt",
  469. expect: "1 - 2i"
  470. }, {
  471. set: [-2, 0],
  472. fn: "pow",
  473. param: 2,
  474. expect: "4"
  475. }, {
  476. set: { abs: 1, arg: 0 },
  477. fn: "equals",
  478. param: { re: 1, im: 0 },
  479. expect: "true"
  480. }, {
  481. set: -Complex.E.pow(2),
  482. fn: "log",
  483. expect: "2 + 3.141592653589793i"
  484. }, {
  485. set: "4 + 3i",
  486. fn: "log",
  487. expect: "1.6094379124341003 + 0.6435011087932844i"
  488. }, {
  489. set: "4 + 3i",
  490. fn: "exp",
  491. expect: "-54.051758861078156 + 7.704891372731154i"
  492. }, {
  493. set: "1-2i",
  494. fn: "sqrt",
  495. expect: "1.272019649514069 - 0.7861513777574233i"
  496. }, {
  497. set: { re: 1, im: 2 },
  498. fn: "sin",
  499. expect: "3.165778513216168 + 1.9596010414216063i"
  500. }, {
  501. set: "i",
  502. fn: "cos",
  503. expect: "1.5430806348152437"
  504. }, {
  505. set: "i",
  506. fn: "acos",
  507. expect: "1.5707963267948966 - 0.8813735870195428i"
  508. }, {
  509. set: { re: 1, im: 2 },
  510. fn: "cos",
  511. expect: "2.0327230070196656 - 3.0518977991518i"
  512. }, {
  513. set: { re: 1, im: 2 },
  514. fn: "tan",
  515. expect: "0.03381282607989669 + 1.0147936161466335i"
  516. }, {
  517. set: { re: 1, im: 3 },
  518. fn: "sinh",
  519. expect: "-1.1634403637032504 + 0.21775955162215221i"
  520. }, {
  521. set: { re: 1, im: 3 },
  522. fn: "cosh",
  523. expect: "-1.5276382501165433 + 0.1658444019189788i"
  524. }, {
  525. set: { re: 1, im: 3 },
  526. fn: "tanh",
  527. expect: "0.7680176472869112 - 0.059168539566050726i"
  528. }, {
  529. set: { re: 1, im: 3 },
  530. fn: "inverse",
  531. expect: "0.1 - 0.3i"
  532. }, {
  533. set: "3+4i",
  534. fn: "inverse",
  535. expect: "0.12 - 0.16i" // 3/25 - (4/25)i
  536. }, {
  537. set: { re: 0.5, im: -0.5 },
  538. fn: "inverse",
  539. expect: "1 + i"
  540. }, {
  541. set: "1 + i",
  542. fn: "inverse",
  543. expect: "0.5 - 0.5i"
  544. }, {
  545. set: "0",
  546. fn: "inverse",
  547. expect: "Infinity"
  548. }, {
  549. set: Infinity,
  550. fn: "inverse",
  551. expect: "0"
  552. }, {
  553. set: Complex['EPSILON'],
  554. fn: "equals",
  555. param: 1e-16,
  556. expect: "true"
  557. }, {
  558. set: 0,
  559. fn: "equals",
  560. param: "5i",
  561. expect: "false"
  562. }, {
  563. set: 5,
  564. fn: "equals",
  565. param: "5i",
  566. expect: "false"
  567. }, {
  568. set: 5,
  569. fn: "equals",
  570. param: 5,
  571. expect: "true"
  572. }, {
  573. set: "10i",
  574. fn: "equals",
  575. param: "10i",
  576. expect: "true"
  577. }, {
  578. set: "2 + 3i",
  579. fn: "equals",
  580. param: "2 + 3i",
  581. expect: "true"
  582. }, {
  583. set: "2 + 3i",
  584. fn: "equals",
  585. param: "5i",
  586. expect: "false"
  587. }, {
  588. set: "2 + 3i",
  589. fn: "round",
  590. param: "0",
  591. expect: "2 + 3i"
  592. }, {
  593. set: "2.5 + 3.5i",
  594. fn: "round",
  595. param: "1",
  596. expect: "2.5 + 3.5i"
  597. }, {
  598. set: "2.5 + 3.5i",
  599. fn: "sign",
  600. param: null,
  601. expect: "0.5812381937190965 + 0.813733471206735i"
  602. }, {
  603. set: "10 + 24i",
  604. fn: "sign",
  605. param: null,
  606. expect: "0.38461538461538464 + 0.9230769230769231i"
  607. }, {
  608. set: "1e3i",
  609. fn: "add",
  610. param: "3e-3 + 1e2i",
  611. expect: "0.003 + 1100i"
  612. }, {
  613. set: "3.14-4i",
  614. fn: "coth",
  615. expect: "0.9994481238383576 + 0.0037048958915019857i"
  616. }, {
  617. set: "8i-31",
  618. fn: "cot",
  619. expect: "1.6636768291213935e-7 - 1.0000001515864902i"
  620. }, {
  621. set: Complex(1, 1).sub(0, 1), // Distance
  622. fn: "abs",
  623. expect: "1"
  624. }, {
  625. set: Complex(1, 1), // Rotate around center
  626. fn: "mul",
  627. param: { abs: 1, arg: Math.PI / 2 },
  628. expect: "-0.9999999999999999 + i"
  629. }, {
  630. set: Complex(1, 1).sub(0, 1).mul({ abs: 1, arg: Math.PI / 2 }), // Rotate around another point
  631. fn: "add",
  632. param: "i",
  633. expect: "2i"
  634. }, {
  635. set: Complex(0, 10000000000),
  636. fn: "log",
  637. param: null,
  638. expect: "23.025850929940457 + 1.5707963267948966i"
  639. }, {
  640. set: Complex(0, 1000000000000000),
  641. fn: "log",
  642. param: null,
  643. expect: "34.538776394910684 + 1.5707963267948966i"
  644. }, {
  645. set: Complex(0, 100000000000000000),
  646. fn: "log",
  647. param: null,
  648. expect: "39.14394658089878 + 1.5707963267948966i"
  649. }, {
  650. set: Complex(0, 10000000000000000000),
  651. fn: "log",
  652. param: null,
  653. expect: "43.74911676688687 + 1.5707963267948966i"
  654. }, {
  655. set: Complex(0, 1e+30),
  656. fn: "log",
  657. param: null,
  658. expect: "69.07755278982137 + 1.5707963267948966i"
  659. }, {
  660. set: Complex(1, 10000000000),
  661. fn: "log",
  662. param: null,
  663. expect: "23.025850929940454 + 1.5707963266948965i"
  664. }, {
  665. set: Complex(1, 1000000000000000),
  666. fn: "log",
  667. param: null,
  668. expect: "34.538776394910684 + 1.5707963267948957i"
  669. }, {
  670. set: Complex(1, 100000000000000000),
  671. fn: "log",
  672. param: null,
  673. expect: "39.14394658089878 + 1.5707963267948966i"
  674. }, {
  675. set: Complex(1, 10000000000000000000),
  676. fn: "log",
  677. param: null,
  678. expect: "43.74911676688687 + 1.5707963267948966i"
  679. }, {
  680. set: Complex(1, 1e+30),
  681. fn: "log",
  682. param: null,
  683. expect: "69.07755278982137 + 1.5707963267948966i"
  684. }, {
  685. set: Complex(-1, 10000000000),
  686. fn: "log",
  687. param: null,
  688. expect: "23.025850929940454 + 1.5707963268948968i"
  689. }, {
  690. set: Complex(-1, 1000000000000000),
  691. fn: "log",
  692. param: null,
  693. expect: "34.538776394910684 + 1.5707963267948977i"
  694. }, {
  695. set: Complex(-1, 100000000000000000),
  696. fn: "log",
  697. param: null,
  698. expect: "39.14394658089878 + 1.5707963267948968i"
  699. }, {
  700. set: Complex(-1, 10000000000000000000),
  701. fn: "log",
  702. param: null,
  703. expect: "43.74911676688687 + 1.5707963267948966i"
  704. }, {
  705. set: Complex(-1, 1e+30),
  706. fn: "log",
  707. param: null,
  708. expect: "69.07755278982137 + 1.5707963267948966i"
  709. }];
  710. var constructorTests = [{
  711. set: null,
  712. expect: "0"
  713. }, {
  714. set: undefined,
  715. expect: "0"
  716. }, {
  717. set: "foo",
  718. error: "SyntaxError: Invalid Param"
  719. }, {
  720. set: {},
  721. error: "SyntaxError: Invalid Param"
  722. }, {
  723. set: " + i",
  724. expect: "i"
  725. }, {
  726. set: "3+4i",
  727. expect: "3 + 4i"
  728. }, {
  729. set: "i",
  730. expect: "i"
  731. }, {
  732. set: "3",
  733. expect: "3"
  734. }, {
  735. set: [9, 8],
  736. expect: "9 + 8i"
  737. }, {
  738. set: "2.3",
  739. expect: "2.3"
  740. }, {
  741. set: "2.3",
  742. expect: "2.3"
  743. }, {
  744. set: "0",
  745. expect: "0"
  746. }, {
  747. set: "-0",
  748. expect: "0"
  749. }, {
  750. set: { re: -0, im: 0 },
  751. expect: "0"
  752. }, {
  753. set: { re: 0, im: -0 },
  754. expect: "0"
  755. }, {
  756. set: Infinity,
  757. expect: "Infinity"
  758. }, {
  759. set: -Infinity,
  760. expect: "Infinity"
  761. }, {
  762. set: { re: Infinity, im: 0 },
  763. expect: "Infinity"
  764. }, {
  765. set: { re: -Infinity, im: 0 },
  766. expect: "Infinity"
  767. }, {
  768. set: { re: 0, im: Infinity },
  769. expect: "Infinity"
  770. }, {
  771. set: { re: 0, im: -Infinity },
  772. expect: "Infinity"
  773. }, {
  774. set: " + 7 - i + 3i - + + + + 43 + 2i - i4 + - 33 + 65 - 1 ",
  775. expect: "-5"
  776. }, {
  777. set: " + 7 - i + 3i - + + + + 43 + 2i - i4 + - 33 + 65 - 1 + ",
  778. error: "SyntaxError: Invalid Param"
  779. }, {
  780. set: "-3x + 4",
  781. error: "SyntaxError: Invalid Param"
  782. }, {
  783. set: "- + 7",
  784. expect: "-7"
  785. }, {
  786. set: "4 5i",
  787. error: "SyntaxError: Invalid Param"
  788. }, {
  789. set: "-",
  790. error: "SyntaxError: Invalid Param"
  791. }, {
  792. set: "2.2e-1-3.2e-1i",
  793. expect: "0.22 - 0.32i"
  794. }, {
  795. set: "2.2.",
  796. error: "SyntaxError: Invalid Param"
  797. }, {
  798. set: { r: 0, phi: 4 },
  799. expect: "0"
  800. }, {
  801. set: { r: 1, phi: 1 },
  802. expect: "0.5403023058681398 + 0.8414709848078965i"
  803. }, {
  804. set: { r: Infinity, phi: 0 },
  805. expect: "Infinity"
  806. }, {
  807. set: { r: Infinity, phi: 2 },
  808. expect: "Infinity"
  809. }, {
  810. set: { r: Infinity, phi: Infinity },
  811. expect: "NaN"
  812. }, {
  813. set: { r: Infinity, phi: NaN },
  814. expect: "NaN"
  815. }
  816. ];
  817. for (let i = 0, len = constructorTests.length; i < len; ++i) {
  818. if (constructorTests[i].set != null && constructorTests[i].set.hasOwnProperty('r')) {
  819. constructorTests.push({
  820. set: {
  821. abs: constructorTests[i].set.r,
  822. arg: constructorTests[i].set.phi,
  823. },
  824. expect: constructorTests[i].expect
  825. })
  826. }
  827. }
  828. function stringify(value) {
  829. return JSON.stringify(value, function replacer(key, val) {
  830. if (typeof val === "number") {
  831. return val.toString();
  832. }
  833. return val;
  834. })
  835. }
  836. function describeTest(test) {
  837. var ctor = "new Complex(" + (test.set !== undefined ? stringify(test.set) : "") + ")";
  838. var fnCall = test.fn == null
  839. ? ""
  840. : "." + test.fn + "(" + (test.param !== undefined ? stringify(test.param) : "") + ")";
  841. var expectedResult = test.expect == null
  842. ? ""
  843. : " === " + stringify(test.expect);
  844. var error = test.error == null
  845. ? ""
  846. : " should throw " + test.error;
  847. return ctor + fnCall + expectedResult + error;
  848. }
  849. describe("Complex functions", function () {
  850. for (var i = 0; i < functionTests.length; i++) {
  851. (function (test) {
  852. it(describeTest(test), function () {
  853. if (test.error) {
  854. try {
  855. new Complex(test.set)[test.fn](test.param);
  856. } catch (e) {
  857. assert.strictEqual(e.toString(), test.error.toString());
  858. }
  859. } else {
  860. assert.strictEqual(new Complex(test.set)[test.fn](test.param).toString(), test.expect);
  861. }
  862. });
  863. })(functionTests[i]);
  864. }
  865. });
  866. describe("Complex constructor", function () {
  867. for (var i = 0; i < constructorTests.length; i++) {
  868. (function (test) {
  869. it(describeTest(test), function () {
  870. if (test.error) {
  871. try {
  872. new Complex(test.set);
  873. } catch (e) {
  874. assert.strictEqual(e.toString(), test.error.toString());
  875. }
  876. } else {
  877. assert.strictEqual(new Complex(test.set).toString(), test.expect);
  878. }
  879. });
  880. })(constructorTests[i]);
  881. }
  882. });
  883. describe("Complex Details", function () {
  884. it("should work with different params", function () {
  885. assert.strictEqual(Complex(1, -1).toString(), "1 - i");
  886. assert.strictEqual(Complex(0, 0).toString(), "0");
  887. assert.strictEqual(Complex(0, 2).toString(), "2i");
  888. assert.strictEqual(Complex.I.toString(), "i");
  889. assert.strictEqual(Complex(0, -2).toString(), "-2i");
  890. assert.strictEqual(Complex({ re: 0, im: -2 }).toString(), "-2i");
  891. });
  892. it("Complex Combinations", function () {
  893. var zero = Complex(0, 0), one = Complex(1, 1), two = Complex(2, 2);
  894. assert.strictEqual(zero.toString(), "0");
  895. assert.strictEqual(one.toString(), "1 + i");
  896. assert(one.neg().equals(Complex(-1, -1)));
  897. assert(one.conjugate().equals(Complex(1, -1)));
  898. assert.strictEqual(one.abs(), Math.SQRT2);
  899. assert.strictEqual(one.arg(), Math.PI / 4);
  900. assert.strictEqual(one.add(one).toString(), two.toString());
  901. assert.strictEqual(one.sub(one).toString(), zero.toString());
  902. assert.strictEqual(one.mul(2).toString(), two.toString());
  903. assert.strictEqual(one.mul(one).toString(), Complex(0, 2).toString());
  904. assert.strictEqual(one.div(2).toString(), "0.5 + 0.5i");
  905. assert.strictEqual(one.div(one).toString(), "1");
  906. assert.strictEqual(one.div(0).toString(), "Infinity");
  907. assert.strictEqual(one.exp().toString(), "1.4686939399158851 + 2.2873552871788423i");
  908. assert.strictEqual(one.log().toString(), "0.34657359027997264 + 0.7853981633974483i");
  909. assert.strictEqual(one.pow(one).toString(), "0.2739572538301211 + 0.5837007587586147i");
  910. assert.strictEqual(one.pow(zero).toString(), "1");
  911. assert.strictEqual(one.sqrt().toString(), "1.09868411346781 + 0.45508986056222733i");
  912. assert.strictEqual(one.sin().toString(), "1.2984575814159773 + 0.6349639147847361i");
  913. assert.strictEqual(one.cos().toString(), "0.8337300251311491 - 0.9888977057628651i");
  914. assert.strictEqual(one.tan().toString(), "0.27175258531951174 + 1.0839233273386948i");
  915. assert.strictEqual(one.asin().toString(), "0.6662394324925153 + 1.0612750619050355i");
  916. assert.strictEqual(one.acos().toString(), "0.9045568943023813 - 1.0612750619050355i");
  917. assert.strictEqual(one.atan().toString(), "1.0172219678978514 + 0.40235947810852507i");
  918. assert.strictEqual(Complex(3, 4).abs(), 5);
  919. assert.strictEqual(Complex("5i + 3").log().exp().toString(), "3 + 5i")
  920. assert.strictEqual(Complex("-2i - 1").log().exp().toString(), "-1 - 2i")
  921. });
  922. it("should calculate distributed conjugate", function () {
  923. var c1 = Complex(7, 3);
  924. var c2 = Complex(1, 2);
  925. var r1 = c1.add(c2).conjugate();
  926. var r2 = c1.conjugate().add(c2.conjugate());
  927. assert.strictEqual(r1.toString(), r2.toString());
  928. });
  929. it("should be raised to power of 6", function () {
  930. var c1 = Complex(2, 2);
  931. var t = c1.pow(6);
  932. assert.strictEqual(t.toString(), "-9.405287417451663e-14 - 511.9999999999995i");
  933. });
  934. it("should handle inverse trig fns", function () {
  935. var values = [
  936. new Complex(2.3, 1.4),
  937. new Complex(-2.3, 1.4),
  938. new Complex(-2.3, -1.4),
  939. new Complex(2.3, -1.4)];
  940. var fns = ['sin', 'cos', 'tan'];
  941. for (var i = 0; i < values.length; i++) {
  942. for (var j = 0; j < 3; j++) {
  943. var a = values[i]['a' + fns[j]]()[fns[j]]();
  944. var res = values[i];
  945. assert(Math.abs(a.re - res.re) < 1e-12 && Math.abs(a.im - res.im) < 1e-12);
  946. }
  947. }
  948. });
  949. it('should handle get real part', function () {
  950. assert.strictEqual(Complex({ abs: 1, arg: Math.PI / 4 }).re, Math.SQRT2 / 2);
  951. });
  952. it('should handle get complex part', function () {
  953. assert.strictEqual(Complex({ abs: 1, arg: Math.PI / 4 }).im, 0.7071067811865475);
  954. });
  955. it('should handle sum', function () {
  956. assert.strictEqual(Complex({ abs: 1, arg: 0 }).add({ abs: 1, arg: Math.PI / 2 }).abs(), Math.SQRT2);
  957. assert.strictEqual(Complex({ abs: 1, arg: 0 }).add({ abs: 1, arg: Math.PI / 2 }).arg(), Math.PI / 4);
  958. });
  959. it('should handle conjugate', function () {
  960. assert.strictEqual(Complex({ abs: 1, arg: Math.PI / 4 }).conjugate().toString(), Complex({ abs: 1, arg: -Math.PI / 4 }).toString());
  961. });
  962. it('should handle substract', function () {
  963. assert.strictEqual(Complex({ abs: 1, arg: 0 }).sub({ abs: 1, arg: Math.PI / 2 }).abs().toString(), "1.414213562373095");
  964. assert.strictEqual(Complex({ abs: 1, arg: 0 }).sub({ abs: 1, arg: Math.PI / 2 }).arg().toString(), "-0.7853981633974484");
  965. });
  966. it('should handle arg for the first quadrant', function () {
  967. assert.strictEqual(Complex({ re: 1, im: 1 }).arg(), Math.PI / 4);
  968. });
  969. it('should handle arg for the second quadrant', function () {
  970. assert.strictEqual(Complex({ re: -1, im: 1 }).arg(), 3 * Math.PI / 4);
  971. });
  972. it('should handle arg for the third quadrant', function () {
  973. assert.strictEqual(Complex({ re: -1, im: -1 }).arg(), -3 * Math.PI / 4);
  974. });
  975. it('should handle arg for the fourth quadrant', function () {
  976. assert.strictEqual(Complex({ re: 1, im: -1 }).arg(), -Math.PI / 4);
  977. });
  978. it('should handle arg for the fourth and first quadrant', function () {
  979. assert.strictEqual(Complex({ re: 1, im: 0 }).arg(), 0);
  980. });
  981. it('should handle arg for first and second quadrant', function () {
  982. assert.strictEqual(Complex({ re: 0, im: 1 }).arg(), Math.PI / 2);
  983. });
  984. it('should handle arg for the second and third quadrant', function () {
  985. assert.strictEqual(Complex({ re: -1, im: 0 }).arg(), Math.PI);
  986. });
  987. it('should handle arg for the third and fourth quadrant', function () {
  988. assert.strictEqual(Complex({ re: 0, im: -1 }).arg(), -Math.PI / 2);
  989. });
  990. it("should eat its own dog food", function () {
  991. var a = Complex(1, -5).toString();
  992. var b = Complex(a).toString();
  993. var c = Complex(b).mul(a);
  994. assert.strictEqual(c.toString(), '-24 - 10i');
  995. });
  996. it("should calculate the absolute value of i", function () {
  997. var a = Complex("i").sign().inverse().mul("i");
  998. assert.strictEqual(a.toString(), '1');
  999. });
  1000. it('should take the natural logarithm', function () {
  1001. var n = Complex(Math.E * Math.E).log().div("i").mul(-Math.PI * 2, 1);
  1002. assert.strictEqual(n.toString(), '2 + ' + 4 * Math.PI + "i");
  1003. });
  1004. });