diff --git a/math.js b/math.js index 8d1538a12..6422fd0a4 100644 --- a/math.js +++ b/math.js @@ -6,8 +6,8 @@ * It features real and complex numbers, units, matrices, a large set of * mathematical functions, and a flexible expression parser. * - * @version 0.4.0 - * @date 2013-03-16 + * @version 0.5.0-SNAPSHOT + * @date 2013-03-19 * * @license * Copyright (C) 2013 Jos de Jong @@ -188,6 +188,87 @@ util.map2 = function map2(array1, array2, fn) { return res; }; +util.array = {}; + +/** + * Recursively get the size of an array or object. + * The size is calculated from the first dimension. + * The array is not checked for matching dimensions, that should be done using + * util.array.validate or util.array.validatedSize + * @param {Array | Object} x + * @Return {Number[]} size + */ +util.array.size = function size (x) { + if (x instanceof Array) { + var sizeX = x.length; + if (sizeX) { + var size0 = util.array.size(x[0]); + return [sizeX].concat(size0); + } + else { + return [sizeX]; + } + } + else { + return []; + } +}; + +/** + * Verify whether each element in an n dimensional array has the correct size + * @param {Array | Object} array Array to be validated + * @param {Number[]} size Array with dimensions + * @param {Number} [dim] Current dimension + * @throw Error + */ +util.array.validate = function validate(array, size, dim) { + var i, + len = array.length; + if (!dim) { + dim = 0; + } + + if (len != size[dim]) { + throw new Error('Dimension mismatch (' + 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 (!(child instanceof Array)) { + throw new Error('Dimension mismatch ' + + '(' + (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[i] instanceof Array) { + throw new Error('Dimension mismatch ' + + '(' + (size.length + 1) + ' > ' + size.length + ')'); + } + } + } + + return true; +}; + +/** + * Recursively get the size of a multidimensional array or object. + * The array is checked for matching dimensions. + * @param {Array | Object} x + * @Return {Number[]} size + */ +util.array.validatedSize = function validatedSize(x) { + var s = util.array.size(x); + util.array.validate(x, s); + return s; +}; + // Internet Explorer 8 and older does not support Array.indexOf, so we define // it here in that case. // http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/ @@ -570,9 +651,9 @@ function isComplex(value) { /** * Create a copy of the complex value - * @return {Complex} copy + * @return {Complex} clone */ -Complex.prototype.copy = function () { +Complex.prototype.clone = function () { return new Complex(this.re, this.im); }; @@ -651,6 +732,117 @@ Complex.doc = { }; +/** + * @constructor Matrix + * + * TODO: document Matrix + * + * @param {Array} [array] A multi dimensional array + */ +function Matrix(array) { + if (this.constructor != Matrix) { + throw new SyntaxError( + 'Matrix constructor must be called with the new operator'); + } + + this.array = array || []; +} + +math.Matrix = Matrix; + +// TODO: implement a parse method + +// TODO: implement method toVector +// TODO: implement method isVector + + + +/** + * Retrieve the size of the matrix. + * The size of the matrix will be validated too + * @returns {Number[]} size + */ +Matrix.prototype.size = function () { + return util.array.validatedSize(this.array); +}; + +/** + * Get the scalar value of the matrix. Will return null if the matrix is no + * scalar value + * @return {* | null} scalar + */ +Matrix.prototype.toScalar = function () { + var value = this.array; + while (value instanceof Array && value.length == 1) { + value = value[0]; + } + + if (value instanceof Array) { + return null; + } + else { + return value; + } +}; + +/** + * Test whether the matrix is a scalar. + * @return {boolean} isScalar + */ +Matrix.prototype.isScalar = function () { + var value = this.array; + while (value instanceof Array && value.length == 1) { + value = array[0]; + } + return !(value instanceof Array); +}; + +/** + * Get the matrix contents as vector. Returns null if the Matrix is no vector + * return {Array} vector + */ +Matrix.prototype.toVector = function () { + var s = util.array.validatedSize(this.array); + if (s.length != 2) { + return null; + } + if (s[0] != 1 && s[1] != 1) { + return null; + } + + if (s[0] == 1) { + return this.array[0].concat(); + } + else { + var vector = []; + this.array.forEach(function (row, index) { + vector[index] = row[0]; + }); + return vector; + } +}; + +/** + * Test if the matrix is a vector. + * A matrix is a vector when the dims is [1 x n] or [n x 1] + * return {boolean} isVector + */ +Matrix.prototype.isVector = function () { + var s = util.array.validatedSize(this.array); + if (s.length != 2) { + return false; + } + return (s[0] == 1 || s[1] == 1); +}; + +/** + * Get the primitive value of the Matrix: a multidimensional array + * @returns {Array} array + */ +Matrix.prototype.valueOf = function () { + return this.array; +}; + /** * Utility functions for Numbers */ @@ -938,9 +1130,9 @@ function isUnit(value) { /** * create a copy of this unit - * @return {Unit} copy + * @return {Unit} clone */ -Unit.prototype.copy = function () { +Unit.prototype.clone = function () { var clone = new Unit(); for (var p in this) { @@ -1451,7 +1643,11 @@ function abs(x) { if (x instanceof Array) { return util.map(x, abs); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return abs(x.valueOf()); + } throw newUnsupportedTypeError('abs', x); } @@ -1529,7 +1725,7 @@ function add(x, y) { throw new Error('Unit on right hand side of operator + has no value'); } - var res = x.copy(); + var res = x.clone(); res.value += y.value; res.fixPrefix = false; return res; @@ -1543,7 +1739,11 @@ function add(x, y) { if (x instanceof Array || y instanceof Array) { return util.map2(x, y, add); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return add(x.valueOf()); + } throw newUnsupportedTypeError('add', x, y); } @@ -1597,7 +1797,11 @@ function ceil(x) { if (x instanceof Array) { return util.map(x, ceil); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return ceil(x.valueOf()); + } throw newUnsupportedTypeError('ceil', x); } @@ -1646,7 +1850,11 @@ function cube(x) { if (x instanceof Array) { return multiply(multiply(x, x), x); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return cube(x.valueOf()); + } throw newUnsupportedTypeError('cube', x); } @@ -1711,7 +1919,7 @@ function divide(x, y) { if (x instanceof Unit) { if (isNumber(y)) { - var res = x.copy(); + var res = x.clone(); res.value /= y; return res; } @@ -1731,7 +1939,10 @@ function divide(x, y) { // TODO: implement scalar/matrix } - // TODO: implement matrix support + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive value + return divide(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('divide', x, y); } @@ -1820,7 +2031,11 @@ function equal(x, y) { if (x instanceof Array || y instanceof Array) { return util.map2(x, y, equal); } - // TODO: implement matrix support + + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return equal(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('equal', x, y); } @@ -1877,7 +2092,11 @@ function exp (x) { if (x instanceof Array) { return util.map(x, exp); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return exp(x.valueOf()); + } throw newUnsupportedTypeError('exp', x); } @@ -1931,7 +2150,11 @@ function fix(x) { if (x instanceof Array) { return util.map(x, fix); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return fix(x.valueOf()); + } throw newUnsupportedTypeError('fix', x); } @@ -1984,7 +2207,11 @@ function floor(x) { if (x instanceof Array) { return util.map(x, floor); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return floor(x.valueOf()); + } throw newUnsupportedTypeError('floor', x); } @@ -2055,7 +2282,11 @@ function larger(x, y) { if (x instanceof Array || y instanceof Array) { return util.map2(x, y, equal); } - // TODO: implement matrix support + + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return larger(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('larger', x, y); } @@ -2132,7 +2363,11 @@ function largereq(x, y) { if (x instanceof Array || y instanceof Array) { return util.map2(x, y, largereq); } - // TODO: implement matrix support + + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return largereq(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('largereq', x, y); } @@ -2205,7 +2440,10 @@ function log(x, base) { return divide(log(x), log(base)); } - // TODO: implement matrix support + if (x.valueOf() !== x || base.valueOf() !== base) { + // fallback on the objects primitive values + return log(x.valueOf(), base.valueOf()); + } throw newUnsupportedTypeError('log', x, base); } @@ -2272,7 +2510,11 @@ function log10(x) { if (x instanceof Array) { return util.map(x, log10); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return log10(x.valueOf()); + } throw newUnsupportedTypeError('log10', x); } @@ -2339,7 +2581,11 @@ function mod(x, y) { if (x instanceof Array || y instanceof Array) { return util.map2(x, y, mod); } - // TODO: implement matrix support + + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return mod(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('mod', x, y); } @@ -2391,7 +2637,7 @@ function multiply(x, y) { return multiplyComplex(new Complex(x, 0), y); } else if (y instanceof Unit) { - res = y.copy(); + res = y.clone(); res.value *= x; return res; } @@ -2408,7 +2654,7 @@ function multiply(x, y) { } else if (x instanceof Unit) { if (isNumber(y)) { - res = x.copy(); + res = x.clone(); res.value *= y; return res; } @@ -2416,8 +2662,8 @@ function multiply(x, y) { else if (x instanceof Array) { if (y instanceof Array) { // matrix * matrix - var sizeX = size(x)[0]; - var sizeY = size(y)[0]; + var sizeX = util.array.validatedSize(x); + var sizeY = util.array.validatedSize(y); if (sizeX.length != 2) { throw new Error('Can only multiply a 2 dimensional matrix ' + @@ -2465,7 +2711,10 @@ function multiply(x, y) { return util.map2(x, y, multiply); } - // TODO: implement matrix support + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return multiply(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('multiply', x, y); } @@ -2549,7 +2798,7 @@ function pow(x, y) { } // verify that A is a 2 dimensional square matrix - var s = size(x)[0]; + var s = util.array.validatedSize(x); if (s.length != 2) { throw new Error('For A^b, A must be 2 dimensional ' + '(A has ' + s.length + ' dimensions)'); @@ -2561,7 +2810,6 @@ function pow(x, y) { if (y == 0) { // return the identity matrix - // TODO: implement method eye return eye(s[0]); } else { @@ -2574,7 +2822,10 @@ function pow(x, y) { } } - // TODO: implement matrix support + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return pow(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('pow', x, y); } @@ -2646,6 +2897,11 @@ function round(x, n) { util.map(x, round); } + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return round(x.valueOf()); + } + throw newUnsupportedTypeError('round', x); } else { @@ -2675,10 +2931,13 @@ function round(x, n) { return util.map2(x, n, round); } + if (x.valueOf() !== x || n.valueOf() !== n) { + // fallback on the objects primitive values + return larger(x.valueOf(), n.valueOf()); + } + throw newUnsupportedTypeError('round', x, n); } - - // TODO: implement matrix support } math.round = round; @@ -2754,7 +3013,11 @@ function sign(x) { if (x instanceof Array) { return util.map(x, sign); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return sign(x.valueOf()); + } throw newUnsupportedTypeError('sign', x); } @@ -2826,7 +3089,11 @@ function smaller(x, y) { if (x instanceof Array || y instanceof Array) { return util.map2(x, y, smaller); } - // TODO: implement matrix support + + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return smaller(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('smaller', x, y); } @@ -2902,7 +3169,11 @@ function smallereq(x, y) { if (x instanceof Array || y instanceof Array) { return util.map2(x, y, smallereq); } - // TODO: implement matrix support + + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return smallereq(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('smallereq', x, y); } @@ -2972,7 +3243,11 @@ function sqrt (x) { if (x instanceof Array) { return util.map(x, sqrt); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return sqrt(x.valueOf()); + } throw newUnsupportedTypeError('sqrt', x); } @@ -3023,7 +3298,11 @@ function square(x) { if (x instanceof Array) { return multiply(x, x); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return square(x.valueOf()); + } throw newUnsupportedTypeError('square', x); } @@ -3110,7 +3389,7 @@ function subtract(x, y) { throw new Error('Unit on right hand side of operator - has no value'); } - var res = x.copy(); + var res = x.clone(); res.value -= y.value; res.fixPrefix = false; @@ -3121,7 +3400,11 @@ function subtract(x, y) { if (x instanceof Array || y instanceof Array) { return util.map2(x, y, subtract); } - // TODO: implement matrix support + + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return subtract(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('subtract', x, y); } @@ -3170,7 +3453,7 @@ function unaryminus(x) { ); } else if (x instanceof Unit) { - var res = x.copy(); + var res = x.clone(); res.value = -x.value; return res; } @@ -3178,7 +3461,11 @@ function unaryminus(x) { if (x instanceof Array) { return util.map(x, unaryminus); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return unaryminus(x.valueOf()); + } throw newUnsupportedTypeError('unaryminus', x); } @@ -3249,7 +3536,11 @@ function unequal(x, y) { if (x instanceof Array || y instanceof Array) { return util.map2(x, y, unequal); } - // TODO: implement matrix support + + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return unequal(x.valueOf(), y.valueOf()); + } throw newUnsupportedTypeError('unequal', x, y); } @@ -3305,7 +3596,11 @@ function arg(x) { if (x instanceof Array) { return util.map(x, arg); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return arg(x.valueOf()); + } throw newUnsupportedTypeError('arg', x); } @@ -3359,7 +3654,11 @@ function conj(x) { if (x instanceof Array) { return util.map(x, conj); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return conj(x.valueOf()); + } throw newUnsupportedTypeError('conj', x); } @@ -3412,7 +3711,11 @@ function im(x) { if (x instanceof Array) { return util.map(x, im); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return im(x.valueOf()); + } throw newUnsupportedTypeError('im', x); } @@ -3464,7 +3767,11 @@ function re(x) { if (x instanceof Array) { return util.map(x, re); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return re(x.valueOf()); + } throw newUnsupportedTypeError('re', x); } @@ -3552,8 +3859,7 @@ function eye (m, n) { return res; } -// TODO: export method eye to math -// math.eye = eye; +math.eye = eye; /** * Function documentation @@ -3606,103 +3912,22 @@ function size (x) { } if (x instanceof Array) { - var s = getSize(x); - validate(x, s); - return [getSize(x)]; + var s = util.array.validatedSize(x); + if (s.length == 1) { + s.push(0); + } + return [s]; + } + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return size(x.valueOf()); } - // TODO: implement matrix support throw newUnsupportedTypeError('size', x); } -/** - * Recursively get the size of an array or object - * @param {Array | Object} x - * @Return {Array} - */ -function getSize (x) { - if (x instanceof Array) { - var sizeX = x.length; - if (sizeX) { - var size0 = getSize(x[0]); - return [sizeX].concat(size0); - } - else { - return [sizeX]; - } - } - else { - return []; - } -} - -/** - * Verify whether each element in an n dimensional array has the correct size - * @param {Array | Object} array Array to be validated - * @param {Number[]} size Array with dimensions - * @param {Number} [dim] Current dimension - * @throw Error - */ -function validate(array, size, dim) { - var i, - len = array.length; - if (!dim) { - dim = 0; - } - - if (len != size[dim]) { - throw new Error('Dimension mismatch (' + 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 (!(child instanceof Array)) { - throw new Error('Dimension mismatch ' + - '(' + (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[i] instanceof Array) { - throw new Error('Dimension mismatch ' + - '(' + (size.length + 1) + ' > ' + size.length + ')'); - } - } - } - - return true; -} - -/** - * Compare two arrays - * @param a - * @param b - * @return {Boolean} equal True if both arrays are equal, else false - */ -function compare(a, b) { - var len = a.length; - if (len != b.length) { - return false; - } - - for (var i = 0; i < len; i++) { - if (a[i] != b[i]) { - return false; - } - } - - return true; -} - - -// TODO: export method size to math -// math.size = size; +math.size = size; /** * Function documentation @@ -3758,7 +3983,11 @@ function factorial (x) { if (x instanceof Array) { return util.map(x, factorial); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return factorial(x.valueOf()); + } throw newUnsupportedTypeError('factorial', x); } @@ -3826,10 +4055,9 @@ function max(args) { throw new Error('Function sum requires one or more parameters (0 provided)'); } - if (arguments.length == 1 && arguments[0] instanceof Array) { - return max.apply(this, arguments[0]); + if (arguments.length == 1 && (args.valueOf() instanceof Array)) { + return max.apply(this, args.valueOf()); } - // TODO: implement matrix support var res = arguments[0]; for (var i = 1, iMax = arguments.length; i < iMax; i++) { @@ -3880,10 +4108,9 @@ function min(args) { throw new Error('Function sum requires one or more parameters (0 provided)'); } - if (arguments.length == 1 && arguments[0] instanceof Array) { - return min.apply(this, arguments[0]); + if (arguments.length == 1 && (args.valueOf() instanceof Array)) { + return min.apply(this, args.valueOf()); } - // TODO: implement matrix support var res = arguments[0]; for (var i = 1, iMax = arguments.length; i < iMax; i++) { @@ -3966,7 +4193,11 @@ function acos(x) { if (x instanceof Array) { return util.map(x, acos); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return acos(x.valueOf()); + } throw newUnsupportedTypeError('acos', x); } @@ -4036,7 +4267,11 @@ function asin(x) { if (x instanceof Array) { return util.map(x, asin); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return asin(x.valueOf()); + } throw newUnsupportedTypeError('asin', x); } @@ -4099,7 +4334,11 @@ function atan(x) { if (x instanceof Array) { return util.map(x, atan); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return atan(x.valueOf()); + } throw newUnsupportedTypeError('atan', x); } @@ -4158,7 +4397,11 @@ function atan2(y, x) { if (x instanceof Array || y instanceof Array) { return util.map2(y, x, atan2); } - // TODO: implement matrix support + + if (x.valueOf() !== x || y.valueOf() !== y) { + // fallback on the objects primitive values + return atan2(y.valueOf(), x.valueOf()); + } throw newUnsupportedTypeError('atan2', y, x); } @@ -4222,7 +4465,11 @@ function cos(x) { if (x instanceof Array) { return util.map(x, cos); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return cos(x.valueOf()); + } throw newUnsupportedTypeError('cos', x); } @@ -4287,7 +4534,11 @@ function cot(x) { if (x instanceof Array) { return util.map(x, cot); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return cot(x.valueOf()); + } throw newUnsupportedTypeError('cot', x); } @@ -4351,7 +4602,11 @@ function csc(x) { if (x instanceof Array) { return util.map(x, csc); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return csc(x.valueOf()); + } throw newUnsupportedTypeError('csc', x); } @@ -4414,7 +4669,11 @@ function sec(x) { if (x instanceof Array) { return util.map(x, sec); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return sec(x.valueOf()); + } throw newUnsupportedTypeError('sec', x); } @@ -4474,7 +4733,11 @@ function sin(x) { if (x instanceof Array) { return util.map(x, sin); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return sin(x.valueOf()); + } throw newUnsupportedTypeError('sin', x); } @@ -4540,7 +4803,11 @@ function tan(x) { if (x instanceof Array) { return util.map(x, tan); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return tan(x.valueOf()); + } throw newUnsupportedTypeError('tan', x); } @@ -4582,16 +4849,17 @@ function unit_in(x, unit) { } if (x instanceof Unit && unit instanceof Unit) { - // Test if unit has no value + if (!x.equalBase(unit)) { + throw new Error('Units do not match'); + } if (unit.hasValue) { throw new Error('Cannot convert to a unit with a value'); } - // Test if unit has a unit if (!unit.hasUnit) { throw new Error('Unit expected on the right hand side of function in'); } - var res = unit.copy(); + var res = unit.clone(); res.value = x.value; res.fixPrefix = true; @@ -4601,12 +4869,16 @@ function unit_in(x, unit) { if (x instanceof Array || unit instanceof Array) { return util.map2(x, unit, unit_in); } - // TODO: implement matrix support + + if (x.valueOf() !== x) { + // fallback on the objects primitive value + return math.in(x.valueOf()); + } throw newUnsupportedTypeError('in', x); } -math['in'] = unit_in; +math.in = unit_in; /** * Function documentation @@ -4695,7 +4967,7 @@ math.format = format; */ function formatArray (array) { var str = '['; - var s = size(array)[0]; + var s = util.array.validatedSize(array); if (s.length != 2) { return formatArrayN(array); diff --git a/math.min.js b/math.min.js index 3a748d93b..bb9a26c80 100644 --- a/math.min.js +++ b/math.min.js @@ -6,8 +6,8 @@ * It features real and complex numbers, units, matrices, a large set of * mathematical functions, and a flexible expression parser. * - * @version 0.4.0 - * @date 2013-03-16 + * @version 0.5.0-SNAPSHOT + * @date 2013-03-19 * * @license * Copyright (C) 2013 Jos de Jong @@ -24,6 +24,6 @@ * License for the specific language governing permissions and limitations under * the License. */ -(function(){function e(n,i){if(this.constructor!=e)throw new SyntaxError("Complex constructor must be called with the new operator");switch(arguments.length){case 2:if(!t(n)||!t(i))throw new TypeError("Two numbers or a single string expected in Complex constructor");this.re=n,this.im=i;break;case 1:if(!r(n))throw new TypeError("Two numbers or a single string expected in Complex constructor");var a=e.parse(n);if(a)return a;throw new SyntaxError('String "'+n+'" is no valid complex number');case 0:this.re=0,this.im=0;break;default:throw new SyntaxError("Wrong number of arguments in Complex constructor ("+arguments.length+" provided, 0, 1, or 2 expected)")}}function t(e){return e instanceof Number||"number"==typeof e}function n(e){return e==Math.round(e)}function r(e){return e instanceof String||"string"==typeof e}function i(e,t){if(this.constructor!=i)throw Error("Unit constructor must be called with the new operator");this.value=1,this.unit=i.UNIT_NONE,this.prefix=i.PREFIX_NONE,this.hasUnit=!1,this.hasValue=!1,this.fixPrefix=!1;var n=arguments.length;if(0==n);else{if(1==n){if(!r(e))throw new TypeError("A string or a number and string expected in Unit constructor");var a=i.parse(e);if(a)return a;throw new SyntaxError('String "'+e+'" is no valid unit')}if(2!=n)throw Error("Too many parameters in Unit constructor, 1 or 2 expected");if(!r(t))throw Error("Second parameter in Unit constructor must be a String");for(var s=i.UNITS,o=!1,f=0,u=s.length;u>f;f++){var h=s[f];if(i.endsWith(t,h.name)){var c=t.length-h.name.length,l=t.substring(0,c),p=h.prefixes[l];if(void 0!==p){this.unit=h,this.prefix=p,this.hasUnit=!0,o=!0;break}}}if(!o)throw Error('String "'+t+'" is no unit');null!=e?(this.value=this._normalize(e),this.hasValue=!0):this.value=this._normalize(1)}}function a(e,t){var n=void 0;if(2==arguments.length){var r=lt(t);n="Function "+e+" does not support a parameter of type "+r}else if(arguments.length>2){for(var i=[],a=1;arguments.length>a;a++)i.push(lt(arguments[a]));n="Function "+e+" does not support a parameters of type "+i.join(", ")}else n="Unsupported parameter in function "+e;return new TypeError(n)}function s(e,t,n,r){var i="Wrong number of arguments in function "+e+" ("+t+" provided, "+n+(void 0!=r?"-"+r:"")+" expected)";return new SyntaxError(i)}function o(n){if(1!=arguments.length)throw s("abs",arguments.length,1);if(t(n))return Math.abs(n);if(n instanceof e)return Math.sqrt(n.re*n.re+n.im*n.im);if(n instanceof Array)return Mt.map(n,o);throw a("abs",n)}function f(n,o){if(2!=arguments.length)throw s("add",arguments.length,2);if(t(n)){if(t(o))return n+o;if(o instanceof e)return new e(n+o.re,o.im)}else if(n instanceof e){if(t(o))return new e(n.re+o,n.im);if(o instanceof e)return new e(n.re+o.re,n.im+o.im)}else if(n instanceof i&&o instanceof i){if(!n.equalBase(o))throw Error("Units do not match");if(!n.hasValue)throw Error("Unit on left hand side of operator + has no value");if(!o.hasValue)throw Error("Unit on right hand side of operator + has no value");var u=n.copy();return u.value+=o.value,u.fixPrefix=!1,u}if(r(n)||r(o))return n+o;if(n instanceof Array||o instanceof Array)return Mt.map2(n,o,f);throw a("add",n,o)}function u(n){if(1!=arguments.length)throw s("ceil",arguments.length,1);if(t(n))return Math.ceil(n);if(n instanceof e)return new e(Math.ceil(n.re),Math.ceil(n.im));if(n instanceof Array)return Mt.map(n,u);throw a("ceil",n)}function h(n){if(1!=arguments.length)throw s("cube",arguments.length,1);if(t(n))return n*n*n;if(n instanceof e)return N(N(n,n),n);if(n instanceof Array)return N(N(n,n),n);throw a("cube",n)}function c(n,r){if(2!=arguments.length)throw s("divide",arguments.length,2);if(t(n)){if(t(r))return n/r;if(r instanceof e)return l(new e(n,0),r)}if(n instanceof e){if(t(r))return l(n,new e(r,0));if(r instanceof e)return l(n,r)}if(n instanceof i&&t(r)){var o=n.copy();return o.value/=r,o}if(n instanceof Array&&!(r instanceof Array))return Mt.map2(n,r,c);throw a("divide",n,r)}function l(t,n){var r=n.re*n.re+n.im*n.im;return new e((t.re*n.re+t.im*n.im)/r,(t.im*n.re-t.re*n.im)/r)}function p(n,o){if(2!=arguments.length)throw s("equal",arguments.length,2);if(t(n)){if(t(o))return n==o;if(o instanceof e)return n==o.re&&0==o.im}if(n instanceof e){if(t(o))return n.re==o&&0==n.im;if(o instanceof e)return n.re==o.re&&n.im==o.im}if(n instanceof i&&o instanceof i){if(!n.equalBase(o))throw Error("Cannot compare units with different base");return n.value==o.value}if(r(n)||r(o))return n==o;if(n instanceof Array||o instanceof Array)return Mt.map2(n,o,p);throw a("equal",n,o)}function m(n){if(1!=arguments.length)throw s("exp",arguments.length,1);if(t(n))return Math.exp(n);if(n instanceof e){var r=Math.exp(n.re);return new e(r*Math.cos(n.im),r*Math.sin(n.im))}if(n instanceof Array)return Mt.map(n,m);throw a("exp",n)}function d(n){if(1!=arguments.length)throw s("fix",arguments.length,1);if(t(n))return value>0?Math.floor(n):Math.ceil(n);if(n instanceof e)return new e(n.re>0?Math.floor(n.re):Math.ceil(n.re),n.im>0?Math.floor(n.im):Math.ceil(n.im));if(n instanceof Array)return Mt.map(n,d);throw a("fix",n)}function g(n){if(1!=arguments.length)throw s("floor",arguments.length,1);if(t(n))return Math.floor(n);if(n instanceof e)return new e(Math.floor(n.re),Math.floor(n.im));if(n instanceof Array)return Mt.map(n,g);throw a("floor",n)}function v(n,f){if(2!=arguments.length)throw s("larger",arguments.length,2);if(t(n)){if(t(f))return n>f;if(f instanceof e)return n>o(f)}if(n instanceof e){if(t(f))return o(n)>f;if(f instanceof e)return o(n)>o(f)}if(n instanceof i&&f instanceof i){if(!n.equalBase(f))throw Error("Cannot compare units with different base");return n.value>f.value}if(r(n)||r(f))return n>f;if(n instanceof Array||f instanceof Array)return Mt.map2(n,f,p);throw a("larger",n,f)}function y(n,f){if(2!=arguments.length)throw s("largereq",arguments.length,2);if(t(n)){if(t(f))return n>=f;if(f instanceof e)return n>=o(f)}if(n instanceof e){if(t(f))return o(n)>=f;if(f instanceof e)return o(n)>=o(f)}if(n instanceof i&&f instanceof i){if(!n.equalBase(f))throw Error("Cannot compare units with different base");return n.value>=f.value}if(r(n)||r(f))return n>=f;if(n instanceof Array||f instanceof Array)return Mt.map2(n,f,y);throw a("largereq",n,f)}function x(n,r){if(1!=arguments.length&&2!=arguments.length)throw s("log",arguments.length,1,2);if(void 0!==r)return c(x(n),x(r));if(t(n))return n>=0?Math.log(n):x(new e(n,0));if(n instanceof e)return new e(Math.log(Math.sqrt(n.re*n.re+n.im*n.im)),Math.atan2(n.im,n.re));if(n instanceof Array)return Mt.map(n,x);throw a("log",n,r)}function w(n){if(1!=arguments.length)throw s("log10",arguments.length,1);if(t(n))return n>=0?Math.log(n)/Math.LN10:w(new e(n,0));if(n instanceof e)return new e(Math.log(Math.sqrt(n.re*n.re+n.im*n.im))/Math.LN10,Math.atan2(n.im,n.re)/Math.LN10);if(n instanceof Array)return Mt.map(n,w);throw a("log10",n)}function E(n,r){if(2!=arguments.length)throw s("mod",arguments.length,2);if(t(n)){if(t(r))return n%r;if(r instanceof e&&0==r.im)return n%r.re}else if(n instanceof e&&0==n.im){if(t(r))return n.re%r;if(r instanceof e&&0==r.im)return n.re%r.re}if(n instanceof Array||r instanceof Array)return Mt.map2(n,r,E);throw a("mod",n,r)}function N(n,r){if(2!=arguments.length)throw s("multiply",arguments.length,2);if(t(n)){if(t(r))return n*r;if(r instanceof e)return b(new e(n,0),r);if(r instanceof i)return h=r.copy(),h.value*=n,h}else if(n instanceof e){if(t(r))return b(n,new e(r,0));if(r instanceof e)return b(n,r)}else if(n instanceof i){if(t(r))return h=n.copy(),h.value*=r,h}else if(n instanceof Array){if(r instanceof Array){var o=Y(n)[0],u=Y(r)[0];if(2!=o.length)throw Error("Can only multiply a 2 dimensional matrix (A has "+o.length+" dimensions)");if(2!=u.length)throw Error("Can only multiply a 2 dimensional matrix (B has "+u.length+" dimensions)");if(o[1]!=u[0])throw Error("Dimensions mismatch in multiplication. Columns of A must match rows of B (A is "+o[0]+"x"+o[1]+", B is "+u[0]+"x"+u[1]+", "+u[1]+" != "+u[0]+")");for(var h=[],c=o[0],l=u[1],p=o[1],m=0;c>m;m++){h[m]=[];for(var d=0;l>d;d++){for(var g=null,v=0;p>v;v++){var y=N(n[m][v],r[v][d]);g=null==g?y:f(g,y)}h[m][d]=g}}return h}return Mt.map2(n,r,N)}if(r instanceof Array)return Mt.map2(n,r,N);throw a("multiply",n,r)}function b(t,n){return new e(t.re*n.re-t.im*n.im,t.re*n.im+t.im*n.re)}function M(r,i){if(2!=arguments.length)throw s("pow",arguments.length,2);if(t(r)){if(t(i))return n(i)||r>=0?Math.pow(r,i):O(new e(r,0),new e(i,0));if(i instanceof e)return O(new e(r,0),i)}else if(r instanceof e){if(t(i))return O(r,new e(i,0));if(i instanceof e)return O(r,i)}else if(r instanceof Array){if(!t(i)||!n(i)||0>i)throw new TypeError("For A^b, b must be a positive integer (value is "+i+")");var o=Y(r)[0];if(2!=o.length)throw Error("For A^b, A must be 2 dimensional (A has "+o.length+" dimensions)");if(o[0]!=o[1])throw Error("For A^b, A must be square (size is "+o[0]+"x"+o[1]+")");if(0==i)return D(o[0]);for(var f=r,u=1;i>u;u++)f=N(r,f);return f}throw a("pow",r,i)}function O(e,t){var n=x(e),r=N(n,t);return m(r)}function T(n,r){if(1!=arguments.length&&2!=arguments.length)throw s("round",arguments.length,1,2);if(void 0==r){if(t(n))return Math.round(n);if(n instanceof e)return new e(Math.round(n.re),Math.round(n.im));throw n instanceof Array&&Mt.map(n,T),a("round",n)}if(!t(r))throw new TypeError("Number of digits in function round must be an integer");if(r!==Math.round(r))throw new TypeError("Number of digits in function round must be integer");if(0>r||r>9)throw Error("Number of digits in function round must be in te range of 0-9");if(t(n))return S(n,r);if(n instanceof e)return new e(S(n.re,r),S(n.im,r));if(n instanceof Array||r instanceof Array)return Mt.map2(n,r,T);throw a("round",n,r)}function S(e,t){var n=Math.pow(10,void 0!=t?t:bt.options.precision);return Math.round(e*n)/n}function A(n){if(1!=arguments.length)throw s("sign",arguments.length,1);if(t(n)){var r;return r=n>0?1:0>n?-1:0}if(n instanceof e){var i=Math.sqrt(n.re*n.re+n.im*n.im);return new e(n.re/i,n.im/i)}if(n instanceof Array)return Mt.map(n,r);throw a("sign",n)}function k(n,f){if(2!=arguments.length)throw s("smaller",arguments.length,2);if(t(n)){if(t(f))return f>n;if(f instanceof e)return o(f)>n}if(n instanceof e){if(t(f))return f>o(n);if(f instanceof e)return o(n)n;if(n instanceof Array||f instanceof Array)return Mt.map2(n,f,k);throw a("smaller",n,f)}function _(n,f){if(2!=arguments.length)throw s("smallereq",arguments.length,2);if(t(n)){if(t(f))return f>=n;if(f instanceof e)return o(f)>=n}if(n instanceof e){if(t(f))return f>=o(n);if(f instanceof e)return o(n)<=o(f)}if(n instanceof i&&f instanceof i){if(!n.equalBase(f))throw Error("Cannot compare units with different base");return n.value<=f.value}if(r(n)||r(f))return f>=n;if(n instanceof Array||f instanceof Array)return Mt.map2(n,f,_);throw a("smallereq",n,f)}function U(n){if(1!=arguments.length)throw s("sqrt",arguments.length,1);if(t(n))return n>=0?Math.sqrt(n):U(new e(n,0));if(n instanceof e){var r=Math.sqrt(n.re*n.re+n.im*n.im);return n.im>=0?new e(.5*Math.sqrt(2*(r+n.re)),.5*Math.sqrt(2*(r-n.re))):new e(.5*Math.sqrt(2*(r+n.re)),-.5*Math.sqrt(2*(r-n.re)))}if(n instanceof Array)return Mt.map(n,U);throw a("sqrt",n)}function q(n){if(1!=arguments.length)throw s("square",arguments.length,1);if(t(n))return n*n;if(n instanceof e)return N(n,n);if(n instanceof Array)return N(n,n);throw a("square",n)}function L(n,r){if(2!=arguments.length)throw s("subtract",arguments.length,2);if(t(n)){if(t(r))return n-r;if(r instanceof e)return new e(n-r.re,r.im)}else if(n instanceof e){if(t(r))return new e(n.re-r,n.im);if(r instanceof e)return new e(n.re-r.re,n.im-r.im)}else if(n instanceof i&&r instanceof i){if(!n.equalBase(r))throw Error("Units do not match");if(!n.hasValue)throw Error("Unit on left hand side of operator - has no value");if(!r.hasValue)throw Error("Unit on right hand side of operator - has no value");var o=n.copy();return o.value-=r.value,o.fixPrefix=!1,o}if(n instanceof Array||r instanceof Array)return Mt.map2(n,r,L);throw a("subtract",n,r)}function C(n){if(1!=arguments.length)throw s("unaryminus",arguments.length,1);if(t(n))return-n;if(n instanceof e)return new e(-n.re,-n.im);if(n instanceof i){var r=n.copy();return r.value=-n.value,r}if(n instanceof Array)return Mt.map(n,C);throw a("unaryminus",n)}function R(n,o){if(2!=arguments.length)throw s("unequal",arguments.length,2);if(t(n)){if(t(o))return n==o;if(o instanceof e)return n==o.re&&0==o.im}if(n instanceof e){if(t(o))return n.re==o&&0==n.im;if(o instanceof e)return n.re==o.re&&n.im==o.im}if(n instanceof i&&o instanceof i){if(!n.equalBase(o))throw Error("Cannot compare units with different base");return n.value==o.value}if(r(n)||r(o))return n==o;if(n instanceof Array||o instanceof Array)return Mt.map2(n,o,R);throw a("unequal",n,o)}function I(n){if(1!=arguments.length)throw s("arg",arguments.length,1);if(t(n))return Math.atan2(0,n);if(n instanceof e)return Math.atan2(n.im,n.re);if(n instanceof Array)return Mt.map(n,I);throw a("arg",n)}function P(n){if(1!=arguments.length)throw s("conj",arguments.length,1);if(t(n))return n;if(n instanceof e)return new e(n.re,-n.im);if(n instanceof Array)return Mt.map(n,P);throw a("conj",n)}function B(n){if(1!=arguments.length)throw s("im",arguments.length,1);if(t(n))return 0;if(n instanceof e)return n.im;if(n instanceof Array)return Mt.map(n,B);throw a("im",n)}function G(n){if(1!=arguments.length)throw s("re",arguments.length,1);if(t(n))return n;if(n instanceof e)return n.re;if(n instanceof Array)return Mt.map(n,G);throw a("re",n)}function D(e,r){var i,a,o=arguments.length;if(0>o||o>2)throw s("eye",o,0,2);if(0==o)return 1;if(1==o?(i=e,a=e):2==o&&(i=e,a=r),!t(i)||!n(i)||1>i)throw Error("Parameters in function eye must be positive integers");if(a&&(!t(a)||!n(a)||1>a))throw Error("Parameters in function eye must be positive integers");for(var f=[],u=0;i>u;u++){for(var h=[],c=0;a>c;c++)h[c]=0;f[u]=h}for(var l=Math.min(i,a),p=0;l>p;p++)f[p][p]=1;return f}function Y(n){if(1!=arguments.length)throw s("size",arguments.length,1);if(t(n))return[[1,1]];if(n instanceof e)return[[1,1]];if(n instanceof i)return[[1,1]];if(r(n))return[[1,n.length]];if(n instanceof Array){var o=V(n);return F(n,o),[V(n)]}throw a("size",n)}function V(e){if(e instanceof Array){var t=e.length;if(t){var n=V(e[0]);return[t].concat(n)}return[t]}return[]}function F(e,t,n){var r,i=e.length;if(n||(n=0),i!=t[n])throw Error("Dimension mismatch ("+i+" != "+t[n]+")");if(t.length-1>n){var a=n+1;for(r=0;i>r;r++){var s=e[r];if(!(s instanceof Array))throw Error("Dimension mismatch ("+(t.length-1)+" < "+t.length+")");F(e[r],t,a)}}else for(r=0;i>r;r++)if(e[r]instanceof Array)throw Error("Dimension mismatch ("+(t.length+1)+" > "+t.length+")");return!0}function z(e){if(1!=arguments.length)throw s("factorial",arguments.length,1);if(t(e)){if(!n(e))throw new TypeError("Function factorial can only handle integer values");var r=e,i=r;for(r--;r>1;)i*=r,r--;return 0==i&&(i=1),i}if(e instanceof Array)return Mt.map(e,z);throw a("factorial",e)}function j(){if(0!=arguments.length)throw s("random",arguments.length,0);return Math.random()}function H(){if(0==arguments.length)throw Error("Function sum requires one or more parameters (0 provided)");if(1==arguments.length&&arguments[0]instanceof Array)return H.apply(this,arguments[0]);for(var e=arguments[0],t=1,n=arguments.length;n>t;t++){var r=arguments[t];v(r,e)&&(e=r)}return e}function K(){if(0==arguments.length)throw Error("Function sum requires one or more parameters (0 provided)");if(1==arguments.length&&arguments[0]instanceof Array)return K.apply(this,arguments[0]);for(var e=arguments[0],t=1,n=arguments.length;n>t;t++){var r=arguments[t];k(r,e)&&(e=r)}return e}function W(n){if(1!=arguments.length)throw s("acos",arguments.length,1);if(t(n))return n>=-1&&1>=n?Math.acos(n):W(new e(n,0));if(n instanceof e){var r=new e(n.im*n.im-n.re*n.re+1,-2*n.re*n.im),i=U(r),o=new e(i.re-n.im,i.im+n.re),f=x(o);return new e(1.5707963267948966-f.im,f.re)}if(n instanceof Array)return Mt.map(n,W);throw a("acos",n)}function X(n){if(1!=arguments.length)throw s("asin",arguments.length,1);if(t(n))return n>=-1&&1>=n?Math.asin(n):X(new e(n,0));if(n instanceof e){var r=n.re,i=n.im,o=new e(i*i-r*r+1,-2*r*i),f=U(o),u=new e(f.re-i,f.im+r),h=x(u);return new e(h.im,-h.re)}if(n instanceof Array)return Mt.map(n,X);throw a("asin",n)}function Z(n){if(1!=arguments.length)throw s("atan",arguments.length,1);if(t(n))return Math.atan(n);if(n instanceof e){var r=n.re,i=n.im,o=r*r+(1-i)*(1-i),f=new e((1-i*i-r*r)/o,-2*r/o),u=x(f);return new e(-.5*u.im,.5*u.re)}if(n instanceof Array)return Mt.map(n,Z);throw a("atan",n)}function Q(n,r){if(2!=arguments.length)throw s("atan2",arguments.length,2);if(t(n)){if(t(r))return Math.atan2(n,r);if(r instanceof e)return Math.atan2(n,r.re)}else if(n instanceof e){if(t(r))return Math.atan2(n.re,r);if(r instanceof e)return Math.atan2(n.re,r.re)}if(r instanceof Array||n instanceof Array)return Mt.map2(n,r,Q);throw a("atan2",n,r)}function J(n){if(1!=arguments.length)throw s("cos",arguments.length,1);if(t(n))return Math.cos(n);if(n instanceof e)return new e(.5*Math.cos(n.re)*(Math.exp(-n.im)+Math.exp(n.im)),.5*Math.sin(n.re)*(Math.exp(-n.im)-Math.exp(n.im)));if(n instanceof i){if(!n.hasBase(i.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.cos(n.value)}if(n instanceof Array)return Mt.map(n,J);throw a("cos",n)}function $(n){if(1!=arguments.length)throw s("cot",arguments.length,1);if(t(n))return 1/Math.tan(n);if(n instanceof e){var r=Math.exp(-4*n.im)-2*Math.exp(-2*n.im)*Math.cos(2*n.re)+1;return new e(2*Math.exp(-2*n.im)*Math.sin(2*n.re)/r,(Math.exp(-4*n.im)-1)/r)}if(n instanceof i){if(!n.hasBase(i.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cot is no angle");return 1/Math.tan(n.value)}if(n instanceof Array)return Mt.map(n,$);throw a("cot",n)}function et(n){if(1!=arguments.length)throw s("csc",arguments.length,1);if(t(n))return 1/Math.sin(n);if(n instanceof e){var r=.25*(Math.exp(-2*n.im)+Math.exp(2*n.im))-.5*Math.cos(2*n.re);return new e(.5*Math.sin(n.re)*(Math.exp(-n.im)+Math.exp(n.im))/r,.5*Math.cos(n.re)*(Math.exp(-n.im)-Math.exp(n.im))/r)}if(n instanceof i){if(!n.hasBase(i.BASE_UNITS.ANGLE))throw new TypeError("Unit in function csc is no angle");return 1/Math.sin(n.value)}if(n instanceof Array)return Mt.map(n,et);throw a("csc",n)}function tt(n){if(1!=arguments.length)throw s("sec",arguments.length,1);if(t(n))return 1/Math.cos(n);if(n instanceof e){var r=.25*(Math.exp(-2*n.im)+Math.exp(2*n.im))+.5*Math.cos(2*n.re);return new e(.5*Math.cos(n.re)*(Math.exp(-n.im)+Math.exp(n.im))/r,.5*Math.sin(n.re)*(Math.exp(n.im)-Math.exp(-n.im))/r)}if(n instanceof i){if(!n.hasBase(i.BASE_UNITS.ANGLE))throw new TypeError("Unit in function sec is no angle");return 1/Math.cos(n.value)}if(n instanceof Array)return Mt.map(n,tt);throw a("sec",n)}function nt(n){if(1!=arguments.length)throw s("sin",arguments.length,1);if(t(n))return Math.sin(n);if(n instanceof e)return new e(.5*Math.sin(n.re)*(Math.exp(-n.im)+Math.exp(n.im)),.5*Math.cos(n.re)*(Math.exp(n.im)-Math.exp(-n.im)));if(n instanceof i){if(!n.hasBase(i.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.sin(n.value)}if(n instanceof Array)return Mt.map(n,nt);throw a("sin",n)}function rt(n){if(1!=arguments.length)throw s("tan",arguments.length,1);if(t(n))return Math.tan(n);if(n instanceof e){var r=Math.exp(-4*n.im)+2*Math.exp(-2*n.im)*Math.cos(2*n.re)+1;return new e(2*Math.exp(-2*n.im)*Math.sin(2*n.re)/r,(1-Math.exp(-4*n.im))/r)}if(n instanceof i){if(!n.hasBase(i.BASE_UNITS.ANGLE))throw new TypeError("Unit in function tan is no angle");return Math.tan(n.value)}if(n instanceof Array)return Mt.map(n,rt);throw a("tan",n)}function it(e,t){if(2!=arguments.length)throw s("in",arguments.length,2);if(e instanceof i&&t instanceof i){if(t.hasValue)throw Error("Cannot convert to a unit with a value");if(!t.hasUnit)throw Error("Unit expected on the right hand side of function in");var n=t.copy();return n.value=e.value,n.fixPrefix=!0,n}if(e instanceof Array||t instanceof Array)return Mt.map2(e,t,it);throw a("in",e)}function at(e,n){var i=arguments.length;if(1!=i&&2!=i)throw s("format",i,1,2);if(1==i){var a=arguments[0];return t(a)?Mt.format(a):a instanceof Array?st(a):r(a)?'"'+a+'"':a instanceof Object?""+a:a+""}if(!r(e))throw new TypeError("String expected as first parameter in function format");if(!(n instanceof Object))throw new TypeError("Object expected as first parameter in function format");return e.replace(/\$([\w\.]+)/g,function(e,t){for(var r=t.split("."),i=n[r.shift()];r.length&&void 0!=i;){var a=r.shift();i=a?i[a]:i+"."}return void 0!=i?i:e})}function st(e){var t="[",n=Y(e)[0];if(2!=n.length)return ot(e);for(var r=n[0],i=n[1],a=0;r>a;a++){0!=a&&(t+="; ");for(var s=e[a],o=0;i>o;o++){0!=o&&(t+=", ");var f=s[o];void 0!=f&&(t+=at(f))}}return t+="]"}function ot(e){if(e instanceof Array){for(var t="[",n=e.length,r=0;n>r;r++)0!=r&&(t+=", "),t+=ot(e[r]);return t+="]"}return at(e)}function ft(e){if(1!=arguments.length)throw s("help",arguments.length,1);if(void 0!=e){if(e.doc)return ut(e.doc);if(e.constructor.doc)return ut(e.constructor.doc);if(r(e)){var t=bt[e];if(t&&t.doc)return ut(t.doc)}}return e instanceof Object&&e.name?'No documentation found on subject "'+e.name+'"':e instanceof Object&&e.constructor.name?'No documentation found on subject "'+e.constructor.name+'"':'No documentation found on subject "'+e+'"'}function ut(e){var t="";if(e.name&&(t+="NAME\n"+e.name+"\n\n"),e.category&&(t+="CATEGORY\n"+e.category+"\n\n"),e.syntax&&(t+="SYNTAX\n"+e.syntax.join("\n")+"\n\n"),e.examples){var n=new bt.parser.Parser;t+="EXAMPLES\n";for(var r=0;e.examples.length>r;r++){var i,a=e.examples[r];try{i=n.eval(a)}catch(s){i=s}t+=a+"\n",t+=" "+bt.format(i)+"\n"}t+="\n"}return e.seealso&&(t+="SEE ALSO\n"+e.seealso.join(", ")+"\n"),t}function ht(e,t){var n;if(r(e)){if("undefined"==typeof require)throw Error("Cannot load file: require not available.");var i=require(e);ht(i)}else if(ct(e)){if(n=e.name,!n)throw Error("Cannot import an unnamed function");(t||void 0===bt[n])&&(bt[n]=e)}else if(e instanceof Object)for(n in e)if(e.hasOwnProperty(n)){var a=e[n];ct(a)?(t||void 0===bt[n])&&(bt[n]=a):ht(a)}}function ct(n){return"function"==typeof n||t(n)||r(n)||n instanceof e||n instanceof i}function lt(e){if(1!=arguments.length)throw s("typeof",arguments.length,1);var t=typeof e;if("object"==t){if(null==e)return"null";if(e.constructor){for(var n in bt)if(bt.hasOwnProperty(n)&&e.constructor==bt[n])return n.toLowerCase();if(e.constructor.name)return e.constructor.name.toLowerCase()}}return t}function pt(){}function mt(e,t,n){this.name=e,this.fn=t,this.params=n}function dt(e){this.value=e}function gt(e){this.nodes=e||[]}function vt(){this.params=[],this.visible=[]}function yt(e,t,n,r){this.name=e,this.params=t,this.expr=n,this.result=r}function xt(e,t,n,r,i){this.name=e,this.variables=n,this.values=[];for(var a=0,s=this.variables.length;s>a;a++)this.values[a]=function(){var e=function(){return e.value};return e.value=void 0,e}();this.def=this.createFunction(e,t,n,r),this.result=i}function wt(e){this.parentScope=e,this.nestedScopes=void 0,this.symbols={},this.defs={},this.updates={},this.links={}}function Et(){if(this.constructor!=Et)throw new SyntaxError("Parser constructor must be called with the new operator");this.TOKENTYPE={NULL:0,DELIMITER:1,NUMBER:2,SYMBOL:3,UNKNOWN:4},this.expr="",this.index=0,this.c="",this.token="",this.token_type=this.TOKENTYPE.NULL,this.scope=new wt}function Nt(){this.idMax=-1,this.updateSeq=0,this.parser=new Et,this.scope=new wt,this.nodes={},this.firstNode=void 0,this.lastNode=void 0}var bt={parser:{node:{}},options:{precision:10}};"undefined"!=typeof module&&module.exports!==void 0&&(module.exports=bt),"undefined"!=typeof exports&&(exports=bt),"undefined"!=typeof require&&"undefined"!=typeof define&&define(function(){return bt}),"undefined"!=typeof window&&(window.math=bt);var Mt={};Mt.format=function at(e,t){if(1/0===e)return"Infinity";if(e===-1/0)return"-Infinity";if(0/0===e)return"NaN";var n=Math.abs(e);if(n>1e-4&&1e6>n||0==n)return S(e,t)+"";var r=Math.round(Math.log(n)/Math.LN10),i=e/Math.pow(10,r);return S(i,t)+"E"+r},Mt.randomUUID=function(){var e=function(){return Math.floor(65536*Math.random()).toString(16)};return e()+e()+"-"+e()+"-"+e()+"-"+e()+"-"+e()+e()+e()},Mt.map=function(e,t){if(!e instanceof Array)throw new TypeError("Array expected");return e.map(function(e){return t(e)})},Mt.map2=function(e,t,n){var r,i,a;if(e instanceof Array)if(t instanceof Array){if(e.length!=t.length)throw Error("Dimension mismatch ("+e.length+" != "+t.length+")");for(r=[],i=e.length,a=0;i>a;a++)r[a]=n(e[a],t[a])}else for(r=[],i=e.length,a=0;i>a;a++)r[a]=n(e[a],t);else if(t instanceof Array)for(r=[],i=t.length,a=0;i>a;a++)r[a]=n(e,t[a]);else r=n(e,t);return r},Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var t=0;this.length>t;t++)if(this[t]==e)return t;return-1}),Array.prototype.forEach||(Array.prototype.forEach=function(e,t){for(var n=0,r=this.length;r>n;++n)e.call(t||this,this[n],n,this)}),Array.prototype.map||(Array.prototype.map=function(e,t){var n,r,i;if(null==this)throw new TypeError(" this is null or not defined");var a=Object(this),s=a.length>>>0;if("function"!=typeof e)throw new TypeError(e+" is not a function");for(t&&(n=t),r=Array(s),i=0;s>i;){var o,f;i in a&&(o=a[i],f=e.call(n,o,i,a),r[i]=f),i++}return r}),bt.Complex=e,function(){function t(){for(;" "==c||" "==c;)a()}function n(e){return e>="0"&&"9">=e||"."==e}function i(e){return e>="0"&&"9">=e}function a(){h++,c=u[h]}function s(e){h=e,c=u[h]}function o(){var e="",t=h;if("+"==c?a():"-"==c&&(e+=c,a()),!n(c))return s(t),null;for(;n(c);)e+=c,a();if("E"==c||"e"==c){if(e+=c,a(),("+"==c||"-"==c)&&(e+=c,a()),!i(c))return s(t),null;for(;i(c);)e+=c,a()}return e}function f(){var e=u[h+1];if("I"==c||"i"==c)return a(),"1";if(!("+"!=c&&"-"!=c||"I"!=e&&"i"!=e)){var t="+"==c?"1":"-1";return a(),a(),t}return null}var u,h,c;e.parse=function(n){if(u=n,h=-1,c="",!r(u))return null;a(),t();var i=o();if(i){if("I"==c||"i"==c)return a(),t(),c?null:new e(0,Number(i));t();var s=c;if("+"!=s&&"-"!=s)return t(),c?null:new e(Number(i),0);a(),t();var l=o();if(l){if("I"!=c&&"i"!=c)return null;a()}else if(l=f(),!l)return null;return"-"==s&&(l="-"==l[0]?"+"+l.substring(1):"-"+l),a(),t(),c?null:new e(Number(i),Number(l))}return(i=f())?(t(),c?null:new e(0,Number(i))):null}}(),e.prototype.copy=function(){return new e(this.re,this.im)},e.prototype.toString=function(){var e="";return e=0==this.im?Mt.format(this.re):0==this.re?1==this.im?"i":-1==this.im?"-i":Mt.format(this.im)+"i":this.im>0?1==this.im?Mt.format(this.re)+" + i":Mt.format(this.re)+" + "+Mt.format(this.im)+"i":-1==this.im?Mt.format(this.re)+" - i":Mt.format(this.re)+" - "+Mt.format(Math.abs(this.im))+"i"},e.doc={name:"Complex",category:"type",syntax:["a + bi","a + b * i"],description:"A complex value a + bi, where a is the real part and b is the complex part, and i is the imaginary number defined as sqrt(-1).",examples:["2 + 3i","sqrt(-4)","(1.2 -5i) * 2"],seealso:["abs","arg","conj","im","re"]},bt.Unit=i,function(){function e(){for(;" "==c||" "==c;)a()}function t(e){return e>="0"&&"9">=e||"."==e}function n(e){return e>="0"&&"9">=e}function a(){h++,c=u[h]}function s(e){h=e,c=u[h]}function o(){var e="",r=h;if("+"==c?a():"-"==c&&(e+=c,a()),!t(c))return s(r),null;for(;t(c);)e+=c,a();if("E"==c||"e"==c){if(e+=c,a(),("+"==c||"-"==c)&&(e+=c,a()),!n(c))return s(r),null;for(;n(c);)e+=c,a()}return e}function f(){var t="";for(e();c&&" "!=c&&" "!=c;)t+=c,a();return t||null}var u,h,c;i.parse=function(t){if(u=t,h=-1,c="",!r(u))return null;a(),e();var n,s=o();return s?(n=f(),a(),e(),c?null:s&&n?new i(Number(s),n):null):(n=f(),a(),e(),c?null:new i(null,n))}}(),i.prototype.copy=function(){var e=new i;for(var t in this)this.hasOwnProperty(t)&&(e[t]=this[t]);return e},i.endsWith=function(e,t){var n=e.length-t.length,r=e.length;return e.substring(n,r)===t},i.prototype._normalize=function(e){return(e+this.unit.offset)*this.unit.value*this.prefix.value},i.prototype._unnormalize=function(e,t){return void 0===t?e/this.unit.value/this.prefix.value-this.unit.offset:e/this.unit.value/t-this.unit.offset},i.isUnit=function(e){for(var t=i.UNITS,n=t.length,r=0;n>r;r++){var a=t[r];if(i.endsWith(e,a.name)){var s=e.length-a.name.length;if(0==s)return!0;var o=e.substring(0,s),f=a.prefixes[o];if(void 0!==f)return!0}}return!1},i.prototype.hasBase=function(e){return void 0===this.unit.base?void 0===e:this.unit.base===e},i.prototype.equalBase=function(e){return this.unit.base===e.unit.base},i.prototype.equals=function(e){return this.equalBase(e)&&this.value==e.value},i.prototype.toString=function(){var e;if(this.fixPrefix)return e=this._unnormalize(this.value),Mt.format(e)+" "+this.prefix.name+this.unit.name;var t=Math.abs(this.value/this.unit.value),n=i.PREFIX_NONE,r=Math.abs(Math.log(t/n.value)/Math.LN10-1.2),a=this.unit.prefixes;for(var s in a)if(a.hasOwnProperty(s)){var o=a[s];if(o.scientific){var f=Math.abs(Math.log(t/o.value)/Math.LN10-1.2);r>f&&(n=o,r=f)}}return e=this._unnormalize(this.value,n.value),Mt.format(e)+" "+n.name+this.unit.name},i.PREFIXES={NONE:{"":{name:"",value:1,scientific:!0}},SHORT:{"":{name:"",value:1,scientific:!0},da:{name:"da",value:10,scientific:!1},h:{name:"h",value:100,scientific:!1},k:{name:"k",value:1e3,scientific:!0},M:{name:"M",value:1e6,scientific:!0},G:{name:"G",value:1e9,scientific:!0},T:{name:"T",value:1e12,scientific:!0},P:{name:"P",value:1e15,scientific:!0},E:{name:"E",value:1e18,scientific:!0},Z:{name:"Z",value:1e21,scientific:!0},Y:{name:"Y",value:1e24,scientific:!0},d:{name:"d",value:.1,scientific:!1},c:{name:"c",value:.01,scientific:!1},m:{name:"m",value:.001,scientific:!0},u:{name:"u",value:1e-6,scientific:!0},n:{name:"n",value:1e-9,scientific:!0},p:{name:"p",value:1e-12,scientific:!0},f:{name:"f",value:1e-15,scientific:!0},a:{name:"a",value:1e-18,scientific:!0},z:{name:"z",value:1e-21,scientific:!0},y:{name:"y",value:1e-24,scientific:!0}},LONG:{"":{name:"",value:1,scientific:!0},deca:{name:"deca",value:10,scientific:!1},hecto:{name:"hecto",value:100,scientific:!1},kilo:{name:"kilo",value:1e3,scientific:!0},mega:{name:"mega",value:1e6,scientific:!0},giga:{name:"giga",value:1e9,scientific:!0},tera:{name:"tera",value:1e12,scientific:!0},peta:{name:"peta",value:1e15,scientific:!0},exa:{name:"exa",value:1e18,scientific:!0},zetta:{name:"zetta",value:1e21,scientific:!0},yotta:{name:"yotta",value:1e24,scientific:!0},deci:{name:"deci",value:.1,scientific:!1},centi:{name:"centi",value:.01,scientific:!1},milli:{name:"milli",value:.001,scientific:!0},micro:{name:"micro",value:1e-6,scientific:!0},nano:{name:"nano",value:1e-9,scientific:!0},pico:{name:"pico",value:1e-12,scientific:!0},femto:{name:"femto",value:1e-15,scientific:!0},atto:{name:"atto",value:1e-18,scientific:!0},zepto:{name:"zepto",value:1e-21,scientific:!0},yocto:{name:"yocto",value:1e-24,scientific:!0}},BINARY_SHORT:{"":{name:"",value:1,scientific:!0},k:{name:"k",value:1024,scientific:!0},M:{name:"M",value:Math.pow(1024,2),scientific:!0},G:{name:"G",value:Math.pow(1024,3),scientific:!0},T:{name:"T",value:Math.pow(1024,4),scientific:!0},P:{name:"P",value:Math.pow(1024,5),scientific:!0},E:{name:"E",value:Math.pow(1024,6),scientific:!0},Z:{name:"Z",value:Math.pow(1024,7),scientific:!0},Y:{name:"Y",value:Math.pow(1024,8),scientific:!0},Ki:{name:"Ki",value:1024,scientific:!0},Mi:{name:"Mi",value:Math.pow(1024,2),scientific:!0},Gi:{name:"Gi",value:Math.pow(1024,3),scientific:!0},Ti:{name:"Ti",value:Math.pow(1024,4),scientific:!0},Pi:{name:"Pi",value:Math.pow(1024,5),scientific:!0},Ei:{name:"Ei",value:Math.pow(1024,6),scientific:!0},Zi:{name:"Zi",value:Math.pow(1024,7),scientific:!0},Yi:{name:"Yi",value:Math.pow(1024,8),scientific:!0}},BINARY_LONG:{"":{name:"",value:1,scientific:!0},kilo:{name:"kilo",value:1024,scientific:!0},mega:{name:"mega",value:Math.pow(1024,2),scientific:!0},giga:{name:"giga",value:Math.pow(1024,3),scientific:!0},tera:{name:"tera",value:Math.pow(1024,4),scientific:!0},peta:{name:"peta",value:Math.pow(1024,5),scientific:!0},exa:{name:"exa",value:Math.pow(1024,6),scientific:!0},zetta:{name:"zetta",value:Math.pow(1024,7),scientific:!0},yotta:{name:"yotta",value:Math.pow(1024,8),scientific:!0},kibi:{name:"kibi",value:1024,scientific:!0},mebi:{name:"mebi",value:Math.pow(1024,2),scientific:!0},gibi:{name:"gibi",value:Math.pow(1024,3),scientific:!0},tebi:{name:"tebi",value:Math.pow(1024,4),scientific:!0},pebi:{name:"pebi",value:Math.pow(1024,5),scientific:!0},exi:{name:"exi",value:Math.pow(1024,6),scientific:!0},zebi:{name:"zebi",value:Math.pow(1024,7),scientific:!0},yobi:{name:"yobi",value:Math.pow(1024,8),scientific:!0}}},i.PREFIX_NONE={name:"",value:1,scientific:!0},i.BASE_UNITS={NONE:{},LENGTH:{},MASS:{},TIME:{},CURRENT:{},TEMPERATURE:{},LUMINOUS_INTENSITY:{},AMOUNT_OF_SUBSTANCE:{},FORCE:{},SURFACE:{},VOLUME:{},ANGLE:{},BIT:{}}; -var Ot=i.BASE_UNITS,Tt=i.PREFIXES;i.BASE_UNIT_NONE={},i.UNIT_NONE={name:"",base:i.BASE_UNIT_NONE,value:1,offset:0},i.UNITS=[{name:"meter",base:Ot.LENGTH,prefixes:Tt.LONG,value:1,offset:0},{name:"inch",base:Ot.LENGTH,prefixes:Tt.NONE,value:.0254,offset:0},{name:"foot",base:Ot.LENGTH,prefixes:Tt.NONE,value:.3048,offset:0},{name:"yard",base:Ot.LENGTH,prefixes:Tt.NONE,value:.9144,offset:0},{name:"mile",base:Ot.LENGTH,prefixes:Tt.NONE,value:1609.344,offset:0},{name:"link",base:Ot.LENGTH,prefixes:Tt.NONE,value:.201168,offset:0},{name:"rod",base:Ot.LENGTH,prefixes:Tt.NONE,value:5.02921,offset:0},{name:"chain",base:Ot.LENGTH,prefixes:Tt.NONE,value:20.1168,offset:0},{name:"angstrom",base:Ot.LENGTH,prefixes:Tt.NONE,value:1e-10,offset:0},{name:"m",base:Ot.LENGTH,prefixes:Tt.SHORT,value:1,offset:0},{name:"ft",base:Ot.LENGTH,prefixes:Tt.NONE,value:.3048,offset:0},{name:"yd",base:Ot.LENGTH,prefixes:Tt.NONE,value:.9144,offset:0},{name:"mi",base:Ot.LENGTH,prefixes:Tt.NONE,value:1609.344,offset:0},{name:"li",base:Ot.LENGTH,prefixes:Tt.NONE,value:.201168,offset:0},{name:"rd",base:Ot.LENGTH,prefixes:Tt.NONE,value:5.02921,offset:0},{name:"ch",base:Ot.LENGTH,prefixes:Tt.NONE,value:20.1168,offset:0},{name:"mil",base:Ot.LENGTH,prefixes:Tt.NONE,value:254e-7,offset:0},{name:"m2",base:Ot.SURFACE,prefixes:Tt.SHORT,value:1,offset:0},{name:"sqin",base:Ot.SURFACE,prefixes:Tt.NONE,value:64516e-8,offset:0},{name:"sqft",base:Ot.SURFACE,prefixes:Tt.NONE,value:.09290304,offset:0},{name:"sqyd",base:Ot.SURFACE,prefixes:Tt.NONE,value:.83612736,offset:0},{name:"sqmi",base:Ot.SURFACE,prefixes:Tt.NONE,value:2589988.110336,offset:0},{name:"sqrd",base:Ot.SURFACE,prefixes:Tt.NONE,value:25.29295,offset:0},{name:"sqch",base:Ot.SURFACE,prefixes:Tt.NONE,value:404.6873,offset:0},{name:"sqmil",base:Ot.SURFACE,prefixes:Tt.NONE,value:6.4516e-10,offset:0},{name:"m3",base:Ot.VOLUME,prefixes:Tt.SHORT,value:1,offset:0},{name:"L",base:Ot.VOLUME,prefixes:Tt.SHORT,value:.001,offset:0},{name:"litre",base:Ot.VOLUME,prefixes:Tt.LONG,value:.001,offset:0},{name:"cuin",base:Ot.VOLUME,prefixes:Tt.NONE,value:16387064e-12,offset:0},{name:"cuft",base:Ot.VOLUME,prefixes:Tt.NONE,value:.028316846592,offset:0},{name:"cuyd",base:Ot.VOLUME,prefixes:Tt.NONE,value:.764554857984,offset:0},{name:"teaspoon",base:Ot.VOLUME,prefixes:Tt.NONE,value:5e-6,offset:0},{name:"tablespoon",base:Ot.VOLUME,prefixes:Tt.NONE,value:15e-6,offset:0},{name:"minim",base:Ot.VOLUME,prefixes:Tt.NONE,value:6.161152e-8,offset:0},{name:"fluiddram",base:Ot.VOLUME,prefixes:Tt.NONE,value:36966911e-13,offset:0},{name:"fluidounce",base:Ot.VOLUME,prefixes:Tt.NONE,value:2957353e-11,offset:0},{name:"gill",base:Ot.VOLUME,prefixes:Tt.NONE,value:.0001182941,offset:0},{name:"cup",base:Ot.VOLUME,prefixes:Tt.NONE,value:.0002365882,offset:0},{name:"pint",base:Ot.VOLUME,prefixes:Tt.NONE,value:.0004731765,offset:0},{name:"quart",base:Ot.VOLUME,prefixes:Tt.NONE,value:.0009463529,offset:0},{name:"gallon",base:Ot.VOLUME,prefixes:Tt.NONE,value:.003785412,offset:0},{name:"beerbarrel",base:Ot.VOLUME,prefixes:Tt.NONE,value:.1173478,offset:0},{name:"oilbarrel",base:Ot.VOLUME,prefixes:Tt.NONE,value:.1589873,offset:0},{name:"hogshead",base:Ot.VOLUME,prefixes:Tt.NONE,value:.238481,offset:0},{name:"fldr",base:Ot.VOLUME,prefixes:Tt.NONE,value:36966911e-13,offset:0},{name:"floz",base:Ot.VOLUME,prefixes:Tt.NONE,value:2957353e-11,offset:0},{name:"gi",base:Ot.VOLUME,prefixes:Tt.NONE,value:.0001182941,offset:0},{name:"cp",base:Ot.VOLUME,prefixes:Tt.NONE,value:.0002365882,offset:0},{name:"pt",base:Ot.VOLUME,prefixes:Tt.NONE,value:.0004731765,offset:0},{name:"qt",base:Ot.VOLUME,prefixes:Tt.NONE,value:.0009463529,offset:0},{name:"gal",base:Ot.VOLUME,prefixes:Tt.NONE,value:.003785412,offset:0},{name:"bbl",base:Ot.VOLUME,prefixes:Tt.NONE,value:.1173478,offset:0},{name:"obl",base:Ot.VOLUME,prefixes:Tt.NONE,value:.1589873,offset:0},{name:"g",base:Ot.MASS,prefixes:Tt.SHORT,value:.001,offset:0},{name:"gram",base:Ot.MASS,prefixes:Tt.LONG,value:.001,offset:0},{name:"ton",base:Ot.MASS,prefixes:Tt.SHORT,value:907.18474,offset:0},{name:"tonne",base:Ot.MASS,prefixes:Tt.SHORT,value:1e3,offset:0},{name:"grain",base:Ot.MASS,prefixes:Tt.NONE,value:6479891e-11,offset:0},{name:"dram",base:Ot.MASS,prefixes:Tt.NONE,value:.0017718451953125,offset:0},{name:"ounce",base:Ot.MASS,prefixes:Tt.NONE,value:.028349523125,offset:0},{name:"poundmass",base:Ot.MASS,prefixes:Tt.NONE,value:.45359237,offset:0},{name:"hundredweight",base:Ot.MASS,prefixes:Tt.NONE,value:45.359237,offset:0},{name:"stick",base:Ot.MASS,prefixes:Tt.NONE,value:.115,offset:0},{name:"gr",base:Ot.MASS,prefixes:Tt.NONE,value:6479891e-11,offset:0},{name:"dr",base:Ot.MASS,prefixes:Tt.NONE,value:.0017718451953125,offset:0},{name:"oz",base:Ot.MASS,prefixes:Tt.NONE,value:.028349523125,offset:0},{name:"lbm",base:Ot.MASS,prefixes:Tt.NONE,value:.45359237,offset:0},{name:"cwt",base:Ot.MASS,prefixes:Tt.NONE,value:45.359237,offset:0},{name:"s",base:Ot.TIME,prefixes:Tt.SHORT,value:1,offset:0},{name:"min",base:Ot.TIME,prefixes:Tt.NONE,value:60,offset:0},{name:"h",base:Ot.TIME,prefixes:Tt.NONE,value:3600,offset:0},{name:"seconds",base:Ot.TIME,prefixes:Tt.LONG,value:1,offset:0},{name:"second",base:Ot.TIME,prefixes:Tt.LONG,value:1,offset:0},{name:"sec",base:Ot.TIME,prefixes:Tt.LONG,value:1,offset:0},{name:"minutes",base:Ot.TIME,prefixes:Tt.NONE,value:60,offset:0},{name:"minute",base:Ot.TIME,prefixes:Tt.NONE,value:60,offset:0},{name:"hours",base:Ot.TIME,prefixes:Tt.NONE,value:3600,offset:0},{name:"hour",base:Ot.TIME,prefixes:Tt.NONE,value:3600,offset:0},{name:"day",base:Ot.TIME,prefixes:Tt.NONE,value:86400,offset:0},{name:"days",base:Ot.TIME,prefixes:Tt.NONE,value:86400,offset:0},{name:"rad",base:Ot.ANGLE,prefixes:Tt.NONE,value:1,offset:0},{name:"deg",base:Ot.ANGLE,prefixes:Tt.NONE,value:.017453292519943295,offset:0},{name:"grad",base:Ot.ANGLE,prefixes:Tt.NONE,value:.015707963267948967,offset:0},{name:"cycle",base:Ot.ANGLE,prefixes:Tt.NONE,value:6.283185307179586,offset:0},{name:"A",base:Ot.CURRENT,prefixes:Tt.SHORT,value:1,offset:0},{name:"ampere",base:Ot.CURRENT,prefixes:Tt.LONG,value:1,offset:0},{name:"K",base:Ot.TEMPERATURE,prefixes:Tt.NONE,value:1,offset:0},{name:"degC",base:Ot.TEMPERATURE,prefixes:Tt.NONE,value:1,offset:273.15},{name:"degF",base:Ot.TEMPERATURE,prefixes:Tt.NONE,value:1/1.8,offset:459.67},{name:"degR",base:Ot.TEMPERATURE,prefixes:Tt.NONE,value:1/1.8,offset:0},{name:"kelvin",base:Ot.TEMPERATURE,prefixes:Tt.NONE,value:1,offset:0},{name:"celsius",base:Ot.TEMPERATURE,prefixes:Tt.NONE,value:1,offset:273.15},{name:"fahrenheit",base:Ot.TEMPERATURE,prefixes:Tt.NONE,value:1/1.8,offset:459.67},{name:"rankine",base:Ot.TEMPERATURE,prefixes:Tt.NONE,value:1/1.8,offset:0},{name:"mol",base:Ot.AMOUNT_OF_SUBSTANCE,prefixes:Tt.NONE,value:1,offset:0},{name:"mole",base:Ot.AMOUNT_OF_SUBSTANCE,prefixes:Tt.NONE,value:1,offset:0},{name:"cd",base:Ot.LUMINOUS_INTENSITY,prefixes:Tt.NONE,value:1,offset:0},{name:"candela",base:Ot.LUMINOUS_INTENSITY,prefixes:Tt.NONE,value:1,offset:0},{name:"N",base:Ot.FORCE,prefixes:Tt.SHORT,value:1,offset:0},{name:"newton",base:Ot.FORCE,prefixes:Tt.LONG,value:1,offset:0},{name:"lbf",base:Ot.FORCE,prefixes:Tt.NONE,value:4.4482216152605,offset:0},{name:"poundforce",base:Ot.FORCE,prefixes:Tt.NONE,value:4.4482216152605,offset:0},{name:"b",base:Ot.BIT,prefixes:Tt.BINARY_SHORT,value:1,offset:0},{name:"bits",base:Ot.BIT,prefixes:Tt.BINARY_LONG,value:1,offset:0},{name:"B",base:Ot.BIT,prefixes:Tt.BINARY_SHORT,value:8,offset:0},{name:"bytes",base:Ot.BIT,prefixes:Tt.BINARY_LONG,value:8,offset:0}],bt.E=Math.E,bt.LN2=Math.LN2,bt.LN10=Math.LN10,bt.LOG2E=Math.LOG2E,bt.LOG10E=Math.LOG10E,bt.PI=Math.PI,bt.SQRT1_2=Math.SQRT1_2,bt.SQRT2=Math.SQRT2,bt.I=new e(0,-1),bt.pi=bt.PI,bt.e=bt.E,bt.i=bt.I,bt.abs=o,o.doc={name:"abs",category:"Arithmetic",syntax:["abs(x)"],description:"Compute the absolute value.",examples:["abs(3.5)","abs(-4.2)"],seealso:["sign"]},bt.add=f,f.doc={name:"add",category:"Operators",syntax:["x + y","add(x, y)"],description:"Add two values.",examples:["2.1 + 3.6","ans - 3.6","3 + 2i",'"hello" + " world"',"3 cm + 2 inch"],seealso:["subtract"]},bt.ceil=u,u.doc={name:"ceil",category:"Arithmetic",syntax:["ceil(x)"],description:"Round a value towards plus infinity.If x is complex, both real and imaginary part are rounded towards plus infinity.",examples:["ceil(3.2)","ceil(3.8)","ceil(-4.2)"],seealso:["floor","fix","round"]},bt.cube=h,h.doc={name:"cube",category:"Arithmetic",syntax:["cube(x)"],description:"Compute the cube of a value. The cube of x is x * x * x.",examples:["cube(2)","2^3","2 * 2 * 2"],seealso:["multiply","square","pow"]},bt.divide=c,c.doc={name:"divide",category:"Operators",syntax:["x / y","divide(x, y)"],description:"Divide two values.",examples:["2 / 3","ans * 3","4.5 / 2","3 + 4 / 2","(3 + 4) / 2","18 km / 4.5"],seealso:["multiply"]},bt.equal=p,p.doc={name:"equal",category:"Operators",syntax:["x == y","equal(x, y)"],description:"Check equality of two values. Returns 1 if the values are equal, and 0 if not.",examples:["2+2 == 3","2+2 == 4","a = 3.2","b = 6-2.8","a == b","50cm == 0.5m"],seealso:["unequal","smaller","larger","smallereq","largereq"]},bt.exp=m,m.doc={name:"exp",category:"Arithmetic",syntax:["exp(x)"],description:"Calculate the exponent of a value.",examples:["exp(1.3)","e ^ 1.3","log(exp(1.3))","x = 2.4","(exp(i*x) == cos(x) + i*sin(x)) # Euler's formula"],seealso:["square","multiply","log"]},bt.fix=d,d.doc={name:"fix",category:"Arithmetic",syntax:["fix(x)"],description:"Round a value towards zero.If x is complex, both real and imaginary part are rounded towards zero.",examples:["fix(3.2)","fix(3.8)","fix(-4.2)","fix(-4.8)"],seealso:["ceil","floor","round"]},bt.floor=g,g.doc={name:"floor",category:"Arithmetic",syntax:["floor(x)"],description:"Round a value towards minus infinity.If x is complex, both real and imaginary part are rounded towards minus infinity.",examples:["floor(3.2)","floor(3.8)","floor(-4.2)"],seealso:["ceil","fix","round"]},bt.larger=v,v.doc={name:"larger",category:"Operators",syntax:["x > y","larger(x, y)"],description:"Check if value x is larger than y. Returns 1 if x is larger than y, and 0 if not.",examples:["2 > 3","5 > 2*2","a = 3.3","b = 6-2.8","(a > b)","(b < a)","5 cm > 2 inch"],seealso:["equal","unequal","smaller","smallereq","largereq"]},bt.largereq=y,y.doc={name:"largereq",category:"Operators",syntax:["x >= y","largereq(x, y)"],description:"Check if value x is larger or equal to y. Returns 1 if x is larger or equal to y, and 0 if not.",examples:["2 > 1+1","2 >= 1+1","a = 3.2","b = 6-2.8","(a > b)"],seealso:["equal","unequal","smallereq","smaller","largereq"]},bt.log=x,x.doc={name:"log",category:"Arithmetic",syntax:["log(x)","log(x, base)"],description:"Compute the logarithm of a value. If no base is provided, the natural logarithm of x is calculated. If base if provided, the logarithm is calculated for the specified base. log(x, base) is defined as log(x) / log(base).",examples:["log(3.5)","a = log(2.4)","exp(a)","10 ^ 3","log(1000, 10)","log(1000) / log(10)","b = logb(1024, 2)","2 ^ b"],seealso:["exp","log10"]},bt.log10=w,w.doc={name:"log10",category:"Arithmetic",syntax:["log10(x)"],description:"Compute the 10-base logarithm of a value.",examples:["log10(1000)","10 ^ 3","log10(0.01)","log(1000) / log(10)","log(1000, 10)"],seealso:["exp","log"]},bt.mod=E,E.doc={name:"mod",category:"Operators",syntax:["x % y","x mod y","mod(x, y)"],description:"Calculates the modulus, the remainder of an integer division.",examples:["7 % 3","11 % 2","10 mod 4","function isOdd(x) = x % 2","isOdd(2)","isOdd(3)"],seealso:[]},bt.multiply=N,N.doc={name:"multiply",category:"Operators",syntax:["x * y","multiply(x, y)"],description:"multiply two values.",examples:["2.1 * 3.6","ans / 3.6","2 * 3 + 4","2 * (3 + 4)","3 * 2.1 km"],seealso:["divide"]},bt.pow=M,M.doc={name:"pow",category:"Operators",syntax:["x ^ y","pow(x, y)"],description:"Calculates the power of x to y, x^y.",examples:["2^3 = 8","2*2*2","1 + e ^ (pi * i)"],seealso:["unequal","smaller","larger","smallereq","largereq"]},bt.round=T,T.doc={name:"round",category:"Arithmetic",syntax:["round(x)","round(x, n)"],description:"round a value towards the nearest integer.If x is complex, both real and imaginary part are rounded towards the nearest integer. When n is specified, the value is rounded to n decimals.",examples:["round(3.2)","round(3.8)","round(-4.2)","round(-4.8)","round(pi, 3)","round(123.45678, 2)"],seealso:["ceil","floor","fix"]},bt.sign=A,A.doc={name:"sign",category:"Arithmetic",syntax:["sign(x)"],description:"Compute the sign of a value. The sign of a value x is 1 when x>1, -1 when x<0, and 0 when x=0.",examples:["sign(3.5)","sign(-4.2)","sign(0)"],seealso:["abs"]},bt.smaller=k,k.doc={name:"smaller",category:"Operators",syntax:["x < y","smaller(x, y)"],description:"Check if value x is smaller than value y. Returns 1 if x is smaller than y, and 0 if not.",examples:["2 < 3","5 < 2*2","a = 3.3","b = 6-2.8","(a < b)","5 cm < 2 inch"],seealso:["equal","unequal","larger","smallereq","largereq"]},bt.smallereq=_,_.doc={name:"smallereq",category:"Operators",syntax:["x <= y","smallereq(x, y)"],description:"Check if value x is smaller or equal to value y. Returns 1 if x is smaller than y, and 0 if not.",examples:["2 < 1+1","2 <= 1+1","a = 3.2","b = 6-2.8","(a < b)"],seealso:["equal","unequal","larger","smaller","largereq"]},bt.sqrt=U,U.doc={name:"sqrt",category:"Arithmetic",syntax:["sqrt(x)"],description:"Compute the square root value. If x = y * y, then y is the square root of x.",examples:["sqrt(25)","5 * 5","sqrt(-1)"],seealso:["square","multiply"]},bt.square=q,q.doc={name:"square",category:"Arithmetic",syntax:["square(x)"],description:"Compute the square of a value. The square of x is x * x.",examples:["square(3)","sqrt(9)","3^2","3 * 3"],seealso:["multiply","pow","sqrt","cube"]},bt.subtract=L,L.doc={name:"subtract",category:"Operators",syntax:["x - y","subtract(x, y)"],description:"subtract two values.",examples:["5.3 - 2","ans + 2","2/3 - 1/6","2 * 3 - 3","2.1 km - 500m"],seealso:["add"]},bt.unaryminus=C,C.doc={name:"unaryminus",category:"Operators",syntax:["-x","unaryminus(x)"],description:"Inverse the sign of a value.",examples:["-4.5","-(-5.6)"],seealso:["add","subtract"]},bt.unequal=R,R.doc={name:"unequal",category:"Operators",syntax:["x != y","unequal(x, y)"],description:"Check unequality of two values. Returns 1 if the values are unequal, and 0 if they are equal.",examples:["2+2 != 3","2+2 != 4","a = 3.2","b = 6-2.8","a != b","50cm != 0.5m","5 cm != 2 inch"],seealso:["equal","smaller","larger","smallereq","largereq"]},bt.arg=I,I.doc={name:"arg",category:"Complex",syntax:["arg(x)"],description:"Compute the argument of a complex value. If x = a+bi, the argument is computed as atan2(b, a).",examples:["arg(2 + 2i)","atan2(3, 2)","arg(2 - 3i)"],seealso:["re","im","conj","abs"]},bt.conj=P,P.doc={name:"conj",category:"Complex",syntax:["conj(x)"],description:"Compute the complex conjugate of a complex value. If x = a+bi, the complex conjugate is a-bi.",examples:["conj(2 + 3i)","conj(2 - 3i)","conj(-5.2i)"],seealso:["re","im","abs","arg"]},bt.im=B,B.doc={name:"im",category:"Complex",syntax:["im(x)"],description:"Get the imaginary part of a complex number.",examples:["im(2 + 3i)","re(2 + 3i)","im(-5.2i)","im(2.4)"],seealso:["re","conj","abs","arg"]},bt.re=G,G.doc={name:"re",category:"Complex",syntax:["re(x)"],description:"Get the real part of a complex number.",examples:["re(2 + 3i)","im(2 + 3i)","re(-5.2i)","re(2.4)"],seealso:["im","conj","abs","arg"]},D.doc={name:"eye",category:"Matrix",syntax:["eye(n)","eye(m, n)","eye([m, n])","eye"],description:"Returns the identity matrix with size m-by-n. The matrix has ones on the diagonal and zeros elsewhere.",examples:["eye(3)","eye(3, 5)","a = [1, 2, 3; 4, 5, 6]","eye(size(a))"],seealso:["diag","ones","range","size","transpose","zeros"]},Y.doc={name:"size",category:"Matrix",syntax:["size(x)"],description:"Calculate the size of a matrix.",examples:["size(2.3)",'size("hello world")',"a = [1, 2; 3, 4; 5, 6]","size(a)","size(1:6)"],seealso:["diag","eye","ones","range","transpose","zeros"]},bt.factorial=z,z.doc={name:"factorial",category:"Probability",syntax:["x!","factorial(x)"],description:"Compute the factorial of a value",examples:["5!","5*4*3*2*1","3!"],seealso:[]},bt.random=j,j.doc={name:"random",category:"Probability",syntax:["random()"],description:"Return a random number between 0 and 1.",examples:["random()","100 * random()"],seealso:[]},bt.max=H,H.doc={name:"max",category:"Statistics",syntax:["max(a, b, c, ...)"],description:"Compute the maximum value of a list of values.",examples:["max(2, 3, 4, 1)","max(2.7, 7.1, -4.5, 2.0, 4.1)","min(2.7, 7.1, -4.5, 2.0, 4.1)"],seealso:["sum","prod","avg","var","std","min","median"]},bt.min=K,K.doc={name:"min",category:"Statistics",syntax:["min(a, b, c, ...)"],description:"Compute the minimum value of a list of values.",examples:["max(2, 3, 4, 1)","max(2.7, 7.1, -4.5, 2.0, 4.1)","min(2.7, 7.1, -4.5, 2.0, 4.1)"],seealso:["sum","prod","avg","var","std","min","median"]},bt.acos=W,W.doc={name:"acos",category:"Trigonometry",syntax:["acos(x)"],description:"Compute the inverse cosine of a value in radians.",examples:["acos(0.5)","acos(cos(2.3))"],seealso:["cos","acos","asin"]},bt.asin=X,X.doc={name:"asin",category:"Trigonometry",syntax:["asin(x)"],description:"Compute the inverse sine of a value in radians.",examples:["asin(0.5)","asin(sin(2.3))"],seealso:["sin","acos","asin"]},bt.atan=Z,Z.doc={name:"atan",category:"Trigonometry",syntax:["atan(x)"],description:"Compute the inverse tangent of a value in radians.",examples:["atan(0.5)","atan(tan(2.3))"],seealso:["tan","acos","asin"]},bt.atan2=Q,Q.doc={name:"atan2",category:"Trigonometry",syntax:["atan2(y, x)"],description:"Computes the principal value of the arc tangent of y/x in radians.",examples:["atan2(2, 2) / pi","angle = 60 deg in rad","x = cos(angle)","y = sin(angle)","atan2(y, x)"],seealso:["sin","cos","tan"]},bt.cos=J,J.doc={name:"cos",category:"Trigonometry",syntax:["cos(x)"],description:"Compute the cosine of x in radians.",examples:["cos(2)","cos(pi / 4) ^ 2","cos(180 deg)","cos(60 deg)","sin(0.2)^2 + cos(0.2)^2"],seealso:["acos","sin","tan"]},bt.cot=$,$.doc={name:"cot",category:"Trigonometry",syntax:["cot(x)"],description:"Compute the cotangent of x in radians. Defined as 1/tan(x)",examples:["cot(2)","1 / tan(2)"],seealso:["sec","csc","tan"]},bt.csc=et,et.doc={name:"csc",category:"Trigonometry",syntax:["csc(x)"],description:"Compute the cosecant of x in radians. Defined as 1/sin(x)",examples:["csc(2)","1 / sin(2)"],seealso:["sec","cot","sin"]},bt.sec=tt,tt.doc={name:"sec",category:"Trigonometry",syntax:["sec(x)"],description:"Compute the secant of x in radians. Defined as 1/cos(x)",examples:["sec(2)","1 / cos(2)"],seealso:["cot","csc","cos"]},bt.sin=nt,nt.doc={name:"sin",category:"Trigonometry",syntax:["sin(x)"],description:"Compute the sine of x in radians.",examples:["sin(2)","sin(pi / 4) ^ 2","sin(90 deg)","sin(30 deg)","sin(0.2)^2 + cos(0.2)^2"],seealso:["asin","cos","tan"]},bt.tan=rt,rt.doc={name:"tan",category:"Trigonometry",syntax:["tan(x)"],description:"Compute the tangent of x in radians.",examples:["tan(0.5)","sin(0.5) / cos(0.5)","tan(pi / 4)","tan(45 deg)"],seealso:["atan","sin","cos"]},bt["in"]=it,it.doc={name:"in",category:"Units",syntax:["x in unit","in(x, unit)"],description:"Change the unit of a value.",examples:["5 inch in cm","3.2kg in g","16 bytes in bits"],seealso:[]},bt.format=at,at.doc={name:"format",category:"Utils",syntax:["format(value)"],description:"Format a value of any type as string.",examples:["format(2.3)","format(3 - 4i)","format([])"],seealso:[]},bt.help=ft,ft.doc={name:"help",category:"Utils",syntax:["help(object)"],description:"Display documentation on a function or data type.",examples:['help("sqrt")','help("Complex")'],seealso:[]},bt["import"]=ht,ht.doc={name:"import",category:"Utils",syntax:["import(string)"],description:"Import functions from a file.",examples:['import("numbers")','import("./mylib.js")'],seealso:[]},bt["typeof"]=lt,lt.doc={name:"typeof",category:"Utils",syntax:["typeof(x)"],description:"Get the type of a variable.",examples:["typeof(3.5)","typeof(2 - 4i)","typeof(45 deg)",'typeof("hello world")'],seealso:[]},bt.parser.node.Node=pt,pt.prototype.eval=function(){throw Error("Cannot evaluate a Node interface")},pt.prototype.toString=function(){return""},mt.prototype=new pt,bt.parser.node.Symbol=mt,mt.prototype.hasParams=function(){return void 0!=this.params&&this.params.length>0},mt.prototype.eval=function(){var e=this.fn;if(void 0===e)throw Error("Undefined symbol "+this.name);var t=this.params.map(function(e){return e.eval()});return e.apply(this,t)},mt.prototype.toString=function(){if(this.name&&!this.params)return this.name;var e=this.name;return this.params&&this.params.length&&(e+="("+this.params.join(", ")+")"),e},dt.prototype=new pt,bt.parser.node.Constant=dt,dt.prototype.eval=function(){return this.value},dt.prototype.toString=function(){return this.value?bt.format(this.value):""},gt.prototype=new pt,bt.parser.node.ArrayNode=gt,function(){function e(t){return t.map(function(t){return t instanceof Array?e(t):t.eval()})}function t(e){if(e instanceof Array){for(var n="[",r=e.length,i=0;r>i;i++)0!=i&&(n+=", "),n+=t(e[i]);return n+="]"}return""+e}gt.prototype.eval=function(){return e(this.nodes)},gt.prototype.toString=function(){return t(this.nodes)}}(),vt.prototype=new pt,bt.parser.node.Block=vt,vt.prototype.add=function(e,t){var n=this.params.length;this.params[n]=e,this.visible[n]=void 0!=t?t:!0},vt.prototype.eval=function(){for(var e=[],t=0,n=this.params.length;n>t;t++){var r=this.params[t].eval();this.visible[t]&&e.push(r)}return e},vt.prototype.toString=function(){for(var e=[],t=0,n=this.params.length;n>t;t++)this.visible[t]&&e.push("\n "+(""+this.params[t]));return"["+e.join(",")+"\n]"},yt.prototype=new pt,bt.parser.node.Assignment=yt,yt.prototype.eval=function(){if(void 0===this.expr)throw Error("Undefined symbol "+this.name);var e,t=this.params;if(t&&t.length){var n=[];this.params.forEach(function(e){n.push(e.eval())});var r=this.expr.eval();if(void 0==this.result.value)throw Error("Undefined symbol "+this.name);var i=this.result.eval();e=i.set(n,r),this.result.value=e}else e=this.expr.eval(),this.result.value=e;return e},yt.prototype.toString=function(){var e="";return e+=this.name,this.params&&this.params.length&&(e+="("+this.params.join(", ")+")"),e+=" = ",e+=""+this.expr},xt.prototype=new pt,bt.parser.node.FunctionAssignment=xt,xt.prototype.createFunction=function(e,t,n,r){var i=function(){var t=n?n.length:0,i=arguments?arguments.length:0;if(t!=i)throw s(e,i,t);if(t>0)for(var a=0;t>a;a++)n[a].value=arguments[a];return r.eval()};return i.toString=function(){return e+"("+t.join(", ")+")"},i},xt.prototype.eval=function(){for(var e=this.variables,t=this.values,n=0,r=e.length;r>n;n++)e[n].value=t[n];return this.result.value=this.def,this.def},xt.prototype.toString=function(){return""+this.def},bt.parser.node.Scope=wt,wt.prototype.createNestedScope=function(){var e=new wt(this);return this.nestedScopes||(this.nestedScopes=[]),this.nestedScopes.push(e),e},wt.prototype.clear=function(){if(this.symbols={},this.defs={},this.links={},this.updates={},this.nestedScopes)for(var e=this.nestedScopes,t=0,n=e.length;n>t;t++)e[t].clear()},wt.prototype.createSymbol=function(e){var t=this.symbols[e];if(!t){var n=this.findDef(e);t=this.newSymbol(e,n),this.symbols[e]=t}return t},wt.prototype.newSymbol=function(e,t){var n=this,r=function(){if(!r.value&&(r.value=n.findDef(e),!r.value))throw Error("Undefined symbol "+e);return"function"==typeof r.value?r.value.apply(null,arguments):r.value};return r.value=t,r.toString=function(){return r.value?""+r.value:""},r},wt.prototype.createLink=function(e){var t=this.links[e];return t||(t=this.createSymbol(e),this.links[e]=t),t},wt.prototype.createDef=function(e,t){var n=this.defs[e];return n||(n=this.createSymbol(e),this.defs[e]=n),n&&void 0!=t&&(n.value=t),n},wt.prototype.createUpdate=function(e){var t=this.updates[e];return t||(t=this.createLink(e),this.updates[e]=t),t},wt.prototype.findDef=function(t){function n(e,t){var n=a(e,t);return s[e]=n,o[e]=n,n}var r;if(r=this.defs[t])return r;if(r=this.updates[t])return r;if(this.parentScope)return this.parentScope.findDef(t);var a=this.newSymbol,s=this.symbols,o=this.defs;if("pi"==t)return n(t,bt.PI);if("e"==t)return n(t,bt.E);if("i"==t)return n(t,new e(0,1));var f=bt[t];if(f)return n(t,f);if(i.isUnit(t)){var u=new i(null,t);return n(t,u)}return void 0},wt.prototype.removeLink=function(e){delete this.links[e]},wt.prototype.removeDef=function(e){delete this.defs[e]},wt.prototype.removeUpdate=function(e){delete this.updates[e]},wt.prototype.init=function(){var e=this.symbols,t=this.parentScope;for(var n in e)if(e.hasOwnProperty(n)){var r=e[n];r.value=t?t.findDef(n):void 0}this.nestedScopes&&this.nestedScopes.forEach(function(e){e.init()})},wt.prototype.hasLink=function(e){if(this.links[e])return!0;if(this.nestedScopes)for(var t=this.nestedScopes,n=0,r=t.length;r>n;n++)if(t[n].hasLink(e))return!0;return!1},wt.prototype.hasDef=function(e){return void 0!=this.defs[e]},wt.prototype.hasUpdate=function(e){return void 0!=this.updates[e]},wt.prototype.getUndefinedSymbols=function(){var e=this.symbols,t=[];for(var n in e)if(e.hasOwnProperty(n)){var r=e[n];void 0==r.value&&t.push(r)}return this.nestedScopes&&this.nestedScopes.forEach(function(e){t=t.concat(e.getUndefinedSymbols())}),t},bt.parser.Parser=Et,Et.prototype.parse=function(e,t){return this.expr=e||"",t||(this.newScope(),t=this.scope),this.parse_start(t)},Et.prototype.eval=function(e){var t=this.parse(e);return t.eval()},Et.prototype.get=function(e){this.newScope();var t=this.scope.findDef(e);return t?t.value:void 0},Et.prototype.put=function(e,t){this.scope.createDef(e,t)},Et.prototype.newScope=function(){this.scope=new wt(this.scope)},Et.prototype.clear=function(){this.scope.clear()},Et.prototype.getChar=function(){this.index++,this.c=this.expr.charAt(this.index)},Et.prototype.getFirstChar=function(){this.index=0,this.c=this.expr.charAt(0)},Et.prototype.getToken=function(){for(this.token_type=this.TOKENTYPE.NULL,this.token="";" "==this.c||" "==this.c;)this.getChar();if("#"==this.c)for(;"\n"!=this.c&&""!=this.c;)this.getChar();if(""==this.c)return this.token_type=this.TOKENTYPE.DELIMITER,void 0;if("-"==this.c||","==this.c||"("==this.c||")"==this.c||"["==this.c||"]"==this.c||'"'==this.c||"\n"==this.c||";"==this.c||":"==this.c)return this.token_type=this.TOKENTYPE.DELIMITER,this.token+=this.c,this.getChar(),void 0;if(this.isDelimiter(this.c))for(this.token_type=this.TOKENTYPE.DELIMITER;this.isDelimiter(this.c);)this.token+=this.c,this.getChar();else if(this.isDigitDot(this.c)){for(this.token_type=this.TOKENTYPE.NUMBER;this.isDigitDot(this.c);)this.token+=this.c,this.getChar();if("E"==this.c||"e"==this.c)for(this.token+=this.c,this.getChar(),("+"==this.c||"-"==this.c)&&(this.token+=this.c,this.getChar()),this.isDigit(this.c)||(this.token_type=this.TOKENTYPE.UNKNOWN);this.isDigit(this.c);)this.token+=this.c,this.getChar()}else{if(!this.isAlpha(this.c)){for(this.token_type=this.TOKENTYPE.UNKNOWN;""!=this.c;)this.token+=this.c,this.getChar();throw this.createSyntaxError('Syntax error in part "'+this.token+'"')}for(this.token_type=this.TOKENTYPE.SYMBOL;this.isAlpha(this.c)||this.isDigit(this.c);)this.token+=this.c,this.getChar()}},Et.prototype.isDelimiter=function(e){return"&"==e||"|"==e||"<"==e||">"==e||"="==e||"+"==e||"/"==e||"*"==e||"%"==e||"^"==e||","==e||";"==e||"\n"==e||"!"==e},Et.prototype.isValidSymbolName=function(e){for(var t=0,n=e.length;n>t;t++){var r=e.charAt(t),i=this.isAlpha(r);if(!i)return!1}return!0},Et.prototype.isAlpha=function(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e||"_"==e},Et.prototype.isDigitDot=function(e){return e>="0"&&"9">=e||"."==e},Et.prototype.isDigit=function(e){return e>="0"&&"9">=e},Et.prototype.parse_start=function(e){this.getFirstChar(),this.getToken();var t;if(t=""==this.token?new dt(void 0):this.parse_block(e),""!=this.token)throw this.token_type==this.TOKENTYPE.DELIMITER?this.createError("Unknown operator "+this.token):this.createSyntaxError('Unexpected part "'+this.token+'"');return t},Et.prototype.parse_ans=function(e){var t=this.parse_function_assignment(e);if(!(t instanceof yt)){var n="ans",r=void 0,i=e.createDef(n);return new yt(n,r,t,i)}return t},Et.prototype.parse_block=function(e){var t,n,r;for("\n"!=this.token&&";"!=this.token&&""!=this.token&&(t=this.parse_ans(e));"\n"==this.token||";"==this.token;)n||(n=new vt,t&&(r=";"!=this.token,n.add(t,r))),this.getToken(),"\n"!=this.token&&";"!=this.token&&""!=this.token&&(t=this.parse_ans(e),r=";"!=this.token,n.add(t,r));return n?n:(t||(t=this.parse_ans(e)),t)},Et.prototype.parse_function_assignment=function(e){if(this.token_type==this.TOKENTYPE.SYMBOL&&"function"==this.token){if(this.getToken(),this.token_type!=this.TOKENTYPE.SYMBOL)throw this.createSyntaxError("Function name expected");var t=this.token;if(this.getToken(),"("!=this.token)throw this.createSyntaxError("Opening parenthesis ( expected");for(var n=e.createNestedScope(),r=[],i=[];;){if(this.getToken(),this.token_type!=this.TOKENTYPE.SYMBOL)throw this.createSyntaxError("Variable name expected");var a=this.token,s=n.createDef(a);if(r.push(a),i.push(s),this.getToken(),","!=this.token){if(")"==this.token)break;throw this.createSyntaxError('Comma , or closing parenthesis ) expected"')}}if(this.getToken(),"="!=this.token)throw this.createSyntaxError("Equal sign = expected");this.getToken();var o=this.parse_range(n),f=e.createDef(t);return new xt(t,r,i,o,f)}return this.parse_assignment(e)},Et.prototype.parse_assignment=function(e){var t=!1;this.token_type==this.TOKENTYPE.SYMBOL&&(t=e.hasLink(this.token));var n=this.parse_range(e);if("="==this.token){if(!(n instanceof mt))throw this.createSyntaxError("Symbol expected at the left hand side of assignment operator =");var r=n.name,i=n.params;t||e.removeLink(r),this.getToken();var a=this.parse_range(e),s=n.hasParams()?e.createUpdate(r):e.createDef(r);return new yt(r,i,a,s)}return n},Et.prototype.parse_range=function(e){var t=this.parse_conditions(e);return t},Et.prototype.parse_conditions=function(e){for(var t=this.parse_bitwise_conditions(e),n={"in":"in"};void 0!==n[this.token];){var r=this.token,i=bt[n[r]];this.getToken();var a=[t,this.parse_bitwise_conditions(e)];t=new mt(r,i,a)}return t},Et.prototype.parse_bitwise_conditions=function(e){var t=this.parse_comparison(e);return t},Et.prototype.parse_comparison=function(e){for(var t=this.parse_addsubtract(e),n={"==":"equal","!=":"unequal","<":"smaller",">":"larger","<=":"smallereq",">=":"largereq"};void 0!==n[this.token];){var r=this.token,i=bt[n[r]];this.getToken();var a=[t,this.parse_addsubtract(e)];t=new mt(r,i,a)}return t},Et.prototype.parse_addsubtract=function(e){for(var t=this.parse_multiplydivide(e),n={"+":"add","-":"subtract"};void 0!==n[this.token];){var r=this.token,i=bt[n[r]];this.getToken();var a=[t,this.parse_multiplydivide(e)];t=new mt(r,i,a)}return t},Et.prototype.parse_multiplydivide=function(e){for(var t=this.parse_pow(e),n={"*":"multiply","/":"divide","%":"mod",mod:"mod"};void 0!==n[this.token];){var r=this.token,i=bt[n[r]];this.getToken();var a=[t,this.parse_pow(e)];t=new mt(r,i,a)}return t},Et.prototype.parse_pow=function(e){for(var t=this.parse_factorial(e);"^"==this.token;){var n=this.token,r=M;this.getToken();var i=[t,this.parse_factorial(e)];t=new mt(n,r,i)}return t},Et.prototype.parse_factorial=function(e){for(var t=this.parse_unaryminus(e);"!"==this.token;){var n=this.token,r=z;this.getToken();var i=[t];t=new mt(n,r,i)}return t},Et.prototype.parse_unaryminus=function(e){if("-"==this.token){var t=this.token,n=C;this.getToken();var r=[this.parse_plot(e)];return new mt(t,n,r)}return this.parse_plot(e)},Et.prototype.parse_plot=function(e){return this.parse_symbol(e)},Et.prototype.parse_symbol=function(e){if(this.token_type==this.TOKENTYPE.SYMBOL){var t=this.token; -this.getToken();var n=e.createLink(t),r=this.parse_arguments(e),i=new mt(t,n,r);return i}return this.parse_string(e)},Et.prototype.parse_arguments=function(e){var t=[];if("("==this.token){if(this.getToken(),")"!=this.token)for(t.push(this.parse_range(e));","==this.token;)this.getToken(),t.push(this.parse_range(e));if(")"!=this.token)throw this.createSyntaxError("Parenthesis ) missing");this.getToken()}return t},Et.prototype.parse_string=function(e){if('"'==this.token){for(var t="",n="";""!=this.c&&('"'!=this.c||"\\"==n);)t+=this.c,n=this.c,this.getChar();if(this.getToken(),'"'!=this.token)throw this.createSyntaxError('End of string " missing');this.getToken();var r=new dt(t);return r}return this.parse_matrix(e)},Et.prototype.parse_matrix=function(e){if("["==this.token){var t;for(this.getToken();"\n"==this.token;)this.getToken();if("]"!=this.token){var n=[],r=0,i=0;for(n[0]=[this.parse_range(e)];","==this.token||";"==this.token;){for(","==this.token?i++:(r++,i=0,n[r]=[]),this.getToken();"\n"==this.token;)this.getToken();for(n[r][i]=this.parse_range(e);"\n"==this.token;)this.getToken()}var a=n.length,s=n.length>0?n[0].length:0;for(r=1;a>r;r++)if(n[r].length!=s)throw this.createError("Number of columns must match ("+n[r].length+" != "+s+")");if("]"!=this.token)throw this.createSyntaxError("End of matrix ] missing");this.getToken(),t=new gt(n)}else this.getToken(),t=new gt([]);for(;"("==this.token;)t=this.parse_arguments(e,t);return t}return this.parse_number(e)},Et.prototype.parse_number=function(t){if(this.token_type==this.TOKENTYPE.NUMBER){var n;n="."==this.token?0:Number(this.token),this.getToken();var r;if(this.token_type==this.TOKENTYPE.SYMBOL){if("i"==this.token||"I"==this.token)return r=new e(0,n),this.getToken(),new dt(r);if(i.isUnit(this.token))return r=new i(n,this.token),this.getToken(),new dt(r);throw this.createTypeError('Unknown unit "'+this.token+'"')}var a=new dt(n);return a}return this.parse_parentheses(t)},Et.prototype.parse_parentheses=function(e){if("("==this.token){this.getToken();var t=this.parse_range(e);if(")"!=this.token)throw this.createSyntaxError("Parenthesis ) expected");return this.getToken(),t}return this.parse_end(e)},Et.prototype.parse_end=function(){throw""==this.token?this.createSyntaxError("Unexpected end of expression"):this.createSyntaxError("Value expected")},Et.prototype.row=function(){return void 0},Et.prototype.col=function(){return this.index-this.token.length+1},Et.prototype.createErrorMessage=function(e){var t=this.row(),n=this.col();return void 0===t?void 0===n?e:e+" (col "+n+")":e+" (ln "+t+", col "+n+")"},Et.prototype.createSyntaxError=function(e){return new SyntaxError(this.createErrorMessage(e))},Et.prototype.createTypeError=function(e){return new TypeError(this.createErrorMessage(e))},Et.prototype.createError=function(e){return Error(this.createErrorMessage(e))},bt.parser.Workspace=Nt,Nt.prototype.clear=function(){this.nodes={},this.firstNode=void 0,this.lastNode=void 0},Nt.prototype.append=function(e){var t=this._getNewId(),n=this.lastNode?this.lastNode.scope:this.scope,r=new wt(n),i=new Nt.Node({id:t,expression:e,parser:this.parser,scope:r,nextNode:void 0,previousNode:this.lastNode});return this.nodes[t]=i,this.firstNode||(this.firstNode=i),this.lastNode&&(this.lastNode.nextNode=i),this.lastNode=i,this._update([t]),t},Nt.prototype.insertBefore=function(e,t){var n=this.nodes[t];if(!n)throw'Node with id "'+t+'" not found';var r=n.previousNode,i=this._getNewId(),a=r?r.scope:this.scope,s=new wt(a),o=new Nt.Node({id:i,expression:e,parser:this.parser,scope:s,nextNode:n,previousNode:r});this.nodes[i]=o,r?r.nextNode=o:this.firstNode=o,n.previousNode=o,n.scope.parentScope=o.scope;var f=this.getDependencies(i);return-1==f.indexOf(i)&&f.unshift(i),this._update(f),i},Nt.prototype.insertAfter=function(e,t){var n=this.nodes[t];if(!n)throw'Node with id "'+t+'" not found';return n==this.lastNode?this.append(e):this.insertBefore(t+1,e)},Nt.prototype.remove=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';var n=this.getDependencies(e),r=t.previousNode,i=t.nextNode;r?r.nextNode=i:this.firstNode=i,i?i.previousNode=r:this.lastNode=r;var a=r?r.scope:this.scope;i&&(i.scope.parentScope=a),delete this.nodes[e],this._update(n)},Nt.prototype.replace=function(e,t){var n=this.nodes[t];if(!n)throw'Node with id "'+t+'" not found';var r=[t];Nt._merge(r,this.getDependencies(t));var i=n.previousNode;n.nextNode,i?i.scope:this.scope,n.setExpr(e),Nt._merge(r,this.getDependencies(t)),this._update(r)},Nt.Node=function(e){this.id=e.id,this.parser=e.parser,this.scope=e.scope,this.nextNode=e.nextNode,this.previousNode=e.previousNode,this.updateSeq=0,this.result=void 0,this.setExpr(e.expression)},Nt.Node.prototype.setExpr=function(e){this.expression=e||"",this.scope.clear(),this._parse()},Nt.Node.prototype.getExpr=function(){return this.expression},Nt.Node.prototype.getResult=function(){return this.result},Nt.Node.prototype._parse=function(){try{this.fn=this.parser.parse(this.expression,this.scope)}catch(e){var t="Error: "+((e.message||e)+"");this.fn=new dt(t)}},Nt.Node.prototype.eval=function(){try{this.scope.init(),this.result=this.fn.eval()}catch(e){this.scope.init(),this.result="Error: "+((e.message||e)+"")}return this.result},Nt._merge=function(e,t){for(var n=0,r=t.length;r>n;n++){var i=t[n];-1==e.indexOf(i)&&e.push(i)}},Nt.prototype.getDependencies=function(e){var t,n=[],r=this.nodes[e];if(r){var i=r.scope.defs,a=r.scope.updates,s=[];for(t in i)i.hasOwnProperty(t)&&s.push(t);for(t in a)a.hasOwnProperty(t)&&-1==s.indexOf(t)&&s.push(t);for(var o=r.nextNode;o&&s.length;){for(var f=o.scope,u=0;s.length>u;){if(t=s[u],(f.hasLink(t)||f.hasUpdate(t))&&-1==n.indexOf(o.id)){n.push(o.id);var h=this.getDependencies(o.id);Nt._merge(n,h)}f.hasDef(t)&&(s.splice(u,1),u--),u++}o=o.nextNode}}return n},Nt.prototype.getExpr=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';return t.getExpr()},Nt.prototype.getResult=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';return t.getResult()},Nt.prototype._update=function(e){this.updateSeq++;for(var t=this.updateSeq,n=this.nodes,r=0,i=e.length;i>r;r++){var a=e[r],s=n[a];s&&(s.eval(),s.updateSeq=t)}},Nt.prototype.getChanges=function(e){var t=[],n=this.firstNode;for(e=e||0;n;)n.updateSeq>e&&t.push(n.id),n=n.nextNode;return{ids:t,updateSeq:this.updateSeq}},Nt.prototype._getNewId=function(){return this.idMax++,this.idMax},Nt.prototype.toString=function(){return JSON.stringify(this.toJSON())},Nt.prototype.toJSON=function(){for(var e=[],t=this.firstNode;t;){var n={id:t.id,expression:t.expression,dependencies:this.getDependencies(t.id)};try{n.result=t.getResult()}catch(r){n.result="Error: "+((r.message||r)+"")}e.push(n),t=t.nextNode}return e}})(); \ No newline at end of file +(function(){function e(t,n){if(this.constructor!=e)throw new SyntaxError("Complex constructor must be called with the new operator");switch(arguments.length){case 2:if(!r(t)||!r(n))throw new TypeError("Two numbers or a single string expected in Complex constructor");this.re=t,this.im=n;break;case 1:if(!i(t))throw new TypeError("Two numbers or a single string expected in Complex constructor");var a=e.parse(t);if(a)return a;throw new SyntaxError('String "'+t+'" is no valid complex number');case 0:this.re=0,this.im=0;break;default:throw new SyntaxError("Wrong number of arguments in Complex constructor ("+arguments.length+" provided, 0, 1, or 2 expected)")}}function t(e){if(this.constructor!=t)throw new SyntaxError("Matrix constructor must be called with the new operator");this.array=e||[]}function r(e){return e instanceof Number||"number"==typeof e}function n(e){return e==Math.round(e)}function i(e){return e instanceof String||"string"==typeof e}function a(e,t){if(this.constructor!=a)throw Error("Unit constructor must be called with the new operator");this.value=1,this.unit=a.UNIT_NONE,this.prefix=a.PREFIX_NONE,this.hasUnit=!1,this.hasValue=!1,this.fixPrefix=!1;var r=arguments.length;if(0==r);else{if(1==r){if(!i(e))throw new TypeError("A string or a number and string expected in Unit constructor");var n=a.parse(e);if(n)return n;throw new SyntaxError('String "'+e+'" is no valid unit')}if(2!=r)throw Error("Too many parameters in Unit constructor, 1 or 2 expected");if(!i(t))throw Error("Second parameter in Unit constructor must be a String");for(var s=a.UNITS,o=!1,f=0,u=s.length;u>f;f++){var l=s[f];if(a.endsWith(t,l.name)){var h=t.length-l.name.length,c=t.substring(0,h),p=l.prefixes[c];if(void 0!==p){this.unit=l,this.prefix=p,this.hasUnit=!0,o=!0;break}}}if(!o)throw Error('String "'+t+'" is no unit');null!=e?(this.value=this._normalize(e),this.hasValue=!0):this.value=this._normalize(1)}}function s(e,t){var r=void 0;if(2==arguments.length){var n=ht(t);r="Function "+e+" does not support a parameter of type "+n}else if(arguments.length>2){for(var i=[],a=1;arguments.length>a;a++)i.push(ht(arguments[a]));r="Function "+e+" does not support a parameters of type "+i.join(", ")}else r="Unsupported parameter in function "+e;return new TypeError(r)}function o(e,t,r,n){var i="Wrong number of arguments in function "+e+" ("+t+" provided, "+r+(void 0!=n?"-"+n:"")+" expected)";return new SyntaxError(i)}function f(t){if(1!=arguments.length)throw o("abs",arguments.length,1);if(r(t))return Math.abs(t);if(t instanceof e)return Math.sqrt(t.re*t.re+t.im*t.im);if(t instanceof Array)return Ot.map(t,f);if(t.valueOf()!==t)return f(t.valueOf());throw s("abs",t)}function u(t,n){if(2!=arguments.length)throw o("add",arguments.length,2);if(r(t)){if(r(n))return t+n;if(n instanceof e)return new e(t+n.re,n.im)}else if(t instanceof e){if(r(n))return new e(t.re+n,t.im);if(n instanceof e)return new e(t.re+n.re,t.im+n.im)}else if(t instanceof a&&n instanceof a){if(!t.equalBase(n))throw Error("Units do not match");if(!t.hasValue)throw Error("Unit on left hand side of operator + has no value");if(!n.hasValue)throw Error("Unit on right hand side of operator + has no value");var f=t.clone();return f.value+=n.value,f.fixPrefix=!1,f}if(i(t)||i(n))return t+n;if(t instanceof Array||n instanceof Array)return Ot.map2(t,n,u);if(t.valueOf()!==t)return u(t.valueOf());throw s("add",t,n)}function l(t){if(1!=arguments.length)throw o("ceil",arguments.length,1);if(r(t))return Math.ceil(t);if(t instanceof e)return new e(Math.ceil(t.re),Math.ceil(t.im));if(t instanceof Array)return Ot.map(t,l);if(t.valueOf()!==t)return l(t.valueOf());throw s("ceil",t)}function h(t){if(1!=arguments.length)throw o("cube",arguments.length,1);if(r(t))return t*t*t;if(t instanceof e)return O(O(t,t),t);if(t instanceof Array)return O(O(t,t),t);if(t.valueOf()!==t)return h(t.valueOf());throw s("cube",t)}function c(t,n){if(2!=arguments.length)throw o("divide",arguments.length,2);if(r(t)){if(r(n))return t/n;if(n instanceof e)return p(new e(t,0),n)}if(t instanceof e){if(r(n))return p(t,new e(n,0));if(n instanceof e)return p(t,n)}if(t instanceof a&&r(n)){var i=t.clone();return i.value/=n,i}if(t instanceof Array&&!(n instanceof Array))return Ot.map2(t,n,c);if(t.valueOf()!==t||n.valueOf()!==n)return c(t.valueOf(),n.valueOf());throw s("divide",t,n)}function p(t,r){var n=r.re*r.re+r.im*r.im;return new e((t.re*r.re+t.im*r.im)/n,(t.im*r.re-t.re*r.im)/n)}function m(t,n){if(2!=arguments.length)throw o("equal",arguments.length,2);if(r(t)){if(r(n))return t==n;if(n instanceof e)return t==n.re&&0==n.im}if(t instanceof e){if(r(n))return t.re==n&&0==t.im;if(n instanceof e)return t.re==n.re&&t.im==n.im}if(t instanceof a&&n instanceof a){if(!t.equalBase(n))throw Error("Cannot compare units with different base");return t.value==n.value}if(i(t)||i(n))return t==n;if(t instanceof Array||n instanceof Array)return Ot.map2(t,n,m);if(t.valueOf()!==t||n.valueOf()!==n)return m(t.valueOf(),n.valueOf());throw s("equal",t,n)}function v(t){if(1!=arguments.length)throw o("exp",arguments.length,1);if(r(t))return Math.exp(t);if(t instanceof e){var n=Math.exp(t.re);return new e(n*Math.cos(t.im),n*Math.sin(t.im))}if(t instanceof Array)return Ot.map(t,v);if(t.valueOf()!==t)return v(t.valueOf());throw s("exp",t)}function d(t){if(1!=arguments.length)throw o("fix",arguments.length,1);if(r(t))return value>0?Math.floor(t):Math.ceil(t);if(t instanceof e)return new e(t.re>0?Math.floor(t.re):Math.ceil(t.re),t.im>0?Math.floor(t.im):Math.ceil(t.im));if(t instanceof Array)return Ot.map(t,d);if(t.valueOf()!==t)return d(t.valueOf());throw s("fix",t)}function g(t){if(1!=arguments.length)throw o("floor",arguments.length,1);if(r(t))return Math.floor(t);if(t instanceof e)return new e(Math.floor(t.re),Math.floor(t.im));if(t instanceof Array)return Ot.map(t,g);if(t.valueOf()!==t)return g(t.valueOf());throw s("floor",t)}function y(t,n){if(2!=arguments.length)throw o("larger",arguments.length,2);if(r(t)){if(r(n))return t>n;if(n instanceof e)return t>f(n)}if(t instanceof e){if(r(n))return f(t)>n;if(n instanceof e)return f(t)>f(n)}if(t instanceof a&&n instanceof a){if(!t.equalBase(n))throw Error("Cannot compare units with different base");return t.value>n.value}if(i(t)||i(n))return t>n;if(t instanceof Array||n instanceof Array)return Ot.map2(t,n,m);if(t.valueOf()!==t||n.valueOf()!==n)return y(t.valueOf(),n.valueOf());throw s("larger",t,n)}function x(t,n){if(2!=arguments.length)throw o("largereq",arguments.length,2);if(r(t)){if(r(n))return t>=n;if(n instanceof e)return t>=f(n)}if(t instanceof e){if(r(n))return f(t)>=n;if(n instanceof e)return f(t)>=f(n)}if(t instanceof a&&n instanceof a){if(!t.equalBase(n))throw Error("Cannot compare units with different base");return t.value>=n.value}if(i(t)||i(n))return t>=n;if(t instanceof Array||n instanceof Array)return Ot.map2(t,n,x);if(t.valueOf()!==t||n.valueOf()!==n)return x(t.valueOf(),n.valueOf());throw s("largereq",t,n)}function w(t,n){if(1!=arguments.length&&2!=arguments.length)throw o("log",arguments.length,1,2);if(void 0!==n)return c(w(t),w(n));if(r(t))return t>=0?Math.log(t):w(new e(t,0));if(t instanceof e)return new e(Math.log(Math.sqrt(t.re*t.re+t.im*t.im)),Math.atan2(t.im,t.re));if(t instanceof Array)return Ot.map(t,w);if(t.valueOf()!==t||n.valueOf()!==n)return w(t.valueOf(),n.valueOf());throw s("log",t,n)}function E(t){if(1!=arguments.length)throw o("log10",arguments.length,1);if(r(t))return t>=0?Math.log(t)/Math.LN10:E(new e(t,0));if(t instanceof e)return new e(Math.log(Math.sqrt(t.re*t.re+t.im*t.im))/Math.LN10,Math.atan2(t.im,t.re)/Math.LN10);if(t instanceof Array)return Ot.map(t,E);if(t.valueOf()!==t)return E(t.valueOf());throw s("log10",t)}function N(t,n){if(2!=arguments.length)throw o("mod",arguments.length,2);if(r(t)){if(r(n))return t%n;if(n instanceof e&&0==n.im)return t%n.re}else if(t instanceof e&&0==t.im){if(r(n))return t.re%n;if(n instanceof e&&0==n.im)return t.re%n.re}if(t instanceof Array||n instanceof Array)return Ot.map2(t,n,N);if(t.valueOf()!==t||n.valueOf()!==n)return N(t.valueOf(),n.valueOf());throw s("mod",t,n)}function O(t,n){if(2!=arguments.length)throw o("multiply",arguments.length,2);if(r(t)){if(r(n))return t*n;if(n instanceof e)return b(new e(t,0),n);if(n instanceof a)return l=n.clone(),l.value*=t,l}else if(t instanceof e){if(r(n))return b(t,new e(n,0));if(n instanceof e)return b(t,n)}else if(t instanceof a){if(r(n))return l=t.clone(),l.value*=n,l}else if(t instanceof Array){if(n instanceof Array){var i=Ot.array.validatedSize(t),f=Ot.array.validatedSize(n);if(2!=i.length)throw Error("Can only multiply a 2 dimensional matrix (A has "+i.length+" dimensions)");if(2!=f.length)throw Error("Can only multiply a 2 dimensional matrix (B has "+f.length+" dimensions)");if(i[1]!=f[0])throw Error("Dimensions mismatch in multiplication. Columns of A must match rows of B (A is "+i[0]+"x"+i[1]+", B is "+f[0]+"x"+f[1]+", "+f[1]+" != "+f[0]+")");for(var l=[],h=i[0],c=f[1],p=i[1],m=0;h>m;m++){l[m]=[];for(var v=0;c>v;v++){for(var d=null,g=0;p>g;g++){var y=O(t[m][g],n[g][v]);d=null==d?y:u(d,y)}l[m][v]=d}}return l}return Ot.map2(t,n,O)}if(n instanceof Array)return Ot.map2(t,n,O);if(t.valueOf()!==t||n.valueOf()!==n)return O(t.valueOf(),n.valueOf());throw s("multiply",t,n)}function b(t,r){return new e(t.re*r.re-t.im*r.im,t.re*r.im+t.im*r.re)}function M(t,i){if(2!=arguments.length)throw o("pow",arguments.length,2);if(r(t)){if(r(i))return n(i)||t>=0?Math.pow(t,i):T(new e(t,0),new e(i,0));if(i instanceof e)return T(new e(t,0),i)}else if(t instanceof e){if(r(i))return T(t,new e(i,0));if(i instanceof e)return T(t,i)}else if(t instanceof Array){if(!r(i)||!n(i)||0>i)throw new TypeError("For A^b, b must be a positive integer (value is "+i+")");var a=Ot.array.validatedSize(t);if(2!=a.length)throw Error("For A^b, A must be 2 dimensional (A has "+a.length+" dimensions)");if(a[0]!=a[1])throw Error("For A^b, A must be square (size is "+a[0]+"x"+a[1]+")");if(0==i)return D(a[0]);for(var f=t,u=1;i>u;u++)f=O(t,f);return f}if(t.valueOf()!==t||i.valueOf()!==i)return M(t.valueOf(),i.valueOf());throw s("pow",t,i)}function T(e,t){var r=w(e),n=O(r,t);return v(n)}function S(t,n){if(1!=arguments.length&&2!=arguments.length)throw o("round",arguments.length,1,2);if(void 0==n){if(r(t))return Math.round(t);if(t instanceof e)return new e(Math.round(t.re),Math.round(t.im));if(t instanceof Array&&Ot.map(t,S),t.valueOf()!==t)return S(t.valueOf());throw s("round",t)}if(!r(n))throw new TypeError("Number of digits in function round must be an integer");if(n!==Math.round(n))throw new TypeError("Number of digits in function round must be integer");if(0>n||n>9)throw Error("Number of digits in function round must be in te range of 0-9");if(r(t))return A(t,n);if(t instanceof e)return new e(A(t.re,n),A(t.im,n));if(t instanceof Array||n instanceof Array)return Ot.map2(t,n,S);if(t.valueOf()!==t||n.valueOf()!==n)return y(t.valueOf(),n.valueOf());throw s("round",t,n)}function A(e,t){var r=Math.pow(10,void 0!=t?t:Nt.options.precision);return Math.round(e*r)/r}function k(t){if(1!=arguments.length)throw o("sign",arguments.length,1);if(r(t)){var n;return n=t>0?1:0>t?-1:0}if(t instanceof e){var i=Math.sqrt(t.re*t.re+t.im*t.im);return new e(t.re/i,t.im/i)}if(t instanceof Array)return Ot.map(t,n);if(t.valueOf()!==t)return n(t.valueOf());throw s("sign",t)}function _(t,n){if(2!=arguments.length)throw o("smaller",arguments.length,2);if(r(t)){if(r(n))return n>t;if(n instanceof e)return f(n)>t}if(t instanceof e){if(r(n))return n>f(t);if(n instanceof e)return f(t)t;if(t instanceof Array||n instanceof Array)return Ot.map2(t,n,_);if(t.valueOf()!==t||n.valueOf()!==n)return _(t.valueOf(),n.valueOf());throw s("smaller",t,n)}function U(t,n){if(2!=arguments.length)throw o("smallereq",arguments.length,2);if(r(t)){if(r(n))return n>=t;if(n instanceof e)return f(n)>=t}if(t instanceof e){if(r(n))return n>=f(t);if(n instanceof e)return f(t)<=f(n)}if(t instanceof a&&n instanceof a){if(!t.equalBase(n))throw Error("Cannot compare units with different base");return t.value<=n.value}if(i(t)||i(n))return n>=t;if(t instanceof Array||n instanceof Array)return Ot.map2(t,n,U);if(t.valueOf()!==t||n.valueOf()!==n)return U(t.valueOf(),n.valueOf());throw s("smallereq",t,n)}function q(t){if(1!=arguments.length)throw o("sqrt",arguments.length,1);if(r(t))return t>=0?Math.sqrt(t):q(new e(t,0));if(t instanceof e){var n=Math.sqrt(t.re*t.re+t.im*t.im);return t.im>=0?new e(.5*Math.sqrt(2*(n+t.re)),.5*Math.sqrt(2*(n-t.re))):new e(.5*Math.sqrt(2*(n+t.re)),-.5*Math.sqrt(2*(n-t.re)))}if(t instanceof Array)return Ot.map(t,q);if(t.valueOf()!==t)return q(t.valueOf());throw s("sqrt",t)}function L(t){if(1!=arguments.length)throw o("square",arguments.length,1);if(r(t))return t*t;if(t instanceof e)return O(t,t);if(t instanceof Array)return O(t,t);if(t.valueOf()!==t)return L(t.valueOf());throw s("square",t)}function C(t,n){if(2!=arguments.length)throw o("subtract",arguments.length,2);if(r(t)){if(r(n))return t-n;if(n instanceof e)return new e(t-n.re,n.im)}else if(t instanceof e){if(r(n))return new e(t.re-n,t.im);if(n instanceof e)return new e(t.re-n.re,t.im-n.im)}else if(t instanceof a&&n instanceof a){if(!t.equalBase(n))throw Error("Units do not match");if(!t.hasValue)throw Error("Unit on left hand side of operator - has no value");if(!n.hasValue)throw Error("Unit on right hand side of operator - has no value");var i=t.clone();return i.value-=n.value,i.fixPrefix=!1,i}if(t instanceof Array||n instanceof Array)return Ot.map2(t,n,C);if(t.valueOf()!==t||n.valueOf()!==n)return C(t.valueOf(),n.valueOf());throw s("subtract",t,n)}function R(t){if(1!=arguments.length)throw o("unaryminus",arguments.length,1);if(r(t))return-t;if(t instanceof e)return new e(-t.re,-t.im);if(t instanceof a){var n=t.clone();return n.value=-t.value,n}if(t instanceof Array)return Ot.map(t,R);if(t.valueOf()!==t)return R(t.valueOf());throw s("unaryminus",t)}function I(t,n){if(2!=arguments.length)throw o("unequal",arguments.length,2);if(r(t)){if(r(n))return t==n;if(n instanceof e)return t==n.re&&0==n.im}if(t instanceof e){if(r(n))return t.re==n&&0==t.im;if(n instanceof e)return t.re==n.re&&t.im==n.im}if(t instanceof a&&n instanceof a){if(!t.equalBase(n))throw Error("Cannot compare units with different base");return t.value==n.value}if(i(t)||i(n))return t==n;if(t instanceof Array||n instanceof Array)return Ot.map2(t,n,I);if(t.valueOf()!==t||n.valueOf()!==n)return I(t.valueOf(),n.valueOf());throw s("unequal",t,n)}function P(t){if(1!=arguments.length)throw o("arg",arguments.length,1);if(r(t))return Math.atan2(0,t);if(t instanceof e)return Math.atan2(t.im,t.re);if(t instanceof Array)return Ot.map(t,P);if(t.valueOf()!==t)return P(t.valueOf());throw s("arg",t)}function B(t){if(1!=arguments.length)throw o("conj",arguments.length,1);if(r(t))return t;if(t instanceof e)return new e(t.re,-t.im);if(t instanceof Array)return Ot.map(t,B);if(t.valueOf()!==t)return B(t.valueOf());throw s("conj",t)}function G(t){if(1!=arguments.length)throw o("im",arguments.length,1);if(r(t))return 0;if(t instanceof e)return t.im;if(t instanceof Array)return Ot.map(t,G);if(t.valueOf()!==t)return G(t.valueOf());throw s("im",t)}function z(t){if(1!=arguments.length)throw o("re",arguments.length,1);if(r(t))return t;if(t instanceof e)return t.re;if(t instanceof Array)return Ot.map(t,z);if(t.valueOf()!==t)return z(t.valueOf());throw s("re",t)}function D(e,t){var i,a,s=arguments.length;if(0>s||s>2)throw o("eye",s,0,2);if(0==s)return 1;if(1==s?(i=e,a=e):2==s&&(i=e,a=t),!r(i)||!n(i)||1>i)throw Error("Parameters in function eye must be positive integers");if(a&&(!r(a)||!n(a)||1>a))throw Error("Parameters in function eye must be positive integers");for(var f=[],u=0;i>u;u++){for(var l=[],h=0;a>h;h++)l[h]=0;f[u]=l}for(var c=Math.min(i,a),p=0;c>p;p++)f[p][p]=1;return f}function Y(t){if(1!=arguments.length)throw o("size",arguments.length,1);if(r(t))return[[1,1]];if(t instanceof e)return[[1,1]];if(t instanceof a)return[[1,1]];if(i(t))return[[1,t.length]];if(t instanceof Array){var n=Ot.array.validatedSize(t);return 1==n.length&&n.push(0),[n]}if(t.valueOf()!==t)return Y(t.valueOf());throw s("size",t)}function V(e){if(1!=arguments.length)throw o("factorial",arguments.length,1);if(r(e)){if(!n(e))throw new TypeError("Function factorial can only handle integer values");var t=e,i=t;for(t--;t>1;)i*=t,t--;return 0==i&&(i=1),i}if(e instanceof Array)return Ot.map(e,V);if(e.valueOf()!==e)return V(e.valueOf());throw s("factorial",e)}function F(){if(0!=arguments.length)throw o("random",arguments.length,0);return Math.random()}function j(e){if(0==arguments.length)throw Error("Function sum requires one or more parameters (0 provided)");if(1==arguments.length&&e.valueOf()instanceof Array)return j.apply(this,e.valueOf());for(var t=arguments[0],r=1,n=arguments.length;n>r;r++){var i=arguments[r];y(i,t)&&(t=i)}return t}function H(e){if(0==arguments.length)throw Error("Function sum requires one or more parameters (0 provided)");if(1==arguments.length&&e.valueOf()instanceof Array)return H.apply(this,e.valueOf());for(var t=arguments[0],r=1,n=arguments.length;n>r;r++){var i=arguments[r];_(i,t)&&(t=i)}return t}function K(t){if(1!=arguments.length)throw o("acos",arguments.length,1);if(r(t))return t>=-1&&1>=t?Math.acos(t):K(new e(t,0));if(t instanceof e){var n=new e(t.im*t.im-t.re*t.re+1,-2*t.re*t.im),i=q(n),a=new e(i.re-t.im,i.im+t.re),f=w(a);return new e(1.5707963267948966-f.im,f.re)}if(t instanceof Array)return Ot.map(t,K);if(t.valueOf()!==t)return K(t.valueOf());throw s("acos",t)}function W(t){if(1!=arguments.length)throw o("asin",arguments.length,1);if(r(t))return t>=-1&&1>=t?Math.asin(t):W(new e(t,0));if(t instanceof e){var n=t.re,i=t.im,a=new e(i*i-n*n+1,-2*n*i),f=q(a),u=new e(f.re-i,f.im+n),l=w(u);return new e(l.im,-l.re)}if(t instanceof Array)return Ot.map(t,W);if(t.valueOf()!==t)return W(t.valueOf());throw s("asin",t)}function X(t){if(1!=arguments.length)throw o("atan",arguments.length,1);if(r(t))return Math.atan(t);if(t instanceof e){var n=t.re,i=t.im,a=n*n+(1-i)*(1-i),f=new e((1-i*i-n*n)/a,-2*n/a),u=w(f);return new e(-.5*u.im,.5*u.re)}if(t instanceof Array)return Ot.map(t,X);if(t.valueOf()!==t)return X(t.valueOf());throw s("atan",t)}function Z(t,n){if(2!=arguments.length)throw o("atan2",arguments.length,2);if(r(t)){if(r(n))return Math.atan2(t,n);if(n instanceof e)return Math.atan2(t,n.re)}else if(t instanceof e){if(r(n))return Math.atan2(t.re,n);if(n instanceof e)return Math.atan2(t.re,n.re)}if(n instanceof Array||t instanceof Array)return Ot.map2(t,n,Z);if(n.valueOf()!==n||t.valueOf()!==t)return Z(t.valueOf(),n.valueOf());throw s("atan2",t,n)}function Q(t){if(1!=arguments.length)throw o("cos",arguments.length,1);if(r(t))return Math.cos(t);if(t instanceof e)return new e(.5*Math.cos(t.re)*(Math.exp(-t.im)+Math.exp(t.im)),.5*Math.sin(t.re)*(Math.exp(-t.im)-Math.exp(t.im)));if(t instanceof a){if(!t.hasBase(a.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.cos(t.value)}if(t instanceof Array)return Ot.map(t,Q);if(t.valueOf()!==t)return Q(t.valueOf());throw s("cos",t)}function J(t){if(1!=arguments.length)throw o("cot",arguments.length,1);if(r(t))return 1/Math.tan(t);if(t instanceof e){var n=Math.exp(-4*t.im)-2*Math.exp(-2*t.im)*Math.cos(2*t.re)+1;return new e(2*Math.exp(-2*t.im)*Math.sin(2*t.re)/n,(Math.exp(-4*t.im)-1)/n)}if(t instanceof a){if(!t.hasBase(a.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cot is no angle");return 1/Math.tan(t.value)}if(t instanceof Array)return Ot.map(t,J);if(t.valueOf()!==t)return J(t.valueOf());throw s("cot",t)}function $(t){if(1!=arguments.length)throw o("csc",arguments.length,1);if(r(t))return 1/Math.sin(t);if(t instanceof e){var n=.25*(Math.exp(-2*t.im)+Math.exp(2*t.im))-.5*Math.cos(2*t.re);return new e(.5*Math.sin(t.re)*(Math.exp(-t.im)+Math.exp(t.im))/n,.5*Math.cos(t.re)*(Math.exp(-t.im)-Math.exp(t.im))/n)}if(t instanceof a){if(!t.hasBase(a.BASE_UNITS.ANGLE))throw new TypeError("Unit in function csc is no angle");return 1/Math.sin(t.value)}if(t instanceof Array)return Ot.map(t,$);if(t.valueOf()!==t)return $(t.valueOf());throw s("csc",t)}function et(t){if(1!=arguments.length)throw o("sec",arguments.length,1);if(r(t))return 1/Math.cos(t);if(t instanceof e){var n=.25*(Math.exp(-2*t.im)+Math.exp(2*t.im))+.5*Math.cos(2*t.re);return new e(.5*Math.cos(t.re)*(Math.exp(-t.im)+Math.exp(t.im))/n,.5*Math.sin(t.re)*(Math.exp(t.im)-Math.exp(-t.im))/n)}if(t instanceof a){if(!t.hasBase(a.BASE_UNITS.ANGLE))throw new TypeError("Unit in function sec is no angle");return 1/Math.cos(t.value)}if(t instanceof Array)return Ot.map(t,et);if(t.valueOf()!==t)return et(t.valueOf());throw s("sec",t)}function tt(t){if(1!=arguments.length)throw o("sin",arguments.length,1);if(r(t))return Math.sin(t);if(t instanceof e)return new e(.5*Math.sin(t.re)*(Math.exp(-t.im)+Math.exp(t.im)),.5*Math.cos(t.re)*(Math.exp(t.im)-Math.exp(-t.im)));if(t instanceof a){if(!t.hasBase(a.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.sin(t.value)}if(t instanceof Array)return Ot.map(t,tt);if(t.valueOf()!==t)return tt(t.valueOf());throw s("sin",t)}function rt(t){if(1!=arguments.length)throw o("tan",arguments.length,1);if(r(t))return Math.tan(t);if(t instanceof e){var n=Math.exp(-4*t.im)+2*Math.exp(-2*t.im)*Math.cos(2*t.re)+1;return new e(2*Math.exp(-2*t.im)*Math.sin(2*t.re)/n,(1-Math.exp(-4*t.im))/n)}if(t instanceof a){if(!t.hasBase(a.BASE_UNITS.ANGLE))throw new TypeError("Unit in function tan is no angle");return Math.tan(t.value)}if(t instanceof Array)return Ot.map(t,rt);if(t.valueOf()!==t)return rt(t.valueOf());throw s("tan",t)}function nt(e,t){if(2!=arguments.length)throw o("in",arguments.length,2);if(e instanceof a&&t instanceof a){if(!e.equalBase(t))throw Error("Units do not match");if(t.hasValue)throw Error("Cannot convert to a unit with a value");if(!t.hasUnit)throw Error("Unit expected on the right hand side of function in");var r=t.clone();return r.value=e.value,r.fixPrefix=!0,r}if(e instanceof Array||t instanceof Array)return Ot.map2(e,t,nt);if(e.valueOf()!==e)return Nt.in(e.valueOf());throw s("in",e)}function it(e,t){var n=arguments.length;if(1!=n&&2!=n)throw o("format",n,1,2);if(1==n){var a=arguments[0];return r(a)?Ot.format(a):a instanceof Array?at(a):i(a)?'"'+a+'"':a instanceof Object?""+a:a+""}if(!i(e))throw new TypeError("String expected as first parameter in function format");if(!(t instanceof Object))throw new TypeError("Object expected as first parameter in function format");return e.replace(/\$([\w\.]+)/g,function(e,r){for(var n=r.split("."),i=t[n.shift()];n.length&&void 0!=i;){var a=n.shift();i=a?i[a]:i+"."}return void 0!=i?i:e})}function at(e){var t="[",r=Ot.array.validatedSize(e);if(2!=r.length)return st(e);for(var n=r[0],i=r[1],a=0;n>a;a++){0!=a&&(t+="; ");for(var s=e[a],o=0;i>o;o++){0!=o&&(t+=", ");var f=s[o];void 0!=f&&(t+=it(f))}}return t+="]"}function st(e){if(e instanceof Array){for(var t="[",r=e.length,n=0;r>n;n++)0!=n&&(t+=", "),t+=st(e[n]);return t+="]"}return it(e)}function ot(e){if(1!=arguments.length)throw o("help",arguments.length,1);if(void 0!=e){if(e.doc)return ft(e.doc);if(e.constructor.doc)return ft(e.constructor.doc);if(i(e)){var t=Nt[e];if(t&&t.doc)return ft(t.doc)}}return e instanceof Object&&e.name?'No documentation found on subject "'+e.name+'"':e instanceof Object&&e.constructor.name?'No documentation found on subject "'+e.constructor.name+'"':'No documentation found on subject "'+e+'"'}function ft(e){var t="";if(e.name&&(t+="NAME\n"+e.name+"\n\n"),e.category&&(t+="CATEGORY\n"+e.category+"\n\n"),e.syntax&&(t+="SYNTAX\n"+e.syntax.join("\n")+"\n\n"),e.examples){var r=new Nt.parser.Parser;t+="EXAMPLES\n";for(var n=0;e.examples.length>n;n++){var i,a=e.examples[n];try{i=r.eval(a)}catch(s){i=s}t+=a+"\n",t+=" "+Nt.format(i)+"\n"}t+="\n"}return e.seealso&&(t+="SEE ALSO\n"+e.seealso.join(", ")+"\n"),t}function ut(e,t){var r;if(i(e)){if("undefined"==typeof require)throw Error("Cannot load file: require not available.");var n=require(e);ut(n)}else if(lt(e)){if(r=e.name,!r)throw Error("Cannot import an unnamed function");(t||void 0===Nt[r])&&(Nt[r]=e)}else if(e instanceof Object)for(r in e)if(e.hasOwnProperty(r)){var a=e[r];lt(a)?(t||void 0===Nt[r])&&(Nt[r]=a):ut(a)}}function lt(t){return"function"==typeof t||r(t)||i(t)||t instanceof e||t instanceof a}function ht(e){if(1!=arguments.length)throw o("typeof",arguments.length,1);var t=typeof e;if("object"==t){if(null==e)return"null";if(e.constructor){for(var r in Nt)if(Nt.hasOwnProperty(r)&&e.constructor==Nt[r])return r.toLowerCase();if(e.constructor.name)return e.constructor.name.toLowerCase()}}return t}function ct(){}function pt(e,t,r){this.name=e,this.fn=t,this.params=r}function mt(e){this.value=e}function vt(e){this.nodes=e||[]}function dt(){this.params=[],this.visible=[]}function gt(e,t,r,n){this.name=e,this.params=t,this.expr=r,this.result=n}function yt(e,t,r,n,i){this.name=e,this.variables=r,this.values=[];for(var a=0,s=this.variables.length;s>a;a++)this.values[a]=function(){var e=function(){return e.value};return e.value=void 0,e}();this.def=this.createFunction(e,t,r,n),this.result=i}function xt(e){this.parentScope=e,this.nestedScopes=void 0,this.symbols={},this.defs={},this.updates={},this.links={}}function wt(){if(this.constructor!=wt)throw new SyntaxError("Parser constructor must be called with the new operator");this.TOKENTYPE={NULL:0,DELIMITER:1,NUMBER:2,SYMBOL:3,UNKNOWN:4},this.expr="",this.index=0,this.c="",this.token="",this.token_type=this.TOKENTYPE.NULL,this.scope=new xt}function Et(){this.idMax=-1,this.updateSeq=0,this.parser=new wt,this.scope=new xt,this.nodes={},this.firstNode=void 0,this.lastNode=void 0}var Nt={parser:{node:{}},options:{precision:10}};"undefined"!=typeof module&&module.exports!==void 0&&(module.exports=Nt),"undefined"!=typeof exports&&(exports=Nt),"undefined"!=typeof require&&"undefined"!=typeof define&&define(function(){return Nt}),"undefined"!=typeof window&&(window.math=Nt);var Ot={};Ot.format=function it(e,t){if(1/0===e)return"Infinity";if(e===-1/0)return"-Infinity";if(0/0===e)return"NaN";var r=Math.abs(e);if(r>1e-4&&1e6>r||0==r)return A(e,t)+"";var n=Math.round(Math.log(r)/Math.LN10),i=e/Math.pow(10,n);return A(i,t)+"E"+n},Ot.randomUUID=function(){var e=function(){return Math.floor(65536*Math.random()).toString(16)};return e()+e()+"-"+e()+"-"+e()+"-"+e()+"-"+e()+e()+e()},Ot.map=function(e,t){if(!e instanceof Array)throw new TypeError("Array expected");return e.map(function(e){return t(e)})},Ot.map2=function(e,t,r){var n,i,a;if(e instanceof Array)if(t instanceof Array){if(e.length!=t.length)throw Error("Dimension mismatch ("+e.length+" != "+t.length+")");for(n=[],i=e.length,a=0;i>a;a++)n[a]=r(e[a],t[a])}else for(n=[],i=e.length,a=0;i>a;a++)n[a]=r(e[a],t);else if(t instanceof Array)for(n=[],i=t.length,a=0;i>a;a++)n[a]=r(e,t[a]);else n=r(e,t);return n},Ot.array={},Ot.array.size=function Y(e){if(e instanceof Array){var t=e.length;if(t){var r=Ot.array.size(e[0]);return[t].concat(r)}return[t]}return[]},Ot.array.validate=function bt(e,t,r){var n,i=e.length;if(r||(r=0),i!=t[r])throw Error("Dimension mismatch ("+i+" != "+t[r]+")");if(t.length-1>r){var a=r+1;for(n=0;i>n;n++){var s=e[n];if(!(s instanceof Array))throw Error("Dimension mismatch ("+(t.length-1)+" < "+t.length+")");bt(e[n],t,a)}}else for(n=0;i>n;n++)if(e[n]instanceof Array)throw Error("Dimension mismatch ("+(t.length+1)+" > "+t.length+")");return!0},Ot.array.validatedSize=function(e){var t=Ot.array.size(e);return Ot.array.validate(e,t),t},Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var t=0;this.length>t;t++)if(this[t]==e)return t;return-1}),Array.prototype.forEach||(Array.prototype.forEach=function(e,t){for(var r=0,n=this.length;n>r;++r)e.call(t||this,this[r],r,this)}),Array.prototype.map||(Array.prototype.map=function(e,t){var r,n,i;if(null==this)throw new TypeError(" this is null or not defined");var a=Object(this),s=a.length>>>0;if("function"!=typeof e)throw new TypeError(e+" is not a function");for(t&&(r=t),n=Array(s),i=0;s>i;){var o,f;i in a&&(o=a[i],f=e.call(r,o,i,a),n[i]=f),i++}return n}),Nt.Complex=e,function(){function t(){for(;" "==h||" "==h;)a()}function r(e){return e>="0"&&"9">=e||"."==e}function n(e){return e>="0"&&"9">=e}function a(){l++,h=u[l]}function s(e){l=e,h=u[l]}function o(){var e="",t=l;if("+"==h?a():"-"==h&&(e+=h,a()),!r(h))return s(t),null;for(;r(h);)e+=h,a();if("E"==h||"e"==h){if(e+=h,a(),("+"==h||"-"==h)&&(e+=h,a()),!n(h))return s(t),null;for(;n(h);)e+=h,a()}return e}function f(){var e=u[l+1];if("I"==h||"i"==h)return a(),"1";if(!("+"!=h&&"-"!=h||"I"!=e&&"i"!=e)){var t="+"==h?"1":"-1";return a(),a(),t}return null}var u,l,h;e.parse=function(r){if(u=r,l=-1,h="",!i(u))return null;a(),t();var n=o();if(n){if("I"==h||"i"==h)return a(),t(),h?null:new e(0,Number(n));t();var s=h;if("+"!=s&&"-"!=s)return t(),h?null:new e(Number(n),0);a(),t();var c=o();if(c){if("I"!=h&&"i"!=h)return null;a()}else if(c=f(),!c)return null;return"-"==s&&(c="-"==c[0]?"+"+c.substring(1):"-"+c),a(),t(),h?null:new e(Number(n),Number(c))}return(n=f())?(t(),h?null:new e(0,Number(n))):null}}(),e.prototype.clone=function(){return new e(this.re,this.im)},e.prototype.toString=function(){var e="";return e=0==this.im?Ot.format(this.re):0==this.re?1==this.im?"i":-1==this.im?"-i":Ot.format(this.im)+"i":this.im>0?1==this.im?Ot.format(this.re)+" + i":Ot.format(this.re)+" + "+Ot.format(this.im)+"i":-1==this.im?Ot.format(this.re)+" - i":Ot.format(this.re)+" - "+Ot.format(Math.abs(this.im))+"i"},e.doc={name:"Complex",category:"type",syntax:["a + bi","a + b * i"],description:"A complex value a + bi, where a is the real part and b is the complex part, and i is the imaginary number defined as sqrt(-1).",examples:["2 + 3i","sqrt(-4)","(1.2 -5i) * 2"],seealso:["abs","arg","conj","im","re"]},Nt.Matrix=t,t.prototype.size=function(){return Ot.array.validatedSize(this.array)},t.prototype.toScalar=function(){for(var e=this.array;e instanceof Array&&1==e.length;)e=e[0];return e instanceof Array?null:e},t.prototype.isScalar=function(){for(var e=this.array;e instanceof Array&&1==e.length;)e=array[0];return!(e instanceof Array)},t.prototype.toVector=function(){var e=Ot.array.validatedSize(this.array);if(2!=e.length)return null;if(1!=e[0]&&1!=e[1])return null;if(1==e[0])return this.array[0].concat();var t=[];return this.array.forEach(function(e,r){t[r]=e[0]}),t},t.prototype.isVector=function(){var e=Ot.array.validatedSize(this.array);return 2!=e.length?!1:1==e[0]||1==e[1]},t.prototype.valueOf=function(){return this.array},Nt.Unit=a,function(){function e(){for(;" "==h||" "==h;)n()}function t(e){return e>="0"&&"9">=e||"."==e}function r(e){return e>="0"&&"9">=e}function n(){l++,h=u[l]}function s(e){l=e,h=u[l]}function o(){var e="",i=l;if("+"==h?n():"-"==h&&(e+=h,n()),!t(h))return s(i),null;for(;t(h);)e+=h,n();if("E"==h||"e"==h){if(e+=h,n(),("+"==h||"-"==h)&&(e+=h,n()),!r(h))return s(i),null;for(;r(h);)e+=h,n()}return e}function f(){var t="";for(e();h&&" "!=h&&" "!=h;)t+=h,n();return t||null}var u,l,h;a.parse=function(t){if(u=t,l=-1,h="",!i(u))return null;n(),e();var r,s=o();return s?(r=f(),n(),e(),h?null:s&&r?new a(Number(s),r):null):(r=f(),n(),e(),h?null:new a(null,r))}}(),a.prototype.clone=function(){var e=new a;for(var t in this)this.hasOwnProperty(t)&&(e[t]=this[t]);return e},a.endsWith=function(e,t){var r=e.length-t.length,n=e.length;return e.substring(r,n)===t},a.prototype._normalize=function(e){return(e+this.unit.offset)*this.unit.value*this.prefix.value},a.prototype._unnormalize=function(e,t){return void 0===t?e/this.unit.value/this.prefix.value-this.unit.offset:e/this.unit.value/t-this.unit.offset},a.isUnit=function(e){for(var t=a.UNITS,r=t.length,n=0;r>n;n++){var i=t[n];if(a.endsWith(e,i.name)){var s=e.length-i.name.length;if(0==s)return!0;var o=e.substring(0,s),f=i.prefixes[o];if(void 0!==f)return!0 +}}return!1},a.prototype.hasBase=function(e){return void 0===this.unit.base?void 0===e:this.unit.base===e},a.prototype.equalBase=function(e){return this.unit.base===e.unit.base},a.prototype.equals=function(e){return this.equalBase(e)&&this.value==e.value},a.prototype.toString=function(){var e;if(this.fixPrefix)return e=this._unnormalize(this.value),Ot.format(e)+" "+this.prefix.name+this.unit.name;var t=Math.abs(this.value/this.unit.value),r=a.PREFIX_NONE,n=Math.abs(Math.log(t/r.value)/Math.LN10-1.2),i=this.unit.prefixes;for(var s in i)if(i.hasOwnProperty(s)){var o=i[s];if(o.scientific){var f=Math.abs(Math.log(t/o.value)/Math.LN10-1.2);n>f&&(r=o,n=f)}}return e=this._unnormalize(this.value,r.value),Ot.format(e)+" "+r.name+this.unit.name},a.PREFIXES={NONE:{"":{name:"",value:1,scientific:!0}},SHORT:{"":{name:"",value:1,scientific:!0},da:{name:"da",value:10,scientific:!1},h:{name:"h",value:100,scientific:!1},k:{name:"k",value:1e3,scientific:!0},M:{name:"M",value:1e6,scientific:!0},G:{name:"G",value:1e9,scientific:!0},T:{name:"T",value:1e12,scientific:!0},P:{name:"P",value:1e15,scientific:!0},E:{name:"E",value:1e18,scientific:!0},Z:{name:"Z",value:1e21,scientific:!0},Y:{name:"Y",value:1e24,scientific:!0},d:{name:"d",value:.1,scientific:!1},c:{name:"c",value:.01,scientific:!1},m:{name:"m",value:.001,scientific:!0},u:{name:"u",value:1e-6,scientific:!0},n:{name:"n",value:1e-9,scientific:!0},p:{name:"p",value:1e-12,scientific:!0},f:{name:"f",value:1e-15,scientific:!0},a:{name:"a",value:1e-18,scientific:!0},z:{name:"z",value:1e-21,scientific:!0},y:{name:"y",value:1e-24,scientific:!0}},LONG:{"":{name:"",value:1,scientific:!0},deca:{name:"deca",value:10,scientific:!1},hecto:{name:"hecto",value:100,scientific:!1},kilo:{name:"kilo",value:1e3,scientific:!0},mega:{name:"mega",value:1e6,scientific:!0},giga:{name:"giga",value:1e9,scientific:!0},tera:{name:"tera",value:1e12,scientific:!0},peta:{name:"peta",value:1e15,scientific:!0},exa:{name:"exa",value:1e18,scientific:!0},zetta:{name:"zetta",value:1e21,scientific:!0},yotta:{name:"yotta",value:1e24,scientific:!0},deci:{name:"deci",value:.1,scientific:!1},centi:{name:"centi",value:.01,scientific:!1},milli:{name:"milli",value:.001,scientific:!0},micro:{name:"micro",value:1e-6,scientific:!0},nano:{name:"nano",value:1e-9,scientific:!0},pico:{name:"pico",value:1e-12,scientific:!0},femto:{name:"femto",value:1e-15,scientific:!0},atto:{name:"atto",value:1e-18,scientific:!0},zepto:{name:"zepto",value:1e-21,scientific:!0},yocto:{name:"yocto",value:1e-24,scientific:!0}},BINARY_SHORT:{"":{name:"",value:1,scientific:!0},k:{name:"k",value:1024,scientific:!0},M:{name:"M",value:Math.pow(1024,2),scientific:!0},G:{name:"G",value:Math.pow(1024,3),scientific:!0},T:{name:"T",value:Math.pow(1024,4),scientific:!0},P:{name:"P",value:Math.pow(1024,5),scientific:!0},E:{name:"E",value:Math.pow(1024,6),scientific:!0},Z:{name:"Z",value:Math.pow(1024,7),scientific:!0},Y:{name:"Y",value:Math.pow(1024,8),scientific:!0},Ki:{name:"Ki",value:1024,scientific:!0},Mi:{name:"Mi",value:Math.pow(1024,2),scientific:!0},Gi:{name:"Gi",value:Math.pow(1024,3),scientific:!0},Ti:{name:"Ti",value:Math.pow(1024,4),scientific:!0},Pi:{name:"Pi",value:Math.pow(1024,5),scientific:!0},Ei:{name:"Ei",value:Math.pow(1024,6),scientific:!0},Zi:{name:"Zi",value:Math.pow(1024,7),scientific:!0},Yi:{name:"Yi",value:Math.pow(1024,8),scientific:!0}},BINARY_LONG:{"":{name:"",value:1,scientific:!0},kilo:{name:"kilo",value:1024,scientific:!0},mega:{name:"mega",value:Math.pow(1024,2),scientific:!0},giga:{name:"giga",value:Math.pow(1024,3),scientific:!0},tera:{name:"tera",value:Math.pow(1024,4),scientific:!0},peta:{name:"peta",value:Math.pow(1024,5),scientific:!0},exa:{name:"exa",value:Math.pow(1024,6),scientific:!0},zetta:{name:"zetta",value:Math.pow(1024,7),scientific:!0},yotta:{name:"yotta",value:Math.pow(1024,8),scientific:!0},kibi:{name:"kibi",value:1024,scientific:!0},mebi:{name:"mebi",value:Math.pow(1024,2),scientific:!0},gibi:{name:"gibi",value:Math.pow(1024,3),scientific:!0},tebi:{name:"tebi",value:Math.pow(1024,4),scientific:!0},pebi:{name:"pebi",value:Math.pow(1024,5),scientific:!0},exi:{name:"exi",value:Math.pow(1024,6),scientific:!0},zebi:{name:"zebi",value:Math.pow(1024,7),scientific:!0},yobi:{name:"yobi",value:Math.pow(1024,8),scientific:!0}}},a.PREFIX_NONE={name:"",value:1,scientific:!0},a.BASE_UNITS={NONE:{},LENGTH:{},MASS:{},TIME:{},CURRENT:{},TEMPERATURE:{},LUMINOUS_INTENSITY:{},AMOUNT_OF_SUBSTANCE:{},FORCE:{},SURFACE:{},VOLUME:{},ANGLE:{},BIT:{}};var Mt=a.BASE_UNITS,Tt=a.PREFIXES;a.BASE_UNIT_NONE={},a.UNIT_NONE={name:"",base:a.BASE_UNIT_NONE,value:1,offset:0},a.UNITS=[{name:"meter",base:Mt.LENGTH,prefixes:Tt.LONG,value:1,offset:0},{name:"inch",base:Mt.LENGTH,prefixes:Tt.NONE,value:.0254,offset:0},{name:"foot",base:Mt.LENGTH,prefixes:Tt.NONE,value:.3048,offset:0},{name:"yard",base:Mt.LENGTH,prefixes:Tt.NONE,value:.9144,offset:0},{name:"mile",base:Mt.LENGTH,prefixes:Tt.NONE,value:1609.344,offset:0},{name:"link",base:Mt.LENGTH,prefixes:Tt.NONE,value:.201168,offset:0},{name:"rod",base:Mt.LENGTH,prefixes:Tt.NONE,value:5.02921,offset:0},{name:"chain",base:Mt.LENGTH,prefixes:Tt.NONE,value:20.1168,offset:0},{name:"angstrom",base:Mt.LENGTH,prefixes:Tt.NONE,value:1e-10,offset:0},{name:"m",base:Mt.LENGTH,prefixes:Tt.SHORT,value:1,offset:0},{name:"ft",base:Mt.LENGTH,prefixes:Tt.NONE,value:.3048,offset:0},{name:"yd",base:Mt.LENGTH,prefixes:Tt.NONE,value:.9144,offset:0},{name:"mi",base:Mt.LENGTH,prefixes:Tt.NONE,value:1609.344,offset:0},{name:"li",base:Mt.LENGTH,prefixes:Tt.NONE,value:.201168,offset:0},{name:"rd",base:Mt.LENGTH,prefixes:Tt.NONE,value:5.02921,offset:0},{name:"ch",base:Mt.LENGTH,prefixes:Tt.NONE,value:20.1168,offset:0},{name:"mil",base:Mt.LENGTH,prefixes:Tt.NONE,value:254e-7,offset:0},{name:"m2",base:Mt.SURFACE,prefixes:Tt.SHORT,value:1,offset:0},{name:"sqin",base:Mt.SURFACE,prefixes:Tt.NONE,value:64516e-8,offset:0},{name:"sqft",base:Mt.SURFACE,prefixes:Tt.NONE,value:.09290304,offset:0},{name:"sqyd",base:Mt.SURFACE,prefixes:Tt.NONE,value:.83612736,offset:0},{name:"sqmi",base:Mt.SURFACE,prefixes:Tt.NONE,value:2589988.110336,offset:0},{name:"sqrd",base:Mt.SURFACE,prefixes:Tt.NONE,value:25.29295,offset:0},{name:"sqch",base:Mt.SURFACE,prefixes:Tt.NONE,value:404.6873,offset:0},{name:"sqmil",base:Mt.SURFACE,prefixes:Tt.NONE,value:6.4516e-10,offset:0},{name:"m3",base:Mt.VOLUME,prefixes:Tt.SHORT,value:1,offset:0},{name:"L",base:Mt.VOLUME,prefixes:Tt.SHORT,value:.001,offset:0},{name:"litre",base:Mt.VOLUME,prefixes:Tt.LONG,value:.001,offset:0},{name:"cuin",base:Mt.VOLUME,prefixes:Tt.NONE,value:16387064e-12,offset:0},{name:"cuft",base:Mt.VOLUME,prefixes:Tt.NONE,value:.028316846592,offset:0},{name:"cuyd",base:Mt.VOLUME,prefixes:Tt.NONE,value:.764554857984,offset:0},{name:"teaspoon",base:Mt.VOLUME,prefixes:Tt.NONE,value:5e-6,offset:0},{name:"tablespoon",base:Mt.VOLUME,prefixes:Tt.NONE,value:15e-6,offset:0},{name:"minim",base:Mt.VOLUME,prefixes:Tt.NONE,value:6.161152e-8,offset:0},{name:"fluiddram",base:Mt.VOLUME,prefixes:Tt.NONE,value:36966911e-13,offset:0},{name:"fluidounce",base:Mt.VOLUME,prefixes:Tt.NONE,value:2957353e-11,offset:0},{name:"gill",base:Mt.VOLUME,prefixes:Tt.NONE,value:.0001182941,offset:0},{name:"cup",base:Mt.VOLUME,prefixes:Tt.NONE,value:.0002365882,offset:0},{name:"pint",base:Mt.VOLUME,prefixes:Tt.NONE,value:.0004731765,offset:0},{name:"quart",base:Mt.VOLUME,prefixes:Tt.NONE,value:.0009463529,offset:0},{name:"gallon",base:Mt.VOLUME,prefixes:Tt.NONE,value:.003785412,offset:0},{name:"beerbarrel",base:Mt.VOLUME,prefixes:Tt.NONE,value:.1173478,offset:0},{name:"oilbarrel",base:Mt.VOLUME,prefixes:Tt.NONE,value:.1589873,offset:0},{name:"hogshead",base:Mt.VOLUME,prefixes:Tt.NONE,value:.238481,offset:0},{name:"fldr",base:Mt.VOLUME,prefixes:Tt.NONE,value:36966911e-13,offset:0},{name:"floz",base:Mt.VOLUME,prefixes:Tt.NONE,value:2957353e-11,offset:0},{name:"gi",base:Mt.VOLUME,prefixes:Tt.NONE,value:.0001182941,offset:0},{name:"cp",base:Mt.VOLUME,prefixes:Tt.NONE,value:.0002365882,offset:0},{name:"pt",base:Mt.VOLUME,prefixes:Tt.NONE,value:.0004731765,offset:0},{name:"qt",base:Mt.VOLUME,prefixes:Tt.NONE,value:.0009463529,offset:0},{name:"gal",base:Mt.VOLUME,prefixes:Tt.NONE,value:.003785412,offset:0},{name:"bbl",base:Mt.VOLUME,prefixes:Tt.NONE,value:.1173478,offset:0},{name:"obl",base:Mt.VOLUME,prefixes:Tt.NONE,value:.1589873,offset:0},{name:"g",base:Mt.MASS,prefixes:Tt.SHORT,value:.001,offset:0},{name:"gram",base:Mt.MASS,prefixes:Tt.LONG,value:.001,offset:0},{name:"ton",base:Mt.MASS,prefixes:Tt.SHORT,value:907.18474,offset:0},{name:"tonne",base:Mt.MASS,prefixes:Tt.SHORT,value:1e3,offset:0},{name:"grain",base:Mt.MASS,prefixes:Tt.NONE,value:6479891e-11,offset:0},{name:"dram",base:Mt.MASS,prefixes:Tt.NONE,value:.0017718451953125,offset:0},{name:"ounce",base:Mt.MASS,prefixes:Tt.NONE,value:.028349523125,offset:0},{name:"poundmass",base:Mt.MASS,prefixes:Tt.NONE,value:.45359237,offset:0},{name:"hundredweight",base:Mt.MASS,prefixes:Tt.NONE,value:45.359237,offset:0},{name:"stick",base:Mt.MASS,prefixes:Tt.NONE,value:.115,offset:0},{name:"gr",base:Mt.MASS,prefixes:Tt.NONE,value:6479891e-11,offset:0},{name:"dr",base:Mt.MASS,prefixes:Tt.NONE,value:.0017718451953125,offset:0},{name:"oz",base:Mt.MASS,prefixes:Tt.NONE,value:.028349523125,offset:0},{name:"lbm",base:Mt.MASS,prefixes:Tt.NONE,value:.45359237,offset:0},{name:"cwt",base:Mt.MASS,prefixes:Tt.NONE,value:45.359237,offset:0},{name:"s",base:Mt.TIME,prefixes:Tt.SHORT,value:1,offset:0},{name:"min",base:Mt.TIME,prefixes:Tt.NONE,value:60,offset:0},{name:"h",base:Mt.TIME,prefixes:Tt.NONE,value:3600,offset:0},{name:"seconds",base:Mt.TIME,prefixes:Tt.LONG,value:1,offset:0},{name:"second",base:Mt.TIME,prefixes:Tt.LONG,value:1,offset:0},{name:"sec",base:Mt.TIME,prefixes:Tt.LONG,value:1,offset:0},{name:"minutes",base:Mt.TIME,prefixes:Tt.NONE,value:60,offset:0},{name:"minute",base:Mt.TIME,prefixes:Tt.NONE,value:60,offset:0},{name:"hours",base:Mt.TIME,prefixes:Tt.NONE,value:3600,offset:0},{name:"hour",base:Mt.TIME,prefixes:Tt.NONE,value:3600,offset:0},{name:"day",base:Mt.TIME,prefixes:Tt.NONE,value:86400,offset:0},{name:"days",base:Mt.TIME,prefixes:Tt.NONE,value:86400,offset:0},{name:"rad",base:Mt.ANGLE,prefixes:Tt.NONE,value:1,offset:0},{name:"deg",base:Mt.ANGLE,prefixes:Tt.NONE,value:.017453292519943295,offset:0},{name:"grad",base:Mt.ANGLE,prefixes:Tt.NONE,value:.015707963267948967,offset:0},{name:"cycle",base:Mt.ANGLE,prefixes:Tt.NONE,value:6.283185307179586,offset:0},{name:"A",base:Mt.CURRENT,prefixes:Tt.SHORT,value:1,offset:0},{name:"ampere",base:Mt.CURRENT,prefixes:Tt.LONG,value:1,offset:0},{name:"K",base:Mt.TEMPERATURE,prefixes:Tt.NONE,value:1,offset:0},{name:"degC",base:Mt.TEMPERATURE,prefixes:Tt.NONE,value:1,offset:273.15},{name:"degF",base:Mt.TEMPERATURE,prefixes:Tt.NONE,value:1/1.8,offset:459.67},{name:"degR",base:Mt.TEMPERATURE,prefixes:Tt.NONE,value:1/1.8,offset:0},{name:"kelvin",base:Mt.TEMPERATURE,prefixes:Tt.NONE,value:1,offset:0},{name:"celsius",base:Mt.TEMPERATURE,prefixes:Tt.NONE,value:1,offset:273.15},{name:"fahrenheit",base:Mt.TEMPERATURE,prefixes:Tt.NONE,value:1/1.8,offset:459.67},{name:"rankine",base:Mt.TEMPERATURE,prefixes:Tt.NONE,value:1/1.8,offset:0},{name:"mol",base:Mt.AMOUNT_OF_SUBSTANCE,prefixes:Tt.NONE,value:1,offset:0},{name:"mole",base:Mt.AMOUNT_OF_SUBSTANCE,prefixes:Tt.NONE,value:1,offset:0},{name:"cd",base:Mt.LUMINOUS_INTENSITY,prefixes:Tt.NONE,value:1,offset:0},{name:"candela",base:Mt.LUMINOUS_INTENSITY,prefixes:Tt.NONE,value:1,offset:0},{name:"N",base:Mt.FORCE,prefixes:Tt.SHORT,value:1,offset:0},{name:"newton",base:Mt.FORCE,prefixes:Tt.LONG,value:1,offset:0},{name:"lbf",base:Mt.FORCE,prefixes:Tt.NONE,value:4.4482216152605,offset:0},{name:"poundforce",base:Mt.FORCE,prefixes:Tt.NONE,value:4.4482216152605,offset:0},{name:"b",base:Mt.BIT,prefixes:Tt.BINARY_SHORT,value:1,offset:0},{name:"bits",base:Mt.BIT,prefixes:Tt.BINARY_LONG,value:1,offset:0},{name:"B",base:Mt.BIT,prefixes:Tt.BINARY_SHORT,value:8,offset:0},{name:"bytes",base:Mt.BIT,prefixes:Tt.BINARY_LONG,value:8,offset:0}],Nt.E=Math.E,Nt.LN2=Math.LN2,Nt.LN10=Math.LN10,Nt.LOG2E=Math.LOG2E,Nt.LOG10E=Math.LOG10E,Nt.PI=Math.PI,Nt.SQRT1_2=Math.SQRT1_2,Nt.SQRT2=Math.SQRT2,Nt.I=new e(0,-1),Nt.pi=Nt.PI,Nt.e=Nt.E,Nt.i=Nt.I,Nt.abs=f,f.doc={name:"abs",category:"Arithmetic",syntax:["abs(x)"],description:"Compute the absolute value.",examples:["abs(3.5)","abs(-4.2)"],seealso:["sign"]},Nt.add=u,u.doc={name:"add",category:"Operators",syntax:["x + y","add(x, y)"],description:"Add two values.",examples:["2.1 + 3.6","ans - 3.6","3 + 2i",'"hello" + " world"',"3 cm + 2 inch"],seealso:["subtract"]},Nt.ceil=l,l.doc={name:"ceil",category:"Arithmetic",syntax:["ceil(x)"],description:"Round a value towards plus infinity.If x is complex, both real and imaginary part are rounded towards plus infinity.",examples:["ceil(3.2)","ceil(3.8)","ceil(-4.2)"],seealso:["floor","fix","round"]},Nt.cube=h,h.doc={name:"cube",category:"Arithmetic",syntax:["cube(x)"],description:"Compute the cube of a value. The cube of x is x * x * x.",examples:["cube(2)","2^3","2 * 2 * 2"],seealso:["multiply","square","pow"]},Nt.divide=c,c.doc={name:"divide",category:"Operators",syntax:["x / y","divide(x, y)"],description:"Divide two values.",examples:["2 / 3","ans * 3","4.5 / 2","3 + 4 / 2","(3 + 4) / 2","18 km / 4.5"],seealso:["multiply"]},Nt.equal=m,m.doc={name:"equal",category:"Operators",syntax:["x == y","equal(x, y)"],description:"Check equality of two values. Returns 1 if the values are equal, and 0 if not.",examples:["2+2 == 3","2+2 == 4","a = 3.2","b = 6-2.8","a == b","50cm == 0.5m"],seealso:["unequal","smaller","larger","smallereq","largereq"]},Nt.exp=v,v.doc={name:"exp",category:"Arithmetic",syntax:["exp(x)"],description:"Calculate the exponent of a value.",examples:["exp(1.3)","e ^ 1.3","log(exp(1.3))","x = 2.4","(exp(i*x) == cos(x) + i*sin(x)) # Euler's formula"],seealso:["square","multiply","log"]},Nt.fix=d,d.doc={name:"fix",category:"Arithmetic",syntax:["fix(x)"],description:"Round a value towards zero.If x is complex, both real and imaginary part are rounded towards zero.",examples:["fix(3.2)","fix(3.8)","fix(-4.2)","fix(-4.8)"],seealso:["ceil","floor","round"]},Nt.floor=g,g.doc={name:"floor",category:"Arithmetic",syntax:["floor(x)"],description:"Round a value towards minus infinity.If x is complex, both real and imaginary part are rounded towards minus infinity.",examples:["floor(3.2)","floor(3.8)","floor(-4.2)"],seealso:["ceil","fix","round"]},Nt.larger=y,y.doc={name:"larger",category:"Operators",syntax:["x > y","larger(x, y)"],description:"Check if value x is larger than y. Returns 1 if x is larger than y, and 0 if not.",examples:["2 > 3","5 > 2*2","a = 3.3","b = 6-2.8","(a > b)","(b < a)","5 cm > 2 inch"],seealso:["equal","unequal","smaller","smallereq","largereq"]},Nt.largereq=x,x.doc={name:"largereq",category:"Operators",syntax:["x >= y","largereq(x, y)"],description:"Check if value x is larger or equal to y. Returns 1 if x is larger or equal to y, and 0 if not.",examples:["2 > 1+1","2 >= 1+1","a = 3.2","b = 6-2.8","(a > b)"],seealso:["equal","unequal","smallereq","smaller","largereq"]},Nt.log=w,w.doc={name:"log",category:"Arithmetic",syntax:["log(x)","log(x, base)"],description:"Compute the logarithm of a value. If no base is provided, the natural logarithm of x is calculated. If base if provided, the logarithm is calculated for the specified base. log(x, base) is defined as log(x) / log(base).",examples:["log(3.5)","a = log(2.4)","exp(a)","10 ^ 3","log(1000, 10)","log(1000) / log(10)","b = logb(1024, 2)","2 ^ b"],seealso:["exp","log10"]},Nt.log10=E,E.doc={name:"log10",category:"Arithmetic",syntax:["log10(x)"],description:"Compute the 10-base logarithm of a value.",examples:["log10(1000)","10 ^ 3","log10(0.01)","log(1000) / log(10)","log(1000, 10)"],seealso:["exp","log"]},Nt.mod=N,N.doc={name:"mod",category:"Operators",syntax:["x % y","x mod y","mod(x, y)"],description:"Calculates the modulus, the remainder of an integer division.",examples:["7 % 3","11 % 2","10 mod 4","function isOdd(x) = x % 2","isOdd(2)","isOdd(3)"],seealso:[]},Nt.multiply=O,O.doc={name:"multiply",category:"Operators",syntax:["x * y","multiply(x, y)"],description:"multiply two values.",examples:["2.1 * 3.6","ans / 3.6","2 * 3 + 4","2 * (3 + 4)","3 * 2.1 km"],seealso:["divide"]},Nt.pow=M,M.doc={name:"pow",category:"Operators",syntax:["x ^ y","pow(x, y)"],description:"Calculates the power of x to y, x^y.",examples:["2^3 = 8","2*2*2","1 + e ^ (pi * i)"],seealso:["unequal","smaller","larger","smallereq","largereq"]},Nt.round=S,S.doc={name:"round",category:"Arithmetic",syntax:["round(x)","round(x, n)"],description:"round a value towards the nearest integer.If x is complex, both real and imaginary part are rounded towards the nearest integer. When n is specified, the value is rounded to n decimals.",examples:["round(3.2)","round(3.8)","round(-4.2)","round(-4.8)","round(pi, 3)","round(123.45678, 2)"],seealso:["ceil","floor","fix"]},Nt.sign=k,k.doc={name:"sign",category:"Arithmetic",syntax:["sign(x)"],description:"Compute the sign of a value. The sign of a value x is 1 when x>1, -1 when x<0, and 0 when x=0.",examples:["sign(3.5)","sign(-4.2)","sign(0)"],seealso:["abs"]},Nt.smaller=_,_.doc={name:"smaller",category:"Operators",syntax:["x < y","smaller(x, y)"],description:"Check if value x is smaller than value y. Returns 1 if x is smaller than y, and 0 if not.",examples:["2 < 3","5 < 2*2","a = 3.3","b = 6-2.8","(a < b)","5 cm < 2 inch"],seealso:["equal","unequal","larger","smallereq","largereq"]},Nt.smallereq=U,U.doc={name:"smallereq",category:"Operators",syntax:["x <= y","smallereq(x, y)"],description:"Check if value x is smaller or equal to value y. Returns 1 if x is smaller than y, and 0 if not.",examples:["2 < 1+1","2 <= 1+1","a = 3.2","b = 6-2.8","(a < b)"],seealso:["equal","unequal","larger","smaller","largereq"]},Nt.sqrt=q,q.doc={name:"sqrt",category:"Arithmetic",syntax:["sqrt(x)"],description:"Compute the square root value. If x = y * y, then y is the square root of x.",examples:["sqrt(25)","5 * 5","sqrt(-1)"],seealso:["square","multiply"]},Nt.square=L,L.doc={name:"square",category:"Arithmetic",syntax:["square(x)"],description:"Compute the square of a value. The square of x is x * x.",examples:["square(3)","sqrt(9)","3^2","3 * 3"],seealso:["multiply","pow","sqrt","cube"]},Nt.subtract=C,C.doc={name:"subtract",category:"Operators",syntax:["x - y","subtract(x, y)"],description:"subtract two values.",examples:["5.3 - 2","ans + 2","2/3 - 1/6","2 * 3 - 3","2.1 km - 500m"],seealso:["add"]},Nt.unaryminus=R,R.doc={name:"unaryminus",category:"Operators",syntax:["-x","unaryminus(x)"],description:"Inverse the sign of a value.",examples:["-4.5","-(-5.6)"],seealso:["add","subtract"]},Nt.unequal=I,I.doc={name:"unequal",category:"Operators",syntax:["x != y","unequal(x, y)"],description:"Check unequality of two values. Returns 1 if the values are unequal, and 0 if they are equal.",examples:["2+2 != 3","2+2 != 4","a = 3.2","b = 6-2.8","a != b","50cm != 0.5m","5 cm != 2 inch"],seealso:["equal","smaller","larger","smallereq","largereq"]},Nt.arg=P,P.doc={name:"arg",category:"Complex",syntax:["arg(x)"],description:"Compute the argument of a complex value. If x = a+bi, the argument is computed as atan2(b, a).",examples:["arg(2 + 2i)","atan2(3, 2)","arg(2 - 3i)"],seealso:["re","im","conj","abs"]},Nt.conj=B,B.doc={name:"conj",category:"Complex",syntax:["conj(x)"],description:"Compute the complex conjugate of a complex value. If x = a+bi, the complex conjugate is a-bi.",examples:["conj(2 + 3i)","conj(2 - 3i)","conj(-5.2i)"],seealso:["re","im","abs","arg"]},Nt.im=G,G.doc={name:"im",category:"Complex",syntax:["im(x)"],description:"Get the imaginary part of a complex number.",examples:["im(2 + 3i)","re(2 + 3i)","im(-5.2i)","im(2.4)"],seealso:["re","conj","abs","arg"]},Nt.re=z,z.doc={name:"re",category:"Complex",syntax:["re(x)"],description:"Get the real part of a complex number.",examples:["re(2 + 3i)","im(2 + 3i)","re(-5.2i)","re(2.4)"],seealso:["im","conj","abs","arg"]},Nt.eye=D,D.doc={name:"eye",category:"Matrix",syntax:["eye(n)","eye(m, n)","eye([m, n])","eye"],description:"Returns the identity matrix with size m-by-n. The matrix has ones on the diagonal and zeros elsewhere.",examples:["eye(3)","eye(3, 5)","a = [1, 2, 3; 4, 5, 6]","eye(size(a))"],seealso:["diag","ones","range","size","transpose","zeros"]},Nt.size=Y,Y.doc={name:"size",category:"Matrix",syntax:["size(x)"],description:"Calculate the size of a matrix.",examples:["size(2.3)",'size("hello world")',"a = [1, 2; 3, 4; 5, 6]","size(a)","size(1:6)"],seealso:["diag","eye","ones","range","transpose","zeros"]},Nt.factorial=V,V.doc={name:"factorial",category:"Probability",syntax:["x!","factorial(x)"],description:"Compute the factorial of a value",examples:["5!","5*4*3*2*1","3!"],seealso:[]},Nt.random=F,F.doc={name:"random",category:"Probability",syntax:["random()"],description:"Return a random number between 0 and 1.",examples:["random()","100 * random()"],seealso:[]},Nt.max=j,j.doc={name:"max",category:"Statistics",syntax:["max(a, b, c, ...)"],description:"Compute the maximum value of a list of values.",examples:["max(2, 3, 4, 1)","max(2.7, 7.1, -4.5, 2.0, 4.1)","min(2.7, 7.1, -4.5, 2.0, 4.1)"],seealso:["sum","prod","avg","var","std","min","median"]},Nt.min=H,H.doc={name:"min",category:"Statistics",syntax:["min(a, b, c, ...)"],description:"Compute the minimum value of a list of values.",examples:["max(2, 3, 4, 1)","max(2.7, 7.1, -4.5, 2.0, 4.1)","min(2.7, 7.1, -4.5, 2.0, 4.1)"],seealso:["sum","prod","avg","var","std","min","median"]},Nt.acos=K,K.doc={name:"acos",category:"Trigonometry",syntax:["acos(x)"],description:"Compute the inverse cosine of a value in radians.",examples:["acos(0.5)","acos(cos(2.3))"],seealso:["cos","acos","asin"]},Nt.asin=W,W.doc={name:"asin",category:"Trigonometry",syntax:["asin(x)"],description:"Compute the inverse sine of a value in radians.",examples:["asin(0.5)","asin(sin(2.3))"],seealso:["sin","acos","asin"]},Nt.atan=X,X.doc={name:"atan",category:"Trigonometry",syntax:["atan(x)"],description:"Compute the inverse tangent of a value in radians.",examples:["atan(0.5)","atan(tan(2.3))"],seealso:["tan","acos","asin"]},Nt.atan2=Z,Z.doc={name:"atan2",category:"Trigonometry",syntax:["atan2(y, x)"],description:"Computes the principal value of the arc tangent of y/x in radians.",examples:["atan2(2, 2) / pi","angle = 60 deg in rad","x = cos(angle)","y = sin(angle)","atan2(y, x)"],seealso:["sin","cos","tan"]},Nt.cos=Q,Q.doc={name:"cos",category:"Trigonometry",syntax:["cos(x)"],description:"Compute the cosine of x in radians.",examples:["cos(2)","cos(pi / 4) ^ 2","cos(180 deg)","cos(60 deg)","sin(0.2)^2 + cos(0.2)^2"],seealso:["acos","sin","tan"]},Nt.cot=J,J.doc={name:"cot",category:"Trigonometry",syntax:["cot(x)"],description:"Compute the cotangent of x in radians. Defined as 1/tan(x)",examples:["cot(2)","1 / tan(2)"],seealso:["sec","csc","tan"]},Nt.csc=$,$.doc={name:"csc",category:"Trigonometry",syntax:["csc(x)"],description:"Compute the cosecant of x in radians. Defined as 1/sin(x)",examples:["csc(2)","1 / sin(2)"],seealso:["sec","cot","sin"]},Nt.sec=et,et.doc={name:"sec",category:"Trigonometry",syntax:["sec(x)"],description:"Compute the secant of x in radians. Defined as 1/cos(x)",examples:["sec(2)","1 / cos(2)"],seealso:["cot","csc","cos"]},Nt.sin=tt,tt.doc={name:"sin",category:"Trigonometry",syntax:["sin(x)"],description:"Compute the sine of x in radians.",examples:["sin(2)","sin(pi / 4) ^ 2","sin(90 deg)","sin(30 deg)","sin(0.2)^2 + cos(0.2)^2"],seealso:["asin","cos","tan"]},Nt.tan=rt,rt.doc={name:"tan",category:"Trigonometry",syntax:["tan(x)"],description:"Compute the tangent of x in radians.",examples:["tan(0.5)","sin(0.5) / cos(0.5)","tan(pi / 4)","tan(45 deg)"],seealso:["atan","sin","cos"]},Nt.in=nt,nt.doc={name:"in",category:"Units",syntax:["x in unit","in(x, unit)"],description:"Change the unit of a value.",examples:["5 inch in cm","3.2kg in g","16 bytes in bits"],seealso:[]},Nt.format=it,it.doc={name:"format",category:"Utils",syntax:["format(value)"],description:"Format a value of any type as string.",examples:["format(2.3)","format(3 - 4i)","format([])"],seealso:[]},Nt.help=ot,ot.doc={name:"help",category:"Utils",syntax:["help(object)"],description:"Display documentation on a function or data type.",examples:['help("sqrt")','help("Complex")'],seealso:[]},Nt["import"]=ut,ut.doc={name:"import",category:"Utils",syntax:["import(string)"],description:"Import functions from a file.",examples:['import("numbers")','import("./mylib.js")'],seealso:[]},Nt["typeof"]=ht,ht.doc={name:"typeof",category:"Utils",syntax:["typeof(x)"],description:"Get the type of a variable.",examples:["typeof(3.5)","typeof(2 - 4i)","typeof(45 deg)",'typeof("hello world")'],seealso:[]},Nt.parser.node.Node=ct,ct.prototype.eval=function(){throw Error("Cannot evaluate a Node interface")},ct.prototype.toString=function(){return""},pt.prototype=new ct,Nt.parser.node.Symbol=pt,pt.prototype.hasParams=function(){return void 0!=this.params&&this.params.length>0},pt.prototype.eval=function(){var e=this.fn;if(void 0===e)throw Error("Undefined symbol "+this.name);var t=this.params.map(function(e){return e.eval()});return e.apply(this,t)},pt.prototype.toString=function(){if(this.name&&!this.params)return this.name;var e=this.name;return this.params&&this.params.length&&(e+="("+this.params.join(", ")+")"),e},mt.prototype=new ct,Nt.parser.node.Constant=mt,mt.prototype.eval=function(){return this.value},mt.prototype.toString=function(){return this.value?Nt.format(this.value):""},vt.prototype=new ct,Nt.parser.node.ArrayNode=vt,function(){function e(t){return t.map(function(t){return t instanceof Array?e(t):t.eval()})}function t(e){if(e instanceof Array){for(var r="[",n=e.length,i=0;n>i;i++)0!=i&&(r+=", "),r+=t(e[i]);return r+="]"}return""+e}vt.prototype.eval=function(){return e(this.nodes)},vt.prototype.toString=function(){return t(this.nodes)}}(),dt.prototype=new ct,Nt.parser.node.Block=dt,dt.prototype.add=function(e,t){var r=this.params.length;this.params[r]=e,this.visible[r]=void 0!=t?t:!0},dt.prototype.eval=function(){for(var e=[],t=0,r=this.params.length;r>t;t++){var n=this.params[t].eval();this.visible[t]&&e.push(n)}return e},dt.prototype.toString=function(){for(var e=[],t=0,r=this.params.length;r>t;t++)this.visible[t]&&e.push("\n "+(""+this.params[t]));return"["+e.join(",")+"\n]"},gt.prototype=new ct,Nt.parser.node.Assignment=gt,gt.prototype.eval=function(){if(void 0===this.expr)throw Error("Undefined symbol "+this.name);var e,t=this.params;if(t&&t.length){var r=[];this.params.forEach(function(e){r.push(e.eval())});var n=this.expr.eval();if(void 0==this.result.value)throw Error("Undefined symbol "+this.name);var i=this.result.eval();e=i.set(r,n),this.result.value=e}else e=this.expr.eval(),this.result.value=e;return e},gt.prototype.toString=function(){var e="";return e+=this.name,this.params&&this.params.length&&(e+="("+this.params.join(", ")+")"),e+=" = ",e+=""+this.expr},yt.prototype=new ct,Nt.parser.node.FunctionAssignment=yt,yt.prototype.createFunction=function(e,t,r,n){var i=function(){var t=r?r.length:0,i=arguments?arguments.length:0;if(t!=i)throw o(e,i,t);if(t>0)for(var a=0;t>a;a++)r[a].value=arguments[a];return n.eval()};return i.toString=function(){return e+"("+t.join(", ")+")"},i},yt.prototype.eval=function(){for(var e=this.variables,t=this.values,r=0,n=e.length;n>r;r++)e[r].value=t[r];return this.result.value=this.def,this.def},yt.prototype.toString=function(){return""+this.def},Nt.parser.node.Scope=xt,xt.prototype.createNestedScope=function(){var e=new xt(this);return this.nestedScopes||(this.nestedScopes=[]),this.nestedScopes.push(e),e},xt.prototype.clear=function(){if(this.symbols={},this.defs={},this.links={},this.updates={},this.nestedScopes)for(var e=this.nestedScopes,t=0,r=e.length;r>t;t++)e[t].clear()},xt.prototype.createSymbol=function(e){var t=this.symbols[e];if(!t){var r=this.findDef(e);t=this.newSymbol(e,r),this.symbols[e]=t}return t},xt.prototype.newSymbol=function(e,t){var r=this,n=function(){if(!n.value&&(n.value=r.findDef(e),!n.value))throw Error("Undefined symbol "+e);return"function"==typeof n.value?n.value.apply(null,arguments):n.value};return n.value=t,n.toString=function(){return n.value?""+n.value:""},n},xt.prototype.createLink=function(e){var t=this.links[e];return t||(t=this.createSymbol(e),this.links[e]=t),t},xt.prototype.createDef=function(e,t){var r=this.defs[e];return r||(r=this.createSymbol(e),this.defs[e]=r),r&&void 0!=t&&(r.value=t),r},xt.prototype.createUpdate=function(e){var t=this.updates[e];return t||(t=this.createLink(e),this.updates[e]=t),t},xt.prototype.findDef=function(t){function r(e,t){var r=i(e,t);return s[e]=r,o[e]=r,r}var n;if(n=this.defs[t])return n;if(n=this.updates[t])return n;if(this.parentScope)return this.parentScope.findDef(t);var i=this.newSymbol,s=this.symbols,o=this.defs;if("pi"==t)return r(t,Nt.PI);if("e"==t)return r(t,Nt.E);if("i"==t)return r(t,new e(0,1));var f=Nt[t];if(f)return r(t,f);if(a.isUnit(t)){var u=new a(null,t);return r(t,u)}return void 0},xt.prototype.removeLink=function(e){delete this.links[e]},xt.prototype.removeDef=function(e){delete this.defs[e]},xt.prototype.removeUpdate=function(e){delete this.updates[e]},xt.prototype.init=function(){var e=this.symbols,t=this.parentScope;for(var r in e)if(e.hasOwnProperty(r)){var n=e[r];n.value=t?t.findDef(r):void 0}this.nestedScopes&&this.nestedScopes.forEach(function(e){e.init()})},xt.prototype.hasLink=function(e){if(this.links[e])return!0;if(this.nestedScopes)for(var t=this.nestedScopes,r=0,n=t.length;n>r;r++)if(t[r].hasLink(e))return!0;return!1},xt.prototype.hasDef=function(e){return void 0!=this.defs[e]},xt.prototype.hasUpdate=function(e){return void 0!=this.updates[e]},xt.prototype.getUndefinedSymbols=function(){var e=this.symbols,t=[];for(var r in e)if(e.hasOwnProperty(r)){var n=e[r];void 0==n.value&&t.push(n)}return this.nestedScopes&&this.nestedScopes.forEach(function(e){t=t.concat(e.getUndefinedSymbols())}),t},Nt.parser.Parser=wt,wt.prototype.parse=function(e,t){return this.expr=e||"",t||(this.newScope(),t=this.scope),this.parse_start(t)},wt.prototype.eval=function(e){var t=this.parse(e);return t.eval()},wt.prototype.get=function(e){this.newScope();var t=this.scope.findDef(e);return t?t.value:void 0},wt.prototype.put=function(e,t){this.scope.createDef(e,t)},wt.prototype.newScope=function(){this.scope=new xt(this.scope)},wt.prototype.clear=function(){this.scope.clear()},wt.prototype.getChar=function(){this.index++,this.c=this.expr.charAt(this.index)},wt.prototype.getFirstChar=function(){this.index=0,this.c=this.expr.charAt(0)},wt.prototype.getToken=function(){for(this.token_type=this.TOKENTYPE.NULL,this.token="";" "==this.c||" "==this.c;)this.getChar();if("#"==this.c)for(;"\n"!=this.c&&""!=this.c;)this.getChar();if(""==this.c)return this.token_type=this.TOKENTYPE.DELIMITER,void 0;if("-"==this.c||","==this.c||"("==this.c||")"==this.c||"["==this.c||"]"==this.c||'"'==this.c||"\n"==this.c||";"==this.c||":"==this.c)return this.token_type=this.TOKENTYPE.DELIMITER,this.token+=this.c,this.getChar(),void 0;if(this.isDelimiter(this.c))for(this.token_type=this.TOKENTYPE.DELIMITER;this.isDelimiter(this.c);)this.token+=this.c,this.getChar();else if(this.isDigitDot(this.c)){for(this.token_type=this.TOKENTYPE.NUMBER;this.isDigitDot(this.c);)this.token+=this.c,this.getChar();if("E"==this.c||"e"==this.c)for(this.token+=this.c,this.getChar(),("+"==this.c||"-"==this.c)&&(this.token+=this.c,this.getChar()),this.isDigit(this.c)||(this.token_type=this.TOKENTYPE.UNKNOWN);this.isDigit(this.c);)this.token+=this.c,this.getChar()}else{if(!this.isAlpha(this.c)){for(this.token_type=this.TOKENTYPE.UNKNOWN;""!=this.c;)this.token+=this.c,this.getChar();throw this.createSyntaxError('Syntax error in part "'+this.token+'"')}for(this.token_type=this.TOKENTYPE.SYMBOL;this.isAlpha(this.c)||this.isDigit(this.c);)this.token+=this.c,this.getChar()}},wt.prototype.isDelimiter=function(e){return"&"==e||"|"==e||"<"==e||">"==e||"="==e||"+"==e||"/"==e||"*"==e||"%"==e||"^"==e||","==e||";"==e||"\n"==e||"!"==e +},wt.prototype.isValidSymbolName=function(e){for(var t=0,r=e.length;r>t;t++){var n=e.charAt(t),i=this.isAlpha(n);if(!i)return!1}return!0},wt.prototype.isAlpha=function(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e||"_"==e},wt.prototype.isDigitDot=function(e){return e>="0"&&"9">=e||"."==e},wt.prototype.isDigit=function(e){return e>="0"&&"9">=e},wt.prototype.parse_start=function(e){this.getFirstChar(),this.getToken();var t;if(t=""==this.token?new mt(void 0):this.parse_block(e),""!=this.token)throw this.token_type==this.TOKENTYPE.DELIMITER?this.createError("Unknown operator "+this.token):this.createSyntaxError('Unexpected part "'+this.token+'"');return t},wt.prototype.parse_ans=function(e){var t=this.parse_function_assignment(e);if(!(t instanceof gt)){var r="ans",n=void 0,i=e.createDef(r);return new gt(r,n,t,i)}return t},wt.prototype.parse_block=function(e){var t,r,n;for("\n"!=this.token&&";"!=this.token&&""!=this.token&&(t=this.parse_ans(e));"\n"==this.token||";"==this.token;)r||(r=new dt,t&&(n=";"!=this.token,r.add(t,n))),this.getToken(),"\n"!=this.token&&";"!=this.token&&""!=this.token&&(t=this.parse_ans(e),n=";"!=this.token,r.add(t,n));return r?r:(t||(t=this.parse_ans(e)),t)},wt.prototype.parse_function_assignment=function(e){if(this.token_type==this.TOKENTYPE.SYMBOL&&"function"==this.token){if(this.getToken(),this.token_type!=this.TOKENTYPE.SYMBOL)throw this.createSyntaxError("Function name expected");var t=this.token;if(this.getToken(),"("!=this.token)throw this.createSyntaxError("Opening parenthesis ( expected");for(var r=e.createNestedScope(),n=[],i=[];;){if(this.getToken(),this.token_type!=this.TOKENTYPE.SYMBOL)throw this.createSyntaxError("Variable name expected");var a=this.token,s=r.createDef(a);if(n.push(a),i.push(s),this.getToken(),","!=this.token){if(")"==this.token)break;throw this.createSyntaxError('Comma , or closing parenthesis ) expected"')}}if(this.getToken(),"="!=this.token)throw this.createSyntaxError("Equal sign = expected");this.getToken();var o=this.parse_range(r),f=e.createDef(t);return new yt(t,n,i,o,f)}return this.parse_assignment(e)},wt.prototype.parse_assignment=function(e){var t=!1;this.token_type==this.TOKENTYPE.SYMBOL&&(t=e.hasLink(this.token));var r=this.parse_range(e);if("="==this.token){if(!(r instanceof pt))throw this.createSyntaxError("Symbol expected at the left hand side of assignment operator =");var n=r.name,i=r.params;t||e.removeLink(n),this.getToken();var a=this.parse_range(e),s=r.hasParams()?e.createUpdate(n):e.createDef(n);return new gt(n,i,a,s)}return r},wt.prototype.parse_range=function(e){var t=this.parse_conditions(e);return t},wt.prototype.parse_conditions=function(e){for(var t=this.parse_bitwise_conditions(e),r={"in":"in"};void 0!==r[this.token];){var n=this.token,i=Nt[r[n]];this.getToken();var a=[t,this.parse_bitwise_conditions(e)];t=new pt(n,i,a)}return t},wt.prototype.parse_bitwise_conditions=function(e){var t=this.parse_comparison(e);return t},wt.prototype.parse_comparison=function(e){for(var t=this.parse_addsubtract(e),r={"==":"equal","!=":"unequal","<":"smaller",">":"larger","<=":"smallereq",">=":"largereq"};void 0!==r[this.token];){var n=this.token,i=Nt[r[n]];this.getToken();var a=[t,this.parse_addsubtract(e)];t=new pt(n,i,a)}return t},wt.prototype.parse_addsubtract=function(e){for(var t=this.parse_multiplydivide(e),r={"+":"add","-":"subtract"};void 0!==r[this.token];){var n=this.token,i=Nt[r[n]];this.getToken();var a=[t,this.parse_multiplydivide(e)];t=new pt(n,i,a)}return t},wt.prototype.parse_multiplydivide=function(e){for(var t=this.parse_pow(e),r={"*":"multiply","/":"divide","%":"mod",mod:"mod"};void 0!==r[this.token];){var n=this.token,i=Nt[r[n]];this.getToken();var a=[t,this.parse_pow(e)];t=new pt(n,i,a)}return t},wt.prototype.parse_pow=function(e){for(var t=this.parse_factorial(e);"^"==this.token;){var r=this.token,n=M;this.getToken();var i=[t,this.parse_factorial(e)];t=new pt(r,n,i)}return t},wt.prototype.parse_factorial=function(e){for(var t=this.parse_unaryminus(e);"!"==this.token;){var r=this.token,n=V;this.getToken();var i=[t];t=new pt(r,n,i)}return t},wt.prototype.parse_unaryminus=function(e){if("-"==this.token){var t=this.token,r=R;this.getToken();var n=[this.parse_plot(e)];return new pt(t,r,n)}return this.parse_plot(e)},wt.prototype.parse_plot=function(e){return this.parse_symbol(e)},wt.prototype.parse_symbol=function(e){if(this.token_type==this.TOKENTYPE.SYMBOL){var t=this.token;this.getToken();var r=e.createLink(t),n=this.parse_arguments(e),i=new pt(t,r,n);return i}return this.parse_string(e)},wt.prototype.parse_arguments=function(e){var t=[];if("("==this.token){if(this.getToken(),")"!=this.token)for(t.push(this.parse_range(e));","==this.token;)this.getToken(),t.push(this.parse_range(e));if(")"!=this.token)throw this.createSyntaxError("Parenthesis ) missing");this.getToken()}return t},wt.prototype.parse_string=function(e){if('"'==this.token){for(var t="",r="";""!=this.c&&('"'!=this.c||"\\"==r);)t+=this.c,r=this.c,this.getChar();if(this.getToken(),'"'!=this.token)throw this.createSyntaxError('End of string " missing');this.getToken();var n=new mt(t);return n}return this.parse_matrix(e)},wt.prototype.parse_matrix=function(e){if("["==this.token){var t;for(this.getToken();"\n"==this.token;)this.getToken();if("]"!=this.token){var r=[],n=0,i=0;for(r[0]=[this.parse_range(e)];","==this.token||";"==this.token;){for(","==this.token?i++:(n++,i=0,r[n]=[]),this.getToken();"\n"==this.token;)this.getToken();for(r[n][i]=this.parse_range(e);"\n"==this.token;)this.getToken()}var a=r.length,s=r.length>0?r[0].length:0;for(n=1;a>n;n++)if(r[n].length!=s)throw this.createError("Number of columns must match ("+r[n].length+" != "+s+")");if("]"!=this.token)throw this.createSyntaxError("End of matrix ] missing");this.getToken(),t=new vt(r)}else this.getToken(),t=new vt([]);for(;"("==this.token;)t=this.parse_arguments(e,t);return t}return this.parse_number(e)},wt.prototype.parse_number=function(t){if(this.token_type==this.TOKENTYPE.NUMBER){var r;r="."==this.token?0:Number(this.token),this.getToken();var n;if(this.token_type==this.TOKENTYPE.SYMBOL){if("i"==this.token||"I"==this.token)return n=new e(0,r),this.getToken(),new mt(n);if(a.isUnit(this.token))return n=new a(r,this.token),this.getToken(),new mt(n);throw this.createTypeError('Unknown unit "'+this.token+'"')}var i=new mt(r);return i}return this.parse_parentheses(t)},wt.prototype.parse_parentheses=function(e){if("("==this.token){this.getToken();var t=this.parse_range(e);if(")"!=this.token)throw this.createSyntaxError("Parenthesis ) expected");return this.getToken(),t}return this.parse_end(e)},wt.prototype.parse_end=function(){throw""==this.token?this.createSyntaxError("Unexpected end of expression"):this.createSyntaxError("Value expected")},wt.prototype.row=function(){return void 0},wt.prototype.col=function(){return this.index-this.token.length+1},wt.prototype.createErrorMessage=function(e){var t=this.row(),r=this.col();return void 0===t?void 0===r?e:e+" (col "+r+")":e+" (ln "+t+", col "+r+")"},wt.prototype.createSyntaxError=function(e){return new SyntaxError(this.createErrorMessage(e))},wt.prototype.createTypeError=function(e){return new TypeError(this.createErrorMessage(e))},wt.prototype.createError=function(e){return Error(this.createErrorMessage(e))},Nt.parser.Workspace=Et,Et.prototype.clear=function(){this.nodes={},this.firstNode=void 0,this.lastNode=void 0},Et.prototype.append=function(e){var t=this._getNewId(),r=this.lastNode?this.lastNode.scope:this.scope,n=new xt(r),i=new Et.Node({id:t,expression:e,parser:this.parser,scope:n,nextNode:void 0,previousNode:this.lastNode});return this.nodes[t]=i,this.firstNode||(this.firstNode=i),this.lastNode&&(this.lastNode.nextNode=i),this.lastNode=i,this._update([t]),t},Et.prototype.insertBefore=function(e,t){var r=this.nodes[t];if(!r)throw'Node with id "'+t+'" not found';var n=r.previousNode,i=this._getNewId(),a=n?n.scope:this.scope,s=new xt(a),o=new Et.Node({id:i,expression:e,parser:this.parser,scope:s,nextNode:r,previousNode:n});this.nodes[i]=o,n?n.nextNode=o:this.firstNode=o,r.previousNode=o,r.scope.parentScope=o.scope;var f=this.getDependencies(i);return-1==f.indexOf(i)&&f.unshift(i),this._update(f),i},Et.prototype.insertAfter=function(e,t){var r=this.nodes[t];if(!r)throw'Node with id "'+t+'" not found';return r==this.lastNode?this.append(e):this.insertBefore(t+1,e)},Et.prototype.remove=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';var r=this.getDependencies(e),n=t.previousNode,i=t.nextNode;n?n.nextNode=i:this.firstNode=i,i?i.previousNode=n:this.lastNode=n;var a=n?n.scope:this.scope;i&&(i.scope.parentScope=a),delete this.nodes[e],this._update(r)},Et.prototype.replace=function(e,t){var r=this.nodes[t];if(!r)throw'Node with id "'+t+'" not found';var n=[t];Et._merge(n,this.getDependencies(t));var i=r.previousNode;r.nextNode,i?i.scope:this.scope,r.setExpr(e),Et._merge(n,this.getDependencies(t)),this._update(n)},Et.Node=function(e){this.id=e.id,this.parser=e.parser,this.scope=e.scope,this.nextNode=e.nextNode,this.previousNode=e.previousNode,this.updateSeq=0,this.result=void 0,this.setExpr(e.expression)},Et.Node.prototype.setExpr=function(e){this.expression=e||"",this.scope.clear(),this._parse()},Et.Node.prototype.getExpr=function(){return this.expression},Et.Node.prototype.getResult=function(){return this.result},Et.Node.prototype._parse=function(){try{this.fn=this.parser.parse(this.expression,this.scope)}catch(e){var t="Error: "+((e.message||e)+"");this.fn=new mt(t)}},Et.Node.prototype.eval=function(){try{this.scope.init(),this.result=this.fn.eval()}catch(e){this.scope.init(),this.result="Error: "+((e.message||e)+"")}return this.result},Et._merge=function(e,t){for(var r=0,n=t.length;n>r;r++){var i=t[r];-1==e.indexOf(i)&&e.push(i)}},Et.prototype.getDependencies=function(e){var t,r=[],n=this.nodes[e];if(n){var i=n.scope.defs,a=n.scope.updates,s=[];for(t in i)i.hasOwnProperty(t)&&s.push(t);for(t in a)a.hasOwnProperty(t)&&-1==s.indexOf(t)&&s.push(t);for(var o=n.nextNode;o&&s.length;){for(var f=o.scope,u=0;s.length>u;){if(t=s[u],(f.hasLink(t)||f.hasUpdate(t))&&-1==r.indexOf(o.id)){r.push(o.id);var l=this.getDependencies(o.id);Et._merge(r,l)}f.hasDef(t)&&(s.splice(u,1),u--),u++}o=o.nextNode}}return r},Et.prototype.getExpr=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';return t.getExpr()},Et.prototype.getResult=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';return t.getResult()},Et.prototype._update=function(e){this.updateSeq++;for(var t=this.updateSeq,r=this.nodes,n=0,i=e.length;i>n;n++){var a=e[n],s=r[a];s&&(s.eval(),s.updateSeq=t)}},Et.prototype.getChanges=function(e){var t=[],r=this.firstNode;for(e=e||0;r;)r.updateSeq>e&&t.push(r.id),r=r.nextNode;return{ids:t,updateSeq:this.updateSeq}},Et.prototype._getNewId=function(){return this.idMax++,this.idMax},Et.prototype.toString=function(){return JSON.stringify(this.toJSON())},Et.prototype.toJSON=function(){for(var e=[],t=this.firstNode;t;){var r={id:t.id,expression:t.expression,dependencies:this.getDependencies(t.id)};try{r.result=t.getResult()}catch(n){r.result="Error: "+((n.message||n)+"")}e.push(r),t=t.nextNode}return e}})(); \ No newline at end of file diff --git a/src/type/Matrix.js b/src/type/Matrix.js new file mode 100644 index 000000000..d0cad0c5b --- /dev/null +++ b/src/type/Matrix.js @@ -0,0 +1,110 @@ +/** + * @constructor Matrix + * + * TODO: document Matrix + * + * @param {Array} [array] A multi dimensional array + */ +function Matrix(array) { + if (this.constructor != Matrix) { + throw new SyntaxError( + 'Matrix constructor must be called with the new operator'); + } + + this.array = array || []; +} + +math.Matrix = Matrix; + +// TODO: implement a parse method + +// TODO: implement method toVector +// TODO: implement method isVector + + + +/** + * Retrieve the size of the matrix. + * The size of the matrix will be validated too + * @returns {Number[]} size + */ +Matrix.prototype.size = function () { + return util.array.validatedSize(this.array); +}; + +/** + * Get the scalar value of the matrix. Will return null if the matrix is no + * scalar value + * @return {* | null} scalar + */ +Matrix.prototype.toScalar = function () { + var value = this.array; + while (value instanceof Array && value.length == 1) { + value = value[0]; + } + + if (value instanceof Array) { + return null; + } + else { + return value; + } +}; + +/** + * Test whether the matrix is a scalar. + * @return {boolean} isScalar + */ +Matrix.prototype.isScalar = function () { + var value = this.array; + while (value instanceof Array && value.length == 1) { + value = array[0]; + } + return !(value instanceof Array); +}; + +/** + * Get the matrix contents as vector. Returns null if the Matrix is no vector + * return {Array} vector + */ +Matrix.prototype.toVector = function () { + var s = util.array.validatedSize(this.array); + if (s.length != 2) { + return null; + } + if (s[0] != 1 && s[1] != 1) { + return null; + } + + if (s[0] == 1) { + return this.array[0].concat(); + } + else { + var vector = []; + this.array.forEach(function (row, index) { + vector[index] = row[0]; + }); + return vector; + } +}; + +/** + * Test if the matrix is a vector. + * A matrix is a vector when the dims is [1 x n] or [n x 1] + * return {boolean} isVector + */ +Matrix.prototype.isVector = function () { + var s = util.array.validatedSize(this.array); + if (s.length != 2) { + return false; + } + return (s[0] == 1 || s[1] == 1); +}; + +/** + * Get the primitive value of the Matrix: a multidimensional array + * @returns {Array} array + */ +Matrix.prototype.valueOf = function () { + return this.array; +}; diff --git a/test/all.js b/test/all.js index 0c0feef80..a0ec043c6 100644 --- a/test/all.js +++ b/test/all.js @@ -7,6 +7,7 @@ // test data types require('./type/complex.js'); require('./type/unit.js'); +require('./type/matrix.js'); // test functions require('./function/arithmetic.js'); diff --git a/test/type/matrix.js b/test/type/matrix.js new file mode 100644 index 000000000..b8f16eeef --- /dev/null +++ b/test/type/matrix.js @@ -0,0 +1,9 @@ +// test data type Matrix + +var assert = require('assert'); +var math = require('../../math.js'), + Complex = math.Complex, + Matrix = math.Matrix, + Unit = math.Unit; + +// TODO: extensively test Matrix