mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
29 lines
80 KiB
JavaScript
29 lines
80 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.6.0
|
|
* @date 2013-04-13
|
|
*
|
|
* @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 n(e,t){if(this.constructor!=n)throw new SyntaxError("Complex constructor must be called with the new operator");if(null!=e&&!v(e)||null!=t&&!v(t))throw new TypeError("Two numbers or a single string expected in Complex constructor");this.re=e||0,this.im=t||0}function t(e){if(this.constructor!=t)throw new SyntaxError("Matrix constructor must be called with the new operator");if(e instanceof t||e instanceof w)this._data=e.toArray();else if(e instanceof Array)this._data=e;else{if(null!=e)throw new TypeError("Unsupported type of data ("+Z.typeof(e)+")");this._data=[]}this._size=K.size(this._data)}function r(e,n){if(!v(e)||!g(e))throw new TypeError("Index must be an integer (value: "+e+")");if(1>e)throw new RangeError("Index out of range ("+e+" < 1)");if(n&&e>n)throw new RangeError("Index out of range ("+e+" > "+n+")")}function i(e,n){return r(n,e.length),e[n-1]}function a(e,n){return n.forEach(function(n){e=i(e,n)}),Z.clone(e)}function o(e,n){var t=n[0];return t.map?t.map(function(n){return i(e,n)}):[i(e,t)]}function f(e,n){var t=n[0],r=n[1];if(t.map)return r.map?t.map(function(n){var t=i(e,n);return r.map(function(e){return i(t,e)})}):t.map(function(n){return[i(i(e,n),r)]});if(r.map){var a=i(e,t);return[r.map(function(e){return i(a,e)})]}return[[i(i(e,t),r)]]}function s(e,n,t){var r=t==n.length-1,a=n[t],o=function(a){var o=i(e,a);return r?o:s(o,n,t+1)};return a.map?a.map(o):[o(a)]}function u(e,n,t){if(r(n),t instanceof Array)throw new TypeError("Dimension mismatch, value expected instead of array");e[n-1]=t}function c(e,n,t,i){var a=!1;t.length>n.length&&(a=!0);for(var o=0;t.length>o;o++){var f=t[o];r(f),(null==n[o]||f>n[o])&&(n[o]=f,a=!0)}a&&K.resize(e,n,0);var s=n.length;t.forEach(function(n,t){s-1>t?e=e[n-1]:e[n-1]=i})}function l(e,n,t,i){var a=t[0];r(a),a>n[0]&&K.resize(e,[a],0),e[a-1]=i}function h(e,n,t,i){var a=t[0],o=t[1];r(a),r(o);var f=!1;a>(n[0]||0)&&(n[0]=a,f=!0),o>(n[1]||0)&&(n[1]=o,f=!0),f&&K.resize(e,n,0),e[a-1][o-1]=i}function m(e,n,t,r,i){var a=r==t.length-1,o=t[r],f=function(o,f){if(a)u(e,o,i[f]),e.length>(n[r]||0)&&(n[r]=e.length);else{var s=e[o-1];s instanceof Array||(e[o-1]=s=[s],e.length>(n[r]||0)&&(n[r]=e.length)),m(s,n,t,r+1,i[f])}};if(o.map){var s=o.size&&o.size()||o.length;if(s!=i.length)throw new RangeError("Dimensions mismatch ("+s+" != "+i.length+")");o.map(f)}else f(o,0)}function p(e){for(var n=0,t=e.length;t>n;n++){var r=e[n];r instanceof Array?p(r):void 0==r&&(e[n]=0)}}function v(e){return e instanceof Number||"number"==typeof e}function g(e){return e==Math.round(e)}function w(e,n,t){if(this.constructor!=w)throw new SyntaxError("Range constructor must be called with the new operator");if(null!=e&&!v(e))throw new TypeError("Parameter start must be a number");if(null!=t&&!v(t))throw new TypeError("Parameter end must be a number");if(null!=n&&!v(n))throw new TypeError("Parameter step must be a number");this.start=null!=e?e:0,this.end=null!=t?t:0,this.step=null!=n?n:1}function d(e,n){var t=Z.type.Selector,r=Array.prototype.slice;t.prototype[e]="function"==typeof n?function(){var e=[this.value].concat(r.call(arguments,0));return new t(n.apply(this,e))}:new t(n)}function y(e){return e instanceof String||"string"==typeof e}function E(e,n){if(this.constructor!=E)throw Error("Unit constructor must be called with the new operator");if(null!=e&&!v(e))throw Error("First parameter in Unit constructor must be a number");if(null!=n&&!y(n))throw Error("Second parameter in Unit constructor must be a string");if(null!=n){var t=N(n);if(!t)throw Error('String "'+n+'" is no unit');this.unit=t.unit,this.prefix=t.prefix,this.hasUnit=!0}else this.unit=E.UNIT_NONE,this.prefix=E.PREFIX_NONE,this.hasUnit=!1;null!=e?(this.value=this._normalize(e),this.hasValue=!0,this.fixPrefix=!1):(this.value=this._normalize(1),this.hasValue=!1,this.fixPrefix=!0)}function N(e){for(var n=E.UNITS,t=0,r=n.length;r>t;t++){var i=n[t];if(K.endsWith(e,i.name)){var a=e.length-i.name.length,o=e.substring(0,a),f=i.prefixes[o];if(void 0!==f)return{unit:i,prefix:f}}}return null}function O(e,n){var t=void 0;if(2==arguments.length){var r=Z.typeof(n);t="Function "+e+" does not support a parameter of type "+r}else if(arguments.length>2){for(var i=[],a=1;arguments.length>a;a++)i.push(Z.typeof(arguments[a]));t="Function "+e+" does not support a parameters of type "+i.join(", ")}else t="Unsupported parameter in function "+e;return new TypeError(t)}function x(e,n,t,r){var i="Wrong number of arguments in function "+e+" ("+n+" provided, "+t+(void 0!=r?"-"+r:"")+" expected)";return new SyntaxError(i)}function b(e,t){var r=t.re*t.re+t.im*t.im;return new n((e.re*t.re+e.im*t.im)/r,(e.im*t.re-e.re*t.im)/r)}function M(e,t){return new n(e.re*t.re-e.im*t.im,e.re*t.im+e.im*t.re)}function A(e,n){var t=Z.log(e),r=Z.multiply(t,n);return Z.exp(r)}function S(e,n){var t=Math.pow(10,void 0!=n?n:Z.options.precision);return Math.round(e*t)/t}function T(e,n,t,r){if(t>r){if(e.length!=n.length)throw Error("Dimensions mismatch ("+e.length+" != "+n.length+")");for(var i=[],a=0;e.length>a;a++)i[a]=T(e[a],n[a],t,r+1);return i}return e.concat(n)}function U(e,n,t){var r=Z.multiply,i=Z.subtract;if(1==n)return e[0][0];if(2==n)return i(r(e[0][0],e[1][1]),r(e[1][0],e[0][1]));for(var a=0,o=0;t>o;o++){var f=L(e,n,t,0,o);a+=r(r((o+1)%2+(o+1)%2-1,e[0][o]),U(f,n-1,t-1))}return a}function L(e,n,t,r,i){for(var a,o=[],f=0;n>f;f++)if(f!=r){a=o[f-(f>r)]=[];for(var s=0;t>s;s++)s!=i&&(a[s-(s>i)]=e[f][s])}return o}function R(e,n,t){var r,i,a,o,f,s=Z.add,u=Z.unaryminus,c=Z.multiply,l=Z.divide;if(1==n){if(o=e[0][0],0==o)throw Error("Cannot calculate inverse, determinant is zero");return[[l(1,o)]]}if(2==n){var h=Z.det(e);if(0==h)throw Error("Cannot calculate inverse, determinant is zero");return[[l(e[1][1],h),l(u(e[0][1]),h)],[l(u(e[1][0]),h),l(e[0][0],h)]]}var m=e.concat();for(r=0;n>r;r++)m[r]=m[r].concat();for(var p=Z.eye(n).valueOf(),v=0;t>v;v++){for(r=v;n>r&&0==m[r][v];)r++;if(r==n||0==m[r][v])throw Error("Cannot calculate inverse, determinant is zero");r!=v&&(f=m[v],m[v]=m[r],m[r]=f,f=p[v],p[v]=p[r],p[r]=f);var g=m[v],w=p[v];for(r=0;n>r;r++){var d=m[r],y=p[r];if(r!=v){if(0!=d[v]){for(a=l(u(d[v]),g[v]),i=v;t>i;i++)d[i]=s(d[i],c(a,g[i]));for(i=0;t>i;i++)y[i]=s(y[i],c(a,w[i]))}}else{for(a=g[v],i=v;t>i;i++)d[i]=l(d[i],a);for(i=0;t>i;i++)y[i]=l(y[i],a)}}}return p}function _(e){if(1==e.length)return _(e[0]);for(var n=0,t=e.length;t>n;n++){var r=e[n];r instanceof Array&&(e[n]=_(r))}return e}function z(e){for(var n=Z.larger,t=e[0],r=1,i=e.length;i>r;r++){var a=e[r];n(a,t)&&(t=a)}return t}function q(e,n,t){for(var r=Z.larger,i=[],a=0;t>a;a++){for(var o=e[0][a],f=1;n>f;f++){var s=e[f][a];r(s,o)&&(o=s)}i[a]=o}return i}function I(e){for(var n=Z.smaller,t=e[0],r=1,i=e.length;i>r;r++){var a=e[r];n(a,t)&&(t=a)}return t}function P(e,n,t){for(var r=Z.smaller,i=[],a=0;t>a;a++){for(var o=e[0][a],f=1;n>f;f++){var s=e[f][a];r(s,o)&&(o=s)}i[a]=o}return i}function B(e,n){Z[e]=n,d(e,n)}function C(e){return"function"==typeof e||v(e)||y(e)||e instanceof n||e instanceof E}function G(){}function V(e,n,t){this.name=e,this.fn=n,this.params=t}function D(e){this.value=e}function k(e){this.nodes=e||[]}function F(){this.params=[],this.visible=[]}function j(e,n,t,r){this.name=e,this.params=n,this.expr=t,this.result=r}function H(e,n){this.object=e,this.params=n}function Y(e,n,t,r,i){this.name=e,this.variables=t,this.values=[];for(var a=0,o=this.variables.length;o>a;a++)this.values[a]=function(){var e=function(){return e.value};return e.value=void 0,e}();this.def=this.createFunction(e,n,t,r),this.result=i}function W(e,n){throw Error('Constructor "'+e+'" has been replaced by '+'constructor method "'+n+'" in math.js v0.5.0')}var Z={type:{},expr:{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 K=function(){function e(n){if(n instanceof Array){var t=n.length;if(t){var r=e(n[0]);return 0==r[0]?[0].concat(r):[t].concat(r)}return[t]}return[]}function n(e,t,r){var i,a=e.length;if(a!=t[r])throw new RangeError("Dimension mismatch ("+a+" != "+t[r]+")");if(t.length-1>r){var o=r+1;for(i=0;a>i;i++){var f=e[i];if(!(f instanceof Array))throw new RangeError("Dimension mismatch ("+(t.length-1)+" < "+t.length+")");n(e[i],t,o)}}else for(i=0;a>i;i++)if(e[i]instanceof Array)throw new RangeError("Dimension mismatch ("+(t.length+1)+" > "+t.length+")")}function r(e,n,t){if(n.length-1>t){var i=e[0];if(1!=e.length||!(i instanceof Array))throw new RangeError("Dimension mismatch ("+e.length+" > 0)");r(i,n,t+1)}else if(e.length)throw new RangeError("Dimension mismatch ("+e.length+" > 0)")}function i(e,n,t,r){if(!(e instanceof Array))throw new TypeError("Array expected");var a=e.length,o=n[t];if(a!=o){if(o>e.length)for(var f=e.length;o>f;f++)e[f]=r?Z.clone(r):0;else e.length=n[t];a=e.length}if(n.length-1>t){var s=t+1;for(f=0;a>f;f++)u=e[f],u instanceof Array||(u=[u],e[f]=u),i(u,n,s,r)}else for(f=0;a>f;f++){for(var u=e[f];u instanceof Array;)u=u[0];e[f]=u}}var a={};return a.formatNumber=function(e,n){if(1/0===e)return"Infinity";if(e===-1/0)return"-Infinity";if(0/0===e)return"NaN";var t=Math.abs(e);if(t>1e-4&&1e6>t||0==t)return S(e,n)+"";var r=Math.round(Math.log(t)/Math.LN10),i=e/Math.pow(10,r);return S(i,n)+"E"+r},a.formatArray=function(e){if(e instanceof Array){for(var n="[",t=e.length,r=0;t>r;r++)0!=r&&(n+=", "),n+=a.formatArray(e[r]);return n+="]"}return Z.format(e)},a.formatArray2d=function(e){var n="[",t=a.size(e);if(2!=t.length)throw new RangeError("Array must be two dimensional (size: "+a.formatArray(t)+")");for(var r=t[0],i=t[1],o=0;r>o;o++){0!=o&&(n+="; ");for(var f=e[o],s=0;i>s;s++){0!=s&&(n+=", ");var u=f[s];void 0!=u&&(n+=Z.format(u))}}return n+="]"},a.argsToArray=function(e){var n;if(0==e.length)n=[];else if(1==e.length)n=e[0],n instanceof t&&(n=n.toVector()),n instanceof w&&(n=n.valueOf()),n instanceof Array||(n=[n]);else{n=[];for(var r=0;e.length>r;r++)n[r]=e[r]}return n},a.endsWith=function(e,n){var t=e.length-n.length,r=e.length;return e.substring(t,r)===n},a.randomUUID=function(){var e=function(){return Math.floor(65536*Math.random()).toString(16)};return e()+e()+"-"+e()+"-"+e()+"-"+e()+"-"+e()+e()+e()},a.map=function(e,n){if(e instanceof Array||e instanceof t||e instanceof w)return e.map(function(e){return n(e)});throw new TypeError("Array expected")},a.map2=function(e,n,r){var i,o,f;if(e instanceof t||n instanceof t)return new t(a.map2(e.valueOf(),n.valueOf(),r));if(e instanceof w||n instanceof w)return new t(a.map2(e.valueOf(),n.valueOf(),r));if(e instanceof Array)if(n instanceof Array){if(e.length!=n.length)throw new RangeError("Dimension mismatch ("+e.length+" != "+n.length+")");for(i=[],o=e.length,f=0;o>f;f++)i[f]=r(e[f],n[f])}else for(i=[],o=e.length,f=0;o>f;f++)i[f]=r(e[f],n);else if(n instanceof Array)for(i=[],o=n.length,f=0;o>f;f++)i[f]=r(e,n[f]);else i=r(e,n);return i},a.forEach=function(e,n){if(e instanceof Array)e.forEach(n);else for(var t in e)e.hasOwnProperty(t)&&n(e[t],t,e)},a.mapObject=function(e,n){var t={};for(var r in e)e.hasOwnProperty(r)&&(t[r]=n(e[r]));return t},a.deepEqual=function(e,n){var t,r,i;if(e instanceof Array){if(!(n instanceof Array))return!1;for(r=0,i=e.length;i>r;r++)if(!a.deepEqual(e[r],n[r]))return!1;return!0}if(e instanceof Object){if(n instanceof Array||!(n instanceof Object))return!1;for(t in e)if(e.hasOwnProperty(t)&&!a.deepEqual(e[t],n[t]))return!1;for(t in n)if(n.hasOwnProperty(t)&&!a.deepEqual(e[t],n[t]))return!1;return!0}return e.valueOf()==n.valueOf()},a.size=function(n){var t=e(n);return a.validate(n,t),t},a.validate=function(e,t){var i=0==t.length;if(i){if(e instanceof Array)throw new RangeError("Dimension mismatch ("+e.length+" != 0)")}else{var o=-1!=t.indexOf(0);o?(t.forEach(function(e){if(0!=e)throw new RangeError("Invalid size, all dimensions must be either zero or non-zero (size: "+a.formatArray(t)+")")}),r(e,t,0)):n(e,t,0)}},a.resize=function(e,n,t){if(!(n instanceof Array))throw new TypeError("Size must be an array (size is "+Z.typeof(n)+")");n.forEach(function(e){if(!v(e)||!g(e)||0>e)throw new TypeError("Invalid size, must contain positive integers (size: "+a.formatArray(n)+")")});var r=-1!=n.indexOf(0);r&&n.forEach(function(e){if(0!=e)throw new RangeError("Invalid size, all dimensions must be either zero or non-zero (size: "+a.formatArray(n)+")")}),i(e,n,0,t)},Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var n=0;this.length>n;n++)if(this[n]==e)return n;return-1}),Array.prototype.forEach||(Array.prototype.forEach=function(e,n){for(var t=0,r=this.length;r>t;++t)e.call(n||this,this[t],t,this)}),Array.prototype.map||(Array.prototype.map=function(e,n){var t,r,i;if(null==this)throw new TypeError(" this is null or not defined");var a=Object(this),o=a.length>>>0;if("function"!=typeof e)throw new TypeError(e+" is not a function");for(n&&(t=n),r=Array(o),i=0;o>i;){var f,s;i in a&&(f=a[i],s=e.call(t,f,i,a),r[i]=s),i++}return r}),Array.prototype.every||(Array.prototype.every=function(e){"use strict";if(null==this)throw new TypeError;var n=Object(this),t=n.length>>>0;if("function"!=typeof e)throw new TypeError;for(var r=arguments[1],i=0;t>i;i++)if(i in n&&!e.call(r,n[i],i,n))return!1;return!0}),Array.prototype.some||(Array.prototype.some=function(e){"use strict";if(null==this)throw new TypeError;var n=Object(this),t=n.length>>>0;if("function"!=typeof e)throw new TypeError;for(var r=arguments[1],i=0;t>i;i++)if(i in n&&e.call(r,n[i],i,n))return!0;return!1}),a}();Z.type.Complex=n,function(){function e(){for(;" "==c||" "==c;)i()}function t(e){return e>="0"&&"9">=e||"."==e}function r(e){return e>="0"&&"9">=e}function i(){u++,c=s[u]}function a(e){u=e,c=s[u]}function o(){var e="",n=u;if("+"==c?i():"-"==c&&(e+=c,i()),!t(c))return a(n),null;for(;t(c);)e+=c,i();if("E"==c||"e"==c){if(e+=c,i(),("+"==c||"-"==c)&&(e+=c,i()),!r(c))return a(n),null;for(;r(c);)e+=c,i()}return e}function f(){var e=s[u+1];if("I"==c||"i"==c)return i(),"1";if(!("+"!=c&&"-"!=c||"I"!=e&&"i"!=e)){var n="+"==c?"1":"-1";return i(),i(),n}return null}var s,u,c;n.parse=function(t){if(s=t,u=-1,c="",!y(s))return null;i(),e();var r=o();if(r){if("I"==c||"i"==c)return i(),e(),c?null:new n(0,Number(r));e();var a=c;if("+"!=a&&"-"!=a)return e(),c?null:new n(Number(r),0);i(),e();var l=o();if(l){if("I"!=c&&"i"!=c)return null;i()}else if(l=f(),!l)return null;return"-"==a&&(l="-"==l[0]?"+"+l.substring(1):"-"+l),i(),e(),c?null:new n(Number(r),Number(l))}return(r=f())?(e(),c?null:new n(0,Number(r))):null}}(),n.prototype.clone=function(){return new n(this.re,this.im)},n.prototype.toString=function(){var e="",n=K.formatNumber(this.re),t=K.formatNumber(this.im);return e=0==this.im?n:0==this.re?1==this.im?"i":-1==this.im?"-i":t+"i":this.im>0?1==this.im?n+" + i":n+" + "+t+"i":-1==this.im?n+" - i":n+" - "+K.formatNumber(Math.abs(this.im))+"i"},Z.type.Matrix=t,t.prototype.get=function(e){var n;if(e instanceof t)n=e.isVector(),e=e.valueOf();else{if(!(e instanceof Array))throw new TypeError("Unsupported type of index "+Z.typeof(e));n=!e.some(function(e){return e.forEach})}if(e.length!=this._size.length)throw new RangeError("Dimension mismatch ("+e.length+" != "+this._size.length+")");if(n)switch(e.length){case 1:return i(this._data,e[0]);case 2:return i(i(this._data,e[0]),e[1]);default:return a(this._data,e)}else switch(e.length){case 1:return new t(o(this._data,e));case 2:return new t(f(this._data,e));default:return new t(s(this._data,e,0))}},t.prototype.set=function(e,n){var r;if(e instanceof t)r=e.isVector(),e=e.valueOf();else{if(!(e instanceof Array))throw new TypeError("Unsupported type of index "+Z.typeof(e));r=!e.some(function(e){return e.forEach})}if((n instanceof t||n instanceof w)&&(n=n.valueOf()),e.length<this._size.length)throw new RangeError("Dimension mismatch ("+e.length+" != "+this._size.length+")");if(r){if(0!=Z.size(n).length)throw new TypeError("Scalar value expected");switch(e.length){case 1:l(this._data,this._size,e,n);break;case 2:h(this._data,this._size,e,n);break;default:c(this._data,this._size,e,n)}}else{var i=this._size.concat();m(this._data,i,e,0,n),K.deepEqual(this._size,i)||(p(this._data),this.resize(i))}return this},t.prototype.resize=function(e,n){K.resize(this._data,e,n),this._size=Z.clone(e)},t.prototype.clone=function(){var e=new t;return e._data=Z.clone(this._data),e._size=Z.clone(this._size),e},t.prototype.size=function(){return this._size},t.prototype.map=function(e){var n=this,r=new t,i=[],a=function(t,r){return t instanceof Array?t.map(function(e,n){return i[r]=n+1,a(e,r+1)}):e(t,i,n)};return r._data=a(this._data,0),r._size=Z.clone(this._size),r},t.prototype.forEach=function(e){var n=this,t=[],r=function(i,a){i instanceof Array?i.forEach(function(e,n){t[a]=n+1,r(e,a+1)}):e(i,t,n)};r(this._data,0)},t.prototype.toScalar=function(){for(var e=this._data;e instanceof Array&&1==e.length;)e=e[0];return e instanceof Array?null:Z.clone(e)},t.prototype.isScalar=function(){return this._size.every(function(e){return 1>=e})},t.prototype.toVector=function(){var e=0,n=void 0,t=[];if(this._size.forEach(function(r,i){r>1&&(e++,n=i),t[i]=0}),0==e){var r=this.toScalar();return r?[r]:[]}if(1==e){var i=[],a=function(e){e instanceof Array?e.forEach(a):i.push(e)};return a(this._data),i}return null},t.prototype.isVector=function(){var e=0;return this._size.forEach(function(n){n>1&&e++}),1>=e},t.prototype.toArray=function(){return Z.clone(this._data)},t.prototype.valueOf=function(){return this._data},t.prototype.toString=function(){return K.formatArray(this._data)},Z.type.Range=w,w.parse=function(e){if(!y(e))return null;var n=e.split(":"),t=n.map(function(e){return Number(e)}),r=t.some(function(e){return isNaN(e)});if(r)return null;switch(t.length){case 2:return new w(t[0],1,t[1]);case 3:return new w(t[0],t[1],t[2]);default:return null}},w.prototype.clone=function(){return new w(this.start,this.step,this.end)},w.prototype.size=function(){var e=0,n=Number(this.start),t=Number(this.step),r=Number(this.end),i=r-n;return Z.sign(t)==Z.sign(i)?e=Math.floor(i/t)+1:0==i&&(e=1),isNaN(e)&&(e=0),[e]},w.prototype.forEach=function(e){var n=Number(this.start),t=Number(this.step),r=Number(this.end),i=0;if(t>0)for(;r>=n;)e(n,i,this),n+=t,i++;else if(0>t)for(;n>=r;)e(n,i,this),n+=t,i++},w.prototype.map=function(e){var n=[];return this.forEach(function(t,r,i){n[r]=e(t,r,i)}),n},w.prototype.toMatrix=function(){return new t(this.toArray())},w.prototype.toArray=function(){var e=[];return this.forEach(function(n,t){e[t]=n}),e},w.prototype.toVector=w.prototype.toArray,w.prototype.isVector=function(){return!0},w.prototype.toScalar=function(){var e=this.toArray();return 1==e.length?e[0]:null},w.prototype.isScalar=function(){return 1==this.size()[0]},w.prototype.valueOf=function(){return this.toArray()},w.prototype.toString=function(){var e=Z.format(Number(this.start));return 1!=this.step&&(e+=":"+Z.format(Number(this.step))),e+=":"+Z.format(Number(this.end))},Z.type.Selector=function X(e){if(!(this instanceof X))throw new SyntaxError("Selector constructor must be called with the new operator");this.value=e instanceof Z.type.Selector?e.value:e||void 0},Z.type.Selector.prototype={done:function(){return this.value},get:function(){var e=this.value;if(!e)throw Error("Selector value is undefined");if(e.get)return new Z.type.Selector(e.get.apply(e,arguments));if(e instanceof Array)return e=new t(e),new Z.type.Selector(e.get.apply(e,arguments).valueOf());throw Error("Selector value has no method get")},set:function(){var e=this.value;if(!e)throw Error("Selector value is undefined");if(e.set)return new Z.type.Selector(e.set.apply(e,arguments));if(e instanceof Array)return e=new t(e),new Z.type.Selector(e.set.apply(e,arguments).valueOf());throw Error("Selector value has no method set")},valueOf:function(){return this.value},toString:function(){return Z.format(this.value)}},Z.type.Unit=E,function(){function e(){for(;" "==u||" "==u;)r()}function n(e){return e>="0"&&"9">=e||"."==e}function t(e){return e>="0"&&"9">=e}function r(){s++,u=f[s]}function i(e){s=e,u=f[s]}function a(){var e="",a=s;if("+"==u?r():"-"==u&&(e+=u,r()),!n(u))return i(a),null;for(;n(u);)e+=u,r();if("E"==u||"e"==u){if(e+=u,r(),("+"==u||"-"==u)&&(e+=u,r()),!t(u))return i(a),null;for(;t(u);)e+=u,r()}return e}function o(){var n="";for(e();u&&" "!=u&&" "!=u;)n+=u,r();return n||null}var f,s,u;E.parse=function(n){if(f=n,s=-1,u="",!y(f))return null;r(),e();var t,i=a();return i?(t=o(),r(),e(),u?null:i&&t?new E(Number(i),t):null):(t=o(),r(),e(),u?null:new E(null,t))}}(),E.prototype.clone=function(){var e=new E;for(var n in this)this.hasOwnProperty(n)&&(e[n]=this[n]);return e},E.prototype._normalize=function(e){return(e+this.unit.offset)*this.unit.value*this.prefix.value},E.prototype._unnormalize=function(e,n){return void 0==n?e/this.unit.value/this.prefix.value-this.unit.offset:e/this.unit.value/n-this.unit.offset},E.isPlainUnit=function(e){return null!=N(e)},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.in=function(e){var n;if(y(e)){if(n=new E(null,e),!this.equalBase(n))throw Error("Units do not match");return n.value=this.value,n}if(e instanceof E){if(!this.equalBase(e))throw Error("Units do not match");if(e.hasValue)throw Error("Cannot convert to a unit with a value");if(!e.hasUnit)throw Error("Unit expected on the right hand side of function in");return n=e.clone(),n.value=this.value,n.fixPrefix=!0,n}throw Error("String or Unit expected as parameter")},E.prototype.toNumber=function(e){var n=this.in(e),t=this.fixPrefix?n._bestPrefix():n.prefix;return n._unnormalize(n.value,t.value)},E.prototype.toString=function(){var e;if(this.fixPrefix)return e=this._unnormalize(this.value),K.formatNumber(e)+" "+this.prefix.name+this.unit.name;var n=this._bestPrefix();return e=this._unnormalize(this.value,n.value),K.formatNumber(e)+" "+n.name+this.unit.name},E.prototype._bestPrefix=function(){var e=Math.abs(this.value/this.unit.value),n=E.PREFIX_NONE,t=Math.abs(Math.log(e/n.value)/Math.LN10-1.2),r=this.unit.prefixes;for(var i in r)if(r.hasOwnProperty(i)){var a=r[i];if(a.scientific){var o=Math.abs(Math.log(e/a.value)/Math.LN10-1.2);t>o&&(n=a,t=o)}}return n},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 Q=E.BASE_UNITS,J=E.PREFIXES;E.BASE_UNIT_NONE={},E.UNIT_NONE={name:"",base:E.BASE_UNIT_NONE,value:1,offset:0},E.UNITS=[{name:"meter",base:Q.LENGTH,prefixes:J.LONG,value:1,offset:0},{name:"inch",base:Q.LENGTH,prefixes:J.NONE,value:.0254,offset:0},{name:"foot",base:Q.LENGTH,prefixes:J.NONE,value:.3048,offset:0},{name:"yard",base:Q.LENGTH,prefixes:J.NONE,value:.9144,offset:0},{name:"mile",base:Q.LENGTH,prefixes:J.NONE,value:1609.344,offset:0},{name:"link",base:Q.LENGTH,prefixes:J.NONE,value:.201168,offset:0},{name:"rod",base:Q.LENGTH,prefixes:J.NONE,value:5.02921,offset:0},{name:"chain",base:Q.LENGTH,prefixes:J.NONE,value:20.1168,offset:0},{name:"angstrom",base:Q.LENGTH,prefixes:J.NONE,value:1e-10,offset:0},{name:"m",base:Q.LENGTH,prefixes:J.SHORT,value:1,offset:0},{name:"ft",base:Q.LENGTH,prefixes:J.NONE,value:.3048,offset:0},{name:"yd",base:Q.LENGTH,prefixes:J.NONE,value:.9144,offset:0},{name:"mi",base:Q.LENGTH,prefixes:J.NONE,value:1609.344,offset:0},{name:"li",base:Q.LENGTH,prefixes:J.NONE,value:.201168,offset:0},{name:"rd",base:Q.LENGTH,prefixes:J.NONE,value:5.02921,offset:0},{name:"ch",base:Q.LENGTH,prefixes:J.NONE,value:20.1168,offset:0},{name:"mil",base:Q.LENGTH,prefixes:J.NONE,value:254e-7,offset:0},{name:"m2",base:Q.SURFACE,prefixes:J.SHORT,value:1,offset:0},{name:"sqin",base:Q.SURFACE,prefixes:J.NONE,value:64516e-8,offset:0},{name:"sqft",base:Q.SURFACE,prefixes:J.NONE,value:.09290304,offset:0},{name:"sqyd",base:Q.SURFACE,prefixes:J.NONE,value:.83612736,offset:0},{name:"sqmi",base:Q.SURFACE,prefixes:J.NONE,value:2589988.110336,offset:0},{name:"sqrd",base:Q.SURFACE,prefixes:J.NONE,value:25.29295,offset:0},{name:"sqch",base:Q.SURFACE,prefixes:J.NONE,value:404.6873,offset:0},{name:"sqmil",base:Q.SURFACE,prefixes:J.NONE,value:6.4516e-10,offset:0},{name:"m3",base:Q.VOLUME,prefixes:J.SHORT,value:1,offset:0},{name:"L",base:Q.VOLUME,prefixes:J.SHORT,value:.001,offset:0},{name:"litre",base:Q.VOLUME,prefixes:J.LONG,value:.001,offset:0},{name:"cuin",base:Q.VOLUME,prefixes:J.NONE,value:16387064e-12,offset:0},{name:"cuft",base:Q.VOLUME,prefixes:J.NONE,value:.028316846592,offset:0},{name:"cuyd",base:Q.VOLUME,prefixes:J.NONE,value:.764554857984,offset:0},{name:"teaspoon",base:Q.VOLUME,prefixes:J.NONE,value:5e-6,offset:0},{name:"tablespoon",base:Q.VOLUME,prefixes:J.NONE,value:15e-6,offset:0},{name:"minim",base:Q.VOLUME,prefixes:J.NONE,value:6.161152e-8,offset:0},{name:"fluiddram",base:Q.VOLUME,prefixes:J.NONE,value:36966911e-13,offset:0},{name:"fluidounce",base:Q.VOLUME,prefixes:J.NONE,value:2957353e-11,offset:0},{name:"gill",base:Q.VOLUME,prefixes:J.NONE,value:.0001182941,offset:0},{name:"cup",base:Q.VOLUME,prefixes:J.NONE,value:.0002365882,offset:0},{name:"pint",base:Q.VOLUME,prefixes:J.NONE,value:.0004731765,offset:0},{name:"quart",base:Q.VOLUME,prefixes:J.NONE,value:.0009463529,offset:0},{name:"gallon",base:Q.VOLUME,prefixes:J.NONE,value:.003785412,offset:0},{name:"beerbarrel",base:Q.VOLUME,prefixes:J.NONE,value:.1173478,offset:0},{name:"oilbarrel",base:Q.VOLUME,prefixes:J.NONE,value:.1589873,offset:0},{name:"hogshead",base:Q.VOLUME,prefixes:J.NONE,value:.238481,offset:0},{name:"fldr",base:Q.VOLUME,prefixes:J.NONE,value:36966911e-13,offset:0},{name:"floz",base:Q.VOLUME,prefixes:J.NONE,value:2957353e-11,offset:0},{name:"gi",base:Q.VOLUME,prefixes:J.NONE,value:.0001182941,offset:0},{name:"cp",base:Q.VOLUME,prefixes:J.NONE,value:.0002365882,offset:0},{name:"pt",base:Q.VOLUME,prefixes:J.NONE,value:.0004731765,offset:0},{name:"qt",base:Q.VOLUME,prefixes:J.NONE,value:.0009463529,offset:0},{name:"gal",base:Q.VOLUME,prefixes:J.NONE,value:.003785412,offset:0},{name:"bbl",base:Q.VOLUME,prefixes:J.NONE,value:.1173478,offset:0},{name:"obl",base:Q.VOLUME,prefixes:J.NONE,value:.1589873,offset:0},{name:"g",base:Q.MASS,prefixes:J.SHORT,value:.001,offset:0},{name:"gram",base:Q.MASS,prefixes:J.LONG,value:.001,offset:0},{name:"ton",base:Q.MASS,prefixes:J.SHORT,value:907.18474,offset:0},{name:"tonne",base:Q.MASS,prefixes:J.SHORT,value:1e3,offset:0},{name:"grain",base:Q.MASS,prefixes:J.NONE,value:6479891e-11,offset:0},{name:"dram",base:Q.MASS,prefixes:J.NONE,value:.0017718451953125,offset:0},{name:"ounce",base:Q.MASS,prefixes:J.NONE,value:.028349523125,offset:0},{name:"poundmass",base:Q.MASS,prefixes:J.NONE,value:.45359237,offset:0},{name:"hundredweight",base:Q.MASS,prefixes:J.NONE,value:45.359237,offset:0},{name:"stick",base:Q.MASS,prefixes:J.NONE,value:.115,offset:0},{name:"gr",base:Q.MASS,prefixes:J.NONE,value:6479891e-11,offset:0},{name:"dr",base:Q.MASS,prefixes:J.NONE,value:.0017718451953125,offset:0},{name:"oz",base:Q.MASS,prefixes:J.NONE,value:.028349523125,offset:0},{name:"lbm",base:Q.MASS,prefixes:J.NONE,value:.45359237,offset:0},{name:"cwt",base:Q.MASS,prefixes:J.NONE,value:45.359237,offset:0},{name:"s",base:Q.TIME,prefixes:J.SHORT,value:1,offset:0},{name:"min",base:Q.TIME,prefixes:J.NONE,value:60,offset:0},{name:"h",base:Q.TIME,prefixes:J.NONE,value:3600,offset:0},{name:"seconds",base:Q.TIME,prefixes:J.LONG,value:1,offset:0},{name:"second",base:Q.TIME,prefixes:J.LONG,value:1,offset:0},{name:"sec",base:Q.TIME,prefixes:J.LONG,value:1,offset:0},{name:"minutes",base:Q.TIME,prefixes:J.NONE,value:60,offset:0},{name:"minute",base:Q.TIME,prefixes:J.NONE,value:60,offset:0},{name:"hours",base:Q.TIME,prefixes:J.NONE,value:3600,offset:0},{name:"hour",base:Q.TIME,prefixes:J.NONE,value:3600,offset:0},{name:"day",base:Q.TIME,prefixes:J.NONE,value:86400,offset:0},{name:"days",base:Q.TIME,prefixes:J.NONE,value:86400,offset:0},{name:"rad",base:Q.ANGLE,prefixes:J.NONE,value:1,offset:0},{name:"deg",base:Q.ANGLE,prefixes:J.NONE,value:.017453292519943295,offset:0},{name:"grad",base:Q.ANGLE,prefixes:J.NONE,value:.015707963267948967,offset:0},{name:"cycle",base:Q.ANGLE,prefixes:J.NONE,value:6.283185307179586,offset:0},{name:"A",base:Q.CURRENT,prefixes:J.SHORT,value:1,offset:0},{name:"ampere",base:Q.CURRENT,prefixes:J.LONG,value:1,offset:0},{name:"K",base:Q.TEMPERATURE,prefixes:J.NONE,value:1,offset:0},{name:"degC",base:Q.TEMPERATURE,prefixes:J.NONE,value:1,offset:273.15},{name:"degF",base:Q.TEMPERATURE,prefixes:J.NONE,value:1/1.8,offset:459.67},{name:"degR",base:Q.TEMPERATURE,prefixes:J.NONE,value:1/1.8,offset:0},{name:"kelvin",base:Q.TEMPERATURE,prefixes:J.NONE,value:1,offset:0},{name:"celsius",base:Q.TEMPERATURE,prefixes:J.NONE,value:1,offset:273.15},{name:"fahrenheit",base:Q.TEMPERATURE,prefixes:J.NONE,value:1/1.8,offset:459.67},{name:"rankine",base:Q.TEMPERATURE,prefixes:J.NONE,value:1/1.8,offset:0},{name:"mol",base:Q.AMOUNT_OF_SUBSTANCE,prefixes:J.NONE,value:1,offset:0},{name:"mole",base:Q.AMOUNT_OF_SUBSTANCE,prefixes:J.NONE,value:1,offset:0},{name:"cd",base:Q.LUMINOUS_INTENSITY,prefixes:J.NONE,value:1,offset:0},{name:"candela",base:Q.LUMINOUS_INTENSITY,prefixes:J.NONE,value:1,offset:0},{name:"N",base:Q.FORCE,prefixes:J.SHORT,value:1,offset:0},{name:"newton",base:Q.FORCE,prefixes:J.LONG,value:1,offset:0},{name:"lbf",base:Q.FORCE,prefixes:J.NONE,value:4.4482216152605,offset:0},{name:"poundforce",base:Q.FORCE,prefixes:J.NONE,value:4.4482216152605,offset:0},{name:"b",base:Q.BIT,prefixes:J.BINARY_SHORT,value:1,offset:0},{name:"bits",base:Q.BIT,prefixes:J.BINARY_LONG,value:1,offset:0},{name:"B",base:Q.BIT,prefixes:J.BINARY_SHORT,value:8,offset:0},{name:"bytes",base:Q.BIT,prefixes:J.BINARY_LONG,value:8,offset:0}],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 n(0,1),Z.pi=Z.PI,Z.e=Z.E,Z.i=Z.I,Z.abs=function(e){if(1!=arguments.length)throw x("abs",arguments.length,1);
|
|
if(v(e))return Math.abs(e);if(e instanceof n)return Math.sqrt(e.re*e.re+e.im*e.im);if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.abs);if(e.valueOf()!==e)return Z.abs(e.valueOf());throw O("abs",e)},Z.add=function(e,r){if(2!=arguments.length)throw x("add",arguments.length,2);if(v(e)){if(v(r))return e+r;if(r instanceof n)return new n(e+r.re,r.im)}else if(e instanceof n){if(v(r))return new n(e.re+r,e.im);if(r instanceof n)return new n(e.re+r.re,e.im+r.im)}else if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Units do not match");if(!e.hasValue)throw Error("Unit on left hand side of operator + has no value");if(!r.hasValue)throw Error("Unit on right hand side of operator + has no value");var i=e.clone();return i.value+=r.value,i.fixPrefix=!1,i}if(y(e)||y(r))return e+r;if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.add);if(e.valueOf()!==e||r.valueOf()!==r)return Z.add(e.valueOf(),r.valueOf());throw O("add",e,r)},Z.ceil=function(e){if(1!=arguments.length)throw x("ceil",arguments.length,1);if(v(e))return Math.ceil(e);if(e instanceof n)return new n(Math.ceil(e.re),Math.ceil(e.im));if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.ceil);if(e.valueOf()!==e)return Z.ceil(e.valueOf());throw O("ceil",e)},Z.cube=function(e){if(1!=arguments.length)throw x("cube",arguments.length,1);if(v(e))return e*e*e;if(e instanceof n)return Z.multiply(Z.multiply(e,e),e);if(e instanceof Array||e instanceof t||e instanceof w)return Z.multiply(Z.multiply(e,e),e);if(e.valueOf()!==e)return Z.cube(e.valueOf());throw O("cube",e)},Z.divide=function(e,r){if(2!=arguments.length)throw x("divide",arguments.length,2);if(v(e)){if(v(r))return e/r;if(r instanceof n)return b(new n(e,0),r)}if(e instanceof n){if(v(r))return b(e,new n(r,0));if(r instanceof n)return b(e,r)}if(e instanceof E&&v(r)){var i=e.clone();return i.value/=r,i}if(e instanceof Array||e instanceof t)return r instanceof Array||r instanceof t?Z.multiply(e,Z.inv(r)):K.map2(e,r,Z.divide);if(r instanceof Array||r instanceof t)return Z.multiply(e,Z.inv(r));if(e.valueOf()!==e||r.valueOf()!==r)return Z.divide(e.valueOf(),r.valueOf());throw O("divide",e,r)},Z.equal=function $(e,r){if(2!=arguments.length)throw x("equal",arguments.length,2);if(v(e)){if(v(r))return e==r;if(r instanceof n)return e==r.re&&0==r.im}if(e instanceof n){if(v(r))return e.re==r&&0==e.im;if(r instanceof n)return e.re==r.re&&e.im==r.im}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value==r.value}if(y(e)||y(r))return e==r;if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.equal);if(e.valueOf()!==e||r.valueOf()!==r)return $(e.valueOf(),r.valueOf());throw O("equal",e,r)},Z.exp=function(e){if(1!=arguments.length)throw x("exp",arguments.length,1);if(v(e))return Math.exp(e);if(e instanceof n){var r=Math.exp(e.re);return new n(r*Math.cos(e.im),r*Math.sin(e.im))}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.exp);if(e.valueOf()!==e)return Z.exp(e.valueOf());throw O("exp",e)},Z.fix=function(e){if(1!=arguments.length)throw x("fix",arguments.length,1);if(v(e))return e>0?Math.floor(e):Math.ceil(e);if(e instanceof n)return new n(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||e instanceof t||e instanceof w)return K.map(e,Z.fix);if(e.valueOf()!==e)return Z.fix(e.valueOf());throw O("fix",e)},Z.floor=function(e){if(1!=arguments.length)throw x("floor",arguments.length,1);if(v(e))return Math.floor(e);if(e instanceof n)return new n(Math.floor(e.re),Math.floor(e.im));if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.floor);if(e.valueOf()!==e)return Z.floor(e.valueOf());throw O("floor",e)},Z.gcd=function(){var e,n=arguments[0],r=arguments[1];if(2==arguments.length){if(v(n)&&v(r)){if(!g(n)||!g(r))throw Error("Parameters in function gcd must be integer numbers");for(;0!=r;)e=r,r=n%e,n=e;return Math.abs(n)}if(n instanceof Array||n instanceof t||r instanceof Array||r instanceof t)return K.map2(n,r,Z.gcd);if(n.valueOf()!==n||r.valueOf()!==r)return Z.gcd(n.valueOf(),r.valueOf());throw O("gcd",n,r)}if(arguments.length>2){for(var i=1;arguments.length>i;i++)n=Z.gcd(n,arguments[i]);return n}throw new SyntaxError("Function gcd expects two or more arguments")},Z.larger=function(e,r){if(2!=arguments.length)throw x("larger",arguments.length,2);if(v(e)){if(v(r))return e>r;if(r instanceof n)return e>Z.abs(r)}if(e instanceof n){if(v(r))return Z.abs(e)>r;if(r instanceof n)return Z.abs(e)>Z.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value>r.value}if(y(e)||y(r))return e>r;if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.larger);if(e.valueOf()!==e||r.valueOf()!==r)return Z.larger(e.valueOf(),r.valueOf());throw O("larger",e,r)},Z.largereq=function(e,r){if(2!=arguments.length)throw x("largereq",arguments.length,2);if(v(e)){if(v(r))return e>=r;if(r instanceof n)return e>=Z.abs(r)}if(e instanceof n){if(v(r))return Z.abs(e)>=r;if(r instanceof n)return Z.abs(e)>=Z.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value>=r.value}if(y(e)||y(r))return e>=r;if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.largereq);if(e.valueOf()!==e||r.valueOf()!==r)return Z.largereq(e.valueOf(),r.valueOf());throw O("largereq",e,r)},Z.lcm=function(){var e,n=arguments[0],r=arguments[1];if(2==arguments.length){if(v(n)&&v(r)){if(!g(n)||!g(r))throw Error("Parameters in function lcm must be integer numbers");for(var i=n*r;0!=r;)e=r,r=n%e,n=e;return Math.abs(i/n)}if(n instanceof Array||n instanceof t||r instanceof Array||r instanceof t)return K.map2(n,r,Z.lcm);if(n.valueOf()!==n||r.valueOf()!==r)return Z.lcm(n.valueOf(),r.valueOf());throw O("lcm",n,r)}if(arguments.length>2){for(var a=1;arguments.length>a;a++)n=Z.lcm(n,arguments[a]);return n}throw new SyntaxError("Function lcm expects two or more arguments")},Z.log=function(e,r){if(1!=arguments.length&&2!=arguments.length)throw x("log",arguments.length,1,2);if(void 0!==r)return Z.divide(Z.log(e),Z.log(r));if(v(e))return e>=0?Math.log(e):Z.log(new n(e,0));if(e instanceof n)return new n(Math.log(Math.sqrt(e.re*e.re+e.im*e.im)),Math.atan2(e.im,e.re));if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.log);if(e.valueOf()!==e||r.valueOf()!==r)return Z.log(e.valueOf(),r.valueOf());throw O("log",e,r)},Z.log10=function(e){if(1!=arguments.length)throw x("log10",arguments.length,1);if(v(e))return e>=0?Math.log(e)/Math.LN10:Z.log10(new n(e,0));if(e instanceof n)return new n(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||e instanceof t||e instanceof w)return K.map(e,Z.log10);if(e.valueOf()!==e)return Z.log10(e.valueOf());throw O("log10",e)},Z.mod=function(e,r){if(2!=arguments.length)throw x("mod",arguments.length,2);if(v(e)){if(v(r))return e%r;if(r instanceof n&&0==r.im)return e%r.re}else if(e instanceof n&&0==e.im){if(v(r))return e.re%r;if(r instanceof n&&0==r.im)return e.re%r.re}if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.mod);if(e.valueOf()!==e||r.valueOf()!==r)return Z.mod(e.valueOf(),r.valueOf());throw O("mod",e,r)},Z.multiply=function(e,r){if(2!=arguments.length)throw x("multiply",arguments.length,2);if(v(e)){if(v(r))return e*r;if(r instanceof n)return M(new n(e,0),r);if(r instanceof E)return o=r.clone(),o.value*=e,o}else if(e instanceof n){if(v(r))return M(e,new n(r,0));if(r instanceof n)return M(e,r)}else if(e instanceof E){if(v(r))return o=e.clone(),o.value*=r,o}else{if(e instanceof Array){if(r instanceof Array){var i=K.size(e),a=K.size(r);if(2!=i.length)throw Error("Can only multiply a 2 dimensional matrix (A has "+i.length+" dimensions)");if(2!=a.length)throw Error("Can only multiply a 2 dimensional matrix (B has "+a.length+" dimensions)");if(i[1]!=a[0])throw new RangeError("Dimensions mismatch in multiplication. Columns of A must match rows of B (A is "+i[0]+"x"+i[1]+", B is "+a[0]+"x"+a[1]+", "+a[1]+" != "+a[0]+")");for(var o=[],f=i[0],s=a[1],u=i[1],c=Z.multiply,l=Z.add,h=0;f>h;h++){o[h]=[];for(var m=0;s>m;m++){for(var p=null,g=0;u>g;g++){var w=c(e[h][g],r[g][m]);p=null==p?w:l(p,w)}o[h][m]=p}}return o}return r instanceof t?new t(Z.multiply(e.valueOf(),r.valueOf())):K.map2(e,r,Z.multiply)}if(e instanceof t)return new t(Z.multiply(e.valueOf(),r.valueOf()))}if(r instanceof Array)return K.map2(e,r,Z.multiply);if(r instanceof t)return new t(Z.multiply(e.valueOf(),r.valueOf()));if(e.valueOf()!==e||r.valueOf()!==r)return Z.multiply(e.valueOf(),r.valueOf());throw O("multiply",e,r)},Z.pow=function(e,r){if(2!=arguments.length)throw x("pow",arguments.length,2);if(v(e)){if(v(r))return g(r)||e>=0?Math.pow(e,r):A(new n(e,0),new n(r,0));if(r instanceof n)return A(new n(e,0),r)}else if(e instanceof n){if(v(r))return A(e,new n(r,0));if(r instanceof n)return A(e,r)}else{if(e instanceof Array){if(!v(r)||!g(r)||0>r)throw new TypeError("For A^b, b must be a positive integer (value is "+r+")");var i=K.size(e);if(2!=i.length)throw Error("For A^b, A must be 2 dimensional (A has "+i.length+" dimensions)");if(i[0]!=i[1])throw Error("For A^b, A must be square (size is "+i[0]+"x"+i[1]+")");if(0==r)return Z.eye(i[0]);for(var a=e,o=1;r>o;o++)a=Z.multiply(e,a);return a}if(e instanceof t)return new t(Z.pow(e.valueOf(),r))}if(e.valueOf()!==e||r.valueOf()!==r)return Z.pow(e.valueOf(),r.valueOf());throw O("pow",e,r)},Z.round=function(e,r){if(1!=arguments.length&&2!=arguments.length)throw x("round",arguments.length,1,2);if(void 0==r){if(v(e))return Math.round(e);if(e instanceof n)return new n(Math.round(e.re),Math.round(e.im));if((e instanceof Array||e instanceof t||e instanceof w)&&K.map(e,Z.round),e.valueOf()!==e)return Z.round(e.valueOf());throw O("round",e)}if(!v(r))throw new TypeError("Number of digits in function round must be an integer");if(r!==Math.round(r))throw new TypeError("Number of digits in function round must be integer");if(0>r||r>9)throw Error("Number of digits in function round must be in te range of 0-9");if(v(e))return S(e,r);if(e instanceof n)return new n(S(e.re,r),S(e.im,r));if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.round);if(e.valueOf()!==e||r.valueOf()!==r)return Z.round(e.valueOf(),r.valueOf());throw O("round",e,r)},Z.sign=function(e){if(1!=arguments.length)throw x("sign",arguments.length,1);if(v(e)){var r;return r=e>0?1:0>e?-1:0}if(e instanceof n){var i=Math.sqrt(e.re*e.re+e.im*e.im);return new n(e.re/i,e.im/i)}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.sign);if(e.valueOf()!==e)return Z.sign(e.valueOf());throw O("sign",e)},Z.smaller=function(e,r){if(2!=arguments.length)throw x("smaller",arguments.length,2);if(v(e)){if(v(r))return r>e;if(r instanceof n)return Z.abs(r)>e}if(e instanceof n){if(v(r))return r>Z.abs(e);if(r instanceof n)return Z.abs(e)<Z.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value<r.value}if(y(e)||y(r))return r>e;if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.smaller);if(e.valueOf()!==e||r.valueOf()!==r)return Z.smaller(e.valueOf(),r.valueOf());throw O("smaller",e,r)},Z.smallereq=function(e,r){if(2!=arguments.length)throw x("smallereq",arguments.length,2);if(v(e)){if(v(r))return r>=e;if(r instanceof n)return Z.abs(r)>=e}if(e instanceof n){if(v(r))return r>=Z.abs(e);if(r instanceof n)return Z.abs(e)<=Z.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value<=r.value}if(y(e)||y(r))return r>=e;if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.smallereq);if(e.valueOf()!==e||r.valueOf()!==r)return Z.smallereq(e.valueOf(),r.valueOf());throw O("smallereq",e,r)},Z.sqrt=function(e){if(1!=arguments.length)throw x("sqrt",arguments.length,1);if(v(e))return e>=0?Math.sqrt(e):Z.sqrt(new n(e,0));if(e instanceof n){var r=Math.sqrt(e.re*e.re+e.im*e.im);return e.im>=0?new n(.5*Math.sqrt(2*(r+e.re)),.5*Math.sqrt(2*(r-e.re))):new n(.5*Math.sqrt(2*(r+e.re)),-.5*Math.sqrt(2*(r-e.re)))}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.sqrt);if(e.valueOf()!==e)return Z.sqrt(e.valueOf());throw O("sqrt",e)},Z.square=function(e){if(1!=arguments.length)throw x("square",arguments.length,1);if(v(e))return e*e;if(e instanceof n)return Z.multiply(e,e);if(e instanceof Array||e instanceof t||e instanceof w)return Z.multiply(e,e);if(e.valueOf()!==e)return Z.square(e.valueOf());throw O("square",e)},Z.subtract=function(e,r){if(2!=arguments.length)throw x("subtract",arguments.length,2);if(v(e)){if(v(r))return e-r;if(r instanceof n)return new n(e-r.re,r.im)}else if(e instanceof n){if(v(r))return new n(e.re-r,e.im);if(r instanceof n)return new n(e.re-r.re,e.im-r.im)}else if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Units do not match");if(!e.hasValue)throw Error("Unit on left hand side of operator - has no value");if(!r.hasValue)throw Error("Unit on right hand side of operator - has no value");var i=e.clone();return i.value-=r.value,i.fixPrefix=!1,i}if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.subtract);if(e.valueOf()!==e||r.valueOf()!==r)return Z.subtract(e.valueOf(),r.valueOf());throw O("subtract",e,r)},Z.unaryminus=function(e){if(1!=arguments.length)throw x("unaryminus",arguments.length,1);if(v(e))return-e;if(e instanceof n)return new n(-e.re,-e.im);if(e instanceof E){var r=e.clone();return r.value=-e.value,r}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.unaryminus);if(e.valueOf()!==e)return Z.unaryminus(e.valueOf());throw O("unaryminus",e)},Z.unequal=function(e,r){if(2!=arguments.length)throw x("unequal",arguments.length,2);if(v(e)){if(v(r))return e==r;if(r instanceof n)return e==r.re&&0==r.im}if(e instanceof n){if(v(r))return e.re==r&&0==e.im;if(r instanceof n)return e.re==r.re&&e.im==r.im}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value==r.value}if(y(e)||y(r))return e==r;if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.unequal);if(e.valueOf()!==e||r.valueOf()!==r)return Z.unequal(e.valueOf(),r.valueOf());throw O("unequal",e,r)},Z.arg=function(e){if(1!=arguments.length)throw x("arg",arguments.length,1);if(v(e))return Math.atan2(0,e);if(e instanceof n)return Math.atan2(e.im,e.re);if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.arg);if(e.valueOf()!==e)return Z.arg(e.valueOf());throw O("arg",e)},Z.conj=function(e){if(1!=arguments.length)throw x("conj",arguments.length,1);if(v(e))return e;if(e instanceof n)return new n(e.re,-e.im);if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.conj);if(e.valueOf()!==e)return Z.conj(e.valueOf());throw O("conj",e)},Z.im=function(e){if(1!=arguments.length)throw x("im",arguments.length,1);if(v(e))return 0;if(e instanceof n)return e.im;if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.im);if(e.valueOf()!==e)return Z.im(e.valueOf());throw O("im",e)},Z.re=function(e){if(1!=arguments.length)throw x("re",arguments.length,1);if(v(e))return e;if(e instanceof n)return e.re;if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.re);if(e.valueOf()!==e)return Z.re(e.valueOf());throw O("re",e)},Z.complex=function(){switch(arguments.length){case 0:return new n(0,0);case 1:var e=arguments[0];if(!y(e))throw new TypeError("Two numbers or a single string expected in function complex");var t=n.parse(e);if(t)return t;throw new SyntaxError('String "'+e+'" is no valid complex number');case 2:return new n(arguments[0],arguments[1]);default:throw x("complex",arguments.length,0,2)}},Z.matrix=function(e){if(arguments.length>1)throw x("matrix",arguments.length,0,1);return new t(e)},Z.parser=function(){return new Z.expr.Parser},Z.range=function(e){switch(arguments.length){case 1:if(!y(e))throw new TypeError("Two or three numbers or a single string expected in function range");var n=w.parse(e);if(n)return n;throw new SyntaxError('String "'+n+'" is no valid range');case 2:return new w(arguments[0],null,arguments[1]);case 3:return new w(arguments[0],arguments[1],arguments[2]);default:throw x("range",arguments.length,2,3)}},Z.unit=function(){switch(arguments.length){case 1:var e=arguments[0];if(!y(e))throw new TypeError("A string or a number and string expected in function unit");if(E.isPlainUnit(e))return new E(null,e);var n=E.parse(e);if(n)return n;throw new SyntaxError('String "'+e+'" is no valid unit');case 2:return new E(arguments[0],arguments[1]);default:throw x("unit",arguments.length,1,2)}},Z.workspace=function(){return new Z.expr.Workspace},Z.concat=function(){var e,n,r=arguments.length,i=-1,a=!1,o=[];for(e=0;r>e;e++){var f=arguments[e];if(f instanceof t&&(a=!0),e==r-1&&v(f)){if(n=i,i=f,!g(i)||1>i)throw new TypeError("Dimension number must be a positive integer (dim = "+i+")");if(e>0&&i>n)throw new RangeError("Dimension out of range ("+i+" > "+n+")")}else{if(!(f instanceof Array||f instanceof t))throw O("concat",f);var s=Z.clone(f.valueOf()),u=Z.size(f);if(o[e]=s,n=i,i=u.length,e>0&&i!=n)throw new RangeError("Dimension mismatch ("+n+" != "+i+")")}}if(0==o.length)throw new SyntaxError("At least one matrix expected");for(var c=o.shift();o.length;)c=T(c,o.shift(),i-1,0);return a?new t(c):c},Z.det=function(e){if(1!=arguments.length)throw x("det",arguments.length,1);var n=Z.size(e);switch(n.length){case 0:return Z.clone(e);case 1:if(1==n[0])return Z.clone(e.valueOf()[0]);throw new RangeError("Matrix must be square (size: "+Z.format(n)+")");case 2:var t=n[0],r=n[1];if(t==r)return U(e.valueOf(),t,r);throw new RangeError("Matrix must be square (size: "+Z.format(n)+")");default:throw new RangeError("Matrix must be two dimensional (size: "+Z.format(n)+")")}},Z.diag=function(e,n){var r,i,a,o;if(1!=arguments.length&&2!=arguments.length)throw x("diag",arguments.length,1,2);if(n){if(!v(n)||!g(n))throw new TypeError("Second parameter in function diag must be an integer")}else n=0;var f=n>0?n:0,s=0>n?-n:0;e instanceof t||e instanceof w||(e=new t(e));var u;switch(e.isVector()?(e=e.toVector(),u=[e.length]):u=e.size(),u.length){case 1:i=e.valueOf();var c=new t;for(c.resize([i.length+s,i.length+f]),r=c.valueOf(),o=i.length,a=0;o>a;a++)r[a+s][a+f]=Z.clone(i[a]);return c;case 2:for(i=[],r=e.valueOf(),o=Math.min(u[0]-s,u[1]-f),a=0;o>a;a++)i[a]=Z.clone(r[a+s][a+f]);return new t(i);default:throw new RangeError("Matrix for function diag must be 2 dimensional")}},Z.eye=function(){var e=K.argsToArray(arguments);if(0==e.length)e=[1,1];else if(1==e.length)e[1]=e[0];else if(e.length>2)throw x("eye",e.length,0,2);var n=e[0],r=e[1];if(!v(n)||!g(n)||1>n)throw Error("Parameters in function eye must be positive integers");if(r&&(!v(r)||!g(r)||1>r))throw Error("Parameters in function eye must be positive integers");var i=new t;i.resize(e);for(var a=Z.min(e),o=i.valueOf(),f=0;a>f;f++)o[f][f]=1;return i},Z.inv=function(e){if(1!=arguments.length)throw x("inv",arguments.length,1);var n=Z.size(e);switch(n.length){case 0:return Z.divide(1,e);case 1:if(1==n[0])return e instanceof t?new t([Z.divide(1,e.valueOf()[0])]):[Z.divide(1,e[0])];throw new RangeError("Matrix must be square (size: "+Z.format(n)+")");case 2:var r=n[0],i=n[1];if(r==i)return e instanceof t?new t(R(e.valueOf(),r,i)):R(e,r,i);throw new RangeError("Matrix must be square (size: "+Z.format(n)+")");default:throw new RangeError("Matrix must be two dimensional (size: "+Z.format(n)+")")}},Z.ones=function(){var e=K.argsToArray(arguments);0==e.length?e=[1,1]:1==e.length&&(e[1]=e[0]);var n=new t,r=1;return n.resize(e,r),n},Z.size=function(e){if(1!=arguments.length)throw x("size",arguments.length,1);if(v(e)||e instanceof n||e instanceof E||null==e)return[];if(y(e))return[e.length];if(e instanceof Array)return K.size(e);if(e instanceof t||e instanceof w)return e.size();if(e.valueOf()!==e)return Z.size(e.valueOf());throw O("size",e)},Z.squeeze=function(e){if(1!=arguments.length)throw x("squeeze",arguments.length,1);return e instanceof t||e instanceof w?_(e.toArray()):e instanceof Array?_(Z.clone(e)):Z.clone(e)},Z.transpose=function(e){if(1!=arguments.length)throw x("transpose",arguments.length,1);var n=Z.size(e);switch(n.length){case 0:return Z.clone(e);case 1:return Z.clone(e);case 2:for(var r,i=n[1],a=n[0],o=e instanceof t,f=e.valueOf(),s=[],u=Z.clone,c=0;i>c;c++){r=s[c]=[];for(var l=0;a>l;l++)r[l]=u(f[l][c])}return 0==a&&(s[0]=[]),o?new t(s):s;default:throw new RangeError("Matrix must be two dimensional (size: "+Z.format(n)+")")}},Z.zeros=function(){var e=K.argsToArray(arguments);0==e.length?e=[1,1]:1==e.length&&(e[1]=e[0]);var n=new t;return n.resize(e),n},Z.factorial=function(e){if(1!=arguments.length)throw x("factorial",arguments.length,1);if(v(e)){if(!g(e))throw new TypeError("Function factorial can only handle integer values");var n=e,r=n;for(n--;n>1;)r*=n,n--;return 0==r&&(r=1),r}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.factorial);if(e.valueOf()!==e)return Z.factorial(e.valueOf());throw O("factorial",e)},Z.random=function(){if(0!=arguments.length)throw x("random",arguments.length,0);return Math.random()},Z.max=function(e){if(0==arguments.length)throw Error("Function max requires one or more parameters (0 provided)");if(e instanceof Array||e instanceof t||e instanceof w){if(arguments.length>1)throw Error("Wrong number of parameters (1 matrix or multiple scalars expected)");var n=Z.size(e);if(1==n.length){if(0==e.length)throw Error("Cannot calculate max of an empty vector");return z(e.valueOf())}if(2==n.length){if(0==n[0]||0==n[1])throw Error("Cannot calculate max of an empty matrix");if(e instanceof Array)return q(e,n[0],n[1]);if(e instanceof t||e instanceof w)return new t(q(e.valueOf(),n[0],n[1]));throw O("max",e)}throw new RangeError("Cannot calculate max for multi dimensional matrix")}return z(arguments)},Z.min=function(e){if(0==arguments.length)throw Error("Function min requires one or more parameters (0 provided)");if(e instanceof Array||e instanceof t||e instanceof w){if(arguments.length>1)throw Error("Wrong number of parameters (1 matrix or multiple scalars expected)");var n=Z.size(e);if(1==n.length){if(0==e.length)throw Error("Cannot calculate min of an empty vector");return I(e.valueOf())}if(2==n.length){if(0==n[0]||0==n[1])throw Error("Cannot calculate min of an empty matrix");if(e instanceof Array)return P(e,n[0],n[1]);if(e instanceof t||e instanceof w)return new t(P(e.valueOf(),n[0],n[1]));throw O("min",e)}throw new RangeError("Cannot calculate min for multi dimensional matrix")}return I(arguments)},Z.acos=function(e){if(1!=arguments.length)throw x("acos",arguments.length,1);if(v(e))return e>=-1&&1>=e?Math.acos(e):Z.acos(new n(e,0));if(e instanceof n){var r=new n(e.im*e.im-e.re*e.re+1,-2*e.re*e.im),i=Z.sqrt(r),a=new n(i.re-e.im,i.im+e.re),o=Z.log(a);return new n(1.5707963267948966-o.im,o.re)}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.acos);if(e.valueOf()!==e)return Z.acos(e.valueOf());throw O("acos",e)},Z.asin=function(e){if(1!=arguments.length)throw x("asin",arguments.length,1);if(v(e))return e>=-1&&1>=e?Math.asin(e):Z.asin(new n(e,0));if(e instanceof n){var r=e.re,i=e.im,a=new n(i*i-r*r+1,-2*r*i),o=Z.sqrt(a),f=new n(o.re-i,o.im+r),s=Z.log(f);return new n(s.im,-s.re)}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.asin);if(e.valueOf()!==e)return Z.asin(e.valueOf());throw O("asin",e)},Z.atan=function(e){if(1!=arguments.length)throw x("atan",arguments.length,1);if(v(e))return Math.atan(e);if(e instanceof n){var r=e.re,i=e.im,a=r*r+(1-i)*(1-i),o=new n((1-i*i-r*r)/a,-2*r/a),f=Z.log(o);return new n(-.5*f.im,.5*f.re)}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.atan);if(e.valueOf()!==e)return Z.atan(e.valueOf());throw O("atan",e)},Z.atan2=function(e,r){if(2!=arguments.length)throw x("atan2",arguments.length,2);if(v(e)){if(v(r))return Math.atan2(e,r);if(r instanceof n)return Math.atan2(e,r.re)}else if(e instanceof n){if(v(r))return Math.atan2(e.re,r);if(r instanceof n)return Math.atan2(e.re,r.re)}if(e instanceof Array||e instanceof t||e instanceof w||r instanceof Array||r instanceof t||r instanceof w)return K.map2(e,r,Z.atan2);if(r.valueOf()!==r||e.valueOf()!==e)return Z.atan2(e.valueOf(),r.valueOf());throw O("atan2",e,r)},Z.cos=function(e){if(1!=arguments.length)throw x("cos",arguments.length,1);if(v(e))return Math.cos(e);if(e instanceof n)return new n(.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 E){if(!e.hasBase(E.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.cos(e.value)}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.cos);if(e.valueOf()!==e)return Z.cos(e.valueOf());throw O("cos",e)},Z.cot=function(e){if(1!=arguments.length)throw x("cot",arguments.length,1);if(v(e))return 1/Math.tan(e);if(e instanceof n){var r=Math.exp(-4*e.im)-2*Math.exp(-2*e.im)*Math.cos(2*e.re)+1;return new n(2*Math.exp(-2*e.im)*Math.sin(2*e.re)/r,(Math.exp(-4*e.im)-1)/r)}if(e instanceof E){if(!e.hasBase(E.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cot is no angle");return 1/Math.tan(e.value)}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.cot);if(e.valueOf()!==e)return Z.cot(e.valueOf());throw O("cot",e)},Z.csc=function(e){if(1!=arguments.length)throw x("csc",arguments.length,1);if(v(e))return 1/Math.sin(e);if(e instanceof n){var r=.25*(Math.exp(-2*e.im)+Math.exp(2*e.im))-.5*Math.cos(2*e.re);return new n(.5*Math.sin(e.re)*(Math.exp(-e.im)+Math.exp(e.im))/r,.5*Math.cos(e.re)*(Math.exp(-e.im)-Math.exp(e.im))/r)}if(e instanceof E){if(!e.hasBase(E.BASE_UNITS.ANGLE))throw new TypeError("Unit in function csc is no angle");return 1/Math.sin(e.value)}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.csc);if(e.valueOf()!==e)return Z.csc(e.valueOf());throw O("csc",e)},Z.sec=function(e){if(1!=arguments.length)throw x("sec",arguments.length,1);if(v(e))return 1/Math.cos(e);if(e instanceof n){var r=.25*(Math.exp(-2*e.im)+Math.exp(2*e.im))+.5*Math.cos(2*e.re);return new n(.5*Math.cos(e.re)*(Math.exp(-e.im)+Math.exp(e.im))/r,.5*Math.sin(e.re)*(Math.exp(e.im)-Math.exp(-e.im))/r)}if(e instanceof E){if(!e.hasBase(E.BASE_UNITS.ANGLE))throw new TypeError("Unit in function sec is no angle");return 1/Math.cos(e.value)}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.sec);if(e.valueOf()!==e)return Z.sec(e.valueOf());throw O("sec",e)},Z.sin=function(e){if(1!=arguments.length)throw x("sin",arguments.length,1);if(v(e))return Math.sin(e);if(e instanceof n)return new n(.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 E){if(!e.hasBase(E.BASE_UNITS.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.sin(e.value)}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.sin);if(e.valueOf()!==e)return Z.sin(e.valueOf());throw O("sin",e)},Z.tan=function(e){if(1!=arguments.length)throw x("tan",arguments.length,1);if(v(e))return Math.tan(e);if(e instanceof n){var r=Math.exp(-4*e.im)+2*Math.exp(-2*e.im)*Math.cos(2*e.re)+1;return new n(2*Math.exp(-2*e.im)*Math.sin(2*e.re)/r,(1-Math.exp(-4*e.im))/r)}if(e instanceof E){if(!e.hasBase(E.BASE_UNITS.ANGLE))throw new TypeError("Unit in function tan is no angle");return Math.tan(e.value)}if(e instanceof Array||e instanceof t||e instanceof w)return K.map(e,Z.tan);if(e.valueOf()!==e)return Z.tan(e.valueOf());throw O("tan",e)},Z["in"]=function(e,n){if(2!=arguments.length)throw x("in",arguments.length,2);if(e instanceof E&&(n instanceof E||y(n)))return e.in(n);if(e instanceof Array||e instanceof t||e instanceof w||n instanceof Array||n instanceof t||n instanceof w)return K.map2(e,n,Z["in"]);if(e.valueOf()!==e)return Z["in"](e.valueOf());throw O("in",e,n)},Z.clone=function(n){if(1!=arguments.length)throw x("clone",arguments.length,1);if(null==n)return n;if("function"==typeof n.clone)return n.clone();if(v(n)||y(n)||e(n))return n;if(n instanceof Array){var t=Z.clone;return n.map(function(e){return t(e)})}if(n instanceof Object)return K.mapObject(n,Z.clone);throw O("clone",n)},Z.format=function(e,n){var t=arguments.length;if(1!=t&&2!=t)throw x("format",t,1,2);if(1==t){var r=arguments[0];return v(r)?K.formatNumber(r):r instanceof Array?K.formatArray(r):y(r)?'"'+r+'"':r instanceof Object?""+r:r+""}if(!y(e))throw new TypeError("String expected as first parameter in function format");if(!(n instanceof Object))throw new TypeError("Object expected as first parameter in function format");return e.replace(/\$([\w\.]+)/g,function(e,t){for(var r=t.split("."),i=n[r.shift()];r.length&&void 0!=i;){var a=r.shift();i=a?i[a]:i+"."}return void 0!=i?i:e})},Z["import"]=function(e,n){var t;if(y(e)){if("undefined"==typeof require)throw Error("Cannot load file: require not available.");var r=require(e);Z["import"](r)}else if(C(e)){if(t=e.name,!t)throw Error("Cannot import an unnamed function or object");(n||void 0===Z[t])&&B(t,e)}else if(e instanceof Object)for(t in e)if(e.hasOwnProperty(t)){var i=e[t];C(i)?(n||void 0===Z[t])&&B(t,i):Z["import"](i)}},Z.select=function(e){return new Z.type.Selector(e)},Z["typeof"]=function(e){if(1!=arguments.length)throw x("typeof",arguments.length,1);var n=typeof e;if("object"==n){if(null==e)return"null";if(e.constructor){for(var t in Z)if(Z.hasOwnProperty(t)&&e.constructor==Z[t])return t.toLowerCase();if(e.constructor.name)return e.constructor.name.toLowerCase()}}return n},Z.expr.node.Node=G,G.prototype.eval=function(){throw Error("Cannot evaluate a Node interface")},G.prototype.toString=function(){return""},V.prototype=new G,Z.expr.node.Symbol=V,V.prototype.hasParams=function(){return void 0!=this.params&&this.params.length>0},V.prototype.eval=function(){var e=this.fn;if(void 0===e)throw Error("Undefined symbol "+this.name);var n=this.params.map(function(e){return e.eval()});return e.apply(this,n)},V.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},D.prototype=new G,Z.expr.node.Constant=D,D.prototype.eval=function(){return this.value},D.prototype.toString=function(){return this.value?Z.format(this.value):""},k.prototype=new G,Z.expr.node.MatrixNode=k,function(){function e(n){return n.map(function(n){return n instanceof Array?e(n):n.eval()})}function n(e){for(var n=[],r=e.length,i=0;r>i;i++){for(var a=e[i],o=a.length,f=null,s=null,u=0;o>u;u++){var c,l=Z.clone(a[u]);if(l instanceof t){if(c=l.size(),l=l.valueOf(),1==c.length)l=[l],c=[1,c[0]];else if(c.length>2)throw Error("Cannot merge a multi dimensional matrix")}else l instanceof w?(l=[l.valueOf()],c=[1,l[0].length]):(c=[1,1],l=[[l]]);if(null==f)f=l,s=c[0];else{if(c[0]!=s)throw Error("Dimension mismatch ("+c[0]+" != "+s+")");for(var h=0;s>h;h++)f[h]=f[h].concat(l[h])}}n=n.concat(f)}return n}function r(e){return e.some(function(e){return e instanceof t||e instanceof w?!0:e instanceof Array?r(e):!1})}k.prototype.eval=function(){var i=e(this.nodes);return r(i)&&(i=n(i)),new t(i)},k.prototype.toString=function(){return K.formatArray(this.nodes)
|
|
}}(),F.prototype=new G,Z.expr.node.Block=F,F.prototype.add=function(e,n){var t=this.params.length;this.params[t]=e,this.visible[t]=void 0!=n?n:!0},F.prototype.eval=function(){for(var e=[],n=0,t=this.params.length;t>n;n++){var r=this.params[n].eval();this.visible[n]&&e.push(r)}return e},F.prototype.toString=function(){for(var e=[],n=0,t=this.params.length;t>n;n++)this.visible[n]&&e.push("\n "+(""+this.params[n]));return"["+e.join(",")+"\n]"},j.prototype=new G,Z.expr.node.Assignment=j,j.prototype.eval=function(){if(void 0===this.expr)throw Error("Undefined symbol "+this.name);var e,n=this.params;if(n&&n.length){var t=[];this.params.forEach(function(e){t.push(e.eval())});var r=this.expr.eval();if(void 0==this.result.value)throw Error("Undefined symbol "+this.name);var i=this.result();if(!i.set)throw new TypeError("Cannot apply a subset to object of type "+Z.typeof(i));e=i.set(t,r),this.result.value=e}else e=this.expr.eval(),this.result.value=e;return e},j.prototype.toString=function(){var e="";return e+=this.name,this.params&&this.params.length&&(e+="("+this.params.join(", ")+")"),e+=" = ",e+=""+this.expr},H.prototype=new G,Z.expr.node.Arguments=H,H.prototype.eval=function(){var e=this.object;if(void 0==e)throw Error("Node undefined");for(var n=e.eval(),t=this.params,r=[],i=0,a=t.length;a>i;i++)r[i]=t[i].eval();if(!n.get)throw new TypeError("Cannot apply arguments to object of type "+Z.typeof(n));return n.get(r)},H.prototype.toString=function(){var e=this.object?""+this.object:"";return this.params&&(e+="("+this.params.join(", ")+")"),e},Y.prototype=new G,Z.expr.node.FunctionAssignment=Y,Y.prototype.createFunction=function(e,n,t,r){var i=function(){var n=t?t.length:0,i=arguments?arguments.length:0;if(n!=i)throw x(e,i,n);if(n>0)for(var a=0;n>a;a++)t[a].value=arguments[a];return r.eval()};return i.toString=function(){return e+"("+n.join(", ")+")"},i},Y.prototype.eval=function(){for(var e=this.variables,n=this.values,t=0,r=e.length;r>t;t++)e[t].value=n[t];return this.result.value=this.def,this.def},Y.prototype.toString=function(){return""+this.def},Z.expr.Scope=function(e){this.parentScope=e,this.nestedScopes=void 0,this.symbols={},this.defs={},this.updates={},this.links={}},Z.expr.Scope.prototype={createNestedScope:function(){var e=new Z.expr.Scope(this);return this.nestedScopes||(this.nestedScopes=[]),this.nestedScopes.push(e),e},clear:function(){if(this.symbols={},this.defs={},this.links={},this.updates={},this.nestedScopes)for(var e=this.nestedScopes,n=0,t=e.length;t>n;n++)e[n].clear()},createSymbol:function(e){var n=this.symbols[e];if(!n){var t=this.findDef(e);n=this.newSymbol(e,t),this.symbols[e]=n}return n},newSymbol:function(e,n){var r=this,i=function(){var n,a;if(!i.value&&(i.value=r.findDef(e),!i.value))throw Error("Undefined symbol "+e);if("function"==typeof i.value)return i.value.apply(null,arguments);if(i.value instanceof t||i.value instanceof w||i.value instanceof Array){if(arguments.length){var o=i.value instanceof Array?new t(i.value):i.value;for(n=[],a=0;arguments.length>a;a++)n[a]=arguments[a];return o.get(n)}return i.value}return i.value};return i.value=n,i.toString=function(){return i.value?""+i.value:""},i},createLink:function(e){var n=this.links[e];return n||(n=this.createSymbol(e),this.links[e]=n),n},createDef:function(e,n){var t=this.defs[e];return t||(t=this.createSymbol(e),this.defs[e]=t),t&&void 0!=n&&(t.value=n),t},createUpdate:function(e){var n=this.updates[e];return n||(n=this.createLink(e),this.updates[e]=n),n},createConstant:function(e,n){var t=this.newSymbol(e,n);return this.symbols[e]=t,this.defs[e]=t,t},findDef:function(e){var n;if(n=this.defs[e])return n;if(n=this.updates[e])return n;if(this.parentScope)return this.parentScope.findDef(e);var t=Z[e];if(t)return this.createConstant(e,t);if(E.isPlainUnit(e)){var r=new E(null,e);return this.createConstant(e,r)}return void 0},removeLink:function(e){delete this.links[e]},removeDef:function(e){delete this.defs[e]},removeUpdate:function(e){delete this.updates[e]},init:function(){var e=this.symbols,n=this.parentScope;for(var t in e)if(e.hasOwnProperty(t)){var r=e[t];r.value=n?n.findDef(t):void 0}this.nestedScopes&&this.nestedScopes.forEach(function(e){e.init()})},hasLink:function(e){if(this.links[e])return!0;if(this.nestedScopes)for(var n=this.nestedScopes,t=0,r=n.length;r>t;t++)if(n[t].hasLink(e))return!0;return!1},hasDef:function(e){return void 0!=this.defs[e]},hasUpdate:function(e){return void 0!=this.updates[e]},getUndefinedSymbols:function(){var e=this.symbols,n=[];for(var t in e)if(e.hasOwnProperty(t)){var r=e[t];void 0==r.value&&n.push(r)}return this.nestedScopes&&this.nestedScopes.forEach(function(e){n=n.concat(e.getUndefinedSymbols())}),n}},function(){function e(){W++,K=H.charAt(W)}function t(){W=0,K=H.charAt(0)}function r(){for(Q=G.NULL,X="";" "==K||" "==K;)e();if("#"==K)for(;"\n"!=K&&""!=K;)e();if(""==K)return Q=G.DELIMITER,void 0;if("-"==K||","==K||"("==K||")"==K||"["==K||"]"==K||'"'==K||"\n"==K||";"==K||":"==K||"!"==K||"'"==K)return Q=G.DELIMITER,X+=K,e(),void 0;if(i(K))for(Q=G.DELIMITER;i(K);)X+=K,e();else if(o(K)){for(Q=G.NUMBER;o(K);)X+=K,e();if("E"==K||"e"==K)for(X+=K,e(),("+"==K||"-"==K)&&(X+=K,e()),f(K)||(Q=G.UNKNOWN);f(K);)X+=K,e()}else{if(!a(K)){for(Q=G.UNKNOWN;""!=K;)X+=K,e();throw I('Syntax error in part "'+X+'"')}for(Q=G.SYMBOL;a(K)||f(K);)X+=K,e()}}function i(e){return"&"==e||"|"==e||"<"==e||">"==e||"="==e||"+"==e||"/"==e||"*"==e||"%"==e||"^"==e||","==e||";"==e||"\n"==e||"!"==e||"'"==e}function a(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e||"_"==e}function o(e){return e>="0"&&"9">=e||"."==e}function f(e){return e>="0"&&"9">=e}function s(e){t(),r();var n;if(n=""==X?new D(void 0):u(e),""!=X)throw Q==G.DELIMITER?B("Unknown operator "+X):I('Unexpected part "'+X+'"');return n}function u(e){var n,t,i;for("\n"!=X&&";"!=X&&""!=X&&(n=c(e));"\n"==X||";"==X;)t||(t=new F,n&&(i=";"!=X,t.add(n,i))),r(),"\n"!=X&&";"!=X&&""!=X&&(n=c(e),i=";"!=X,t.add(n,i));return t?t:(n||(n=c(e)),n)}function c(e){var n=l(e);if(!(n instanceof j)){var t="ans",r=void 0,i=e.createDef(t);return new j(t,r,n,i)}return n}function l(e){if(Q==G.SYMBOL&&"function"==X){if(r(),Q!=G.SYMBOL)throw I("Function name expected");var n=X;if(r(),"("!=X)throw I("Opening parenthesis ( expected");for(var t=e.createNestedScope(),i=[],a=[];;){if(r(),Q!=G.SYMBOL)throw I("Variable name expected");var o=X,f=t.createDef(o);if(i.push(o),a.push(f),r(),","!=X){if(")"==X)break;throw I('Comma , or closing parenthesis ) expected"')}}if(r(),"="!=X)throw I("Equal sign = expected");r();var s=m(t),u=e.createDef(n);return new Y(n,i,a,s,u)}return h(e)}function h(e){var n=!1;Q==G.SYMBOL&&(n=e.hasLink(X));var t=m(e);if("="==X){if(!(t instanceof V))throw I("Symbol expected at the left hand side of assignment operator =");var i=t.name,a=t.params;n||e.removeLink(i),r();var o=m(e),f=t.hasParams()?e.createUpdate(i):e.createDef(i);return new j(i,a,o,f)}return t}function m(e){var n=p(e);if(":"==X){for(var t=[n];":"==X;)r(),t.push(p(e));if(t.length>3)throw new TypeError("Invalid range");var i="range",a=Z.range;n=new V(i,a,t)}return n}function p(e){for(var n=v(e),t={"in":"in"};void 0!==t[X];){var i=X,a=Z[t[i]];r();var o=[n,v(e)];n=new V(i,a,o)}return n}function v(e){var n=g(e);return n}function g(e){for(var n=w(e),t={"==":"equal","!=":"unequal","<":"smaller",">":"larger","<=":"smallereq",">=":"largereq"};void 0!==t[X];){var i=X,a=Z[t[i]];r();var o=[n,w(e)];n=new V(i,a,o)}return n}function w(e){for(var n=d(e),t={"+":"add","-":"subtract"};void 0!==t[X];){var i=X,a=Z[t[i]];r();var o=[n,d(e)];n=new V(i,a,o)}return n}function d(e){for(var n=y(e),t={"*":"multiply","/":"divide","%":"mod",mod:"mod"};void 0!==t[X];){var i=X,a=Z[t[i]];r();var o=[n,y(e)];n=new V(i,a,o)}return n}function y(e){if("-"==X){var n=X,t=Z.unaryminus;r();var i=[N(e)];return new V(n,t,i)}return N(e)}function N(e){for(var n=[O(e)];"^"==X;)r(),n.push(O(e));for(var t=n.pop();n.length;){var i=n.pop(),a="^",o=Z.pow,f=[i,t];t=new V(a,o,f)}return t}function O(e){for(var n=x(e);"!"==X;){var t=X,i=Z.factorial;r();var a=[n];n=new V(t,i,a)}return n}function x(e){for(var n=b(e);"'"==X;){var t=X,i=Z.transpose;r();var a=[n];n=new V(t,i,a)}return n}function b(e){return M(e)}function M(e){if(Q==G.SYMBOL){var n=X;r();var t=e.createLink(n),i=A(e),a=new V(n,t,i);return a}return S(e)}function A(e){var n=[];if("("==X){if(r(),")"!=X)for(n.push(m(e));","==X;)r(),n.push(m(e));if(")"!=X)throw I("Parenthesis ) missing");r()}return n}function S(n){if('"'==X){for(var t="",i="";""!=K&&('"'!=K||"\\"==i);)t+=K,i=K,e();if(r(),'"'!=X)throw I('End of string " missing');r();var a=new D(t);return a}return T(n)}function T(e){if("["==X){var n;for(r();"\n"==X;)r();if("]"!=X){var t=[],i=0,a=0;for(t[0]=[m(e)];","==X||";"==X;){for(","==X?a++:(i++,a=0,t[i]=[]),r();"\n"==X;)r();for(t[i][a]=m(e);"\n"==X;)r()}var o=t.length,f=t.length>0?t[0].length:0;for(i=1;o>i;i++)if(t[i].length!=f)throw B("Number of columns must match ("+t[i].length+" != "+f+")");if("]"!=X)throw I("End of matrix ] missing");r(),n=new k(t)}else r(),n=new k([[]]);return n}return U(e)}function U(e){if(Q==G.NUMBER){var t;t="."==X?0:Number(X),r();var i;if(Q==G.SYMBOL){if("i"==X||"I"==X)return i=new n(0,t),r(),new D(i);if(E.isPlainUnit(X))return i=new E(t,X),r(),new D(i);throw P('Unknown unit "'+X+'"')}var a=new D(t);return a}return L(e)}function L(e){if("("==X){r();var n=m(e);if(")"!=X)throw I("Parenthesis ) expected");return r(),n}return R(e)}function R(){throw""==X?I("Unexpected end of expression"):I("Value expected")}function _(){return void 0}function z(){return W-X.length+1}function q(e){var n=_(),t=z();return void 0===n?void 0===t?e:e+" (char "+t+")":e+" (line "+n+", char "+t+")"}function I(e){return new SyntaxError(q(e))}function P(e){return new TypeError(q(e))}function B(e){return Error(q(e))}Z.expr.Parser=function C(){if(this.constructor!=C)throw new SyntaxError("Parser constructor must be called with the new operator");this.scope=new Z.expr.Scope},Z.expr.Parser.prototype.parse=function(e,n){return H=e||"",n||(n=this.scope),s(n)},Z.expr.Parser.prototype.eval=function(e){var n=this.parse(e);return n.eval()},Z.expr.Parser.prototype.get=function(e){var n=this.scope.findDef(e);return n?n.value:void 0},Z.expr.Parser.prototype.set=function(e,n){this.scope.createDef(e,n)},Z.expr.Parser.prototype.clear=function(){this.scope.clear()};var G={NULL:0,DELIMITER:1,NUMBER:2,SYMBOL:3,UNKNOWN:4},H="",W=0,K="",X="",Q=G.NULL}(),function(){function e(){this.idMax=-1,this.updateSeq=0,this.parser=new Z.expr.Parser,this.scope=new Z.expr.Scope,this.nodes={},this.firstNode=void 0,this.lastNode=void 0}Z.expr.Workspace=e,e.prototype.clear=function(){this.nodes={},this.firstNode=void 0,this.lastNode=void 0},e.prototype.append=function(n){var t=this._getNewId(),r=this.lastNode?this.lastNode.scope:this.scope,i=new Z.expr.Scope(r),a=new e.Node({id:t,expression:n,parser:this.parser,scope:i,nextNode:void 0,previousNode:this.lastNode});return this.nodes[t]=a,this.firstNode||(this.firstNode=a),this.lastNode&&(this.lastNode.nextNode=a),this.lastNode=a,this._update([t]),t},e.prototype.insertBefore=function(n,t){var r=this.nodes[t];if(!r)throw'Node with id "'+t+'" not found';var i=r.previousNode,a=this._getNewId(),o=i?i.scope:this.scope,f=new Z.expr.Scope(o),s=new e.Node({id:a,expression:n,parser:this.parser,scope:f,nextNode:r,previousNode:i});this.nodes[a]=s,i?i.nextNode=s:this.firstNode=s,r.previousNode=s,r.scope.parentScope=s.scope;var u=this.getDependencies(a);return-1==u.indexOf(a)&&u.unshift(a),this._update(u),a},e.prototype.insertAfter=function(e,n){var t=this.nodes[n];if(!t)throw'Node with id "'+n+'" not found';return t==this.lastNode?this.append(e):this.insertBefore(n+1,e)},e.prototype.remove=function(e){var n=this.nodes[e];if(!n)throw'Node with id "'+e+'" not found';var t=this.getDependencies(e),r=n.previousNode,i=n.nextNode;r?r.nextNode=i:this.firstNode=i,i?i.previousNode=r:this.lastNode=r;var a=r?r.scope:this.scope;i&&(i.scope.parentScope=a),delete this.nodes[e],this._update(t)},e.prototype.replace=function(n,t){var r=this.nodes[t];if(!r)throw'Node with id "'+t+'" not found';var i=[t];e._merge(i,this.getDependencies(t));var a=r.previousNode;r.nextNode,a?a.scope:this.scope,r.setExpr(n),e._merge(i,this.getDependencies(t)),this._update(i)},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 n="Error: "+((e.message||e)+"");this.fn=new D(n)}},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,n){for(var t=0,r=n.length;r>t;t++){var i=n[t];-1==e.indexOf(i)&&e.push(i)}},e.prototype.getDependencies=function(n){var t,r=[],i=this.nodes[n];if(i){var a=i.scope.defs,o=i.scope.updates,f=[];for(t in a)a.hasOwnProperty(t)&&f.push(t);for(t in o)o.hasOwnProperty(t)&&-1==f.indexOf(t)&&f.push(t);for(var s=i.nextNode;s&&f.length;){for(var u=s.scope,c=0;f.length>c;){if(t=f[c],(u.hasLink(t)||u.hasUpdate(t))&&-1==r.indexOf(s.id)){r.push(s.id);var l=this.getDependencies(s.id);e._merge(r,l)}u.hasDef(t)&&(f.splice(c,1),c--),c++}s=s.nextNode}}return r},e.prototype.getExpr=function(e){var n=this.nodes[e];if(!n)throw'Node with id "'+e+'" not found';return n.getExpr()},e.prototype.getResult=function(e){var n=this.nodes[e];if(!n)throw'Node with id "'+e+'" not found';return n.getResult()},e.prototype._update=function(e){this.updateSeq++;for(var n=this.updateSeq,t=this.nodes,r=0,i=e.length;i>r;r++){var a=e[r],o=t[a];o&&(o.eval(),o.updateSeq=n)}},e.prototype.getChanges=function(e){var n=[],t=this.firstNode;for(e=e||0;t;)t.updateSeq>e&&n.push(t.id),t=t.nextNode;return{ids:n,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=[],n=this.firstNode;n;){var t={id:n.id,expression:n.expression,dependencies:this.getDependencies(n.id)};try{t.result=n.getResult()}catch(r){t.result="Error: "+((r.message||r)+"")}e.push(t),n=n.nextNode}return e}}(),Z.Complex=function(){W("new math.Complex()","math.complex()")},Z.Unit=function(){W("new math.Unit()","math.unit()")},Z.parser.Parser=function(){W("new math.parser.Parser()","math.parser()")},Z.parser.Workspace=function(){W("new math.parser.Workspace()","math.workspace()")};for(var en in Z)Z.hasOwnProperty(en)&&en&&d(en,Z[en])})(); |