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.
- 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.
Install the package with a single command:
npm install pro-complex-math
And you’re set to conquer the world of math!
Here’s how to use every feature, with examples and details to make it crystal clear.
The Complex class is your key to mastering complex numbers.
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"
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"
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"
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"
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"
Returns the complex conjugate (flips the imaginary part).
const c = new Complex(1, 2);
const conj = c.conjugate();
console.log(conj.toString()); // "1 + -2i"
Calculates the magnitude (absolute value) of the complex number.
const c = new Complex(3, 4);
console.log(c.magnitude()); // 5 (sqrt(3² + 4²))
Gets the phase (argument) in radians.
const c = new Complex(1, 1);
console.log(c.phase()); // ~0.785 (π/4 radians)
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 }
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"
Returns a clean string representation.
const c = new Complex(1, -2);
console.log(c.toString()); // "1 + -2i"
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
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)
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
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)
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"
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"
- Node.js: v14 or higher.
- TypeScript: Optional, for IntelliSense goodness.
Want to contribute or tweak it? Here’s how:
git clone https://github.com/yourusername/pro-complex-math.git
npm install
npm run build
npm test
MIT License—check out the LICENSE file for details.
Built by Dev - @debrajrout28 Got ideas or need help? Hit me up—I’m always down to chat code!