UNPKG

complex-analysis

Version:

Analysis of complex numbers in JavaScript

194 lines (153 loc) 7.01 kB
// Method to create complex number // @Syntax: new ComplexNumber([realPart, [imaginaryPart]]) var ComplexNumber = function (real, imaginary) { this.real = (!real) ? 0 : real; this.imaginary = (!imaginary) ? 0 : imaginary; }; // Method to get the complex number // @Syntax: ComplexNumber.prototype.getNumber() ComplexNumber.prototype.getNumber = function () { var imaginary = ((this.imaginary > 0) ? '+' + this.imaginary : this.imaginary) + 'i'; return this.real + imaginary; }; // Method to calculate inverse of the complex number // @Syntax: ComplexNumber.prototype.modulus() ComplexNumber.prototype.modulus = function () { var scalar = Math.pow(this.real, 2) + Math.pow(this.imaginary, 2); return Math.sqrt(scalar); }; // Method to set the real part // @Syntax: ComplexNumber.prototype.setReal(real) ComplexNumber.prototype.setReal = function (real) { this.real = real; return this; }; // Method to set the imaginary part // @Syntax: ComplexNumber.prototype.setImaginary(imaginary) ComplexNumber.prototype.setImaginary = function (imaginary) { this.imaginary = imaginary; return this; }; // Method to change the complex number // @Syntax: ComplexNumber.prototype.change(real, imaginary) ComplexNumber.prototype.change = function (real, imaginary) { return this.setReal(real).setImaginary(imaginary); }; // Method to alter the complex number by exchanging values between the real and imaginary parts // @Syntax: ComplexNumber.prototype.alter() ComplexNumber.prototype.alter = function () { return this.change(this.imaginary, this.real); }; // Method to complement the complex number // @Syntax: ComplexNumber.prototype.complement() ComplexNumber.prototype.complement = function () { return this.setImaginary(-this.imaginary); }; // Method to calculate inverse of the complex number // @Syntax: ComplexNumber.prototype.inverse() ComplexNumber.prototype.inverse = function () { var scalar = Math.pow(this.real, 2) + Math.pow(this.imaginary, 2); var real = this.real / scalar; var imaginary = -1 * (this.imaginary / scalar); return this.change(real, imaginary); }; // Method to multiply the complex number by scalar value // @Syntax: ComplexNumber.prototype.multiplyScalar(scalar[, n]) ComplexNumber.prototype.multiplyScalar = function (scalar, n) { var target = this; var multiplyFn = function (number) { return number.setReal(number.real * scalar).setImaginary(number.imaginary * scalar); }; if (!n) { target = multiplyFn(target); } else { for (var i = 0; i < n; i++) target = multiplyFn(target); } return this.change(target.real, target.imaginary); }; // Method to divide the complex number by scalar value // @Syntax: ComplexNumber.prototype.divideScalar(scalar[, n]) ComplexNumber.prototype.divideScalar = function (scalar, n) { var target = this; var divideFn = function (number) { return number.setReal(number.real / scalar).setImaginary(number.imaginary / scalar); }; if (!n) { target = divideFn(target); } else { for (var i = 0; i < n; i++) target = divideFn(target); } return this.change(target.real, target.imaginary); }; // Method to add n complex numbers // @Syntax: ComplexNumber.prototype.addComplex(ComplexNumber1, ComplexNumber2, ..., ComplexNumberN) ComplexNumber.prototype.addComplex = function () { var complexNumbers = Array.prototype.slice.call(arguments); var addFn = function (source1, source2) { return source1.change(source1.real + source2.real, source1.imaginary + source2.imaginary); }; complexNumbers.unshift(this); return complexNumbers.reduce(addFn); }; // Method to subtract n complex numbers // @Syntax: ComplexNumber.prototype.subtractComplex(ComplexNumber1, ComplexNumber2, ..., ComplexNumberN) ComplexNumber.prototype.subtractComplex = function () { var complexNumbers = Array.prototype.slice.call(arguments); var subtractFn = function (source1, source2) { return source1.change(source1.real - source2.real, source1.imaginary - source2.imaginary); }; complexNumbers.unshift(this); return complexNumbers.reduce(subtractFn); }; // Method to multiply n complex numbers // @Syntax: ComplexNumber.prototype.multiplyComplex(ComplexNumber1, ComplexNumber2, ..., ComplexNumberN) ComplexNumber.prototype.multiplyComplex = function () { var complexNumbers = Array.prototype.slice.call(arguments); var multiplyFn = function (source1, source2) { var real = (source1.real * source2.real) - (source1.imaginary * source2.imaginary); var imaginary = (source1.real * source2.imaginary) + (source1.imaginary * source2.real); return source1.change(real, imaginary); }; complexNumbers.unshift(this); return complexNumbers.reduce(multiplyFn); }; // Method to divide n complex numbers // @Syntax: ComplexNumber.prototype.divideComplex(ComplexNumber1, ComplexNumber2, ..., ComplexNumberN) ComplexNumber.prototype.divideComplex = function () { var complexNumbers = Array.prototype.slice.call(arguments); var divideFn = function (source1, source2) { var scalar = Math.pow(source2.real, 2) + Math.pow(source2.imaginary, 2); var real = ((source1.real * source2.real) + (source1.imaginary * source2.imaginary)) / scalar; var imaginary = ((source1.imaginary * source2.real) - (source1.real * source2.imaginary)) / scalar; return source1.change(real, imaginary); }; complexNumbers.unshift(this); return complexNumbers.reduce(divideFn); }; // Method to raise the complex number to nth power // @Syntax: ComplexNumber.prototype.power(power) ComplexNumber.prototype.power = function (power) { var index, comb, real, imaginary; var termFlag = false, sign = 1, realTerm = 0, imaginaryTerm = 0; var factorial = function (n) { if (n <= 1) return 1; else return n * factorial(n - 1); }; var combination = function (n, r) { return factorial(n) / (factorial(r) * factorial(n - r)); }; var signFn = function (n) { if (n === 0 || n === 1) return 1; else if (n === 2 || n === 3) return -1; else return signFn(n - 4); }; for (index = 0; index <= power; index++) { comb = combination(power, index); real = Math.pow(this.real, power - index); imaginary = Math.pow(this.imaginary, index); termFlag = (index % 2) ? true : false; sign = signFn(index); if (termFlag) { imaginaryTerm += sign * comb * real * imaginary; } else { realTerm += sign * comb * real * imaginary } } return this.change(realTerm, imaginaryTerm); }; if (typeof window === 'undefined') module.exports = ComplexNumber; else window.ComplexNumber = ComplexNumber;