This class includes many trig functions and is immutable. None of its methods will change the real or imaginary components, instead, they return new complex objects.
To retrieve the real and imaginary parts of the complex number z
, use
z.real
and z.imag
.
Most methods can be chained. For example, z.tan().sqrt()
will calculate
and return the square root of the tangent of z
. In mathematics, we are
evaluating sqrt(tan(z))
leaving z
unchanged.
In some situations, floating point errors can be significant. Testing for
equality or to see if the number represents a real value can be problematic.
By default, the maximum permitted difference is 10^-8
; but in some
situations, this may be too restrictive and reject values you wish to
consider equal. Use accessor Complex.epsilon
to change the static
#epsilon
property's default value.
⚠️ Notice that b/cComplex.epsilon
is a static accessor, changing it affects all instances of classComplex
. As a workaround, all methods which rely on it accept an optionalepsilon
argument to be used in the test, thus ignoring the global static default value.
-
Last updated: 16 Aug 2023
-
Author: Peter Lager (2023)
-
Refactored by GoToLoop (2023)
The code defines a class called Complex
that represents complex numbers.
It provides various mathematical operations and utility methods
for working with complex numbers.
// Create a complex number
const z = new Complex(2, 3);
// Perform mathematical operations
const sum = z.add(new Complex(1, 1));
const product = z.mult(2);
const abs = z.abs();
// Print the complex number
console.log(z.toString());
- Represents complex numbers with real and imaginary parts
- Provides mathematical operations such as addition, subtraction, multiplication, division, exponentiation, etc.
- Supports trigonometric and hyperbolic functions for complex numbers
- Allows conversion between polar and rectangular coordinates
- Provides methods for comparing complex numbers and finding roots
abs()
: Calculates the absolute value (modulus) of the complex numberadd(n)
: Adds another complex number or a real number to the complex numberacos()
: Calculates the inverse cosine of the complex numberarg()
: Calculates the argument (phase) of the complex number in radiansasin()
: Calculates the inverse sine of the complex numberatan()
: Calculates the inverse tangent of the complex numbercbrt()
: Calculates the cube root of the complex numberclone()
: Creates a copy of the complex numberconj()
: Calculates the conjugate of the complex numbercos()
: Calculates the cosine of the complex numbercosec()
: Calculates the cosecant of the complex numbercosech()
: Calculates the hyperbolic cosecant of the complex numbercosh()
: Calculates the hyperbolic cosine of the complex numbercot()
: Calculates the cotangent of the complex numbercoth()
: Calculates the hyperbolic cotangent of the complex numbercubed()
: Calculates the cube of the complex numberdiv(n)
: Divides the complex number by another complex number or a scalarequals(z, epsilon)
: Checks if the complex number is equal to another complex number within a given toleranceexp()
: Calculates the exponential of the complex numberhashCode(hash)
: Generates a hashcode for the complex numberisReal(epsilon)
: Checks if complex number is real within a given toleranceisZero(epsilon)
: Checks if complex number is zero within a given tolerancelog()
: Calculates the natural logarithm of the complex numbermult(n)
: Multiplies the complex number by another complex number or a scalarnegate()
: Returns the negation of the complex numbernorm()
: Returns the normalization of the complex numberpow(n)
: Returns the complex number raised to a given exponentroots(n)
: Returns the nth roots of the complex numbersin()
: Calculates the sine of the complex numbersinh()
: Calculates the hyperbolic sine of the complex numbersquared()
: Returns the square of the complex numbersqrt()
: Returns the square root of the complex numbersub(n)
: Subtracts another complex number or real number from complex numbertan()
: Returns the tangent of the complex numbertanh()
: Returns the hyperbolic tangent of the complex numberprint(precision)
: Prints the complex number to the console$(precision, $)
: Returns a string representation of the complex number
real
: The real part of the complex numberimag
: The imaginary part of the complex number#hashCode
: The cached hashcode for the complex number#$
: The cached string representation for the complex number