mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
37 lines
57 KiB
JavaScript
37 lines
57 KiB
JavaScript
/**
|
|
* math.js
|
|
* https://github.com/josdejong/mathjs
|
|
*
|
|
* Math.js is an extensive math library for JavaScript and Node.js,
|
|
* compatible with JavaScript's built-in Math library.
|
|
*
|
|
* Features:
|
|
* - A flexible expression parser
|
|
* - Support for numbers, complex numbers, units, strings, arrays*,
|
|
* and matrices*
|
|
* - A large set of built-in functions and constants
|
|
* - Easily extensible with new functions and constants
|
|
* - Powerful and easy to use
|
|
*
|
|
* * Note: arrays and matrices are to be implemented.
|
|
*
|
|
* @version 2013-02-25
|
|
* @date 0.2.0-SNAPSHOT
|
|
*
|
|
* @license
|
|
* Copyright (C) 2013 Jos de Jong <wjosdejong@gmail.com>
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
* use this file except in compliance with the License. You may obtain a copy
|
|
* of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
* License for the specific language governing permissions and limitations under
|
|
* the License.
|
|
*/
|
|
undefined(function(){function e(t,n){if(this.constructor!=e)throw Error("Unit constructor must be called with the new operator");this.value=1,this.unit=e.UNIT_NONE,this.prefix=e.PREFIX_NONE,this.hasUnit=!1,this.hasValue=!1,this.fixPrefix=!1,this._init(t,n)}function t(e){return e instanceof String||"string"==typeof e}function n(e){return e instanceof Number||"number"==typeof e}function i(e){return e==Math.round(e)}function r(e,i){if(this.constructor!=r)throw new SyntaxError("Complex constructor must be called with the new operator");switch(arguments.length){case 2:if(!n(e)||!n(i))throw new TypeError("Two numbers or a single string expected in Complex constructor");this.re=e,this.im=i;break;case 1:if(!t(e))throw new TypeError("Two numbers or a single string expected in Complex constructor");var s,o=[],f="+",u=e.lastIndexOf(f);-1==u&&(f="-",u=e.lastIndexOf(f)),-1!=u?(s=a(e.substring(0,u)),s&&o.push(s),s=a(e.substring(u+1)),s&&o.push(f+s)):(s=a(e),s&&o.push(s));var h=!1;switch(o.length){case 1:s=o[0],"I"==s[s.length-1].toUpperCase()?(this.re=0,this.im=Number(s.substring(0,s.length-1)),h=!isNaN(this.im)):(this.re=Number(s),this.im=0,h=!isNaN(this.re));break;case 2:s=o[0],this.re=Number(o[0]),this.im=Number(o[1].substring(0,o[1].length-1)),h=!isNaN(this.re)&&!isNaN(this.im)&&"I"==o[1][o[1].length-1].toUpperCase()}if(!h)throw new SyntaxError('Invalid value "'+e+'"');break;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 a(e){return e.replace(/^\s+|\s+$/g,"")}function s(e,t){var n=void 0;if(2==arguments.length){var i=c(t);n="Function "+e+" does not support a parameter of type "+i}else if(arguments.length>2){for(var r=[],a=1;arguments.length>a;a++)r.push(c(arguments[a]));n="Function "+e+" does not support a parameters of type "+r.join(", ")}else n="Unsupported parameter in function "+e;return new TypeError(n)}function o(e,t,n,i){var r="Wrong number of arguments in function "+e+" ("+t+" provided, "+n+(void 0!=i?"-"+i:"")+" expected)";return new SyntaxError(r)}function f(e,n){var i;if(t(e)){if("undefined"==typeof require)throw Error("Cannot load file: require not available.");var r=require(e);f(r)}else if(u(e)){if(i=e.name,!i)throw Error("Cannot import an unnamed function");(n||void 0===ct[i])&&(ct[i]=e)}else if(e instanceof Object)for(i in e)if(e.hasOwnProperty(i)){var a=e[i];u(a)?(n||void 0===ct[i])&&(ct[i]=a):f(a)}}function u(i){return"function"==typeof i||n(i)||t(i)||i instanceof r||i instanceof e}function h(e){if(1!=arguments.length)throw o("help",arguments.length,1);if(void 0!=e){if(e.doc)return l(e.doc);if(e.constructor.doc)return l(e.constructor.doc);if(t(e)){var n=ct[e];if(n&&n.doc)return l(n.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 l(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){t+="EXAMPLES\n";for(var n=0;e.examples.length>n;n++)t+=e.examples[n]+"\n";t+="\n"}return e.seealso&&(t+="SEE ALSO\n"+e.seealso.join(", ")+"\n"),t}function c(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&&e.constructor&&e.constructor.name)return e.constructor.name.toLowerCase()}return t}function m(){if(0==arguments.length)throw Error("Function sum requires one or more parameters (0 provided)");for(var e=arguments[0],t=1,n=arguments.length;n>t;t++){var i=arguments[t];K(i,e)&&(e=i)}return p}function p(){if(0==arguments.length)throw Error("Function sum requires one or more parameters (0 provided)");for(var e=arguments[0],t=1,n=arguments.length;n>t;t++){var i=arguments[t];F(i,e)&&(e=i)}return e}function g(t,n){if(2!=arguments.length)throw o("in",arguments.length,2);if(t instanceof e){if(n.hasValue)throw Error("Cannot convert to a unit with a value");if(!n.hasUnit)throw Error("Unit expected on the right hand side of function in");var i=n.copy();return i.value=t.value,i.fixPrefix=!0,i}throw s("in",t)}function v(t){if(1!=arguments.length)throw o("sin",arguments.length,1);if(n(t))return Math.sin(t);if(t instanceof r)return new r(.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 e){if(!t.hasBase(e.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.sin(t.value)}throw s("sin",t)}function d(e,t){if(2!=arguments.length)throw o("atan2",arguments.length,2);if(n(e)){if(n(t))return Math.atan2(e,t);if(t instanceof r)return Math.atan2(e,t.re)}else if(e instanceof r){if(n(t))return Math.atan2(e.re,t);if(t instanceof r)return Math.atan2(e.re,t.re)}throw s("atan2",e,t)}function x(e){if(1!=arguments.length)throw o("asin",arguments.length,1);if(n(e))return e>=-1&&1>=e?Math.asin(e):x(new r(e,0));if(e instanceof r){var t=e.re,i=e.im,a=new r(i*i-t*t+1,-2*t*i),f=V(a),u=new r(f.re-i,f.im+t),h=W(u);return new r(h.im,-h.re)}throw s("asin",e)}function y(e){if(1!=arguments.length)throw o("atan",arguments.length,1);if(n(e))return Math.atan(e);if(e instanceof r){var t=e.re,i=e.im,a=t*t+(1-i)*(1-i),f=new r((1-i*i-t*t)/a,-2*t/a),u=W(f);return new r(-.5*u.im,.5*u.re)}throw s("atan",e)}function E(t){if(1!=arguments.length)throw o("cos",arguments.length,1);if(n(t))return Math.cos(t);if(t instanceof r)return new r(.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 e){if(!t.hasBase(e.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.cos(t.value)}throw s("cos",t)}function w(t){if(1!=arguments.length)throw o("tan",arguments.length,1);if(n(t))return Math.tan(t);if(t instanceof r){var i=Math.exp(-4*t.im)+2*Math.exp(-2*t.im)*Math.cos(2*t.re)+1;return new r(2*Math.exp(-2*t.im)*Math.sin(2*t.re)/i,(1-Math.exp(-4*t.im))/i)}if(t instanceof e){if(!t.hasBase(e.BASE_UNITS.ANGLE))throw new TypeError("Unit in function tan is no angle");return Math.tan(t.value)}throw s("tan",t)}function N(e){if(1!=arguments.length)throw o("acos",arguments.length,1);if(n(e))return e>=-1&&1>=e?Math.acos(e):N(new r(e,0));if(e instanceof r){var t=new r(e.im*e.im-e.re*e.re+1,-2*e.re*e.im),i=V(t),a=new r(i.re-e.im,i.im+e.re),f=W(a);return new r(1.5707963267948966-f.im,f.re)}throw s("acos",e)}function b(e){if(1!=arguments.length)throw o("arg",arguments.length,1);if(n(e))return Math.atan2(0,e);if(e instanceof r)return Math.atan2(e.im,e.re);throw s("arg",e)}function M(e){if(1!=arguments.length)throw o("conj",arguments.length,1);if(n(e))return e;if(e instanceof r)return new r(e.re,-e.im);throw s("conj",e)}function O(e){if(1!=arguments.length)throw o("im",arguments.length,1);if(n(e))return 0;if(e instanceof r)return e.im;throw s("im",e)}function T(e){if(1!=arguments.length)throw o("re",arguments.length,1);if(n(e))return e;if(e instanceof r)return e.re;throw s("re",e)}function S(e){if(1!=arguments.length)throw o("cube",arguments.length,1);if(n(e))return e*e*e;if(e instanceof r)return J(J(e,e),e);throw s("cube",e)}function k(t,i){if(2!=arguments.length)throw o("divide",arguments.length,2);if(n(t)){if(n(i))return t/i;if(i instanceof r)return U(new r(t,0),i)}else if(t instanceof r){if(n(i))return U(t,new r(i,0));if(i instanceof r)return U(t,i)}else if(t instanceof e&&n(i)){var a=t.copy();return a.value/=i,a}throw s("divide",t,i)}function U(e,t){var n=t.re*t.re+t.im*t.im;return new r((e.re*t.re+e.im*t.im)/n,(e.im*t.re-e.re*t.im)/n)}function _(i,a){if(2!=arguments.length)throw o("smallereq",arguments.length,2);if(n(i)){if(n(a))return a>=i;if(a instanceof r)return z(a)>=i}if(i instanceof r){if(n(a))return a>=z(i);if(a instanceof r)return z(i)<=z(a)}if(i instanceof e&&a instanceof e){if(!i.equalBase(a))throw Error("Cannot compare units with different base");return i.value<=a.value}if(t(i)||t(a))return a>=i;throw s("smallereq",i,a)}function q(e,t){if(1!=arguments.length&&2!=arguments.length)throw o("round",arguments.length,1,2);if(void 0==t){if(n(e))return Math.round(e);if(e instanceof r)return new r(Math.round(e.re),Math.round(e.im));throw s("round",e)}if(!n(t))throw new TypeError("Number of digits in function round must be an integer");if(t!==Math.round(t))throw new TypeError("Number of digits in function round must be integer");if(0>t||t>9)throw Error("Number of digits in function round must be in te range of 0-9");if(n(e))return L(e,t);if(e instanceof r)return new r(L(e.re,t),L(e.im,t));throw s("round",e,t)}function L(e,t){var n=Math.pow(10,void 0!=t?t:ct.options.precision);return Math.round(e*n)/n}function A(i,a){if(2!=arguments.length)throw o("equal",arguments.length,2);if(n(i)){if(n(a))return i==a;if(a instanceof r)return i==a.re&&0==a.im}if(i instanceof r){if(n(a))return i.re==a&&0==i.im;if(a instanceof r)return i.re==a.re&&i.im==a.im}if(i instanceof e&&a instanceof e){if(!i.equalBase(a))throw Error("Cannot compare units with different base");return i.value==a.value}if(t(i)||t(a))return i==a;throw s("equal",i,a)}function C(i,a){if(2!=arguments.length)throw o("unequal",arguments.length,2);if(n(i)){if(n(a))return i==a;if(a instanceof r)return i==a.re&&0==a.im}if(i instanceof r){if(n(a))return i.re==a&&0==i.im;if(a instanceof r)return i.re==a.re&&i.im==a.im}if(i instanceof e&&a instanceof e){if(!i.equalBase(a))throw Error("Cannot compare units with different base");return i.value==a.value}if(t(i)||t(a))return i==a;throw s("unequal",i,a)}function R(e){if(1!=arguments.length)throw o("log10",arguments.length,1);if(n(e))return e>=0?Math.log(e)/Math.LN10:R(new r(e,0));if(e instanceof r)return new r(Math.log(Math.sqrt(e.re*e.re+e.im*e.im))/Math.LN10,Math.atan2(e.im,e.re)/Math.LN10);throw s("log10",e)}function I(e){if(1!=arguments.length)throw o("fix",arguments.length,1);if(n(e))return value>0?Math.floor(e):Math.ceil(e);if(e instanceof r)return new r(e.re>0?Math.floor(e.re):Math.ceil(e.re),e.im>0?Math.floor(e.im):Math.ceil(e.im));throw s("fix",e)}function P(i,a){if(2!=arguments.length)throw o("add",arguments.length,2);if(n(i)){if(n(a))return i+a;if(a instanceof r)return new r(i+a.re,a.im)}else if(i instanceof r){if(n(a))return new r(i.re+a,i.im);if(a instanceof r)return new r(i.re+a.re,i.im+a.im)}else if(i instanceof e&&a instanceof e){if(!i.equalBase(a))throw Error("Units do not match");if(!i.hasValue)throw Error("Unit on left hand side of operator + has no value");if(!a.hasValue)throw Error("Unit on right hand side of operator + has no value");var f=i.copy();return f.value+=a.value,f.fixPrefix=!1,f}if(t(i)||t(a))return i+a;throw s("add",i,a)}function G(e,t){if(2!=arguments.length)throw o("mod",arguments.length,2);if(n(e)){if(n(t))return e%t;if(t instanceof r&&0==t.im)return e%t.re}else if(e instanceof r&&0==e.im){if(n(t))return e.re%t;if(t instanceof r&&0==t.im)return e.re%t.re}throw s("mod",e,t)}function B(e){if(1!=arguments.length)throw o("exp",arguments.length,1);if(n(e))return Math.exp(e);if(e instanceof r){var t=Math.exp(e.re);return new r(t*Math.cos(e.im),t*Math.sin(e.im))}throw s("exp",e)}function Y(i,a){if(2!=arguments.length)throw o("largereq",arguments.length,2);if(n(i)){if(n(a))return i>=a;if(a instanceof r)return i>=z(a)}if(i instanceof r){if(n(a))return z(i)>=a;if(a instanceof r)return z(i)>=z(a)}if(i instanceof e&&a instanceof e){if(!i.equalBase(a))throw Error("Cannot compare units with different base");return i.value>=a.value}if(t(i)||t(a))return i>=a;throw s("largereq",i,a)}function V(e){if(1!=arguments.length)throw o("sqrt",arguments.length,1);if(n(e))return e>=0?Math.sqrt(e):V(new r(e,0));if(e instanceof r){var t=Math.sqrt(e.re*e.re+e.im*e.im);return e.im>=0?new r(.5*Math.sqrt(2*(t+e.re)),.5*Math.sqrt(2*(t-e.re))):new r(.5*Math.sqrt(2*(t+e.re)),-.5*Math.sqrt(2*(t-e.re)))}throw s("sqrt",e)}function D(e){if(1!=arguments.length)throw o("square",arguments.length,1);if(n(e))return e*e;if(e instanceof r)return J(e,e);throw s("square",e)}function F(i,a){if(2!=arguments.length)throw o("larger",arguments.length,2);if(n(i)){if(n(a))return i>a;if(a instanceof r)return i>z(a)}if(i instanceof r){if(n(a))return z(i)>a;if(a instanceof r)return z(i)>z(a)}if(i instanceof e&&a instanceof e){if(!i.equalBase(a))throw Error("Cannot compare units with different base");return i.value>a.value}if(t(i)||t(a))return i>a;throw s("larger",i,a)}function H(e){if(1!=arguments.length)throw o("sign",arguments.length,1);if(n(e)){var t;return t=e>0?1:0>e?-1:0}if(e instanceof r){var i=Math.sqrt(e.re*e.re+e.im*e.im);return new r(e.re/i,e.im/i)}throw s("sign",e)}function j(t){if(1!=arguments.length)throw o("unaryminus",arguments.length,1);if(n(t))return-t;if(t instanceof r)return new r(-t.re,-t.im);if(t instanceof e){var i=t.copy();return i.value=-t.value,i}throw s("unaryminus",t)}function K(i,a){if(2!=arguments.length)throw o("smaller",arguments.length,2);if(n(i)){if(n(a))return a>i;if(a instanceof r)return z(a)>i}if(i instanceof r){if(n(a))return a>z(i);if(a instanceof r)return z(i)<z(a)}if(i instanceof e&&a instanceof e){if(!i.equalBase(a))throw Error("Cannot compare units with different base");return i.value<a.value}if(t(i)||t(a))return a>i;throw s("smaller",i,a)}function z(e){if(1!=arguments.length)throw o("abs",arguments.length,1);if(n(e))return Math.abs(e);if(e instanceof r)return Math.sqrt(e.re*e.re+e.im*e.im);throw s("abs",e)}function W(e,t){if(1!=arguments.length&&2!=arguments.length)throw o("log",arguments.length,1,2);if(void 0!==t)return k(W(e),W(t));if(n(e))return e>=0?Math.log(e):W(new r(e,0));if(e instanceof r)return new r(Math.log(Math.sqrt(e.re*e.re+e.im*e.im)),Math.atan2(e.im,e.re));throw s("log",e,t)}function X(e,t){if(2!=arguments.length)throw o("pow",arguments.length,2);if(n(e)){if(n(t))return i(t)||e>=0?Math.pow(e,t):Z(new r(e,0),new r(t,0));if(t instanceof r)return Z(new r(e,0),t)}else if(e instanceof r){if(n(t))return Z(e,new r(t,0));if(t instanceof r)return Z(e,t)}throw s("pow",e,t)}function Z(e,t){var n=W(e),i=J(n,t);return B(i)}function Q(e){if(1!=arguments.length)throw o("floor",arguments.length,1);if(n(e))return Math.floor(e);if(e instanceof r)return new r(Math.floor(e.re),Math.floor(e.im));throw s("floor",e)}function $(e){if(1!=arguments.length)throw o("ceil",arguments.length,1);if(n(e))return Math.ceil(e);if(e instanceof r)return new r(Math.ceil(e.re),Math.ceil(e.im));throw s("ceil",e)}function J(t,i){var a;if(2!=arguments.length)throw o("multiply",arguments.length,2);if(n(t)){if(n(i))return t*i;if(i instanceof r)return et(new r(t,0),i);if(i instanceof e)return a=i.copy(),a.value*=t,a}else if(t instanceof r){if(n(i))return et(t,new r(i,0));if(i instanceof r)return et(t,i)}else if(t instanceof e&&n(i))return a=t.copy(),a.value*=i,a;throw s("multiply",t,i)}function et(e,t){return new r(e.re*t.re-e.im*t.im,e.re*t.im+e.im*t.re)}function tt(t,i){if(2!=arguments.length)throw o("subtract",arguments.length,2);if(n(t)){if(n(i))return t-i;if(i instanceof r)return new r(t-i.re,i.im)}else if(t instanceof r){if(n(i))return new r(t.re-i,t.im);if(i instanceof r)return new r(t.re-i.re,t.im-i.im)}else if(t instanceof e&&i instanceof e){if(!t.equalBase(i))throw Error("Units do not match");if(!t.hasValue)throw Error("Unit on left hand side of operator - has no value");if(!i.hasValue)throw Error("Unit on right hand side of operator - has no value");var a=t.copy();return a.value-=i.value,a.fixPrefix=!1,a}throw s("subtract",t,i)}function nt(){if(0!=arguments.length)throw o("random",arguments.length,0);return Math.random()}function it(e){if(1!=arguments.length)throw o("factorial",arguments.length,1);if(n(e)){if(!i(e))throw new TypeError("Function factorial can only handle integer values");var t=e,r=t;for(t--;t>1;)r*=t,t--;return 0==r&&(r=1),r}throw s("factorial",e)}function rt(){}function at(e,t,n){this.name=e,this.fn=t,this.params=n}function st(e){this.value=e}function ot(){this.params=[],this.visible=[]}function ft(e,t,n,i){this.name=e,this.params=t,this.expr=n,this.result=i}function ut(e,t,n,i,r){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,i),this.result=r}function ht(e){this.parentScope=e,this.nestedScopes=void 0,this.symbols={},this.defs={},this.updates={},this.links={}}function lt(){if(this.constructor!=lt)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 ht}var ct={parser:{node:{}},options:{precision:10}};"undefined"!=typeof module&&module.exports!==void 0&&(module.exports=ct),"undefined"!=typeof exports&&(exports=ct),"undefined"!=typeof require&&"undefined"!=typeof define&&define(function(){return ct}),"undefined"!=typeof window&&(window.math=ct);var mt={};if(mt.format=function(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 L(e,t)+"";var i=Math.round(Math.log(n)/Math.LN10),r=e/Math.pow(10,i);return L(r,t)+"E"+i},mt.randomUUID=function(){var e=function(){return Math.floor(65536*Math.random()).toString(16)};return e()+e()+"-"+e()+"-"+e()+"-"+e()+"-"+e()+e()+e()},!Array.prototype.indexOf){Array.prototype.indexOf=function(e){for(var t=0;this.length>t;t++)if(this[t]==e)return t;return-1};try{console.log("Warning: Ancient browser detected. Please update your browser")}catch(pt){}}Array.prototype.forEach||(Array.prototype.forEach=function(e,t){for(var n=0,i=this.length;i>n;++n)e.call(t||this,this[n],n,this)}),Array.prototype.map||(Array.prototype.map=function(e,t){var n,i,r;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),i=Array(s),r=0;s>r;){var o,f;r in a&&(o=a[r],f=e.call(n,o,r,a),i[r]=f),r++}return i}),ct.Unit=e,e.prototype.copy=function(){var t=new e;for(var n in this)this.hasOwnProperty(n)&&(t[n]=this[n]);return t},e.endsWith=function(e,t){var n=e.length-t.length,i=e.length;return e.substring(n,i)===t},e.prototype._init=function(t,n){if(void 0!==n){for(var i=e.UNITS,r=!1,a=0,s=i.length;s>a;a++){var o=i[a];if(e.endsWith(n,o.name)){var f=n.length-o.name.length,u=n.substring(0,f),h=o.prefixes[u];if(void 0!==h){this.unit=o,this.prefix=h,this.hasUnit=!0,r=!0;break}}}if(!r)throw Error('String "'+n+'" is no unit')}void 0!==t?(this.value=this._normalize(t),this.hasValue=!0):this.value=this._normalize(1)},e.prototype._normalize=function(e){return(e+this.unit.offset)*this.unit.value*this.prefix.value},e.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},e.isUnit=function(t){for(var n=e.UNITS,i=n.length,r=0;i>r;r++){var a=n[r];if(e.endsWith(t,a.name)){var s=t.length-a.name.length;if(0==s)return!0;var o=t.substring(0,s),f=a.prefixes[o];if(void 0!==f)return!0}}return!1},e.prototype.hasBase=function(e){return void 0===this.unit.base?void 0===e:this.unit.base===e},e.prototype.equalBase=function(e){return this.unit.base===e.unit.base},e.prototype.equals=function(e){return this.equalBase(e)&&this.value==e.value},e.prototype.toString=function(){var t;if(this.fixPrefix)return t=this._unnormalize(this.value),mt.format(t)+" "+this.prefix.name+this.unit.name;var n=e.PREFIX_NONE,i=Math.abs(Math.log(this.value/n.value)/Math.LN10-1.5),r=this.unit.prefixes;for(var a in r)if(r.hasOwnProperty(a)){var s=r[a];if(s.scientific){var o=Math.abs(Math.log(this.value/s.value)/Math.LN10-1.5);i>o&&(n=s,i=o)}}return t=this._unnormalize(this.value,n.value),mt.format(t)+" "+n.name+this.unit.name},e.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}}},e.PREFIX_NONE={name:"",value:1,scientific:!0},e.BASE_UNITS={NONE:{},LENGTH:{},MASS:{},TIME:{},CURRENT:{},TEMPERATURE:{},LUMINOUS_INTENSITY:{},AMOUNT_OF_SUBSTANCE:{},FORCE:{},SURFACE:{},VOLUME:{},ANGLE:{},BIT:{}};var gt=e.BASE_UNITS,vt=e.PREFIXES;e.BASE_UNIT_NONE={},e.UNIT_NONE={name:"",base:e.BASE_UNIT_NONE,value:1,offset:0},e.UNITS=[{name:"meter",base:gt.LENGTH,prefixes:vt.LONG,value:1,offset:0},{name:"inch",base:gt.LENGTH,prefixes:vt.NONE,value:.0254,offset:0},{name:"foot",base:gt.LENGTH,prefixes:vt.NONE,value:.3048,offset:0},{name:"yard",base:gt.LENGTH,prefixes:vt.NONE,value:.9144,offset:0},{name:"mile",base:gt.LENGTH,prefixes:vt.NONE,value:1609.344,offset:0},{name:"link",base:gt.LENGTH,prefixes:vt.NONE,value:.201168,offset:0},{name:"rod",base:gt.LENGTH,prefixes:vt.NONE,value:5.02921,offset:0},{name:"chain",base:gt.LENGTH,prefixes:vt.NONE,value:20.1168,offset:0},{name:"angstrom",base:gt.LENGTH,prefixes:vt.NONE,value:1e-10,offset:0},{name:"m",base:gt.LENGTH,prefixes:vt.SHORT,value:1,offset:0},{name:"ft",base:gt.LENGTH,prefixes:vt.NONE,value:.3048,offset:0},{name:"yd",base:gt.LENGTH,prefixes:vt.NONE,value:.9144,offset:0},{name:"mi",base:gt.LENGTH,prefixes:vt.NONE,value:1609.344,offset:0},{name:"li",base:gt.LENGTH,prefixes:vt.NONE,value:.201168,offset:0},{name:"rd",base:gt.LENGTH,prefixes:vt.NONE,value:5.02921,offset:0},{name:"ch",base:gt.LENGTH,prefixes:vt.NONE,value:20.1168,offset:0},{name:"mil",base:gt.LENGTH,prefixes:vt.NONE,value:254e-7,offset:0},{name:"m2",base:gt.SURFACE,prefixes:vt.SHORT,value:1,offset:0},{name:"sqin",base:gt.SURFACE,prefixes:vt.NONE,value:64516e-8,offset:0},{name:"sqft",base:gt.SURFACE,prefixes:vt.NONE,value:.09290304,offset:0},{name:"sqyd",base:gt.SURFACE,prefixes:vt.NONE,value:.83612736,offset:0},{name:"sqmi",base:gt.SURFACE,prefixes:vt.NONE,value:2589988.110336,offset:0},{name:"sqrd",base:gt.SURFACE,prefixes:vt.NONE,value:25.29295,offset:0},{name:"sqch",base:gt.SURFACE,prefixes:vt.NONE,value:404.6873,offset:0},{name:"sqmil",base:gt.SURFACE,prefixes:vt.NONE,value:6.4516e-10,offset:0},{name:"m3",base:gt.VOLUME,prefixes:vt.SHORT,value:1,offset:0},{name:"L",base:gt.VOLUME,prefixes:vt.SHORT,value:.001,offset:0},{name:"litre",base:gt.VOLUME,prefixes:vt.LONG,value:.001,offset:0},{name:"cuin",base:gt.VOLUME,prefixes:vt.NONE,value:16387064e-12,offset:0},{name:"cuft",base:gt.VOLUME,prefixes:vt.NONE,value:.028316846592,offset:0},{name:"cuyd",base:gt.VOLUME,prefixes:vt.NONE,value:.764554857984,offset:0},{name:"teaspoon",base:gt.VOLUME,prefixes:vt.NONE,value:5e-6,offset:0},{name:"tablespoon",base:gt.VOLUME,prefixes:vt.NONE,value:15e-6,offset:0},{name:"minim",base:gt.VOLUME,prefixes:vt.NONE,value:6.161152e-8,offset:0},{name:"fluiddram",base:gt.VOLUME,prefixes:vt.NONE,value:36966911e-13,offset:0},{name:"fluidounce",base:gt.VOLUME,prefixes:vt.NONE,value:2957353e-11,offset:0},{name:"gill",base:gt.VOLUME,prefixes:vt.NONE,value:.0001182941,offset:0},{name:"cup",base:gt.VOLUME,prefixes:vt.NONE,value:.0002365882,offset:0},{name:"pint",base:gt.VOLUME,prefixes:vt.NONE,value:.0004731765,offset:0},{name:"quart",base:gt.VOLUME,prefixes:vt.NONE,value:.0009463529,offset:0},{name:"gallon",base:gt.VOLUME,prefixes:vt.NONE,value:.003785412,offset:0},{name:"beerbarrel",base:gt.VOLUME,prefixes:vt.NONE,value:.1173478,offset:0},{name:"oilbarrel",base:gt.VOLUME,prefixes:vt.NONE,value:.1589873,offset:0},{name:"hogshead",base:gt.VOLUME,prefixes:vt.NONE,value:.238481,offset:0},{name:"fldr",base:gt.VOLUME,prefixes:vt.NONE,value:36966911e-13,offset:0},{name:"floz",base:gt.VOLUME,prefixes:vt.NONE,value:2957353e-11,offset:0},{name:"gi",base:gt.VOLUME,prefixes:vt.NONE,value:.0001182941,offset:0},{name:"cp",base:gt.VOLUME,prefixes:vt.NONE,value:.0002365882,offset:0},{name:"pt",base:gt.VOLUME,prefixes:vt.NONE,value:.0004731765,offset:0},{name:"qt",base:gt.VOLUME,prefixes:vt.NONE,value:.0009463529,offset:0},{name:"gal",base:gt.VOLUME,prefixes:vt.NONE,value:.003785412,offset:0},{name:"bbl",base:gt.VOLUME,prefixes:vt.NONE,value:.1173478,offset:0},{name:"obl",base:gt.VOLUME,prefixes:vt.NONE,value:.1589873,offset:0},{name:"g",base:gt.MASS,prefixes:vt.SHORT,value:.001,offset:0},{name:"gram",base:gt.MASS,prefixes:vt.LONG,value:.001,offset:0},{name:"ton",base:gt.MASS,prefixes:vt.SHORT,value:907.18474,offset:0},{name:"tonne",base:gt.MASS,prefixes:vt.SHORT,value:1e3,offset:0},{name:"grain",base:gt.MASS,prefixes:vt.NONE,value:6479891e-11,offset:0},{name:"dram",base:gt.MASS,prefixes:vt.NONE,value:.0017718451953125,offset:0},{name:"ounce",base:gt.MASS,prefixes:vt.NONE,value:.028349523125,offset:0},{name:"poundmass",base:gt.MASS,prefixes:vt.NONE,value:.45359237,offset:0},{name:"hundredweight",base:gt.MASS,prefixes:vt.NONE,value:45.359237,offset:0},{name:"stick",base:gt.MASS,prefixes:vt.NONE,value:.115,offset:0},{name:"gr",base:gt.MASS,prefixes:vt.NONE,value:6479891e-11,offset:0},{name:"dr",base:gt.MASS,prefixes:vt.NONE,value:.0017718451953125,offset:0},{name:"oz",base:gt.MASS,prefixes:vt.NONE,value:.028349523125,offset:0},{name:"lbm",base:gt.MASS,prefixes:vt.NONE,value:.45359237,offset:0},{name:"cwt",base:gt.MASS,prefixes:vt.NONE,value:45.359237,offset:0},{name:"s",base:gt.TIME,prefixes:vt.SHORT,value:1,offset:0},{name:"min",base:gt.TIME,prefixes:vt.NONE,value:60,offset:0},{name:"h",base:gt.TIME,prefixes:vt.NONE,value:3600,offset:0},{name:"seconds",base:gt.TIME,prefixes:vt.LONG,value:1,offset:0},{name:"second",base:gt.TIME,prefixes:vt.LONG,value:1,offset:0},{name:"sec",base:gt.TIME,prefixes:vt.LONG,value:1,offset:0},{name:"minutes",base:gt.TIME,prefixes:vt.NONE,value:60,offset:0},{name:"minute",base:gt.TIME,prefixes:vt.NONE,value:60,offset:0},{name:"hours",base:gt.TIME,prefixes:vt.NONE,value:3600,offset:0},{name:"hour",base:gt.TIME,prefixes:vt.NONE,value:3600,offset:0},{name:"day",base:gt.TIME,prefixes:vt.NONE,value:86400,offset:0},{name:"days",base:gt.TIME,prefixes:vt.NONE,value:86400,offset:0},{name:"rad",base:gt.ANGLE,prefixes:vt.NONE,value:1,offset:0},{name:"deg",base:gt.ANGLE,prefixes:vt.NONE,value:.017453292519943295,offset:0},{name:"grad",base:gt.ANGLE,prefixes:vt.NONE,value:.015707963267948967,offset:0},{name:"cycle",base:gt.ANGLE,prefixes:vt.NONE,value:6.283185307179586,offset:0},{name:"A",base:gt.CURRENT,prefixes:vt.SHORT,value:1,offset:0},{name:"ampere",base:gt.CURRENT,prefixes:vt.LONG,value:1,offset:0},{name:"K",base:gt.TEMPERATURE,prefixes:vt.NONE,value:1,offset:0},{name:"degC",base:gt.TEMPERATURE,prefixes:vt.NONE,value:1,offset:273.15},{name:"degF",base:gt.TEMPERATURE,prefixes:vt.NONE,value:1/1.8,offset:459.67},{name:"degR",base:gt.TEMPERATURE,prefixes:vt.NONE,value:1/1.8,offset:0},{name:"kelvin",base:gt.TEMPERATURE,prefixes:vt.NONE,value:1,offset:0},{name:"celsius",base:gt.TEMPERATURE,prefixes:vt.NONE,value:1,offset:273.15},{name:"fahrenheit",base:gt.TEMPERATURE,prefixes:vt.NONE,value:1/1.8,offset:459.67},{name:"rankine",base:gt.TEMPERATURE,prefixes:vt.NONE,value:1/1.8,offset:0},{name:"mol",base:gt.AMOUNT_OF_SUBSTANCE,prefixes:vt.NONE,value:1,offset:0},{name:"mole",base:gt.AMOUNT_OF_SUBSTANCE,prefixes:vt.NONE,value:1,offset:0},{name:"cd",base:gt.LUMINOUS_INTENSITY,prefixes:vt.NONE,value:1,offset:0},{name:"candela",base:gt.LUMINOUS_INTENSITY,prefixes:vt.NONE,value:1,offset:0},{name:"N",base:gt.FORCE,prefixes:vt.SHORT,value:1,offset:0},{name:"newton",base:gt.FORCE,prefixes:vt.LONG,value:1,offset:0},{name:"lbf",base:gt.FORCE,prefixes:vt.NONE,value:4.4482216152605,offset:0},{name:"poundforce",base:gt.FORCE,prefixes:vt.NONE,value:4.4482216152605,offset:0},{name:"b",base:gt.BIT,prefixes:vt.BINARY_SHORT,value:1,offset:0},{name:"bits",base:gt.BIT,prefixes:vt.BINARY_LONG,value:1,offset:0},{name:"B",base:gt.BIT,prefixes:vt.BINARY_SHORT,value:8,offset:0},{name:"bytes",base:gt.BIT,prefixes:vt.BINARY_LONG,value:8,offset:0}],ct.Complex=r,r.prototype.copy=function(){return new r(this.re,this.im)},r.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"},r.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"]},ct.E=Math.E,ct.LN2=Math.LN2,ct.LN10=Math.LN10,ct.LOG2E=Math.LOG2E,ct.LOG10E=Math.LOG10E,ct.PI=Math.PI,ct.SQRT1_2=Math.SQRT1_2,ct.SQRT2=Math.SQRT2,ct.I=new r(0,-1),ct.pi=ct.PI,ct.e=ct.E,ct.i=ct.I,ct["import"]=f,f.doc={name:"import",category:"Utils",syntax:["import(string)"],description:"Import functions from a file.",examples:['import("numbers")','import("./mylib.js")'],seealso:[]},ct.help=h,h.doc={name:"help",category:"Utils",syntax:["help(object)"],description:"Display documentation on a function or data type.",examples:['help("sqrt")','help("Complex")'],seealso:[]},ct["typeof"]=c,c.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:[]},ct.min=m,m.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"]},ct.max=p,p.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"]},ct["in"]=g,g.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:[]},ct.sin=v,v.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"]},ct.atan2=d,d.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"]},ct.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"]},ct.atan=y,y.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"]},ct.cos=E,E.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"]},ct.tan=w,w.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"]},ct.acos=N,N.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"]},ct.arg=b,b.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"]},ct.conj=M,M.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"]},ct.im=O,O.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"]},ct.re=T,T.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"]},ct.cube=S,S.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"]},ct.divide=k,k.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"]},ct.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"]},ct.round=q,q.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"]},ct.equal=A,A.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"]},ct.unequal=C,C.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"]},ct.log10=R,R.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"]},ct.fix=I,I.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"]},ct.add=P,P.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"]},ct.mod=G,G.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:[]},ct.exp=B,B.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"]},ct.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"]},ct.sqrt=V,V.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"]},ct.square=D,D.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"]},ct.larger=F,F.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"]},ct.sign=H,H.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"]},ct.unaryminus=j,j.doc={name:"unaryminus",category:"Operators",syntax:["-x","unaryminus(x)"],description:"Inverse the sign of a value.",examples:["-4.5","-(-5.6)"],seealso:["add","subtract"]},ct.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"]},ct.abs=z,z.doc={name:"abs",category:"Arithmetic",syntax:["abs(x)"],description:"Compute the absolute value.",examples:["abs(3.5)","abs(-4.2)"],seealso:["sign"]},ct.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"]},ct.pow=X,X.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"]},ct.floor=Q,Q.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"]},ct.ceil=$,$.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"]},ct.multiply=J,J.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"]},ct.subtract=tt,tt.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"]},ct.random=nt,nt.doc={name:"random",category:"Probability",syntax:["random()"],description:"Return a random number between 0 and 1.",examples:["random()","100 * random()"],seealso:[]},ct.factorial=it,it.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:[]},ct.parser.node.Node=rt,rt.prototype.eval=function(){throw Error("Cannot evaluate a Node interface")
|
|
},rt.prototype.toString=function(){return""},at.prototype=new rt,ct.parser.node.Symbol=at,at.prototype.hasParams=function(){return void 0!=this.params&&this.params.length>0},at.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)},at.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},st.prototype=new rt,ct.parser.node.Constant=st,st.prototype.eval=function(){return this.value},st.prototype.toString=function(){return this.value?""+this.value:""},ot.prototype=new rt,ct.parser.node.Block=ot,ot.prototype.add=function(e,t){var n=this.params.length;this.params[n]=e,this.visible[n]=void 0!=t?t:!0},ot.prototype.eval=function(){for(var e=[],t=0,n=this.params.length;n>t;t++){var i=this.params[t].eval();this.visible[t]&&e.push(i)}return e},ot.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]"},ft.prototype=new rt,ct.parser.node.Assignment=ft,ft.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 i=this.expr.eval();if(void 0==this.result.value)throw Error("Undefined symbol "+this.name);var r=this.result.eval();e=r.set(n,i),this.result.value=e}else e=this.expr.eval(),this.result.value=e;return e},ft.prototype.toString=function(){var e="";return e+=this.name,this.params&&this.params.length&&(e+="("+this.params.join(", ")+")"),e+=" = ",e+=""+this.expr},ut.prototype=new rt,ct.parser.node.FunctionAssignment=ut,ut.prototype.createFunction=function(e,t,n,i){var r=function(){var t=n?n.length:0,r=arguments?arguments.length:0;if(t!=r)throw o(e,r,t);if(t>0)for(var a=0;t>a;a++)n[a].value=arguments[a];return i.eval()};return r.toString=function(){return e+"("+t.join(", ")+")"},r},ut.prototype.eval=function(){for(var e=this.variables,t=this.values,n=0,i=e.length;i>n;n++)e[n].value=t[n];return this.result.value=this.def,this.def},ut.prototype.toString=function(){return""+this.def},ct.parser.node.Scope=ht,ht.prototype.createNestedScope=function(){var e=new ht(this);return this.nestedScopes||(this.nestedScopes=[]),this.nestedScopes.push(e),e},ht.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()},ht.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},ht.prototype.newSymbol=function(e,t){var n=this,i=function(){if(!i.value&&(i.value=n.findDef(e),!i.value))throw Error("Undefined symbol "+e);return"function"==typeof i.value?i.value.apply(null,arguments):i.value};return i.value=t,i.toString=function(){return i.value?""+i.value:""},i},ht.prototype.createLink=function(e){var t=this.links[e];return t||(t=this.createSymbol(e),this.links[e]=t),t},ht.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},ht.prototype.createUpdate=function(e){var t=this.updates[e];return t||(t=this.createLink(e),this.updates[e]=t),t},ht.prototype.findDef=function(t){function n(e,t){var n=a(e,t);return s[e]=n,o[e]=n,n}var i;if(i=this.defs[t])return i;if(i=this.updates[t])return i;if(this.parentScope)return this.parentScope.findDef(t);var a=this.newSymbol,s=this.symbols,o=this.defs;if("pi"==t)return n(t,ct.PI);if("e"==t)return n(t,ct.E);if("i"==t)return n(t,new r(0,1));var f=ct[t];if(f)return n(t,f);if(e.isUnit(t)){var u=new e(void 0,t);return n(t,u)}return void 0},ht.prototype.removeLink=function(e){delete this.links[e]},ht.prototype.removeDef=function(e){delete this.defs[e]},ht.prototype.removeUpdate=function(e){delete this.updates[e]},ht.prototype.init=function(){var e=this.symbols,t=this.parentScope;for(var n in e)if(e.hasOwnProperty(n)){var i=e[n];i.set(t?t.findDef(n):void 0)}this.nestedScopes&&this.nestedScopes.forEach(function(e){e.init()})},ht.prototype.hasLink=function(e){if(this.links[e])return!0;if(this.nestedScopes)for(var t=this.nestedScopes,n=0,i=t.length;i>n;n++)if(t[n].hasLink(e))return!0;return!1},ht.prototype.hasDef=function(e){return void 0!=this.defs[e]},ht.prototype.hasUpdate=function(e){return void 0!=this.updates[e]},ht.prototype.getUndefinedSymbols=function(){var e=this.symbols,t=[];for(var n in e)if(e.hasOwnProperty(n)){var i=e[n];void 0==i.value&&t.push(i)}return this.nestedScopes&&this.nestedScopes.forEach(function(e){t=t.concat(e.getUndefinedSymbols())}),t},ct.parser.Parser=lt,lt.prototype.parse=function(e,t){return this.expr=e||"",t||(t=this.scope),this.parse_start(t)},lt.prototype.eval=function(e){var t=this.parse(e);return t.eval()},lt.prototype.get=function(e){var t=this.scope.findDef(e);return t?t.value:void 0},lt.prototype.put=function(e,t){this.scope.createDef(e,t)},lt.prototype.clear=function(){this.scope.clear()},lt.prototype.getChar=function(){this.index++,this.c=this.expr.charAt(this.index)},lt.prototype.getFirstChar=function(){this.index=0,this.c=this.expr.charAt(0)},lt.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()}},lt.prototype.isDelimiter=function(e){return"&"==e||"|"==e||"<"==e||">"==e||"="==e||"+"==e||"/"==e||"*"==e||"%"==e||"^"==e||","==e||";"==e||"\n"==e||"!"==e},lt.prototype.isValidSymbolName=function(e){for(var t=0,n=e.length;n>t;t++){var i=e.charAt(t),r=this.isAlpha(i);if(!r)return!1}return!0},lt.prototype.isAlpha=function(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e||"_"==e},lt.prototype.isDigitDot=function(e){return e>="0"&&"9">=e||"."==e},lt.prototype.isDigit=function(e){return e>="0"&&"9">=e},lt.prototype.parse_start=function(e){this.getFirstChar(),this.getToken();var t;if(t=""==this.token?new st(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},lt.prototype.parse_ans=function(e){var t=this.parse_function_assignment(e);if(!(t instanceof ft)){var n="ans",i=void 0,r=e.createDef(n);return new ft(n,i,t,r)}return t},lt.prototype.parse_block=function(e){var t,n,i;for("\n"!=this.token&&";"!=this.token&&""!=this.token&&(t=this.parse_ans(e));"\n"==this.token||";"==this.token;)n||(n=new ot,t&&(i=";"!=this.token,n.add(t,i))),this.getToken(),"\n"!=this.token&&";"!=this.token&&""!=this.token&&(t=this.parse_ans(e),i=";"!=this.token,n.add(t,i));return n?n:(t||(t=this.parse_ans(e)),t)},lt.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(),i=[],r=[];;){if(this.getToken(),this.token_type!=this.TOKENTYPE.SYMBOL)throw this.createSyntaxError("Variable name expected");var a=this.token,s=n.createDef(a);if(i.push(a),r.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 ut(t,i,r,o,f)}return this.parse_assignment(e)},lt.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 at))throw this.createSyntaxError("Symbol expected at the left hand side of assignment operator =");var i=n.name,r=n.params;t||e.removeLink(i),this.getToken();var a=this.parse_range(e),s=n.hasParams()?e.createUpdate(i):e.createDef(i);return new ft(i,r,a,s)}return n},lt.prototype.parse_range=function(e){var t=this.parse_conditions(e);return t},lt.prototype.parse_conditions=function(e){for(var t=this.parse_bitwise_conditions(e),n={"in":"in"};void 0!==n[this.token];){var i=this.token,r=ct[n[i]];this.getToken();var a=[t,this.parse_bitwise_conditions(e)];t=new at(i,r,a)}return t},lt.prototype.parse_bitwise_conditions=function(e){var t=this.parse_comparison(e);return t},lt.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 i=this.token,r=ct[n[i]];this.getToken();var a=[t,this.parse_addsubtract(e)];t=new at(i,r,a)}return t},lt.prototype.parse_addsubtract=function(e){for(var t=this.parse_multiplydivide(e),n={"+":"add","-":"subtract"};void 0!==n[this.token];){var i=this.token,r=ct[n[i]];this.getToken();var a=[t,this.parse_multiplydivide(e)];t=new at(i,r,a)}return t},lt.prototype.parse_multiplydivide=function(e){for(var t=this.parse_pow(e),n={"*":"multiply","/":"divide","%":"mod",mod:"mod"};void 0!==n[this.token];){var i=this.token,r=ct[n[i]];this.getToken();var a=[t,this.parse_pow(e)];t=new at(i,r,a)}return t},lt.prototype.parse_pow=function(e){for(var t=this.parse_factorial(e);"^"==this.token;){var n=this.token,i=X;this.getToken();var r=[t,this.parse_factorial(e)];t=new at(n,i,r)}return t},lt.prototype.parse_factorial=function(e){for(var t=this.parse_unaryminus(e);"!"==this.token;){var n=this.token,i=it;this.getToken();var r=[t];t=new at(n,i,r)}return t},lt.prototype.parse_unaryminus=function(e){if("-"==this.token){var t=this.token,n=j;this.getToken();var i=[this.parse_plot(e)];return new at(t,n,i)}return this.parse_plot(e)},lt.prototype.parse_plot=function(e){return this.parse_symbol(e)},lt.prototype.parse_symbol=function(e){if(this.token_type==this.TOKENTYPE.SYMBOL){var t=this.token;this.getToken();var n=e.createLink(t),i=this.parse_arguments(e),r=new at(t,n,i);return r}return this.parse_string(e)},lt.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},lt.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 i=new st(t);return i}return this.parse_matrix(e)},lt.prototype.parse_matrix=function(e){return this.parse_number(e)},lt.prototype.parse_number=function(t){if(this.token_type==this.TOKENTYPE.NUMBER){var n;n="."==this.token?0:Number(this.token),this.getToken();var i;if(this.token_type==this.TOKENTYPE.SYMBOL){if("i"==this.token||"I"==this.token)return i=new r(0,n),this.getToken(),new st(i);if(e.isUnit(this.token))return i=new e(n,this.token),this.getToken(),new st(i);throw this.createTypeError('Unknown unit "'+this.token+'"')}var a=new st(n);return a}return this.parse_parentheses(t)},lt.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)},lt.prototype.parse_end=function(){throw""==this.token?this.createSyntaxError("Unexpected end of expression"):this.createSyntaxError("Value expected")},lt.prototype.row=function(){return void 0},lt.prototype.col=function(){return this.index-this.token.length+1},lt.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+")"},lt.prototype.createSyntaxError=function(e){return new SyntaxError(this.createErrorMessage(e))},lt.prototype.createTypeError=function(e){return new TypeError(this.createErrorMessage(e))},lt.prototype.createError=function(e){return Error(this.createErrorMessage(e))}})(); |