mathjs/math.min.js
2013-03-16 12:54:36 +01:00

29 lines
71 KiB
JavaScript

/**
* math.js
* https://github.com/josdejong/mathjs
*
* Math.js is an extensive math library for JavaScript and Node.js,
* 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
*
* @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.
*/
(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)<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,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}})();