A package providing the implementation of complex numbers and mathematical functions.
# deno
deno add jsr:@babia/complex
# node.js
npx jsr add @babia/complex
yarn dlx jsr add @babia/complex
pnpm dlx jsr add @babia/complex
# bun
bunx jsr add @babia/complex
A complex number is an extension of the real number. It combines both real and
imaginary components. It can be expressed in the standard form a + bi
, where:
a
: the real partb
: the imaginary parti
: the imaginary unit
A real number can be regarded as a complex number a + 0i
, whose the imaginary
part is 0
. A purely imaginary number is a complex number 0 + bi
, whose the
real part is 0
.
This package provides an implementation of complex number through the complex
class. It has methods to perform basic operations on complex numbers:
const cmplx1 = new complex(3, 1); // 3 + i
const cmplx2 = new complex(2, 9); // 2 + 9i
cmplx1.add(cmplx2); // (3 + i) + (2 + 9i) = (5 + 10i)
// Also works with real numbers
cmplx1.add(3); // (3 + i) + 3 = (6 + i)
Methods abs()
, conj()
, phase()
return the absolute value, the conjugate,
and the argument of the complex number respectively:
cmplx2.abs(); // 9.2195445
cmplx2.conj(); // (2 - 9i)
cmplx2.phase(); // 1.3521274
The cmplx
function was added in v1.1.0
to simplify the creation of complex
numbers.
cmplx(2, 3); // 2 + 3i
Unlike complex
class, the cmplx
function requires the first argument (the
real part).
cmplx(0); // 0 + 0i
Besides complex
, there is also cmath
which is a collection of mathematical
functions for complex numbers:
- Power and logarithm functions:
exp
,log
,log10
,sqrt
- Hyperbolic functions:
sinh
,cosh
,tanh
,asinh
,acosh
,atanh
- Trigonometric functions:
sin
,cos
,tan
,asin
,acos
,atan
Note
Do not confuse with <cmath>
header in C++
- Go
math/cmplx
- C++
std::complex
- Python
complex
andcmath
MIT