forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.min.js
29 lines (29 loc) · 88 KB
/
math.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 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.9.0
* @date 2013-06-16
*
* @license
* Copyright (C) 2013 Jos de Jong <[email protected]>
*
* 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&&!p(e)||null!=t&&!p(t))throw new TypeError("Two numbers 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 ("+rn["typeof"](e)+")");this._data=[]}this._size=an.size(this._data)}function r(e,n){return an.validateIndex(n,e.length),e[n-1]}function i(e,n){return n.forEach(function(n){e=r(e,n)}),rn.clone(e)}function a(e,n){var t=n[0];return t.map?t.map(function(n){return r(e,n)}):[r(e,t)]}function o(e,n){var t=n[0],i=n[1];if(t.map)return i.map?t.map(function(n){var t=r(e,n);return i.map(function(e){return r(t,e)})}):t.map(function(n){return[r(r(e,n),i)]});if(i.map){var a=r(e,t);return[i.map(function(e){return r(a,e)})]}return[[r(r(e,t),i)]]}function s(e,n,t){var i=t==n.length-1,a=n[t],o=function(a){var o=r(e,a);return i?o:s(o,n,t+1)};return a.map?a.map(o):[o(a)]}function f(e,n,t){if(an.validateIndex(n),t instanceof Array)throw new TypeError("Dimension mismatch, value expected instead of array");e[n-1]=t}function u(e,n,t,r){var i=!1;t.length>n.length&&(i=!0);for(var a=0;a<t.length;a++){var o=t[a];an.validateIndex(o),(null==n[a]||o>n[a])&&(n[a]=o,i=!0)}i&&an.resize(e,n,0);var s=n.length;t.forEach(function(n,t){s-1>t?e=e[n-1]:e[n-1]=r})}function c(e,n,t,r){var i=t[0];an.validateIndex(i),i>n[0]&&(an.resize(e,[i],0),n[0]=i),e[i-1]=r}function l(e,n,t,r){var i=t[0],a=t[1];an.validateIndex(i),an.validateIndex(a);var o=!1;i>(n[0]||0)&&(n[0]=i,o=!0),a>(n[1]||0)&&(n[1]=a,o=!0),o&&an.resize(e,n,0),e[i-1][a-1]=r}function h(e,n,t,r,i){var a=r==t.length-1,o=t[r],s=function(o,s){if(a)f(e,o,i[s]),o>(n[r]||0)&&(n[r]=o);else{var u=e[o-1];u instanceof Array||(e[o-1]=u=[u]),o>(n[r]||0)&&(n[r]=o),h(u,n,t,r+1,i[s])}};if(o.map){var u=o.size&&o.size()||o.length;if(u!=i.length)throw new RangeError("Dimensions mismatch ("+u+" != "+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 p(e){return e instanceof Number||"number"==typeof e}function v(e){return e==Math.round(e)}function g(e){return"[object Object]"===Object.prototype.toString.call(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&&!p(e))throw new TypeError("Parameter start must be a number");if(null!=t&&!p(t))throw new TypeError("Parameter end must be a number");if(null!=n&&!p(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=rn.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&&!p(e))throw new TypeError("First parameter in Unit constructor must be a number");if(null!=n&&!y(n)&&!g(n))throw new TypeError("Second parameter in Unit constructor must be a string");if(y(n)){var t=x(n);if(!t)throw new SyntaxError('String "'+n+'" is no unit');this.unit=t.unit,this.prefix=t.prefix}else if(null==n)this.unit=E.UNIT_NONE,this.prefix=E.PREFIX_NONE;else if(g(n)){this.value=e;var r=E._findBaseQuantity(n);this.unit=E._findUnitFromBase(r),this.prefix=E.PREFIX_NONE}null==e||g(n)?null!==e||g(n)||(this.value=null,this.fixPrefix=!0):(this.value=this._normalize(e),this.fixPrefix=!1)}function x(e){for(var n=E.UNITS,t=0,r=n.length;r>t;t++){var i=n[t];if(an.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 N(e,n){var t=void 0;if(2==arguments.length){var r=rn["typeof"](n);t="Function "+e+"("+r+") not supported"}else if(arguments.length>2){for(var i=[],a=1;a<arguments.length;a++)i.push(rn["typeof"](arguments[a]));t="Function "+e+"("+i.join(", ")+") not supported"}else t="Unsupported parameter in function "+e;return new TypeError(t)}function O(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.scope=n}function T(e,n,t){if(this.object=e,this.params=n,this.paramScopes=t,this.hasContextParams=!1,n)for(var r={type:rn.type.SymbolNode,properties:{name:"end"}},i=0,a=n.length;a>i;i++)if(n[i].find(r).length>0){this.hasContextParams=!0;break}}function R(e){this.nodes=e||[]}function U(){this.params=[],this.visible=[]}function z(e,n,t){this.name=e,this.expr=n,this.scope=t}function L(e,n,t,r,i){this.name=e,this.params=n,this.paramScopes=t,this.expr=r,this.scope=i,this.hasContextParams=!1;for(var a={type:rn.type.SymbolNode,properties:{name:"end"}},o=0,s=n.length;s>o;o++)if(n[o].find(a).length>0){this.hasContextParams=!0;break}}function _(e,n,t,r,i){this.name=e,this.variables=n,this.scope=i,this.fn=function(){var i=n?n.length:0;if(arguments.length!=i)throw O(e,arguments.length,i);for(var a=0;i>a;a++)r.set(n[a],arguments[a]);return t.eval()},this.fn.toString=function(){return e+"("+n.join(", ")+")"}}function I(){this.idMax=-1,this.updateSeq=0,this.scope=new rn.expr.Scope,this.nodes={},this.firstNode=void 0,this.lastNode=void 0}function q(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 P(e,n){var t=e.value/n.value,r=rn.clone(e.unit.base.dimensions);for(var i in n.unit.base.dimensions)void 0===r[i]?r[i]=-n.unit.base.dimensions[i]:r[i]-=n.unit.base.dimensions[i];return new E(t,r)}function C(e,n){if(n>0)return e>0?e%n:0==e?0:e-n*Math.floor(e/n);if(0==n)return e;throw new Error("Cannot calculate mod for a negative divisor")}function B(e,t){return new n(e.re*t.re-e.im*t.im,e.re*t.im+e.im*t.re)}function G(e,n){var t=e.value*n.value,r=rn.clone(e.unit.base.dimensions);for(var i in n.unit.base.dimensions)void 0===r[i]?r[i]=n.unit.base.dimensions[i]:r[i]+=n.unit.base.dimensions[i];return new E(t,r)}function F(e,n){var t=rn.log(e),r=rn.multiply(t,n);return rn.exp(r)}function H(e,n){if(n){var t=Math.pow(10,n);return Math.round(e*t)/t}return Math.round(e)}function j(e){if(e instanceof Array||e instanceof t||e instanceof w){for(var n=e.valueOf(),r="[",i=n.length,a=0;i>a;a++)0!=a&&(r+=", "),r+=j(n[a]);return r+="]"}return p(e)?an.formatNumber(e):e.toString()}function D(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]=D(e[a],n[a],t,r+1);return i}return e.concat(n)}function V(e,n,t){var r=rn.multiply,i=rn.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=1,o=0,s=0;n>s&&!(o>=t);s++){for(var f=s;0==e[f][o];)if(f++,f==n&&(f=s,o++,o==t))return an.deepEqual(e,rn.eye(n).valueOf())?rn.round(a,6):0;if(f!=s){for(var u=0;t>u;u++){var c=e[f][u];e[f][u]=e[s][u],e[s][u]=c}a*=-1}for(var l=e[s][o],u=0;t>u;u++)e[s][u]=e[s][u]/l;a*=l;for(var h=0;n>h;h++)if(h!=s)for(var m=e[h][o],u=0;t>u;u++)e[h][u]=e[h][u]-e[s][u]*m;o++}return an.deepEqual(e,rn.eye(n).valueOf())?rn.round(a,6):0}function Y(e,n,t){var r,i,a,o,s,f=rn.add,u=rn.unaryminus,c=rn.multiply,l=rn.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=rn.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=rn.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&&(s=m[v],m[v]=m[r],m[r]=s,s=p[v],p[v]=p[r],p[r]=s);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]=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 p}function k(e){if(1==e.length)return k(e[0]);for(var n=0,t=e.length;t>n;n++){var r=e[n];r instanceof Array&&(e[n]=k(r))}return e}function Q(e,n){var r,i;return e instanceof Array||e instanceof w?(r=rn.matrix(e),i=r.get(n),i.valueOf()):e instanceof t?e.get(n):y(e)?W(e,n):(r=rn.matrix([e]),i=r.get(n),i.valueOf())}function W(e,n){var t,r;if(n=n.valueOf(),1!=n.length)throw new RangeError("Dimension mismatch ("+n.length+" != 1)");n instanceof Array&&(n=n[0]),n=n.valueOf(),n instanceof Array||(n=[n]);var i="",a=e.length;for(t=0,r=n.length;r>t;t++){var o=n[t];an.validateIndex(o,a),i+=e.charAt(o-1)}return i}function K(e,n,r){if(e instanceof Array||e instanceof w){var i=rn.matrix(rn.clone(e));return i.set(n,r),i.valueOf()}return e instanceof t?e.clone().set(n,r):y(e)?Z(e,n,r):(i=rn.matrix([e]),i.set(n,r),i.isScalar()?i.toScalar():i.valueOf())}function Z(e,n,t){var r,i;if(n=n.valueOf(),1!=n.length)throw new RangeError("Dimension mismatch ("+n.length+" != 1)");if(n instanceof Array&&(n=n[0]),n=n.valueOf(),n instanceof Array||(n=[n]),n.length!=t.length)throw new RangeError("Dimension mismatch ("+n.length+" != "+t.length+")");var a=e.length,o=[];for(r=0;a>r;r++)o[r]=e.charAt(r);for(r=0,i=n.length;i>r;r++){var s=n[r];an.validateIndex(s),o[s-1]=t.charAt(r)}if(o.length>a)for(r=a-1,i=o.length;i>r;r++)o[r]||(o[r]=" ");return o.join("")}function X(e){for(var n=rn.larger,t=e[0],r=1,i=e.length;i>r;r++){var a=e[r];n(a,t)&&(t=a)}return t}function J(e,n,t){for(var r=rn.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 $(e){for(var n=rn.smaller,t=e[0],r=1,i=e.length;i>r;r++){var a=e[r];n(a,t)&&(t=a)}return t}function en(e,n,t){for(var r=rn.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 nn(e,n,t){(t.override||void 0===rn[e])&&(rn[e]=t.wrap&&"function"==typeof n?function(){for(var e=[],t=0,r=arguments.length;r>t;t++)e[t]=arguments[t].valueOf();return n.apply(rn,e)}:n,d(e,n))}function tn(e){return"function"==typeof e||p(e)||y(e)||e instanceof n||e instanceof E}var rn={type:{},expr:{node:{}},options:{precision:5}};"undefined"!=typeof module&&"undefined"!=typeof module.exports&&(module.exports=rn),"undefined"!=typeof exports&&(exports=rn),"undefined"!=typeof require&&"undefined"!=typeof define&&define(function(){return rn}),"undefined"!=typeof window&&(window.math=rn);var an=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?rn.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 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 rn.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+=rn.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.validateIndex=function(e,n){if(!p(e)||!v(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+")")},a.resize=function(e,n,t){if(!(n instanceof Array))throw new TypeError("Size must be an array (size is "+rn["typeof"](n)+")");n.forEach(function(e){if(!p(e)||!v(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}();rn.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=an.formatNumber(this.re,rn.options.precision),t=an.formatNumber(this.im,rn.options.precision);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+" - "+an.formatNumber(Math.abs(this.im),rn.options.precision)+"i"},rn.type.Matrix=t,t.prototype.get=function(e){var n;if(e instanceof t)n=1==e.size().length||!e.size().some(function(e){return 1!=e}),e=e.valueOf();else{if(!(e instanceof Array))throw new TypeError("Invalid index");n=!e.some(function(e){var n=rn.size(e);return 0!=n.length&&n!=[1]})}if(e.length!=this._size.length)throw new RangeError("Dimension mismatch ("+e.length+" != "+this._size.length+")");if(n)switch(e.length){case 1:return r(this._data,e[0]);case 2:return r(r(this._data,e[0]),e[1]);default:return i(this._data,e)}else switch(e.length){case 1:return new t(a(this._data,e));case 2:return new t(o(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=1==e.size().length||!e.size().some(function(e){return 1!=e}),e=e.valueOf();else{if(!(e instanceof Array))throw new TypeError("Invalid index");r=!e.some(function(e){var n=rn.size(e);return 0!=n.length&&n!=[1]})}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!=rn.size(n).length)throw new TypeError("Scalar value expected");switch(e.length){case 1:c(this._data,this._size,e,n);break;case 2:l(this._data,this._size,e,n);break;default:u(this._data,this._size,e,n)}}else{var i=this._size.concat();h(this._data,i,e,0,n),an.deepEqual(this._size,i)||(m(this._data),this.resize(i))}return this},t.prototype.resize=function(e,n){an.resize(this._data,e,n),this._size=rn.clone(e)},t.prototype.clone=function(){var e=new t;return e._data=rn.clone(this._data),e._size=rn.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=rn.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:rn.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 rn.clone(this._data)},t.prototype.valueOf=function(){return this._data},t.prototype.toString=function(){return rn.format(this._data)},rn.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 rn.sign(t)==rn.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=rn.format(Number(this.start));return 1!=this.step&&(e+=":"+rn.format(Number(this.step))),e+=":"+rn.format(Number(this.end))},rn.type.Selector=function(e){if(!(this instanceof rn.type.Selector))throw new SyntaxError("Selector constructor must be called with the new operator");this.value=e instanceof rn.type.Selector?e.value:e||void 0},rn.type.Selector.prototype={done:function(){return this.value},get:function(e){var n=this.value;if(!n)throw Error("Selector value is undefined");return new rn.type.Selector(rn.subset(n,e))},set:function(e,n){var t=this.value;if(!t)throw Error("Selector value is undefined");return new rn.type.Selector(rn.subset(t,e,n))},valueOf:function(){return this.value},toString:function(){return rn.format(this.value)}},rn.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!=x(e)},E._findBaseQuantity=function(e){var n=E.BASE_QUANTITY;for(var t in e)0===e[t]&&delete e[t];e:for(var r in n){var i=n[r];for(var t in e)if(e[t]!==i.dimensions[t])continue e;for(var t in i.dimensions)if(void 0===e[t])continue e;return i}throw new TypeError("Could not find a matching base quantity for the given dimensions: "+JSON.stringify(e))},E._findUnitFromBase=function(e){if(e==on.NONE)return E.UNIT_NONE;for(var n=E.UNITS,t=0,r=n.length;r>t;t++){var i=n[t];if(i.base===e)return i}throw new Error("No matching unit for the base quantity "+e.name+" was found")},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?an.formatNumber(e,rn.options.precision)+" ":"",n+=this.prefix.name+this.unit.name;else{var t=this._bestPrefix();e=this._unnormalize(this.value,t.value),n=null!=this.value?an.formatNumber(e,rn.options.precision)+" ":"",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_QUANTITY={NONE:{name:"none",dimensions:{}},LENGTH:{name:"length",dimensions:{m:1}},MASS:{name:"mass",dimensions:{kg:1}},TIME:{name:"time",dimensions:{s:1}},CURRENT:{name:"current",dimensions:{A:1}},TEMPERATURE:{name:"temperature",dimensions:{K:1}},LUMINOUS_INTENSITY:{name:"luminous intensity",dimensions:{cd:1}},AMOUNT_OF_SUBSTANCE:{name:"amount of substance",dimensions:{mol:1}},FORCE:{name:"force",dimensions:{kg:1,m:1,s:-2}},SURFACE:{name:"surface",dimensions:{m:2}},VOLUME:{name:"volume",dimensions:{m:3}},ANGLE:{name:"angle",dimensions:{rad:1}},BIT:{name:"bit",dimensions:{b:1}},FREQUENCY:{name:"frequency",dimensions:{s:-1}},SPEED:{name:"speed",dimensions:{m:1,s:-1}},ACCELERATION:{name:"acceleration",dimensions:{m:1,s:-2}}};var on=E.BASE_QUANTITY,sn=E.PREFIXES;E.UNIT_NONE={name:"",base:E.BASE_QUANTITY.NONE,value:1,offset:0},E.UNITS=[{name:"meter",base:on.LENGTH,prefixes:sn.LONG,value:1,offset:0},{name:"inch",base:on.LENGTH,prefixes:sn.NONE,value:.0254,offset:0},{name:"foot",base:on.LENGTH,prefixes:sn.NONE,value:.3048,offset:0},{name:"yard",base:on.LENGTH,prefixes:sn.NONE,value:.9144,offset:0},{name:"mile",base:on.LENGTH,prefixes:sn.NONE,value:1609.344,offset:0},{name:"link",base:on.LENGTH,prefixes:sn.NONE,value:.201168,offset:0},{name:"rod",base:on.LENGTH,prefixes:sn.NONE,value:5.02921,offset:0},{name:"chain",base:on.LENGTH,prefixes:sn.NONE,value:20.1168,offset:0},{name:"angstrom",base:on.LENGTH,prefixes:sn.NONE,value:1e-10,offset:0},{name:"m",base:on.LENGTH,prefixes:sn.SHORT,value:1,offset:0},{name:"ft",base:on.LENGTH,prefixes:sn.NONE,value:.3048,offset:0},{name:"yd",base:on.LENGTH,prefixes:sn.NONE,value:.9144,offset:0},{name:"mi",base:on.LENGTH,prefixes:sn.NONE,value:1609.344,offset:0},{name:"li",base:on.LENGTH,prefixes:sn.NONE,value:.201168,offset:0},{name:"rd",base:on.LENGTH,prefixes:sn.NONE,value:5.02921,offset:0},{name:"ch",base:on.LENGTH,prefixes:sn.NONE,value:20.1168,offset:0},{name:"mil",base:on.LENGTH,prefixes:sn.NONE,value:254e-7,offset:0},{name:"m2",base:on.SURFACE,prefixes:sn.SHORT,value:1,offset:0},{name:"sqin",base:on.SURFACE,prefixes:sn.NONE,value:64516e-8,offset:0},{name:"sqft",base:on.SURFACE,prefixes:sn.NONE,value:.09290304,offset:0},{name:"sqyd",base:on.SURFACE,prefixes:sn.NONE,value:.83612736,offset:0},{name:"sqmi",base:on.SURFACE,prefixes:sn.NONE,value:2589988.110336,offset:0},{name:"sqrd",base:on.SURFACE,prefixes:sn.NONE,value:25.29295,offset:0},{name:"sqch",base:on.SURFACE,prefixes:sn.NONE,value:404.6873,offset:0},{name:"sqmil",base:on.SURFACE,prefixes:sn.NONE,value:6.4516e-10,offset:0},{name:"m3",base:on.VOLUME,prefixes:sn.SHORT,value:1,offset:0},{name:"L",base:on.VOLUME,prefixes:sn.SHORT,value:.001,offset:0},{name:"litre",base:on.VOLUME,prefixes:sn.LONG,value:.001,offset:0},{name:"cuin",base:on.VOLUME,prefixes:sn.NONE,value:16387064e-12,offset:0},{name:"cuft",base:on.VOLUME,prefixes:sn.NONE,value:.028316846592,offset:0},{name:"cuyd",base:on.VOLUME,prefixes:sn.NONE,value:.764554857984,offset:0},{name:"teaspoon",base:on.VOLUME,prefixes:sn.NONE,value:5e-6,offset:0},{name:"tablespoon",base:on.VOLUME,prefixes:sn.NONE,value:15e-6,offset:0},{name:"minim",base:on.VOLUME,prefixes:sn.NONE,value:6.161152e-8,offset:0},{name:"fluiddram",base:on.VOLUME,prefixes:sn.NONE,value:36966911e-13,offset:0},{name:"fluidounce",base:on.VOLUME,prefixes:sn.NONE,value:2957353e-11,offset:0},{name:"gill",base:on.VOLUME,prefixes:sn.NONE,value:.0001182941,offset:0},{name:"cup",base:on.VOLUME,prefixes:sn.NONE,value:.0002365882,offset:0},{name:"pint",base:on.VOLUME,prefixes:sn.NONE,value:.0004731765,offset:0},{name:"quart",base:on.VOLUME,prefixes:sn.NONE,value:.0009463529,offset:0},{name:"gallon",base:on.VOLUME,prefixes:sn.NONE,value:.003785412,offset:0},{name:"beerbarrel",base:on.VOLUME,prefixes:sn.NONE,value:.1173478,offset:0},{name:"oilbarrel",base:on.VOLUME,prefixes:sn.NONE,value:.1589873,offset:0},{name:"hogshead",base:on.VOLUME,prefixes:sn.NONE,value:.238481,offset:0},{name:"fldr",base:on.VOLUME,prefixes:sn.NONE,value:36966911e-13,offset:0},{name:"floz",base:on.VOLUME,prefixes:sn.NONE,value:2957353e-11,offset:0},{name:"gi",base:on.VOLUME,prefixes:sn.NONE,value:.0001182941,offset:0},{name:"cp",base:on.VOLUME,prefixes:sn.NONE,value:.0002365882,offset:0},{name:"pt",base:on.VOLUME,prefixes:sn.NONE,value:.0004731765,offset:0},{name:"qt",base:on.VOLUME,prefixes:sn.NONE,value:.0009463529,offset:0},{name:"gal",base:on.VOLUME,prefixes:sn.NONE,value:.003785412,offset:0},{name:"bbl",base:on.VOLUME,prefixes:sn.NONE,value:.1173478,offset:0},{name:"obl",base:on.VOLUME,prefixes:sn.NONE,value:.1589873,offset:0},{name:"g",base:on.MASS,prefixes:sn.SHORT,value:.001,offset:0},{name:"gram",base:on.MASS,prefixes:sn.LONG,value:.001,offset:0},{name:"ton",base:on.MASS,prefixes:sn.SHORT,value:907.18474,offset:0},{name:"tonne",base:on.MASS,prefixes:sn.SHORT,value:1e3,offset:0},{name:"grain",base:on.MASS,prefixes:sn.NONE,value:6479891e-11,offset:0},{name:"dram",base:on.MASS,prefixes:sn.NONE,value:.0017718451953125,offset:0},{name:"ounce",base:on.MASS,prefixes:sn.NONE,value:.028349523125,offset:0},{name:"poundmass",base:on.MASS,prefixes:sn.NONE,value:.45359237,offset:0},{name:"hundredweight",base:on.MASS,prefixes:sn.NONE,value:45.359237,offset:0},{name:"stick",base:on.MASS,prefixes:sn.NONE,value:.115,offset:0},{name:"gr",base:on.MASS,prefixes:sn.NONE,value:6479891e-11,offset:0},{name:"dr",base:on.MASS,prefixes:sn.NONE,value:.0017718451953125,offset:0},{name:"oz",base:on.MASS,prefixes:sn.NONE,value:.028349523125,offset:0},{name:"lbm",base:on.MASS,prefixes:sn.NONE,value:.45359237,offset:0},{name:"cwt",base:on.MASS,prefixes:sn.NONE,value:45.359237,offset:0},{name:"s",base:on.TIME,prefixes:sn.SHORT,value:1,offset:0},{name:"min",base:on.TIME,prefixes:sn.NONE,value:60,offset:0},{name:"h",base:on.TIME,prefixes:sn.NONE,value:3600,offset:0},{name:"seconds",base:on.TIME,prefixes:sn.LONG,value:1,offset:0},{name:"second",base:on.TIME,prefixes:sn.LONG,value:1,offset:0},{name:"sec",base:on.TIME,prefixes:sn.LONG,value:1,offset:0},{name:"minutes",base:on.TIME,prefixes:sn.NONE,value:60,offset:0},{name:"minute",base:on.TIME,prefixes:sn.NONE,value:60,offset:0},{name:"hours",base:on.TIME,prefixes:sn.NONE,value:3600,offset:0},{name:"hour",base:on.TIME,prefixes:sn.NONE,value:3600,offset:0},{name:"day",base:on.TIME,prefixes:sn.NONE,value:86400,offset:0},{name:"days",base:on.TIME,prefixes:sn.NONE,value:86400,offset:0},{name:"m/s",base:on.SPEED,prefixes:sn.SHORT,value:1,offset:0},{name:"mps",base:on.SPEED,prefixes:sn.SHORT,value:1,offset:0},{name:"m/h",base:on.SPEED,prefixes:sn.SHORT,value:1/3600,offset:0},{name:"mph",base:on.SPEED,prefixes:sn.SHORT,value:1/3600,offset:0},{name:"m/s2",base:on.ACCELERATION,prefixes:sn.SHORT,value:1,offset:0},{name:"mps2",base:on.ACCELERATION,prefixes:sn.SHORT,value:1,offset:0},{name:"Hz",base:on.FREQUENCY,prefixes:sn.SHORT,value:1,offset:0},{name:"rad",base:on.ANGLE,prefixes:sn.NONE,value:1,offset:0},{name:"deg",base:on.ANGLE,prefixes:sn.NONE,value:.017453292519943295,offset:0},{name:"grad",base:on.ANGLE,prefixes:sn.NONE,value:.015707963267948967,offset:0},{name:"cycle",base:on.ANGLE,prefixes:sn.NONE,value:6.283185307179586,offset:0},{name:"A",base:on.CURRENT,prefixes:sn.SHORT,value:1,offset:0},{name:"ampere",base:on.CURRENT,prefixes:sn.LONG,value:1,offset:0},{name:"K",base:on.TEMPERATURE,prefixes:sn.NONE,value:1,offset:0},{name:"degC",base:on.TEMPERATURE,prefixes:sn.NONE,value:1,offset:273.15},{name:"degF",base:on.TEMPERATURE,prefixes:sn.NONE,value:1/1.8,offset:459.67},{name:"degR",base:on.TEMPERATURE,prefixes:sn.NONE,value:1/1.8,offset:0},{name:"kelvin",base:on.TEMPERATURE,prefixes:sn.NONE,value:1,offset:0},{name:"celsius",base:on.TEMPERATURE,prefixes:sn.NONE,value:1,offset:273.15},{name:"fahrenheit",base:on.TEMPERATURE,prefixes:sn.NONE,value:1/1.8,offset:459.67},{name:"rankine",base:on.TEMPERATURE,prefixes:sn.NONE,value:1/1.8,offset:0},{name:"mol",base:on.AMOUNT_OF_SUBSTANCE,prefixes:sn.NONE,value:1,offset:0},{name:"mole",base:on.AMOUNT_OF_SUBSTANCE,prefixes:sn.NONE,value:1,offset:0},{name:"cd",base:on.LUMINOUS_INTENSITY,prefixes:sn.NONE,value:1,offset:0},{name:"candela",base:on.LUMINOUS_INTENSITY,prefixes:sn.NONE,value:1,offset:0},{name:"N",base:on.FORCE,prefixes:sn.SHORT,value:1,offset:0},{name:"newton",base:on.FORCE,prefixes:sn.LONG,value:1,offset:0},{name:"lbf",base:on.FORCE,prefixes:sn.NONE,value:4.4482216152605,offset:0},{name:"poundforce",base:on.FORCE,prefixes:sn.NONE,value:4.4482216152605,offset:0},{name:"b",base:on.BIT,prefixes:sn.BINARY_SHORT,value:1,offset:0},{name:"bits",base:on.BIT,prefixes:sn.BINARY_LONG,value:1,offset:0},{name:"B",base:on.BIT,prefixes:sn.BINARY_SHORT,value:8,offset:0},{name:"bytes",base:on.BIT,prefixes:sn.BINARY_LONG,value:8,offset:0}],rn.E=Math.E,rn.LN2=Math.LN2,rn.LN10=Math.LN10,rn.LOG2E=Math.LOG2E,rn.LOG10E=Math.LOG10E,rn.PI=Math.PI,rn.SQRT1_2=Math.SQRT1_2,rn.SQRT2=Math.SQRT2,rn.I=new n(0,1),rn.pi=rn.PI,rn.e=rn.E,rn.i=rn.I,rn.expr.node.Node=b,b.prototype.eval=function(){throw new Error("Cannot evaluate a Node interface")
},b.prototype.find=function(e){return this.match(e)?[this]:[]},b.prototype.match=function(e){var n=!0;if(e&&(!e.type||this instanceof e.type||(n=!1),n&&e.properties))for(var t in e.properties)if(e.properties.hasOwnProperty(t)&&this[t]!=e.properties[t]){n=!1;break}return n},b.prototype.toString=function(){return""},M.prototype=new b,rn.expr.node.ConstantNode=M,M.prototype.eval=function(){return this.value},M.prototype.toString=function(){return rn.format(this.value||null)},A.prototype=new b,rn.expr.node.OperatorNode=A,A.prototype.eval=function(){return this.fn.apply(this,this.params.map(function(e){return e.eval()}))},A.prototype.find=function(e){var n=[];this.match(e)&&n.push(this);var t=this.params;if(t)for(var r=0,i=t.length;i>r;r++)n=n.concat(t[r].find(e));return n},A.prototype.toString=function(){var e=this.params;if(this.fn===rn.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,rn.expr.node.SymbolNode=S,S.prototype.eval=function(){var e=this.scope.get(this.name);if(void 0===e)throw new Error("Undefined symbol "+this.name);return e},S.prototype.toString=function(){return this.name},T.prototype=new b,rn.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.hasContextParams){var i,a=this.paramScopes;if(i=r.size?r.size():void 0!==r.length?[r.length]:[],a&&i)for(e=0,n=this.params.length;n>e;e++){var o=a[e];o&&o.set("end",i[e])}}var s=this.params,f=[];for(e=0,n=this.params.length;n>e;e++)f[e]=s[e].eval();return"function"==typeof r?r.apply(this,f):rn.subset(r,f)},T.prototype.find=function(e){var n=[];this.match(e)&&n.push(this),this.object&&(n=n.concat(this.object.find(e)));var t=this.params;if(t)for(var r=0,i=t.length;i>r;r++)n=n.concat(t[r].find(e));return n},T.prototype.toString=function(){var e=this.object?this.object.toString():"";return this.params&&(e+="("+this.params.join(", ")+")"),e},R.prototype=new b,rn.expr.node.MatrixNode=R,function(){function e(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=rn.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]):l instanceof Array?(c=[1,l.length],l=[l]):(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}R.prototype.eval=function(){for(var n=this.nodes,r=[],i=!1,a=0,o=n.length;o>a;a++){for(var s=n[a],f=[],u=0,c=s.length;c>u;u++){var l=s[u].eval();(l instanceof t||l instanceof w||l instanceof Array)&&(i=!0),f[u]=l}r[a]=f}return i&&(r=e(r)),new t(r)},R.prototype.find=function(e){var n=[];this.match(e)&&n.push(this);for(var t=this.nodes,r=0,i=t.length;i>r;r++)for(var a=t[r],o=0,s=a.length;s>o;o++)n=n.concat(a[o].find(e));return n},R.prototype.toString=function(){return an.formatArray(this.nodes)}}(),U.prototype=new b,rn.expr.node.BlockNode=U,U.prototype.add=function(e,n){var t=this.params.length;this.params[t]=e,this.visible[t]=void 0!=n?n:!0},U.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},U.prototype.find=function(e){var n=[];this.match(e)&&n.push(this);var t=this.params;if(t)for(var r=0,i=t.length;i>r;r++)n=n.concat(t[r].find(e));return n},U.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]"},z.prototype=new b,rn.expr.node.AssignmentNode=z,z.prototype.eval=function(){if(void 0===this.expr)throw new Error("Undefined symbol "+this.name);var e=this.expr.eval();return this.scope.set(this.name,e),e},z.prototype.find=function(e){var n=[];return this.match(e)&&n.push(this),this.expr&&(n=n.concat(this.expr.find(e))),n},z.prototype.toString=function(){return this.name+" = "+this.expr.toString()},L.prototype=new b,rn.expr.node.UpdateNode=L,L.prototype.eval=function(){if(void 0===this.expr)throw new Error("Undefined symbol "+this.name);var e;this.params;var n=this.scope.get(this.name);if(void 0==n)throw new Error("Undefined symbol "+this.name);if(this.hasContextParams){var t,r=this.paramScopes;if(t=n.size?n.size():void 0!==n.length?[n.length]:[],r&&t)for(var i=0,a=this.params.length;a>i;i++){var o=r[i];o&&o.set("end",t[i])}}var s=[];this.params.forEach(function(e){s.push(e.eval())});var f=this.expr.eval();return e=rn.subset(n,s,f),this.scope.set(this.name,e),e},L.prototype.find=function(e){var n=[];this.match(e)&&n.push(this);var t=this.params;if(t)for(var r=0,i=t.length;i>r;r++)n=n.concat(t[r].find(e));return this.expr&&(n=n.concat(this.expr.find(e))),n},L.prototype.toString=function(){var e="";return e+=this.name,this.params&&this.params.length&&(e+="("+this.params.join(", ")+")"),e+=" = ",e+=this.expr.toString()},_.prototype=new b,rn.expr.node.FunctionNode=_,_.prototype.eval=function(){return this.scope.set(this.name,this.fn),this.fn},_.prototype.find=function(e){var n=[];return this.match(e)&&n.push(this),this.expr&&(n=n.concat(this.expr.find(e))),n},_.prototype.toString=function(){return this.fn.toString()},rn.expr.Scope=function(){if(this.parentScope=null,this.subScopes=null,this.symbols={},this.cache={},arguments.length>0){var e=arguments[0];e instanceof rn.expr.Scope?this.parentScope=e:e instanceof Object&&(this.symbols=e)}if(arguments.length>1){var n=arguments[1];n instanceof Object&&(this.symbols=n)}},rn.expr.Scope.prototype={createSubScope:function(){var e=new rn.expr.Scope(this);return this.subScopes||(this.subScopes=[]),this.subScopes.push(e),e},get:function(e){var n;if(n=this.symbols[e],void 0!==n)return n;var t=this.cache[e];if(t)return t[e];for(var r=this.parentScope;r;){if(n=r.symbols[e],void 0!==n)return this.cache[e]=r.symbols,n;r=r.parentScope}return n=rn[e],void 0!==n?(this.cache[e]=rn,n):E.isPlainUnit(e)?(n=new E(null,e),this.cache[e]={},this.cache[e][e]=n,n):void 0},has:function(e){return void 0!==this.symbols[e]},set:function(e,n){return this.symbols[e]=n},remove:function(e){delete this.symbols[e]},clear:function(){var e=this.symbols;for(var n in e)e.hasOwnProperty(n)&&delete e[n];if(this.subScopes)for(var t=this.subScopes,r=0,i=t.length;i>r;r++)t[r].clear();this.clearCache()},clearCache:function(){this.cache={}}},rn.expr.Parser=function(){if(!(this instanceof rn.expr.Parser))throw new SyntaxError("Parser constructor must be called with the new operator");this.scope=new rn.expr.Scope},rn.expr.Parser.prototype.parse=function(e){return rn.parse(e,this.scope)},rn.expr.Parser.prototype.eval=function(e){var n=rn.parse(e,this.scope);return n.eval()},rn.expr.Parser.prototype.get=function(e){return this.scope.get(e)},rn.expr.Parser.prototype.set=function(e,n){this.scope.set(e,n)},rn.expr.Parser.prototype.remove=function(e){this.scope.remove(e)},rn.expr.Parser.prototype.clear=function(){this.scope.clear()},rn.expr.Expression=function(e){this.id=e.id,this.scope=e.scope,this.nextNode=e.nextNode,this.previousNode=e.previousNode,this.updateSeq=0,this.node=void 0,this.symbols={},this.assignments={},this.updates={},this.result=void 0,this.setExpr(e.expression)},rn.expr.Expression.prototype.setExpr=function(e){this.expression=e||"",this.scope.clear(),this._parse(),this._analyse()},rn.expr.Expression.prototype.getExpr=function(){return this.expression},rn.expr.Expression.prototype.getResult=function(){return this.result},rn.expr.Expression.prototype._parse=function(){try{this.node=rn.parse(this.expression,this.scope)}catch(e){var n="Error: "+String(e.message||e);this.node=new M(n)}},rn.expr.Expression.prototype._analyse=function(){var e,n,t;if(this.node){var r=this.node.find({type:rn.expr.node.SymbolNode});for(this.symbols={},e=0,n=r.length;n>e;e++)t=r[e],this.symbols[t.name]=t;var i=this.node.find({type:rn.expr.node.AssignmentNode});for(this.assignments={},e=0,n=i.length;n>e;e++)t=i[e],this.assignments[t.name]=t;var a=this.node.find({type:rn.expr.node.UpdateNode});for(this.updates={},e=0,n=a.length;n>e;e++)t=a[e],this.updates[t.name]=t}},rn.expr.Expression.prototype.eval=function(){try{this.scope.clear(),this.result=this.node.eval()}catch(e){this.scope.clear(),this.result="Error: "+String(e.message||e)}return this.result},rn.expr.Workspace=I,I.prototype.clear=function(){this.nodes={},this.firstNode=void 0,this.lastNode=void 0},I.prototype.append=function(e){var n=this._getNewId(),t=this.lastNode?this.lastNode.scope:this.scope,r=new rn.expr.Scope(t),i=new rn.expr.Expression({id:n,expression:e,scope:r,nextNode:void 0,previousNode:this.lastNode});return this.nodes[n]=i,this.firstNode||(this.firstNode=i),this.lastNode&&(this.lastNode.nextNode=i),this.lastNode=i,this._update([n]),n},I.prototype.insertBefore=function(e,n){var t=this.nodes[n];if(!t)throw new RangeError('Node with id "'+n+'" not found');var r=t.previousNode,i=this._getNewId(),a=r?r.scope:this.scope,o=new rn.expr.Scope(a),s=new rn.expr.Expression({id:i,expression:e,scope:o,nextNode:t,previousNode:r});this.nodes[i]=s,r?r.nextNode=s:this.firstNode=s,t.previousNode=s,t.scope.parentScope=s.scope;var f=this.getDependencies(i);return-1==f.indexOf(i)&&f.unshift(i),this._update(f),i},I.prototype.insertAfter=function(e,n){var t=this.nodes[n];if(!t)throw new RangeError('Node with id "'+n+'" not found');var r=t.nextNode;return r?this.insertBefore(e,r.id):this.append(e)},I.prototype.remove=function(e){var n=this.nodes[e];if(!n)throw new RangeError('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)},I.prototype.replace=function(e,n){var t=this.nodes[n];if(!t)throw new RangeError('Node with id "'+n+'" not found');var r=[n];I._merge(r,this.getDependencies(n)),t.setExpr(e),I._merge(r,this.getDependencies(n)),this._update(r)},I._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)}},I.prototype.getDependencies=function(e){var n,t=this.nodes[e],r=[],i={};if(!t)throw new RangeError('Node with id "'+e+'" not found');var a=function(e,n){var t,r=e.assignments,a=e.updates;for(t in r)r.hasOwnProperty(t)&&(i[t]=n);for(t in a)a.hasOwnProperty(t)&&(i[t]=n)};for(a(t,!0),t=t.nextNode;t;){var o=t.symbols,s=!1;for(n in o)if(o.hasOwnProperty(n)&&1==i[n]){s=!0;break}s?(a(t,!0),r.push(t.id)):a(t,!1),t=t.nextNode}return r},I.prototype.getExpr=function(e){var n=this.nodes[e];if(!n)throw new RangeError('Node with id "'+e+'" not found');return n.getExpr()},I.prototype.getResult=function(e){var n=this.nodes[e];if(!n)throw new RangeError('Node with id "'+e+'" not found');return n.getResult()},I.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)}},I.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}},I.prototype._getNewId=function(){return this.idMax++,this.idMax},I.prototype.toString=function(){return JSON.stringify(this.toJSON())},I.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},rn.abs=function(e){if(1!=arguments.length)throw O("abs",arguments.length,1);if(p(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 an.map(e,rn.abs);if(e.valueOf()!==e)return rn.abs(e.valueOf());throw N("abs",e)},rn.add=function(e,r){if(2!=arguments.length)throw O("add",arguments.length,2);if(p(e)){if(p(r))return e+r;if(r instanceof n)return new n(e+r.re,r.im)}else if(e instanceof n){if(p(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 an.map2(e,r,rn.add);if(e.valueOf()!==e||r.valueOf()!==r)return rn.add(e.valueOf(),r.valueOf());throw N("add",e,r)},rn.ceil=function(e){if(1!=arguments.length)throw O("ceil",arguments.length,1);if(p(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 an.map(e,rn.ceil);if(e.valueOf()!==e)return rn.ceil(e.valueOf());throw N("ceil",e)},rn.cube=function(e){if(1!=arguments.length)throw O("cube",arguments.length,1);if(p(e))return e*e*e;if(e instanceof n)return rn.multiply(rn.multiply(e,e),e);if(e instanceof Array||e instanceof t)return an.map(e,rn.cube);if(e.valueOf()!==e)return rn.cube(e.valueOf());throw N("cube",e)},rn.divide=function(e,r){if(2!=arguments.length)throw O("divide",arguments.length,2);if(p(e)){if(p(r))return e/r;if(r instanceof n)return q(new n(e,0),r);if(r instanceof E)return rn.divide(new E(e,null),r)}if(e instanceof n){if(p(r))return q(e,new n(r,0));if(r instanceof n)return q(e,r)}if(e instanceof E){if(p(r)){var i=e.clone();return i.value/=r,i}if(r instanceof E)return P(e,r)}if(e instanceof Array||e instanceof t)return r instanceof Array||r instanceof t?rn.multiply(e,rn.inv(r)):an.map2(e,r,rn.divide);if(r instanceof Array||r instanceof t)return rn.multiply(e,rn.inv(r));if(e.valueOf()!==e||r.valueOf()!==r)return rn.divide(e.valueOf(),r.valueOf());throw N("divide",e,r)},rn.equal=function un(e,r){if(2!=arguments.length)throw O("equal",arguments.length,2);if(p(e)){if(p(r))return e==r;if(r instanceof n)return e==r.re&&0==r.im}if(e instanceof n){if(p(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 an.map2(e,r,rn.equal);if(e.valueOf()!==e||r.valueOf()!==r)return un(e.valueOf(),r.valueOf());throw N("equal",e,r)},rn.exp=function(e){if(1!=arguments.length)throw O("exp",arguments.length,1);if(p(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 an.map(e,rn.exp);if(e.valueOf()!==e)return rn.exp(e.valueOf());throw N("exp",e)},rn.fix=function(e){if(1!=arguments.length)throw O("fix",arguments.length,1);if(p(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 an.map(e,rn.fix);if(e.valueOf()!==e)return rn.fix(e.valueOf());throw N("fix",e)},rn.floor=function(e){if(1!=arguments.length)throw O("floor",arguments.length,1);if(p(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 an.map(e,rn.floor);if(e.valueOf()!==e)return rn.floor(e.valueOf());throw N("floor",e)},rn.gcd=function(){var e,n=arguments[0],r=arguments[1];if(2==arguments.length){if(p(n)&&p(r)){if(!v(n)||!v(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 an.map2(n,r,rn.gcd);if(n.valueOf()!==n||r.valueOf()!==r)return rn.gcd(n.valueOf(),r.valueOf());throw N("gcd",n,r)}if(arguments.length>2){for(var i=1;i<arguments.length;i++)n=rn.gcd(n,arguments[i]);return n}throw new SyntaxError("Function gcd expects two or more arguments")},rn.larger=function(e,r){if(2!=arguments.length)throw O("larger",arguments.length,2);if(p(e)){if(p(r))return e>r;if(r instanceof n)return e>rn.abs(r)}if(e instanceof n){if(p(r))return rn.abs(e)>r;if(r instanceof n)return rn.abs(e)>rn.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 an.map2(e,r,rn.larger);if(e.valueOf()!==e||r.valueOf()!==r)return rn.larger(e.valueOf(),r.valueOf());throw N("larger",e,r)},rn.largereq=function(e,r){if(2!=arguments.length)throw O("largereq",arguments.length,2);if(p(e)){if(p(r))return e>=r;if(r instanceof n)return e>=rn.abs(r)}if(e instanceof n){if(p(r))return rn.abs(e)>=r;if(r instanceof n)return rn.abs(e)>=rn.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 an.map2(e,r,rn.largereq);if(e.valueOf()!==e||r.valueOf()!==r)return rn.largereq(e.valueOf(),r.valueOf());throw N("largereq",e,r)},rn.lcm=function(){var e,n=arguments[0],r=arguments[1];if(2==arguments.length){if(p(n)&&p(r)){if(!v(n)||!v(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 an.map2(n,r,rn.lcm);if(n.valueOf()!==n||r.valueOf()!==r)return rn.lcm(n.valueOf(),r.valueOf());throw N("lcm",n,r)}if(arguments.length>2){for(var a=1;a<arguments.length;a++)n=rn.lcm(n,arguments[a]);return n}throw new SyntaxError("Function lcm expects two or more arguments")},rn.log=function(e,r){if(1==arguments.length){if(p(e))return e>=0?Math.log(e):rn.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 an.map(e,rn.log);if(e.valueOf()!==e)return rn.log(e.valueOf());throw N("log",e)}if(2==arguments.length)return rn.divide(rn.log(e),rn.log(r));throw O("log",arguments.length,1,2)},rn.log10=function(e){if(1!=arguments.length)throw O("log10",arguments.length,1);if(p(e))return e>=0?Math.log(e)/Math.LN10:rn.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 an.map(e,rn.log10);if(e.valueOf()!==e)return rn.log10(e.valueOf());throw N("log10",e)},rn.mod=function(e,n){if(2!=arguments.length)throw O("mod",arguments.length,2);if(p(e)&&p(n))return C(e,n);if(e instanceof Array||e instanceof t||n instanceof Array||n instanceof t)return an.map2(e,n,rn.mod);if(e.valueOf()!==e||n.valueOf()!==n)return rn.mod(e.valueOf(),n.valueOf());throw N("mod",e,n)},rn.multiply=function cn(e,r){if(2!=arguments.length)throw O("multiply",arguments.length,2);if(p(e)){if(p(r))return e*r;if(r instanceof n)return B(new n(e,0),r);if(r instanceof E)return o=r.clone(),o.value*=e,o}else if(e instanceof n){if(p(r))return B(e,new n(r,0));if(r instanceof n)return B(e,r)}else if(e instanceof E){if(p(r))return o=e.clone(),o.value*=r,o;if(r instanceof E)return G(e,r)}else{if(e instanceof Array){if(r instanceof Array){var i=an.size(e),a=an.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],cn=rn.multiply,c=rn.add,l=0;s>l;l++){o[l]=[];for(var h=0;f>h;h++){for(var m=null,v=0;u>v;v++){var g=cn(e[l][v],r[v][h]);m=null==m?g:c(m,g)}o[l][h]=m}}return o}return r instanceof t?new t(rn.multiply(e.valueOf(),r.valueOf())):an.map2(e,r,rn.multiply)}if(e instanceof t)return new t(rn.multiply(e.valueOf(),r.valueOf()))}if(r instanceof Array)return an.map2(e,r,rn.multiply);if(r instanceof t)return new t(rn.multiply(e.valueOf(),r.valueOf()));if(e.valueOf()!==e||r.valueOf()!==r)return rn.multiply(e.valueOf(),r.valueOf());throw N("multiply",e,r)},rn.pow=function(e,r){if(2!=arguments.length)throw O("pow",arguments.length,2);if(p(e)){if(p(r))return v(r)||e>=0?Math.pow(e,r):F(new n(e,0),new n(r,0));if(r instanceof n)return F(new n(e,0),r)}else if(e instanceof n){if(p(r))return F(e,new n(r,0));if(r instanceof n)return F(e,r)}else{if(e instanceof Array){if(!p(r)||!v(r)||0>r)throw new TypeError("For A^b, b must be a positive integer (value is "+r+")");var i=an.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 rn.eye(i[0]);for(var a=e,o=1;r>o;o++)a=rn.multiply(e,a);return a}if(e instanceof t)return new t(rn.pow(e.valueOf(),r))}if(e.valueOf()!==e||r.valueOf()!==r)return rn.pow(e.valueOf(),r.valueOf());throw N("pow",e,r)},rn.round=function(e,r){if(1!=arguments.length&&2!=arguments.length)throw O("round",arguments.length,1,2);if(void 0==r){if(p(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 an.map(e,rn.round);if(e.valueOf()!==e)return rn.round(e.valueOf());throw N("round",e)}if(!p(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(p(e))return H(e,r);if(e instanceof n)return new n(H(e.re,r),H(e.im,r));if(e instanceof Array||e instanceof t||r instanceof Array||r instanceof t)return an.map2(e,r,rn.round);if(e.valueOf()!==e||r.valueOf()!==r)return rn.round(e.valueOf(),r.valueOf());throw N("round",e,r)},rn.sign=function ln(e){if(1!=arguments.length)throw O("sign",arguments.length,1);if(p(e)){var ln;return ln=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 an.map(e,rn.sign);if(e.valueOf()!==e)return rn.sign(e.valueOf());throw N("sign",e)},rn.smaller=function(e,r){if(2!=arguments.length)throw O("smaller",arguments.length,2);if(p(e)){if(p(r))return r>e;if(r instanceof n)return e<rn.abs(r)}if(e instanceof n){if(p(r))return rn.abs(e)<r;if(r instanceof n)return rn.abs(e)<rn.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 an.map2(e,r,rn.smaller);if(e.valueOf()!==e||r.valueOf()!==r)return rn.smaller(e.valueOf(),r.valueOf());throw N("smaller",e,r)},rn.smallereq=function(e,r){if(2!=arguments.length)throw O("smallereq",arguments.length,2);if(p(e)){if(p(r))return r>=e;if(r instanceof n)return e<=rn.abs(r)}if(e instanceof n){if(p(r))return rn.abs(e)<=r;if(r instanceof n)return rn.abs(e)<=rn.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 an.map2(e,r,rn.smallereq);if(e.valueOf()!==e||r.valueOf()!==r)return rn.smallereq(e.valueOf(),r.valueOf());throw N("smallereq",e,r)},rn.sqrt=function(e){if(1!=arguments.length)throw O("sqrt",arguments.length,1);if(p(e))return e>=0?Math.sqrt(e):rn.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 E){var i=Math.sqrt(e.value),a=rn.clone(e.unit.base.dimensions);for(var o in a)a[o]/=2;return new E(i,a)}if(e instanceof Array||e instanceof t)return an.map(e,rn.sqrt);if(e.valueOf()!==e)return rn.sqrt(e.valueOf());throw N("sqrt",e)},rn.square=function(e){if(1!=arguments.length)throw O("square",arguments.length,1);if(p(e))return e*e;if(e instanceof n)return rn.multiply(e,e);if(e instanceof Array||e instanceof t)return an.map(e,rn.square);if(e instanceof E){var r=e.value*e.value,i=rn.clone(e.unit.base.dimensions);for(var a in i)i[a]*=2;return new E(r,i)}if(e.valueOf()!==e)return rn.square(e.valueOf());throw N("square",e)},rn.subtract=function(e,r){if(2!=arguments.length)throw O("subtract",arguments.length,2);if(p(e)){if(p(r))return e-r;if(r instanceof n)return new n(e-r.re,-r.im)}else if(e instanceof n){if(p(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 an.map2(e,r,rn.subtract);if(e.valueOf()!==e||r.valueOf()!==r)return rn.subtract(e.valueOf(),r.valueOf());throw N("subtract",e,r)},rn.unaryminus=function(e){if(1!=arguments.length)throw O("unaryminus",arguments.length,1);if(p(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 an.map(e,rn.unaryminus);if(e.valueOf()!==e)return rn.unaryminus(e.valueOf());throw N("unaryminus",e)},rn.unequal=function(e,r){if(2!=arguments.length)throw O("unequal",arguments.length,2);if(p(e)){if(p(r))return e!=r;if(r instanceof n)return e!=r.re||0!=r.im}if(e instanceof n){if(p(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 an.map2(e,r,rn.unequal);if(e.valueOf()!==e||r.valueOf()!==r)return rn.unequal(e.valueOf(),r.valueOf());throw N("unequal",e,r)},rn.xgcd=function hn(e,n){if(2==arguments.length){if(p(e)&&p(n)){if(!v(e)||!v(n))throw new Error("Parameters in function xgcd must be integer numbers");if(0==n)return[e,1,0];var t=hn(n,e%n),r=t[0],i=t[1],a=t[2];return[r,a,i-a*Math.floor(e/n)]}throw N("xgcd",e,n)}throw new SyntaxError("Function xgcd expects two arguments")},rn.arg=function(e){if(1!=arguments.length)throw O("arg",arguments.length,1);return p(e)?Math.atan2(0,e):e instanceof n?Math.atan2(e.im,e.re):e instanceof Array||e instanceof t?an.map(e,rn.arg):e.valueOf()!==e?rn.arg(e.valueOf()):rn.atan2(0,e)},rn.conj=function(e){if(1!=arguments.length)throw O("conj",arguments.length,1);return p(e)?e:e instanceof n?new n(e.re,-e.im):e instanceof Array||e instanceof t?an.map(e,rn.conj):e.valueOf()!==e?rn.conj(e.valueOf()):clone(e)},rn.im=function(e){if(1!=arguments.length)throw O("im",arguments.length,1);return p(e)?0:e instanceof n?e.im:e instanceof Array||e instanceof t?an.map(e,rn.im):e.valueOf()!==e?rn.im(e.valueOf()):0},rn.re=function(e){if(1!=arguments.length)throw O("re",arguments.length,1);return p(e)?e:e instanceof n?e.re:e instanceof Array||e instanceof t?an.map(e,rn.re):e.valueOf()!==e?rn.re(e.valueOf()):rn.clone(e)},rn.complex=function(){switch(arguments.length){case 0:return new n(0,0);case 1:var e=arguments[0];if(e instanceof n)return e.clone();if(y(e)){var t=n.parse(e);if(t)return t;throw new SyntaxError('String "'+e+'" is no valid complex number')}throw new TypeError("Two numbers or a single string expected in function complex");case 2:return new n(arguments[0],arguments[1]);default:throw O("complex",arguments.length,0,2)}},rn.matrix=function(e){if(arguments.length>1)throw O("matrix",arguments.length,0,1);return new t(e)},rn.number=function(e){switch(arguments.length){case 0:return 0;case 1:var n=Number(e);if(isNaN(n)&&(n=Number(e.valueOf())),isNaN(n))throw new SyntaxError(e.toString()+" is no valid number");return n;default:throw O("number",arguments.length,0,1)}},rn.parser=function(){return new rn.expr.Parser},rn.range=function(e){switch(arguments.length){case 1:if(e instanceof w)return e.clone();if(y(e)){var n=w.parse(e);if(n)return n;throw new SyntaxError('String "'+n+'" is no valid range')}throw new TypeError("Two or three numbers or a single string expected in function range");case 2:return new w(arguments[0],null,arguments[1]);case 3:return new w(arguments[0],arguments[1],arguments[2]);default:throw O("range",arguments.length,2,3)}},rn.string=function(e){switch(arguments.length){case 0:return"";case 1:return j(e);default:throw O("string",arguments.length,0,1)}},rn.unit=function(){switch(arguments.length){case 1:var e=arguments[0];if(e instanceof E)return e.clone();if(y(e)){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')}throw new TypeError("A string or a number and string expected in function unit");case 2:return new E(arguments[0],arguments[1]);default:throw O("unit",arguments.length,1,2)}},rn.workspace=function(){return new rn.expr.Workspace},rn.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&&p(s)){if(n=i,i=s,!v(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 N("concat",s);var f=rn.clone(s.valueOf()),u=rn.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=D(c,o.shift(),i-1,0);return a?new t(c):c},rn.det=function(e){if(1!=arguments.length)throw O("det",arguments.length,1);var n=rn.size(e);switch(n.length){case 0:return rn.clone(e);case 1:if(1==n[0])return rn.clone(e.valueOf()[0]);throw new RangeError("Matrix must be square (size: "+rn.format(n)+")");case 2:var t=n[0],r=n[1];if(t==r)return V(e.valueOf(),t,r);throw new RangeError("Matrix must be square (size: "+rn.format(n)+")");default:throw new RangeError("Matrix must be two dimensional (size: "+rn.format(n)+")")}},rn.diag=function(e,n){var r,i,a,o;if(1!=arguments.length&&2!=arguments.length)throw O("diag",arguments.length,1,2);if(n){if(!p(n)||!v(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]=rn.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]=rn.clone(r[a+f][a+s]);return new t(i);default:throw new RangeError("Matrix for function diag must be 2 dimensional")}},rn.eye=function(){var e=an.argsToArray(arguments);if(0==e.length)e=[1,1];else if(1==e.length)e[1]=e[0];else if(e.length>2)throw O("eye",e.length,0,2);var n=e[0],r=e[1];if(!p(n)||!v(n)||1>n)throw new Error("Parameters in function eye must be positive integers");if(r&&(!p(r)||!v(r)||1>r))throw new Error("Parameters in function eye must be positive integers");
var i=new t;i.resize(e);for(var a=rn.min(e),o=i.valueOf(),s=0;a>s;s++)o[s][s]=1;return i},rn.inv=function(e){if(1!=arguments.length)throw O("inv",arguments.length,1);var n=rn.size(e);switch(n.length){case 0:return rn.divide(1,e);case 1:if(1==n[0])return e instanceof t?new t([rn.divide(1,e.valueOf()[0])]):[rn.divide(1,e[0])];throw new RangeError("Matrix must be square (size: "+rn.format(n)+")");case 2:var r=n[0],i=n[1];if(r==i)return e instanceof t?new t(Y(e.valueOf(),r,i)):Y(e,r,i);throw new RangeError("Matrix must be square (size: "+rn.format(n)+")");default:throw new RangeError("Matrix must be two dimensional (size: "+rn.format(n)+")")}},rn.ones=function(){var e=an.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},rn.size=function(e){if(1!=arguments.length)throw O("size",arguments.length,1);if(p(e)||e instanceof n||e instanceof E||null==e)return[];if(y(e))return[e.length];if(e instanceof Array)return an.size(e);if(e instanceof t)return e.size();if(e.valueOf()!==e)return rn.size(e.valueOf());throw N("size",e)},rn.squeeze=function(e){if(1!=arguments.length)throw O("squeeze",arguments.length,1);return e instanceof Array?k(rn.clone(e)):e instanceof t?k(e.toArray()):e.valueOf()instanceof Array?k(rn.clone(e.valueOf())):rn.clone(e)},rn.subset=function(){switch(arguments.length){case 2:return Q(arguments[0],arguments[1]);case 3:return K(arguments[0],arguments[1],arguments[2]);default:throw O("subset",arguments.length,2,3)}},rn.transpose=function(e){if(1!=arguments.length)throw O("transpose",arguments.length,1);var n=rn.size(e);switch(n.length){case 0:return rn.clone(e);case 1:return rn.clone(e);case 2:for(var r,i=n[1],a=n[0],o=e instanceof t,s=e.valueOf(),f=[],u=rn.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: "+rn.format(n)+")")}},rn.zeros=function(){var e=an.argsToArray(arguments);0==e.length?e=[1,1]:1==e.length&&(e[1]=e[0]);var n=new t;return n.resize(e),n},rn.factorial=function(e){if(1!=arguments.length)throw O("factorial",arguments.length,1);if(p(e)){if(!v(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 an.map(e,rn.factorial);if(e.valueOf()!==e)return rn.factorial(e.valueOf());throw N("factorial",e)},rn.random=function(){if(0!=arguments.length)throw O("random",arguments.length,0);return Math.random()},rn.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=rn.size(e);if(1==n.length){if(0==e.length)throw new Error("Cannot calculate max of an empty vector");return X(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 J(e,n[0],n[1]);if(e instanceof t||e instanceof w)return new t(J(e.valueOf(),n[0],n[1]));throw N("max",e)}throw new RangeError("Cannot calculate max for multi dimensional matrix")}return X(arguments)},rn.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=rn.size(e);if(1==n.length){if(0==e.length)throw new Error("Cannot calculate min of an empty vector");return $(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 en(e,n[0],n[1]);if(e instanceof t||e instanceof w)return new t(en(e.valueOf(),n[0],n[1]));throw N("min",e)}throw new RangeError("Cannot calculate min for multi dimensional matrix")}return $(arguments)},rn.acos=function(e){if(1!=arguments.length)throw O("acos",arguments.length,1);if(p(e))return e>=-1&&1>=e?Math.acos(e):rn.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=rn.sqrt(r),a=new n(i.re-e.im,i.im+e.re),o=rn.log(a);return new n(1.5707963267948966-o.im,o.re)}if(e instanceof Array||e instanceof t)return an.map(e,rn.acos);if(e.valueOf()!==e)return rn.acos(e.valueOf());throw N("acos",e)},rn.asin=function(e){if(1!=arguments.length)throw O("asin",arguments.length,1);if(p(e))return e>=-1&&1>=e?Math.asin(e):rn.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=rn.sqrt(a),s=new n(o.re-i,o.im+r),f=rn.log(s);return new n(f.im,-f.re)}if(e instanceof Array||e instanceof t)return an.map(e,rn.asin);if(e.valueOf()!==e)return rn.asin(e.valueOf());throw N("asin",e)},rn.atan=function(e){if(1!=arguments.length)throw O("atan",arguments.length,1);if(p(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=rn.log(o);return new n(-.5*s.im,.5*s.re)}if(e instanceof Array||e instanceof t)return an.map(e,rn.atan);if(e.valueOf()!==e)return rn.atan(e.valueOf());throw N("atan",e)},rn.atan2=function(e,r){if(2!=arguments.length)throw O("atan2",arguments.length,2);if(p(e)){if(p(r))return Math.atan2(e,r)}else if(e instanceof n&&p(r))return Math.atan2(e.re,r);if(e instanceof Array||e instanceof t||r instanceof Array||r instanceof t)return an.map2(e,r,rn.atan2);if(r.valueOf()!==r||e.valueOf()!==e)return rn.atan2(e.valueOf(),r.valueOf());throw N("atan2",e,r)},rn.cos=function(e){if(1!=arguments.length)throw O("cos",arguments.length,1);if(p(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_QUANTITY.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.cos(e.value)}if(e instanceof Array||e instanceof t)return an.map(e,rn.cos);if(e.valueOf()!==e)return rn.cos(e.valueOf());throw N("cos",e)},rn.cot=function(e){if(1!=arguments.length)throw O("cot",arguments.length,1);if(p(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_QUANTITY.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 an.map(e,rn.cot);if(e.valueOf()!==e)return rn.cot(e.valueOf());throw N("cot",e)},rn.csc=function(e){if(1!=arguments.length)throw O("csc",arguments.length,1);if(p(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_QUANTITY.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 an.map(e,rn.csc);if(e.valueOf()!==e)return rn.csc(e.valueOf());throw N("csc",e)},rn.sec=function(e){if(1!=arguments.length)throw O("sec",arguments.length,1);if(p(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_QUANTITY.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 an.map(e,rn.sec);if(e.valueOf()!==e)return rn.sec(e.valueOf());throw N("sec",e)},rn.sin=function(e){if(1!=arguments.length)throw O("sin",arguments.length,1);if(p(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_QUANTITY.ANGLE))throw new TypeError("Unit in function cos is no angle");return Math.sin(e.value)}if(e instanceof Array||e instanceof t)return an.map(e,rn.sin);if(e.valueOf()!==e)return rn.sin(e.valueOf());throw N("sin",e)},rn.tan=function(e){if(1!=arguments.length)throw O("tan",arguments.length,1);if(p(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_QUANTITY.ANGLE))throw new TypeError("Unit in function tan is no angle");return Math.tan(e.value)}if(e instanceof Array||e instanceof t)return an.map(e,rn.tan);if(e.valueOf()!==e)return rn.tan(e.valueOf());throw N("tan",e)},rn["in"]=function(e,n){if(2!=arguments.length)throw O("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 an.map2(e,n,rn["in"]);if(e.valueOf()!==e||n.valueOf()!==n)return rn["in"](e.valueOf(),n.valueOf());throw N("in",e,n)},rn.clone=function(n){if(1!=arguments.length)throw O("clone",arguments.length,1);if(null==n)return n;if("function"==typeof n.clone)return n.clone();if(p(n)||y(n)||e(n))return n;if(n instanceof Array){var t=rn.clone;return n.map(function(e){return t(e)})}if(n instanceof Object)return an.mapObject(n,rn.clone);throw N("clone",n)},rn.eval=function(e,n){if(1!=arguments.length&&2!=arguments.length)throw O("eval",arguments.length,1,2);var r;if(r=n?n instanceof rn.expr.Scope?n:new rn.expr.Scope(n):new rn.expr.Scope,y(e)){var i=rn.parse(e,r);return i.eval()}if(e instanceof Array||e instanceof t)return an.map(e,function(e){var n=rn.parse(e,r);return n.eval()});throw new TypeError("String or matrix expected")},rn.format=function(e,n){var t=arguments.length;if(1!=t&&2!=t)throw O("format",t,1,2);if(1==t){var r=arguments[0];return p(r)?an.formatNumber(r,rn.options.precision):r instanceof Array?an.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})},rn["import"]=function(e,n){var t,r={override:!1,wrap:!0};if(n&&n instanceof Object&&an.extend(r,n),y(e)){if("undefined"==typeof require)throw new Error("Cannot load file: require not available.");var i=require(e);rn["import"](i)}else if(tn(e)){if(t=e.name,!t)throw new Error("Cannot import an unnamed function or object");(r.override||void 0===rn[t])&&nn(t,e,r)}else if(e instanceof Object)for(t in e)if(e.hasOwnProperty(t)){var a=e[t];tn(a)?nn(t,a,r):rn["import"](a)}},function(){function e(){J++,$=X.charAt(J)}function r(){J=0,$=X.charAt(0)}function i(){for(nn=Z.NULL,en="";" "==$||" "==$;)e();if("#"==$)for(;"\n"!=$&&""!=$;)e();if(""==$)return nn=Z.DELIMITER,void 0;if("!"==$)return nn=Z.DELIMITER,en+=$,e(),"="==$&&(en+=$,e()),void 0;if("-"==$||","==$||"("==$||")"==$||"["==$||"]"==$||'"'==$||"'"==$||"\n"==$||";"==$||":"==$)return nn=Z.DELIMITER,en+=$,e(),void 0;if(a($))for(nn=Z.DELIMITER;a($);)en+=$,e();else if(s($)){if(nn=Z.NUMBER,"."==$)en+=$,e(),f($)||(nn=Z.UNKNOWN);else{for(;f($);)en+=$,e();"."==$&&(en+=$,e())}for(;f($);)en+=$,e();if("E"==$||"e"==$)for(en+=$,e(),("+"==$||"-"==$)&&(en+=$,e()),f($)||(nn=Z.UNKNOWN);f($);)en+=$,e()}else{if(!o($)){for(nn=Z.UNKNOWN;""!=$;)en+=$,e();throw Q('Syntax error in part "'+en+'"')}for(nn=Z.SYMBOL;o($)||f($);)en+=$,e()}}function a(e){return"&"==e||"|"==e||"<"==e||">"==e||"="==e||"+"==e||"/"==e||"*"==e||"%"==e||"^"==e||","==e||";"==e||"\n"==e||"!"==e||"'"==e}function o(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e||"_"==e}function s(e){return e>="0"&&"9">=e||"."==e}function f(e){return e>="0"&&"9">=e}function u(e){r(),i();var n;if(n=""==en?new M(void 0):c(e),""!=en)throw nn==Z.DELIMITER?K("Unknown operator "+en):Q('Unexpected part "'+en+'"');return n}function c(e){var n,t,r;for("\n"!=en&&";"!=en&&""!=en&&(n=l(e));"\n"==en||";"==en;)t||(t=new U,n&&(r=";"!=en,t.add(n,r))),i(),"\n"!=en&&";"!=en&&""!=en&&(n=l(e),r=";"!=en,t.add(n,r));return t?t:(n||(n=l(e)),n)}function l(e){var n=h(e),t="ans";return new z(t,n,e)}function h(e){if(nn==Z.SYMBOL&&"function"==en){if(i(),nn!=Z.SYMBOL)throw Q("Function name expected");var n=en;if(i(),"("!=en)throw Q("Opening parenthesis ( expected");for(var t=e.createSubScope(),r=[];;){if(i(),nn!=Z.SYMBOL)throw Q("Variable name expected");if(r.push(en),i(),","!=en){if(")"==en)break;throw Q('Comma , or closing parenthesis ) expected"')}}if(i(),"="!=en)throw Q("Equal sign = expected");i();var a=m(t);return new _(n,r,a,t,e)}return m(e)}function m(e){var n,t,r,a,o=p(e);if("="==en){if(o instanceof S)return i(),n=o.name,t=null,a=m(e),new z(n,a,e);if(o instanceof T&&o.object instanceof S)return i(),n=o.object.name,t=o.params,r=o.paramScopes,a=m(e),new L(n,t,r,a,e);throw Q("Symbol expected at the left hand side of assignment operator =")}return o}function p(e){var n,t,r,a=[];if(n=":"==en?new M(1):v(e),":"==en){for(a.push(n);":"==en;)i(),")"==en||","==en||""==en?a.push(new S("end",e)):a.push(v(e));a.length&&(t="range",r=rn.range,n=new A(t,r,a))}return n}function v(e){var n,t,r,a,o;for(n=g(e),t={"in":"in"};void 0!==t[en];)r=en,a=rn[t[r]],i(),o=[n,g(e)],n=new A(r,a,o);return n}function g(e){var n=w(e);return n}function w(e){var n,t,r,a,o;for(n=d(e),t={"==":"equal","!=":"unequal","<":"smaller",">":"larger","<=":"smallereq",">=":"largereq"};void 0!==t[en];)r=en,a=rn[t[r]],i(),o=[n,d(e)],n=new A(r,a,o);return n}function d(e){var n,t,r,a,o;for(n=x(e),t={"+":"add","-":"subtract"};void 0!==t[en];)r=en,a=rn[t[r]],i(),o=[n,x(e)],n=new A(r,a,o);return n}function x(e){var n,t,r,a,o;for(n=N(e),t={"*":"multiply","/":"divide","%":"mod",mod:"mod"};void 0!==t[en];)r=en,a=rn[t[r]],i(),o=[n,N(e)],n=new A(r,a,o);return n}function N(e){var n,t,r;return"-"==en?(n=en,t=rn.unaryminus,i(),r=[b(e)],new A(n,t,r)):b(e)}function b(e){var n,t,r,a,o,s;for(r=[I(e)];"^"==en;)i(),r.push(I(e));for(n=r.pop();r.length;)t=r.pop(),a="^",o=rn.pow,s=[t,n],n=new A(a,o,s);return n}function I(e){var n,t,r,a;for(n=q(e);"!"==en;)t=en,r=rn.factorial,i(),a=[n],n=new A(t,r,a);return n}function q(e){var n,t,r,a;for(n=P(e);"'"==en;)t=en,r=rn.transpose,i(),a=[n],n=new A(t,r,a);return n}function P(e){return C(e)}function C(e){var n,t;return nn==Z.SYMBOL?(t=en,i(),n=new S(t,e),B(e,n)):G(e)}function B(e,n){for(var t,r,a;"("==en;){if(t=[],r=[],i(),")"!=en)for(a=e.createSubScope(),r.push(a),t.push(p(a));","==en;)i(),a=e.createSubScope(),r.push(a),t.push(p(a));if(")"!=en)throw Q("Parenthesis ) missing");i(),n=new T(n,t,r)}return n}function G(n){var t,r,a;if('"'==en){for(r="",a="";""!=$&&('"'!=$||"\\"==a);)r+=$,a=$,e();if(i(),'"'!=en)throw Q('End of string " missing');return i(),t=new M(r),t=B(n,t)}return F(n)}function F(e){var n,t,r,a,o,s;if("["==en){for(i();"\n"==en;)i();if("]"!=en){for(t=[],r=0,a=0,t[0]=[m(e)];","==en||";"==en;){for(","==en?a++:(r++,a=0,t[r]=[]),i();"\n"==en;)i();for(t[r][a]=m(e);"\n"==en;)i()}for(o=t.length,s=t.length>0?t[0].length:0,r=1;o>r;r++)if(t[r].length!=s)throw K("Number of columns must match ("+t[r].length+" != "+s+")");if("]"!=en)throw Q("End of matrix ] missing");i(),n=new R(t)}else i(),n=new R([[]]);return n=B(e,n)}return H(e)}function H(e){var t,r,a;if(nn==Z.NUMBER){if(a="."==en?0:Number(en),i(),nn==Z.SYMBOL){if("i"==en||"I"==en)return r=new n(0,a),i(),new M(r);if(E.isPlainUnit(en))return r=new E(a,en),i(),new M(r);throw W('Unknown unit "'+en+'"')}return t=new M(a),t=B(e,t)}return j(e)}function j(e){var n;if("("==en){if(i(),n=m(e),")"!=en)throw Q("Parenthesis ) expected");return i(),n=B(e,n)}return D(e)}function D(){throw""==en?Q("Unexpected end of expression"):Q("Value expected")}function V(){return void 0}function Y(){return J-en.length+1}function k(e){var n=V(),t=Y();return void 0===n?void 0===t?e:e+" (char "+t+")":e+" (line "+n+", char "+t+")"}function Q(e){return new SyntaxError(k(e))}function W(e){return new TypeError(k(e))}function K(e){return new Error(k(e))}rn.parse=function(e,n){if(1!=arguments.length&&2!=arguments.length)throw O("parse",arguments.length,1,2);var r;if(r=n?n instanceof rn.expr.Scope?n:new rn.expr.Scope(n):new rn.expr.Scope,y(e))return X=e||"",u(r);if(e instanceof Array||e instanceof t)return an.map(e,function(e){return X=e||"",u(r)});throw new TypeError("String or matrix expected")};var Z={NULL:0,DELIMITER:1,NUMBER:2,SYMBOL:3,UNKNOWN:4},X="",J=0,$="",en="",nn=Z.NULL}(),rn.select=function(e){return new rn.type.Selector(e)},rn["typeof"]=function(e){if(1!=arguments.length)throw O("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 rn)if(rn.hasOwnProperty(n)&&e.constructor==rn[n])return n.toLowerCase();for(n in rn.type)if(rn.type.hasOwnProperty(n)&&e.constructor==rn.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 fn in rn)rn.hasOwnProperty(fn)&&fn&&d(fn,rn[fn])}();