Intial Commit

This commit is contained in:
valki
2020-10-17 18:42:50 +02:00
commit 664c6d8ca3
5892 changed files with 759183 additions and 0 deletions

View File

@@ -0,0 +1,347 @@
'use strict';
var number = require('./number');
var string = require('./string');
var object = require('./object');
var types = require('./types');
var DimensionError = require('../error/DimensionError');
var IndexError = require('../error/IndexError');
/**
* Calculate the size of a multi dimensional array.
* This function checks the size of the first entry, it does not validate
* whether all dimensions match. (use function `validate` for that)
* @param {Array} x
* @Return {Number[]} size
*/
exports.size = function (x) {
var s = [];
while (Array.isArray(x)) {
s.push(x.length);
x = x[0];
}
return s;
};
/**
* Recursively validate whether each element in a multi dimensional array
* has a size corresponding to the provided size array.
* @param {Array} array Array to be validated
* @param {number[]} size Array with the size of each dimension
* @param {number} dim Current dimension
* @throws DimensionError
* @private
*/
function _validate(array, size, dim) {
var i;
var len = array.length;
if (len != size[dim]) {
throw new DimensionError(len, size[dim]);
}
if (dim < size.length - 1) {
// recursively validate each child array
var dimNext = dim + 1;
for (i = 0; i < len; i++) {
var child = array[i];
if (!Array.isArray(child)) {
throw new DimensionError(size.length - 1, size.length, '<');
}
_validate(array[i], size, dimNext);
}
}
else {
// last dimension. none of the childs may be an array
for (i = 0; i < len; i++) {
if (Array.isArray(array[i])) {
throw new DimensionError(size.length + 1, size.length, '>');
}
}
}
}
/**
* Validate whether each element in a multi dimensional array has
* a size corresponding to the provided size array.
* @param {Array} array Array to be validated
* @param {number[]} size Array with the size of each dimension
* @throws DimensionError
*/
exports.validate = function(array, size) {
var isScalar = (size.length == 0);
if (isScalar) {
// scalar
if (Array.isArray(array)) {
throw new DimensionError(array.length, 0);
}
}
else {
// array
_validate(array, size, 0);
}
};
/**
* Test whether index is an integer number with index >= 0 and index < length
* when length is provided
* @param {number} index Zero-based index
* @param {number} [length] Length of the array
*/
exports.validateIndex = function(index, length) {
if (!number.isNumber(index) || !number.isInteger(index)) {
throw new TypeError('Index must be an integer (value: ' + index + ')');
}
if (index < 0 || (typeof length === 'number' && index >= length)) {
throw new IndexError(index, length);
}
};
// a constant used to specify an undefined defaultValue
exports.UNINITIALIZED = {};
/**
* Resize a multi dimensional array. The resized array is returned.
* @param {Array} array Array to be resized
* @param {Array.<number>} size Array with the size of each dimension
* @param {*} [defaultValue=0] Value to be filled in in new entries,
* zero by default. To leave new entries undefined,
* specify array.UNINITIALIZED as defaultValue
* @return {Array} array The resized array
*/
exports.resize = function(array, size, defaultValue) {
// TODO: add support for scalars, having size=[] ?
// check the type of the arguments
if (!Array.isArray(array) || !Array.isArray(size)) {
throw new TypeError('Array expected');
}
if (size.length === 0) {
throw new Error('Resizing to scalar is not supported');
}
// check whether size contains positive integers
size.forEach(function (value) {
if (!number.isNumber(value) || !number.isInteger(value) || value < 0) {
throw new TypeError('Invalid size, must contain positive integers ' +
'(size: ' + string.format(size) + ')');
}
});
// recursively resize the array
var _defaultValue = (defaultValue !== undefined) ? defaultValue : 0;
_resize(array, size, 0, _defaultValue);
return array;
};
/**
* Recursively resize a multi dimensional array
* @param {Array} array Array to be resized
* @param {number[]} size Array with the size of each dimension
* @param {number} dim Current dimension
* @param {*} [defaultValue] Value to be filled in in new entries,
* undefined by default.
* @private
*/
function _resize (array, size, dim, defaultValue) {
var i;
var elem;
var oldLen = array.length;
var newLen = size[dim];
var minLen = Math.min(oldLen, newLen);
// apply new length
array.length = newLen;
if (dim < size.length - 1) {
// non-last dimension
var dimNext = dim + 1;
// resize existing child arrays
for (i = 0; i < minLen; i++) {
// resize child array
elem = array[i];
if (!Array.isArray(elem)) {
elem = [elem]; // add a dimension
array[i] = elem;
}
_resize(elem, size, dimNext, defaultValue);
}
// create new child arrays
for (i = minLen; i < newLen; i++) {
// get child array
elem = [];
array[i] = elem;
// resize new child array
_resize(elem, size, dimNext, defaultValue);
}
}
else {
// last dimension
// remove dimensions of existing values
for (i = 0; i < minLen; i++) {
while (Array.isArray(array[i])) {
array[i] = array[i][0];
}
}
if(defaultValue !== exports.UNINITIALIZED) {
// fill new elements with the default value
for (i = minLen; i < newLen; i++) {
array[i] = defaultValue;
}
}
}
}
/**
* Squeeze a multi dimensional array
* @param {Array} array
* @param {Array} [size]
* @returns {Array} returns the array itself
*/
exports.squeeze = function(array, size) {
var s = size || exports.size(array);
// squeeze outer dimensions
while (Array.isArray(array) && array.length === 1) {
array = array[0];
s.shift();
}
// find the first dimension to be squeezed
var dims = s.length;
while (s[dims - 1] === 1) {
dims--;
}
// squeeze inner dimensions
if (dims < s.length) {
array = _squeeze(array, dims, 0);
s.length = dims;
}
return array;
};
/**
* Recursively squeeze a multi dimensional array
* @param {Array} array
* @param {number} dims Required number of dimensions
* @param {number} dim Current dimension
* @returns {Array | *} Returns the squeezed array
* @private
*/
function _squeeze (array, dims, dim) {
var i, ii;
if (dim < dims) {
var next = dim + 1;
for (i = 0, ii = array.length; i < ii; i++) {
array[i] = _squeeze(array[i], dims, next);
}
}
else {
while (Array.isArray(array)) {
array = array[0];
}
}
return array;
}
/**
* Unsqueeze a multi dimensional array: add dimensions when missing
*
* Paramter `size` will be mutated to match the new, unqueezed matrix size.
*
* @param {Array} array
* @param {number} dims Desired number of dimensions of the array
* @param {number} [outer] Number of outer dimensions to be added
* @param {Array} [size] Current size of array.
* @returns {Array} returns the array itself
* @private
*/
exports.unsqueeze = function(array, dims, outer, size) {
var s = size || exports.size(array);
// unsqueeze outer dimensions
if (outer) {
for (var i = 0; i < outer; i++) {
array = [array];
s.unshift(1);
}
}
// unsqueeze inner dimensions
array = _unsqueeze(array, dims, 0);
while (s.length < dims) {
s.push(1);
}
return array;
};
/**
* Recursively unsqueeze a multi dimensional array
* @param {Array} array
* @param {number} dims Required number of dimensions
* @param {number} dim Current dimension
* @returns {Array | *} Returns the squeezed array
* @private
*/
function _unsqueeze (array, dims, dim) {
var i, ii;
if (Array.isArray(array)) {
var next = dim + 1;
for (i = 0, ii = array.length; i < ii; i++) {
array[i] = _unsqueeze(array[i], dims, next);
}
}
else {
for (var d = dim; d < dims; d++) {
array = [array];
}
}
return array;
}
/**
* Flatten a multi dimensional array, put all elements in a one dimensional
* array
* @param {Array} array A multi dimensional array
* @return {Array} The flattened array (1 dimensional)
*/
exports.flatten = function(array) {
if (!Array.isArray(array)) {
//if not an array, return as is
return array;
}
var flat = [];
array.forEach(function callback(value) {
if (Array.isArray(value)) {
value.forEach(callback); //traverse through sub-arrays recursively
}
else {
flat.push(value);
}
});
return flat;
};
/**
* Test whether an object is an array
* @param {*} value
* @return {boolean} isArray
*/
exports.isArray = Array.isArray;

View File

@@ -0,0 +1,68 @@
var bitwise = require('./bitwise');
/**
* Bitwise and for Bignumbers
*
* Special Cases:
* N & n = N
* n & 0 = 0
* n & -1 = n
* n & n = n
* I & I = I
* -I & -I = -I
* I & -I = 0
* I & n = n
* I & -n = I
* -I & n = 0
* -I & -n = -I
*
* @param {BigNumber} x
* @param {BigNumber} y
* @return {BigNumber} Result of `x` & `y`, is fully precise
* @private
*/
module.exports = function bitAnd(x, y) {
if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) {
throw new Error('Integers expected in function bitAnd');
}
var BigNumber = x.constructor;
if (x.isNaN() || y.isNaN()) {
return new BigNumber(NaN);
}
if (x.isZero() || y.eq(-1) || x.eq(y)) {
return x;
}
if (y.isZero() || x.eq(-1)) {
return y;
}
if (!x.isFinite() || !y.isFinite()) {
if (!x.isFinite() && !y.isFinite()) {
if (x.isNegative() == y.isNegative()) {
return x;
}
return new BigNumber(0);
}
if (!x.isFinite()) {
if (y.isNegative()) {
return x;
}
if (x.isNegative()) {
return new BigNumber(0);
}
return y;
}
if (!y.isFinite()) {
if (x.isNegative()) {
return y;
}
if (y.isNegative()) {
return new BigNumber(0);
}
return x;
}
}
return bitwise(x, y, function (a, b) { return a & b });
};

View File

@@ -0,0 +1,21 @@
/**
* Bitwise not
* @param {BigNumber} value
* @return {BigNumber} Result of ~`x`, fully precise
*
*/
module.exports = function bitNot (x) {
if (x.isFinite() && !x.isInteger()) {
throw new Error('Integer expected in function bitNot');
}
var BigNumber = x.constructor;
var prevPrec = BigNumber.precision;
BigNumber.config({precision: 1E9});
var x = x.plus(new BigNumber(1));
x.s = -x.s || null;
BigNumber.config({precision: prevPrec});
return x;
};

View File

@@ -0,0 +1,53 @@
var bitwise = require('./bitwise');
/**
* Bitwise OR for BigNumbers
*
* Special Cases:
* N | n = N
* n | 0 = n
* n | -1 = -1
* n | n = n
* I | I = I
* -I | -I = -I
* I | -n = -1
* I | -I = -1
* I | n = I
* -I | n = -I
* -I | -n = -n
*
* @param {BigNumber} x
* @param {BigNumber} y
* @return {BigNumber} Result of `x` | `y`, fully precise
*/
module.exports = function bitOr (x, y) {
if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) {
throw new Error('Integers expected in function bitOr');
}
var BigNumber = x.constructor;
if (x.isNaN() || y.isNaN()) {
return new BigNumber(NaN);
}
var negOne = new BigNumber(-1);
if (x.isZero() || y.eq(negOne) || x.eq(y)) {
return y;
}
if (y.isZero() || x.eq(negOne)) {
return x;
}
if (!x.isFinite() || !y.isFinite()) {
if ((!x.isFinite() && !x.isNegative() && y.isNegative()) ||
(x.isNegative() && !y.isNegative() && !y.isFinite())) {
return negOne;
}
if (x.isNegative() && y.isNegative()) {
return x.isFinite() ? x : y;
}
return x.isFinite() ? y : x;
}
return bitwise(x, y, function (a, b) { return a | b });
};

View File

@@ -0,0 +1,60 @@
var bitwise = require('./bitwise');
var bitNot = require('./bitNot');
/**
* Bitwise XOR for BigNumbers
*
* Special Cases:
* N ^ n = N
* n ^ 0 = n
* n ^ n = 0
* n ^ -1 = ~n
* I ^ n = I
* I ^ -n = -I
* I ^ -I = -1
* -I ^ n = -I
* -I ^ -n = I
*
* @param {BigNumber} x
* @param {BigNumber} y
* @return {BigNumber} Result of `x` ^ `y`, fully precise
*
*/
module.exports = function bitXor(x, y) {
if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) {
throw new Error('Integers expected in function bitXor');
}
var BigNumber = x.constructor;
if (x.isNaN() || y.isNaN()) {
return new BigNumber(NaN);
}
if (x.isZero()) {
return y;
}
if (y.isZero()) {
return x;
}
if (x.eq(y)) {
return new BigNumber(0);
}
var negOne = new BigNumber(-1);
if (x.eq(negOne)) {
return bitNot(y);
}
if (y.eq(negOne)) {
return bitNot(x);
}
if (!x.isFinite() || !y.isFinite()) {
if (!x.isFinite() && !y.isFinite()) {
return negOne;
}
return new BigNumber(x.isNegative() == y.isNegative()
? Infinity
: -Infinity);
}
return bitwise(x, y, function (a, b) { return a ^ b });
};

View File

@@ -0,0 +1,124 @@
var bitNot = require('./bitNot');
/**
* Applies bitwise function to numbers
* @param {BigNumber} x
* @param {BigNumber} y
* @param {function (a, b)} func
* @return {BigNumber}
*/
module.exports = function bitwise(x, y, func) {
var BigNumber = x.constructor;
var xBits, yBits;
var xSign = +(x.s < 0);
var ySign = +(y.s < 0);
if (xSign) {
xBits = decCoefficientToBinaryString(bitNot(x));
for (var i = 0; i < xBits.length; ++i) {
xBits[i] ^= 1;
}
} else {
xBits = decCoefficientToBinaryString(x);
}
if (ySign) {
yBits = decCoefficientToBinaryString(bitNot(y));
for (var i = 0; i < yBits.length; ++i) {
yBits[i] ^= 1;
}
} else {
yBits = decCoefficientToBinaryString(y);
}
var minBits, maxBits, minSign;
if (xBits.length <= yBits.length) {
minBits = xBits;
maxBits = yBits;
minSign = xSign;
} else {
minBits = yBits;
maxBits = xBits;
minSign = ySign;
}
var shortLen = minBits.length;
var longLen = maxBits.length;
var expFuncVal = func(xSign, ySign) ^ 1;
var outVal = new BigNumber(expFuncVal ^ 1);
var twoPower = new BigNumber(1);
var two = new BigNumber(2);
var prevPrec = BigNumber.precision;
BigNumber.config({precision: 1E9});
while (shortLen > 0) {
if (func(minBits[--shortLen], maxBits[--longLen]) == expFuncVal) {
outVal = outVal.plus(twoPower);
}
twoPower = twoPower.times(two);
}
while (longLen > 0) {
if (func(minSign, maxBits[--longLen]) == expFuncVal) {
outVal = outVal.plus(twoPower);
}
twoPower = twoPower.times(two);
}
BigNumber.config({precision: prevPrec});
if (expFuncVal == 0) {
outVal.s = -outVal.s;
}
return outVal;
};
/* Extracted from decimal.js, and edited to specialize. */
function decCoefficientToBinaryString (x) {
// Convert to string
var a = x.d; // array with digits
var r = a[0] + '';
for (var i = 1; i < a.length; ++i) {
var s = a[i] + '';
for (var z = 7 - s.length; z--; ) {
s = '0' + s;
}
r += s;
}
var j;
for (j = r.length - 1; r.charAt(j) == '0'; --j);
var xe = x.e;
var str = r.slice(0, j + 1 || 1);
var strL = str.length;
if (xe > 0) {
if (++xe > strL) {
// Append zeros.
for (xe -= strL; xe--; str += '0');
} else if (xe < strL) {
str = str.slice(0, xe) + '.' + str.slice(xe);
}
}
// Convert from base 10 (decimal) to base 2
var arr = [0];
for (var i = 0; i < str.length; ) {
for (var arrL = arr.length; arrL--; arr[arrL] *= 10);
arr[0] += str.charAt(i++) << 0; // convert to int
for (var j = 0; j < arr.length; ++j) {
if (arr[j] > 1) {
if (arr[j + 1] == null) {
arr[j + 1] = 0;
}
arr[j + 1] += arr[j] >> 1;
arr[j] &= 1;
}
}
}
return arr.reverse();
}

View File

@@ -0,0 +1,49 @@
var memoize = require('../function').memoize;
/**
* Calculate BigNumber e
* @param {function} BigNumber BigNumber constructor
* @returns {BigNumber} Returns e
*/
exports.e = memoize(function (BigNumber) {
return new BigNumber(1).exp();
}, hasher);
/**
* Calculate BigNumber golden ratio, phi = (1+sqrt(5))/2
* @param {function} BigNumber BigNumber constructor
* @returns {BigNumber} Returns phi
*/
exports.phi = memoize(function (BigNumber) {
return new BigNumber(1).plus(new BigNumber(5).sqrt()).div(2);
}, hasher);
/**
* Calculate BigNumber pi.
* @param {function} BigNumber BigNumber constructor
* @returns {BigNumber} Returns pi
*/
exports.pi = memoize(function (BigNumber) {
return pi = BigNumber.acos(-1);
}, hasher);
/**
* Calculate BigNumber tau, tau = 2 * pi
* @param {function} BigNumber BigNumber constructor
* @returns {BigNumber} Returns tau
*/
exports.tau = memoize(function (BigNumber) {
return exports.pi(BigNumber).times(2);
}, hasher);
/**
* Create a hash for a BigNumber constructor function. The created has is
* the configured precision
* @param {Array} args Supposed to contain a single entry with
* a BigNumber constructor
* @return {number} precision
* @private
*/
function hasher (args) {
return args[0].precision;
}

View File

@@ -0,0 +1,183 @@
/**
* Convert a BigNumber to a formatted string representation.
*
* Syntax:
*
* format(value)
* format(value, options)
* format(value, precision)
* format(value, fn)
*
* Where:
*
* {number} value The value to be formatted
* {Object} options An object with formatting options. Available options:
* {string} notation
* Number notation. Choose from:
* 'fixed' Always use regular number notation.
* For example '123.40' and '14000000'
* 'exponential' Always use exponential notation.
* For example '1.234e+2' and '1.4e+7'
* 'auto' (default) Regular number notation for numbers
* having an absolute value between
* `lower` and `upper` bounds, and uses
* exponential notation elsewhere.
* Lower bound is included, upper bound
* is excluded.
* For example '123.4' and '1.4e7'.
* {number} precision A number between 0 and 16 to round
* the digits of the number.
* In case of notations 'exponential' and
* 'auto', `precision` defines the total
* number of significant digits returned
* and is undefined by default.
* In case of notation 'fixed',
* `precision` defines the number of
* significant digits after the decimal
* point, and is 0 by default.
* {Object} exponential An object containing two parameters,
* {number} lower and {number} upper,
* used by notation 'auto' to determine
* when to return exponential notation.
* Default values are `lower=1e-3` and
* `upper=1e5`.
* Only applicable for notation `auto`.
* {Function} fn A custom formatting function. Can be used to override the
* built-in notations. Function `fn` is called with `value` as
* parameter and must return a string. Is useful for example to
* format all values inside a matrix in a particular way.
*
* Examples:
*
* format(6.4); // '6.4'
* format(1240000); // '1.24e6'
* format(1/3); // '0.3333333333333333'
* format(1/3, 3); // '0.333'
* format(21385, 2); // '21000'
* format(12.071, {notation: 'fixed'}); // '12'
* format(2.3, {notation: 'fixed', precision: 2}); // '2.30'
* format(52.8, {notation: 'exponential'}); // '5.28e+1'
*
* @param {BigNumber} value
* @param {Object | Function | number} [options]
* @return {string} str The formatted value
*/
exports.format = function (value, options) {
if (typeof options === 'function') {
// handle format(value, fn)
return options(value);
}
// handle special cases
if (!value.isFinite()) {
return value.isNaN() ? 'NaN' : (value.gt(0) ? 'Infinity' : '-Infinity');
}
// default values for options
var notation = 'auto';
var precision = undefined;
if (options !== undefined) {
// determine notation from options
if (options.notation) {
notation = options.notation;
}
// determine precision from options
if (typeof options === 'number') {
precision = options;
}
else if (options.precision) {
precision = options.precision;
}
}
// handle the various notations
switch (notation) {
case 'fixed':
return exports.toFixed(value, precision);
case 'exponential':
return exports.toExponential(value, precision);
case 'auto':
// determine lower and upper bound for exponential notation.
// TODO: implement support for upper and lower to be BigNumbers themselves
var lower = 1e-3;
var upper = 1e5;
if (options && options.exponential) {
if (options.exponential.lower !== undefined) {
lower = options.exponential.lower;
}
if (options.exponential.upper !== undefined) {
upper = options.exponential.upper;
}
}
// adjust the configuration of the BigNumber constructor (yeah, this is quite tricky...)
var oldConfig = {
toExpNeg: value.constructor.toExpNeg,
toExpPos: value.constructor.toExpPos
};
value.constructor.config({
toExpNeg: Math.round(Math.log(lower) / Math.LN10),
toExpPos: Math.round(Math.log(upper) / Math.LN10)
});
// handle special case zero
if (value.isZero()) return '0';
// determine whether or not to output exponential notation
var str;
var abs = value.abs();
if (abs.gte(lower) && abs.lt(upper)) {
// normal number notation
str = value.toSignificantDigits(precision).toFixed();
}
else {
// exponential notation
str = exports.toExponential(value, precision);
}
// remove trailing zeros after the decimal point
return str.replace(/((\.\d*?)(0+))($|e)/, function () {
var digits = arguments[2];
var e = arguments[4];
return (digits !== '.') ? digits + e : e;
});
default:
throw new Error('Unknown notation "' + notation + '". ' +
'Choose "auto", "exponential", or "fixed".');
}
};
/**
* Format a number in exponential notation. Like '1.23e+5', '2.3e+0', '3.500e-3'
* @param {BigNumber} value
* @param {number} [precision] Number of digits in formatted output.
* If not provided, the maximum available digits
* is used.
* @returns {string} str
*/
exports.toExponential = function (value, precision) {
if (precision !== undefined) {
return value.toExponential(precision - 1); // Note the offset of one
}
else {
return value.toExponential();
}
};
/**
* Format a number with fixed notation.
* @param {BigNumber} value
* @param {number} [precision=0] Optional number of decimals after the
* decimal point. Zero by default.
*/
exports.toFixed = function (value, precision) {
return value.toFixed(precision || 0);
// Note: the (precision || 0) is needed as the toFixed of BigNumber has an
// undefined default precision instead of 0.
};

View File

@@ -0,0 +1,41 @@
/**
* Bitwise left shift
*
* Special Cases:
* n << -n = N
* n << N = N
* N << n = N
* n << 0 = n
* 0 << n = 0
* I << I = N
* I << n = I
* n << I = I
*
* @param {BigNumber} x
* @param {BigNumber} y
* @return {BigNumber} Result of `x` << `y`
*
*/
module.exports = function leftShift (x, y) {
if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) {
throw new Error('Integers expected in function leftShift');
}
var BigNumber = x.constructor;
if (x.isNaN() || y.isNaN() || (y.isNegative() && !y.isZero())) {
return new BigNumber(NaN);
}
if (x.isZero() || y.isZero()) {
return x;
}
if (!x.isFinite() && !y.isFinite()) {
return new BigNumber(NaN);
}
// Math.pow(2, y) is fully precise for y < 55, and fast
if (y.lt(55)) {
return x.times(Math.pow(2, y.toNumber()) + '');
}
return x.times(new BigNumber(2).pow(y));
};

View File

@@ -0,0 +1,45 @@
'use strict';
/**
* Compares two BigNumbers.
* @param {BigNumber} x First value to compare
* @param {BigNumber} y Second value to compare
* @param {number} [epsilon] The maximum relative difference between x and y
* If epsilon is undefined or null, the function will
* test whether x and y are exactly equal.
* @return {boolean} whether the two numbers are nearly equal
*/
module.exports = function nearlyEqual(x, y, epsilon) {
// if epsilon is null or undefined, test whether x and y are exactly equal
if (epsilon == null) {
return x.eq(y);
}
// use "==" operator, handles infinities
if (x.eq(y)) {
return true;
}
// NaN
if (x.isNaN() || y.isNaN()) {
return false;
}
// at this point x and y should be finite
if(x.isFinite() && y.isFinite()) {
// check numbers are very close, needed when comparing numbers near zero
var diff = x.minus(y).abs();
if (diff.isZero()) {
return true;
}
else {
// use relative error
var max = x.constructor.max(x.abs(), y.abs());
return diff.lte(max.times(epsilon));
}
}
// Infinite and Number or negative Infinite and positive Infinite cases
return false;
};

View File

@@ -0,0 +1,47 @@
/*
* Special Cases:
* n >> -n = N
* n >> N = N
* N >> n = N
* I >> I = N
* n >> 0 = n
* I >> n = I
* -I >> n = -I
* -I >> I = -I
* n >> I = I
* -n >> I = -1
* 0 >> n = 0
*
* @param {BigNumber} value
* @param {BigNumber} value
* @return {BigNumber} Result of `x` >> `y`
*
*/
module.exports = function rightArithShift (x, y) {
if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) {
throw new Error('Integers expected in function rightArithShift');
}
var BigNumber = x.constructor;
if (x.isNaN() || y.isNaN() || (y.isNegative() && !y.isZero())) {
return new BigNumber(NaN);
}
if (x.isZero() || y.isZero()) {
return x;
}
if (!y.isFinite()) {
if (x.isNegative()) {
return new BigNumber(-1);
}
if (!x.isFinite()) {
return new BigNumber(NaN);
}
return new BigNumber(0);
}
// Math.pow(2, y) is fully precise for y < 55, and fast
if (y.lt(55)) {
return x.div(Math.pow(2, y.toNumber()) + '').floor();
}
return x.div(new BigNumber(2).pow(y)).floor();
};

View File

@@ -0,0 +1,10 @@
'use strict';
/**
* Test whether value is a boolean
* @param {*} value
* @return {boolean} isBoolean
*/
exports.isBoolean = function(value) {
return typeof value == 'boolean';
};

View File

@@ -0,0 +1,18 @@
'use strict';
var isCollection = require('./isCollection');
/**
* Test whether an array contains collections
* @param {Array} array
* @returns {boolean} Returns true when the array contains one or multiple
* collections (Arrays or Matrices). Returns false otherwise.
*/
module.exports = function containsCollections (array) {
for (var i = 0; i < array.length; i++) {
if (isCollection(array[i])) {
return true;
}
}
return false;
};

View File

@@ -0,0 +1,25 @@
'use strict';
/**
* Recursively loop over all elements in a given multi dimensional array
* and invoke the callback on each of the elements.
* @param {Array | Matrix} array
* @param {Function} callback The callback method is invoked with one
* parameter: the current element in the array
*/
module.exports = function deepForEach (array, callback) {
if (array && array.isMatrix === true) {
array = array.valueOf();
}
for (var i = 0, ii = array.length; i < ii; i++) {
var value = array[i];
if (Array.isArray(value)) {
deepForEach(value, callback);
}
else {
callback(value);
}
}
};

View File

@@ -0,0 +1,25 @@
'use strict';
/**
* Execute the callback function element wise for each element in array and any
* nested array
* Returns an array with the results
* @param {Array | Matrix} array
* @param {Function} callback The callback is called with two parameters:
* value1 and value2, which contain the current
* element of both arrays.
* @param {boolean} [skipZeros] Invoke callback function for non-zero values only.
*
* @return {Array | Matrix} res
*/
module.exports = function deepMap(array, callback, skipZeros) {
if (array && (typeof array.map === 'function')) {
// TODO: replace array.map with a for loop to improve performance
return array.map(function (x) {
return deepMap(x, callback, skipZeros);
});
}
else {
return callback(array);
}
};

View File

@@ -0,0 +1,10 @@
'use strict';
/**
* Test whether a value is a collection: an Array or Matrix
* @param {*} x
* @returns {boolean} isCollection
*/
module.exports = function isCollection (x) {
return (Array.isArray(x) || (x && x.isMatrix === true));
};

View File

@@ -0,0 +1,83 @@
'use strict';
var arraySize = require('../array').size;
var IndexError = require('../../error/IndexError');
/**
* Reduce a given matrix or array to a new matrix or
* array with one less dimension, applying the given
* callback in the selected dimension.
* @param {Array | Matrix} mat
* @param {number} dim
* @param {Function} callback
* @return {Array | Matrix} res
*/
module.exports = function(mat, dim, callback) {
var size = Array.isArray(mat) ? arraySize(mat) : mat.size();
if (dim < 0 || (dim >= size.length)) {
// TODO: would be more clear when throwing a DimensionError here
throw new IndexError(dim, size.length);
}
if (mat && mat.isMatrix === true) {
return mat.create(_reduce(mat.valueOf(), dim, callback));
}else {
return _reduce(mat, dim, callback);
}
};
/**
* Recursively reduce a matrix
* @param {Array} mat
* @param {number} dim
* @param {Function} callback
* @returns {Array} ret
* @private
*/
function _reduce(mat, dim, callback){
var i, ret, val, tran;
if(dim<=0){
if( !Array.isArray(mat[0]) ){
val = mat[0];
for(i=1; i<mat.length; i++){
val = callback(val, mat[i]);
}
return val;
}else{
tran = _switch(mat);
ret = [];
for(i=0; i<tran.length; i++){
ret[i] = _reduce(tran[i], dim-1, callback);
}
return ret;
}
}else{
ret = [];
for(i=0; i<mat.length; i++){
ret[i] = _reduce(mat[i], dim-1, callback);
}
return ret;
}
}
/**
* Transpose a matrix
* @param {Array} mat
* @returns {Array} ret
* @private
*/
function _switch(mat){
var I = mat.length;
var J = mat[0].length;
var i, j;
var ret = [];
for( j=0; j<J; j++) {
var tmp = [];
for( i=0; i<I; i++) {
tmp.push(mat[i][j]);
}
ret.push(tmp);
}
return ret;
}

View File

@@ -0,0 +1,64 @@
'use strict';
module.exports = function scatter(a, j, w, x, u, mark, c, f, inverse, update, value) {
// a arrays
var avalues = a._values;
var aindex = a._index;
var aptr = a._ptr;
// c arrays
var cindex = c._index;
// vars
var k, k0, k1, i;
// check we need to process values (pattern matrix)
if (x) {
// values in j
for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) {
// row
i = aindex[k];
// check value exists in current j
if (w[i] !== mark) {
// i is new entry in j
w[i] = mark;
// add i to pattern of C
cindex.push(i);
// x(i) = A, check we need to call function this time
if (update) {
// copy value to workspace calling callback function
x[i] = inverse ? f(avalues[k], value) : f(value, avalues[k]);
// function was called on current row
u[i] = mark;
}
else {
// copy value to workspace
x[i] = avalues[k];
}
}
else {
// i exists in C already
x[i] = inverse ? f(avalues[k], x[i]) : f(x[i], avalues[k]);
// function was called on current row
u[i] = mark;
}
}
}
else {
// values in j
for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) {
// row
i = aindex[k];
// check value exists in current j
if (w[i] !== mark) {
// i is new entry in j
w[i] = mark;
// add i to pattern of C
cindex.push(i);
}
else {
// indicate function was called on current row
u[i] = mark;
}
}
}
};

View File

@@ -0,0 +1,19 @@
var Emitter = require('tiny-emitter');
/**
* Extend given object with emitter functions `on`, `off`, `once`, `emit`
* @param {Object} obj
* @return {Object} obj
*/
exports.mixin = function (obj) {
// create event emitter
var emitter = new Emitter();
// bind methods to obj (we don't want to expose the emitter.e Array...)
obj.on = emitter.on.bind(emitter);
obj.off = emitter.off.bind(emitter);
obj.once = emitter.once.bind(emitter);
obj.emit = emitter.emit.bind(emitter);
return obj;
};

View File

@@ -0,0 +1,45 @@
// function utils
/*
* Memoize a given function by caching the computed result.
* The cache of a memoized function can be cleared by deleting the `cache`
* property of the function.
*
* @param {function} fn The function to be memoized.
* Must be a pure function.
* @param {function(args: Array)} [hasher] A custom hash builder.
* Is JSON.stringify by default.
* @return {function} Returns the memoized function
*/
exports.memoize = function(fn, hasher) {
return function memoize() {
if (typeof memoize.cache !== 'object') {
memoize.cache = {};
}
var args = [];
for (var i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}
var hash = hasher ? hasher(args) : JSON.stringify(args);
if (!(hash in memoize.cache)) {
return memoize.cache[hash] = fn.apply(fn, args);
}
return memoize.cache[hash];
};
};
/**
* Find the maximum number of arguments expected by a typed function.
* @param {function} fn A typed function
* @return {number} Returns the maximum number of expected arguments.
* Returns -1 when no signatures where found on the function.
*/
exports.maxArgumentCount = function (fn) {
return Object.keys(fn.signatures || {})
.reduce(function (args, signature) {
var count = (signature.match(/,/g) || []).length + 1;
return Math.max(args, count);
}, -1);
};

View File

@@ -0,0 +1,10 @@
'use strict';
exports.array = require('./array');
exports['boolean'] = require('./boolean');
exports['function'] = require('./function');
exports.number = require('./number');
exports.object = require('./object');
exports.string = require('./string');
exports.types = require('./types');
exports.emitter = require('./emitter');

View File

@@ -0,0 +1,104 @@
'use strict';
exports.symbols = {
// GREEK LETTERS
Alpha: 'A', alpha: '\\alpha',
Beta: 'B', beta: '\\beta',
Gamma: '\\Gamma', gamma: '\\gamma',
Delta: '\\Delta', delta: '\\delta',
Epsilon: 'E', epsilon: '\\epsilon', varepsilon: '\\varepsilon',
Zeta: 'Z', zeta: '\\zeta',
Eta: 'H', eta: '\\eta',
Theta: '\\Theta', theta: '\\theta', vartheta: '\\vartheta',
Iota: 'I', iota: '\\iota',
Kappa: 'K', kappa: '\\kappa', varkappa: '\\varkappa',
Lambda: '\\Lambda', lambda: '\\lambda',
Mu: 'M', mu: '\\mu',
Nu: 'N', nu: '\\nu',
Xi: '\\Xi', xi: '\\xi',
Omicron: 'O', omicron: 'o',
Pi: '\\Pi', pi: '\\pi', varpi: '\\varpi',
Rho: 'P', rho: '\\rho', varrho: '\\varrho',
Sigma: '\\Sigma', sigma: '\\sigma', varsigma: '\\varsigma',
Tau: 'T', tau: '\\tau',
Upsilon: '\\Upsilon', upsilon: '\\upsilon',
Phi: '\\Phi', phi: '\\phi', varphi: '\\varphi',
Chi: 'X', chi: '\\chi',
Psi: '\\Psi', psi: '\\psi',
Omega: '\\Omega', omega: '\\omega',
//logic
'true': '\\mathrm{True}',
'false': '\\mathrm{False}',
//other
i: 'i', //TODO use \i ??
inf: '\\infty',
Inf: '\\infty',
infinity: '\\infty',
Infinity: '\\infty',
oo: '\\infty',
lim: '\\lim',
'undefined': '\\mathbf{?}'
};
exports.operators = {
'transpose': '^\\top',
'factorial': '!',
'pow': '^',
'dotPow': '.^\\wedge', //TODO find ideal solution
'unaryPlus': '+',
'unaryMinus': '-',
'bitNot': '~', //TODO find ideal solution
'not': '\\neg',
'multiply': '\\cdot',
'divide': '\\frac', //TODO how to handle that properly?
'dotMultiply': '.\\cdot', //TODO find ideal solution
'dotDivide': '.:', //TODO find ideal solution
'mod': '\\mod',
'add': '+',
'subtract': '-',
'to': '\\rightarrow',
'leftShift': '<<',
'rightArithShift': '>>',
'rightLogShift': '>>>',
'equal': '=',
'unequal': '\\neq',
'smaller': '<',
'larger': '>',
'smallerEq': '\\leq',
'largerEq': '\\geq',
'bitAnd': '\\&',
'bitXor': '\\underline{|}',
'bitOr': '|',
'and': '\\wedge',
'xor': '\\veebar',
'or': '\\vee'
};
exports.defaultTemplate = '\\mathrm{${name}}\\left(${args}\\right)';
var units = {
deg: '^\\circ'
};
//@param {string} name
//@param {boolean} isUnit
exports.toSymbol = function (name, isUnit) {
isUnit = typeof isUnit === 'undefined' ? false : isUnit;
if (isUnit) {
if (units.hasOwnProperty(name)) {
return units[name];
}
return '\\mathrm{' + name + '}';
}
if (exports.symbols.hasOwnProperty(name)) {
return exports.symbols[name];
}
else if (name.indexOf('_') !== -1) {
//symbol with index (eg. alpha_1)
var index = name.indexOf('_');
return exports.toSymbol(name.substring(0, index)) + '_{'
+ exports.toSymbol(name.substring(index + 1)) + '}';
}
return name;
};

View File

@@ -0,0 +1,506 @@
'use strict';
/**
* @typedef {{sign: '+' | '-' | '', coefficients: number[], exponent: number}} SplitValue
*/
/**
* Test whether value is a number
* @param {*} value
* @return {boolean} isNumber
*/
exports.isNumber = function(value) {
return typeof value === 'number';
};
/**
* Check if a number is integer
* @param {number | boolean} value
* @return {boolean} isInteger
*/
exports.isInteger = function(value) {
return isFinite(value)
? (value == Math.round(value))
: false;
// Note: we use ==, not ===, as we can have Booleans as well
};
/**
* Calculate the sign of a number
* @param {number} x
* @returns {*}
*/
exports.sign = Math.sign || function(x) {
if (x > 0) {
return 1;
}
else if (x < 0) {
return -1;
}
else {
return 0;
}
};
/**
* Convert a number to a formatted string representation.
*
* Syntax:
*
* format(value)
* format(value, options)
* format(value, precision)
* format(value, fn)
*
* Where:
*
* {number} value The value to be formatted
* {Object} options An object with formatting options. Available options:
* {string} notation
* Number notation. Choose from:
* 'fixed' Always use regular number notation.
* For example '123.40' and '14000000'
* 'exponential' Always use exponential notation.
* For example '1.234e+2' and '1.4e+7'
* 'engineering' Always use engineering notation.
* For example '123.4e+0' and '14.0e+6'
* 'auto' (default) Regular number notation for numbers
* having an absolute value between
* `lower` and `upper` bounds, and uses
* exponential notation elsewhere.
* Lower bound is included, upper bound
* is excluded.
* For example '123.4' and '1.4e7'.
* {number} precision A number between 0 and 16 to round
* the digits of the number.
* In case of notations 'exponential' and
* 'auto', `precision` defines the total
* number of significant digits returned
* and is undefined by default.
* In case of notation 'fixed',
* `precision` defines the number of
* significant digits after the decimal
* point, and is 0 by default.
* {Object} exponential An object containing two parameters,
* {number} lower and {number} upper,
* used by notation 'auto' to determine
* when to return exponential notation.
* Default values are `lower=1e-3` and
* `upper=1e5`.
* Only applicable for notation `auto`.
* {Function} fn A custom formatting function. Can be used to override the
* built-in notations. Function `fn` is called with `value` as
* parameter and must return a string. Is useful for example to
* format all values inside a matrix in a particular way.
*
* Examples:
*
* format(6.4); // '6.4'
* format(1240000); // '1.24e6'
* format(1/3); // '0.3333333333333333'
* format(1/3, 3); // '0.333'
* format(21385, 2); // '21000'
* format(12.071, {notation: 'fixed'}); // '12'
* format(2.3, {notation: 'fixed', precision: 2}); // '2.30'
* format(52.8, {notation: 'exponential'}); // '5.28e+1'
* format(12345678, {notation: 'engineering'}); // '12.345678e+6'
*
* @param {number} value
* @param {Object | Function | number} [options]
* @return {string} str The formatted value
*/
exports.format = function(value, options) {
if (typeof options === 'function') {
// handle format(value, fn)
return options(value);
}
// handle special cases
if (value === Infinity) {
return 'Infinity';
}
else if (value === -Infinity) {
return '-Infinity';
}
else if (isNaN(value)) {
return 'NaN';
}
// default values for options
var notation = 'auto';
var precision = undefined;
if (options) {
// determine notation from options
if (options.notation) {
notation = options.notation;
}
// determine precision from options
if (exports.isNumber(options)) {
precision = options;
}
else if (options.precision) {
precision = options.precision;
}
}
// handle the various notations
switch (notation) {
case 'fixed':
return exports.toFixed(value, precision);
case 'exponential':
return exports.toExponential(value, precision);
case 'engineering':
return exports.toEngineering(value, precision);
case 'auto':
return exports
.toPrecision(value, precision, options && options.exponential)
// remove trailing zeros after the decimal point
.replace(/((\.\d*?)(0+))($|e)/, function () {
var digits = arguments[2];
var e = arguments[4];
return (digits !== '.') ? digits + e : e;
});
default:
throw new Error('Unknown notation "' + notation + '". ' +
'Choose "auto", "exponential", or "fixed".');
}
};
/**
* Split a number into sign, coefficients, and exponent
* @param {number | string} value
* @return {SplitValue}
* Returns an object containing sign, coefficients, and exponent
*/
exports.splitNumber = function (value) {
// parse the input value
var match = String(value).toLowerCase().match(/^0*?(-?)(\d+\.?\d*)(e([+-]?\d+))?$/);
if (!match) {
throw new SyntaxError('Invalid number ' + value);
}
var sign = match[1];
var digits = match[2];
var exponent = parseFloat(match[4] || '0');
var dot = digits.indexOf('.');
exponent += (dot !== -1) ? (dot - 1) : (digits.length - 1);
var coefficients = digits
.replace('.', '') // remove the dot (must be removed before removing leading zeros)
.replace(/^0*/, function (zeros) {
// remove leading zeros, add their count to the exponent
exponent -= zeros.length;
return '';
})
.replace(/0*$/, '') // remove trailing zeros
.split('')
.map(function (d) {
return parseInt(d);
});
if (coefficients.length === 0) {
coefficients.push(0);
exponent++;
}
return {
sign: sign,
coefficients: coefficients,
exponent: exponent
};
};
/**
* Format a number in engineering notation. Like '1.23e+6', '2.3e+0', '3.500e-3'
* @param {number | string} value
* @param {number} [precision=0] Optional number of decimals after the
* decimal point. Zero by default.
*/
exports.toEngineering = function (value, precision) {
if (isNaN(value) || !isFinite(value)) {
return String(value);
}
var rounded = exports.roundDigits(exports.splitNumber(value), precision);
var e = rounded.exponent;
var c = rounded.coefficients;
// find nearest lower multiple of 3 for exponent
var newExp = e % 3 === 0 ? e : (e < 0 ? (e - 3) - (e % 3) : e - (e % 3));
// concatenate coefficients with necessary zeros
var significandsDiff = e >= 0 ? e : Math.abs(newExp);
// add zeros if necessary (for ex: 1e+8)
if (c.length - 1 < significandsDiff) c = c.concat(zeros(significandsDiff - (c.length - 1)));
// find difference in exponents
var expDiff = Math.abs(e - newExp);
var decimalIdx = 1;
// push decimal index over by expDiff times
while (--expDiff >= 0) decimalIdx++;
// if all coefficient values are zero after the decimal point, don't add a decimal value.
// otherwise concat with the rest of the coefficients
var decimals = c.slice(decimalIdx).join('');
var decimalVal = decimals.match(/[1-9]/) ? ('.' + decimals) : '';
var str = c.slice(0, decimalIdx).join('') +
decimalVal +
'e' + (e >= 0 ? '+' : '') + newExp.toString();
return rounded.sign + str;
};
/**
* Format a number with fixed notation.
* @param {number | string} value
* @param {number} [precision=0] Optional number of decimals after the
* decimal point. Zero by default.
*/
exports.toFixed = function (value, precision) {
if (isNaN(value) || !isFinite(value)) {
return String(value);
}
var splitValue = exports.splitNumber(value)
var rounded = exports.roundDigits(splitValue, splitValue.exponent + 1 + (precision || 0));
var c = rounded.coefficients;
var p = rounded.exponent + 1; // exponent may have changed
// append zeros if needed
var pp = p + (precision || 0);
if (c.length < pp) {
c = c.concat(zeros(pp - c.length));
}
// prepend zeros if needed
if (p < 0) {
c = zeros(-p + 1).concat(c);
p = 1;
}
// insert a dot if needed
if (precision) {
c.splice(p, 0, (p === 0) ? '0.' : '.');
}
return rounded.sign + c.join('');
};
/**
* Format a number in exponential notation. Like '1.23e+5', '2.3e+0', '3.500e-3'
* @param {number | string} value
* @param {number} [precision] Number of digits in formatted output.
* If not provided, the maximum available digits
* is used.
*/
exports.toExponential = function (value, precision) {
if (isNaN(value) || !isFinite(value)) {
return String(value);
}
// round if needed, else create a clone
var split = exports.splitNumber(value)
var rounded = precision ? exports.roundDigits(split, precision) : split;
var c = rounded.coefficients;
var e = rounded.exponent;
// append zeros if needed
if (c.length < precision) {
c = c.concat(zeros(precision - c.length));
}
// format as `C.CCCe+EEE` or `C.CCCe-EEE`
var first = c.shift();
return rounded.sign + first + (c.length > 0 ? ('.' + c.join('')) : '') +
'e' + (e >= 0 ? '+' : '') + e;
}
/**
* Format a number with a certain precision
* @param {number | string} value
* @param {number} [precision=undefined] Optional number of digits.
* @param {{lower: number | undefined, upper: number | undefined}} [options]
* By default:
* lower = 1e-3 (excl)
* upper = 1e+5 (incl)
* @return {string}
*/
exports.toPrecision = function (value, precision, options) {
if (isNaN(value) || !isFinite(value)) {
return String(value);
}
// determine lower and upper bound for exponential notation.
var lower = (options && options.lower !== undefined) ? options.lower : 1e-3;
var upper = (options && options.upper !== undefined) ? options.upper : 1e+5;
var split = exports.splitNumber(value)
var abs = Math.abs(Math.pow(10, split.exponent));
if (abs < lower || abs >= upper) {
// exponential notation
return exports.toExponential(value, precision);
}
else {
var rounded = precision ? exports.roundDigits(split, precision) : split;
var c = rounded.coefficients;
var e = rounded.exponent;
// append trailing zeros
if (c.length < precision) {
c = c.concat(zeros(precision - c.length));
}
// append trailing zeros
// TODO: simplify the next statement
c = c.concat(zeros(e - c.length + 1 +
(c.length < precision ? precision - c.length : 0)));
// prepend zeros
c = zeros(-e).concat(c);
var dot = e > 0 ? e : 0;
if (dot < c.length - 1) {
c.splice(dot + 1, 0, '.');
}
return rounded.sign + c.join('');
}
}
/**
* Round the number of digits of a number *
* @param {SplitValue} split A value split with .splitNumber(value)
* @param {number} precision A positive integer
* @return {SplitValue}
* Returns an object containing sign, coefficients, and exponent
* with rounded digits
*/
exports.roundDigits = function (split, precision) {
// create a clone
var rounded = {
sign: split.sign,
coefficients: split.coefficients,
exponent: split.exponent
}
var c = rounded.coefficients;
// prepend zeros if needed
while (precision <= 0) {
c.unshift(0);
rounded.exponent++;
precision++;
}
if (c.length > precision) {
var removed = c.splice(precision, c.length - precision);
if (removed[0] >= 5) {
var i = precision - 1;
c[i]++;
while (c[i] === 10) {
c.pop();
if (i === 0) {
c.unshift(0);
rounded.exponent++;
i++;
}
i--;
c[i]++;
}
}
}
return rounded;
};
/**
* Create an array filled with zeros.
* @param {number} length
* @return {Array}
*/
function zeros(length) {
var arr = [];
for (var i = 0; i < length; i++) {
arr.push(0);
}
return arr;
}
/**
* Count the number of significant digits of a number.
*
* For example:
* 2.34 returns 3
* 0.0034 returns 2
* 120.5e+30 returns 4
*
* @param {number} value
* @return {number} digits Number of significant digits
*/
exports.digits = function(value) {
return value
.toExponential()
.replace(/e.*$/, '') // remove exponential notation
.replace( /^0\.?0*|\./, '') // remove decimal point and leading zeros
.length
};
/**
* Minimum number added to one that makes the result different than one
*/
exports.DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16;
/**
* Compares two floating point numbers.
* @param {number} x First value to compare
* @param {number} y Second value to compare
* @param {number} [epsilon] The maximum relative difference between x and y
* If epsilon is undefined or null, the function will
* test whether x and y are exactly equal.
* @return {boolean} whether the two numbers are nearly equal
*/
exports.nearlyEqual = function(x, y, epsilon) {
// if epsilon is null or undefined, test whether x and y are exactly equal
if (epsilon == null) {
return x == y;
}
// use "==" operator, handles infinities
if (x == y) {
return true;
}
// NaN
if (isNaN(x) || isNaN(y)) {
return false;
}
// at this point x and y should be finite
if(isFinite(x) && isFinite(y)) {
// check numbers are very close, needed when comparing numbers near zero
var diff = Math.abs(x - y);
if (diff < exports.DBL_EPSILON) {
return true;
}
else {
// use relative error
return diff <= Math.max(Math.abs(x), Math.abs(y)) * epsilon;
}
}
// Infinite and Number or negative Infinite and positive Infinite cases
return false;
};

View File

@@ -0,0 +1,241 @@
'use strict';
/**
* Clone an object
*
* clone(x)
*
* Can clone any primitive type, array, and object.
* If x has a function clone, this function will be invoked to clone the object.
*
* @param {*} x
* @return {*} clone
*/
exports.clone = function clone(x) {
var type = typeof x;
// immutable primitive types
if (type === 'number' || type === 'string' || type === 'boolean' ||
x === null || x === undefined) {
return x;
}
// use clone function of the object when available
if (typeof x.clone === 'function') {
return x.clone();
}
// array
if (Array.isArray(x)) {
return x.map(function (value) {
return clone(value);
});
}
if (x instanceof Number) return new Number(x.valueOf());
if (x instanceof String) return new String(x.valueOf());
if (x instanceof Boolean) return new Boolean(x.valueOf());
if (x instanceof Date) return new Date(x.valueOf());
if (x && x.isBigNumber === true) return x; // bignumbers are immutable
if (x instanceof RegExp) throw new TypeError('Cannot clone ' + x); // TODO: clone a RegExp
// object
var m = {};
for (var key in x) {
if (x.hasOwnProperty(key)) {
m[key] = clone(x[key]);
}
}
return m;
};
/**
* Extend object a with the properties of object b
* @param {Object} a
* @param {Object} b
* @return {Object} a
*/
exports.extend = function(a, b) {
for (var prop in b) {
if (b.hasOwnProperty(prop)) {
a[prop] = b[prop];
}
}
return a;
};
/**
* Deep extend an object a with the properties of object b
* @param {Object} a
* @param {Object} b
* @returns {Object}
*/
exports.deepExtend = function deepExtend (a, b) {
// TODO: add support for Arrays to deepExtend
if (Array.isArray(b)) {
throw new TypeError('Arrays are not supported by deepExtend');
}
for (var prop in b) {
if (b.hasOwnProperty(prop)) {
if (b[prop] && b[prop].constructor === Object) {
if (a[prop] === undefined) {
a[prop] = {};
}
if (a[prop].constructor === Object) {
deepExtend(a[prop], b[prop]);
}
else {
a[prop] = b[prop];
}
} else if (Array.isArray(b[prop])) {
throw new TypeError('Arrays are not supported by deepExtend');
} else {
a[prop] = b[prop];
}
}
}
return a;
};
/**
* Deep test equality of all fields in two pairs of arrays or objects.
* @param {Array | Object} a
* @param {Array | Object} b
* @returns {boolean}
*/
exports.deepEqual = function deepEqual (a, b) {
var prop, i, len;
if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return false;
}
if (a.length != b.length) {
return false;
}
for (i = 0, len = a.length; i < len; i++) {
if (!exports.deepEqual(a[i], b[i])) {
return false;
}
}
return true;
}
else if (a instanceof Object) {
if (Array.isArray(b) || !(b instanceof Object)) {
return false;
}
for (prop in a) {
//noinspection JSUnfilteredForInLoop
if (!exports.deepEqual(a[prop], b[prop])) {
return false;
}
}
for (prop in b) {
//noinspection JSUnfilteredForInLoop
if (!exports.deepEqual(a[prop], b[prop])) {
return false;
}
}
return true;
}
else {
return (typeof a === typeof b) && (a == b);
}
};
/**
* Test whether the current JavaScript engine supports Object.defineProperty
* @returns {boolean} returns true if supported
*/
exports.canDefineProperty = function () {
// test needed for broken IE8 implementation
try {
if (Object.defineProperty) {
Object.defineProperty({}, 'x', { get: function () {} });
return true;
}
} catch (e) {}
return false;
};
/**
* Attach a lazy loading property to a constant.
* The given function `fn` is called once when the property is first requested.
* On older browsers (<IE8), the function will fall back to direct evaluation
* of the properties value.
* @param {Object} object Object where to add the property
* @param {string} prop Property name
* @param {Function} fn Function returning the property value. Called
* without arguments.
*/
exports.lazy = function (object, prop, fn) {
if (exports.canDefineProperty()) {
var _uninitialized = true;
var _value;
Object.defineProperty(object, prop, {
get: function () {
if (_uninitialized) {
_value = fn();
_uninitialized = false;
}
return _value;
},
set: function (value) {
_value = value;
_uninitialized = false;
},
configurable: true,
enumerable: true
});
}
else {
// fall back to immediate evaluation
object[prop] = fn();
}
};
/**
* Traverse a path into an object.
* When a namespace is missing, it will be created
* @param {Object} object
* @param {string} path A dot separated string like 'name.space'
* @return {Object} Returns the object at the end of the path
*/
exports.traverse = function(object, path) {
var obj = object;
if (path) {
var names = path.split('.');
for (var i = 0; i < names.length; i++) {
var name = names[i];
if (!(name in obj)) {
obj[name] = {};
}
obj = obj[name];
}
}
return obj;
};
/**
* Test whether an object is a factory. a factory has fields:
*
* - factory: function (type: Object, config: Object, load: function, typed: function [, math: Object]) (required)
* - name: string (optional)
* - path: string A dot separated path (optional)
* - math: boolean If true (false by default), the math namespace is passed
* as fifth argument of the factory function
*
* @param {*} object
* @returns {boolean}
*/
exports.isFactory = function (object) {
return object && typeof object.factory === 'function';
};

View File

@@ -0,0 +1,143 @@
'use strict';
var formatNumber = require('./number').format;
var formatBigNumber = require('./bignumber/formatter').format;
/**
* Test whether value is a string
* @param {*} value
* @return {boolean} isString
*/
exports.isString = function(value) {
return typeof value === 'string';
};
/**
* Check if a text ends with a certain string.
* @param {string} text
* @param {string} search
*/
exports.endsWith = function(text, search) {
var start = text.length - search.length;
var end = text.length;
return (text.substring(start, end) === search);
};
/**
* Format a value of any type into a string.
*
* Usage:
* math.format(value)
* math.format(value, precision)
*
* When value is a function:
*
* - When the function has a property `syntax`, it returns this
* syntax description.
* - In other cases, a string `'function'` is returned.
*
* When `value` is an Object:
*
* - When the object contains a property `format` being a function, this
* function is invoked as `value.format(options)` and the result is returned.
* - When the object has its own `toString` method, this method is invoked
* and the result is returned.
* - In other cases the function will loop over all object properties and
* return JSON object notation like '{"a": 2, "b": 3}'.
*
* Example usage:
* math.format(2/7); // '0.2857142857142857'
* math.format(math.pi, 3); // '3.14'
* math.format(new Complex(2, 3)); // '2 + 3i'
* math.format('hello'); // '"hello"'
*
* @param {*} value Value to be stringified
* @param {Object | number | Function} [options] Formatting options. See
* lib/utils/number:format for a
* description of the available
* options.
* @return {string} str
*/
exports.format = function(value, options) {
if (typeof value === 'number') {
return formatNumber(value, options);
}
if (value && value.isBigNumber === true) {
return formatBigNumber(value, options);
}
if (value && value.isFraction === true) {
if (!options || options.fraction !== 'decimal') {
// output as ratio, like '1/3'
return (value.s * value.n) + '/' + value.d;
}
else {
// output as decimal, like '0.(3)'
return value.toString();
}
}
if (Array.isArray(value)) {
return formatArray(value, options);
}
if (exports.isString(value)) {
return '"' + value + '"';
}
if (typeof value === 'function') {
return value.syntax ? String(value.syntax) : 'function';
}
if (value && typeof value === 'object') {
if (typeof value.format === 'function') {
return value.format(options);
}
else if (value && value.toString() !== {}.toString()) {
// this object has a non-native toString method, use that one
return value.toString();
}
else {
var entries = [];
for (var key in value) {
if (value.hasOwnProperty(key)) {
entries.push('"' + key + '": ' + exports.format(value[key], options));
}
}
return '{' + entries.join(', ') + '}';
}
}
return String(value);
};
/**
* Recursively format an n-dimensional matrix
* Example output: "[[1, 2], [3, 4]]"
* @param {Array} array
* @param {Object | number | Function} [options] Formatting options. See
* lib/utils/number:format for a
* description of the available
* options.
* @returns {string} str
*/
function formatArray (array, options) {
if (Array.isArray(array)) {
var str = '[';
var len = array.length;
for (var i = 0; i < len; i++) {
if (i != 0) {
str += ', ';
}
str += formatArray(array[i], options);
}
str += ']';
return str;
}
else {
return exports.format(array, options);
}
}

View File

@@ -0,0 +1,54 @@
'use strict';
/**
* Determine the type of a variable
*
* type(x)
*
* The following types are recognized:
*
* 'undefined'
* 'null'
* 'boolean'
* 'number'
* 'string'
* 'Array'
* 'Function'
* 'Date'
* 'RegExp'
* 'Object'
*
* @param {*} x
* @return {string} Returns the name of the type. Primitive types are lower case,
* non-primitive types are upper-camel-case.
* For example 'number', 'string', 'Array', 'Date'.
*/
exports.type = function(x) {
var type = typeof x;
if (type === 'object') {
if (x === null) return 'null';
if (x instanceof Boolean) return 'boolean';
if (x instanceof Number) return 'number';
if (x instanceof String) return 'string';
if (Array.isArray(x)) return 'Array';
if (x instanceof Date) return 'Date';
if (x instanceof RegExp) return 'RegExp';
return 'Object';
}
if (type === 'function') return 'Function';
return type;
};
/**
* Test whether a value is a scalar
* @param x
* @return {boolean} Returns true when x is a scalar, returns false when
* x is a Matrix or Array.
*/
exports.isScalar = function (x) {
return !((x && x.isMatrix) || Array.isArray(x));
};