pro-complex-math
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

pro-complex-math

npm | License | Tests

A powerful, optimized, and TypeScript-based package for complex number operations and advanced mathematical functions. Whether you’re handling intricate calculations or financial computations, this package is built for developers who need precise outcomes with a dash of fun.

Features

  • Complex Number Operations: Addition, subtraction, multiplication, division, and more.
  • Financial Tools: Simple interest, compound interest, and NPV calculations.
  • Advanced Math: Exponentiation, logarithms, and sine for complex numbers.
  • TypeScript Ready: Full IntelliSense support for autocompletion and documentation.
  • Robust Error Handling: Clear and witty error messages for edge cases.

Installation

Install the package with a single command:

npm install pro-complex-math

And you’re set to conquer the world of math!

Usage

Here’s how to use every feature, with examples and details to make it crystal clear.

Complex Number Operations

The Complex class is your key to mastering complex numbers.

new Complex(real, imag)

Creates a new complex number with real and imaginary parts.

  • real (number): The real part.
  • imag (number): The imaginary part.
const { Complex } = require("pro-complex-math");
const c = new Complex(1, 2); // 1 + 2i
console.log(c.toString()); // "1 + 2i"

add(other)

Adds two complex numbers together.

  • other (Complex): The number to add.
const c1 = new Complex(1, 2);
const c2 = new Complex(3, 4);
const sum = c1.add(c2);
console.log(sum.toString()); // "4 + 6i"

subtract(other)

Subtracts one complex number from another.

  • other (Complex): The number to subtract.
const c1 = new Complex(5, 6);
const c2 = new Complex(3, 4);
const diff = c1.subtract(c2);
console.log(diff.toString()); // "2 + 2i"

multiply(other)

Multiplies two complex numbers.

  • other (Complex): The number to multiply with.
const c1 = new Complex(1, 2);
const c2 = new Complex(3, 4);
const product = c1.multiply(c2);
console.log(product.toString()); // "-5 + 10i"

divide(other)

Divides one complex number by another.

  • other (Complex): The divisor.

Throws: "Division by zero? Not cool!" if dividing by 0 + 0i.

const c1 = new Complex(1, 2);
const c2 = new Complex(1, 1);
const quotient = c1.divide(c2);
console.log(quotient.toString()); // "1.5 + 0.5i"

conjugate()

Returns the complex conjugate (flips the imaginary part).

const c = new Complex(1, 2);
const conj = c.conjugate();
console.log(conj.toString()); // "1 + -2i"

magnitude()

Calculates the magnitude (absolute value) of the complex number.

const c = new Complex(3, 4);
console.log(c.magnitude()); // 5 (sqrt(3² + 4²))

phase()

Gets the phase (argument) in radians.

const c = new Complex(1, 1);
console.log(c.phase()); // ~0.785 (π/4 radians)

toPolar()

Converts to polar form (magnitude and phase).

const c = new Complex(3, 4);
const polar = c.toPolar();
console.log(polar); // { magnitude: 5, phase: ~0.927 }

Complex.fromPolar(magnitude, phase)

Creates a complex number from polar coordinates.

  • magnitude (number): The magnitude.
  • phase (number): The phase in radians.
const c = Complex.fromPolar(5, Math.PI / 4);
console.log(c.toString()); // "~3.536 + 3.536i"

toString()

Returns a clean string representation.

const c = new Complex(1, -2);
console.log(c.toString()); // "1 + -2i"

Financial & Advanced Math Functions

simpleInterest(principal, rate, time)

Calculates simple interest—straightforward and sweet!

  • principal (number): Initial amount.
  • rate (number): Interest rate per period.
  • time (number): Number of periods.

Throws: "No negatives allowed, fam!" if any input is negative.

const { simpleInterest } = require("pro-complex-math");
console.log(simpleInterest(1000, 0.05, 2)); // 1100

compoundInterest(principal, rate, time, n)

Computes compound interest—watch your money grow!

  • principal (number): Initial amount.
  • rate (number): Interest rate per period.
  • time (number): Number of periods.
  • n (number, optional): Compounding frequency (default: 1).

Throws: "Invalid input, dude!" if inputs are negative or n is zero/negative.

const { compoundInterest } = require("pro-complex-math");
console.log(compoundInterest(1000, 0.05, 2)); // ~1102.5
console.log(compoundInterest(1000, 0.05, 2, 2)); // ~1103.8 (semi-annual)

npv(rate, cashFlows)

Calculates Net Present Value—how much is your money worth today?

  • rate (number): Discount rate.
  • cashFlows (number[]): Array of cash flows (negative for outflows).

Throws: "Discount rate can’t be ≤ -1, bro!" if rate is too low.

const { npv } = require("pro-complex-math");
const cashFlows = [-1000, 300, 400, 500];
console.log(npv(0.1, cashFlows)); // ~62.09

exp(z)

Computes the exponential of a complex number (e^z).

  • z (Complex): The complex input.
const { exp, Complex } = require("pro-complex-math");
const result = exp(new Complex(0, Math.PI));
console.log(result.toString()); // "-1 + 0i" (e^(iπ) = -1)

log(z)

Calculates the natural logarithm of a complex number.

  • z (Complex): The complex input.

Throws: "Log of zero? Nope!" if z is 0 + 0i.

const { log, Complex } = require("pro-complex-math");
const result = log(new Complex(1, 1));
console.log(result.toString()); // "~0.346 + 0.785i"

sin(z)

Computes the sine of a complex number.

  • z (Complex): The complex input.
const { sin, Complex } = require("pro-complex-math");
const result = sin(new Complex(Math.PI / 2, 0));
console.log(result.toString()); // "1 + 0i"

Requirements

  • Node.js: v14 or higher.
  • TypeScript: Optional, for IntelliSense goodness.

Development

Want to contribute or tweak it? Here’s how:

Clone the repo:

git clone https://github.com/yourusername/pro-complex-math.git

Install dependencies:

npm install

Build the package:

npm run build

Run the tests:

npm test

License

MIT License—check out the LICENSE file for details.

Author

Built by Dev - @debrajrout28 Got ideas or need help? Hit me up—I’m always down to chat code!

/pro-complex-math/

    Package Sidebar

    Install

    npm i pro-complex-math

    Weekly Downloads

    3

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    20.6 kB

    Total Files

    14

    Last publish

    Collaborators

    • debarajrout28