complex.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. /**
  2. * @license Complex.js v2.1.1 12/05/2020
  3. *
  4. * Copyright (c) 2020, Robert Eisele (robert@xarg.org)
  5. * Dual licensed under the MIT or GPL Version 2 licenses.
  6. **/
  7. /**
  8. *
  9. * This class allows the manipulation of complex numbers.
  10. * You can pass a complex number in different formats. Either as object, double, string or two integer parameters.
  11. *
  12. * Object form
  13. * { re: <real>, im: <imaginary> }
  14. * { arg: <angle>, abs: <radius> }
  15. * { phi: <angle>, r: <radius> }
  16. *
  17. * Array / Vector form
  18. * [ real, imaginary ]
  19. *
  20. * Double form
  21. * 99.3 - Single double value
  22. *
  23. * String form
  24. * '23.1337' - Simple real number
  25. * '15+3i' - a simple complex number
  26. * '3-i' - a simple complex number
  27. *
  28. * Example:
  29. *
  30. * var c = new Complex('99.3+8i');
  31. * c.mul({r: 3, i: 9}).div(4.9).sub(3, 2);
  32. *
  33. */
  34. (function(root) {
  35. 'use strict';
  36. var cosh = Math.cosh || function(x) {
  37. return Math.abs(x) < 1e-9 ? 1 - x : (Math.exp(x) + Math.exp(-x)) * 0.5;
  38. };
  39. var sinh = Math.sinh || function(x) {
  40. return Math.abs(x) < 1e-9 ? x : (Math.exp(x) - Math.exp(-x)) * 0.5;
  41. };
  42. /**
  43. * Calculates cos(x) - 1 using Taylor series if x is small (-¼π ≤ x ≤ ¼π).
  44. *
  45. * @param {number} x
  46. * @returns {number} cos(x) - 1
  47. */
  48. var cosm1 = function(x) {
  49. var b = Math.PI / 4;
  50. if (-b > x || x > b) {
  51. return Math.cos(x) - 1.0;
  52. }
  53. /* Calculate horner form of polynomial of taylor series in Q
  54. var fac = 1, alt = 1, pol = {};
  55. for (var i = 0; i <= 16; i++) {
  56. fac*= i || 1;
  57. if (i % 2 == 0) {
  58. pol[i] = new Fraction(1, alt * fac);
  59. alt = -alt;
  60. }
  61. }
  62. console.log(new Polynomial(pol).toHorner()); // (((((((1/20922789888000x^2-1/87178291200)x^2+1/479001600)x^2-1/3628800)x^2+1/40320)x^2-1/720)x^2+1/24)x^2-1/2)x^2+1
  63. */
  64. var xx = x * x;
  65. return xx * (
  66. xx * (
  67. xx * (
  68. xx * (
  69. xx * (
  70. xx * (
  71. xx * (
  72. xx / 20922789888000
  73. - 1 / 87178291200)
  74. + 1 / 479001600)
  75. - 1 / 3628800)
  76. + 1 / 40320)
  77. - 1 / 720)
  78. + 1 / 24)
  79. - 1 / 2);
  80. };
  81. var hypot = function(x, y) {
  82. var a = Math.abs(x);
  83. var b = Math.abs(y);
  84. if (a < 3000 && b < 3000) {
  85. return Math.sqrt(a * a + b * b);
  86. }
  87. if (a < b) {
  88. a = b;
  89. b = x / y;
  90. } else {
  91. b = y / x;
  92. }
  93. return a * Math.sqrt(1 + b * b);
  94. };
  95. var parser_exit = function() {
  96. throw SyntaxError('Invalid Param');
  97. };
  98. /**
  99. * Calculates log(sqrt(a^2+b^2)) in a way to avoid overflows
  100. *
  101. * @param {number} a
  102. * @param {number} b
  103. * @returns {number}
  104. */
  105. function logHypot(a, b) {
  106. var _a = Math.abs(a);
  107. var _b = Math.abs(b);
  108. if (a === 0) {
  109. return Math.log(_b);
  110. }
  111. if (b === 0) {
  112. return Math.log(_a);
  113. }
  114. if (_a < 3000 && _b < 3000) {
  115. return Math.log(a * a + b * b) * 0.5;
  116. }
  117. /* I got 4 ideas to compute this property without overflow:
  118. *
  119. * Testing 1000000 times with random samples for a,b ∈ [1, 1000000000] against a big decimal library to get an error estimate
  120. *
  121. * 1. Only eliminate the square root: (OVERALL ERROR: 3.9122483030951116e-11)
  122. Math.log(a * a + b * b) / 2
  123. *
  124. *
  125. * 2. Try to use the non-overflowing pythagoras: (OVERALL ERROR: 8.889760039210159e-10)
  126. var fn = function(a, b) {
  127. a = Math.abs(a);
  128. b = Math.abs(b);
  129. var t = Math.min(a, b);
  130. a = Math.max(a, b);
  131. t = t / a;
  132. return Math.log(a) + Math.log(1 + t * t) / 2;
  133. };
  134. * 3. Abuse the identity cos(atan(y/x) = x / sqrt(x^2+y^2): (OVERALL ERROR: 3.4780178737037204e-10)
  135. Math.log(a / Math.cos(Math.atan2(b, a)))
  136. * 4. Use 3. and apply log rules: (OVERALL ERROR: 1.2014087502620896e-9)
  137. Math.log(a) - Math.log(Math.cos(Math.atan2(b, a)))
  138. */
  139. a = a / 2;
  140. b = b / 2;
  141. return 0.5 * Math.log(a * a + b * b) + Math.LN2;
  142. }
  143. var parse = function(a, b) {
  144. var z = { 're': 0, 'im': 0 };
  145. if (a === undefined || a === null) {
  146. z['re'] =
  147. z['im'] = 0;
  148. } else if (b !== undefined) {
  149. z['re'] = a;
  150. z['im'] = b;
  151. } else
  152. switch (typeof a) {
  153. case 'object':
  154. if ('im' in a && 're' in a) {
  155. z['re'] = a['re'];
  156. z['im'] = a['im'];
  157. } else if ('abs' in a && 'arg' in a) {
  158. if (!Number.isFinite(a['abs']) && Number.isFinite(a['arg'])) {
  159. return Complex['INFINITY'];
  160. }
  161. z['re'] = a['abs'] * Math.cos(a['arg']);
  162. z['im'] = a['abs'] * Math.sin(a['arg']);
  163. } else if ('r' in a && 'phi' in a) {
  164. if (!Number.isFinite(a['r']) && Number.isFinite(a['phi'])) {
  165. return Complex['INFINITY'];
  166. }
  167. z['re'] = a['r'] * Math.cos(a['phi']);
  168. z['im'] = a['r'] * Math.sin(a['phi']);
  169. } else if (a.length === 2) { // Quick array check
  170. z['re'] = a[0];
  171. z['im'] = a[1];
  172. } else {
  173. parser_exit();
  174. }
  175. break;
  176. case 'string':
  177. z['im'] = /* void */
  178. z['re'] = 0;
  179. var tokens = a.match(/\d+\.?\d*e[+-]?\d+|\d+\.?\d*|\.\d+|./g);
  180. var plus = 1;
  181. var minus = 0;
  182. if (tokens === null) {
  183. parser_exit();
  184. }
  185. for (var i = 0; i < tokens.length; i++) {
  186. var c = tokens[i];
  187. if (c === ' ' || c === '\t' || c === '\n') {
  188. /* void */
  189. } else if (c === '+') {
  190. plus++;
  191. } else if (c === '-') {
  192. minus++;
  193. } else if (c === 'i' || c === 'I') {
  194. if (plus + minus === 0) {
  195. parser_exit();
  196. }
  197. if (tokens[i + 1] !== ' ' && !isNaN(tokens[i + 1])) {
  198. z['im'] += parseFloat((minus % 2 ? '-' : '') + tokens[i + 1]);
  199. i++;
  200. } else {
  201. z['im'] += parseFloat((minus % 2 ? '-' : '') + '1');
  202. }
  203. plus = minus = 0;
  204. } else {
  205. if (plus + minus === 0 || isNaN(c)) {
  206. parser_exit();
  207. }
  208. if (tokens[i + 1] === 'i' || tokens[i + 1] === 'I') {
  209. z['im'] += parseFloat((minus % 2 ? '-' : '') + c);
  210. i++;
  211. } else {
  212. z['re'] += parseFloat((minus % 2 ? '-' : '') + c);
  213. }
  214. plus = minus = 0;
  215. }
  216. }
  217. // Still something on the stack
  218. if (plus + minus > 0) {
  219. parser_exit();
  220. }
  221. break;
  222. case 'number':
  223. z['im'] = 0;
  224. z['re'] = a;
  225. break;
  226. default:
  227. parser_exit();
  228. }
  229. if (isNaN(z['re']) || isNaN(z['im'])) {
  230. // If a calculation is NaN, we treat it as NaN and don't throw
  231. //parser_exit();
  232. }
  233. return z;
  234. };
  235. /**
  236. * @constructor
  237. * @returns {Complex}
  238. */
  239. function Complex(a, b) {
  240. if (!(this instanceof Complex)) {
  241. return new Complex(a, b);
  242. }
  243. var z = parse(a, b);
  244. this['re'] = z['re'];
  245. this['im'] = z['im'];
  246. }
  247. Complex.prototype = {
  248. 're': 0,
  249. 'im': 0,
  250. /**
  251. * Calculates the sign of a complex number, which is a normalized complex
  252. *
  253. * @returns {Complex}
  254. */
  255. 'sign': function() {
  256. var abs = this['abs']();
  257. return new Complex(
  258. this['re'] / abs,
  259. this['im'] / abs);
  260. },
  261. /**
  262. * Adds two complex numbers
  263. *
  264. * @returns {Complex}
  265. */
  266. 'add': function(a, b) {
  267. var z = new Complex(a, b);
  268. // Infinity + Infinity = NaN
  269. if (this['isInfinite']() && z['isInfinite']()) {
  270. return Complex['NAN'];
  271. }
  272. // Infinity + z = Infinity { where z != Infinity }
  273. if (this['isInfinite']() || z['isInfinite']()) {
  274. return Complex['INFINITY'];
  275. }
  276. return new Complex(
  277. this['re'] + z['re'],
  278. this['im'] + z['im']);
  279. },
  280. /**
  281. * Subtracts two complex numbers
  282. *
  283. * @returns {Complex}
  284. */
  285. 'sub': function(a, b) {
  286. var z = new Complex(a, b);
  287. // Infinity - Infinity = NaN
  288. if (this['isInfinite']() && z['isInfinite']()) {
  289. return Complex['NAN'];
  290. }
  291. // Infinity - z = Infinity { where z != Infinity }
  292. if (this['isInfinite']() || z['isInfinite']()) {
  293. return Complex['INFINITY'];
  294. }
  295. return new Complex(
  296. this['re'] - z['re'],
  297. this['im'] - z['im']);
  298. },
  299. /**
  300. * Multiplies two complex numbers
  301. *
  302. * @returns {Complex}
  303. */
  304. 'mul': function(a, b) {
  305. var z = new Complex(a, b);
  306. // Infinity * 0 = NaN
  307. if ((this['isInfinite']() && z['isZero']()) || (this['isZero']() && z['isInfinite']())) {
  308. return Complex['NAN'];
  309. }
  310. // Infinity * z = Infinity { where z != 0 }
  311. if (this['isInfinite']() || z['isInfinite']()) {
  312. return Complex['INFINITY'];
  313. }
  314. // Short circuit for real values
  315. if (z['im'] === 0 && this['im'] === 0) {
  316. return new Complex(this['re'] * z['re'], 0);
  317. }
  318. return new Complex(
  319. this['re'] * z['re'] - this['im'] * z['im'],
  320. this['re'] * z['im'] + this['im'] * z['re']);
  321. },
  322. /**
  323. * Divides two complex numbers
  324. *
  325. * @returns {Complex}
  326. */
  327. 'div': function(a, b) {
  328. var z = new Complex(a, b);
  329. // 0 / 0 = NaN and Infinity / Infinity = NaN
  330. if ((this['isZero']() && z['isZero']()) || (this['isInfinite']() && z['isInfinite']())) {
  331. return Complex['NAN'];
  332. }
  333. // Infinity / 0 = Infinity
  334. if (this['isInfinite']() || z['isZero']()) {
  335. return Complex['INFINITY'];
  336. }
  337. // 0 / Infinity = 0
  338. if (this['isZero']() || z['isInfinite']()) {
  339. return Complex['ZERO'];
  340. }
  341. a = this['re'];
  342. b = this['im'];
  343. var c = z['re'];
  344. var d = z['im'];
  345. var t, x;
  346. if (0 === d) {
  347. // Divisor is real
  348. return new Complex(a / c, b / c);
  349. }
  350. if (Math.abs(c) < Math.abs(d)) {
  351. x = c / d;
  352. t = c * x + d;
  353. return new Complex(
  354. (a * x + b) / t,
  355. (b * x - a) / t);
  356. } else {
  357. x = d / c;
  358. t = d * x + c;
  359. return new Complex(
  360. (a + b * x) / t,
  361. (b - a * x) / t);
  362. }
  363. },
  364. /**
  365. * Calculate the power of two complex numbers
  366. *
  367. * @returns {Complex}
  368. */
  369. 'pow': function(a, b) {
  370. var z = new Complex(a, b);
  371. a = this['re'];
  372. b = this['im'];
  373. if (z['isZero']()) {
  374. return Complex['ONE'];
  375. }
  376. // If the exponent is real
  377. if (z['im'] === 0) {
  378. if (b === 0 && a > 0) {
  379. return new Complex(Math.pow(a, z['re']), 0);
  380. } else if (a === 0) { // If base is fully imaginary
  381. switch ((z['re'] % 4 + 4) % 4) {
  382. case 0:
  383. return new Complex(Math.pow(b, z['re']), 0);
  384. case 1:
  385. return new Complex(0, Math.pow(b, z['re']));
  386. case 2:
  387. return new Complex(-Math.pow(b, z['re']), 0);
  388. case 3:
  389. return new Complex(0, -Math.pow(b, z['re']));
  390. }
  391. }
  392. }
  393. /* I couldn't find a good formula, so here is a derivation and optimization
  394. *
  395. * z_1^z_2 = (a + bi)^(c + di)
  396. * = exp((c + di) * log(a + bi)
  397. * = pow(a^2 + b^2, (c + di) / 2) * exp(i(c + di)atan2(b, a))
  398. * =>...
  399. * Re = (pow(a^2 + b^2, c / 2) * exp(-d * atan2(b, a))) * cos(d * log(a^2 + b^2) / 2 + c * atan2(b, a))
  400. * Im = (pow(a^2 + b^2, c / 2) * exp(-d * atan2(b, a))) * sin(d * log(a^2 + b^2) / 2 + c * atan2(b, a))
  401. *
  402. * =>...
  403. * Re = exp(c * log(sqrt(a^2 + b^2)) - d * atan2(b, a)) * cos(d * log(sqrt(a^2 + b^2)) + c * atan2(b, a))
  404. * Im = exp(c * log(sqrt(a^2 + b^2)) - d * atan2(b, a)) * sin(d * log(sqrt(a^2 + b^2)) + c * atan2(b, a))
  405. *
  406. * =>
  407. * Re = exp(c * logsq2 - d * arg(z_1)) * cos(d * logsq2 + c * arg(z_1))
  408. * Im = exp(c * logsq2 - d * arg(z_1)) * sin(d * logsq2 + c * arg(z_1))
  409. *
  410. */
  411. if (a === 0 && b === 0 && z['re'] > 0 && z['im'] >= 0) {
  412. return Complex['ZERO'];
  413. }
  414. var arg = Math.atan2(b, a);
  415. var loh = logHypot(a, b);
  416. a = Math.exp(z['re'] * loh - z['im'] * arg);
  417. b = z['im'] * loh + z['re'] * arg;
  418. return new Complex(
  419. a * Math.cos(b),
  420. a * Math.sin(b));
  421. },
  422. /**
  423. * Calculate the complex square root
  424. *
  425. * @returns {Complex}
  426. */
  427. 'sqrt': function() {
  428. var a = this['re'];
  429. var b = this['im'];
  430. var r = this['abs']();
  431. var re, im;
  432. if (a >= 0) {
  433. if (b === 0) {
  434. return new Complex(Math.sqrt(a), 0);
  435. }
  436. re = 0.5 * Math.sqrt(2.0 * (r + a));
  437. } else {
  438. re = Math.abs(b) / Math.sqrt(2 * (r - a));
  439. }
  440. if (a <= 0) {
  441. im = 0.5 * Math.sqrt(2.0 * (r - a));
  442. } else {
  443. im = Math.abs(b) / Math.sqrt(2 * (r + a));
  444. }
  445. return new Complex(re, b < 0 ? -im : im);
  446. },
  447. /**
  448. * Calculate the complex exponent
  449. *
  450. * @returns {Complex}
  451. */
  452. 'exp': function() {
  453. var tmp = Math.exp(this['re']);
  454. if (this['im'] === 0) {
  455. //return new Complex(tmp, 0);
  456. }
  457. return new Complex(
  458. tmp * Math.cos(this['im']),
  459. tmp * Math.sin(this['im']));
  460. },
  461. /**
  462. * Calculate the complex exponent and subtracts one.
  463. *
  464. * This may be more accurate than `Complex(x).exp().sub(1)` if
  465. * `x` is small.
  466. *
  467. * @returns {Complex}
  468. */
  469. 'expm1': function() {
  470. /**
  471. * exp(a + i*b) - 1
  472. = exp(a) * (cos(b) + j*sin(b)) - 1
  473. = expm1(a)*cos(b) + cosm1(b) + j*exp(a)*sin(b)
  474. */
  475. var a = this['re'];
  476. var b = this['im'];
  477. return new Complex(
  478. Math.expm1(a) * Math.cos(b) + cosm1(b),
  479. Math.exp(a) * Math.sin(b));
  480. },
  481. /**
  482. * Calculate the natural log
  483. *
  484. * @returns {Complex}
  485. */
  486. 'log': function() {
  487. var a = this['re'];
  488. var b = this['im'];
  489. if (b === 0 && a > 0) {
  490. //return new Complex(Math.log(a), 0);
  491. }
  492. return new Complex(
  493. logHypot(a, b),
  494. Math.atan2(b, a));
  495. },
  496. /**
  497. * Calculate the magnitude of the complex number
  498. *
  499. * @returns {number}
  500. */
  501. 'abs': function() {
  502. return hypot(this['re'], this['im']);
  503. },
  504. /**
  505. * Calculate the angle of the complex number
  506. *
  507. * @returns {number}
  508. */
  509. 'arg': function() {
  510. return Math.atan2(this['im'], this['re']);
  511. },
  512. /**
  513. * Calculate the sine of the complex number
  514. *
  515. * @returns {Complex}
  516. */
  517. 'sin': function() {
  518. // sin(z) = ( e^iz - e^-iz ) / 2i
  519. // = sin(a)cosh(b) + i cos(a)sinh(b)
  520. var a = this['re'];
  521. var b = this['im'];
  522. return new Complex(
  523. Math.sin(a) * cosh(b),
  524. Math.cos(a) * sinh(b));
  525. },
  526. /**
  527. * Calculate the cosine
  528. *
  529. * @returns {Complex}
  530. */
  531. 'cos': function() {
  532. // cos(z) = ( e^iz + e^-iz ) / 2
  533. // = cos(a)cosh(b) - i sin(a)sinh(b)
  534. var a = this['re'];
  535. var b = this['im'];
  536. return new Complex(
  537. Math.cos(a) * cosh(b),
  538. -Math.sin(a) * sinh(b));
  539. },
  540. /**
  541. * Calculate the tangent
  542. *
  543. * @returns {Complex}
  544. */
  545. 'tan': function() {
  546. // tan(z) = sin(z) / cos(z)
  547. // = ( e^iz - e^-iz ) / ( i( e^iz + e^-iz ) )
  548. // = ( e^2iz - 1 ) / i( e^2iz + 1 )
  549. // = ( sin(2a) + i sinh(2b) ) / ( cos(2a) + cosh(2b) )
  550. var a = 2 * this['re'];
  551. var b = 2 * this['im'];
  552. var d = Math.cos(a) + cosh(b);
  553. return new Complex(
  554. Math.sin(a) / d,
  555. sinh(b) / d);
  556. },
  557. /**
  558. * Calculate the cotangent
  559. *
  560. * @returns {Complex}
  561. */
  562. 'cot': function() {
  563. // cot(c) = i(e^(ci) + e^(-ci)) / (e^(ci) - e^(-ci))
  564. var a = 2 * this['re'];
  565. var b = 2 * this['im'];
  566. var d = Math.cos(a) - cosh(b);
  567. return new Complex(
  568. -Math.sin(a) / d,
  569. sinh(b) / d);
  570. },
  571. /**
  572. * Calculate the secant
  573. *
  574. * @returns {Complex}
  575. */
  576. 'sec': function() {
  577. // sec(c) = 2 / (e^(ci) + e^(-ci))
  578. var a = this['re'];
  579. var b = this['im'];
  580. var d = 0.5 * cosh(2 * b) + 0.5 * Math.cos(2 * a);
  581. return new Complex(
  582. Math.cos(a) * cosh(b) / d,
  583. Math.sin(a) * sinh(b) / d);
  584. },
  585. /**
  586. * Calculate the cosecans
  587. *
  588. * @returns {Complex}
  589. */
  590. 'csc': function() {
  591. // csc(c) = 2i / (e^(ci) - e^(-ci))
  592. var a = this['re'];
  593. var b = this['im'];
  594. var d = 0.5 * cosh(2 * b) - 0.5 * Math.cos(2 * a);
  595. return new Complex(
  596. Math.sin(a) * cosh(b) / d,
  597. -Math.cos(a) * sinh(b) / d);
  598. },
  599. /**
  600. * Calculate the complex arcus sinus
  601. *
  602. * @returns {Complex}
  603. */
  604. 'asin': function() {
  605. // asin(c) = -i * log(ci + sqrt(1 - c^2))
  606. var a = this['re'];
  607. var b = this['im'];
  608. var t1 = new Complex(
  609. b * b - a * a + 1,
  610. -2 * a * b)['sqrt']();
  611. var t2 = new Complex(
  612. t1['re'] - b,
  613. t1['im'] + a)['log']();
  614. return new Complex(t2['im'], -t2['re']);
  615. },
  616. /**
  617. * Calculate the complex arcus cosinus
  618. *
  619. * @returns {Complex}
  620. */
  621. 'acos': function() {
  622. // acos(c) = i * log(c - i * sqrt(1 - c^2))
  623. var a = this['re'];
  624. var b = this['im'];
  625. var t1 = new Complex(
  626. b * b - a * a + 1,
  627. -2 * a * b)['sqrt']();
  628. var t2 = new Complex(
  629. t1['re'] - b,
  630. t1['im'] + a)['log']();
  631. return new Complex(Math.PI / 2 - t2['im'], t2['re']);
  632. },
  633. /**
  634. * Calculate the complex arcus tangent
  635. *
  636. * @returns {Complex}
  637. */
  638. 'atan': function() {
  639. // atan(c) = i / 2 log((i + x) / (i - x))
  640. var a = this['re'];
  641. var b = this['im'];
  642. if (a === 0) {
  643. if (b === 1) {
  644. return new Complex(0, Infinity);
  645. }
  646. if (b === -1) {
  647. return new Complex(0, -Infinity);
  648. }
  649. }
  650. var d = a * a + (1.0 - b) * (1.0 - b);
  651. var t1 = new Complex(
  652. (1 - b * b - a * a) / d,
  653. -2 * a / d).log();
  654. return new Complex(-0.5 * t1['im'], 0.5 * t1['re']);
  655. },
  656. /**
  657. * Calculate the complex arcus cotangent
  658. *
  659. * @returns {Complex}
  660. */
  661. 'acot': function() {
  662. // acot(c) = i / 2 log((c - i) / (c + i))
  663. var a = this['re'];
  664. var b = this['im'];
  665. if (b === 0) {
  666. return new Complex(Math.atan2(1, a), 0);
  667. }
  668. var d = a * a + b * b;
  669. return (d !== 0)
  670. ? new Complex(
  671. a / d,
  672. -b / d).atan()
  673. : new Complex(
  674. (a !== 0) ? a / 0 : 0,
  675. (b !== 0) ? -b / 0 : 0).atan();
  676. },
  677. /**
  678. * Calculate the complex arcus secant
  679. *
  680. * @returns {Complex}
  681. */
  682. 'asec': function() {
  683. // asec(c) = -i * log(1 / c + sqrt(1 - i / c^2))
  684. var a = this['re'];
  685. var b = this['im'];
  686. if (a === 0 && b === 0) {
  687. return new Complex(0, Infinity);
  688. }
  689. var d = a * a + b * b;
  690. return (d !== 0)
  691. ? new Complex(
  692. a / d,
  693. -b / d).acos()
  694. : new Complex(
  695. (a !== 0) ? a / 0 : 0,
  696. (b !== 0) ? -b / 0 : 0).acos();
  697. },
  698. /**
  699. * Calculate the complex arcus cosecans
  700. *
  701. * @returns {Complex}
  702. */
  703. 'acsc': function() {
  704. // acsc(c) = -i * log(i / c + sqrt(1 - 1 / c^2))
  705. var a = this['re'];
  706. var b = this['im'];
  707. if (a === 0 && b === 0) {
  708. return new Complex(Math.PI / 2, Infinity);
  709. }
  710. var d = a * a + b * b;
  711. return (d !== 0)
  712. ? new Complex(
  713. a / d,
  714. -b / d).asin()
  715. : new Complex(
  716. (a !== 0) ? a / 0 : 0,
  717. (b !== 0) ? -b / 0 : 0).asin();
  718. },
  719. /**
  720. * Calculate the complex sinh
  721. *
  722. * @returns {Complex}
  723. */
  724. 'sinh': function() {
  725. // sinh(c) = (e^c - e^-c) / 2
  726. var a = this['re'];
  727. var b = this['im'];
  728. return new Complex(
  729. sinh(a) * Math.cos(b),
  730. cosh(a) * Math.sin(b));
  731. },
  732. /**
  733. * Calculate the complex cosh
  734. *
  735. * @returns {Complex}
  736. */
  737. 'cosh': function() {
  738. // cosh(c) = (e^c + e^-c) / 2
  739. var a = this['re'];
  740. var b = this['im'];
  741. return new Complex(
  742. cosh(a) * Math.cos(b),
  743. sinh(a) * Math.sin(b));
  744. },
  745. /**
  746. * Calculate the complex tanh
  747. *
  748. * @returns {Complex}
  749. */
  750. 'tanh': function() {
  751. // tanh(c) = (e^c - e^-c) / (e^c + e^-c)
  752. var a = 2 * this['re'];
  753. var b = 2 * this['im'];
  754. var d = cosh(a) + Math.cos(b);
  755. return new Complex(
  756. sinh(a) / d,
  757. Math.sin(b) / d);
  758. },
  759. /**
  760. * Calculate the complex coth
  761. *
  762. * @returns {Complex}
  763. */
  764. 'coth': function() {
  765. // coth(c) = (e^c + e^-c) / (e^c - e^-c)
  766. var a = 2 * this['re'];
  767. var b = 2 * this['im'];
  768. var d = cosh(a) - Math.cos(b);
  769. return new Complex(
  770. sinh(a) / d,
  771. -Math.sin(b) / d);
  772. },
  773. /**
  774. * Calculate the complex coth
  775. *
  776. * @returns {Complex}
  777. */
  778. 'csch': function() {
  779. // csch(c) = 2 / (e^c - e^-c)
  780. var a = this['re'];
  781. var b = this['im'];
  782. var d = Math.cos(2 * b) - cosh(2 * a);
  783. return new Complex(
  784. -2 * sinh(a) * Math.cos(b) / d,
  785. 2 * cosh(a) * Math.sin(b) / d);
  786. },
  787. /**
  788. * Calculate the complex sech
  789. *
  790. * @returns {Complex}
  791. */
  792. 'sech': function() {
  793. // sech(c) = 2 / (e^c + e^-c)
  794. var a = this['re'];
  795. var b = this['im'];
  796. var d = Math.cos(2 * b) + cosh(2 * a);
  797. return new Complex(
  798. 2 * cosh(a) * Math.cos(b) / d,
  799. -2 * sinh(a) * Math.sin(b) / d);
  800. },
  801. /**
  802. * Calculate the complex asinh
  803. *
  804. * @returns {Complex}
  805. */
  806. 'asinh': function() {
  807. // asinh(c) = log(c + sqrt(c^2 + 1))
  808. var tmp = this['im'];
  809. this['im'] = -this['re'];
  810. this['re'] = tmp;
  811. var res = this['asin']();
  812. this['re'] = -this['im'];
  813. this['im'] = tmp;
  814. tmp = res['re'];
  815. res['re'] = -res['im'];
  816. res['im'] = tmp;
  817. return res;
  818. },
  819. /**
  820. * Calculate the complex acosh
  821. *
  822. * @returns {Complex}
  823. */
  824. 'acosh': function() {
  825. // acosh(c) = log(c + sqrt(c^2 - 1))
  826. var res = this['acos']();
  827. if (res['im'] <= 0) {
  828. var tmp = res['re'];
  829. res['re'] = -res['im'];
  830. res['im'] = tmp;
  831. } else {
  832. var tmp = res['im'];
  833. res['im'] = -res['re'];
  834. res['re'] = tmp;
  835. }
  836. return res;
  837. },
  838. /**
  839. * Calculate the complex atanh
  840. *
  841. * @returns {Complex}
  842. */
  843. 'atanh': function() {
  844. // atanh(c) = log((1+c) / (1-c)) / 2
  845. var a = this['re'];
  846. var b = this['im'];
  847. var noIM = a > 1 && b === 0;
  848. var oneMinus = 1 - a;
  849. var onePlus = 1 + a;
  850. var d = oneMinus * oneMinus + b * b;
  851. var x = (d !== 0)
  852. ? new Complex(
  853. (onePlus * oneMinus - b * b) / d,
  854. (b * oneMinus + onePlus * b) / d)
  855. : new Complex(
  856. (a !== -1) ? (a / 0) : 0,
  857. (b !== 0) ? (b / 0) : 0);
  858. var temp = x['re'];
  859. x['re'] = logHypot(x['re'], x['im']) / 2;
  860. x['im'] = Math.atan2(x['im'], temp) / 2;
  861. if (noIM) {
  862. x['im'] = -x['im'];
  863. }
  864. return x;
  865. },
  866. /**
  867. * Calculate the complex acoth
  868. *
  869. * @returns {Complex}
  870. */
  871. 'acoth': function() {
  872. // acoth(c) = log((c+1) / (c-1)) / 2
  873. var a = this['re'];
  874. var b = this['im'];
  875. if (a === 0 && b === 0) {
  876. return new Complex(0, Math.PI / 2);
  877. }
  878. var d = a * a + b * b;
  879. return (d !== 0)
  880. ? new Complex(
  881. a / d,
  882. -b / d).atanh()
  883. : new Complex(
  884. (a !== 0) ? a / 0 : 0,
  885. (b !== 0) ? -b / 0 : 0).atanh();
  886. },
  887. /**
  888. * Calculate the complex acsch
  889. *
  890. * @returns {Complex}
  891. */
  892. 'acsch': function() {
  893. // acsch(c) = log((1+sqrt(1+c^2))/c)
  894. var a = this['re'];
  895. var b = this['im'];
  896. if (b === 0) {
  897. return new Complex(
  898. (a !== 0)
  899. ? Math.log(a + Math.sqrt(a * a + 1))
  900. : Infinity, 0);
  901. }
  902. var d = a * a + b * b;
  903. return (d !== 0)
  904. ? new Complex(
  905. a / d,
  906. -b / d).asinh()
  907. : new Complex(
  908. (a !== 0) ? a / 0 : 0,
  909. (b !== 0) ? -b / 0 : 0).asinh();
  910. },
  911. /**
  912. * Calculate the complex asech
  913. *
  914. * @returns {Complex}
  915. */
  916. 'asech': function() {
  917. // asech(c) = log((1+sqrt(1-c^2))/c)
  918. var a = this['re'];
  919. var b = this['im'];
  920. if (this['isZero']()) {
  921. return Complex['INFINITY'];
  922. }
  923. var d = a * a + b * b;
  924. return (d !== 0)
  925. ? new Complex(
  926. a / d,
  927. -b / d).acosh()
  928. : new Complex(
  929. (a !== 0) ? a / 0 : 0,
  930. (b !== 0) ? -b / 0 : 0).acosh();
  931. },
  932. /**
  933. * Calculate the complex inverse 1/z
  934. *
  935. * @returns {Complex}
  936. */
  937. 'inverse': function() {
  938. // 1 / 0 = Infinity and 1 / Infinity = 0
  939. if (this['isZero']()) {
  940. return Complex['INFINITY'];
  941. }
  942. if (this['isInfinite']()) {
  943. return Complex['ZERO'];
  944. }
  945. var a = this['re'];
  946. var b = this['im'];
  947. var d = a * a + b * b;
  948. return new Complex(a / d, -b / d);
  949. },
  950. /**
  951. * Returns the complex conjugate
  952. *
  953. * @returns {Complex}
  954. */
  955. 'conjugate': function() {
  956. return new Complex(this['re'], -this['im']);
  957. },
  958. /**
  959. * Gets the negated complex number
  960. *
  961. * @returns {Complex}
  962. */
  963. 'neg': function() {
  964. return new Complex(-this['re'], -this['im']);
  965. },
  966. /**
  967. * Ceils the actual complex number
  968. *
  969. * @returns {Complex}
  970. */
  971. 'ceil': function(places) {
  972. places = Math.pow(10, places || 0);
  973. return new Complex(
  974. Math.ceil(this['re'] * places) / places,
  975. Math.ceil(this['im'] * places) / places);
  976. },
  977. /**
  978. * Floors the actual complex number
  979. *
  980. * @returns {Complex}
  981. */
  982. 'floor': function(places) {
  983. places = Math.pow(10, places || 0);
  984. return new Complex(
  985. Math.floor(this['re'] * places) / places,
  986. Math.floor(this['im'] * places) / places);
  987. },
  988. /**
  989. * Ceils the actual complex number
  990. *
  991. * @returns {Complex}
  992. */
  993. 'round': function(places) {
  994. places = Math.pow(10, places || 0);
  995. return new Complex(
  996. Math.round(this['re'] * places) / places,
  997. Math.round(this['im'] * places) / places);
  998. },
  999. /**
  1000. * Compares two complex numbers
  1001. *
  1002. * **Note:** new Complex(Infinity).equals(Infinity) === false
  1003. *
  1004. * @returns {boolean}
  1005. */
  1006. 'equals': function(a, b) {
  1007. var z = new Complex(a, b);
  1008. return Math.abs(z['re'] - this['re']) <= Complex['EPSILON'] &&
  1009. Math.abs(z['im'] - this['im']) <= Complex['EPSILON'];
  1010. },
  1011. /**
  1012. * Clones the actual object
  1013. *
  1014. * @returns {Complex}
  1015. */
  1016. 'clone': function() {
  1017. return new Complex(this['re'], this['im']);
  1018. },
  1019. /**
  1020. * Gets a string of the actual complex number
  1021. *
  1022. * @returns {string}
  1023. */
  1024. 'toString': function() {
  1025. var a = this['re'];
  1026. var b = this['im'];
  1027. var ret = "";
  1028. if (this['isNaN']()) {
  1029. return 'NaN';
  1030. }
  1031. if (this['isInfinite']()) {
  1032. return 'Infinity';
  1033. }
  1034. if (Math.abs(a) < Complex['EPSILON']) {
  1035. a = 0;
  1036. }
  1037. if (Math.abs(b) < Complex['EPSILON']) {
  1038. b = 0;
  1039. }
  1040. // If is real number
  1041. if (b === 0) {
  1042. return ret + a;
  1043. }
  1044. if (a !== 0) {
  1045. ret += a;
  1046. ret += " ";
  1047. if (b < 0) {
  1048. b = -b;
  1049. ret += "-";
  1050. } else {
  1051. ret += "+";
  1052. }
  1053. ret += " ";
  1054. } else if (b < 0) {
  1055. b = -b;
  1056. ret += "-";
  1057. }
  1058. if (1 !== b) { // b is the absolute imaginary part
  1059. ret += b;
  1060. }
  1061. return ret + "i";
  1062. },
  1063. /**
  1064. * Returns the actual number as a vector
  1065. *
  1066. * @returns {Array}
  1067. */
  1068. 'toVector': function() {
  1069. return [this['re'], this['im']];
  1070. },
  1071. /**
  1072. * Returns the actual real value of the current object
  1073. *
  1074. * @returns {number|null}
  1075. */
  1076. 'valueOf': function() {
  1077. if (this['im'] === 0) {
  1078. return this['re'];
  1079. }
  1080. return null;
  1081. },
  1082. /**
  1083. * Determines whether a complex number is not on the Riemann sphere.
  1084. *
  1085. * @returns {boolean}
  1086. */
  1087. 'isNaN': function() {
  1088. return isNaN(this['re']) || isNaN(this['im']);
  1089. },
  1090. /**
  1091. * Determines whether or not a complex number is at the zero pole of the
  1092. * Riemann sphere.
  1093. *
  1094. * @returns {boolean}
  1095. */
  1096. 'isZero': function() {
  1097. return this['im'] === 0 && this['re'] === 0;
  1098. },
  1099. /**
  1100. * Determines whether a complex number is not at the infinity pole of the
  1101. * Riemann sphere.
  1102. *
  1103. * @returns {boolean}
  1104. */
  1105. 'isFinite': function() {
  1106. return isFinite(this['re']) && isFinite(this['im']);
  1107. },
  1108. /**
  1109. * Determines whether or not a complex number is at the infinity pole of the
  1110. * Riemann sphere.
  1111. *
  1112. * @returns {boolean}
  1113. */
  1114. 'isInfinite': function() {
  1115. return !(this['isNaN']() || this['isFinite']());
  1116. }
  1117. };
  1118. Complex['ZERO'] = new Complex(0, 0);
  1119. Complex['ONE'] = new Complex(1, 0);
  1120. Complex['I'] = new Complex(0, 1);
  1121. Complex['PI'] = new Complex(Math.PI, 0);
  1122. Complex['E'] = new Complex(Math.E, 0);
  1123. Complex['INFINITY'] = new Complex(Infinity, Infinity);
  1124. Complex['NAN'] = new Complex(NaN, NaN);
  1125. Complex['EPSILON'] = 1e-15;
  1126. if (typeof define === 'function' && define['amd']) {
  1127. define([], function() {
  1128. return Complex;
  1129. });
  1130. } else if (typeof exports === 'object') {
  1131. Object.defineProperty(Complex, "__esModule", { 'value': true });
  1132. Complex['default'] = Complex;
  1133. Complex['Complex'] = Complex;
  1134. module['exports'] = Complex;
  1135. } else {
  1136. root['Complex'] = Complex;
  1137. }
  1138. })(this);