niosh-math
Version:
advanced numbers math with float 128bit suppport and functions inline
151 lines (150 loc) • 9.29 kB
JavaScript
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-–––––––––––––––-––––––––––––––– ƒx advanced math
const mathjsModule = require("mathjs");
const mathjsConfig = {
epsilon: 1e-12,
matrix: "Matrix",
number: "BigNumber",
precision: 256,
predictable: false,
randomSeed: null,
};
const mathjs = mathjsModule.create(mathjsModule.all,mathjsConfig);
const { MathExpression, Matrix } = require("mathjs");
//--------------------------------------------------------------------------------------
/**
* Matematical function extended returning any
* @callback NumberFunctionExtended
* @param {MathExpression|Matrix} expr
* @param {object} [scope]
* @returns {any}
*/
/** @type {NumberFunctionExtended} */
const ƒxe = global.ƒxe = function(expr,scope) {
if (typeof scope == "object" && scope != null) {
let temp = scope;
scope = {};
for (var p in temp) {
let value = temp[p];
if (typeof value == "number" || value instanceof Number) value = mathjs.bignumber(parseFloat(value));
if (typeof value != "undefined") scope[p] = value;
}
return mathjs.evaluate(expr,scope);
} else return mathjs.evaluate(expr);
};
//--------------------------------------------------------------------------------------
/**
* Matematical function returning number
* @callback NumberFunction
* @param {MathExpression|Matrix} expr
* @param {object} [scope]
* @returns {number}
*/
/** @type {NumberFunction} */
const ƒx = global.ƒx = function(expr,scope) {
return parseFloat(ƒxe(expr,scope));
};
//#endregion
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-–––––––––––––––-––––––––––––––– create internal secret memory area called "µ" as memory
Number.µ = {
//--------------------------------------------------------------------------------------
prime: {
maxPrime: 9_007_199_254_740_881,
dividers: [
0, 2, 6, 8, 12, 18, 20, 26, 30, 32, 36, 42,
48, 50, 56, 60, 62, 68, 72, 78, 86, 90, 92, 96,
98, 102, 110, 116, 120, 126, 128, 132, 138, 140, 146, 152,
156, 158, 162, 168, 170, 176, 180, 182, 186, 188, 198, 200,
],
isPrime: /** @param {number|Number} x */ function(x) {
if (x instanceof Number) x = parseFloat(x.toString());
if (typeof x != "number" || isNaN(x) || x < 2 || x > Number.µ.prime.maxPrime || x % 1) return false;
if (x % 2 === 0) return (x === 2) ? true : false;
if (x % 3 === 0) return (x === 3) ? true : false;
if (x % 5 === 0) return (x === 5) ? true : false;
if (x % 7 === 0) return (x === 7) ? true : false;
const m = Math.sqrt(x);
for (let i = 11; i <= m; i += 210)
for (const a of Number.µ.prime.dividers)
if (x % (i + a) === 0) return (i + a === x) ? true : false;
return true;
},
},
//--------------------------------------------------------------------------------------
fibonacci: {
isPerfectSquare: /** @param {number|Number} x */ function(x) {
if (x instanceof Number) x = parseFloat(x.toString());
if (typeof x != "number" || isNaN(x) || x <= 0 || !Number.isFinite(x)) return false;
return (Math.sqrt(x) % 1 === 0) ? true : false;
},
isFibonacci: /** @param {number|Number} x */ function(x) {
if (x instanceof Number) x = parseFloat(x.toString());
if (typeof x != "number" || isNaN(x) || x < 0 || !Number.isInteger(x)) return false;
if (x === 0 || x === 1 || x === 2 || x === 3 || x === 5 || x === 8) return true;
if (x < 8) return false;
x = 5 * x * x;
return (Number.µ.fibonacci.isPerfectSquare(x+4)||Number.µ.fibonacci.isPerfectSquare(x-4)) ? true : false;
},
}
};
//#endregion
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-–––––––––––––––-––––––––––––––– instance function without parameters
/** return integer round number */ Number.prototype.round = function() { return Math.round(this); };
/** return integer trunc number */ Number.prototype.trunc = function() { return Math.trunc(this); };
/** return integer floor number */ Number.prototype.floor = function() { return Math.floor(this); };
/** return integer ceil number */ Number.prototype.ceil = function() { return Math.ceil(this); };
/** return absolute number */ Number.prototype.abs = function() { return Math.abs(this); };
/** return boolean pair number */ Number.prototype.pair = function() { return(this%2==0)?true:false; };
/** return boolean odd number */ Number.prototype.odd = function() { return(this%2==1)?true:false; };
/** return boolean prime number */ Number.prototype.prime = function() { return (Number.µ.prime.isPrime(this)) ? true : false; };
/** return boolean fibonacci number */ Number.prototype.fibonacci = function() { return (Number.µ.fibonacci.isFibonacci(this)) ? true : false; };
/** return boolean perfectSquare number */ Number.prototype.perfectSquare = function() { return (Number.µ.fibonacci.isPerfectSquare(this)) ? true : false; };
//#endregion
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-–––––––––––––––-––––––––––––––– ƒx advanced math direct in number
/**
* Matematical function returning number
* inside a number where x is this instance
* @callback NumberFunctionInside
* @param {MathExpression|Matrix} expr - use x for this value
* @param {object} [scope] - others variables
* @returns {number}
*/
/** @type {NumberFunctionInside} */
Number.prototype.ƒx = function(expr,scope) {
var x = this;
if (typeof scope != "object" || scope == null || Array.isArray(scope)) {
scope = {x: x};
} else {
scope.x = x;
}
return parseFloat(ƒxe(expr,scope));
};
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-–––––––––––––––-––––––––––––––– function with one number parameter
/**
* return module, rest of division
* if divisor non valid return NaN
* @param {number|Number} divisor
*/
Number.prototype.mod = function(divisor) {
if (typeof divisor != "number" && !(divisor instanceof Number)) return NaN;
return this % divisor;
}
//--------------------------------------------------------------------------------------
/**
* return integer division
* if divisor non valid return NaN
* @param {number|Number} divisor
*/
Number.prototype.div = function(divisor) {
if (typeof divisor != "number" && !(divisor instanceof Number)) return NaN;
return Math.trunc(this / divisor);
}
//#endregion
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-–––––––––––––––-––––––––––––––– module exporting
module.exports = { ƒxe, ƒx };
//#endregion