mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
29 lines
83 KiB
JavaScript
29 lines
83 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.5.0-SNAPSHOT
|
|
* @date 2013-03-31
|
|
*
|
|
* @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(e){return e instanceof Boolean||"boolean"==typeof e}function t(e,n){if(this.constructor!=t)throw new SyntaxError("Complex constructor must be called with the new operator");if(null!=e&&!p(e)||null!=n&&!p(n))throw new TypeError("Two numbers or a single string expected in Complex constructor");this.re=e||0,this.im=n||0}function n(e){if(this.constructor!=n)throw new SyntaxError("Matrix constructor must be called with the new operator");if(e instanceof n||e instanceof d)this._data=e.toArray();else if(e instanceof Array)this._data=e;else{if(null!=e)throw new TypeError("Unsupported type of data ("+Dt.typeof(e)+")");this._data=[]}this._size=jt.size(this._data)}function r(e,t){if(!p(t)||!v(t)||0>t)throw new TypeError("Index must contain positive integers (value: "+t+")");if(t>e.length-1)throw new RangeError("Index out of range ("+t+">"+(e.length-1)+")");return e[t]}function a(e,t){return t.forEach(function(t){e=r(e,t)}),Tt(e)}function i(e,t){var n=t[0];return n.map?n.map(function(t){return r(e,t)}):[r(e,n)]}function o(e,t){var n=t[0],a=t[1];if(n.map)return a.map?n.map(function(t){var n=r(e,t);return a.map(function(e){return r(n,e)})}):n.map(function(t){return[r(r(e,t),a)]});if(a.map){var i=r(e,n);return[a.map(function(e){return r(i,e)})]}return[[r(r(e,n),a)]]}function s(e,t,n){var a=n==t.length-1,i=t[n],o=function(i){var o=r(e,i);return a?o:s(o,t,n+1)};return i.map?i.map(o):[o(i)]}function f(e,t,n){if(!p(t)||!v(t)||0>t)throw new TypeError("Index must contain positive integers (value: "+t+")");if(n instanceof Array)throw new TypeError("Dimension mismatch, value expected instead of array");e[t]=n}function u(e,t,n,r){var a=!1;n.length>t.length&&(a=!0);for(var i=0;n.length>i;i++){var o=n[i];if(!p(o)||!v(o)||0>o)throw new TypeError("Positive integer expected as index in method get");(null==t[i]||o+1>t[i])&&(t[i]=o+1,a=!0)}a&&jt.resize(e,t,0);var s=t.length;n.forEach(function(t,n){s-1>n?e=e[t]:e[t]=r})}function l(e,t,n,r){var a=n[0];if(!p(a)||!v(a)||0>a)throw new TypeError("Positive integer expected as index in method get");a+1>t[0]&&jt.resize(e,[a+1],0),e[a]=r}function c(e,t,n,r){var a=n[0],i=n[1];if(!p(a)||!v(a)||0>a||!p(i)||!v(i)||0>i)throw new TypeError("Positive integer expected as index in method get");var o=!1;a+1>(t[0]||0)&&(t[0]=a+1,o=!0),i+1>(t[1]||0)&&(t[1]=i+1,o=!0),o&&jt.resize(e,t,0),e[a][i]=r}function h(e,t,n,r,a){var i=r==n.length-1,o=n[r],s=function(o,s){if(i)f(e,o,a[s]),e.length>(t[r]||0)&&(t[r]=e.length);else{var u=e[o];u instanceof Array||(e[o]=u=[u],e.length>(t[r]||0)&&(t[r]=e.length)),h(u,t,n,r+1,a[s])}};if(o.map){if(o.length!=a.length)throw new RangeError("Dimensions mismatch ("+o.length+" != "+a.length+")");o.map(s)}else s(o)}function m(e){for(var t=0,n=e.length;n>t;t++){var r=e[t];r instanceof Array?m(r):void 0==r&&(e[t]=0)}}function p(e){return e instanceof Number||"number"==typeof e}function v(e){return e==Math.round(e)}function d(e,t,n){if(this.constructor!=d)throw new SyntaxError("Range constructor must be called with the new operator");if(null!=e&&!p(e))throw new TypeError("Parameter start must be a number");if(null!=n&&!p(n))throw new TypeError("Parameter end must be a number");if(null!=t&&!p(t))throw new TypeError("Parameter step must be a number");this.start=null!=e?e:0,this.end=null!=n?n:0,this.step=null!=t?t:1}function g(e){return e instanceof String||"string"==typeof e}function y(e,t){if(this.constructor!=y)throw Error("Unit constructor must be called with the new operator");if(null!=e&&!p(e))throw Error("First parameter in Unit constructor must be a number");if(null!=t&&!g(t))throw Error("Second parameter in Unit constructor must be a string");if(this.value=1,this.unit=y.UNIT_NONE,this.prefix=y.PREFIX_NONE,this.hasUnit=!1,this.hasValue=!1,this.fixPrefix=!1,null!=t){for(var n=y.UNITS,r=!1,a=0,i=n.length;i>a;a++){var o=n[a];if(y.endsWith(t,o.name)){var s=t.length-o.name.length,f=t.substring(0,s),u=o.prefixes[f];if(void 0!==u){this.unit=o,this.prefix=u,this.hasUnit=!0,r=!0;break}}}if(!r)throw Error('String "'+t+'" is no unit')}null!=e?(this.value=this._normalize(e),this.hasValue=!0):this.value=this._normalize(1)}function x(e,t){var n=void 0;if(2==arguments.length){var r=_t(t);n="Function "+e+" does not support a parameter of type "+r}else if(arguments.length>2){for(var a=[],i=1;arguments.length>i;i++)a.push(_t(arguments[i]));n="Function "+e+" does not support a parameters of type "+a.join(", ")}else n="Unsupported parameter in function "+e;return new TypeError(n)}function w(e,t,n,r){var a="Wrong number of arguments in function "+e+" ("+t+" provided, "+n+(void 0!=r?"-"+r:"")+" expected)";return new SyntaxError(a)}function E(e){if(1!=arguments.length)throw w("abs",arguments.length,1);if(p(e))return Math.abs(e);if(e instanceof t)return Math.sqrt(e.re*e.re+e.im*e.im);if(e instanceof Array)return jt.map(e,E);if(e.valueOf()!==e)return E(e.valueOf());throw x("abs",e)}function N(e,n){if(2!=arguments.length)throw w("add",arguments.length,2);if(p(e)){if(p(n))return e+n;if(n instanceof t)return new t(e+n.re,n.im)}else if(e instanceof t){if(p(n))return new t(e.re+n,e.im);if(n instanceof t)return new t(e.re+n.re,e.im+n.im)}else if(e instanceof y&&n instanceof y){if(!e.equalBase(n))throw Error("Units do not match");if(!e.hasValue)throw Error("Unit on left hand side of operator + has no value");if(!n.hasValue)throw Error("Unit on right hand side of operator + has no value");var r=e.clone();return r.value+=n.value,r.fixPrefix=!1,r}if(g(e)||g(n))return e+n;if(e instanceof Array||n instanceof Array)return jt.map2(e,n,N);if(e.valueOf()!==e)return N(e.valueOf());throw x("add",e,n)}function b(e){if(1!=arguments.length)throw w("ceil",arguments.length,1);if(p(e))return Math.ceil(e);if(e instanceof t)return new t(Math.ceil(e.re),Math.ceil(e.im));if(e instanceof Array)return jt.map(e,b);if(e.valueOf()!==e)return b(e.valueOf());throw x("ceil",e)}function O(e){if(1!=arguments.length)throw w("cube",arguments.length,1);if(p(e))return e*e*e;if(e instanceof t)return C(C(e,e),e);if(e instanceof Array)return C(C(e,e),e);if(e.valueOf()!==e)return O(e.valueOf());throw x("cube",e)}function M(e,n){if(2!=arguments.length)throw w("divide",arguments.length,2);if(p(e)){if(p(n))return e/n;if(n instanceof t)return A(new t(e,0),n)}if(e instanceof t){if(p(n))return A(e,new t(n,0));if(n instanceof t)return A(e,n)}if(e instanceof y&&p(n)){var r=e.clone();return r.value/=n,r}if(e instanceof Array&&!(n instanceof Array))return jt.map2(e,n,M);if(e.valueOf()!==e||n.valueOf()!==n)return M(e.valueOf(),n.valueOf());throw x("divide",e,n)}function A(e,n){var r=n.re*n.re+n.im*n.im;return new t((e.re*n.re+e.im*n.im)/r,(e.im*n.re-e.re*n.im)/r)}function S(e,n){if(2!=arguments.length)throw w("equal",arguments.length,2);if(p(e)){if(p(n))return e==n;if(n instanceof t)return e==n.re&&0==n.im}if(e instanceof t){if(p(n))return e.re==n&&0==e.im;if(n instanceof t)return e.re==n.re&&e.im==n.im}if(e instanceof y&&n instanceof y){if(!e.equalBase(n))throw Error("Cannot compare units with different base");return e.value==n.value}if(g(e)||g(n))return e==n;if(e instanceof Array||n instanceof Array)return jt.map2(e,n,S);if(e.valueOf()!==e||n.valueOf()!==n)return S(e.valueOf(),n.valueOf());throw x("equal",e,n)}function T(e){if(1!=arguments.length)throw w("exp",arguments.length,1);if(p(e))return Math.exp(e);if(e instanceof t){var n=Math.exp(e.re);return new t(n*Math.cos(e.im),n*Math.sin(e.im))}if(e instanceof Array)return jt.map(e,T);if(e.valueOf()!==e)return T(e.valueOf());throw x("exp",e)}function q(e){if(1!=arguments.length)throw w("fix",arguments.length,1);if(p(e))return value>0?Math.floor(e):Math.ceil(e);if(e instanceof t)return new t(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 jt.map(e,q);if(e.valueOf()!==e)return q(e.valueOf());throw x("fix",e)}function U(e){if(1!=arguments.length)throw w("floor",arguments.length,1);if(p(e))return Math.floor(e);if(e instanceof t)return new t(Math.floor(e.re),Math.floor(e.im));if(e instanceof Array)return jt.map(e,U);if(e.valueOf()!==e)return U(e.valueOf());throw x("floor",e)}function z(e,n){if(2!=arguments.length)throw w("larger",arguments.length,2);if(p(e)){if(p(n))return e>n;if(n instanceof t)return e>E(n)}if(e instanceof t){if(p(n))return E(e)>n;if(n instanceof t)return E(e)>E(n)}if(e instanceof y&&n instanceof y){if(!e.equalBase(n))throw Error("Cannot compare units with different base");return e.value>n.value}if(g(e)||g(n))return e>n;if(e instanceof Array||n instanceof Array)return jt.map2(e,n,S);if(e.valueOf()!==e||n.valueOf()!==n)return z(e.valueOf(),n.valueOf());throw x("larger",e,n)}function L(e,n){if(2!=arguments.length)throw w("largereq",arguments.length,2);if(p(e)){if(p(n))return e>=n;if(n instanceof t)return e>=E(n)}if(e instanceof t){if(p(n))return E(e)>=n;if(n instanceof t)return E(e)>=E(n)}if(e instanceof y&&n instanceof y){if(!e.equalBase(n))throw Error("Cannot compare units with different base");return e.value>=n.value}if(g(e)||g(n))return e>=n;if(e instanceof Array||n instanceof Array)return jt.map2(e,n,L);if(e.valueOf()!==e||n.valueOf()!==n)return L(e.valueOf(),n.valueOf());throw x("largereq",e,n)}function R(e,n){if(1!=arguments.length&&2!=arguments.length)throw w("log",arguments.length,1,2);if(void 0!==n)return M(R(e),R(n));if(p(e))return e>=0?Math.log(e):R(new t(e,0));if(e instanceof t)return new t(Math.log(Math.sqrt(e.re*e.re+e.im*e.im)),Math.atan2(e.im,e.re));if(e instanceof Array)return jt.map(e,R);if(e.valueOf()!==e||n.valueOf()!==n)return R(e.valueOf(),n.valueOf());throw x("log",e,n)}function _(e){if(1!=arguments.length)throw w("log10",arguments.length,1);if(p(e))return e>=0?Math.log(e)/Math.LN10:_(new t(e,0));if(e instanceof t)return new t(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 jt.map(e,_);if(e.valueOf()!==e)return _(e.valueOf());throw x("log10",e)}function I(e,n){if(2!=arguments.length)throw w("mod",arguments.length,2);if(p(e)){if(p(n))return e%n;if(n instanceof t&&0==n.im)return e%n.re}else if(e instanceof t&&0==e.im){if(p(n))return e.re%n;if(n instanceof t&&0==n.im)return e.re%n.re}if(e instanceof Array||n instanceof Array)return jt.map2(e,n,I);if(e.valueOf()!==e||n.valueOf()!==n)return I(e.valueOf(),n.valueOf());throw x("mod",e,n)}function C(e,n){if(2!=arguments.length)throw w("multiply",arguments.length,2);if(p(e)){if(p(n))return e*n;if(n instanceof t)return P(new t(e,0),n);if(n instanceof y)return i=n.clone(),i.value*=e,i}else if(e instanceof t){if(p(n))return P(e,new t(n,0));if(n instanceof t)return P(e,n)}else if(e instanceof y){if(p(n))return i=e.clone(),i.value*=n,i}else if(e instanceof Array){if(n instanceof Array){var r=jt.size(e),a=jt.size(n);if(2!=r.length)throw Error("Can only multiply a 2 dimensional matrix (A has "+r.length+" dimensions)");if(2!=a.length)throw Error("Can only multiply a 2 dimensional matrix (B has "+a.length+" dimensions)");if(r[1]!=a[0])throw new RangeError("Dimensions mismatch in multiplication. Columns of A must match rows of B (A is "+r[0]+"x"+r[1]+", B is "+a[0]+"x"+a[1]+", "+a[1]+" != "+a[0]+")");for(var i=[],o=r[0],s=a[1],f=r[1],u=0;o>u;u++){i[u]=[];for(var l=0;s>l;l++){for(var c=null,h=0;f>h;h++){var m=C(e[u][h],n[h][l]);c=null==c?m:N(c,m)}i[u][l]=c}}return i}return jt.map2(e,n,C)}if(n instanceof Array)return jt.map2(e,n,C);if(e.valueOf()!==e||n.valueOf()!==n)return C(e.valueOf(),n.valueOf());throw x("multiply",e,n)}function P(e,n){return new t(e.re*n.re-e.im*n.im,e.re*n.im+e.im*n.re)}function B(e,n){if(2!=arguments.length)throw w("pow",arguments.length,2);if(p(e)){if(p(n))return v(n)||e>=0?Math.pow(e,n):G(new t(e,0),new t(n,0));if(n instanceof t)return G(new t(e,0),n)}else if(e instanceof t){if(p(n))return G(e,new t(n,0));if(n instanceof t)return G(e,n)}else if(e instanceof Array){if(!p(n)||!v(n)||0>n)throw new TypeError("For A^b, b must be a positive integer (value is "+n+")");var r=jt.size(e);if(2!=r.length)throw Error("For A^b, A must be 2 dimensional (A has "+r.length+" dimensions)");if(r[0]!=r[1])throw Error("For A^b, A must be square (size is "+r[0]+"x"+r[1]+")");if(0==n)return st(r[0]);for(var a=e,i=1;n>i;i++)a=C(e,a);return a}if(e.valueOf()!==e||n.valueOf()!==n)return B(e.valueOf(),n.valueOf());throw x("pow",e,n)}function G(e,t){var n=R(e),r=C(n,t);return T(r)}function k(e,n){if(1!=arguments.length&&2!=arguments.length)throw w("round",arguments.length,1,2);if(void 0==n){if(p(e))return Math.round(e);if(e instanceof t)return new t(Math.round(e.re),Math.round(e.im));if(e instanceof Array&&jt.map(e,k),e.valueOf()!==e)return k(e.valueOf());throw x("round",e)}if(!p(n))throw new TypeError("Number of digits in function round must be an integer");if(n!==Math.round(n))throw new TypeError("Number of digits in function round must be integer");if(0>n||n>9)throw Error("Number of digits in function round must be in te range of 0-9");if(p(e))return V(e,n);if(e instanceof t)return new t(V(e.re,n),V(e.im,n));if(e instanceof Array||n instanceof Array)return jt.map2(e,n,k);if(e.valueOf()!==e||n.valueOf()!==n)return z(e.valueOf(),n.valueOf());throw x("round",e,n)}function V(e,t){var n=Math.pow(10,void 0!=t?t:Dt.options.precision);return Math.round(e*n)/n}function D(e){if(1!=arguments.length)throw w("sign",arguments.length,1);if(p(e)){var n;return n=e>0?1:0>e?-1:0}if(e instanceof t){var r=Math.sqrt(e.re*e.re+e.im*e.im);return new t(e.re/r,e.im/r)}if(e instanceof Array)return jt.map(e,n);if(e.valueOf()!==e)return n(e.valueOf());throw x("sign",e)}function j(e,n){if(2!=arguments.length)throw w("smaller",arguments.length,2);if(p(e)){if(p(n))return n>e;if(n instanceof t)return E(n)>e}if(e instanceof t){if(p(n))return n>E(e);if(n instanceof t)return E(e)<E(n)}if(e instanceof y&&n instanceof y){if(!e.equalBase(n))throw Error("Cannot compare units with different base");return e.value<n.value}if(g(e)||g(n))return n>e;if(e instanceof Array||n instanceof Array)return jt.map2(e,n,j);if(e.valueOf()!==e||n.valueOf()!==n)return j(e.valueOf(),n.valueOf());throw x("smaller",e,n)}function F(e,n){if(2!=arguments.length)throw w("smallereq",arguments.length,2);if(p(e)){if(p(n))return n>=e;if(n instanceof t)return E(n)>=e}if(e instanceof t){if(p(n))return n>=E(e);if(n instanceof t)return E(e)<=E(n)}if(e instanceof y&&n instanceof y){if(!e.equalBase(n))throw Error("Cannot compare units with different base");return e.value<=n.value}if(g(e)||g(n))return n>=e;if(e instanceof Array||n instanceof Array)return jt.map2(e,n,F);if(e.valueOf()!==e||n.valueOf()!==n)return F(e.valueOf(),n.valueOf());throw x("smallereq",e,n)}function H(e){if(1!=arguments.length)throw w("sqrt",arguments.length,1);if(p(e))return e>=0?Math.sqrt(e):H(new t(e,0));if(e instanceof t){var n=Math.sqrt(e.re*e.re+e.im*e.im);return e.im>=0?new t(.5*Math.sqrt(2*(n+e.re)),.5*Math.sqrt(2*(n-e.re))):new t(.5*Math.sqrt(2*(n+e.re)),-.5*Math.sqrt(2*(n-e.re)))}if(e instanceof Array)return jt.map(e,H);if(e.valueOf()!==e)return H(e.valueOf());throw x("sqrt",e)}function Y(e){if(1!=arguments.length)throw w("square",arguments.length,1);if(p(e))return e*e;if(e instanceof t)return C(e,e);if(e instanceof Array)return C(e,e);if(e.valueOf()!==e)return Y(e.valueOf());throw x("square",e)}function W(e,n){if(2!=arguments.length)throw w("subtract",arguments.length,2);if(p(e)){if(p(n))return e-n;if(n instanceof t)return new t(e-n.re,n.im)}else if(e instanceof t){if(p(n))return new t(e.re-n,e.im);if(n instanceof t)return new t(e.re-n.re,e.im-n.im)}else if(e instanceof y&&n instanceof y){if(!e.equalBase(n))throw Error("Units do not match");if(!e.hasValue)throw Error("Unit on left hand side of operator - has no value");if(!n.hasValue)throw Error("Unit on right hand side of operator - has no value");var r=e.clone();return r.value-=n.value,r.fixPrefix=!1,r}if(e instanceof Array||n instanceof Array)return jt.map2(e,n,W);if(e.valueOf()!==e||n.valueOf()!==n)return W(e.valueOf(),n.valueOf());throw x("subtract",e,n)}function X(e){if(1!=arguments.length)throw w("unaryminus",arguments.length,1);if(p(e))return-e;if(e instanceof t)return new t(-e.re,-e.im);if(e instanceof y){var n=e.clone();return n.value=-e.value,n}if(e instanceof Array)return jt.map(e,X);if(e.valueOf()!==e)return X(e.valueOf());throw x("unaryminus",e)}function Z(e,n){if(2!=arguments.length)throw w("unequal",arguments.length,2);if(p(e)){if(p(n))return e==n;if(n instanceof t)return e==n.re&&0==n.im}if(e instanceof t){if(p(n))return e.re==n&&0==e.im;if(n instanceof t)return e.re==n.re&&e.im==n.im}if(e instanceof y&&n instanceof y){if(!e.equalBase(n))throw Error("Cannot compare units with different base");return e.value==n.value}if(g(e)||g(n))return e==n;if(e instanceof Array||n instanceof Array)return jt.map2(e,n,Z);if(e.valueOf()!==e||n.valueOf()!==n)return Z(e.valueOf(),n.valueOf());throw x("unequal",e,n)}function K(e){if(1!=arguments.length)throw w("arg",arguments.length,1);if(p(e))return Math.atan2(0,e);if(e instanceof t)return Math.atan2(e.im,e.re);if(e instanceof Array)return jt.map(e,K);if(e.valueOf()!==e)return K(e.valueOf());throw x("arg",e)}function Q(e){if(1!=arguments.length)throw w("conj",arguments.length,1);if(p(e))return e;if(e instanceof t)return new t(e.re,-e.im);if(e instanceof Array)return jt.map(e,Q);if(e.valueOf()!==e)return Q(e.valueOf());throw x("conj",e)}function J(e){if(1!=arguments.length)throw w("im",arguments.length,1);if(p(e))return 0;if(e instanceof t)return e.im;if(e instanceof Array)return jt.map(e,J);if(e.valueOf()!==e)return J(e.valueOf());throw x("im",e)}function $(e){if(1!=arguments.length)throw w("re",arguments.length,1);if(p(e))return e;if(e instanceof t)return e.re;if(e instanceof Array)return jt.map(e,$);if(e.valueOf()!==e)return $(e.valueOf());throw x("re",e)}function et(){switch(arguments.length){case 0:return new t(0,0);case 1:var e=arguments[0];if(!g(e))throw new TypeError("Two numbers or a single string expected in function complex");var n=t.parse(e);if(n)return n;throw new SyntaxError('String "'+e+'" is no valid complex number');case 2:return new t(arguments[0],arguments[1]);default:throw w("complex",arguments.length,0,2)}}function tt(e){if(arguments.length>1)throw w("matrix",arguments.length,0,1);return new n(e)}function nt(){return new Dt.expr.Parser}function rt(){switch(arguments.length){case 2:return new d(arguments[0],null,arguments[1]);case 3:return new d(arguments[0],arguments[1],arguments[2]);default:throw w("range",arguments.length,2,3)}}function at(){switch(arguments.length){case 1:var e=arguments[0];if(!g(e))throw new TypeError("A string or a number and string expected in function unit");if(y.isUnit(e))return new y(null,e);var t=y.parse(e);if(t)return t;throw new SyntaxError('String "'+e+'" is no valid unit');case 2:return new y(arguments[0],arguments[1]);default:throw w("unit",arguments.length,1,2)}}function it(){return new Dt.expr.Workspace}function ot(e,t){var r,a,i,o;if(1!=arguments.length&&2!=arguments.length)throw w("diag",arguments.length,1,2);if(t){if(!p(t)||!v(t))throw new TypeError("Second parameter in function diag must be an integer")}else t=0;var s=t>0?t:0,f=0>t?-t:0;e instanceof n||e instanceof d||(e=new n(e));var u;switch(e.isVector()?(e=e.toVector(),u=[e.length]):u=e.size(),u.length){case 1:a=e.valueOf();var l=new n;for(l.resize([a.length+f,a.length+s]),r=l.valueOf(),o=a.length,i=0;o>i;i++)r[i+f][i+s]=Tt(a[i]);return l;case 2:for(a=[],r=e.valueOf(),o=Math.min(u[0]-f,u[1]-s),i=0;o>i;i++)a[i]=Tt(r[i+f][i+s]);return new n(a);default:throw new RangeError("Matrix for function diag must be 2 dimensional")}}function st(){var e=jt.argsToArray(arguments);if(0==e.length)e=[1,1];else if(1==e.length)e[1]=e[0];else if(e.length>2)throw w("identity",num,0,2);var t=e[0],r=e[1];if(!p(t)||!v(t)||1>t)throw Error("Parameters in function identity must be positive integers");if(r&&(!p(r)||!v(r)||1>r))throw Error("Parameters in function identity must be positive integers");var a=new n;a.resize(e);for(var i=Dt.min(e),o=a.valueOf(),s=0;i>s;s++)o[s][s]=1;return a}function ft(){var e=jt.argsToArray(arguments);0==e.length?e=[1,1]:1==e.length&&(e[1]=e[0]);var t=new n,r=1;return t.resize(e,r),t}function ut(e){if(1!=arguments.length)throw w("size",arguments.length,1);if(p(e)||e instanceof t||e instanceof y||null==e)return[];if(g(e))return[e.length];if(e instanceof Array)return jt.size(e);if(e instanceof n||e instanceof d)return e.size();if(e.valueOf()!==e)return ut(e.valueOf());throw x("size",e)}function lt(e){if(1!=arguments.length)throw w("squeeze",arguments.length,1);return e instanceof n||e instanceof d?ct(e.toArray()):e instanceof Array?ct(Tt(e)):Tt(e)}function ct(e){if(1==e.length)return ct(e[0]);for(var t=0,n=e.length;n>t;t++){var r=e[t];r instanceof Array&&(e[t]=ct(r))}return e}function ht(){var e=jt.argsToArray(arguments);0==e.length?e=[1,1]:1==e.length&&(e[1]=e[0]);var t=new n;return t.resize(e),t}function mt(e){if(1!=arguments.length)throw w("factorial",arguments.length,1);if(p(e)){if(!v(e))throw new TypeError("Function factorial can only handle integer values");var t=e,n=t;for(t--;t>1;)n*=t,t--;return 0==n&&(n=1),n}if(e instanceof Array)return jt.map(e,mt);if(e.valueOf()!==e)return mt(e.valueOf());throw x("factorial",e)}function pt(){if(0!=arguments.length)throw w("random",arguments.length,0);return Math.random()}function vt(e){if(0==arguments.length)throw Error("Function sum requires one or more parameters (0 provided)");if(1==arguments.length&&e.valueOf()instanceof Array)return vt.apply(this,e.valueOf());for(var t=arguments[0],n=1,r=arguments.length;r>n;n++){var a=arguments[n];z(a,t)&&(t=a)}return t}function dt(e){if(0==arguments.length)throw Error("Function sum requires one or more parameters (0 provided)");if(1==arguments.length&&e.valueOf()instanceof Array)return dt.apply(this,e.valueOf());for(var t=arguments[0],n=1,r=arguments.length;r>n;n++){var a=arguments[n];j(a,t)&&(t=a)}return t}function gt(e){if(1!=arguments.length)throw w("acos",arguments.length,1);if(p(e))return e>=-1&&1>=e?Math.acos(e):gt(new t(e,0));if(e instanceof t){var n=new t(e.im*e.im-e.re*e.re+1,-2*e.re*e.im),r=H(n),a=new t(r.re-e.im,r.im+e.re),i=R(a);return new t(1.5707963267948966-i.im,i.re)}if(e instanceof Array)return jt.map(e,gt);if(e.valueOf()!==e)return gt(e.valueOf());throw x("acos",e)}function yt(e){if(1!=arguments.length)throw w("asin",arguments.length,1);if(p(e))return e>=-1&&1>=e?Math.asin(e):yt(new t(e,0));if(e instanceof t){var n=e.re,r=e.im,a=new t(r*r-n*n+1,-2*n*r),i=H(a),o=new t(i.re-r,i.im+n),s=R(o);return new t(s.im,-s.re)}if(e instanceof Array)return jt.map(e,yt);if(e.valueOf()!==e)return yt(e.valueOf());throw x("asin",e)}function xt(e){if(1!=arguments.length)throw w("atan",arguments.length,1);if(p(e))return Math.atan(e);if(e instanceof t){var n=e.re,r=e.im,a=n*n+(1-r)*(1-r),i=new t((1-r*r-n*n)/a,-2*n/a),o=R(i);return new t(-.5*o.im,.5*o.re)}if(e instanceof Array)return jt.map(e,xt);if(e.valueOf()!==e)return xt(e.valueOf());throw x("atan",e)}function wt(e,n){if(2!=arguments.length)throw w("atan2",arguments.length,2);if(p(e)){if(p(n))return Math.atan2(e,n);if(n instanceof t)return Math.atan2(e,n.re)}else if(e instanceof t){if(p(n))return Math.atan2(e.re,n);if(n instanceof t)return Math.atan2(e.re,n.re)}if(n instanceof Array||e instanceof Array)return jt.map2(e,n,wt);if(n.valueOf()!==n||e.valueOf()!==e)return wt(e.valueOf(),n.valueOf());throw x("atan2",e,n)}function Et(e){if(1!=arguments.length)throw w("cos",arguments.length,1);if(p(e))return Math.cos(e);if(e instanceof t)return new t(.5*Math.cos(e.re)*(Math.exp(-e.im)+Math.exp(e.im)),.5*Math.sin(e.re)*(Math.exp(-e.im)-Math.exp(e.im)));if(e instanceof y){if(!e.hasBase(y.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.cos(e.value)}if(e instanceof Array)return jt.map(e,Et);if(e.valueOf()!==e)return Et(e.valueOf());throw x("cos",e)}function Nt(e){if(1!=arguments.length)throw w("cot",arguments.length,1);if(p(e))return 1/Math.tan(e);if(e instanceof t){var n=Math.exp(-4*e.im)-2*Math.exp(-2*e.im)*Math.cos(2*e.re)+1;return new t(2*Math.exp(-2*e.im)*Math.sin(2*e.re)/n,(Math.exp(-4*e.im)-1)/n)}if(e instanceof y){if(!e.hasBase(y.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cot is no angle");return 1/Math.tan(e.value)}if(e instanceof Array)return jt.map(e,Nt);if(e.valueOf()!==e)return Nt(e.valueOf());throw x("cot",e)}function bt(e){if(1!=arguments.length)throw w("csc",arguments.length,1);if(p(e))return 1/Math.sin(e);if(e instanceof t){var n=.25*(Math.exp(-2*e.im)+Math.exp(2*e.im))-.5*Math.cos(2*e.re);return new t(.5*Math.sin(e.re)*(Math.exp(-e.im)+Math.exp(e.im))/n,.5*Math.cos(e.re)*(Math.exp(-e.im)-Math.exp(e.im))/n)}if(e instanceof y){if(!e.hasBase(y.BASE_UNITS.ANGLE))throw new TypeError("Unit in function csc is no angle");return 1/Math.sin(e.value)}if(e instanceof Array)return jt.map(e,bt);if(e.valueOf()!==e)return bt(e.valueOf());throw x("csc",e)}function Ot(e){if(1!=arguments.length)throw w("sec",arguments.length,1);if(p(e))return 1/Math.cos(e);if(e instanceof t){var n=.25*(Math.exp(-2*e.im)+Math.exp(2*e.im))+.5*Math.cos(2*e.re);return new t(.5*Math.cos(e.re)*(Math.exp(-e.im)+Math.exp(e.im))/n,.5*Math.sin(e.re)*(Math.exp(e.im)-Math.exp(-e.im))/n)}if(e instanceof y){if(!e.hasBase(y.BASE_UNITS.ANGLE))throw new TypeError("Unit in function sec is no angle");return 1/Math.cos(e.value)}if(e instanceof Array)return jt.map(e,Ot);if(e.valueOf()!==e)return Ot(e.valueOf());throw x("sec",e)}function Mt(e){if(1!=arguments.length)throw w("sin",arguments.length,1);if(p(e))return Math.sin(e);if(e instanceof t)return new t(.5*Math.sin(e.re)*(Math.exp(-e.im)+Math.exp(e.im)),.5*Math.cos(e.re)*(Math.exp(e.im)-Math.exp(-e.im)));if(e instanceof y){if(!e.hasBase(y.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.sin(e.value)}if(e instanceof Array)return jt.map(e,Mt);if(e.valueOf()!==e)return Mt(e.valueOf());throw x("sin",e)}function At(e){if(1!=arguments.length)throw w("tan",arguments.length,1);if(p(e))return Math.tan(e);if(e instanceof t){var n=Math.exp(-4*e.im)+2*Math.exp(-2*e.im)*Math.cos(2*e.re)+1;return new t(2*Math.exp(-2*e.im)*Math.sin(2*e.re)/n,(1-Math.exp(-4*e.im))/n)}if(e instanceof y){if(!e.hasBase(y.BASE_UNITS.ANGLE))throw new TypeError("Unit in function tan is no angle");return Math.tan(e.value)}if(e instanceof Array)return jt.map(e,At);if(e.valueOf()!==e)return At(e.valueOf());throw x("tan",e)}function St(e,t){if(2!=arguments.length)throw w("in",arguments.length,2);if(e instanceof y&&t instanceof y){if(!e.equalBase(t))throw Error("Units do not match");if(t.hasValue)throw Error("Cannot convert to a unit with a value");if(!t.hasUnit)throw Error("Unit expected on the right hand side of function in");var n=t.clone();return n.value=e.value,n.fixPrefix=!0,n}if(e instanceof Array||t instanceof Array)return jt.map2(e,t,St);if(e.valueOf()!==e)return Dt.in(e.valueOf());throw x("in",e)}function Tt(t){if(1!=arguments.length)throw w("clone",arguments.length,1);if(null==t)return t;if("function"==typeof t.clone)return t.clone();if(p(t)||g(t)||e(t))return t;if(t instanceof Array)return t.map(function(e){return Tt(e)});if(t instanceof Object)return jt.mapObject(t,Tt);throw x("clone",t)}function qt(e,t){var n=arguments.length;if(1!=n&&2!=n)throw w("format",n,1,2);if(1==n){var r=arguments[0];return p(r)?jt.formatNumber(r):r instanceof Array?jt.formatArray(r):g(r)?'"'+r+'"':r instanceof Object?""+r:r+""}if(!g(e))throw new TypeError("String expected as first parameter in function format");if(!(t instanceof Object))throw new TypeError("Object expected as first parameter in function format");return e.replace(/\$([\w\.]+)/g,function(e,n){for(var r=n.split("."),a=t[r.shift()];r.length&&void 0!=a;){var i=r.shift();a=i?a[i]:a+"."}return void 0!=a?a:e})}function Ut(e){if(1!=arguments.length)throw w("help",arguments.length,1);if(void 0!=e){if(e.doc)return zt(e.doc);if(e.constructor.doc)return zt(e.constructor.doc);if(g(e)){var t=Dt[e];if(t&&t.doc)return zt(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 zt(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=Dt.parser();t+="EXAMPLES\n";for(var r=0;e.examples.length>r;r++){var a,i=e.examples[r];try{a=n.eval(i)}catch(o){a=o}t+=i+"\n",t+=" "+Dt.format(a)+"\n"}t+="\n"}return e.seealso&&(t+="SEE ALSO\n"+e.seealso.join(", ")+"\n"),t}function Lt(e,t){var n;if(g(e)){if("undefined"==typeof require)throw Error("Cannot load file: require not available.");var r=require(e);Lt(r)}else if(Rt(e)){if(n=e.name,!n)throw Error("Cannot import an unnamed function");(t||void 0===Dt[n])&&(Dt[n]=e)}else if(e instanceof Object)for(n in e)if(e.hasOwnProperty(n)){var a=e[n];Rt(a)?(t||void 0===Dt[n])&&(Dt[n]=a):Lt(a)}}function Rt(e){return"function"==typeof e||p(e)||g(e)||e instanceof t||e instanceof y}function _t(e){if(1!=arguments.length)throw w("typeof",arguments.length,1);var t=typeof e;if("object"==t){if(null==e)return"null";if(e.constructor){for(var n in Dt)if(Dt.hasOwnProperty(n)&&e.constructor==Dt[n])return n.toLowerCase();if(e.constructor.name)return e.constructor.name.toLowerCase()}}return t}function It(){}function Ct(e,t,n){this.name=e,this.fn=t,this.params=n}function Pt(e){this.value=e}function Bt(e){this.nodes=e||[]}function Gt(){this.params=[],this.visible=[]}function kt(e,t,n,r){this.name=e,this.params=t,this.expr=n,this.result=r}function Vt(e,t,n,r,a){this.name=e,this.variables=n,this.values=[];for(var i=0,o=this.variables.length;o>i;i++)this.values[i]=function(){var e=function(){return e.value};return e.value=void 0,e}();this.def=this.createFunction(e,t,n,r),this.result=a}var Dt={type:{},expr:{node:{}},options:{precision:10}};"undefined"!=typeof module&&module.exports!==void 0&&(module.exports=Dt),"undefined"!=typeof exports&&(exports=Dt),"undefined"!=typeof require&&"undefined"!=typeof define&&define(function(){return Dt}),"undefined"!=typeof window&&(window.math=Dt);var jt=function(){function e(e){if(e instanceof Array){var t=e.length;if(t){var n=o(e[0]);return 0==n[0]?[0].concat(n):[t].concat(n)}return[t]}return[]}function t(e,n,r){var a,i=e.length;if(i!=n[r])throw new RangeError("Dimension mismatch ("+i+" != "+n[r]+")");if(n.length-1>r){var o=r+1;for(a=0;i>a;a++){var s=e[a];if(!(s instanceof Array))throw new RangeError("Dimension mismatch ("+(n.length-1)+" < "+n.length+")");t(e[a],n,o)}}else for(a=0;i>a;a++)if(e[a]instanceof Array)throw new RangeError("Dimension mismatch ("+(n.length+1)+" > "+n.length+")")}function r(e,t,n){if(t.length-1>n){var a=e[0];if(1!=e.length||!(a instanceof Array))throw new RangeError("Dimension mismatch ("+e.length+" > 0)");r(a,t,n+1)}else if(e.length)throw new RangeError("Dimension mismatch ("+e.length+" > 0)")}function a(e,t,n,r){if(!(e instanceof Array))throw new TypeError("Array expected");var i=e.length,o=t[n];if(i!=o){if(o>e.length)for(var s=e.length;o>s;s++)e[s]=r?Tt(r):0;else e.length=t[n];i=e.length}if(t.length-1>n){var f=n+1;for(s=0;i>s;s++)u=e[s],u instanceof Array||(u=[u],e[s]=u),a(u,t,f,r)}else for(s=0;i>s;s++){for(var u=e[s];u instanceof Array;)u=u[0];e[s]=u}}var i={};return i.formatNumber=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 V(e,t)+"";var r=Math.round(Math.log(n)/Math.LN10),a=e/Math.pow(10,r);return V(a,t)+"E"+r},i.formatArray=function(e){if(e instanceof Array){for(var t="[",n=e.length,r=0;n>r;r++)0!=r&&(t+=", "),t+=i.formatArray(e[r]);return t+="]"}return qt(e)},i.formatArray2d=function(e){var t="[",n=i.size(e);if(2!=n.length)throw new RangeError("Array must be two dimensional (size: "+i.formatArray(n)+")");for(var r=n[0],a=n[1],o=0;r>o;o++){0!=o&&(t+="; ");for(var s=e[o],f=0;a>f;f++){0!=f&&(t+=", ");var u=s[f];void 0!=u&&(t+=qt(u))}}return t+="]"},i.argsToArray=function(e){var t;if(0==e.length)t=[];
|
|
else if(1==e.length)t=e[0],t instanceof n&&(t=t.toVector()),t instanceof d&&(t=t.valueOf()),t instanceof Array||(t=[t]);else{t=[];for(var r=0;e.length>r;r++)t[r]=e[r]}return t},i.randomUUID=function(){var e=function(){return Math.floor(65536*Math.random()).toString(16)};return e()+e()+"-"+e()+"-"+e()+"-"+e()+"-"+e()+e()+e()},i.map=function(e,t){if(!e instanceof Array)throw new TypeError("Array expected");return e.map(function(e){return t(e)})},i.map2=function(e,t,n){var r,a,i;if(e instanceof Array)if(t instanceof Array){if(e.length!=t.length)throw new RangeError("Dimension mismatch ("+e.length+" != "+t.length+")");for(r=[],a=e.length,i=0;a>i;i++)r[i]=n(e[i],t[i])}else for(r=[],a=e.length,i=0;a>i;i++)r[i]=n(e[i],t);else if(t instanceof Array)for(r=[],a=t.length,i=0;a>i;i++)r[i]=n(e,t[i]);else r=n(e,t);return r},i.forEach=function(e,t){if(e instanceof Array)e.forEach(t);else for(var n in e)e.hasOwnProperty(n)&&t(e[n],n,e)},i.mapObject=function(e,t){var n={};for(var r in e)e.hasOwnProperty(r)&&(n[r]=t(e[r]));return n},i.deepEqual=function(e,t){var n,r,a;if(e instanceof Array){if(!(t instanceof Array))return!1;for(r=0,a=e.length;a>r;r++)if(!i.deepEqual(e[r],t[r]))return!1;return!0}if(e instanceof Object){if(t instanceof Array||!(t instanceof Object))return!1;for(n in e)if(e.hasOwnProperty(n)&&!i.deepEqual(e[n],t[n]))return!1;for(n in t)if(t.hasOwnProperty(n)&&!i.deepEqual(e[n],t[n]))return!1;return!0}return e.valueOf()==t.valueOf()},i.size=function o(t){var n=e(t);return i.validate(t,n),n},i.validate=function(e,n){var a=0==n.length;if(a){if(e instanceof Array)throw new RangeError("Dimension mismatch ("+e.length+" != 0)")}else{var o=-1!=n.indexOf(0);o?(n.forEach(function(e){if(0!=e)throw new RangeError("Invalid size, all dimensions must be either zero or non-zero (size: "+i.formatArray(n)+")")}),r(e,n,0)):t(e,n,0)}},i.resize=function(e,t,n){if(!(t instanceof Array))throw new TypeError("Size must be an array (size is "+Dt.typeof(t)+")");t.forEach(function(e){if(!p(e)||!v(e)||0>e)throw new TypeError("Invalid size, must contain positive integers (size: "+i.formatArray(t)+")")});var r=-1!=t.indexOf(0);r&&t.forEach(function(e){if(0!=e)throw new RangeError("Invalid size, all dimensions must be either zero or non-zero (size: "+i.formatArray(t)+")")}),a(e,t,0,n)},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,a;if(null==this)throw new TypeError(" this is null or not defined");var i=Object(this),o=i.length>>>0;if("function"!=typeof e)throw new TypeError(e+" is not a function");for(t&&(n=t),r=Array(o),a=0;o>a;){var s,f;a in i&&(s=i[a],f=e.call(n,s,a,i),r[a]=f),a++}return r}),Array.prototype.every||(Array.prototype.every=function(e){"use strict";if(null==this)throw new TypeError;var t=Object(this),n=t.length>>>0;if("function"!=typeof e)throw new TypeError;for(var r=arguments[1],a=0;n>a;a++)if(a in t&&!e.call(r,t[a],a,t))return!1;return!0}),Array.prototype.some||(Array.prototype.some=function(e){"use strict";if(null==this)throw new TypeError;var t=Object(this),n=t.length>>>0;if("function"!=typeof e)throw new TypeError;for(var r=arguments[1],a=0;n>a;a++)if(a in t&&e.call(r,t[a],a,t))return!0;return!1}),i}();Dt.type.Complex=t,function(){function e(){for(;" "==l||" "==l;)a()}function n(e){return e>="0"&&"9">=e||"."==e}function r(e){return e>="0"&&"9">=e}function a(){u++,l=f[u]}function i(e){u=e,l=f[u]}function o(){var e="",t=u;if("+"==l?a():"-"==l&&(e+=l,a()),!n(l))return i(t),null;for(;n(l);)e+=l,a();if("E"==l||"e"==l){if(e+=l,a(),("+"==l||"-"==l)&&(e+=l,a()),!r(l))return i(t),null;for(;r(l);)e+=l,a()}return e}function s(){var e=f[u+1];if("I"==l||"i"==l)return a(),"1";if(!("+"!=l&&"-"!=l||"I"!=e&&"i"!=e)){var t="+"==l?"1":"-1";return a(),a(),t}return null}var f,u,l;t.parse=function(n){if(f=n,u=-1,l="",!g(f))return null;a(),e();var r=o();if(r){if("I"==l||"i"==l)return a(),e(),l?null:new t(0,Number(r));e();var i=l;if("+"!=i&&"-"!=i)return e(),l?null:new t(Number(r),0);a(),e();var c=o();if(c){if("I"!=l&&"i"!=l)return null;a()}else if(c=s(),!c)return null;return"-"==i&&(c="-"==c[0]?"+"+c.substring(1):"-"+c),a(),e(),l?null:new t(Number(r),Number(c))}return(r=s())?(e(),l?null:new t(0,Number(r))):null}}(),t.prototype.clone=function(){return new t(this.re,this.im)},t.prototype.toString=function(){var e="",t=jt.formatNumber(this.re),n=jt.formatNumber(this.im);return e=0==this.im?t:0==this.re?1==this.im?"i":-1==this.im?"-i":n+"i":this.im>0?1==this.im?t+" + i":t+" + "+n+"i":-1==this.im?t+" - i":t+" - "+jt.formatNumber(Math.abs(this.im))+"i"},t.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"]},Dt.type.Matrix=n,n.prototype.get=function(e){var t;if(e instanceof n)t=e.isVector(),e=e.valueOf();else{if(!(e instanceof Array))throw new TypeError("Unsupported type of index "+Dt.typeof(e));t=!e.some(function(e){return e.forEach})}if(e.length!=this._size.length)throw new RangeError("Dimension mismatch ("+e.length+" != "+this._size.length+")");if(t)switch(e.length){case 1:return r(this._data,e[0]);case 2:return r(r(this._data,e[0]),e[1]);default:return a(this._data,e)}else switch(e.length){case 1:return i(this._data,e);case 2:return o(this._data,e);default:return s(this._data,e,0)}},n.prototype.set=function(e,t){var r;if(e instanceof n)r=e.isVector(),e=e.valueOf();else{if(!(e instanceof Array))throw new TypeError("Unsupported type of index "+Dt.typeof(e));r=!e.some(function(e){return e.forEach})}if((t instanceof n||t instanceof d)&&(t=t.valueOf()),e.length<this._size.length)throw new RangeError("Dimension mismatch ("+e.length+" != "+this._size.length+")");if(r){if(0!=Dt.size(t).length)throw new TypeError("Scalar value expected");switch(e.length){case 1:l(this._data,this._size,e,t);break;case 2:c(this._data,this._size,e,t);break;default:u(this._data,this._size,e,t)}}else{var a=this._size.concat();h(this._data,a,e,0,t),jt.deepEqual(this._size,a)||(m(this._data),this.resize(a))}return this},n.prototype.resize=function(e,t){jt.resize(this._data,e,t),this._size=Tt(e)},n.prototype.clone=function(){var e=new n;return e._data=Tt(this._data),e._size=Tt(this._size),e},n.prototype.size=function(){return this._size},n.prototype.map=function(e){var t=this,r=new n,a=[],i=function(n,r){return n instanceof Array?n.map(function(e,t){return a[r]=t,i(e,r+1)}):e(n,a,t)};return r._data=i(this._data,0),r._size=Tt(this._size),r},n.prototype.forEach=function(e){var t=this,n=[],r=function(a,i){a instanceof Array?a.forEach(function(e,t){n[i]=t,r(e,i+1)}):e(a,n,t)};r(this._data,0)},n.prototype.toScalar=function(){for(var e=this._data;e instanceof Array&&1==e.length;)e=e[0];return e instanceof Array?null:Tt(e)},n.prototype.isScalar=function(){return this._size.every(function(e){return 1>=e})},n.prototype.toVector=function(){var e=0,t=void 0,n=[];if(this._size.forEach(function(r,a){r>1&&(e++,t=a),n[a]=0}),0==e){var r=this.toScalar();return r?[r]:[]}if(1==e){for(var a=[],i=0,o=this._size[t];o>i;i++)n[t]=i,a[i]=Tt(this.get(n));return a}return null},n.prototype.isVector=function(){var e=0;return this._size.forEach(function(t){t>1&&e++}),1>=e},n.prototype.toArray=function(){return Tt(this._data)},n.prototype.valueOf=function(){return this._data},n.prototype.toString=function(){return jt.formatArray(this._data)},Dt.type.Range=d,d.prototype.clone=function(){return new d(this.start,this.step,this.end)},d.prototype.size=function(){var e=0,t=Number(this.start),n=Number(this.step),r=Number(this.end),a=r-t;return D(n)==D(a)?e=Math.floor(a/n)+1:0==a&&(e=1),isNaN(e)&&(e=0),[e]},d.prototype.forEach=function(e){var t=Number(this.start),n=Number(this.step),r=Number(this.end),a=0;if(n>0)for(;r>=t;)e(t,a,this),t+=n,a++;else if(0>n)for(;t>=r;)e(t,a,this),t+=n,a++},d.prototype.map=function(e){var t=[];return this.forEach(function(n,r,a){t[r]=e(n,r,a)}),t},d.prototype.toMatrix=function(){return new n(this.toArray())},d.prototype.toArray=function(){var e=[];return this.forEach(function(t,n){e[n]=t}),e},d.prototype.toVector=d.prototype.toArray,d.prototype.isVector=function(){return!0},d.prototype.toScalar=function(){var e=this.toArray();return 1==e.length?e[0]:null},d.prototype.isScalar=function(){return 1==this.size()[0]},d.prototype.valueOf=function(){return this.toArray()},d.prototype.toString=function(){var e=qt(Number(this.start));return 1!=this.step&&(e+=":"+qt(Number(this.step))),e+=":"+qt(Number(this.end))},Dt.type.Unit=y,function(){function e(){for(;" "==u||" "==u;)r()}function t(e){return e>="0"&&"9">=e||"."==e}function n(e){return e>="0"&&"9">=e}function r(){f++,u=s[f]}function a(e){f=e,u=s[f]}function i(){var e="",i=f;if("+"==u?r():"-"==u&&(e+=u,r()),!t(u))return a(i),null;for(;t(u);)e+=u,r();if("E"==u||"e"==u){if(e+=u,r(),("+"==u||"-"==u)&&(e+=u,r()),!n(u))return a(i),null;for(;n(u);)e+=u,r()}return e}function o(){var t="";for(e();u&&" "!=u&&" "!=u;)t+=u,r();return t||null}var s,f,u;y.parse=function(t){if(s=t,f=-1,u="",!g(s))return null;r(),e();var n,a=i();return a?(n=o(),r(),e(),u?null:a&&n?new y(Number(a),n):null):(n=o(),r(),e(),u?null:new y(null,n))}}(),y.prototype.clone=function(){var e=new y;for(var t in this)this.hasOwnProperty(t)&&(e[t]=this[t]);return e},y.endsWith=function(e,t){var n=e.length-t.length,r=e.length;return e.substring(n,r)===t},y.prototype._normalize=function(e){return(e+this.unit.offset)*this.unit.value*this.prefix.value},y.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},y.isUnit=function(e){for(var t=y.UNITS,n=t.length,r=0;n>r;r++){var a=t[r];if(y.endsWith(e,a.name)){var i=e.length-a.name.length;if(0==i)return!0;var o=e.substring(0,i),s=a.prefixes[o];if(void 0!==s)return!0}}return!1},y.prototype.hasBase=function(e){return void 0===this.unit.base?void 0===e:this.unit.base===e},y.prototype.equalBase=function(e){return this.unit.base===e.unit.base},y.prototype.equals=function(e){return this.equalBase(e)&&this.value==e.value},y.prototype.toString=function(){var e;if(this.fixPrefix)return e=this._unnormalize(this.value),jt.formatNumber(e)+" "+this.prefix.name+this.unit.name;var t=Math.abs(this.value/this.unit.value),n=y.PREFIX_NONE,r=Math.abs(Math.log(t/n.value)/Math.LN10-1.2),a=this.unit.prefixes;for(var i in a)if(a.hasOwnProperty(i)){var o=a[i];if(o.scientific){var s=Math.abs(Math.log(t/o.value)/Math.LN10-1.2);r>s&&(n=o,r=s)}}return e=this._unnormalize(this.value,n.value),jt.formatNumber(e)+" "+n.name+this.unit.name},y.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}}},y.PREFIX_NONE={name:"",value:1,scientific:!0},y.BASE_UNITS={NONE:{},LENGTH:{},MASS:{},TIME:{},CURRENT:{},TEMPERATURE:{},LUMINOUS_INTENSITY:{},AMOUNT_OF_SUBSTANCE:{},FORCE:{},SURFACE:{},VOLUME:{},ANGLE:{},BIT:{}};var Ft=y.BASE_UNITS,Ht=y.PREFIXES;y.BASE_UNIT_NONE={},y.UNIT_NONE={name:"",base:y.BASE_UNIT_NONE,value:1,offset:0},y.UNITS=[{name:"meter",base:Ft.LENGTH,prefixes:Ht.LONG,value:1,offset:0},{name:"inch",base:Ft.LENGTH,prefixes:Ht.NONE,value:.0254,offset:0},{name:"foot",base:Ft.LENGTH,prefixes:Ht.NONE,value:.3048,offset:0},{name:"yard",base:Ft.LENGTH,prefixes:Ht.NONE,value:.9144,offset:0},{name:"mile",base:Ft.LENGTH,prefixes:Ht.NONE,value:1609.344,offset:0},{name:"link",base:Ft.LENGTH,prefixes:Ht.NONE,value:.201168,offset:0},{name:"rod",base:Ft.LENGTH,prefixes:Ht.NONE,value:5.02921,offset:0},{name:"chain",base:Ft.LENGTH,prefixes:Ht.NONE,value:20.1168,offset:0},{name:"angstrom",base:Ft.LENGTH,prefixes:Ht.NONE,value:1e-10,offset:0},{name:"m",base:Ft.LENGTH,prefixes:Ht.SHORT,value:1,offset:0},{name:"ft",base:Ft.LENGTH,prefixes:Ht.NONE,value:.3048,offset:0},{name:"yd",base:Ft.LENGTH,prefixes:Ht.NONE,value:.9144,offset:0},{name:"mi",base:Ft.LENGTH,prefixes:Ht.NONE,value:1609.344,offset:0},{name:"li",base:Ft.LENGTH,prefixes:Ht.NONE,value:.201168,offset:0},{name:"rd",base:Ft.LENGTH,prefixes:Ht.NONE,value:5.02921,offset:0},{name:"ch",base:Ft.LENGTH,prefixes:Ht.NONE,value:20.1168,offset:0},{name:"mil",base:Ft.LENGTH,prefixes:Ht.NONE,value:254e-7,offset:0},{name:"m2",base:Ft.SURFACE,prefixes:Ht.SHORT,value:1,offset:0},{name:"sqin",base:Ft.SURFACE,prefixes:Ht.NONE,value:64516e-8,offset:0},{name:"sqft",base:Ft.SURFACE,prefixes:Ht.NONE,value:.09290304,offset:0},{name:"sqyd",base:Ft.SURFACE,prefixes:Ht.NONE,value:.83612736,offset:0},{name:"sqmi",base:Ft.SURFACE,prefixes:Ht.NONE,value:2589988.110336,offset:0},{name:"sqrd",base:Ft.SURFACE,prefixes:Ht.NONE,value:25.29295,offset:0},{name:"sqch",base:Ft.SURFACE,prefixes:Ht.NONE,value:404.6873,offset:0},{name:"sqmil",base:Ft.SURFACE,prefixes:Ht.NONE,value:6.4516e-10,offset:0},{name:"m3",base:Ft.VOLUME,prefixes:Ht.SHORT,value:1,offset:0},{name:"L",base:Ft.VOLUME,prefixes:Ht.SHORT,value:.001,offset:0},{name:"litre",base:Ft.VOLUME,prefixes:Ht.LONG,value:.001,offset:0},{name:"cuin",base:Ft.VOLUME,prefixes:Ht.NONE,value:16387064e-12,offset:0},{name:"cuft",base:Ft.VOLUME,prefixes:Ht.NONE,value:.028316846592,offset:0},{name:"cuyd",base:Ft.VOLUME,prefixes:Ht.NONE,value:.764554857984,offset:0},{name:"teaspoon",base:Ft.VOLUME,prefixes:Ht.NONE,value:5e-6,offset:0},{name:"tablespoon",base:Ft.VOLUME,prefixes:Ht.NONE,value:15e-6,offset:0},{name:"minim",base:Ft.VOLUME,prefixes:Ht.NONE,value:6.161152e-8,offset:0},{name:"fluiddram",base:Ft.VOLUME,prefixes:Ht.NONE,value:36966911e-13,offset:0},{name:"fluidounce",base:Ft.VOLUME,prefixes:Ht.NONE,value:2957353e-11,offset:0},{name:"gill",base:Ft.VOLUME,prefixes:Ht.NONE,value:.0001182941,offset:0},{name:"cup",base:Ft.VOLUME,prefixes:Ht.NONE,value:.0002365882,offset:0},{name:"pint",base:Ft.VOLUME,prefixes:Ht.NONE,value:.0004731765,offset:0},{name:"quart",base:Ft.VOLUME,prefixes:Ht.NONE,value:.0009463529,offset:0},{name:"gallon",base:Ft.VOLUME,prefixes:Ht.NONE,value:.003785412,offset:0},{name:"beerbarrel",base:Ft.VOLUME,prefixes:Ht.NONE,value:.1173478,offset:0},{name:"oilbarrel",base:Ft.VOLUME,prefixes:Ht.NONE,value:.1589873,offset:0},{name:"hogshead",base:Ft.VOLUME,prefixes:Ht.NONE,value:.238481,offset:0},{name:"fldr",base:Ft.VOLUME,prefixes:Ht.NONE,value:36966911e-13,offset:0},{name:"floz",base:Ft.VOLUME,prefixes:Ht.NONE,value:2957353e-11,offset:0},{name:"gi",base:Ft.VOLUME,prefixes:Ht.NONE,value:.0001182941,offset:0},{name:"cp",base:Ft.VOLUME,prefixes:Ht.NONE,value:.0002365882,offset:0},{name:"pt",base:Ft.VOLUME,prefixes:Ht.NONE,value:.0004731765,offset:0},{name:"qt",base:Ft.VOLUME,prefixes:Ht.NONE,value:.0009463529,offset:0},{name:"gal",base:Ft.VOLUME,prefixes:Ht.NONE,value:.003785412,offset:0},{name:"bbl",base:Ft.VOLUME,prefixes:Ht.NONE,value:.1173478,offset:0},{name:"obl",base:Ft.VOLUME,prefixes:Ht.NONE,value:.1589873,offset:0},{name:"g",base:Ft.MASS,prefixes:Ht.SHORT,value:.001,offset:0},{name:"gram",base:Ft.MASS,prefixes:Ht.LONG,value:.001,offset:0},{name:"ton",base:Ft.MASS,prefixes:Ht.SHORT,value:907.18474,offset:0},{name:"tonne",base:Ft.MASS,prefixes:Ht.SHORT,value:1e3,offset:0},{name:"grain",base:Ft.MASS,prefixes:Ht.NONE,value:6479891e-11,offset:0},{name:"dram",base:Ft.MASS,prefixes:Ht.NONE,value:.0017718451953125,offset:0},{name:"ounce",base:Ft.MASS,prefixes:Ht.NONE,value:.028349523125,offset:0},{name:"poundmass",base:Ft.MASS,prefixes:Ht.NONE,value:.45359237,offset:0},{name:"hundredweight",base:Ft.MASS,prefixes:Ht.NONE,value:45.359237,offset:0},{name:"stick",base:Ft.MASS,prefixes:Ht.NONE,value:.115,offset:0},{name:"gr",base:Ft.MASS,prefixes:Ht.NONE,value:6479891e-11,offset:0},{name:"dr",base:Ft.MASS,prefixes:Ht.NONE,value:.0017718451953125,offset:0},{name:"oz",base:Ft.MASS,prefixes:Ht.NONE,value:.028349523125,offset:0},{name:"lbm",base:Ft.MASS,prefixes:Ht.NONE,value:.45359237,offset:0},{name:"cwt",base:Ft.MASS,prefixes:Ht.NONE,value:45.359237,offset:0},{name:"s",base:Ft.TIME,prefixes:Ht.SHORT,value:1,offset:0},{name:"min",base:Ft.TIME,prefixes:Ht.NONE,value:60,offset:0},{name:"h",base:Ft.TIME,prefixes:Ht.NONE,value:3600,offset:0},{name:"seconds",base:Ft.TIME,prefixes:Ht.LONG,value:1,offset:0},{name:"second",base:Ft.TIME,prefixes:Ht.LONG,value:1,offset:0},{name:"sec",base:Ft.TIME,prefixes:Ht.LONG,value:1,offset:0},{name:"minutes",base:Ft.TIME,prefixes:Ht.NONE,value:60,offset:0},{name:"minute",base:Ft.TIME,prefixes:Ht.NONE,value:60,offset:0},{name:"hours",base:Ft.TIME,prefixes:Ht.NONE,value:3600,offset:0},{name:"hour",base:Ft.TIME,prefixes:Ht.NONE,value:3600,offset:0},{name:"day",base:Ft.TIME,prefixes:Ht.NONE,value:86400,offset:0},{name:"days",base:Ft.TIME,prefixes:Ht.NONE,value:86400,offset:0},{name:"rad",base:Ft.ANGLE,prefixes:Ht.NONE,value:1,offset:0},{name:"deg",base:Ft.ANGLE,prefixes:Ht.NONE,value:.017453292519943295,offset:0},{name:"grad",base:Ft.ANGLE,prefixes:Ht.NONE,value:.015707963267948967,offset:0},{name:"cycle",base:Ft.ANGLE,prefixes:Ht.NONE,value:6.283185307179586,offset:0},{name:"A",base:Ft.CURRENT,prefixes:Ht.SHORT,value:1,offset:0},{name:"ampere",base:Ft.CURRENT,prefixes:Ht.LONG,value:1,offset:0},{name:"K",base:Ft.TEMPERATURE,prefixes:Ht.NONE,value:1,offset:0},{name:"degC",base:Ft.TEMPERATURE,prefixes:Ht.NONE,value:1,offset:273.15},{name:"degF",base:Ft.TEMPERATURE,prefixes:Ht.NONE,value:1/1.8,offset:459.67},{name:"degR",base:Ft.TEMPERATURE,prefixes:Ht.NONE,value:1/1.8,offset:0},{name:"kelvin",base:Ft.TEMPERATURE,prefixes:Ht.NONE,value:1,offset:0},{name:"celsius",base:Ft.TEMPERATURE,prefixes:Ht.NONE,value:1,offset:273.15},{name:"fahrenheit",base:Ft.TEMPERATURE,prefixes:Ht.NONE,value:1/1.8,offset:459.67},{name:"rankine",base:Ft.TEMPERATURE,prefixes:Ht.NONE,value:1/1.8,offset:0},{name:"mol",base:Ft.AMOUNT_OF_SUBSTANCE,prefixes:Ht.NONE,value:1,offset:0},{name:"mole",base:Ft.AMOUNT_OF_SUBSTANCE,prefixes:Ht.NONE,value:1,offset:0},{name:"cd",base:Ft.LUMINOUS_INTENSITY,prefixes:Ht.NONE,value:1,offset:0},{name:"candela",base:Ft.LUMINOUS_INTENSITY,prefixes:Ht.NONE,value:1,offset:0},{name:"N",base:Ft.FORCE,prefixes:Ht.SHORT,value:1,offset:0},{name:"newton",base:Ft.FORCE,prefixes:Ht.LONG,value:1,offset:0},{name:"lbf",base:Ft.FORCE,prefixes:Ht.NONE,value:4.4482216152605,offset:0},{name:"poundforce",base:Ft.FORCE,prefixes:Ht.NONE,value:4.4482216152605,offset:0},{name:"b",base:Ft.BIT,prefixes:Ht.BINARY_SHORT,value:1,offset:0},{name:"bits",base:Ft.BIT,prefixes:Ht.BINARY_LONG,value:1,offset:0},{name:"B",base:Ft.BIT,prefixes:Ht.BINARY_SHORT,value:8,offset:0},{name:"bytes",base:Ft.BIT,prefixes:Ht.BINARY_LONG,value:8,offset:0}],Dt.E=Math.E,Dt.LN2=Math.LN2,Dt.LN10=Math.LN10,Dt.LOG2E=Math.LOG2E,Dt.LOG10E=Math.LOG10E,Dt.PI=Math.PI,Dt.SQRT1_2=Math.SQRT1_2,Dt.SQRT2=Math.SQRT2,Dt.I=new t(0,-1),Dt.pi=Dt.PI,Dt.e=Dt.E,Dt.i=Dt.I,Dt.abs=E,E.doc={name:"abs",category:"Arithmetic",syntax:["abs(x)"],description:"Compute the absolute value.",examples:["abs(3.5)","abs(-4.2)"],seealso:["sign"]},Dt.add=N,N.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"]},Dt.ceil=b,b.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"]},Dt.cube=O,O.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"]},Dt.divide=M,M.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"]},Dt.equal=S,S.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"]},Dt.exp=T,T.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"]},Dt.fix=q,q.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"]},Dt.floor=U,U.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"]},Dt.larger=z,z.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"]},Dt.largereq=L,L.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"]},Dt.log=R,R.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"]},Dt.log10=_,_.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"]},Dt.mod=I,I.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:[]},Dt.multiply=C,C.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"]},Dt.pow=B,B.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"]},Dt.round=k,k.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"]},Dt.sign=D,D.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"]},Dt.smaller=j,j.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"]},Dt.smallereq=F,F.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"]},Dt.sqrt=H,H.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"]},Dt.square=Y,Y.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"]},Dt.subtract=W,W.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"]},Dt.unaryminus=X,X.doc={name:"unaryminus",category:"Operators",syntax:["-x","unaryminus(x)"],description:"Inverse the sign of a value.",examples:["-4.5","-(-5.6)"],seealso:["add","subtract"]},Dt.unequal=Z,Z.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"]},Dt.arg=K,K.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"]},Dt.conj=Q,Q.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"]},Dt.im=J,J.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"]},Dt.re=$,$.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"]},Dt.complex=et,Dt.matrix=tt,Dt.parser=nt,Dt.range=rt,Dt.unit=at,Dt.workspace=it,Dt.diag=ot,ot.doc={name:"diag",category:"Matrix",syntax:["diag(x)","diag(x, k)"],description:"Create a diagonal matrix or retrieve the diagonal of a matrix. When x is a vector, a matrix with the vector values on the diagonal will be returned. When x is a matrix, a vector with the diagonal values of the matrix is returned.When k is provided, the k-th diagonal will be filled in or retrieved, if k is positive, the values are placed on the super diagonal. When k is negative, the values are placed on the sub diagonal.",examples:["diag(1:4)","diag(1:4, 1)","a = [1, 2, 3; 4, 5, 6; 7, 8, 9]","diag(a)"],seealso:["identity","ones","range","size","squeeze","transpose","zeros"]},Dt.identity=st,st.doc={name:"identity",category:"Numerics",syntax:["identity(n)","identity(m, n)","identity([m, n])","identity"],description:"Returns the identity matrix with size m-by-n. The matrix has ones on the diagonal and zeros elsewhere.",examples:["identity(3)","identity(3, 5)","a = [1, 2, 3; 4, 5, 6]","identity(size(a))"],seealso:["diag","ones","range","size","squeeze","transpose","zeros"]},Dt.ones=ft,ft.doc={name:"ones",category:"Numerics",syntax:["ones(n)","ones(m, n)","ones(m, n, p, ...)","ones([m, n])","ones([m, n, p, ...])","ones"],description:"Create a matrix containing ones.",examples:["ones(3)","ones(3, 5)","ones([2,3]) * 4.5","a = [1, 2, 3; 4, 5, 6]","ones(size(a))"],seealso:["diag","identity","range","size","squeeze","transpose","zeros"]},Dt.size=ut,ut.doc={name:"size",category:"Numerics",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","identity","ones","range","squeeze","transpose","zeros"]},Dt.squeeze=lt,lt.doc={name:"squeeze",category:"Numerics",syntax:["squeeze(x)"],description:"Remove singleton dimensions from a matrix.",examples:["a = zeros(1,3,2)","size(squeeze(a))","b = zeros(3,1,1)","size(squeeze(b))"],seealso:["diag","identity","ones","range","transpose","zeros"]},Dt.zeros=ht,ht.doc={name:"zeros",category:"Numerics",syntax:["zeros(n)","zeros(m, n)","zeros(m, n, p, ...)","zeros([m, n])","zeros([m, n, p, ...])","zeros"],description:"Create a matrix containing zeros.",examples:["zeros(3)","zeros(3, 5)","a = [1, 2, 3; 4, 5, 6]","zeros(size(a))"],seealso:["diag","identity","ones","range","size","squeeze","transpose"]},Dt.factorial=mt,mt.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:[]},Dt.random=pt,pt.doc={name:"random",category:"Probability",syntax:["random()"],description:"Return a random number between 0 and 1.",examples:["random()","100 * random()"],seealso:[]},Dt.max=vt,vt.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"]},Dt.min=dt,dt.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"]},Dt.acos=gt,gt.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"]},Dt.asin=yt,yt.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"]},Dt.atan=xt,xt.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"]},Dt.atan2=wt,wt.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"]},Dt.cos=Et,Et.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"]},Dt.cot=Nt,Nt.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"]},Dt.csc=bt,bt.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"]},Dt.sec=Ot,Ot.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"]},Dt.sin=Mt,Mt.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"]},Dt.tan=At,At.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"]},Dt.in=St,St.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:[]},Dt.clone=Tt,Tt.doc={name:"clone",category:"Utils",syntax:["clone(x)"],description:"Clone a variable. Creates a copy of primitive variables,and a deep copy of matrices",examples:["clone(3.5)","clone(2 - 4i)","clone(45 deg)","clone([1, 2; 3, 4])",'clone("hello world")'],seealso:[]},Dt.format=qt,qt.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:[]},Dt.help=Ut,Ut.doc={name:"help",category:"Utils",syntax:["help(object)"],description:"Display documentation on a function or data type.",examples:['help("sqrt")','help("Complex")'],seealso:[]},Dt["import"]=Lt,Lt.doc={name:"import",category:"Utils",syntax:["import(string)"],description:"Import functions from a file.",examples:['import("numbers")','import("./mylib.js")'],seealso:[]},Dt["typeof"]=_t,_t.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:[]},Dt.expr.node.Node=It,It.prototype.eval=function(){throw Error("Cannot evaluate a Node interface")
|
|
},It.prototype.toString=function(){return""},Ct.prototype=new It,Dt.expr.node.Symbol=Ct,Ct.prototype.hasParams=function(){return void 0!=this.params&&this.params.length>0},Ct.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)},Ct.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},Pt.prototype=new It,Dt.expr.node.Constant=Pt,Pt.prototype.eval=function(){return this.value},Pt.prototype.toString=function(){return this.value?Dt.format(this.value):""},Bt.prototype=new It,Dt.expr.node.MatrixNode=Bt,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,a=0;r>a;a++)0!=a&&(n+=", "),n+=t(e[a]);return n+="]"}return""+e}Bt.prototype.eval=function(){return new n(e(this.nodes))},Bt.prototype.toString=function(){return t(this.nodes)}}(),Gt.prototype=new It,Dt.expr.node.Block=Gt,Gt.prototype.add=function(e,t){var n=this.params.length;this.params[n]=e,this.visible[n]=void 0!=t?t:!0},Gt.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},Gt.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]"},kt.prototype=new It,Dt.expr.node.Assignment=kt,kt.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 a=this.result.eval();e=a.set(n,r),this.result.value=e}else e=this.expr.eval(),this.result.value=e;return e},kt.prototype.toString=function(){var e="";return e+=this.name,this.params&&this.params.length&&(e+="("+this.params.join(", ")+")"),e+=" = ",e+=""+this.expr},Vt.prototype=new It,Dt.expr.node.FunctionAssignment=Vt,Vt.prototype.createFunction=function(e,t,n,r){var a=function(){var t=n?n.length:0,a=arguments?arguments.length:0;if(t!=a)throw w(e,a,t);if(t>0)for(var i=0;t>i;i++)n[i].value=arguments[i];return r.eval()};return a.toString=function(){return e+"("+t.join(", ")+")"},a},Vt.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},Vt.prototype.toString=function(){return""+this.def},function(){function e(e){this.parentScope=e,this.nestedScopes=void 0,this.symbols={},this.defs={},this.updates={},this.links={}}Dt.expr.Scope=e,e.prototype.createNestedScope=function(){var t=new e(this);return this.nestedScopes||(this.nestedScopes=[]),this.nestedScopes.push(t),t},e.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()},e.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},e.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},e.prototype.createLink=function(e){var t=this.links[e];return t||(t=this.createSymbol(e),this.links[e]=t),t},e.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},e.prototype.createUpdate=function(e){var t=this.updates[e];return t||(t=this.createLink(e),this.updates[e]=t),t},e.prototype.findDef=function(e){function n(e,t){var n=a(e,t);return i[e]=n,o[e]=n,n}var r;if(r=this.defs[e])return r;if(r=this.updates[e])return r;if(this.parentScope)return this.parentScope.findDef(e);var a=this.newSymbol,i=this.symbols,o=this.defs;if("pi"==e)return n(e,Dt.PI);if("e"==e)return n(e,Dt.E);if("i"==e)return n(e,new t(0,1));var s=Dt[e];if(s)return n(e,s);if(y.isUnit(e)){var f=new y(null,e);return n(e,f)}return void 0},e.prototype.removeLink=function(e){delete this.links[e]},e.prototype.removeDef=function(e){delete this.defs[e]},e.prototype.removeUpdate=function(e){delete this.updates[e]},e.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()})},e.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},e.prototype.hasDef=function(e){return void 0!=this.defs[e]},e.prototype.hasUpdate=function(e){return void 0!=this.updates[e]},e.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}}(),function(){function e(){G++,k=P.charAt(G)}function n(){G=0,k=P.charAt(0)}function r(){for(D=C.NULL,V="";" "==k||" "==k;)e();if("#"==k)for(;"\n"!=k&&""!=k;)e();if(""==k)return D=C.DELIMITER,void 0;if("-"==k||","==k||"("==k||")"==k||"["==k||"]"==k||'"'==k||"\n"==k||";"==k||":"==k)return D=C.DELIMITER,V+=k,e(),void 0;if(a(k))for(D=C.DELIMITER;a(k);)V+=k,e();else if(o(k)){for(D=C.NUMBER;o(k);)V+=k,e();if("E"==k||"e"==k)for(V+=k,e(),("+"==k||"-"==k)&&(V+=k,e()),s(k)||(D=C.UNKNOWN);s(k);)V+=k,e()}else{if(!i(k)){for(D=C.UNKNOWN;""!=k;)V+=k,e();throw L('Syntax error in part "'+V+'"')}for(D=C.SYMBOL;i(k)||s(k);)V+=k,e()}}function a(e){return"&"==e||"|"==e||"<"==e||">"==e||"="==e||"+"==e||"/"==e||"*"==e||"%"==e||"^"==e||","==e||";"==e||"\n"==e||"!"==e}function i(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e||"_"==e}function o(e){return e>="0"&&"9">=e||"."==e}function s(e){return e>="0"&&"9">=e}function f(e){n(),r();var t;if(t=""==V?new Pt(void 0):l(e),""!=V)throw D==C.DELIMITER?_("Unknown operator "+V):L('Unexpected part "'+V+'"');return t}function u(e){var t=c(e);if(!(t instanceof kt)){var n="ans",r=void 0,a=e.createDef(n);return new kt(n,r,t,a)}return t}function l(e){var t,n,a;for("\n"!=V&&";"!=V&&""!=V&&(t=u(e));"\n"==V||";"==V;)n||(n=new Gt,t&&(a=";"!=V,n.add(t,a))),r(),"\n"!=V&&";"!=V&&""!=V&&(t=u(e),a=";"!=V,n.add(t,a));return n?n:(t||(t=u(e)),t)}function c(e){if(D==C.SYMBOL&&"function"==V){if(r(),D!=C.SYMBOL)throw L("Function name expected");var t=V;if(r(),"("!=V)throw L("Opening parenthesis ( expected");for(var n=e.createNestedScope(),a=[],i=[];;){if(r(),D!=C.SYMBOL)throw L("Variable name expected");var o=V,s=n.createDef(o);if(a.push(o),i.push(s),r(),","!=V){if(")"==V)break;throw L('Comma , or closing parenthesis ) expected"')}}if(r(),"="!=V)throw L("Equal sign = expected");r();var f=m(n),u=e.createDef(t);return new Vt(t,a,i,f,u)}return h(e)}function h(e){var t=!1;D==C.SYMBOL&&(t=e.hasLink(V));var n=m(e);if("="==V){if(!(n instanceof Ct))throw L("Symbol expected at the left hand side of assignment operator =");var a=n.name,i=n.params;t||e.removeLink(a),r();var o=m(e),s=n.hasParams()?e.createUpdate(a):e.createDef(a);return new kt(a,i,o,s)}return n}function m(e){var t=p(e);if(":"==V){for(var n=[t];":"==V;)r(),n.push(p(e));if(n.length>3)throw new TypeError("Invalid range");var a="range",i=rt;t=new Ct(a,i,n)}return t}function p(e){for(var t=v(e),n={"in":"in"};void 0!==n[V];){var a=V,i=Dt[n[a]];r();var o=[t,v(e)];t=new Ct(a,i,o)}return t}function v(e){var t=d(e);return t}function d(e){for(var t=g(e),n={"==":"equal","!=":"unequal","<":"smaller",">":"larger","<=":"smallereq",">=":"largereq"};void 0!==n[V];){var a=V,i=Dt[n[a]];r();var o=[t,g(e)];t=new Ct(a,i,o)}return t}function g(e){for(var t=x(e),n={"+":"add","-":"subtract"};void 0!==n[V];){var a=V,i=Dt[n[a]];r();var o=[t,x(e)];t=new Ct(a,i,o)}return t}function x(e){for(var t=w(e),n={"*":"multiply","/":"divide","%":"mod",mod:"mod"};void 0!==n[V];){var a=V,i=Dt[n[a]];r();var o=[t,w(e)];t=new Ct(a,i,o)}return t}function w(e){if("-"==V){var t=V,n=X;r();var a=[E(e)];return new Ct(t,n,a)}return E(e)}function E(e){for(var t=[N(e)];"^"==V;)r(),t.push(N(e));for(var n=t.pop();t.length;){var a=t.pop(),i="^",o=B,s=[a,n];n=new Ct(i,o,s)}return n}function N(e){for(var t=b(e);"!"==V;){var n=V,a=mt;r();var i=[t];t=new Ct(n,a,i)}return t}function b(e){return O(e)}function O(e){if(D==C.SYMBOL){var t=V;r();var n=e.createLink(t),a=M(e),i=new Ct(t,n,a);return i}return A(e)}function M(e){var t=[];if("("==V){if(r(),")"!=V)for(t.push(m(e));","==V;)r(),t.push(m(e));if(")"!=V)throw L("Parenthesis ) missing");r()}return t}function A(t){if('"'==V){for(var n="",a="";""!=k&&('"'!=k||"\\"==a);)n+=k,a=k,e();if(r(),'"'!=V)throw L('End of string " missing');r();var i=new Pt(n);return i}return S(t)}function S(e){if("["==V){var t;for(r();"\n"==V;)r();if("]"!=V){var n=[],a=0,i=0;for(n[0]=[m(e)];","==V||";"==V;){for(","==V?i++:(a++,i=0,n[a]=[]),r();"\n"==V;)r();for(n[a][i]=m(e);"\n"==V;)r()}var o=n.length,s=n.length>0?n[0].length:0;for(a=1;o>a;a++)if(n[a].length!=s)throw _("Number of columns must match ("+n[a].length+" != "+s+")");if("]"!=V)throw L("End of matrix ] missing");r(),t=new Bt(n)}else r(),t=new Bt([]);for(;"("==V;)t=M(e,t);return t}return T(e)}function T(e){if(D==C.NUMBER){var n;n="."==V?0:Number(V),r();var a;if(D==C.SYMBOL){if("i"==V||"I"==V)return a=new t(0,n),r(),new Pt(a);if(y.isUnit(V))return a=new y(n,V),r(),new Pt(a);throw R('Unknown unit "'+V+'"')}var i=new Pt(n);return i}return q(e)}function q(e){if("("==V){r();var t=m(e);if(")"!=V)throw L("Parenthesis ) expected");return r(),t}return U(e)}function U(){throw""==V?L("Unexpected end of expression"):L("Value expected")}function z(e){var t=t(),n=n();return void 0===t?void 0===n?e:e+" (col "+n+")":e+" (ln "+t+", col "+n+")"}function L(e){return new SyntaxError(z(e))}function R(e){return new TypeError(z(e))}function _(e){return Error(z(e))}Dt.expr.Parser=function I(){if(this.constructor!=I)throw new SyntaxError("Parser constructor must be called with the new operator");this.scope=new Dt.expr.Scope},Dt.expr.Parser.prototype.parse=function(e,t){return P=e||"",t||(this._newScope(),t=this.scope),f(t)},Dt.expr.Parser.prototype.eval=function(e){var t=this.parse(e);return t.eval()},Dt.expr.Parser.prototype.get=function(e){this._newScope();var t=this.scope.findDef(e);return t?t.value:void 0},Dt.expr.Parser.prototype.set=function(e,t){this.scope.createDef(e,t)},Dt.expr.Parser.prototype._newScope=function(){this.scope=new Dt.expr.Scope(this.scope)},Dt.expr.Parser.prototype.clear=function(){this.scope.clear()};var C={NULL:0,DELIMITER:1,NUMBER:2,SYMBOL:3,UNKNOWN:4},P="",G=0,k="",V="",D=C.NULL}(),function(){function e(){this.idMax=-1,this.updateSeq=0,this.parser=new Dt.expr.Parser,this.scope=new Dt.expr.Scope,this.nodes={},this.firstNode=void 0,this.lastNode=void 0}Dt.expr.Workspace=e,e.prototype.clear=function(){this.nodes={},this.firstNode=void 0,this.lastNode=void 0},e.prototype.append=function(t){var n=this._getNewId(),r=this.lastNode?this.lastNode.scope:this.scope,a=new Dt.expr.Scope(r),i=new e.Node({id:n,expression:t,parser:this.parser,scope:a,nextNode:void 0,previousNode:this.lastNode});return this.nodes[n]=i,this.firstNode||(this.firstNode=i),this.lastNode&&(this.lastNode.nextNode=i),this.lastNode=i,this._update([n]),n},e.prototype.insertBefore=function(t,n){var r=this.nodes[n];if(!r)throw'Node with id "'+n+'" not found';var a=r.previousNode,i=this._getNewId(),o=a?a.scope:this.scope,s=new Dt.expr.Scope(o),f=new e.Node({id:i,expression:t,parser:this.parser,scope:s,nextNode:r,previousNode:a});this.nodes[i]=f,a?a.nextNode=f:this.firstNode=f,r.previousNode=f,r.scope.parentScope=f.scope;var u=this.getDependencies(i);return-1==u.indexOf(i)&&u.unshift(i),this._update(u),i},e.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)},e.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,a=t.nextNode;r?r.nextNode=a:this.firstNode=a,a?a.previousNode=r:this.lastNode=r;var i=r?r.scope:this.scope;a&&(a.scope.parentScope=i),delete this.nodes[e],this._update(n)},e.prototype.replace=function(t,n){var r=this.nodes[n];if(!r)throw'Node with id "'+n+'" not found';var a=[n];e._merge(a,this.getDependencies(n));var i=r.previousNode;r.nextNode,i?i.scope:this.scope,r.setExpr(t),e._merge(a,this.getDependencies(n)),this._update(a)},e.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)},e.Node.prototype.setExpr=function(e){this.expression=e||"",this.scope.clear(),this._parse()},e.Node.prototype.getExpr=function(){return this.expression},e.Node.prototype.getResult=function(){return this.result},e.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 Pt(t)}},e.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},e._merge=function(e,t){for(var n=0,r=t.length;r>n;n++){var a=t[n];-1==e.indexOf(a)&&e.push(a)}},e.prototype.getDependencies=function(t){var n,r=[],a=this.nodes[t];if(a){var i=a.scope.defs,o=a.scope.updates,s=[];for(n in i)i.hasOwnProperty(n)&&s.push(n);for(n in o)o.hasOwnProperty(n)&&-1==s.indexOf(n)&&s.push(n);for(var f=a.nextNode;f&&s.length;){for(var u=f.scope,l=0;s.length>l;){if(n=s[l],(u.hasLink(n)||u.hasUpdate(n))&&-1==r.indexOf(f.id)){r.push(f.id);var c=this.getDependencies(f.id);e._merge(r,c)}u.hasDef(n)&&(s.splice(l,1),l--),l++}f=f.nextNode}}return r},e.prototype.getExpr=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';return t.getExpr()},e.prototype.getResult=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';return t.getResult()},e.prototype._update=function(e){this.updateSeq++;for(var t=this.updateSeq,n=this.nodes,r=0,a=e.length;a>r;r++){var i=e[r],o=n[i];o&&(o.eval(),o.updateSeq=t)}},e.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}},e.prototype._getNewId=function(){return this.idMax++,this.idMax},e.prototype.toString=function(){return JSON.stringify(this.toJSON())},e.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}}()})(); |