@jrc03c/complex-number
v0.0.6
Published
a little tool for performing simple operations with with complex numbers.
Readme
intro
a little tool for performing simple operations with with complex numbers.
installation
npm install @jrc03c/complex-numberusage
import { ComplexNumber } from "@jrc03c/complex-number"
// -----------------------------------------------------------------------------
// construct some complex numbers in various ways:
const a = new ComplexNumber()
a.print()
// 0 + 0i
const b = new ComplexNumber(2.3, -4.5)
b.print()
// 2.3 - 4.5i
const c = new ComplexNumber({ real: Math.random(), imag: Math.random() })
c.print()
// 0.302830970835392 + 0.8051186842022172i
const d = new ComplexNumber("0.1 + -0.2i")
d.print()
// 0.1 - 0.2i
const e = new ComplexNumber("-0.3 - 0.4i")
e.print()
// -0.3 - 0.4i
// -----------------------------------------------------------------------------
// add, subtract, multiply, and divide complex numbers:
b.add(c).print()
// 2.6028309708353916 - 3.6948813157977827i
c.sub(d).print()
// 0.202830970835392 + 1.0051186842022173i
d.mul(e).print()
// -0.11000000000000001 + 0.01999999999999999i
e.div(b).print()
// 0.043461237274862966 - 0.0888801879404855i
// -----------------------------------------------------------------------------
// get the conjugate of a complex number (i.e., a - bi) and the conjugate
// product (i.e., (a + bi) * (a - bi))
b.print()
// 2.3 - 4.5i
b.conj.print()
// 2.3 + 4.5i
b.conjProd.print()
// 25.54 + 0iapi
ComplexNumber() (constructor)
accepts as arguments any one of these:
nullorundefined- a single real number
- two numbers representing real and imaginary parts respectively
- a string in the form
a + bi - an options object with "real" and "imag" properties
- a
ComplexNumberinstance
new ComplexNumber() // 0 + 0i
new ComplexNumber(5) // 5 + 0i
new ComplexNumber(2, 3) // 2 + 3i
new ComplexNumber("-0.009 - 0.0756i") // -0.009 - 0.0756i
new ComplexNumber({ real: -1.7, imag: 3.4 }) // -1.7 + 3.4iproperties
conj (getter)
returns a new ComplexNumber instance that represents the conjugate of the number (i.e., a - bi).
const x = new ComplexNumber("-2 - 3i")
x.print() // -2 - 3i
x.conj.print() // -2 + 3iconjProd (getter)
returns a new ComplexNumber instance representing the product of the complex number and its conjugate (i.e., (a + bi) * (a - bi)). note that, by definition, the imaginary part of the returned value will always be zero (i.e., a + 0i).
const x = new ComplexNumber("-2 - 3i")
x.print() // -2 - 3i
x.conjProd.print() // 13 + 0iimag
the imaginary part of the complex number as a floating point value.
real
the real part of the complex number as a floating point value.
methods
add()
returns a new ComplexNumber instance in which the number has been added to a given number. the arguments are the same as the constructor's.
const a = new ComplexNumber("2 + 3i")
a.add(4).print()
// 6 + 3i
a.add(5, -6).print()
// 7 - 3i
a.add("-7 + 8i").print()
// -5 + 11icopy()
returns a new ComplexNumber instance with the same real and imaginary parts as the number.
const a = new ComplexNumber("2 + 3i")
a.print() // 2 + 3i
a.copy().print() // 2 + 3idiv()
returns a new ComplexNumber instance in which the number has been divided by a given number. the arguments are the same as the constructor's.
const a = new ComplexNumber("2 + 3i")
a.div(4).print()
// 0.5 + 0.75i
a.div(5, -6).print()
// -0.13114754098360656 + 0.4426229508196721i
a.div("-7 + 8i").print()
// 0.08849557522123894 - 0.3274336283185841imul()
returns a new ComplexNumber instance in which the number has been multiplied by a given number. the arguments are the same as the constructor's.
const a = new ComplexNumber("2 + 3i")
a.mul(4).print()
// 8 + 12i
a.mul(5, -6).print()
// 28 + 3i
a.mul("-7 + 8i").print()
// -38 - 5iprint()
logs to the console the value returned from the toString method.
const a = new ComplexNumber("2 + 3i")
a.print() // 2 + 3isub(v)
returns a new ComplexNumber instance in which a given number has been subtracted from the number. the arguments are the same as the constructor's.
const a = new ComplexNumber("2 + 3i")
a.sub(4).print()
// -2 + 3i
a.sub(5, -6).print()
// -3 + 9i
a.sub("-7 + 8i").print()
// 9 - 5itoString()
returns the complex number as a string in the form a + bi.
const a = new ComplexNumber("2 + 3i")
console.log(a.toString()) // 2 + 3i