/**
* @file Complex number operations
* @module Complex
*/
require('./monkey-patch.js');
let Complex = require('complex.js');
/**
* Checks if the current complex number is less than the given complex number.
* @param {Complex} other - The complex number to compare with.
* @returns {boolean} - Returns true if the current complex number is less than the given complex number, otherwise returns false.
*/
Complex.prototype.lessThan = function (other) {
if (this.re < other.re) return true;
if (this.re == other.re && this.im < other.im) return true;
return false;
}
/**
* Checks if the current complex number is greater than the given complex number.
* @param {Complex} other - The complex number to compare with.
* @returns {boolean} - Returns true if the current complex number is greater than the given complex number, otherwise returns false.
*/
Complex.prototype.greaterThan = function (other) {
if (this.re > other.re) return true;
if (this.re == other.re && this.im > other.im) return true;
return false;
}
/**
* Set of supported operators.
* @type {Set<string>}
*/
let Operators = new Set(['add', 'sub', 'mul', 'div', 'equals', 'pow', 'neg', 'lessThan'])
let oldComplex = Object.create(null);
for (let op of Operators) {
oldComplex[op] = Complex.prototype[op];
/**
* Overrides the operator method for complex numbers.
* @param {Complex|function|boolean|string} other - The operand to perform the operation with.
* @returns {*} - The result of the operation.
* @throws {Error} - Throws an error if the operation is not supported for the given operand.
*/
Complex.prototype[op] = function (other) {
try {
if (typeof other === 'function') return other[op](this);
if (typeof other === 'boolean') return this[op](Complex(Number(other)));
if (typeof other === 'string') return String(this) [op](other);
return oldComplex[op].call(this, other)
}
catch (e) {
throw new Error(`Complex numbers do not support ${op} for ${other}\n${e}`)
}
}
}
/**
* Returns the current complex number.
* @param {*} other - Ignored parameter.
* @returns {Complex} - The current complex number.
*/
Complex.prototype.call = function (other) {
return this
}
module.exports = Complex;