mathjs/math.min.js
2013-05-10 23:15:52 +02:00

29 lines
82 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.8.1
* @date 2013-05-10
*
* @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 instanceof 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 instanceof 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 ("+K["typeof"](e)+")");this._data=[]}this._size=Z.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)}),K.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 s(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 f(e,n,t){var r=t==n.length-1,a=n[t],o=function(a){var o=i(e,a);return r?o:f(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;o<t.length;o++){var s=t[o];r(s),(null==n[o]||s>n[o])&&(n[o]=s,a=!0)}a&&Z.resize(e,n,0);var f=n.length;t.forEach(function(n,t){f-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]&&Z.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 s=!1;a>(n[0]||0)&&(n[0]=a,s=!0),o>(n[1]||0)&&(n[1]=o,s=!0),s&&Z.resize(e,n,0),e[a-1][o-1]=i}function p(e,n,t,r,i){var a=r==t.length-1,o=t[r],s=function(o,s){if(a)u(e,o,i[s]),o>(n[r]||0)&&(n[r]=o);else{var f=e[o-1];f instanceof Array||(e[o-1]=f=[f]),o>(n[r]||0)&&(n[r]=o),p(f,n,t,r+1,i[s])}};if(o.map){var f=o.size&&o.size()||o.length;if(f!=i.length)throw new RangeError("Dimensions mismatch ("+f+" != "+i.length+")");o.map(s)}else s(o,0)}function m(e){for(var n=0,t=e.length;t>n;n++){var r=e[n];r instanceof Array?m(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 instanceof 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=K.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 instanceof E))throw new Error("Unit constructor must be called with the new operator");if(null!=e&&!v(e))throw new Error("First parameter in Unit constructor must be a number");if(null!=n&&!y(n))throw new Error("Second parameter in Unit constructor must be a string");if(null!=n){var t=N(n);if(!t)throw new Error('String "'+n+'" is no unit');this.unit=t.unit,this.prefix=t.prefix}else this.unit=E.UNIT_NONE,this.prefix=E.PREFIX_NONE;null!=e?(this.value=this._normalize(e),this.fixPrefix=!1):(this.value=null,this.fixPrefix=!0)}function N(e){for(var n=E.UNITS,t=0,r=n.length;r>t;t++){var i=n[t];if(Z.endsWith(e,i.name)){var a=e.length-i.name.length,o=e.substring(0,a),s=i.prefixes[o];if(void 0!==s)return{unit:i,prefix:s}}}return null}function O(e,n){var t=void 0;if(2==arguments.length){var r=K["typeof"](n);t="Function "+e+"("+r+") not supported"}else if(arguments.length>2){for(var i=[],a=1;a<arguments.length;a++)i.push(K["typeof"](arguments[a]));t="Function "+e+"("+i.join(", ")+") not supported"}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(){}function M(e){this.value=e}function A(e,n,t){this.name=e,this.fn=n,this.params=t}function S(e,n){this.name=e,this.symbol=n}function T(e,n,t){this.object=e,this.params=n,this.scopes=t;var r=null;if(t)for(var i=0,a=t.length;a>i;i++){var o=t[i];o.hasLink("end")&&(r||(r=[]),r[i]=o.createLink("end"))}this.ends=r}function U(e){this.nodes=e||[]}function L(){this.params=[],this.visible=[]}function R(e,n,t,r,i){this.name=e,this.params=n,this.expr=r,this.symbol=i;var a=null;if(t)for(var o=0,s=t.length;s>o;o++){var f=t[o];f.hasLink("end")&&(a||(a=[]),a[o]=f.createLink("end"))}this.ends=a}function z(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.fn=this.createFunction(e,n,t,r),this.symbol=i}function _(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 q(e,t){return new n(e.re*t.re-e.im*t.im,e.re*t.im+e.im*t.re)}function I(e,n){var t=K.log(e),r=K.multiply(t,n);return K.exp(r)}function P(e,n){if(n){var t=Math.pow(10,n);return Math.round(e*t)/t}return Math.round(e)}function B(e,n,t,r){if(t>r){if(e.length!=n.length)throw new Error("Dimensions mismatch ("+e.length+" != "+n.length+")");for(var i=[],a=0;a<e.length;a++)i[a]=B(e[a],n[a],t,r+1);return i}return e.concat(n)}function C(e,n,t){var r=K.multiply,i=K.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 s=G(e,n,t,0,o);a+=r(r((o+1)%2+(o+1)%2-1,e[0][o]),C(s,n-1,t-1))}return a}function G(e,n,t,r,i){for(var a,o=[],s=0;n>s;s++)if(s!=r){a=o[s-(s>r)]=[];for(var f=0;t>f;f++)f!=i&&(a[f-(f>i)]=e[s][f])}return o}function D(e,n,t){var r,i,a,o,s,f=K.add,u=K.unaryminus,c=K.multiply,l=K.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=K.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 p=e.concat();for(r=0;n>r;r++)p[r]=p[r].concat();for(var m=K.eye(n).valueOf(),v=0;t>v;v++){for(r=v;n>r&&0==p[r][v];)r++;if(r==n||0==p[r][v])throw Error("Cannot calculate inverse, determinant is zero");r!=v&&(s=p[v],p[v]=p[r],p[r]=s,s=m[v],m[v]=m[r],m[r]=s);var g=p[v],w=m[v];for(r=0;n>r;r++){var d=p[r],y=m[r];if(r!=v){if(0!=d[v]){for(a=l(u(d[v]),g[v]),i=v;t>i;i++)d[i]=f(d[i],c(a,g[i]));for(i=0;t>i;i++)y[i]=f(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 m}function F(e){if(1==e.length)return F(e[0]);for(var n=0,t=e.length;t>n;n++){var r=e[n];r instanceof Array&&(e[n]=F(r))}return e}function k(e){for(var n=K.larger,t=e[0],r=1,i=e.length;i>r;r++){var a=e[r];n(a,t)&&(t=a)}return t}function V(e,n,t){for(var r=K.larger,i=[],a=0;t>a;a++){for(var o=e[0][a],s=1;n>s;s++){var f=e[s][a];r(f,o)&&(o=f)}i[a]=o}return i}function j(e){for(var n=K.smaller,t=e[0],r=1,i=e.length;i>r;r++){var a=e[r];n(a,t)&&(t=a)}return t}function H(e,n,t){for(var r=K.smaller,i=[],a=0;t>a;a++){for(var o=e[0][a],s=1;n>s;s++){var f=e[s][a];r(f,o)&&(o=f)}i[a]=o}return i}function Y(e,n){K[e]=n,d(e,n)}function W(e){return"function"==typeof e||v(e)||y(e)||e instanceof n||e instanceof E}var K={type:{},expr:{node:{}},options:{precision:5}};"undefined"!=typeof module&&"undefined"!=typeof module.exports&&(module.exports=K),"undefined"!=typeof exports&&(exports=K),"undefined"!=typeof require&&"undefined"!=typeof define&&define(function(){return K}),"undefined"!=typeof window&&(window.math=K);var Z=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(r<t.length-1){var o=r+1;for(i=0;a>i;i++){var s=e[i];if(!(s 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(t<n.length-1){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 s=e.length;o>s;s++)e[s]=r?K.clone(r):0;else e.length=n[t];a=e.length}if(t<n.length-1){var f=t+1;for(s=0;a>s;s++)u=e[s],u instanceof Array||(u=[u],e[s]=u),i(u,n,f,r)}else for(s=0;a>s;s++){for(var u=e[s];u instanceof Array;)u=u[0];e[s]=u}}var a={};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>.001&&1e5>t||0==t)return a.toPrecision(e,n);var r=Math.round(Math.log(t)/Math.LN10),i=e/Math.pow(10,r);return a.toPrecision(i,n)+"e"+r},a.toPrecision=function(e,n){return void 0==n&&(n=K.options.precision),e.toPrecision(n).replace(o,function(e,n,t){return e.substring(0,e.length-(n.length?0:1)-t.length)})};var o=/\.(\d*?)(0+)$/g;return 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 K.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 s=e[o],f=0;i>f;f++){0!=f&&(n+=", ");var u=s[f];void 0!=u&&(n+=K.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;r<e.length;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.extend=function(e,n){for(var t in n)n.hasOwnProperty(t)&&(e[t]=n[t]);return e},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,s;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 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,s=0;o>s;s++)i[s]=r(e[s],n[s])}else for(i=[],o=e.length,s=0;o>s;s++)i[s]=r(e[s],n);else if(n instanceof Array)for(i=[],o=n.length,s=0;o>s;s++)i[s]=r(e,n[s]);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 "+K["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)},a}();K.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=f.charAt(u)}function a(e){u=e,c=f.charAt(u)}function o(){var e,n="";if(e=u,"+"==c?i():"-"==c&&(n+=c,i()),!t(c))return a(e),null;if("."==c){if(n+=c,i(),!r(c))return a(e),null}else{for(;r(c);)n+=c,i();"."==c&&(n+=c,i())}for(;r(c);)n+=c,i();if("E"==c||"e"==c){if(n+=c,i(),("+"==c||"-"==c)&&(n+=c,i()),!r(c))return a(e),null;for(;r(c);)n+=c,i()}return n}function s(){var e=f.charAt(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 f,u,c;n.parse=function(t){if(f=t,u=-1,c="",!y(f))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=s(),!l)return null;return"-"==a&&(l="-"==l[0]?"+"+l.substring(1):"-"+l),i(),e(),c?null:new n(Number(r),Number(l))}return(r=s())?(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=Z.formatNumber(this.re),t=Z.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+" - "+Z.formatNumber(Math.abs(this.im))+"i"},K.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 "+K["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(s(this._data,e));default:return new t(f(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 "+K["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!=K.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();p(this._data,i,e,0,n),Z.deepEqual(this._size,i)||(m(this._data),this.resize(i))}return this},t.prototype.resize=function(e,n){Z.resize(this._data,e,n),this._size=K.clone(e)},t.prototype.clone=function(){var e=new t;return e._data=K.clone(this._data),e._size=K.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=K.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:K.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 K.clone(this._data)},t.prototype.valueOf=function(){return this._data},t.prototype.toString=function(){return K.format(this._data)},K.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 K.sign(t)==K.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=K.format(Number(this.start));return 1!=this.step&&(e+=":"+K.format(Number(this.step))),e+=":"+K.format(Number(this.end))},K.type.Selector=function(e){if(!(this instanceof K.type.Selector))throw new SyntaxError("Selector constructor must be called with the new operator");this.value=e instanceof K.type.Selector?e.value:e||void 0},K.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 K.type.Selector(e.get.apply(e,arguments));if(e instanceof Array)return e=new t(e),new K.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 K.type.Selector(e.set.apply(e,arguments));if(e instanceof Array)return e=new t(e),new K.type.Selector(e.set.apply(e,arguments).valueOf());throw Error("Selector value has no method set")},valueOf:function(){return this.value},toString:function(){return K.format(this.value)}},K.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(){f++,u=s.charAt(f)}function i(e){f=e,u=s.charAt(f)}function a(){var e,a="";if(e=f,"+"==u?r():"-"==u&&(a+=u,r()),!n(u))return i(e),null;if("."==u){if(a+=u,r(),!t(u))return i(e),null}else{for(;t(u);)a+=u,r();"."==u&&(a+=u,r())}for(;t(u);)a+=u,r();if("E"==u||"e"==u){if(a+=u,r(),("+"==u||"-"==u)&&(a+=u,r()),!t(u))return i(e),null;for(;t(u);)a+=u,r()}return a}function o(){var n="";for(e();u&&" "!=u&&" "!=u;)n+=u,r();return n||null}var s,f,u;E.parse=function(n){if(s=n,f=-1,u="",!y(s))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 new Error("Units do not match");return n.value=this.value,n}if(e instanceof E){if(!this.equalBase(e))throw new Error("Units do not match");if(null!=e.value)throw new Error("Cannot convert to a unit with a value");if(null==e.unit)throw new Error("Unit expected on the right hand side of function in");return n=e.clone(),n.value=this.value,n.fixPrefix=!0,n}throw new 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,n;if(this.fixPrefix)e=this._unnormalize(this.value),n=null!=this.value?Z.formatNumber(e)+" ":"",n+=this.prefix.name+this.unit.name;else{var t=this._bestPrefix();e=this._unnormalize(this.value,t.value),n=null!=this.value?Z.formatNumber(e)+" ":"",n+=t.name+this.unit.name}return n},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 X=E.BASE_UNITS,Q=E.PREFIXES;E.BASE_UNIT_NONE={},E.UNIT_NONE={name:"",base:E.BASE_UNIT_NONE,value:1,offset:0},E.UNITS=[{name:"meter",base:X.LENGTH,prefixes:Q.LONG,value:1,offset:0},{name:"inch",base:X.LENGTH,prefixes:Q.NONE,value:.0254,offset:0},{name:"foot",base:X.LENGTH,prefixes:Q.NONE,value:.3048,offset:0},{name:"yard",base:X.LENGTH,prefixes:Q.NONE,value:.9144,offset:0},{name:"mile",base:X.LENGTH,prefixes:Q.NONE,value:1609.344,offset:0},{name:"link",base:X.LENGTH,prefixes:Q.NONE,value:.201168,offset:0},{name:"rod",base:X.LENGTH,prefixes:Q.NONE,value:5.02921,offset:0},{name:"chain",base:X.LENGTH,prefixes:Q.NONE,value:20.1168,offset:0},{name:"angstrom",base:X.LENGTH,prefixes:Q.NONE,value:1e-10,offset:0},{name:"m",base:X.LENGTH,prefixes:Q.SHORT,value:1,offset:0},{name:"ft",base:X.LENGTH,prefixes:Q.NONE,value:.3048,offset:0},{name:"yd",base:X.LENGTH,prefixes:Q.NONE,value:.9144,offset:0},{name:"mi",base:X.LENGTH,prefixes:Q.NONE,value:1609.344,offset:0},{name:"li",base:X.LENGTH,prefixes:Q.NONE,value:.201168,offset:0},{name:"rd",base:X.LENGTH,prefixes:Q.NONE,value:5.02921,offset:0},{name:"ch",base:X.LENGTH,prefixes:Q.NONE,value:20.1168,offset:0},{name:"mil",base:X.LENGTH,prefixes:Q.NONE,value:254e-7,offset:0},{name:"m2",base:X.SURFACE,prefixes:Q.SHORT,value:1,offset:0},{name:"sqin",base:X.SURFACE,prefixes:Q.NONE,value:64516e-8,offset:0},{name:"sqft",base:X.SURFACE,prefixes:Q.NONE,value:.09290304,offset:0},{name:"sqyd",base:X.SURFACE,prefixes:Q.NONE,value:.83612736,offset:0},{name:"sqmi",base:X.SURFACE,prefixes:Q.NONE,value:2589988.110336,offset:0},{name:"sqrd",base:X.SURFACE,prefixes:Q.NONE,value:25.29295,offset:0},{name:"sqch",base:X.SURFACE,prefixes:Q.NONE,value:404.6873,offset:0},{name:"sqmil",base:X.SURFACE,prefixes:Q.NONE,value:6.4516e-10,offset:0},{name:"m3",base:X.VOLUME,prefixes:Q.SHORT,value:1,offset:0},{name:"L",base:X.VOLUME,prefixes:Q.SHORT,value:.001,offset:0},{name:"litre",base:X.VOLUME,prefixes:Q.LONG,value:.001,offset:0},{name:"cuin",base:X.VOLUME,prefixes:Q.NONE,value:16387064e-12,offset:0},{name:"cuft",base:X.VOLUME,prefixes:Q.NONE,value:.028316846592,offset:0},{name:"cuyd",base:X.VOLUME,prefixes:Q.NONE,value:.764554857984,offset:0},{name:"teaspoon",base:X.VOLUME,prefixes:Q.NONE,value:5e-6,offset:0},{name:"tablespoon",base:X.VOLUME,prefixes:Q.NONE,value:15e-6,offset:0},{name:"minim",base:X.VOLUME,prefixes:Q.NONE,value:6.161152e-8,offset:0},{name:"fluiddram",base:X.VOLUME,prefixes:Q.NONE,value:36966911e-13,offset:0},{name:"fluidounce",base:X.VOLUME,prefixes:Q.NONE,value:2957353e-11,offset:0},{name:"gill",base:X.VOLUME,prefixes:Q.NONE,value:.0001182941,offset:0},{name:"cup",base:X.VOLUME,prefixes:Q.NONE,value:.0002365882,offset:0},{name:"pint",base:X.VOLUME,prefixes:Q.NONE,value:.0004731765,offset:0},{name:"quart",base:X.VOLUME,prefixes:Q.NONE,value:.0009463529,offset:0},{name:"gallon",base:X.VOLUME,prefixes:Q.NONE,value:.003785412,offset:0},{name:"beerbarrel",base:X.VOLUME,prefixes:Q.NONE,value:.1173478,offset:0},{name:"oilbarrel",base:X.VOLUME,prefixes:Q.NONE,value:.1589873,offset:0},{name:"hogshead",base:X.VOLUME,prefixes:Q.NONE,value:.238481,offset:0},{name:"fldr",base:X.VOLUME,prefixes:Q.NONE,value:36966911e-13,offset:0},{name:"floz",base:X.VOLUME,prefixes:Q.NONE,value:2957353e-11,offset:0},{name:"gi",base:X.VOLUME,prefixes:Q.NONE,value:.0001182941,offset:0},{name:"cp",base:X.VOLUME,prefixes:Q.NONE,value:.0002365882,offset:0},{name:"pt",base:X.VOLUME,prefixes:Q.NONE,value:.0004731765,offset:0},{name:"qt",base:X.VOLUME,prefixes:Q.NONE,value:.0009463529,offset:0},{name:"gal",base:X.VOLUME,prefixes:Q.NONE,value:.003785412,offset:0},{name:"bbl",base:X.VOLUME,prefixes:Q.NONE,value:.1173478,offset:0},{name:"obl",base:X.VOLUME,prefixes:Q.NONE,value:.1589873,offset:0},{name:"g",base:X.MASS,prefixes:Q.SHORT,value:.001,offset:0},{name:"gram",base:X.MASS,prefixes:Q.LONG,value:.001,offset:0},{name:"ton",base:X.MASS,prefixes:Q.SHORT,value:907.18474,offset:0},{name:"tonne",base:X.MASS,prefixes:Q.SHORT,value:1e3,offset:0},{name:"grain",base:X.MASS,prefixes:Q.NONE,value:6479891e-11,offset:0},{name:"dram",base:X.MASS,prefixes:Q.NONE,value:.0017718451953125,offset:0},{name:"ounce",base:X.MASS,prefixes:Q.NONE,value:.028349523125,offset:0},{name:"poundmass",base:X.MASS,prefixes:Q.NONE,value:.45359237,offset:0},{name:"hundredweight",base:X.MASS,prefixes:Q.NONE,value:45.359237,offset:0},{name:"stick",base:X.MASS,prefixes:Q.NONE,value:.115,offset:0},{name:"gr",base:X.MASS,prefixes:Q.NONE,value:6479891e-11,offset:0},{name:"dr",base:X.MASS,prefixes:Q.NONE,value:.0017718451953125,offset:0},{name:"oz",base:X.MASS,prefixes:Q.NONE,value:.028349523125,offset:0},{name:"lbm",base:X.MASS,prefixes:Q.NONE,value:.45359237,offset:0},{name:"cwt",base:X.MASS,prefixes:Q.NONE,value:45.359237,offset:0},{name:"s",base:X.TIME,prefixes:Q.SHORT,value:1,offset:0},{name:"min",base:X.TIME,prefixes:Q.NONE,value:60,offset:0},{name:"h",base:X.TIME,prefixes:Q.NONE,value:3600,offset:0},{name:"seconds",base:X.TIME,prefixes:Q.LONG,value:1,offset:0},{name:"second",base:X.TIME,prefixes:Q.LONG,value:1,offset:0},{name:"sec",base:X.TIME,prefixes:Q.LONG,value:1,offset:0},{name:"minutes",base:X.TIME,prefixes:Q.NONE,value:60,offset:0},{name:"minute",base:X.TIME,prefixes:Q.NONE,value:60,offset:0},{name:"hours",base:X.TIME,prefixes:Q.NONE,value:3600,offset:0},{name:"hour",base:X.TIME,prefixes:Q.NONE,value:3600,offset:0},{name:"day",base:X.TIME,prefixes:Q.NONE,value:86400,offset:0},{name:"days",base:X.TIME,prefixes:Q.NONE,value:86400,offset:0},{name:"rad",base:X.ANGLE,prefixes:Q.NONE,value:1,offset:0},{name:"deg",base:X.ANGLE,prefixes:Q.NONE,value:.017453292519943295,offset:0},{name:"grad",base:X.ANGLE,prefixes:Q.NONE,value:.015707963267948967,offset:0},{name:"cycle",base:X.ANGLE,prefixes:Q.NONE,value:6.283185307179586,offset:0},{name:"A",base:X.CURRENT,prefixes:Q.SHORT,value:1,offset:0},{name:"ampere",base:X.CURRENT,prefixes:Q.LONG,value:1,offset:0},{name:"K",base:X.TEMPERATURE,prefixes:Q.NONE,value:1,offset:0},{name:"degC",base:X.TEMPERATURE,prefixes:Q.NONE,value:1,offset:273.15},{name:"degF",base:X.TEMPERATURE,prefixes:Q.NONE,value:1/1.8,offset:459.67},{name:"degR",base:X.TEMPERATURE,prefixes:Q.NONE,value:1/1.8,offset:0},{name:"kelvin",base:X.TEMPERATURE,prefixes:Q.NONE,value:1,offset:0},{name:"celsius",base:X.TEMPERATURE,prefixes:Q.NONE,value:1,offset:273.15},{name:"fahrenheit",base:X.TEMPERATURE,prefixes:Q.NONE,value:1/1.8,offset:459.67},{name:"rankine",base:X.TEMPERATURE,prefixes:Q.NONE,value:1/1.8,offset:0},{name:"mol",base:X.AMOUNT_OF_SUBSTANCE,prefixes:Q.NONE,value:1,offset:0},{name:"mole",base:X.AMOUNT_OF_SUBSTANCE,prefixes:Q.NONE,value:1,offset:0},{name:"cd",base:X.LUMINOUS_INTENSITY,prefixes:Q.NONE,value:1,offset:0},{name:"candela",base:X.LUMINOUS_INTENSITY,prefixes:Q.NONE,value:1,offset:0},{name:"N",base:X.FORCE,prefixes:Q.SHORT,value:1,offset:0},{name:"newton",base:X.FORCE,prefixes:Q.LONG,value:1,offset:0},{name:"lbf",base:X.FORCE,prefixes:Q.NONE,value:4.4482216152605,offset:0},{name:"poundforce",base:X.FORCE,prefixes:Q.NONE,value:4.4482216152605,offset:0},{name:"b",base:X.BIT,prefixes:Q.BINARY_SHORT,value:1,offset:0},{name:"bits",base:X.BIT,prefixes:Q.BINARY_LONG,value:1,offset:0},{name:"B",base:X.BIT,prefixes:Q.BINARY_SHORT,value:8,offset:0},{name:"bytes",base:X.BIT,prefixes:Q.BINARY_LONG,value:8,offset:0}],K.E=Math.E,K.LN2=Math.LN2,K.LN10=Math.LN10,K.LOG2E=Math.LOG2E,K.LOG10E=Math.LOG10E,K.PI=Math.PI,K.SQRT1_2=Math.SQRT1_2,K.SQRT2=Math.SQRT2,K.I=new n(0,1),K.pi=K.PI,K.e=K.E,K.i=K.I,K.expr.node.Node=b,b.prototype.eval=function(){throw new Error("Cannot evaluate a Node interface")
},b.prototype.toString=function(){return""},M.prototype=new b,K.expr.node.ConstantNode=M,M.prototype.eval=function(){return this.value},M.prototype.toString=function(){return K.format(this.value||null)},A.prototype=new b,K.expr.node.OperatorNode=A,A.prototype.eval=function(){return this.fn.apply(this,this.params.map(function(e){return e.eval()}))},A.prototype.toString=function(){var e=this.params;if(this.fn===K.unaryminus)return"-"+e[0].toString();switch(e.length){case 1:return e[0].toString()+this.name;case 2:var n=e[0].toString();e[0]instanceof A&&(n="("+n+")");var t=e[1].toString();return e[1]instanceof A&&(t="("+t+")"),n+" "+this.name+" "+t;default:return this.name+"("+this.params.join(", ")+")"}},S.prototype=new b,K.expr.node.SymbolNode=S,S.prototype.eval=function(){var e=this.symbol.get();if(void 0===e)throw new Error("Undefined symbol "+this.name);return e},S.prototype.toString=function(){return this.name},T.prototype=new b,K.expr.node.ParamsNode=T,T.prototype.eval=function(){var e,n,t=this.object;if(void 0==t)throw new Error("Node undefined");var r=t.eval();if(this.ends){var i=this.ends,a=r.size&&r.size();for(e=0,n=this.params.length;n>e;e++){var o=i[e];o&&a&&o.set(a[e])}}var s=this.params,f=[];for(e=0,n=this.params.length;n>e;e++)f[e]=s[e].eval();if("function"==typeof r)return r.apply(this,f);if(r instanceof Object&&r.get)return r.get(f);throw new TypeError("Cannot apply parameters to object of type "+K["typeof"](r))},T.prototype.toString=function(){var e=this.object?this.object.toString():"";return this.params&&(e+="("+this.params.join(", ")+")"),e},U.prototype=new b,K.expr.node.MatrixNode=U,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,s=null,f=null,u=0;o>u;u++){var c,l=K.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 new 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==s)s=l,f=c[0];else{if(c[0]!=f)throw new Error("Dimension mismatch ("+c[0]+" != "+f+")");for(var h=0;f>h;h++)s[h]=s[h].concat(l[h])}}n=n.concat(s)}return n}function r(e){return e.some(function(e){return e instanceof t||e instanceof w?!0:e instanceof Array?r(e):!1})}U.prototype.eval=function(){var i=e(this.nodes);return r(i)&&(i=n(i)),new t(i)},U.prototype.toString=function(){return Z.formatArray(this.nodes)}}(),L.prototype=new b,K.expr.node.BlockNode=L,L.prototype.add=function(e,n){var t=this.params.length;this.params[t]=e,this.visible[t]=void 0!=n?n:!0},L.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},L.prototype.toString=function(){for(var e=[],n=0,t=this.params.length;t>n;n++)this.visible[n]&&e.push("\n "+this.params[n].toString());return"["+e.join(",")+"\n]"},R.prototype=new b,K.expr.node.AssignmentNode=R,R.prototype.eval=function(){if(void 0===this.expr)throw new Error("Undefined symbol "+this.name);var e,n=this.params;if(n&&n.length){var t=this.symbol.get();if(void 0==t)throw new Error("Undefined symbol "+this.name);if(this.ends)for(var r=this.ends,i=t.size&&t.size(),a=0,o=this.params.length;o>a;a++){var s=r[a];s&&i&&s.set(i[a])}var f=[];this.params.forEach(function(e){f.push(e.eval())});var u=this.expr.eval();if(!t.set)throw new TypeError("Cannot apply a subset to object of type "+K["typeof"](t));e=t.set(f,u),this.symbol.set(e)}else e=this.expr.eval(),this.symbol.set(e);return e},R.prototype.toString=function(){var e="";return e+=this.name,this.params&&this.params.length&&(e+="("+this.params.join(", ")+")"),e+=" = ",e+=this.expr.toString()},z.prototype=new b,K.expr.node.FunctionNode=z,z.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},z.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.symbol.set(this.fn),this.fn},z.prototype.toString=function(){return this.fn.toString()},K.expr.Symbol=function(e,n){this.name=e,this.value=n},K.expr.Symbol.prototype.get=function(){for(var e=this.value;e instanceof K.expr.Symbol;)e=e.get();return e},K.expr.Symbol.prototype.set=function(e){this.value=e},K.expr.Symbol.prototype.valueOf=function(){return this.get()},K.expr.Scope=function(e){this.parentScope=e,this.nestedScopes=void 0,this.symbols={},this.defs={},this.updates={},this.links={}},K.expr.Scope.prototype={createNestedScope:function(){var e=new K.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=new K.expr.Symbol(e,t),this.symbols[e]=n}return n},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];if(t)void 0!=n&&t.set(n);else{t=this.createSymbol(e),this.defs[e]=t,void 0!=n&&t.set(n);var r=this.getUndefinedSymbols(e);r.length&&r.forEach(function(e){e!=t&&e.set(t)})}return t},createUpdate:function(e){var n=this.updates[e];if(!n){n=this.createLink(e),this.updates[e]=n;var t=this.getUndefinedSymbols(e);t.length&&t.forEach(function(e){e!=n&&e.set(n)})}return n},createConstant:function(e,n){var t=new K.expr.Symbol(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=K[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},setUndefined:function(e){var n=this.symbols[e];n&&n.set(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.set(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(e){var n=this.symbols,t=[];for(var r in n)if(n.hasOwnProperty(r)){var i=n[r];void 0!=i.value||e&&i.name!=e||t.push(i)}return this.nestedScopes&&this.nestedScopes.forEach(function(n){t=t.concat(n.getUndefinedSymbols(e))}),t}},function(){function e(){X++,Q=Z.charAt(X)}function t(){X=0,Q=Z.charAt(0)}function r(){for($=W.NULL,J="";" "==Q||" "==Q;)e();if("#"==Q)for(;"\n"!=Q&&""!=Q;)e();if(""==Q)return $=W.DELIMITER,void 0;if("!"==Q)return $=W.DELIMITER,J+=Q,e(),"="==Q&&(J+=Q,e()),void 0;if("-"==Q||","==Q||"("==Q||")"==Q||"["==Q||"]"==Q||'"'==Q||"'"==Q||"\n"==Q||";"==Q||":"==Q)return $=W.DELIMITER,J+=Q,e(),void 0;if(i(Q))for($=W.DELIMITER;i(Q);)J+=Q,e();else if(o(Q)){if($=W.NUMBER,"."==Q)J+=Q,e(),s(Q)||($=W.UNKNOWN);else{for(;s(Q);)J+=Q,e();"."==Q&&(J+=Q,e())}for(;s(Q);)J+=Q,e();if("E"==Q||"e"==Q)for(J+=Q,e(),("+"==Q||"-"==Q)&&(J+=Q,e()),s(Q)||($=W.UNKNOWN);s(Q);)J+=Q,e()}else{if(!a(Q)){for($=W.UNKNOWN;""!=Q;)J+=Q,e();throw j('Syntax error in part "'+J+'"')}for($=W.SYMBOL;a(Q)||s(Q);)J+=Q,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 s(e){return e>="0"&&"9">=e}function f(e){t(),r();var n;if(n=""==J?new M(void 0):u(e),""!=J)throw $==W.DELIMITER?Y("Unknown operator "+J):j('Unexpected part "'+J+'"');return n}function u(e){var n,t,i;for("\n"!=J&&";"!=J&&""!=J&&(n=c(e));"\n"==J||";"==J;)t||(t=new L,n&&(i=";"!=J,t.add(n,i))),r(),"\n"!=J&&";"!=J&&""!=J&&(n=c(e),i=";"!=J,t.add(n,i));return t?t:(n||(n=c(e)),n)}function c(e){var n=l(e),t="ans",r=void 0,i=e.createDef(t);return new R(t,r,null,n,i)}function l(e){if($==W.SYMBOL&&"function"==J){if(r(),$!=W.SYMBOL)throw j("Function name expected");var n=J;if(r(),"("!=J)throw j("Opening parenthesis ( expected");for(var t=e.createNestedScope(),i=[],a=[];;){if(r(),$!=W.SYMBOL)throw j("Variable name expected");var o=J,s=t.createDef(o);if(i.push(o),a.push(s),r(),","!=J){if(")"==J)break;throw j('Comma , or closing parenthesis ) expected"')}}if(r(),"="!=J)throw j("Equal sign = expected");r();var f=h(t),u=e.createDef(n);return new z(n,i,a,f,u)}return h(e)}function h(e){var n,t,i,a,o,s=p(e);if("="==J){if(s instanceof S)return r(),n=s.name,t=null,a=h(e),o=e.createDef(n),new R(n,t,null,a,o);if(s instanceof T&&s.object instanceof S)return r(),n=s.object.name,t=s.params,i=s.scopes,a=h(e),o=e.createUpdate(n),new R(n,t,i,a,o);throw j("Symbol expected at the left hand side of assignment operator =")}return s}function p(e){var n,t,i,a=[];if(n=":"==J?new M(1):m(e),":"==J){for(a.push(n);":"==J;)if(r(),")"==J||","==J||""==J){var o=e.createLink("end");a.push(new S("end",o))}else a.push(m(e));a.length&&(t="range",i=K.range,n=new A(t,i,a))}return n}function m(e){var n,t,i,a,o;for(n=v(e),t={"in":"in"};void 0!==t[J];)i=J,a=K[t[i]],r(),o=[n,v(e)],n=new A(i,a,o);return n}function v(e){var n=g(e);return n}function g(e){var n,t,i,a,o;for(n=w(e),t={"==":"equal","!=":"unequal","<":"smaller",">":"larger","<=":"smallereq",">=":"largereq"};void 0!==t[J];)i=J,a=K[t[i]],r(),o=[n,w(e)],n=new A(i,a,o);return n}function w(e){var n,t,i,a,o;for(n=d(e),t={"+":"add","-":"subtract"};void 0!==t[J];)i=J,a=K[t[i]],r(),o=[n,d(e)],n=new A(i,a,o);return n}function d(e){var n,t,i,a,o;for(n=N(e),t={"*":"multiply","/":"divide","%":"mod",mod:"mod"};void 0!==t[J];)i=J,a=K[t[i]],r(),o=[n,N(e)],n=new A(i,a,o);return n}function N(e){var n,t,i;return"-"==J?(n=J,t=K.unaryminus,r(),i=[O(e)],new A(n,t,i)):O(e)}function O(e){var n,t,i,a,o,s;for(i=[x(e)];"^"==J;)r(),i.push(x(e));for(n=i.pop();i.length;)t=i.pop(),a="^",o=K.pow,s=[t,n],n=new A(a,o,s);return n}function x(e){var n,t,i,a;for(n=b(e);"!"==J;)t=J,i=K.factorial,r(),a=[n],n=new A(t,i,a);return n}function b(e){var n,t,i,a;for(n=_(e);"'"==J;)t=J,i=K.transpose,r(),a=[n],n=new A(t,i,a);return n}function _(e){return q(e)}function q(e){var n,t,i;return $==W.SYMBOL?(t=J,r(),i=e.createLink(t),n=new S(t,i),I(e,n)):P(e)}function I(e,n){for(var t,i,a;"("==J;){if(t=[],i=[],r(),")"!=J)for(a=e.createNestedScope(),i.push(a),t.push(p(a));","==J;)r(),a=e.createNestedScope(),i.push(a),t.push(p(a));if(")"!=J)throw j("Parenthesis ) missing");r(),n=new T(n,t,i)}return n}function P(n){var t,i,a;if('"'==J){for(i="",a="";""!=Q&&('"'!=Q||"\\"==a);)i+=Q,a=Q,e();if(r(),'"'!=J)throw j('End of string " missing');return r(),t=new M(i),t=I(n,t)}return B(n)}function B(e){var n,t,i,a,o,s;if("["==J){for(r();"\n"==J;)r();if("]"!=J){for(t=[],i=0,a=0,t[0]=[h(e)];","==J||";"==J;){for(","==J?a++:(i++,a=0,t[i]=[]),r();"\n"==J;)r();for(t[i][a]=h(e);"\n"==J;)r()}for(o=t.length,s=t.length>0?t[0].length:0,i=1;o>i;i++)if(t[i].length!=s)throw Y("Number of columns must match ("+t[i].length+" != "+s+")");if("]"!=J)throw j("End of matrix ] missing");r(),n=new U(t)}else r(),n=new U([[]]);return n=I(e,n)}return C(e)}function C(e){var t,i,a;if($==W.NUMBER){if(a="."==J?0:Number(J),r(),$==W.SYMBOL){if("i"==J||"I"==J)return i=new n(0,a),r(),new M(i);if(E.isPlainUnit(J))return i=new E(a,J),r(),new M(i);throw H('Unknown unit "'+J+'"')}return t=new M(a),t=I(e,t)}return G(e)}function G(e){var n;if("("==J){if(r(),n=h(e),")"!=J)throw j("Parenthesis ) expected");return r(),n=I(e,n)}return D(e)}function D(){throw""==J?j("Unexpected end of expression"):j("Value expected")}function F(){return void 0}function k(){return X-J.length+1}function V(e){var n=F(),t=k();return void 0===n?void 0===t?e:e+" (char "+t+")":e+" (line "+n+", char "+t+")"}function j(e){return new SyntaxError(V(e))}function H(e){return new TypeError(V(e))}function Y(e){return new Error(V(e))}K.expr.Parser=function(){if(!(this instanceof K.expr.Parser))throw new SyntaxError("Parser constructor must be called with the new operator");this.scope=new K.expr.Scope(null)},K.expr.Parser.prototype.parse=function(e,n){if(!y(e))throw new TypeError("String expected");return Z=e||"",n||(n=this.scope),f(n)},K.expr.Parser.prototype.eval=function(e){var n=this.parse(e);return n.eval()},K.expr.Parser.prototype.get=function(e){var n=this.scope.findDef(e);return n?n.get():void 0},K.expr.Parser.prototype.set=function(e,n){this.scope.createDef(e,n)},K.expr.Parser.prototype.remove=function(e){this.scope.setUndefined(e)},K.expr.Parser.prototype.clear=function(){this.scope.clear()};var W={NULL:0,DELIMITER:1,NUMBER:2,SYMBOL:3,UNKNOWN:4},Z="",X=0,Q="",J="",$=W.NULL}(),function(){function e(){this.idMax=-1,this.updateSeq=0,this.parser=new K.expr.Parser,this.scope=new K.expr.Scope,this.nodes={},this.firstNode=void 0,this.lastNode=void 0}K.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 K.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,s=new K.expr.Scope(o),f=new e.Node({id:a,expression:n,parser:this.parser,scope:s,nextNode:r,previousNode:i});this.nodes[a]=f,i?i.nextNode=f:this.firstNode=f,r.previousNode=f,r.scope.parentScope=f.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';var r=t.nextNode;return r?this.insertBefore(e,r.id):this.append(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: "+String(e.message||e);this.fn=new M(n)}},e.Node.prototype.eval=function(){try{this.scope.init(),this.result=this.fn.eval()}catch(e){this.scope.init(),this.result="Error: "+String(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,s=[];for(t in a)a.hasOwnProperty(t)&&s.push(t);for(t in o)o.hasOwnProperty(t)&&-1==s.indexOf(t)&&s.push(t);for(var f=i.nextNode;f&&s.length;){for(var u=f.scope,c=0;c<s.length;){if(t=s[c],(u.hasLink(t)||u.hasUpdate(t))&&-1==r.indexOf(f.id)){r.push(f.id);var l=this.getDependencies(f.id);e._merge(r,l)}u.hasDef(t)&&(s.splice(c,1),c--),c++}f=f.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: "+String(r.message||r)}e.push(t),n=n.nextNode}return e}}(),K.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)return Z.map(e,K.abs);if(e.valueOf()!==e)return K.abs(e.valueOf());throw O("abs",e)},K.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 new Error("Units do not match");if(null==e.value)throw new Error("Unit on left hand side of operator + has an undefined value");if(null==r.value)throw new Error("Unit on right hand side of operator + has an undefined 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||r instanceof Array||r instanceof t)return Z.map2(e,r,K.add);if(e.valueOf()!==e||r.valueOf()!==r)return K.add(e.valueOf(),r.valueOf());throw O("add",e,r)},K.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)return Z.map(e,K.ceil);if(e.valueOf()!==e)return K.ceil(e.valueOf());throw O("ceil",e)},K.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 K.multiply(K.multiply(e,e),e);if(e instanceof Array||e instanceof t)return Z.map(e,K.cube);if(e.valueOf()!==e)return K.cube(e.valueOf());throw O("cube",e)},K.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 _(new n(e,0),r)}if(e instanceof n){if(v(r))return _(e,new n(r,0));if(r instanceof n)return _(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?K.multiply(e,K.inv(r)):Z.map2(e,r,K.divide);if(r instanceof Array||r instanceof t)return K.multiply(e,K.inv(r));if(e.valueOf()!==e||r.valueOf()!==r)return K.divide(e.valueOf(),r.valueOf());throw O("divide",e,r)},K.equal=function en(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 new 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||r instanceof Array||r instanceof t)return Z.map2(e,r,K.equal);if(e.valueOf()!==e||r.valueOf()!==r)return en(e.valueOf(),r.valueOf());throw O("equal",e,r)},K.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)return Z.map(e,K.exp);if(e.valueOf()!==e)return K.exp(e.valueOf());throw O("exp",e)},K.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)return Z.map(e,K.fix);if(e.valueOf()!==e)return K.fix(e.valueOf());throw O("fix",e)},K.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)return Z.map(e,K.floor);if(e.valueOf()!==e)return K.floor(e.valueOf());throw O("floor",e)},K.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 new 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 Z.map2(n,r,K.gcd);if(n.valueOf()!==n||r.valueOf()!==r)return K.gcd(n.valueOf(),r.valueOf());throw O("gcd",n,r)}if(arguments.length>2){for(var i=1;i<arguments.length;i++)n=K.gcd(n,arguments[i]);return n}throw new SyntaxError("Function gcd expects two or more arguments")},K.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>K.abs(r)}if(e instanceof n){if(v(r))return K.abs(e)>r;if(r instanceof n)return K.abs(e)>K.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw new 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||r instanceof Array||r instanceof t)return Z.map2(e,r,K.larger);if(e.valueOf()!==e||r.valueOf()!==r)return K.larger(e.valueOf(),r.valueOf());throw O("larger",e,r)},K.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>=K.abs(r)}if(e instanceof n){if(v(r))return K.abs(e)>=r;if(r instanceof n)return K.abs(e)>=K.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw new 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||r instanceof Array||r instanceof t)return Z.map2(e,r,K.largereq);if(e.valueOf()!==e||r.valueOf()!==r)return K.largereq(e.valueOf(),r.valueOf());throw O("largereq",e,r)},K.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 new 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 Z.map2(n,r,K.lcm);if(n.valueOf()!==n||r.valueOf()!==r)return K.lcm(n.valueOf(),r.valueOf());throw O("lcm",n,r)}if(arguments.length>2){for(var a=1;a<arguments.length;a++)n=K.lcm(n,arguments[a]);return n}throw new SyntaxError("Function lcm expects two or more arguments")},K.log=function(e,r){if(1==arguments.length){if(v(e))return e>=0?Math.log(e):K.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)return Z.map(e,K.log);if(e.valueOf()!==e)return K.log(e.valueOf());throw O("log",e)}if(2==arguments.length)return K.divide(K.log(e),K.log(r));throw x("log",arguments.length,1,2)},K.log10=function(e){if(1!=arguments.length)throw x("log10",arguments.length,1);if(v(e))return e>=0?Math.log(e)/Math.LN10:K.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)return Z.map(e,K.log10);if(e.valueOf()!==e)return K.log10(e.valueOf());throw O("log10",e)},K.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||r instanceof Array||r instanceof t)return Z.map2(e,r,K.mod);if(e.valueOf()!==e||r.valueOf()!==r)return K.mod(e.valueOf(),r.valueOf());throw O("mod",e,r)},K.multiply=function nn(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 q(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 q(e,new n(r,0));if(r instanceof n)return q(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=Z.size(e),a=Z.size(r);if(2!=i.length)throw new Error("Can only multiply a 2 dimensional matrix (A has "+i.length+" dimensions)");if(2!=a.length)throw new 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=[],s=i[0],f=a[1],u=i[1],nn=K.multiply,c=K.add,l=0;s>l;l++){o[l]=[];for(var h=0;f>h;h++){for(var p=null,m=0;u>m;m++){var g=nn(e[l][m],r[m][h]);p=null==p?g:c(p,g)}o[l][h]=p}}return o}return r instanceof t?new t(K.multiply(e.valueOf(),r.valueOf())):Z.map2(e,r,K.multiply)}if(e instanceof t)return new t(K.multiply(e.valueOf(),r.valueOf()))}if(r instanceof Array)return Z.map2(e,r,K.multiply);if(r instanceof t)return new t(K.multiply(e.valueOf(),r.valueOf()));if(e.valueOf()!==e||r.valueOf()!==r)return K.multiply(e.valueOf(),r.valueOf());throw O("multiply",e,r)},K.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):I(new n(e,0),new n(r,0));if(r instanceof n)return I(new n(e,0),r)}else if(e instanceof n){if(v(r))return I(e,new n(r,0));if(r instanceof n)return I(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=Z.size(e);if(2!=i.length)throw new Error("For A^b, A must be 2 dimensional (A has "+i.length+" dimensions)");if(i[0]!=i[1])throw new Error("For A^b, A must be square (size is "+i[0]+"x"+i[1]+")");if(0==r)return K.eye(i[0]);for(var a=e,o=1;r>o;o++)a=K.multiply(e,a);return a}if(e instanceof t)return new t(K.pow(e.valueOf(),r))}if(e.valueOf()!==e||r.valueOf()!==r)return K.pow(e.valueOf(),r.valueOf());throw O("pow",e,r)},K.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)return Z.map(e,K.round);if(e.valueOf()!==e)return K.round(e.valueOf());throw O("round",e)}if(!v(r))throw new TypeError("Number of decimals in function round must be an integer");if(r!==Math.round(r))throw new TypeError("Number of decimals in function round must be integer");if(0>r||r>9)throw new Error("Number of decimals in function round must be in te range of 0-9");if(v(e))return P(e,r);if(e instanceof n)return new n(P(e.re,r),P(e.im,r));if(e instanceof Array||e instanceof t||r instanceof Array||r instanceof t)return Z.map2(e,r,K.round);if(e.valueOf()!==e||r.valueOf()!==r)return K.round(e.valueOf(),r.valueOf());throw O("round",e,r)},K.sign=function tn(e){if(1!=arguments.length)throw x("sign",arguments.length,1);if(v(e)){var tn;return tn=e>0?1:0>e?-1:0}if(e instanceof n){var r=Math.sqrt(e.re*e.re+e.im*e.im);return new n(e.re/r,e.im/r)}if(e instanceof Array||e instanceof t)return Z.map(e,K.sign);if(e.valueOf()!==e)return K.sign(e.valueOf());throw O("sign",e)},K.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 e<K.abs(r)}if(e instanceof n){if(v(r))return K.abs(e)<r;if(r instanceof n)return K.abs(e)<K.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw new 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||r instanceof Array||r instanceof t)return Z.map2(e,r,K.smaller);if(e.valueOf()!==e||r.valueOf()!==r)return K.smaller(e.valueOf(),r.valueOf());throw O("smaller",e,r)},K.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 e<=K.abs(r)}if(e instanceof n){if(v(r))return K.abs(e)<=r;if(r instanceof n)return K.abs(e)<=K.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw new 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||r instanceof Array||r instanceof t)return Z.map2(e,r,K.smallereq);if(e.valueOf()!==e||r.valueOf()!==r)return K.smallereq(e.valueOf(),r.valueOf());throw O("smallereq",e,r)},K.sqrt=function(e){if(1!=arguments.length)throw x("sqrt",arguments.length,1);if(v(e))return e>=0?Math.sqrt(e):K.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)return Z.map(e,K.sqrt);if(e.valueOf()!==e)return K.sqrt(e.valueOf());throw O("sqrt",e)},K.square=function(e){if(1!=arguments.length)throw x("square",arguments.length,1);if(v(e))return e*e;if(e instanceof n)return K.multiply(e,e);if(e instanceof Array||e instanceof t)return Z.map(e,K.square);if(e.valueOf()!==e)return K.square(e.valueOf());throw O("square",e)},K.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 new Error("Units do not match");if(null==e.value)throw new Error("Unit on left hand side of operator - has an undefined value");if(null==r.value)throw new Error("Unit on right hand side of operator - has an undefined value");var i=e.clone();return i.value-=r.value,i.fixPrefix=!1,i}if(e instanceof Array||e instanceof t||r instanceof Array||r instanceof t)return Z.map2(e,r,K.subtract);if(e.valueOf()!==e||r.valueOf()!==r)return K.subtract(e.valueOf(),r.valueOf());throw O("subtract",e,r)},K.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)return Z.map(e,K.unaryminus);if(e.valueOf()!==e)return K.unaryminus(e.valueOf());throw O("unaryminus",e)},K.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 new 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||r instanceof Array||r instanceof t)return Z.map2(e,r,K.unequal);if(e.valueOf()!==e||r.valueOf()!==r)return K.unequal(e.valueOf(),r.valueOf());throw O("unequal",e,r)},K.xgcd=function rn(e,n){if(2==arguments.length){if(v(e)&&v(n)){if(!g(e)||!g(n))throw new Error("Parameters in function xgcd must be integer numbers");if(0==n)return[e,1,0];
var t=rn(n,e%n),r=t[0],i=t[1],a=t[2];return[r,a,i-a*Math.floor(e/n)]}throw O("xgcd",e,n)}throw new SyntaxError("Function xgcd expects two arguments")},K.arg=function(e){if(1!=arguments.length)throw x("arg",arguments.length,1);return v(e)?Math.atan2(0,e):e instanceof n?Math.atan2(e.im,e.re):e instanceof Array||e instanceof t?Z.map(e,K.arg):e.valueOf()!==e?K.arg(e.valueOf()):K.atan2(0,e)},K.conj=function(e){if(1!=arguments.length)throw x("conj",arguments.length,1);return v(e)?e:e instanceof n?new n(e.re,-e.im):e instanceof Array||e instanceof t?Z.map(e,K.conj):e.valueOf()!==e?K.conj(e.valueOf()):clone(e)},K.im=function(e){if(1!=arguments.length)throw x("im",arguments.length,1);return v(e)?0:e instanceof n?e.im:e instanceof Array||e instanceof t?Z.map(e,K.im):e.valueOf()!==e?K.im(e.valueOf()):0},K.re=function(e){if(1!=arguments.length)throw x("re",arguments.length,1);return v(e)?e:e instanceof n?e.re:e instanceof Array||e instanceof t?Z.map(e,K.re):e.valueOf()!==e?K.re(e.valueOf()):K.clone(e)},K.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)}},K.matrix=function(e){if(arguments.length>1)throw x("matrix",arguments.length,0,1);return new t(e)},K.parser=function(){return new K.expr.Parser},K.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)}},K.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)}},K.workspace=function(){return new K.expr.Workspace},K.concat=function(){var e,n,r=arguments.length,i=-1,a=!1,o=[];for(e=0;r>e;e++){var s=arguments[e];if(s instanceof t&&(a=!0),e==r-1&&v(s)){if(n=i,i=s,!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(!(s instanceof Array||s instanceof t))throw O("concat",s);var f=K.clone(s.valueOf()),u=K.size(s);if(o[e]=f,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=B(c,o.shift(),i-1,0);return a?new t(c):c},K.det=function(e){if(1!=arguments.length)throw x("det",arguments.length,1);var n=K.size(e);switch(n.length){case 0:return K.clone(e);case 1:if(1==n[0])return K.clone(e.valueOf()[0]);throw new RangeError("Matrix must be square (size: "+K.format(n)+")");case 2:var t=n[0],r=n[1];if(t==r)return C(e.valueOf(),t,r);throw new RangeError("Matrix must be square (size: "+K.format(n)+")");default:throw new RangeError("Matrix must be two dimensional (size: "+K.format(n)+")")}},K.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 s=n>0?n:0,f=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+f,i.length+s]),r=c.valueOf(),o=i.length,a=0;o>a;a++)r[a+f][a+s]=K.clone(i[a]);return c;case 2:for(i=[],r=e.valueOf(),o=Math.min(u[0]-f,u[1]-s),a=0;o>a;a++)i[a]=K.clone(r[a+f][a+s]);return new t(i);default:throw new RangeError("Matrix for function diag must be 2 dimensional")}},K.eye=function(){var e=Z.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 new Error("Parameters in function eye must be positive integers");if(r&&(!v(r)||!g(r)||1>r))throw new Error("Parameters in function eye must be positive integers");var i=new t;i.resize(e);for(var a=K.min(e),o=i.valueOf(),s=0;a>s;s++)o[s][s]=1;return i},K.inv=function(e){if(1!=arguments.length)throw x("inv",arguments.length,1);var n=K.size(e);switch(n.length){case 0:return K.divide(1,e);case 1:if(1==n[0])return e instanceof t?new t([K.divide(1,e.valueOf()[0])]):[K.divide(1,e[0])];throw new RangeError("Matrix must be square (size: "+K.format(n)+")");case 2:var r=n[0],i=n[1];if(r==i)return e instanceof t?new t(D(e.valueOf(),r,i)):D(e,r,i);throw new RangeError("Matrix must be square (size: "+K.format(n)+")");default:throw new RangeError("Matrix must be two dimensional (size: "+K.format(n)+")")}},K.ones=function(){var e=Z.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},K.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 Z.size(e);if(e instanceof t)return e.size();if(e.valueOf()!==e)return K.size(e.valueOf());throw O("size",e)},K.squeeze=function(e){if(1!=arguments.length)throw x("squeeze",arguments.length,1);return e instanceof Array?F(K.clone(e)):e instanceof t?F(e.toArray()):e.valueOf()instanceof Array?F(K.clone(e.valueOf())):K.clone(e)},K.transpose=function(e){if(1!=arguments.length)throw x("transpose",arguments.length,1);var n=K.size(e);switch(n.length){case 0:return K.clone(e);case 1:return K.clone(e);case 2:for(var r,i=n[1],a=n[0],o=e instanceof t,s=e.valueOf(),f=[],u=K.clone,c=0;i>c;c++){r=f[c]=[];for(var l=0;a>l;l++)r[l]=u(s[l][c])}return 0==a&&(f[0]=[]),o?new t(f):f;default:throw new RangeError("Matrix must be two dimensional (size: "+K.format(n)+")")}},K.zeros=function(){var e=Z.argsToArray(arguments);0==e.length?e=[1,1]:1==e.length&&(e[1]=e[0]);var n=new t;return n.resize(e),n},K.factorial=function(e){if(1!=arguments.length)throw x("factorial",arguments.length,1);if(v(e)){if(!g(e)||0>e)throw new TypeError("Positive integer value expected in function factorial");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)return Z.map(e,K.factorial);if(e.valueOf()!==e)return K.factorial(e.valueOf());throw O("factorial",e)},K.random=function(){if(0!=arguments.length)throw x("random",arguments.length,0);return Math.random()},K.max=function(e){if(0==arguments.length)throw new 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=K.size(e);if(1==n.length){if(0==e.length)throw new Error("Cannot calculate max of an empty vector");return k(e.valueOf())}if(2==n.length){if(0==n[0]||0==n[1])throw new Error("Cannot calculate max of an empty matrix");if(e instanceof Array)return V(e,n[0],n[1]);if(e instanceof t||e instanceof w)return new t(V(e.valueOf(),n[0],n[1]));throw O("max",e)}throw new RangeError("Cannot calculate max for multi dimensional matrix")}return k(arguments)},K.min=function(e){if(0==arguments.length)throw new 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=K.size(e);if(1==n.length){if(0==e.length)throw new Error("Cannot calculate min of an empty vector");return j(e.valueOf())}if(2==n.length){if(0==n[0]||0==n[1])throw new Error("Cannot calculate min of an empty matrix");if(e instanceof Array)return H(e,n[0],n[1]);if(e instanceof t||e instanceof w)return new t(H(e.valueOf(),n[0],n[1]));throw O("min",e)}throw new RangeError("Cannot calculate min for multi dimensional matrix")}return j(arguments)},K.acos=function(e){if(1!=arguments.length)throw x("acos",arguments.length,1);if(v(e))return e>=-1&&1>=e?Math.acos(e):K.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=K.sqrt(r),a=new n(i.re-e.im,i.im+e.re),o=K.log(a);return new n(1.5707963267948966-o.im,o.re)}if(e instanceof Array||e instanceof t)return Z.map(e,K.acos);if(e.valueOf()!==e)return K.acos(e.valueOf());throw O("acos",e)},K.asin=function(e){if(1!=arguments.length)throw x("asin",arguments.length,1);if(v(e))return e>=-1&&1>=e?Math.asin(e):K.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=K.sqrt(a),s=new n(o.re-i,o.im+r),f=K.log(s);return new n(f.im,-f.re)}if(e instanceof Array||e instanceof t)return Z.map(e,K.asin);if(e.valueOf()!==e)return K.asin(e.valueOf());throw O("asin",e)},K.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),s=K.log(o);return new n(-.5*s.im,.5*s.re)}if(e instanceof Array||e instanceof t)return Z.map(e,K.atan);if(e.valueOf()!==e)return K.atan(e.valueOf());throw O("atan",e)},K.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)}else if(e instanceof n&&v(r))return Math.atan2(e.re,r);if(e instanceof Array||e instanceof t||r instanceof Array||r instanceof t)return Z.map2(e,r,K.atan2);if(r.valueOf()!==r||e.valueOf()!==e)return K.atan2(e.valueOf(),r.valueOf());throw O("atan2",e,r)},K.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)return Z.map(e,K.cos);if(e.valueOf()!==e)return K.cos(e.valueOf());throw O("cos",e)},K.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)return Z.map(e,K.cot);if(e.valueOf()!==e)return K.cot(e.valueOf());throw O("cot",e)},K.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)return Z.map(e,K.csc);if(e.valueOf()!==e)return K.csc(e.valueOf());throw O("csc",e)},K.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)return Z.map(e,K.sec);if(e.valueOf()!==e)return K.sec(e.valueOf());throw O("sec",e)},K.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)return Z.map(e,K.sin);if(e.valueOf()!==e)return K.sin(e.valueOf());throw O("sin",e)},K.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)return Z.map(e,K.tan);if(e.valueOf()!==e)return K.tan(e.valueOf());throw O("tan",e)},K["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||n instanceof Array||n instanceof t)return Z.map2(e,n,K["in"]);if(e.valueOf()!==e||n.valueOf()!==n)return K["in"](e.valueOf(),n.valueOf());throw O("in",e,n)},K.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=K.clone;return n.map(function(e){return t(e)})}if(n instanceof Object)return Z.mapObject(n,K.clone);throw O("clone",n)},K.eval=function(e){var n,t;switch(arguments.length){case 0:throw new Error("Function eval requires one or more parameters (0 provided)");case 1:return n=new K.expr.Parser,J(n,e);default:n=new K.expr.Parser,t=[];for(var r=0,i=arguments.length;i>r;r++)t[r]=J(n,arguments[r]);return t}};var J=function(e,n){if(y(n))return e.eval(n);if(n instanceof Array||n instanceof t)return Z.map(n,e.eval.bind(e));throw new TypeError("String or matrix expected")};K.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)?Z.formatNumber(r):r instanceof Array?Z.formatArray(r):y(r)?'"'+r+'"':r instanceof Object?r.toString():String(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})},K["import"]=function(e,n){var t,r={override:!1};if(n&&n instanceof Object&&Z.extend(r,n),y(e)){if("undefined"==typeof require)throw new Error("Cannot load file: require not available.");var i=require(e);K["import"](i)}else if(W(e)){if(t=e.name,!t)throw new Error("Cannot import an unnamed function or object");(r.override||void 0===K[t])&&Y(t,e)}else if(e instanceof Object)for(t in e)if(e.hasOwnProperty(t)){var a=e[t];W(a)?(r.override||void 0===K[t])&&Y(t,a):K["import"](a)}},K.select=function(e){return new K.type.Selector(e)},K["typeof"]=function(e){if(1!=arguments.length)throw x("typeof",arguments.length,1);var n,t=typeof e;if("object"==t){if(null==e)return"null";if(e instanceof Boolean)return"boolean";if(e instanceof Number)return"number";if(e instanceof String)return"string";if(e instanceof Array)return"array";if(e instanceof Date)return"date";if(e.constructor){for(n in K)if(K.hasOwnProperty(n)&&e.constructor==K[n])return n.toLowerCase();for(n in K.type)if(K.type.hasOwnProperty(n)&&e.constructor==K.type[n])return n.toLowerCase();if(e.constructor.name)return e.constructor.name.toLowerCase()}}return t},Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var n=0;n<this.length;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=new Array(o),i=0;o>i;){var s,f;i in a&&(s=a[i],f=e.call(t,s,i,a),r[i]=f),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}),Function.prototype.bind||(Function.prototype.bind=function(e){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var n=Array.prototype.slice.call(arguments,1),t=this,r=function(){},i=function(){return t.apply(this instanceof r&&e?this:e,n.concat(Array.prototype.slice.call(arguments)))};return r.prototype=this.prototype,i.prototype=new r,i});for(var $ in K)K.hasOwnProperty($)&&$&&d($,K[$])}();