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