What is complex.js?
The complex.js npm package is a library for complex number arithmetic in JavaScript. It provides a comprehensive set of functions to perform operations on complex numbers, including basic arithmetic, trigonometric functions, and more.
What are complex.js's main functionalities?
Basic Arithmetic
This feature allows you to perform basic arithmetic operations such as addition, subtraction, multiplication, and division on complex numbers.
const Complex = require('complex.js');
const a = new Complex(2, 3);
const b = new Complex(1, 1);
const sum = a.add(b);
console.log(sum.toString()); // '3 + 4i'
Trigonometric Functions
This feature provides trigonometric functions like sine, cosine, and tangent for complex numbers.
const Complex = require('complex.js');
const a = new Complex(1, 1);
const sinA = a.sin();
console.log(sinA.toString()); // '1.2984575814159773 + 0.6349639147847361i'
Exponential and Logarithmic Functions
This feature allows you to compute the exponential and logarithmic functions of complex numbers.
const Complex = require('complex.js');
const a = new Complex(1, 1);
const expA = a.exp();
console.log(expA.toString()); // '1.4686939399158851 + 2.2873552871788423i'
Polar Coordinates
This feature allows you to convert complex numbers to and from polar coordinates.
const Complex = require('complex.js');
const a = new Complex(1, 1);
const polar = a.toPolar();
console.log(polar); // { r: 1.4142135623730951, phi: 0.7853981633974483 }
Other packages similar to complex.js
mathjs
Math.js is an extensive math library for JavaScript and Node.js. It supports complex numbers, matrices, units, and many other mathematical functions. Compared to complex.js, math.js offers a broader range of mathematical functionalities but may be more complex to use for operations specifically focused on complex numbers.
numeric
Numeric is a library for numerical analysis in JavaScript. It includes support for complex numbers, matrix operations, and other numerical methods. While it provides similar functionalities for complex numbers, it is more focused on numerical analysis and matrix computations.
complex-number
Complex-number is a lightweight library for complex number arithmetic in JavaScript. It offers basic operations and some advanced functions for complex numbers. It is simpler and more lightweight compared to complex.js but may lack some of the advanced features.
Complex.js - ℂ in JavaSript
Complex.js is a 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.
Example
var Complex = require('complex.js');
var c = new Complex("99.3+8i");
c.mul({r: 3, i: 9}).div(4.9).sub(3, 2);
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({r: real, i: imaginary});
new Complex({arg: angle, abs: radius});
Doubles
new Complex(55.4);
Strings
new Complex("123.45");
new Complex("15+3i");
new Complex("i");
Two arguments
new Complex(3, 2);
Functions
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
Complex sqrt()
Returns the complex square root of the number
Complex exp(n)
Returns e^n
with complex exponential.
Complex log()
Returns the natural logarithm (base E
) of the actual complex number
double abs()
Calculates the magnitude of the complex number
double arg()
Calculates the angle of the complex number
Complex sin()
Calculates the sine of the complex number
Complex cos()
Calculates the cosine of the complex number
Complex tan()
Calculates the tangent of the complex number
Complex sinh()
Calculates the hyperbolic sine of the complex number
Complex cosh()
Calculates the hyperbolic cosine of the complex number
Complex tanh()
Calculates the hyperbolic tangent of the complex number
Complex asin()
Calculates the arcus sine of the complex number
Complex acos()
Calculates the arcus cosine of the complex number
Complex atan()
Calculates the arcus tangent 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
boolean equals(n)
Checks if both numbers are exactly the same
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
new Complex(1, 2).toString();
new Complex(0, 1).toString();
new Complex(9, 0).toString();
new Complex(1, 1).toString();
double valueOf()
Returns the real part of the number if imaginary part is zero. Otherwise null
Constants
Complex.ZERO
A complex zero shortcut
Complex.ONE
A complex one shortcut
Complex.I
The imaginary number i shortcut
Complex.PI
A complex PI shortcut
Complex.E
A complex euler number shortcut
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 at 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 (robert@xarg.org)
Dual licensed under the MIT or GPL Version 2 licenses.