All Projects → infusion → Complex.js

infusion / Complex.js

Licence: mit
A complex number library

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Complex.js

Numphp
Mathematical PHP library for scientific computing
Stars: ✭ 120 (-27.27%)
Mutual labels:  vector, complex-numbers
Algebra
means completeness and balancing, from the Arabic word الجبر
Stars: ✭ 92 (-44.24%)
Mutual labels:  vector, complex-numbers
React Designer
It's not art
Stars: ✭ 1,762 (+967.88%)
Mutual labels:  vector
Vald
Vald. A Highly Scalable Distributed Vector Search Engine
Stars: ✭ 158 (-4.24%)
Mutual labels:  vector
React Native Easy Gestures
React Native Gestures. Support: Drag, Scale and Rotate a Component.
Stars: ✭ 153 (-7.27%)
Mutual labels:  rotation
Textview Rich Drawable
Android TextView with rich support of compound drawables
Stars: ✭ 136 (-17.58%)
Mutual labels:  vector
Hlslpp
Math library using hlsl syntax with SSE/NEON support
Stars: ✭ 153 (-7.27%)
Mutual labels:  vector
Jpeg Autorotate
Node module to rotate JPEG images based on EXIF orientation.
Stars: ✭ 127 (-23.03%)
Mutual labels:  rotation
Elm Geometry
2D/3D geometry package for Elm
Stars: ✭ 162 (-1.82%)
Mutual labels:  vector
Matrixstats
R package: Methods that Apply to Rows and Columns of Matrices (and to Vectors)
Stars: ✭ 151 (-8.48%)
Mutual labels:  vector
Jqueryrotate
jQueryRotate - plugin to rotate images by any angle cross-browse with animation support
Stars: ✭ 157 (-4.85%)
Mutual labels:  rotation
Moonshot
A SpaceX companion app for Android
Stars: ✭ 150 (-9.09%)
Mutual labels:  vector
Plutovg
Tiny 2D vector graphics library in C
Stars: ✭ 141 (-14.55%)
Mutual labels:  vector
Aginterfaceinteraction
library performs interaction with UI interface
Stars: ✭ 156 (-5.45%)
Mutual labels:  rotation
Mathjs
An extensive math library for JavaScript and Node.js
Stars: ✭ 11,861 (+7088.48%)
Mutual labels:  complex-numbers
Circlr
Animation rotation via scroll, mouse and touch events
Stars: ✭ 159 (-3.64%)
Mutual labels:  rotation
Mapchete
Tile-based geodata processing using rasterio & Fiona
Stars: ✭ 129 (-21.82%)
Mutual labels:  vector
Node Sylvester
🐱 Sylvester is a vector, matrix, and geometry library for JavaScript, that runs in the browser and on the server.
Stars: ✭ 144 (-12.73%)
Mutual labels:  vector
Aegis Icons
Unofficial 2FA entry icons for open source Android authenticator Aegis
Stars: ✭ 153 (-7.27%)
Mutual labels:  vector
Glidetovectoryou
Load SVGs in Android with Glide
Stars: ✭ 165 (+0%)
Mutual labels:  vector

Complex.js - ℂ in JavaScript

NPM Package

Build Status MIT license

Complex.js is a well tested JavaScript library to work with complex number arithmetic in JavaScript. It implements every elementary complex number manipulation function and the API is intentionally similar to Fraction.js. Furthermore, it's the basis of Polynomial.js and Math.js.

Examples

let Complex = require('complex.js');

let c = new Complex("99.3+8i");
c.mul({re: 3, im: 9}).div(4.9).sub(3, 2);

A classical use case for complex numbers is solving quadratic equations ax² + bx + c = 0 for all a, b, c ∈ ℝ:

function quadraticRoot(a, b, c) {
  let sqrt = Complex(b * b - 4 * a * c).sqrt()
  let x1 = Complex(-b).add(sqrt).div(2 * a)
  let x2 = Complex(-b).sub(sqrt).div(2 * a)
  return {x1, x2}
}

// quadraticRoot(1, 4, 5) -> -2 ± i

Parser

Any function (see below) as well as the constructor of the Complex class parses its input like this.

You can pass either Objects, Doubles or Strings.

Objects

new Complex({re: real, im: imaginary});
new Complex({arg: angle, abs: radius});
new Complex({phi: angle, r: radius});
new Complex([real, imaginary]); // Vector/Array syntax

If there are other attributes on the passed object, they're not getting preserved and have to be merged manually.

Doubles

new Complex(55.4);

Strings

new Complex("123.45");
new Complex("15+3i");
new Complex("i");

Two arguments

new Complex(3, 2); // 3+2i

Attributes

Every complex number object exposes its real and imaginary part as attribute re and im:

let c = new Complex(3, 2);

console.log("Real part:", c.re); // 3
console.log("Imaginary part:", c.im); // 2

Functions

Complex sign()

Returns the complex sign, defined as the complex number normalized by it's absolute value

Complex add(n)

Adds another complex number

Complex sub(n)

Subtracts another complex number

Complex mul(n)

Multiplies the number with another complex number

Complex div(n)

Divides the number by another complex number

Complex pow(exp)

Returns the number raised to the complex exponent (Note: Complex.ZERO.pow(0) = Complex.ONE by convention)

Complex sqrt()

Returns the complex square root of the number

Complex exp(n)

Returns e^n with complex exponent n.

Complex log()

Returns the natural logarithm (base E) of the actual complex number

Note: The logarithm to a different base can be calculated with z.log().div(Math.log(base)).

double abs()

Calculates the magnitude of the complex number

double arg()

Calculates the angle of the complex number

Complex inverse()

Calculates the multiplicative inverse of the complex number (1 / z)

Complex conjugate()

Calculates the conjugate of the complex number (multiplies the imaginary part with -1)

Complex neg()

Negates the number (multiplies both the real and imaginary part with -1) in order to get the additive inverse

Complex floor([places=0])

Floors the complex number parts towards zero

Complex ceil([places=0])

Ceils the complex number parts off zero

Complex round([places=0])

Rounds the complex number parts

boolean equals(n)

Checks if both numbers are exactly the same, if both numbers are infinite they are considered not equal.

boolean isNaN()

Checks if the given number is not a number

boolean isFinite()

Checks if the given number is finite

Complex clone()

Returns a new Complex instance with the same real and imaginary properties

Array toVector()

Returns a Vector of the actual complex number with two components

String toString()

Returns a string representation of the actual number. As of v1.9.0 the output is a bit more human readable

new Complex(1, 2).toString(); // 1 + 2i
new Complex(0, 1).toString(); // i
new Complex(9, 0).toString(); // 9
new Complex(1, 1).toString(); // 1 + i

double valueOf()

Returns the real part of the number if imaginary part is zero. Otherwise null

Trigonometric functions

The following trigonometric functions are defined on Complex.js:

Trig Arcus Hyperbolic Area-Hyperbolic
sin() asin() sinh() asinh()
cos() acos() cosh() acosh()
tan() atan() tanh() atanh()
cot() acot() coth() acoth()
sec()  asec() sech() asech()
csc() acsc() csch() acsch()

Geometric Equivalence

Complex 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:

New vector

let v1 = new Complex(1, 0);
let v2 = new Complex(1, 1);

Scale vector

scale(v1, factor):= v1.mul(factor)

Vector norm

norm(v):= v.abs()

Translate vector

translate(v1, v2):= v1.add(v2)

Rotate vector around center

rotate(v, angle):= v.mul({abs: 1, arg: angle})

Rotate vector around a point

rotate(v, p, angle):= v.sub(p).mul({abs: 1, arg: angle}).add(p)

Distance to another vector

distance(v1, v2):= v1.sub(v2).abs()

Constants

Complex.ZERO

A complex zero value (south pole on the Riemann Sphere)

Complex.ONE

A complex one instance

Complex.INFINITY

A complex infinity value (north pole on the Riemann Sphere)

Complex.NAN

A complex NaN value (not on the Riemann Sphere)

Complex.I

An imaginary number i instance

Complex.PI

A complex PI instance

Complex.E

A complex euler number instance

Complex.EPSILON

A small epsilon value used for equals() comparison in order to circumvent double imprecision.

Installation

Installing complex.js is as easy as cloning this repo or use one of the following commands:

bower install complex.js

or

npm install complex.js

Using Complex.js with the browser

<script src="complex.js"></script>
<script>
    console.log(Complex("4+3i"));
</script>

Using Complex.js with require.js

<script src="require.js"></script>
<script>
requirejs(['complex.js'],
function(Complex) {
    console.log(Complex("4+3i"));
});
</script>

Coding Style

As 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.

Testing

If 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

npm test

Copyright and licensing

Copyright (c) 2015, Robert Eisele Dual licensed under the MIT or GPL Version 2 licenses.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].