{"_id":"complex-esm","name":"complex-esm","dist-tags":{"latest":"2.1.1-esm1"},"versions":{"2.1.1-esm1":{"name":"complex-esm","homepage":"https://raw.org/article/complex-numbers-in-javascript/","bugs":{"url":"https://github.com/infusion/Complex.js/issues"},"title":"complex-esm","version":"2.1.1-esm1","description":"A complex numbers library","keywords":["complex numbers","math","complex","number","calculus","parser","arithmetic"],"author":{"name":"Robert Eisele","email":"robert@raw.org","url":"https://raw.org/"},"main":"dist/src/complex.js","module":"dist/src/complex.js","exports":{".":"./dist/src/complex.js"},"type":"module","types":"complex.d.ts","private":false,"directories":{"example":"examples"},"license":"MIT","repository":{"type":"git","url":"git://github.com/holgerengels/Complex.js.git"},"engines":{"npm":">=8.5.0","node":">=16.14.2"},"scripts":{"build":"tsc -w","prepublish":"tsc","test":"mocha tests/*.js"},"devDependencies":{"typescript":"^5.5.4","tslib":"^2.7.0","esbuild":"^0.23.1","mocha":"*"},"_id":"complex-esm@2.1.1-esm1","gitHead":"6a1fed43544a923d006da6b9a654567d1fc73663","_nodeVersion":"20.5.1","_npmVersion":"9.8.0","dist":{"integrity":"sha512-IShBEWHILB9s7MnfyevqNGxV0A1cfcSnewL/4uPFiSxkcQL4Mm3FxJ0pXMtCXuWLjYz3lRRyk6OfkeDZcjD6nw==","shasum":"06cc84fe6e1fec299bf8b7a8e759030efe808b0a","tarball":"https://registry.npmjs.org/complex-esm/-/complex-esm-2.1.1-esm1.tgz","fileCount":19,"unpackedSize":176604,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGCbLICas1M5gXM6GU8VNkJWtwtFoDxr7voWBCNq7H2HAiEAjlkM+iqzjr24hmNZVFA6ywShK4spM8sj2HZ/N4fGwrM="}]},"_npmUser":{"name":"holgerengels","email":"hengels@gmail.com"},"maintainers":[{"name":"holgerengels","email":"hengels@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/complex-esm_2.1.1-esm1_1727261301232_0.13871666088336654"},"_hasShrinkwrap":false}},"time":{"created":"2024-09-25T10:48:21.231Z","2.1.1-esm1":"2024-09-25T10:48:21.400Z","modified":"2024-09-25T10:48:21.631Z"},"maintainers":[{"name":"holgerengels","email":"hengels@gmail.com"}],"description":"A complex numbers library","homepage":"https://raw.org/article/complex-numbers-in-javascript/","keywords":["complex numbers","math","complex","number","calculus","parser","arithmetic"],"repository":{"type":"git","url":"git://github.com/holgerengels/Complex.js.git"},"author":{"name":"Robert Eisele","email":"robert@raw.org","url":"https://raw.org/"},"bugs":{"url":"https://github.com/infusion/Complex.js/issues"},"license":"MIT","readme":"# Complex.js - ℂ in JavaScript\n\n[![NPM Package](https://nodei.co/npm-dl/complex.js.png?months=6&height=1)](https://npmjs.org/package/complex.js)\n\n[![Build Status](https://travis-ci.org/infusion/Complex.js.svg?branch=master)](https://travis-ci.org/infusion/Complex.js)\n[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)\n\nComplex.js is a well tested JavaScript library to work with [complex number arithmetic](https://www.xarg.org/book/analysis/complex-numbers/) in JavaScript. It implements every elementary complex number manipulation function and the API is intentionally similar to [Fraction.js](https://github.com/infusion/Fraction.js). Furthermore, it's the basis of [Polynomial.js](https://github.com/infusion/Polynomial.js) and [Math.js](https://github.com/josdejong/mathjs).\n\n\nExamples\n===\n\n```js\nlet Complex = require('complex.js');\n\nlet c = new Complex(\"99.3+8i\");\nc.mul({re: 3, im: 9}).div(4.9).sub(3, 2);\n```\n\nA classical use case for complex numbers is solving quadratic equations `ax² + bx + c = 0` for all `a, b, c ∈ ℝ`:\n\n```js\n\nfunction quadraticRoot(a, b, c) {\n  let sqrt = Complex(b * b - 4 * a * c).sqrt()\n  let x1 = Complex(-b).add(sqrt).div(2 * a)\n  let x2 = Complex(-b).sub(sqrt).div(2 * a)\n  return {x1, x2}\n}\n\n// quadraticRoot(1, 4, 5) -> -2 ± i\n```\n\nParser\n===\n\nAny function (see below) as well as the constructor of the *Complex* class parses its input like this.\n\nYou can pass either Objects, Doubles or Strings.\n\nObjects\n---\n```javascript\nnew Complex({re: real, im: imaginary});\nnew Complex({arg: angle, abs: radius});\nnew Complex({phi: angle, r: radius});\nnew Complex([real, imaginary]); // Vector/Array syntax\n```\nIf there are other attributes on the passed object, they're not getting preserved and have to be merged manually.\n\nDoubles\n---\n```javascript\nnew Complex(55.4);\n```\n\nStrings\n---\n```javascript\nnew Complex(\"123.45\");\nnew Complex(\"15+3i\");\nnew Complex(\"i\");\n```\n\nTwo arguments\n---\n```javascript\nnew Complex(3, 2); // 3+2i\n```\n\nAttributes\n===\n\nEvery complex number object exposes its real and imaginary part as attribute `re` and `im`:\n\n```javascript\nlet c = new Complex(3, 2);\n\nconsole.log(\"Real part:\", c.re); // 3\nconsole.log(\"Imaginary part:\", c.im); // 2\n```\n\nFunctions\n===\n\nComplex sign()\n---\nReturns the complex sign, defined as the complex number normalized by it's absolute value\n\nComplex add(n)\n---\nAdds another complex number\n\nComplex sub(n)\n---\nSubtracts another complex number\n\nComplex mul(n)\n---\nMultiplies the number with another complex number\n\nComplex div(n)\n---\nDivides the number by another complex number\n\nComplex pow(exp)\n---\nReturns the number raised to the complex exponent (Note: `Complex.ZERO.pow(0) = Complex.ONE` by convention)\n\nComplex sqrt()\n---\nReturns the complex square root of the number\n\nComplex exp(n)\n---\nReturns `e^n` with complex exponent `n`.\n\nComplex log()\n---\nReturns the natural logarithm (base `E`) of the actual complex number\n\n_Note:_ The logarithm to a different base can be calculated with `z.log().div(Math.log(base))`.\n\ndouble abs()\n---\nCalculates the magnitude of the complex number\n\ndouble arg()\n---\nCalculates the angle of the complex number\n\nComplex inverse()\n---\nCalculates the multiplicative inverse of the complex number (1 / z)\n\nComplex conjugate()\n---\nCalculates the conjugate of the complex number (multiplies the imaginary part with -1)\n\nComplex neg()\n---\nNegates the number (multiplies both the real and imaginary part with -1) in order to get the additive inverse\n\nComplex floor([places=0])\n---\nFloors the complex number parts towards zero\n\nComplex ceil([places=0])\n---\nCeils the complex number parts off zero\n\nComplex round([places=0])\n---\nRounds the complex number parts\n\nboolean equals(n)\n---\nChecks if both numbers are exactly the same, if both numbers are infinite they\nare considered **not** equal.\n\nboolean isNaN()\n---\nChecks if the given number is not a number\n\nboolean isFinite()\n---\nChecks if the given number is finite\n\nComplex clone()\n---\nReturns a new Complex instance with the same real and imaginary properties\n\nArray toVector()\n---\nReturns a Vector of the actual complex number with two components\n\nString toString()\n---\nReturns a string representation of the actual number. As of v1.9.0 the output is a bit more human readable\n\n```javascript\nnew Complex(1, 2).toString(); // 1 + 2i\nnew Complex(0, 1).toString(); // i\nnew Complex(9, 0).toString(); // 9\nnew Complex(1, 1).toString(); // 1 + i\n```\n\ndouble valueOf()\n---\nReturns the real part of the number if imaginary part is zero. Otherwise `null`\n\n\nTrigonometric functions\n===\nThe following trigonometric functions are defined on Complex.js:\n\n| Trig | Arcus | Hyperbolic | Area-Hyperbolic |\n|------|-------|------------|------------------|\n| sin()  | asin()  | sinh()       | asinh()            |\n| cos()  | acos()  | cosh()       | acosh()            |\n| tan()  | atan()  | tanh()       | atanh()            |\n| cot()  | acot()  | coth()       | acoth()            |\n| sec()  | asec()  | sech()       | asech()            |\n| csc()  | acsc()  | csch()       | acsch()            |\n\n\nGeometric Equivalence\n===\n\nComplex numbers can also be seen as a vector in the 2D space. Here is a simple overview of basic operations and how to implement them with complex.js:\n\nNew vector\n---\n```js\nlet v1 = new Complex(1, 0);\nlet v2 = new Complex(1, 1);\n```\n\nScale vector\n---\n```js\nscale(v1, factor):= v1.mul(factor)\n```\n\nVector norm\n---\n```js\nnorm(v):= v.abs()\n```\n\nTranslate vector\n---\n```js\ntranslate(v1, v2):= v1.add(v2)\n```\n\nRotate vector around center\n---\n```js\nrotate(v, angle):= v.mul({abs: 1, arg: angle})\n```\n\nRotate vector around a point\n---\n```js\nrotate(v, p, angle):= v.sub(p).mul({abs: 1, arg: angle}).add(p)\n```\n\nDistance to another vector\n---\n```js\ndistance(v1, v2):= v1.sub(v2).abs()\n```\n\nConstants\n===\n\nComplex.ZERO\n---\nA complex zero value (south pole on the Riemann Sphere)\n\nComplex.ONE\n---\nA complex one instance\n\nComplex.INFINITY\n---\nA complex infinity value (north pole on the Riemann Sphere)\n\nComplex.NAN\n---\nA complex NaN value (not on the Riemann Sphere)\n\nComplex.I\n---\nAn imaginary number i instance\n\nComplex.PI\n---\nA complex PI instance\n\nComplex.E\n---\nA complex euler number instance\n\nComplex.EPSILON\n---\nA small epsilon value used for `equals()` comparison in order to circumvent double imprecision.\n\n\nInstallation\n===\nInstalling complex.js is as easy as cloning this repo or use one of the following commands:\n\n```bash\nbower install complex.js\n```\nor\n\n```bash\nnpm install complex.js\n```\n\nUsing Complex.js with the browser\n===\n```html\n<script src=\"complex.js\"></script>\n<script>\n    console.log(Complex(\"4+3i\"));\n</script>\n```\n\nUsing Complex.js with require.js\n===\n```html\n<script src=\"require.js\"></script>\n<script>\nrequirejs(['complex.js'],\nfunction(Complex) {\n    console.log(Complex(\"4+3i\"));\n});\n</script>\n```\n\nCoding Style\n===\nAs every library I publish, complex.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.\n\n\nTesting\n===\nIf you plan to enhance the library, make sure you add test cases and all the previous tests are passing. You can test the library with\n\n```bash\nnpm test\n```\n\n\nCopyright and licensing\n===\nCopyright (c) 2023, [Robert Eisele](https://raw.org/)\nDual licensed under the MIT or GPL Version 2 licenses.\n","readmeFilename":"README.md"}