Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
complex.js
Advanced tools
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.
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 }
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 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 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 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.
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
For cubic roots have a look at RootFinder which uses Complex.js.
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.
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.
Note: Object attributes have to be of type Number to avoid undefined behavior.
new Complex(55.4);
new Complex("123.45");
new Complex("15+3i");
new Complex("i");
new Complex(3, 2); // 3+2i
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
Returns the complex sign, defined as the complex number normalized by it's absolute value
Adds another complex number
Subtracts another complex number
Multiplies the number with another complex number
Divides the number by another complex number
Returns the number raised to the complex exponent (Note: Complex.ZERO.pow(0) = Complex.ONE
by convention)
Returns the complex square root of the number
Returns e^n
with complex exponent n
.
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))
.
Calculates the magnitude of the complex number
Calculates the angle of the complex number
Calculates the multiplicative inverse of the complex number (1 / z)
Calculates the conjugate of the complex number (multiplies the imaginary part with -1)
Negates the number (multiplies both the real and imaginary part with -1) in order to get the additive inverse
Floors the complex number parts towards zero
Ceils the complex number parts off zero
Rounds the complex number parts
Checks if both numbers are exactly the same, if both numbers are infinite they are considered not equal.
Checks if the given number is not a number
Checks if the given number is finite
Returns a new Complex instance with the same real and imaginary properties
Returns a Vector of the actual complex number with two components
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
Returns the real part of the number if imaginary part is zero. Otherwise null
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() |
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:
let v1 = new Complex(1, 0);
let v2 = new Complex(1, 1);
scale(v1, factor):= v1.mul(factor)
norm(v):= v.abs()
translate(v1, v2):= v1.add(v2)
rotate(v, angle):= v.mul({abs: 1, arg: angle})
rotate(v, p, angle):= v.sub(p).mul({abs: 1, arg: angle}).add(p)
distance(v1, v2):= v1.sub(v2).abs()
A complex zero value (south pole on the Riemann Sphere)
A complex one instance
A complex infinity value (north pole on the Riemann Sphere)
A complex NaN value (not on the Riemann Sphere)
An imaginary number i instance
A complex PI instance
A complex euler number instance
A small epsilon value used for equals()
comparison in order to circumvent double imprecision.
Installing complex.js is as easy as cloning this repo or use one of the following command:
npm install complex.js
<script src="complex.min.js"></script>
<script>
console.log(Complex("4+3i"));
</script>
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.
After cloning the Git repository run:
npm install
npm run build
Testing the source against the shipped test suite is as easy as
npm run test
Copyright (c) 2024, Robert Eisele Licensed under the MIT license.
FAQs
A complex numbers library
The npm package complex.js receives a total of 833,607 weekly downloads. As such, complex.js popularity was classified as popular.
We found that complex.js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.