mathjs/math.min.js
2013-03-13 21:45:05 +01:00

38 lines
68 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.
*
* Features:
* - Supports real and complex numbers, units, strings, arrays*, and matrices*.
* - Contains a large set of built-in functions and constants.
* - Contains a flexible expression parser.
* - Easily extensible.
* - Compatible with JavaScript's built-in Math library.
* - Powerful and easy to use.
*
* * Note: arrays and matrices are to be implemented.
*
* @version 2013-03-13
* @date 0.4.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.
*/
(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 r(e){return e==Math.round(e)}function i(e,r){if(this.constructor!=i)throw new SyntaxError("Complex constructor must be called with the new operator");switch(arguments.length){case 2:if(!n(e)||!n(r))throw new TypeError("Two numbers or a single string expected in Complex constructor");this.re=e,this.im=r;break;case 1:if(!t(e))throw new TypeError("Two numbers or a single string expected in Complex constructor");var a=i.parse(e);if(a)return a;throw new SyntaxError('"'+e+'" 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 a(e,t){var n=void 0;if(2==arguments.length){var r=c(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(c(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(e,n){var r;if(t(e)){if("undefined"==typeof require)throw Error("Cannot load file: require not available.");var i=require(e);o(i)}else if(f(e)){if(r=e.name,!r)throw Error("Cannot import an unnamed function");(n||void 0===yt[r])&&(yt[r]=e)}else if(e instanceof Object)for(r in e)if(e.hasOwnProperty(r)){var a=e[r];f(a)?(n||void 0===yt[r])&&(yt[r]=a):o(a)}}function f(r){return"function"==typeof r||n(r)||t(r)||r instanceof i||r instanceof e}function u(e){if(1!=arguments.length)throw s("help",arguments.length,1);if(void 0!=e){if(e.doc)return h(e.doc);if(e.constructor.doc)return h(e.constructor.doc);if(t(e)){var n=yt[e];if(n&&n.doc)return h(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 h(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 yt.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+=" "+xt.toString(i)+"\n"}t+="\n"}return e.seealso&&(t+="SEE ALSO\n"+e.seealso.join(", ")+"\n"),t}function c(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 yt)if(yt.hasOwnProperty(n)&&e.constructor==yt[n])return n.toLowerCase();if(e.constructor.name)return e.constructor.name.toLowerCase()}}return t}function l(){if(0==arguments.length)throw Error("Function sum requires one or more parameters (0 provided)");if(1==arguments.length&&arguments[0]instanceof Array)return l.apply(this,arguments[0]);for(var e=arguments[0],t=1,n=arguments.length;n>t;t++){var r=arguments[t];Q(r,e)&&(e=r)}return e}function p(){if(0==arguments.length)throw Error("Function sum requires one or more parameters (0 provided)");if(1==arguments.length&&arguments[0]instanceof Array)return p.apply(this,arguments[0]);for(var e=arguments[0],t=1,n=arguments.length;n>t;t++){var r=arguments[t];W(r,e)&&(e=r)}return e}function m(t,n){if(2!=arguments.length)throw s("in",arguments.length,2);if(t instanceof e&&n 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 r=n.copy();return r.value=t.value,r.fixPrefix=!0,r}if(t instanceof Array||n instanceof Array)return xt.map2(t,n,m);throw a("in",t)}function d(e,t){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=t),!n(i)||!r(i)||1>i)throw Error("Parameters in function eye must be positive integers");if(a&&(!n(a)||!r(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 g(r){if(1!=arguments.length)throw s("size",arguments.length,1);if(n(r))return[[1,1]];if(r instanceof i)return[[1,1]];if(r instanceof e)return[[1,1]];if(t(r))return[[1,r.length]];if(r instanceof Array)return[v(r)];throw a("size",r)}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 y(t){if(1!=arguments.length)throw s("sin",arguments.length,1);if(n(t))return Math.sin(t);if(t instanceof i)return new i(.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)}if(t instanceof Array)return xt.map(t,y);throw a("sin",t)}function x(e,t){if(2!=arguments.length)throw s("atan2",arguments.length,2);if(n(e)){if(n(t))return Math.atan2(e,t);if(t instanceof i)return Math.atan2(e,t.re)}else if(e instanceof i){if(n(t))return Math.atan2(e.re,t);if(t instanceof i)return Math.atan2(e.re,t.re)}if(t instanceof Array||e instanceof Array)return xt.map2(e,t,x);throw a("atan2",e,t)}function E(e){if(1!=arguments.length)throw s("asin",arguments.length,1);if(n(e))return e>=-1&&1>=e?Math.asin(e):E(new i(e,0));if(e instanceof i){var t=e.re,r=e.im,o=new i(r*r-t*t+1,-2*t*r),f=j(o),u=new i(f.re-r,f.im+t),h=$(u);return new i(h.im,-h.re)}if(e instanceof Array)return xt.map(e,E);throw a("asin",e)}function w(e){if(1!=arguments.length)throw s("atan",arguments.length,1);if(n(e))return Math.atan(e);if(e instanceof i){var t=e.re,r=e.im,o=t*t+(1-r)*(1-r),f=new i((1-r*r-t*t)/o,-2*t/o),u=$(f);return new i(-.5*u.im,.5*u.re)}if(e instanceof Array)return xt.map(e,w);throw a("atan",e)}function N(t){if(1!=arguments.length)throw s("csc",arguments.length,1);if(n(t))return 1/Math.sin(t);if(t instanceof i){var r=.25*(Math.exp(-2*t.im)+Math.exp(2*t.im))-.5*Math.cos(2*t.re);return new i(.5*Math.sin(t.re)*(Math.exp(-t.im)+Math.exp(t.im))/r,.5*Math.cos(t.re)*(Math.exp(-t.im)-Math.exp(t.im))/r)}if(t instanceof e){if(!t.hasBase(e.BASE_UNITS.ANGLE))throw new TypeError("Unit in function csc is no angle");return 1/Math.sin(t.value)}if(t instanceof Array)return xt.map(t,N);throw a("csc",t)}function b(t){if(1!=arguments.length)throw s("cos",arguments.length,1);if(n(t))return Math.cos(t);if(t instanceof i)return new i(.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)}if(t instanceof Array)return xt.map(t,b);throw a("cos",t)}function M(t){if(1!=arguments.length)throw s("cot",arguments.length,1);if(n(t))return 1/Math.tan(t);if(t instanceof i){var r=Math.exp(-4*t.im)-2*Math.exp(-2*t.im)*Math.cos(2*t.re)+1;return new i(2*Math.exp(-2*t.im)*Math.sin(2*t.re)/r,(Math.exp(-4*t.im)-1)/r)}if(t instanceof e){if(!t.hasBase(e.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cot is no angle");return 1/Math.tan(t.value)}if(t instanceof Array)return xt.map(t,M);throw a("cot",t)}function O(t){if(1!=arguments.length)throw s("sec",arguments.length,1);if(n(t))return 1/Math.cos(t);if(t instanceof i){var r=.25*(Math.exp(-2*t.im)+Math.exp(2*t.im))+.5*Math.cos(2*t.re);return new i(.5*Math.cos(t.re)*(Math.exp(-t.im)+Math.exp(t.im))/r,.5*Math.sin(t.re)*(Math.exp(t.im)-Math.exp(-t.im))/r)}if(t instanceof e){if(!t.hasBase(e.BASE_UNITS.ANGLE))throw new TypeError("Unit in function sec is no angle");return 1/Math.cos(t.value)}if(t instanceof Array)return xt.map(t,O);throw a("sec",t)}function T(t){if(1!=arguments.length)throw s("tan",arguments.length,1);if(n(t))return Math.tan(t);if(t instanceof i){var r=Math.exp(-4*t.im)+2*Math.exp(-2*t.im)*Math.cos(2*t.re)+1;return new i(2*Math.exp(-2*t.im)*Math.sin(2*t.re)/r,(1-Math.exp(-4*t.im))/r)}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)}if(t instanceof Array)return xt.map(t,T);throw a("tan",t)}function S(e){if(1!=arguments.length)throw s("acos",arguments.length,1);if(n(e))return e>=-1&&1>=e?Math.acos(e):S(new i(e,0));if(e instanceof i){var t=new i(e.im*e.im-e.re*e.re+1,-2*e.re*e.im),r=j(t),o=new i(r.re-e.im,r.im+e.re),f=$(o);return new i(1.5707963267948966-f.im,f.re)}if(e instanceof Array)return xt.map(e,S);throw a("acos",e)}function A(e){if(1!=arguments.length)throw s("arg",arguments.length,1);if(n(e))return Math.atan2(0,e);if(e instanceof i)return Math.atan2(e.im,e.re);if(e instanceof Array)return xt.map(e,A);throw a("arg",e)}function k(e){if(1!=arguments.length)throw s("conj",arguments.length,1);if(n(e))return e;if(e instanceof i)return new i(e.re,-e.im);if(e instanceof Array)return xt.map(e,k);throw a("conj",e)}function _(e){if(1!=arguments.length)throw s("im",arguments.length,1);if(n(e))return 0;if(e instanceof i)return e.im;if(e instanceof Array)return xt.map(e,_);throw a("im",e)}function U(e){if(1!=arguments.length)throw s("re",arguments.length,1);if(n(e))return e;if(e instanceof i)return e.re;if(e instanceof Array)return xt.map(e,U);throw a("re",e)}function q(e){if(1!=arguments.length)throw s("cube",arguments.length,1);if(n(e))return e*e*e;if(e instanceof i)return it(it(e,e),e);if(e instanceof Array)return it(it(e,e),e);throw a("cube",e)}function L(t,r){if(2!=arguments.length)throw s("divide",arguments.length,2);if(n(t)){if(n(r))return t/r;if(r instanceof i)return C(new i(t,0),r)}if(t instanceof i){if(n(r))return C(t,new i(r,0));if(r instanceof i)return C(t,r)}if(t instanceof e&&n(r)){var o=t.copy();return o.value/=r,o}if(t instanceof Array&&!(r instanceof Array))return xt.map2(t,r,L);throw a("divide",t,r)}function C(e,t){var n=t.re*t.re+t.im*t.im;return new i((e.re*t.re+e.im*t.im)/n,(e.im*t.re-e.re*t.im)/n)}function R(r,o){if(2!=arguments.length)throw s("smallereq",arguments.length,2);if(n(r)){if(n(o))return o>=r;if(o instanceof i)return J(o)>=r}if(r instanceof i){if(n(o))return o>=J(r);if(o instanceof i)return J(r)<=J(o)}if(r instanceof e&&o instanceof e){if(!r.equalBase(o))throw Error("Cannot compare units with different base");return r.value<=o.value}if(t(r)||t(o))return o>=r;if(r instanceof Array||o instanceof Array)return xt.map2(r,o,R);throw a("smallereq",r,o)}function I(e,t){if(1!=arguments.length&&2!=arguments.length)throw s("round",arguments.length,1,2);if(void 0==t){if(n(e))return Math.round(e);if(e instanceof i)return new i(Math.round(e.re),Math.round(e.im));throw e instanceof Array&&xt.map(e,I),a("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 P(e,t);if(e instanceof i)return new i(P(e.re,t),P(e.im,t));if(e instanceof Array||t instanceof Array)return xt.map2(e,t,I);throw a("round",e,t)}function P(e,t){var n=Math.pow(10,void 0!=t?t:yt.options.precision);return Math.round(e*n)/n}function B(r,o){if(2!=arguments.length)throw s("equal",arguments.length,2);if(n(r)){if(n(o))return r==o;if(o instanceof i)return r==o.re&&0==o.im}if(r instanceof i){if(n(o))return r.re==o&&0==r.im;if(o instanceof i)return r.re==o.re&&r.im==o.im}if(r instanceof e&&o instanceof e){if(!r.equalBase(o))throw Error("Cannot compare units with different base");return r.value==o.value}if(t(r)||t(o))return r==o;if(r instanceof Array||o instanceof Array)return xt.map2(r,o,B);throw a("equal",r,o)}function G(r,o){if(2!=arguments.length)throw s("unequal",arguments.length,2);if(n(r)){if(n(o))return r==o;if(o instanceof i)return r==o.re&&0==o.im}if(r instanceof i){if(n(o))return r.re==o&&0==r.im;if(o instanceof i)return r.re==o.re&&r.im==o.im}if(r instanceof e&&o instanceof e){if(!r.equalBase(o))throw Error("Cannot compare units with different base");return r.value==o.value}if(t(r)||t(o))return r==o;if(r instanceof Array||o instanceof Array)return xt.map2(r,o,G);throw a("unequal",r,o)}function D(e){if(1!=arguments.length)throw s("log10",arguments.length,1);if(n(e))return e>=0?Math.log(e)/Math.LN10:D(new i(e,0));if(e instanceof i)return new i(Math.log(Math.sqrt(e.re*e.re+e.im*e.im))/Math.LN10,Math.atan2(e.im,e.re)/Math.LN10);if(e instanceof Array)return xt.map(e,D);throw a("log10",e)}function Y(e){if(1!=arguments.length)throw s("fix",arguments.length,1);if(n(e))return value>0?Math.floor(e):Math.ceil(e);if(e instanceof i)return new i(e.re>0?Math.floor(e.re):Math.ceil(e.re),e.im>0?Math.floor(e.im):Math.ceil(e.im));if(e instanceof Array)return xt.map(e,Y);throw a("fix",e)}function V(r,o){if(2!=arguments.length)throw s("add",arguments.length,2);if(n(r)){if(n(o))return r+o;if(o instanceof i)return new i(r+o.re,o.im)}else if(r instanceof i){if(n(o))return new i(r.re+o,r.im);if(o instanceof i)return new i(r.re+o.re,r.im+o.im)}else if(r instanceof e&&o instanceof e){if(!r.equalBase(o))throw Error("Units do not match");if(!r.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 f=r.copy();return f.value+=o.value,f.fixPrefix=!1,f}if(t(r)||t(o))return r+o;if(r instanceof Array||o instanceof Array)return xt.map2(r,o,V);throw a("add",r,o)}function z(e,t){if(2!=arguments.length)throw s("mod",arguments.length,2);if(n(e)){if(n(t))return e%t;if(t instanceof i&&0==t.im)return e%t.re}else if(e instanceof i&&0==e.im){if(n(t))return e.re%t;if(t instanceof i&&0==t.im)return e.re%t.re}if(e instanceof Array||t instanceof Array)return xt.map2(e,t,z);throw a("mod",e,t)}function F(e){if(1!=arguments.length)throw s("exp",arguments.length,1);if(n(e))return Math.exp(e);if(e instanceof i){var t=Math.exp(e.re);return new i(t*Math.cos(e.im),t*Math.sin(e.im))}if(e instanceof Array)return xt.map(e,F);throw a("exp",e)}function H(r,o){if(2!=arguments.length)throw s("largereq",arguments.length,2);if(n(r)){if(n(o))return r>=o;if(o instanceof i)return r>=J(o)}if(r instanceof i){if(n(o))return J(r)>=o;if(o instanceof i)return J(r)>=J(o)}if(r instanceof e&&o instanceof e){if(!r.equalBase(o))throw Error("Cannot compare units with different base");return r.value>=o.value}if(t(r)||t(o))return r>=o;if(r instanceof Array||o instanceof Array)return xt.map2(r,o,H);throw a("largereq",r,o)}function j(e){if(1!=arguments.length)throw s("sqrt",arguments.length,1);if(n(e))return e>=0?Math.sqrt(e):j(new i(e,0));if(e instanceof i){var t=Math.sqrt(e.re*e.re+e.im*e.im);return e.im>=0?new i(.5*Math.sqrt(2*(t+e.re)),.5*Math.sqrt(2*(t-e.re))):new i(.5*Math.sqrt(2*(t+e.re)),-.5*Math.sqrt(2*(t-e.re)))}if(e instanceof Array)return xt.map(e,j);throw a("sqrt",e)}function K(e){if(1!=arguments.length)throw s("square",arguments.length,1);if(n(e))return e*e;if(e instanceof i)return it(e,e);if(e instanceof Array)return it(e,e);throw a("square",e)}function W(r,o){if(2!=arguments.length)throw s("larger",arguments.length,2);if(n(r)){if(n(o))return r>o;if(o instanceof i)return r>J(o)}if(r instanceof i){if(n(o))return J(r)>o;if(o instanceof i)return J(r)>J(o)}if(r instanceof e&&o instanceof e){if(!r.equalBase(o))throw Error("Cannot compare units with different base");return r.value>o.value}if(t(r)||t(o))return r>o;if(r instanceof Array||o instanceof Array)return xt.map2(r,o,B);throw a("larger",r,o)}function X(e){if(1!=arguments.length)throw s("sign",arguments.length,1);if(n(e)){var t;return t=e>0?1:0>e?-1:0}if(e instanceof i){var r=Math.sqrt(e.re*e.re+e.im*e.im);return new i(e.re/r,e.im/r)}if(e instanceof Array)return xt.map(e,t);throw a("sign",e)}function Z(t){if(1!=arguments.length)throw s("unaryminus",arguments.length,1);if(n(t))return-t;if(t instanceof i)return new i(-t.re,-t.im);if(t instanceof e){var r=t.copy();return r.value=-t.value,r}if(t instanceof Array)return xt.map(t,Z);throw a("unaryminus",t)}function Q(r,o){if(2!=arguments.length)throw s("smaller",arguments.length,2);if(n(r)){if(n(o))return o>r;if(o instanceof i)return J(o)>r}if(r instanceof i){if(n(o))return o>J(r);if(o instanceof i)return J(r)<J(o)}if(r instanceof e&&o instanceof e){if(!r.equalBase(o))throw Error("Cannot compare units with different base");return r.value<o.value}if(t(r)||t(o))return o>r;if(r instanceof Array||o instanceof Array)return xt.map2(r,o,Q);throw a("smaller",r,o)}function J(e){if(1!=arguments.length)throw s("abs",arguments.length,1);if(n(e))return Math.abs(e);if(e instanceof i)return Math.sqrt(e.re*e.re+e.im*e.im);if(e instanceof Array)return xt.map(e,J);throw a("abs",e)}function $(e,t){if(1!=arguments.length&&2!=arguments.length)throw s("log",arguments.length,1,2);if(void 0!==t)return L($(e),$(t));if(n(e))return e>=0?Math.log(e):$(new i(e,0));if(e instanceof i)return new i(Math.log(Math.sqrt(e.re*e.re+e.im*e.im)),Math.atan2(e.im,e.re));if(e instanceof Array)return xt.map(e,$);throw a("log",e,t)}function et(e,t){if(2!=arguments.length)throw s("pow",arguments.length,2);if(n(e)){if(n(t))return r(t)||e>=0?Math.pow(e,t):tt(new i(e,0),new i(t,0));if(t instanceof i)return tt(new i(e,0),t)}else if(e instanceof i){if(n(t))return tt(e,new i(t,0));if(t instanceof i)return tt(e,t)}else if(e instanceof Array){if(!n(t)||!r(t)||0>t)throw new TypeError("For A^b, b must be a positive integer (value is "+t+")");var o=g(e)[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==t)return d(o[0]);for(var f=e,u=1;t>u;u++)f=it(e,f);return f}throw a("pow",e,t)}function tt(e,t){var n=$(e),r=it(n,t);return F(r)}function nt(e){if(1!=arguments.length)throw s("floor",arguments.length,1);if(n(e))return Math.floor(e);if(e instanceof i)return new i(Math.floor(e.re),Math.floor(e.im));if(e instanceof Array)return xt.map(e,nt);throw a("floor",e)}function rt(e){if(1!=arguments.length)throw s("ceil",arguments.length,1);if(n(e))return Math.ceil(e);if(e instanceof i)return new i(Math.ceil(e.re),Math.ceil(e.im));if(e instanceof Array)return xt.map(e,rt);throw a("ceil",e)}function it(t,r){if(2!=arguments.length)throw s("multiply",arguments.length,2);if(n(t)){if(n(r))return t*r;if(r instanceof i)return at(new i(t,0),r);if(r instanceof e)return u=r.copy(),u.value*=t,u}else if(t instanceof i){if(n(r))return at(t,new i(r,0));if(r instanceof i)return at(t,r)}else if(t instanceof e){if(n(r))return u=t.copy(),u.value*=r,u}else if(t instanceof Array){if(r instanceof Array){var o=g(t)[0],f=g(r)[0];if(2!=o.length)throw Error("Can only multiply a 2 dimensional matrix (A has "+o.length+" dimensions)");if(2!=f.length)throw Error("Can only multiply a 2 dimensional matrix (B has "+f.length+" dimensions)");if(o[1]!=f[0])throw Error("Dimensions mismatch in multiplication. Columns of A must match rows of B (A is "+o[0]+"x"+o[1]+", B is "+f[0]+"x"+f[1]+", "+f[1]+" != "+f[0]+")");for(var u=[],h=o[0],c=f[1],l=o[1],p=0;h>p;p++){u[p]=[];for(var m=0;c>m;m++){for(var d=null,v=0;l>v;v++){var y=it(t[p][v],r[v][m]);d=null==d?y:V(d,y)}u[p][m]=d}}return u}return xt.map2(t,r,it)}if(r instanceof Array)return xt.map2(t,r,it);throw a("multiply",t,r)}function at(e,t){return new i(e.re*t.re-e.im*t.im,e.re*t.im+e.im*t.re)}function st(t,r){if(2!=arguments.length)throw s("subtract",arguments.length,2);if(n(t)){if(n(r))return t-r;if(r instanceof i)return new i(t-r.re,r.im)}else if(t instanceof i){if(n(r))return new i(t.re-r,t.im);if(r instanceof i)return new i(t.re-r.re,t.im-r.im)}else if(t instanceof e&&r instanceof e){if(!t.equalBase(r))throw Error("Units do not match");if(!t.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=t.copy();return o.value-=r.value,o.fixPrefix=!1,o}if(t instanceof Array||r instanceof Array)return xt.map2(t,r,st);throw a("subtract",t,r)}function ot(){if(0!=arguments.length)throw s("random",arguments.length,0);return Math.random()}function ft(e){if(1!=arguments.length)throw s("factorial",arguments.length,1);if(n(e)){if(!r(e))throw new TypeError("Function factorial can only handle integer values");var t=e,i=t;for(t--;t>1;)i*=t,t--;return 0==i&&(i=1),i}if(e instanceof Array)return xt.map(e,ft);throw a("factorial",e)}function ut(){}function ht(e,t,n){this.name=e,this.fn=t,this.params=n}function ct(e){this.value=e}function lt(){this.params=[],this.visible=[]}function pt(e,t,n,r){this.name=e,this.params=t,this.expr=n,this.result=r}function mt(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 dt(e){this.parentScope=e,this.nestedScopes=void 0,this.symbols={},this.defs={},this.updates={},this.links={}}function gt(){if(this.constructor!=gt)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 dt}function vt(){this.idMax=-1,this.updateSeq=0,this.parser=new gt,this.scope=new dt,this.nodes={},this.firstNode=void 0,this.lastNode=void 0}var yt={parser:{node:{}},options:{precision:10}};"undefined"!=typeof module&&module.exports!==void 0&&(module.exports=yt),"undefined"!=typeof exports&&(exports=yt),"undefined"!=typeof require&&"undefined"!=typeof define&&define(function(){return yt}),"undefined"!=typeof window&&(window.math=yt);var xt={};xt.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 P(e,t)+"";var r=Math.round(Math.log(n)/Math.LN10),i=e/Math.pow(10,r);return P(i,t)+"E"+r},xt.toString=function(e){return n(e)?xt.format(e):e instanceof Array?""+e:""+e},xt.randomUUID=function(){var e=function(){return Math.floor(65536*Math.random()).toString(16)};return e()+e()+"-"+e()+"-"+e()+"-"+e()+"-"+e()+e()+e()},xt.map=function(e,t){if(!e instanceof Array)throw new TypeError("Array expected");return e.map(function(e){return t(e)})},xt.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}),yt.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,r=e.length;return e.substring(n,r)===t},e.prototype._init=function(t,n){if(void 0!==n){for(var r=e.UNITS,i=!1,a=0,s=r.length;s>a;a++){var o=r[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,i=!0;break}}}if(!i)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,r=n.length,i=0;r>i;i++){var a=n[i];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),xt.format(t)+" "+this.prefix.name+this.unit.name;var n=e.PREFIX_NONE,r=Math.abs(Math.log(this.value/n.value)/Math.LN10-1.5),i=this.unit.prefixes;for(var a in i)if(i.hasOwnProperty(a)){var s=i[a];if(s.scientific){var o=Math.abs(Math.log(this.value/s.value)/Math.LN10-1.5);r>o&&(n=s,r=o)}}return t=this._unnormalize(this.value,n.value),xt.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 Et=e.BASE_UNITS,wt=e.PREFIXES;e.BASE_UNIT_NONE={},e.UNIT_NONE={name:"",base:e.BASE_UNIT_NONE,value:1,offset:0},e.UNITS=[{name:"meter",base:Et.LENGTH,prefixes:wt.LONG,value:1,offset:0},{name:"inch",base:Et.LENGTH,prefixes:wt.NONE,value:.0254,offset:0},{name:"foot",base:Et.LENGTH,prefixes:wt.NONE,value:.3048,offset:0},{name:"yard",base:Et.LENGTH,prefixes:wt.NONE,value:.9144,offset:0},{name:"mile",base:Et.LENGTH,prefixes:wt.NONE,value:1609.344,offset:0},{name:"link",base:Et.LENGTH,prefixes:wt.NONE,value:.201168,offset:0},{name:"rod",base:Et.LENGTH,prefixes:wt.NONE,value:5.02921,offset:0},{name:"chain",base:Et.LENGTH,prefixes:wt.NONE,value:20.1168,offset:0},{name:"angstrom",base:Et.LENGTH,prefixes:wt.NONE,value:1e-10,offset:0},{name:"m",base:Et.LENGTH,prefixes:wt.SHORT,value:1,offset:0},{name:"ft",base:Et.LENGTH,prefixes:wt.NONE,value:.3048,offset:0},{name:"yd",base:Et.LENGTH,prefixes:wt.NONE,value:.9144,offset:0},{name:"mi",base:Et.LENGTH,prefixes:wt.NONE,value:1609.344,offset:0},{name:"li",base:Et.LENGTH,prefixes:wt.NONE,value:.201168,offset:0},{name:"rd",base:Et.LENGTH,prefixes:wt.NONE,value:5.02921,offset:0},{name:"ch",base:Et.LENGTH,prefixes:wt.NONE,value:20.1168,offset:0},{name:"mil",base:Et.LENGTH,prefixes:wt.NONE,value:254e-7,offset:0},{name:"m2",base:Et.SURFACE,prefixes:wt.SHORT,value:1,offset:0},{name:"sqin",base:Et.SURFACE,prefixes:wt.NONE,value:64516e-8,offset:0},{name:"sqft",base:Et.SURFACE,prefixes:wt.NONE,value:.09290304,offset:0},{name:"sqyd",base:Et.SURFACE,prefixes:wt.NONE,value:.83612736,offset:0},{name:"sqmi",base:Et.SURFACE,prefixes:wt.NONE,value:2589988.110336,offset:0},{name:"sqrd",base:Et.SURFACE,prefixes:wt.NONE,value:25.29295,offset:0},{name:"sqch",base:Et.SURFACE,prefixes:wt.NONE,value:404.6873,offset:0},{name:"sqmil",base:Et.SURFACE,prefixes:wt.NONE,value:6.4516e-10,offset:0},{name:"m3",base:Et.VOLUME,prefixes:wt.SHORT,value:1,offset:0},{name:"L",base:Et.VOLUME,prefixes:wt.SHORT,value:.001,offset:0},{name:"litre",base:Et.VOLUME,prefixes:wt.LONG,value:.001,offset:0},{name:"cuin",base:Et.VOLUME,prefixes:wt.NONE,value:16387064e-12,offset:0},{name:"cuft",base:Et.VOLUME,prefixes:wt.NONE,value:.028316846592,offset:0},{name:"cuyd",base:Et.VOLUME,prefixes:wt.NONE,value:.764554857984,offset:0},{name:"teaspoon",base:Et.VOLUME,prefixes:wt.NONE,value:5e-6,offset:0},{name:"tablespoon",base:Et.VOLUME,prefixes:wt.NONE,value:15e-6,offset:0},{name:"minim",base:Et.VOLUME,prefixes:wt.NONE,value:6.161152e-8,offset:0},{name:"fluiddram",base:Et.VOLUME,prefixes:wt.NONE,value:36966911e-13,offset:0},{name:"fluidounce",base:Et.VOLUME,prefixes:wt.NONE,value:2957353e-11,offset:0},{name:"gill",base:Et.VOLUME,prefixes:wt.NONE,value:.0001182941,offset:0},{name:"cup",base:Et.VOLUME,prefixes:wt.NONE,value:.0002365882,offset:0},{name:"pint",base:Et.VOLUME,prefixes:wt.NONE,value:.0004731765,offset:0},{name:"quart",base:Et.VOLUME,prefixes:wt.NONE,value:.0009463529,offset:0},{name:"gallon",base:Et.VOLUME,prefixes:wt.NONE,value:.003785412,offset:0},{name:"beerbarrel",base:Et.VOLUME,prefixes:wt.NONE,value:.1173478,offset:0},{name:"oilbarrel",base:Et.VOLUME,prefixes:wt.NONE,value:.1589873,offset:0},{name:"hogshead",base:Et.VOLUME,prefixes:wt.NONE,value:.238481,offset:0},{name:"fldr",base:Et.VOLUME,prefixes:wt.NONE,value:36966911e-13,offset:0},{name:"floz",base:Et.VOLUME,prefixes:wt.NONE,value:2957353e-11,offset:0},{name:"gi",base:Et.VOLUME,prefixes:wt.NONE,value:.0001182941,offset:0},{name:"cp",base:Et.VOLUME,prefixes:wt.NONE,value:.0002365882,offset:0},{name:"pt",base:Et.VOLUME,prefixes:wt.NONE,value:.0004731765,offset:0},{name:"qt",base:Et.VOLUME,prefixes:wt.NONE,value:.0009463529,offset:0},{name:"gal",base:Et.VOLUME,prefixes:wt.NONE,value:.003785412,offset:0},{name:"bbl",base:Et.VOLUME,prefixes:wt.NONE,value:.1173478,offset:0},{name:"obl",base:Et.VOLUME,prefixes:wt.NONE,value:.1589873,offset:0},{name:"g",base:Et.MASS,prefixes:wt.SHORT,value:.001,offset:0},{name:"gram",base:Et.MASS,prefixes:wt.LONG,value:.001,offset:0},{name:"ton",base:Et.MASS,prefixes:wt.SHORT,value:907.18474,offset:0},{name:"tonne",base:Et.MASS,prefixes:wt.SHORT,value:1e3,offset:0},{name:"grain",base:Et.MASS,prefixes:wt.NONE,value:6479891e-11,offset:0},{name:"dram",base:Et.MASS,prefixes:wt.NONE,value:.0017718451953125,offset:0},{name:"ounce",base:Et.MASS,prefixes:wt.NONE,value:.028349523125,offset:0},{name:"poundmass",base:Et.MASS,prefixes:wt.NONE,value:.45359237,offset:0},{name:"hundredweight",base:Et.MASS,prefixes:wt.NONE,value:45.359237,offset:0},{name:"stick",base:Et.MASS,prefixes:wt.NONE,value:.115,offset:0},{name:"gr",base:Et.MASS,prefixes:wt.NONE,value:6479891e-11,offset:0},{name:"dr",base:Et.MASS,prefixes:wt.NONE,value:.0017718451953125,offset:0},{name:"oz",base:Et.MASS,prefixes:wt.NONE,value:.028349523125,offset:0},{name:"lbm",base:Et.MASS,prefixes:wt.NONE,value:.45359237,offset:0},{name:"cwt",base:Et.MASS,prefixes:wt.NONE,value:45.359237,offset:0},{name:"s",base:Et.TIME,prefixes:wt.SHORT,value:1,offset:0},{name:"min",base:Et.TIME,prefixes:wt.NONE,value:60,offset:0},{name:"h",base:Et.TIME,prefixes:wt.NONE,value:3600,offset:0},{name:"seconds",base:Et.TIME,prefixes:wt.LONG,value:1,offset:0},{name:"second",base:Et.TIME,prefixes:wt.LONG,value:1,offset:0},{name:"sec",base:Et.TIME,prefixes:wt.LONG,value:1,offset:0},{name:"minutes",base:Et.TIME,prefixes:wt.NONE,value:60,offset:0},{name:"minute",base:Et.TIME,prefixes:wt.NONE,value:60,offset:0},{name:"hours",base:Et.TIME,prefixes:wt.NONE,value:3600,offset:0},{name:"hour",base:Et.TIME,prefixes:wt.NONE,value:3600,offset:0},{name:"day",base:Et.TIME,prefixes:wt.NONE,value:86400,offset:0},{name:"days",base:Et.TIME,prefixes:wt.NONE,value:86400,offset:0},{name:"rad",base:Et.ANGLE,prefixes:wt.NONE,value:1,offset:0},{name:"deg",base:Et.ANGLE,prefixes:wt.NONE,value:.017453292519943295,offset:0},{name:"grad",base:Et.ANGLE,prefixes:wt.NONE,value:.015707963267948967,offset:0},{name:"cycle",base:Et.ANGLE,prefixes:wt.NONE,value:6.283185307179586,offset:0},{name:"A",base:Et.CURRENT,prefixes:wt.SHORT,value:1,offset:0},{name:"ampere",base:Et.CURRENT,prefixes:wt.LONG,value:1,offset:0},{name:"K",base:Et.TEMPERATURE,prefixes:wt.NONE,value:1,offset:0},{name:"degC",base:Et.TEMPERATURE,prefixes:wt.NONE,value:1,offset:273.15},{name:"degF",base:Et.TEMPERATURE,prefixes:wt.NONE,value:1/1.8,offset:459.67},{name:"degR",base:Et.TEMPERATURE,prefixes:wt.NONE,value:1/1.8,offset:0},{name:"kelvin",base:Et.TEMPERATURE,prefixes:wt.NONE,value:1,offset:0},{name:"celsius",base:Et.TEMPERATURE,prefixes:wt.NONE,value:1,offset:273.15},{name:"fahrenheit",base:Et.TEMPERATURE,prefixes:wt.NONE,value:1/1.8,offset:459.67},{name:"rankine",base:Et.TEMPERATURE,prefixes:wt.NONE,value:1/1.8,offset:0},{name:"mol",base:Et.AMOUNT_OF_SUBSTANCE,prefixes:wt.NONE,value:1,offset:0},{name:"mole",base:Et.AMOUNT_OF_SUBSTANCE,prefixes:wt.NONE,value:1,offset:0},{name:"cd",base:Et.LUMINOUS_INTENSITY,prefixes:wt.NONE,value:1,offset:0},{name:"candela",base:Et.LUMINOUS_INTENSITY,prefixes:wt.NONE,value:1,offset:0},{name:"N",base:Et.FORCE,prefixes:wt.SHORT,value:1,offset:0},{name:"newton",base:Et.FORCE,prefixes:wt.LONG,value:1,offset:0},{name:"lbf",base:Et.FORCE,prefixes:wt.NONE,value:4.4482216152605,offset:0},{name:"poundforce",base:Et.FORCE,prefixes:wt.NONE,value:4.4482216152605,offset:0},{name:"b",base:Et.BIT,prefixes:wt.BINARY_SHORT,value:1,offset:0},{name:"bits",base:Et.BIT,prefixes:wt.BINARY_LONG,value:1,offset:0},{name:"B",base:Et.BIT,prefixes:wt.BINARY_SHORT,value:8,offset:0},{name:"bytes",base:Et.BIT,prefixes:wt.BINARY_LONG,value:8,offset:0}],yt.Complex=i,i.parse=function(e){function n(){for(;" "==u||" "==u;)s()
}function r(e){return e>="0"&&"9">=e||"."==e}function a(e){return e>="0"&&"9">=e}function s(){f++,u=e[f]}function o(){var e="";for("+"==u&&s(),"-"==u&&(e+=u,s());r(u);)e+=u,s();if("E"==u||"e"==u){if(e+=u,s(),("+"==u||"-"==u)&&(e+=u,s()),!a(u))return null;for(;a(u);)e+=u,s()}return e}var f=-1,u="";if(!t(e))return null;s(),n();var h=o();if(h){if("I"==u||"i"==u)return s(),n(),u?null:new i(0,Number(h));n();var c=u;if("+"!=c&&"-"!=c)return n(),u?null:new i(Number(h),0);s(),n();var l=o();return l?"I"!=u&&"i"!=u?null:("-"==c&&(l="-"==l[0]?"+"+l.substring(1):"-"+l),s(),n(),u?null:new i(Number(h),Number(l))):null}return null},i.prototype.copy=function(){return new i(this.re,this.im)},i.prototype.toString=function(){var e="";return e=0==this.im?xt.format(this.re):0==this.re?1==this.im?"i":-1==this.im?"-i":xt.format(this.im)+"i":this.im>0?1==this.im?xt.format(this.re)+" + i":xt.format(this.re)+" + "+xt.format(this.im)+"i":-1==this.im?xt.format(this.re)+" - i":xt.format(this.re)+" - "+xt.format(Math.abs(this.im))+"i"},i.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"]},yt.E=Math.E,yt.LN2=Math.LN2,yt.LN10=Math.LN10,yt.LOG2E=Math.LOG2E,yt.LOG10E=Math.LOG10E,yt.PI=Math.PI,yt.SQRT1_2=Math.SQRT1_2,yt.SQRT2=Math.SQRT2,yt.I=new i(0,-1),yt.pi=yt.PI,yt.e=yt.E,yt.i=yt.I,yt["import"]=o,o.doc={name:"import",category:"Utils",syntax:["import(string)"],description:"Import functions from a file.",examples:['import("numbers")','import("./mylib.js")'],seealso:[]},yt.help=u,u.doc={name:"help",category:"Utils",syntax:["help(object)"],description:"Display documentation on a function or data type.",examples:['help("sqrt")','help("Complex")'],seealso:[]},yt["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:[]},yt.min=l,l.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"]},yt.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"]},yt["in"]=m,m.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:[]},yt.eye=d,d.doc={name:"eye",category:"Matrix",syntax:["eye(n)","eye(m, n)","eye([m, n])","eye"],description:"Returns the identity matrix with size m-by-n. The matrix has ones on the diagonal and zeros elsewhere.",examples:["eye(3)","eye(3, 5)","a = [1, 2, 3; 4, 5, 6]","eye(size(a))"],seealso:["diag","ones","range","size","transpose","zeros"]},yt.size=g,g.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"]},yt.sin=y,y.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"]},yt.atan2=x,x.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"]},yt.asin=E,E.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"]},yt.atan=w,w.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"]},yt.csc=N,N.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"]},yt.cos=b,b.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"]},yt.cot=M,M.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"]},yt.sec=O,O.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"]},yt.tan=T,T.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"]},yt.acos=S,S.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"]},yt.arg=A,A.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"]},yt.conj=k,k.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"]},yt.im=_,_.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"]},yt.re=U,U.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"]},yt.cube=q,q.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"]},yt.divide=L,L.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"]},yt.smallereq=R,R.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"]},yt.round=I,I.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"]},yt.equal=B,B.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"]},yt.unequal=G,G.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"]},yt.log10=D,D.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"]},yt.fix=Y,Y.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"]},yt.add=V,V.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"]},yt.mod=z,z.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:[]},yt.exp=F,F.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"]},yt.largereq=H,H.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"]},yt.sqrt=j,j.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"]},yt.square=K,K.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"]},yt.larger=W,W.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"]},yt.sign=X,X.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"]},yt.unaryminus=Z,Z.doc={name:"unaryminus",category:"Operators",syntax:["-x","unaryminus(x)"],description:"Inverse the sign of a value.",examples:["-4.5","-(-5.6)"],seealso:["add","subtract"]},yt.smaller=Q,Q.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"]},yt.abs=J,J.doc={name:"abs",category:"Arithmetic",syntax:["abs(x)"],description:"Compute the absolute value.",examples:["abs(3.5)","abs(-4.2)"],seealso:["sign"]},yt.log=$,$.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"]},yt.pow=et,et.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"]},yt.floor=nt,nt.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"]},yt.ceil=rt,rt.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"]},yt.multiply=it,it.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"]},yt.subtract=st,st.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"]},yt.random=ot,ot.doc={name:"random",category:"Probability",syntax:["random()"],description:"Return a random number between 0 and 1.",examples:["random()","100 * random()"],seealso:[]},yt.factorial=ft,ft.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:[]},yt.parser.node.Node=ut,ut.prototype.eval=function(){throw Error("Cannot evaluate a Node interface")},ut.prototype.toString=function(){return""},ht.prototype=new ut,yt.parser.node.Symbol=ht,ht.prototype.hasParams=function(){return void 0!=this.params&&this.params.length>0},ht.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)},ht.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},ct.prototype=new ut,yt.parser.node.Constant=ct,ct.prototype.eval=function(){return this.value},ct.prototype.toString=function(){return this.value?""+this.value:""},lt.prototype=new ut,yt.parser.node.Block=lt,lt.prototype.add=function(e,t){var n=this.params.length;this.params[n]=e,this.visible[n]=void 0!=t?t:!0},lt.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},lt.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]"},pt.prototype=new ut,yt.parser.node.Assignment=pt,pt.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},pt.prototype.toString=function(){var e="";return e+=this.name,this.params&&this.params.length&&(e+="("+this.params.join(", ")+")"),e+=" = ",e+=""+this.expr},mt.prototype=new ut,yt.parser.node.FunctionAssignment=mt,mt.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},mt.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},mt.prototype.toString=function(){return""+this.def},yt.parser.node.Scope=dt,dt.prototype.createNestedScope=function(){var e=new dt(this);return this.nestedScopes||(this.nestedScopes=[]),this.nestedScopes.push(e),e},dt.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()},dt.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},dt.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},dt.prototype.createLink=function(e){var t=this.links[e];return t||(t=this.createSymbol(e),this.links[e]=t),t},dt.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},dt.prototype.createUpdate=function(e){var t=this.updates[e];return t||(t=this.createLink(e),this.updates[e]=t),t},dt.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,yt.PI);if("e"==t)return n(t,yt.E);if("i"==t)return n(t,new i(0,1));var f=yt[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},dt.prototype.removeLink=function(e){delete this.links[e]},dt.prototype.removeDef=function(e){delete this.defs[e]},dt.prototype.removeUpdate=function(e){delete this.updates[e]},dt.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()})},dt.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},dt.prototype.hasDef=function(e){return void 0!=this.defs[e]},dt.prototype.hasUpdate=function(e){return void 0!=this.updates[e]},dt.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},yt.parser.Parser=gt,gt.prototype.parse=function(e,t){return this.expr=e||"",t||(t=this.scope),this.parse_start(t)},gt.prototype.eval=function(e){var t=this.parse(e);return t.eval()},gt.prototype.get=function(e){var t=this.scope.findDef(e);return t?t.value:void 0},gt.prototype.put=function(e,t){this.scope.createDef(e,t)},gt.prototype.clear=function(){this.scope.clear()},gt.prototype.getChar=function(){this.index++,this.c=this.expr.charAt(this.index)},gt.prototype.getFirstChar=function(){this.index=0,this.c=this.expr.charAt(0)},gt.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()}},gt.prototype.isDelimiter=function(e){return"&"==e||"|"==e||"<"==e||">"==e||"="==e||"+"==e||"/"==e||"*"==e||"%"==e||"^"==e||","==e||";"==e||"\n"==e||"!"==e},gt.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},gt.prototype.isAlpha=function(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e||"_"==e},gt.prototype.isDigitDot=function(e){return e>="0"&&"9">=e||"."==e},gt.prototype.isDigit=function(e){return e>="0"&&"9">=e},gt.prototype.parse_start=function(e){this.getFirstChar(),this.getToken();var t;if(t=""==this.token?new ct(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},gt.prototype.parse_ans=function(e){var t=this.parse_function_assignment(e);if(!(t instanceof pt)){var n="ans",r=void 0,i=e.createDef(n);return new pt(n,r,t,i)}return t},gt.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 lt,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)},gt.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 mt(t,r,i,o,f)}return this.parse_assignment(e)},gt.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 ht))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 pt(r,i,a,s)}return n},gt.prototype.parse_range=function(e){var t=this.parse_conditions(e);return t},gt.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=yt[n[r]];this.getToken();var a=[t,this.parse_bitwise_conditions(e)];t=new ht(r,i,a)}return t},gt.prototype.parse_bitwise_conditions=function(e){var t=this.parse_comparison(e);return t},gt.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=yt[n[r]];this.getToken();var a=[t,this.parse_addsubtract(e)];t=new ht(r,i,a)}return t},gt.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=yt[n[r]];this.getToken();var a=[t,this.parse_multiplydivide(e)];t=new ht(r,i,a)}return t},gt.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=yt[n[r]];this.getToken();var a=[t,this.parse_pow(e)];t=new ht(r,i,a)}return t},gt.prototype.parse_pow=function(e){for(var t=this.parse_factorial(e);"^"==this.token;){var n=this.token,r=et;this.getToken();var i=[t,this.parse_factorial(e)];t=new ht(n,r,i)}return t},gt.prototype.parse_factorial=function(e){for(var t=this.parse_unaryminus(e);"!"==this.token;){var n=this.token,r=ft;this.getToken();var i=[t];t=new ht(n,r,i)}return t},gt.prototype.parse_unaryminus=function(e){if("-"==this.token){var t=this.token,n=Z;this.getToken();var r=[this.parse_plot(e)];return new ht(t,n,r)}return this.parse_plot(e)},gt.prototype.parse_plot=function(e){return this.parse_symbol(e)},gt.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 ht(t,n,r);return i}return this.parse_string(e)},gt.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},gt.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 ct(t);return r}return this.parse_matrix(e)},gt.prototype.parse_matrix=function(e){return this.parse_number(e)},gt.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 i(0,n),this.getToken(),new ct(r);if(e.isUnit(this.token))return r=new e(n,this.token),this.getToken(),new ct(r);throw this.createTypeError('Unknown unit "'+this.token+'"')}var a=new ct(n);return a}return this.parse_parentheses(t)},gt.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)},gt.prototype.parse_end=function(){throw""==this.token?this.createSyntaxError("Unexpected end of expression"):this.createSyntaxError("Value expected")},gt.prototype.row=function(){return void 0},gt.prototype.col=function(){return this.index-this.token.length+1},gt.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+")"},gt.prototype.createSyntaxError=function(e){return new SyntaxError(this.createErrorMessage(e))},gt.prototype.createTypeError=function(e){return new TypeError(this.createErrorMessage(e))},gt.prototype.createError=function(e){return Error(this.createErrorMessage(e))},yt.parser.Workspace=vt,vt.prototype.clear=function(){this.nodes={},this.firstNode=void 0,this.lastNode=void 0},vt.prototype.append=function(e){var t=this._getNewId(),n=this.lastNode?this.lastNode.scope:this.scope,r=new dt(n),i=new vt.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},vt.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 dt(a),o=new vt.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},vt.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)},vt.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)},vt.prototype.replace=function(e,t){var n=this.nodes[t];if(!n)throw'Node with id "'+t+'" not found';var r=[t];vt._merge(r,this.getDependencies(t));var i=n.previousNode;n.nextNode,i?i.scope:this.scope,n.setExpr(e),vt._merge(r,this.getDependencies(t)),this._update(r)},vt.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)},vt.Node.prototype.setExpr=function(e){this.expression=e||"",this.scope.clear(),this._parse()},vt.Node.prototype.getExpr=function(){return this.expression},vt.Node.prototype.getResult=function(){return this.result},vt.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 ct(t)}},vt.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},vt._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)}},vt.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);vt._merge(n,h)}f.hasDef(t)&&(s.splice(u,1),u--),u++}o=o.nextNode}}return n},vt.prototype.getExpr=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';return t.getExpr()},vt.prototype.getResult=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';return t.getResult()},vt.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)}},vt.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}},vt.prototype._getNewId=function(){return this.idMax++,this.idMax},vt.prototype.toString=function(){return JSON.stringify(this.toJSON())},vt.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}})();