Math.js: Complex Numbers
JS Calc provides comprehensive support for complex numbers through Math.js, allowing for arithmetic and the use of complex numbers with many standard mathematical functions.
Creating Complex Numbers
- Direct Input (using
i
): The imaginary uniti
(representingsqrt(-1)
) is a built-in constant.
> 2 + 3i
2 + 3i
> 5i
5i
> -2.5 - 1.8i
-2.5 - 1.8i
-
Using the
complex()
function:Syntax:
complex(realPart, imaginaryPart)
orcomplex(polarFormString)
> complex(4, -2)
4 - 2i
> complex("3 + 4i")
3 + 4i
> complex({r: 5, phi: pi/4}) // Polar form: radius 5, angle pi/4
3.5355339059327378 + 3.535533905932737i
Operations with Complex Numbers
Most standard arithmetic operators and mathematical functions work seamlessly with complex numbers.
Arithmetic
> z1 = 1 + 2i
> z2 = 3 - 4i
> z1 + z2
4 - 2i
> z1 * z2
11 + 2i
> (2+i) / (1-i)
0.5 + 1.5i
> (1+i)^3
-2 + 2i
Common Functions
re(z)
: Real part ofz
.
> re(3 + 7i)
3
im(z)
: Imaginary part ofz
.
> im(3 + 7i)
7
conj(z)
: Complex conjugate ofz
.
> conj(3 + 7i)
3 - 7i
abs(z)
: Absolute value (magnitude or modulus) ofz
.
> abs(3 + 4i) // sqrt(3^2 + 4^2)
5
arg(z)
: Argument (angle or phase) ofz
in radians.
> arg(1 + i) // atan2(1,1)
0.7853981633974483 (pi/4)
sqrt(z)
: Square root.
> sqrt(-4)
2i
> sqrt(2i)
1 + i
exp(z)
,log(z)
,sin(z)
,cos(z)
, etc., all support complex arguments.
> exp(i*pi) // Euler's Identity
-1
> log(-1)
3.141592653589793i // i*pi
Persistence
Complex numbers assigned to variables are saved and persist between sessions.
For more details, refer to the Math.js Complex Numbers Documentation.