mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
29 lines
81 KiB
JavaScript
29 lines
81 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.7.2-SNAPSHOT
|
|
* @date 2013-04-27
|
|
*
|
|
* @license
|
|
* Copyright (C) 2013 Jos de Jong <wjosdejong@gmail.com>
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
* use this file except in compliance with the License. You may obtain a copy
|
|
* of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
* License for the specific language governing permissions and limitations under
|
|
* the License.
|
|
*/
|
|
(function(){function e(e){return e instanceof Boolean||"boolean"==typeof e}function t(e,n){if(!(this instanceof t))throw new SyntaxError("Complex constructor must be called with the new operator");if(null!=e&&!v(e)||null!=n&&!v(n))throw new TypeError("Two numbers or a single string expected in Complex constructor");this.re=e||0,this.im=n||0}function n(e){if(!(this instanceof n))throw new SyntaxError("Matrix constructor must be called with the new operator");if(e instanceof n||e instanceof d)this._data=e.toArray();else if(e instanceof Array)this._data=e;else{if(null!=e)throw new TypeError("Unsupported type of data ("+K["typeof"](e)+")");this._data=[]}this._size=Z.size(this._data)}function r(e,t){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(t&&e>t)throw new RangeError("Index out of range ("+e+" > "+t+")")}function i(e,t){return r(t,e.length),e[t-1]}function a(e,t){return t.forEach(function(t){e=i(e,t)}),K.clone(e)}function o(e,t){var n=t[0];return n.map?n.map(function(t){return i(e,t)}):[i(e,n)]}function f(e,t){var n=t[0],r=t[1];if(n.map)return r.map?n.map(function(t){var n=i(e,t);return r.map(function(e){return i(n,e)})}):n.map(function(t){return[i(i(e,t),r)]});if(r.map){var a=i(e,n);return[r.map(function(e){return i(a,e)})]}return[[i(i(e,n),r)]]}function s(e,t,n){var r=n==t.length-1,a=t[n],o=function(a){var o=i(e,a);return r?o:s(o,t,n+1)};return a.map?a.map(o):[o(a)]}function u(e,t,n){if(r(t),n instanceof Array)throw new TypeError("Dimension mismatch, value expected instead of array");e[t-1]=n}function c(e,t,n,i){var a=!1;n.length>t.length&&(a=!0);for(var o=0;n.length>o;o++){var f=n[o];r(f),(null==t[o]||f>t[o])&&(t[o]=f,a=!0)}a&&Z.resize(e,t,0);var s=t.length;n.forEach(function(t,n){s-1>n?e=e[t-1]:e[t-1]=i})}function l(e,t,n,i){var a=n[0];r(a),a>t[0]&&Z.resize(e,[a],0),e[a-1]=i}function h(e,t,n,i){var a=n[0],o=n[1];r(a),r(o);var f=!1;a>(t[0]||0)&&(t[0]=a,f=!0),o>(t[1]||0)&&(t[1]=o,f=!0),f&&Z.resize(e,t,0),e[a-1][o-1]=i}function m(e,t,n,r,i){var a=r==n.length-1,o=n[r],f=function(o,f){if(a)u(e,o,i[f]),e.length>(t[r]||0)&&(t[r]=e.length);else{var s=e[o-1];s instanceof Array||(e[o-1]=s=[s],e.length>(t[r]||0)&&(t[r]=e.length)),m(s,t,n,r+1,i[f])}};if(o.map){var s=o.size&&o.size()||o.length;if(s!=i.length)throw new RangeError("Dimensions mismatch ("+s+" != "+i.length+")");o.map(f)}else f(o,0)}function p(e){for(var t=0,n=e.length;n>t;t++){var r=e[t];r instanceof Array?p(r):void 0==r&&(e[t]=0)}}function v(e){return e instanceof Number||"number"==typeof e}function g(e){return e==Math.round(e)}function d(e,t,n){if(!(this instanceof d))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!=n&&!v(n))throw new TypeError("Parameter end must be a number");if(null!=t&&!v(t))throw new TypeError("Parameter step must be a number");this.start=null!=e?e:0,this.end=null!=n?n:0,this.step=null!=t?t:1}function w(e,t){var n=K.type.Selector,r=Array.prototype.slice;n.prototype[e]="function"==typeof t?function(){var e=[this.value].concat(r.call(arguments,0));return new n(t.apply(this,e))}:new n(t)}function y(e){return e instanceof String||"string"==typeof e}function E(e,t){if(!(this instanceof E))throw Error("Unit constructor must be called with the new operator");if(null!=e&&!v(e))throw Error("First parameter in Unit constructor must be a number");if(null!=t&&!y(t))throw Error("Second parameter in Unit constructor must be a string");if(null!=t){var n=N(t);if(!n)throw Error('String "'+t+'" is no unit');this.unit=n.unit,this.prefix=n.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 t=E.UNITS,n=0,r=t.length;r>n;n++){var i=t[n];if(Z.endsWith(e,i.name)){var a=e.length-i.name.length,o=e.substring(0,a),f=i.prefixes[o];if(void 0!==f)return{unit:i,prefix:f}}}return null}function O(e,t){var n=void 0;if(2==arguments.length){var r=K["typeof"](t);n="Function "+e+"("+r+") not supported"}else if(arguments.length>2){for(var i=[],a=1;arguments.length>a;a++)i.push(K["typeof"](arguments[a]));n="Function "+e+"("+i.join(", ")+") not supported"}else n="Unsupported parameter in function "+e;return new TypeError(n)}function x(e,t,n,r){var i="Wrong number of arguments in function "+e+" ("+t+" provided, "+n+(void 0!=r?"-"+r:"")+" expected)";return new SyntaxError(i)}function b(){}function M(e){this.value=e}function A(e,t,n){this.name=e,this.fn=t,this.params=n}function S(e,t){this.name=e,this.symbol=t}function T(e,t){this.object=e,this.params=t}function U(e){this.nodes=e||[]}function L(){this.params=[],this.visible=[]}function R(e,t,n,r){this.name=e,this.params=t,this.expr=n,this.symbol=r}function _(e,t,n,r,i){this.name=e,this.variables=n,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,t,n,r),this.symbol=i}function z(e,n){var r=n.re*n.re+n.im*n.im;return new t((e.re*n.re+e.im*n.im)/r,(e.im*n.re-e.re*n.im)/r)}function q(e,n){return new t(e.re*n.re-e.im*n.im,e.re*n.im+e.im*n.re)}function I(e,t){var n=K.log(e),r=K.multiply(n,t);return K.exp(r)}function P(e,t){if(t){var n=Math.pow(10,t);return Math.round(e*n)/n}return Math.round(e)}function B(e,t,n,r){if(n>r){if(e.length!=t.length)throw Error("Dimensions mismatch ("+e.length+" != "+t.length+")");for(var i=[],a=0;e.length>a;a++)i[a]=B(e[a],t[a],n,r+1);return i}return e.concat(t)}function C(e,t,n){var r=K.multiply,i=K.subtract;if(1==t)return e[0][0];if(2==t)return i(r(e[0][0],e[1][1]),r(e[1][0],e[0][1]));for(var a=0,o=0;n>o;o++){var f=G(e,t,n,0,o);a+=r(r((o+1)%2+(o+1)%2-1,e[0][o]),C(f,t-1,n-1))}return a}function G(e,t,n,r,i){for(var a,o=[],f=0;t>f;f++)if(f!=r){a=o[f-(f>r)]=[];for(var s=0;n>s;s++)s!=i&&(a[s-(s>i)]=e[f][s])}return o}function D(e,t,n){var r,i,a,o,f,s=K.add,u=K.unaryminus,c=K.multiply,l=K.divide;if(1==t){if(o=e[0][0],0==o)throw Error("Cannot calculate inverse, determinant is zero");return[[l(1,o)]]}if(2==t){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 m=e.concat();for(r=0;t>r;r++)m[r]=m[r].concat();for(var p=K.eye(t).valueOf(),v=0;n>v;v++){for(r=v;t>r&&0==m[r][v];)r++;if(r==t||0==m[r][v])throw Error("Cannot calculate inverse, determinant is zero");r!=v&&(f=m[v],m[v]=m[r],m[r]=f,f=p[v],p[v]=p[r],p[r]=f);var g=m[v],d=p[v];for(r=0;t>r;r++){var w=m[r],y=p[r];if(r!=v){if(0!=w[v]){for(a=l(u(w[v]),g[v]),i=v;n>i;i++)w[i]=s(w[i],c(a,g[i]));for(i=0;n>i;i++)y[i]=s(y[i],c(a,d[i]))}}else{for(a=g[v],i=v;n>i;i++)w[i]=l(w[i],a);for(i=0;n>i;i++)y[i]=l(y[i],a)}}}return p}function V(e){if(1==e.length)return V(e[0]);for(var t=0,n=e.length;n>t;t++){var r=e[t];r instanceof Array&&(e[t]=V(r))}return e}function k(e){for(var t=K.larger,n=e[0],r=1,i=e.length;i>r;r++){var a=e[r];t(a,n)&&(n=a)}return n}function F(e,t,n){for(var r=K.larger,i=[],a=0;n>a;a++){for(var o=e[0][a],f=1;t>f;f++){var s=e[f][a];r(s,o)&&(o=s)}i[a]=o}return i}function j(e){for(var t=K.smaller,n=e[0],r=1,i=e.length;i>r;r++){var a=e[r];t(a,n)&&(n=a)}return n}function H(e,t,n){for(var r=K.smaller,i=[],a=0;n>a;a++){for(var o=e[0][a],f=1;t>f;f++){var s=e[f][a];r(s,o)&&(o=s)}i[a]=o}return i}function Y(e,t){K[e]=t,w(e,t)}function W(e){return"function"==typeof e||v(e)||y(e)||e instanceof t||e instanceof E}var K={type:{},expr:{node:{}},options:{precision:5}};"undefined"!=typeof module&&module.exports!==void 0&&(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(t){if(t instanceof Array){var n=t.length;if(n){var r=e(t[0]);return 0==r[0]?[0].concat(r):[n].concat(r)}return[n]}return[]}function t(e,n,r){var i,a=e.length;if(a!=n[r])throw new RangeError("Dimension mismatch ("+a+" != "+n[r]+")");if(n.length-1>r){var o=r+1;for(i=0;a>i;i++){var f=e[i];if(!(f instanceof Array))throw new RangeError("Dimension mismatch ("+(n.length-1)+" < "+n.length+")");t(e[i],n,o)}}else for(i=0;a>i;i++)if(e[i]instanceof Array)throw new RangeError("Dimension mismatch ("+(n.length+1)+" > "+n.length+")")}function r(e,t,n){if(t.length-1>n){var i=e[0];if(1!=e.length||!(i instanceof Array))throw new RangeError("Dimension mismatch ("+e.length+" > 0)");r(i,t,n+1)}else if(e.length)throw new RangeError("Dimension mismatch ("+e.length+" > 0)")}function i(e,t,n,r){if(!(e instanceof Array))throw new TypeError("Array expected");var a=e.length,o=t[n];if(a!=o){if(o>e.length)for(var f=e.length;o>f;f++)e[f]=r?K.clone(r):0;else e.length=t[n];a=e.length}if(t.length-1>n){var s=n+1;for(f=0;a>f;f++)u=e[f],u instanceof Array||(u=[u],e[f]=u),i(u,t,s,r)}else for(f=0;a>f;f++){for(var u=e[f];u instanceof Array;)u=u[0];e[f]=u}}var a={};a.formatNumber=function(e,t){if(1/0===e)return"Infinity";if(e===-1/0)return"-Infinity";if(0/0===e)return"NaN";var n=Math.abs(e);if(n>.001&&1e5>n||0==n)return a.toPrecision(e,t);var r=Math.round(Math.log(n)/Math.LN10),i=e/Math.pow(10,r);return a.toPrecision(i,t)+"e"+r},a.toPrecision=function(e,t){return void 0==t&&(t=K.options.precision),e.toPrecision(t).replace(o,function(e,t,n){return e.substring(0,e.length-(t.length?0:1)-n.length)})};var o=/\.(\d*?)(0+)$/g;return a.formatArray=function(e){if(e instanceof Array){for(var t="[",n=e.length,r=0;n>r;r++)0!=r&&(t+=", "),t+=a.formatArray(e[r]);return t+="]"}return K.format(e)},a.formatArray2d=function(e){var t="[",n=a.size(e);if(2!=n.length)throw new RangeError("Array must be two dimensional (size: "+a.formatArray(n)+")");for(var r=n[0],i=n[1],o=0;r>o;o++){0!=o&&(t+="; ");for(var f=e[o],s=0;i>s;s++){0!=s&&(t+=", ");var u=f[s];void 0!=u&&(t+=K.format(u))}}return t+="]"},a.argsToArray=function(e){var t;if(0==e.length)t=[];else if(1==e.length)t=e[0],t instanceof n&&(t=t.toVector()),t instanceof d&&(t=t.valueOf()),t instanceof Array||(t=[t]);else{t=[];for(var r=0;e.length>r;r++)t[r]=e[r]}return t},a.endsWith=function(e,t){var n=e.length-t.length,r=e.length;return e.substring(n,r)===t},a.extend=function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);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,t){if(e instanceof Array||e instanceof n||e instanceof d)return e.map(function(e){return t(e)});throw new TypeError("Array expected")},a.map2=function(e,t,r){var i,o,f;if(e instanceof n||t instanceof n)return new n(a.map2(e.valueOf(),t.valueOf(),r));if(e instanceof d||t instanceof d)return a.map2(e.valueOf(),t.valueOf(),r);if(e instanceof Array)if(t instanceof Array){if(e.length!=t.length)throw new RangeError("Dimension mismatch ("+e.length+" != "+t.length+")");for(i=[],o=e.length,f=0;o>f;f++)i[f]=r(e[f],t[f])}else for(i=[],o=e.length,f=0;o>f;f++)i[f]=r(e[f],t);else if(t instanceof Array)for(i=[],o=t.length,f=0;o>f;f++)i[f]=r(e,t[f]);else i=r(e,t);return i},a.forEach=function(e,t){if(e instanceof Array)e.forEach(t);else for(var n in e)e.hasOwnProperty(n)&&t(e[n],n,e)},a.mapObject=function(e,t){var n={};for(var r in e)e.hasOwnProperty(r)&&(n[r]=t(e[r]));return n},a.deepEqual=function(e,t){var n,r,i;if(e instanceof Array){if(!(t instanceof Array))return!1;for(r=0,i=e.length;i>r;r++)if(!a.deepEqual(e[r],t[r]))return!1;return!0}if(e instanceof Object){if(t instanceof Array||!(t instanceof Object))return!1;for(n in e)if(e.hasOwnProperty(n)&&!a.deepEqual(e[n],t[n]))return!1;for(n in t)if(t.hasOwnProperty(n)&&!a.deepEqual(e[n],t[n]))return!1;return!0}return e.valueOf()==t.valueOf()},a.size=function(t){var n=e(t);return a.validate(t,n),n},a.validate=function(e,n){var i=0==n.length;if(i){if(e instanceof Array)throw new RangeError("Dimension mismatch ("+e.length+" != 0)")}else{var o=-1!=n.indexOf(0);o?(n.forEach(function(e){if(0!=e)throw new RangeError("Invalid size, all dimensions must be either zero or non-zero (size: "+a.formatArray(n)+")")}),r(e,n,0)):t(e,n,0)}},a.resize=function(e,t,n){if(!(t instanceof Array))throw new TypeError("Size must be an array (size is "+K["typeof"](t)+")");t.forEach(function(e){if(!v(e)||!g(e)||0>e)throw new TypeError("Invalid size, must contain positive integers (size: "+a.formatArray(t)+")")});var r=-1!=t.indexOf(0);r&&t.forEach(function(e){if(0!=e)throw new RangeError("Invalid size, all dimensions must be either zero or non-zero (size: "+a.formatArray(t)+")")}),i(e,t,0,n)},Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var t=0;this.length>t;t++)if(this[t]==e)return t;return-1}),Array.prototype.forEach||(Array.prototype.forEach=function(e,t){for(var n=0,r=this.length;r>n;++n)e.call(t||this,this[n],n,this)}),Array.prototype.map||(Array.prototype.map=function(e,t){var n,r,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(t&&(n=t),r=Array(o),i=0;o>i;){var f,s;i in a&&(f=a[i],s=e.call(n,f,i,a),r[i]=s),i++}return r}),Array.prototype.every||(Array.prototype.every=function(e){"use strict";if(null==this)throw new TypeError;var t=Object(this),n=t.length>>>0;if("function"!=typeof e)throw new TypeError;for(var r=arguments[1],i=0;n>i;i++)if(i in t&&!e.call(r,t[i],i,t))return!1;return!0}),Array.prototype.some||(Array.prototype.some=function(e){"use strict";if(null==this)throw new TypeError;var t=Object(this),n=t.length>>>0;if("function"!=typeof e)throw new TypeError;for(var r=arguments[1],i=0;n>i;i++)if(i in t&&e.call(r,t[i],i,t))return!0;return!1}),a}();K.type.Complex=t,function(){function e(){for(;" "==c||" "==c;)i()}function n(e){return e>="0"&&"9">=e||"."==e}function r(e){return e>="0"&&"9">=e}function i(){u++,c=s.charAt(u)}function a(e){u=e,c=s.charAt(u)}function o(){var e,t="";if(e=u,"+"==c?i():"-"==c&&(t+=c,i()),!n(c))return a(e),null;if("."==c){if(t+=c,i(),!r(c))return a(e),null}else{for(;r(c);)t+=c,i();"."==c&&(t+=c,i())}for(;r(c);)t+=c,i();if("E"==c||"e"==c){if(t+=c,i(),("+"==c||"-"==c)&&(t+=c,i()),!r(c))return a(e),null;for(;r(c);)t+=c,i()}return t}function f(){var e=s.charAt(u+1);if("I"==c||"i"==c)return i(),"1";if(!("+"!=c&&"-"!=c||"I"!=e&&"i"!=e)){var t="+"==c?"1":"-1";return i(),i(),t}return null}var s,u,c;t.parse=function(n){if(s=n,u=-1,c="",!y(s))return null;i(),e();var r=o();if(r){if("I"==c||"i"==c)return i(),e(),c?null:new t(0,Number(r));e();var a=c;if("+"!=a&&"-"!=a)return e(),c?null:new t(Number(r),0);i(),e();var l=o();if(l){if("I"!=c&&"i"!=c)return null;i()}else if(l=f(),!l)return null;return"-"==a&&(l="-"==l[0]?"+"+l.substring(1):"-"+l),i(),e(),c?null:new t(Number(r),Number(l))}return(r=f())?(e(),c?null:new t(0,Number(r))):null}}(),t.prototype.clone=function(){return new t(this.re,this.im)},t.prototype.toString=function(){var e="",t=Z.formatNumber(this.re),n=Z.formatNumber(this.im);return e=0==this.im?t:0==this.re?1==this.im?"i":-1==this.im?"-i":n+"i":this.im>0?1==this.im?t+" + i":t+" + "+n+"i":-1==this.im?t+" - i":t+" - "+Z.formatNumber(Math.abs(this.im))+"i"},K.type.Matrix=n,n.prototype.get=function(e){var t;if(e instanceof n)t=e.isVector(),e=e.valueOf();else{if(!(e instanceof Array))throw new TypeError("Unsupported type of index "+K["typeof"](e));t=!e.some(function(e){return e.forEach})}if(e.length!=this._size.length)throw new RangeError("Dimension mismatch ("+e.length+" != "+this._size.length+")");if(t)switch(e.length){case 1:return 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 n(o(this._data,e));case 2:return new n(f(this._data,e));default:return new n(s(this._data,e,0))}},n.prototype.set=function(e,t){var r;if(e instanceof n)r=e.isVector(),e=e.valueOf();else{if(!(e instanceof Array))throw new TypeError("Unsupported type of index "+K["typeof"](e));r=!e.some(function(e){return e.forEach})}if((t instanceof n||t instanceof d)&&(t=t.valueOf()),e.length<this._size.length)throw new RangeError("Dimension mismatch ("+e.length+" != "+this._size.length+")");if(r){if(0!=K.size(t).length)throw new TypeError("Scalar value expected");switch(e.length){case 1:l(this._data,this._size,e,t);break;case 2:h(this._data,this._size,e,t);break;default:c(this._data,this._size,e,t)}}else{var i=this._size.concat();m(this._data,i,e,0,t),Z.deepEqual(this._size,i)||(p(this._data),this.resize(i))}return this},n.prototype.resize=function(e,t){Z.resize(this._data,e,t),this._size=K.clone(e)},n.prototype.clone=function(){var e=new n;return e._data=K.clone(this._data),e._size=K.clone(this._size),e},n.prototype.size=function(){return this._size},n.prototype.map=function(e){var t=this,r=new n,i=[],a=function(n,r){return n instanceof Array?n.map(function(e,t){return i[r]=t+1,a(e,r+1)}):e(n,i,t)};return r._data=a(this._data,0),r._size=K.clone(this._size),r},n.prototype.forEach=function(e){var t=this,n=[],r=function(i,a){i instanceof Array?i.forEach(function(e,t){n[a]=t+1,r(e,a+1)}):e(i,n,t)};r(this._data,0)},n.prototype.toScalar=function(){for(var e=this._data;e instanceof Array&&1==e.length;)e=e[0];return e instanceof Array?null:K.clone(e)},n.prototype.isScalar=function(){return this._size.every(function(e){return 1>=e})},n.prototype.toVector=function(){var e=0,t=void 0,n=[];if(this._size.forEach(function(r,i){r>1&&(e++,t=i),n[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},n.prototype.isVector=function(){var e=0;return this._size.forEach(function(t){t>1&&e++}),1>=e},n.prototype.toArray=function(){return K.clone(this._data)},n.prototype.valueOf=function(){return this._data},n.prototype.toString=function(){return K.format(this._data)},K.type.Range=d,d.parse=function(e){if(!y(e))return null;var t=e.split(":"),n=t.map(function(e){return Number(e)}),r=n.some(function(e){return isNaN(e)});if(r)return null;switch(n.length){case 2:return new d(n[0],1,n[1]);case 3:return new d(n[0],n[1],n[2]);default:return null}},d.prototype.clone=function(){return new d(this.start,this.step,this.end)},d.prototype.size=function(){var e=0,t=Number(this.start),n=Number(this.step),r=Number(this.end),i=r-t;return K.sign(n)==K.sign(i)?e=Math.floor(i/n)+1:0==i&&(e=1),isNaN(e)&&(e=0),[e]},d.prototype.forEach=function(e){var t=Number(this.start),n=Number(this.step),r=Number(this.end),i=0;if(n>0)for(;r>=t;)e(t,i,this),t+=n,i++;else if(0>n)for(;t>=r;)e(t,i,this),t+=n,i++},d.prototype.map=function(e){var t=[];return this.forEach(function(n,r,i){t[r]=e(n,r,i)}),t},d.prototype.toMatrix=function(){return new n(this.toArray())},d.prototype.toArray=function(){var e=[];return this.forEach(function(t,n){e[n]=t}),e},d.prototype.toVector=d.prototype.toArray,d.prototype.isVector=function(){return!0},d.prototype.toScalar=function(){var e=this.toArray();return 1==e.length?e[0]:null},d.prototype.isScalar=function(){return 1==this.size()[0]},d.prototype.valueOf=function(){return this.toArray()},d.prototype.toString=function(){var e=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 n(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 n(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 t(e){return e>="0"&&"9">=e||"."==e}function n(e){return e>="0"&&"9">=e}function r(){s++,u=f.charAt(s)}function i(e){s=e,u=f.charAt(s)}function a(){var e,a="";if(e=s,"+"==u?r():"-"==u&&(a+=u,r()),!t(u))return i(e),null;if("."==u){if(a+=u,r(),!n(u))return i(e),null}else{for(;n(u);)a+=u,r();"."==u&&(a+=u,r())}for(;n(u);)a+=u,r();if("E"==u||"e"==u){if(a+=u,r(),("+"==u||"-"==u)&&(a+=u,r()),!n(u))return i(e),null;for(;n(u);)a+=u,r()}return a}function o(){var t="";for(e();u&&" "!=u&&" "!=u;)t+=u,r();return t||null}var f,s,u;E.parse=function(t){if(f=t,s=-1,u="",!y(f))return null;r(),e();var n,i=a();return i?(n=o(),r(),e(),u?null:i&&n?new E(Number(i),n):null):(n=o(),r(),e(),u?null:new E(null,n))}}(),E.prototype.clone=function(){var e=new E;for(var t in this)this.hasOwnProperty(t)&&(e[t]=this[t]);return e},E.prototype._normalize=function(e){return(e+this.unit.offset)*this.unit.value*this.prefix.value},E.prototype._unnormalize=function(e,t){return void 0==t?e/this.unit.value/this.prefix.value-this.unit.offset:e/this.unit.value/t-this.unit.offset},E.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 t;if(y(e)){if(t=new E(null,e),!this.equalBase(t))throw Error("Units do not match");return t.value=this.value,t}if(e instanceof E){if(!this.equalBase(e))throw Error("Units do not match");if(null!=e.value)throw Error("Cannot convert to a unit with a value");if(null==e.unit)throw Error("Unit expected on the right hand side of function in");return t=e.clone(),t.value=this.value,t.fixPrefix=!0,t}throw Error("String or Unit expected as parameter")},E.prototype.toNumber=function(e){var t=this["in"](e),n=this.fixPrefix?t._bestPrefix():t.prefix;return t._unnormalize(t.value,n.value)},E.prototype.toString=function(){var e,t;if(this.fixPrefix)e=this._unnormalize(this.value),t=null!=this.value?Z.formatNumber(e)+" ":"",t+=this.prefix.name+this.unit.name;else{var n=this._bestPrefix();e=this._unnormalize(this.value,n.value),t=null!=this.value?Z.formatNumber(e)+" ":"",t+=n.name+this.unit.name}return t},E.prototype._bestPrefix=function(){var e=Math.abs(this.value/this.unit.value),t=E.PREFIX_NONE,n=Math.abs(Math.log(e/t.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);n>o&&(t=a,n=o)}}return t},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 t(0,1),K.pi=K.PI,K.e=K.E,K.i=K.I,K.expr.node.Node=b,b.prototype.eval=function(){throw 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]);switch(e.length){case 1:return""+e[0]+this.name;case 2:var t=""+e[0];e[0]instanceof A&&(t="("+t+")");var n=""+e[1];return e[1]instanceof A&&(n="("+n+")"),t+" "+this.name+" "+n;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 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=this.object;if(void 0==e)throw Error("Node undefined");var t=e.eval(),n=this.params.map(function(e){return e.eval()});if("function"==typeof t)return t.apply(this,n);if(t instanceof Object&&t.get)return t.get(n);throw new TypeError("Cannot apply parameters to object of type "+K["typeof"](t))},T.prototype.toString=function(){var e=this.object?""+this.object:"";return this.params&&(e+="("+this.params.join(", ")+")"),e},U.prototype=new b,K.expr.node.MatrixNode=U,function(){function e(t){return t.map(function(t){return t instanceof Array?e(t):t.eval()})}function t(e){for(var t=[],r=e.length,i=0;r>i;i++){for(var a=e[i],o=a.length,f=null,s=null,u=0;o>u;u++){var c,l=K.clone(a[u]);if(l instanceof n){if(c=l.size(),l=l.valueOf(),1==c.length)l=[l],c=[1,c[0]];else if(c.length>2)throw Error("Cannot merge a multi dimensional matrix")}else l instanceof d?(l=[l.valueOf()],c=[1,l[0].length]):(c=[1,1],l=[[l]]);if(null==f)f=l,s=c[0];else{if(c[0]!=s)throw Error("Dimension mismatch ("+c[0]+" != "+s+")");for(var h=0;s>h;h++)f[h]=f[h].concat(l[h])}}t=t.concat(f)}return t}function r(e){return e.some(function(e){return e instanceof n||e instanceof d?!0:e instanceof Array?r(e):!1})}U.prototype.eval=function(){var i=e(this.nodes);return r(i)&&(i=t(i)),new n(i)},U.prototype.toString=function(){return Z.formatArray(this.nodes)}}(),L.prototype=new b,K.expr.node.BlockNode=L,L.prototype.add=function(e,t){var n=this.params.length;this.params[n]=e,this.visible[n]=void 0!=t?t:!0},L.prototype.eval=function(){for(var e=[],t=0,n=this.params.length;n>t;t++){var r=this.params[t].eval();this.visible[t]&&e.push(r)}return e},L.prototype.toString=function(){for(var e=[],t=0,n=this.params.length;n>t;t++)this.visible[t]&&e.push("\n "+(""+this.params[t]));return"["+e.join(",")+"\n]"},R.prototype=new b,K.expr.node.AssignmentNode=R,R.prototype.eval=function(){if(void 0===this.expr)throw Error("Undefined symbol "+this.name);var e,t=this.params;if(t&&t.length){var n=[];this.params.forEach(function(e){n.push(e.eval())});var r=this.expr.eval(),i=this.symbol.get();if(void 0==i)throw Error("Undefined symbol "+this.name);if(!i.set)throw new TypeError("Cannot apply a subset to object of type "+K["typeof"](i));e=i.set(n,r),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},_.prototype=new b,K.expr.node.FunctionNode=_,_.prototype.createFunction=function(e,t,n,r){var i=function(){var t=n?n.length:0,i=arguments?arguments.length:0;if(t!=i)throw x(e,i,t);if(t>0)for(var a=0;t>a;a++)n[a].value=arguments[a];return r.eval()};return i.toString=function(){return e+"("+t.join(", ")+")"},i},_.prototype.eval=function(){for(var e=this.variables,t=this.values,n=0,r=e.length;r>n;n++)e[n].value=t[n];return this.symbol.set(this.fn),this.fn},_.prototype.toString=function(){return""+this.fn},K.expr.Symbol=function(e,t){this.name=e,this.value=t},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,t){this.readonly=!1,t&&void 0!=t.readonly&&(this.readonly=t.readonly),this.parentScope=e,this.nestedScopes=void 0,this.symbols={},this.defs={},this.updates={},this.links={}},K.expr.Scope.prototype={createNestedScope:function(){if(this.readonly)throw Error("Cannot create nested scope: Scope is read-only");var e=new K.expr.Scope(this);return this.nestedScopes||(this.nestedScopes=[]),this.nestedScopes.push(e),e},clear:function(){if(this.readonly)throw Error("Cannot clear scope: Scope is read-only");if(this.symbols={},this.defs={},this.links={},this.updates={},this.nestedScopes)for(var e=this.nestedScopes,t=0,n=e.length;n>t;t++)e[t].clear()},createSymbol:function(e){var t=this.symbols[e];if(!t){var n=this.findDef(e);t=new K.expr.Symbol(e,n),this.symbols[e]=t}return t},createLink:function(e){var t=this.links[e];return t||(t=this.createSymbol(e),this.links[e]=t),t},createDef:function(e,t){if(this.readonly)throw Error("Cannot create symbol: Scope is read-only");var n=this.defs[e];if(n)void 0!=t&&n.set(t);else{n=this.createSymbol(e),this.defs[e]=n,void 0!=t&&n.set(t);var r=this.getUndefinedSymbols(e);r.length&&r.forEach(function(e){e.set(n)})}return n},createUpdate:function(e){if(this.readonly)throw Error("Cannot update symbol: Scope is read-only");var t=this.updates[e];if(!t){t=this.createLink(e),this.updates[e]=t;var n=this.getUndefinedSymbols(e);n.length&&n.forEach(function(e){e.set(t)})}return t},createConstant:function(e,t){var n=new K.expr.Symbol(e,t);return this.symbols[e]=n,this.defs[e]=n,n},findDef:function(e){var t;if(t=this.defs[e])return t;if(t=this.updates[e])return t;if(this.parentScope)return this.parentScope.findDef(e);var n=K[e];if(n)return this.createConstant(e,n);if(E.isPlainUnit(e)){var r=new E(null,e);return this.createConstant(e,r)}return void 0},setUndefined:function(e){var t=this.symbols[e];t&&t.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,t=this.parentScope;for(var n in e)if(e.hasOwnProperty(n)){var r=e[n];r.set(t?t.findDef(n):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 t=this.nestedScopes,n=0,r=t.length;r>n;n++)if(t[n].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 t=this.symbols,n=[];for(var r in t)if(t.hasOwnProperty(r)){var i=t[r];void 0!=i.value||e&&i.name!=e||n.push(i)}return this.nestedScopes&&this.nestedScopes.forEach(function(t){n=n.concat(t.getUndefinedSymbols(e))}),n}},function(){function e(){Z++,X=W.charAt(Z)}function n(){Z=0,X=W.charAt(0)}function r(){for(J=Y.NULL,Q="";" "==X||" "==X;)e();if("#"==X)for(;"\n"!=X&&""!=X;)e();if(""==X)return J=Y.DELIMITER,void 0;if("-"==X||","==X||"("==X||")"==X||"["==X||"]"==X||'"'==X||"\n"==X||";"==X||":"==X||"!"==X||"'"==X)return J=Y.DELIMITER,Q+=X,e(),void 0;if(i(X))for(J=Y.DELIMITER;i(X);)Q+=X,e();else if(o(X)){if(J=Y.NUMBER,"."==X)Q+=X,e(),f(X)||(J=Y.UNKNOWN);else{for(;f(X);)Q+=X,e();"."==X&&(Q+=X,e())}for(;f(X);)Q+=X,e();if("E"==X||"e"==X)for(Q+=X,e(),("+"==X||"-"==X)&&(Q+=X,e()),f(X)||(J=Y.UNKNOWN);f(X);)Q+=X,e()}else{if(!a(X)){for(J=Y.UNKNOWN;""!=X;)Q+=X,e();throw F('Syntax error in part "'+Q+'"')}for(J=Y.SYMBOL;a(X)||f(X);)Q+=X,e()}}function i(e){return"&"==e||"|"==e||"<"==e||">"==e||"="==e||"+"==e||"/"==e||"*"==e||"%"==e||"^"==e||","==e||";"==e||"\n"==e||"!"==e||"'"==e}function a(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e||"_"==e}function o(e){return e>="0"&&"9">=e||"."==e}function f(e){return e>="0"&&"9">=e}function s(e){n(),r();var t;if(t=""==Q?new M(void 0):u(e),""!=Q)throw J==Y.DELIMITER?H("Unknown operator "+Q):F('Unexpected part "'+Q+'"');return t}function u(e){var t,n,i;for("\n"!=Q&&";"!=Q&&""!=Q&&(t=c(e));"\n"==Q||";"==Q;)n||(n=new L,t&&(i=";"!=Q,n.add(t,i))),r(),"\n"!=Q&&";"!=Q&&""!=Q&&(t=c(e),i=";"!=Q,n.add(t,i));return n?n:(t||(t=c(e)),t)}function c(e){var t=l(e);if(!e.readonly){var n="ans",r=void 0,i=e.createDef(n);return new R(n,r,t,i)}return t}function l(e){if(J==Y.SYMBOL&&"function"==Q){if(r(),J!=Y.SYMBOL)throw F("Function name expected");var t=Q;if(r(),"("!=Q)throw F("Opening parenthesis ( expected");for(var n=e.createNestedScope(),i=[],a=[];;){if(r(),J!=Y.SYMBOL)throw F("Variable name expected");var o=Q,f=n.createDef(o);if(i.push(o),a.push(f),r(),","!=Q){if(")"==Q)break;throw F('Comma , or closing parenthesis ) expected"')}}if(r(),"="!=Q)throw F("Equal sign = expected");r();var s=h(n),u=e.createDef(t);return new _(t,i,a,s,u)}return h(e)}function h(e){var t,n,i,a,o=!1;J==Y.SYMBOL&&(o=e.hasLink(Q));var f=m(e);if("="==Q){if(f instanceof S)return o||e.removeLink(t),r(),t=f.name,n=null,i=h(e),a=e.createDef(t),new R(t,n,i,a);if(f instanceof T&&f.object instanceof S)return o||e.removeLink(t),r(),t=f.object.name,n=f.params,i=h(e),a=e.createUpdate(t),new R(t,n,i,a);throw F("Symbol expected at the left hand side of assignment operator =")}return f}function m(e){var t,n,i,a;if(t=p(e),":"==Q){for(a=[t];":"==Q;)r(),a.push(p(e));if(a.length>3)throw new TypeError("Invalid range");n="range",i=K.range,t=new A(n,i,a)}return t}function p(e){var t,n,i,a,o;for(t=v(e),n={"in":"in"};void 0!==n[Q];)i=Q,a=K[n[i]],r(),o=[t,v(e)],t=new A(i,a,o);return t}function v(e){var t=g(e);return t}function g(e){var t,n,i,a,o;for(t=d(e),n={"==":"equal","!=":"unequal","<":"smaller",">":"larger","<=":"smallereq",">=":"largereq"};void 0!==n[Q];)i=Q,a=K[n[i]],r(),o=[t,d(e)],t=new A(i,a,o);return t}function d(e){var t,n,i,a,o;for(t=w(e),n={"+":"add","-":"subtract"};void 0!==n[Q];)i=Q,a=K[n[i]],r(),o=[t,w(e)],t=new A(i,a,o);return t}function w(e){var t,n,i,a,o;for(t=y(e),n={"*":"multiply","/":"divide","%":"mod",mod:"mod"};void 0!==n[Q];)i=Q,a=K[n[i]],r(),o=[t,y(e)],t=new A(i,a,o);return t}function y(e){var t,n,i;return"-"==Q?(t=Q,n=K.unaryminus,r(),i=[N(e)],new A(t,n,i)):N(e)}function N(e){var t,n,i,a,o,f;for(i=[O(e)];"^"==Q;)r(),i.push(O(e));for(t=i.pop();i.length;)n=i.pop(),a="^",o=K.pow,f=[n,t],t=new A(a,o,f);return t}function O(e){var t,n,i,a;for(t=x(e);"!"==Q;)n=Q,i=K.factorial,r(),a=[t],t=new A(n,i,a);return t}function x(e){var t,n,i,a;for(t=b(e);"'"==Q;)n=Q,i=K.transpose,r(),a=[t],t=new A(n,i,a);return t}function b(e){return z(e)}function z(e){var t,n,i;return J==Y.SYMBOL?(n=Q,r(),i=e.createLink(n),t=new S(n,i),q(e,t)):I(e)}function q(e,t){for(var n;"("==Q;){if(n=[],r(),")"!=Q)for(n.push(h(e));","==Q;)r(),n.push(h(e));if(")"!=Q)throw F("Parenthesis ) missing");r(),t=new T(t,n)}return t}function I(t){var n,i,a;if('"'==Q){for(i="",a="";""!=X&&('"'!=X||"\\"==a);)i+=X,a=X,e();if(r(),'"'!=Q)throw F('End of string " missing');return r(),n=new M(i),n=q(t,n)}return P(t)}function P(e){var t,n,i,a,o,f;if("["==Q){for(r();"\n"==Q;)r();if("]"!=Q){for(n=[],i=0,a=0,n[0]=[h(e)];","==Q||";"==Q;){for(","==Q?a++:(i++,a=0,n[i]=[]),r();"\n"==Q;)r();for(n[i][a]=h(e);"\n"==Q;)r()}for(o=n.length,f=n.length>0?n[0].length:0,i=1;o>i;i++)if(n[i].length!=f)throw H("Number of columns must match ("+n[i].length+" != "+f+")");if("]"!=Q)throw F("End of matrix ] missing");r(),t=new U(n)}else r(),t=new U([[]]);return t=q(e,t)}return B(e)}function B(e){var n,i,a;if(J==Y.NUMBER){if(a="."==Q?0:Number(Q),r(),J==Y.SYMBOL){if("i"==Q||"I"==Q)return i=new t(0,a),r(),new M(i);if(E.isPlainUnit(Q))return i=new E(a,Q),r(),new M(i);throw j('Unknown unit "'+Q+'"')}return n=new M(a),n=q(e,n)}return C(e)}function C(e){var t;if("("==Q){if(r(),t=h(e),")"!=Q)throw F("Parenthesis ) expected");return r(),t=q(e,t)}return G(e)}function G(){throw""==Q?F("Unexpected end of expression"):F("Value expected")}function D(){return void 0}function V(){return Z-Q.length+1}function k(e){var t=D(),n=V();return void 0===t?void 0===n?e:e+" (char "+n+")":e+" (line "+t+", char "+n+")"}function F(e){return new SyntaxError(k(e))}function j(e){return new TypeError(k(e))}function H(e){return Error(k(e))}K.expr.Parser=function(e){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,e)},K.expr.Parser.prototype.parse=function(e,t){return W=e||"",t||(t=this.scope),s(t)},K.expr.Parser.prototype.eval=function(e){var t=this.parse(e);return t.eval()},K.expr.Parser.prototype.get=function(e){var t=this.scope.findDef(e);return t?t.get():void 0},K.expr.Parser.prototype.set=function(e,t){this.scope.createDef(e,t)},K.expr.Parser.prototype.remove=function(e){this.scope.setUndefined(e)},K.expr.Parser.prototype.clear=function(){this.scope.clear()};var Y={NULL:0,DELIMITER:1,NUMBER:2,SYMBOL:3,UNKNOWN:4},W="",Z=0,X="",Q="",J=Y.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(t){var n=this._getNewId(),r=this.lastNode?this.lastNode.scope:this.scope,i=new K.expr.Scope(r),a=new e.Node({id:n,expression:t,parser:this.parser,scope:i,nextNode:void 0,previousNode:this.lastNode});return this.nodes[n]=a,this.firstNode||(this.firstNode=a),this.lastNode&&(this.lastNode.nextNode=a),this.lastNode=a,this._update([n]),n},e.prototype.insertBefore=function(t,n){var r=this.nodes[n];if(!r)throw'Node with id "'+n+'" not found';var i=r.previousNode,a=this._getNewId(),o=i?i.scope:this.scope,f=new K.expr.Scope(o),s=new e.Node({id:a,expression:t,parser:this.parser,scope:f,nextNode:r,previousNode:i});this.nodes[a]=s,i?i.nextNode=s:this.firstNode=s,r.previousNode=s,r.scope.parentScope=s.scope;var u=this.getDependencies(a);return-1==u.indexOf(a)&&u.unshift(a),this._update(u),a},e.prototype.insertAfter=function(e,t){var n=this.nodes[t];if(!n)throw'Node with id "'+t+'" not found';var r=n.nextNode;return r?this.insertBefore(e,r.id):this.append(e)},e.prototype.remove=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';var n=this.getDependencies(e),r=t.previousNode,i=t.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(n)},e.prototype.replace=function(t,n){var r=this.nodes[n];if(!r)throw'Node with id "'+n+'" not found';var i=[n];e._merge(i,this.getDependencies(n));var a=r.previousNode;r.nextNode,a?a.scope:this.scope,r.setExpr(t),e._merge(i,this.getDependencies(n)),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 t="Error: "+((e.message||e)+"");this.fn=new M(t)}},e.Node.prototype.eval=function(){try{this.scope.init(),this.result=this.fn.eval()}catch(e){this.scope.init(),this.result="Error: "+((e.message||e)+"")}return this.result},e._merge=function(e,t){for(var n=0,r=t.length;r>n;n++){var i=t[n];-1==e.indexOf(i)&&e.push(i)}},e.prototype.getDependencies=function(t){var n,r=[],i=this.nodes[t];if(i){var a=i.scope.defs,o=i.scope.updates,f=[];for(n in a)a.hasOwnProperty(n)&&f.push(n);for(n in o)o.hasOwnProperty(n)&&-1==f.indexOf(n)&&f.push(n);for(var s=i.nextNode;s&&f.length;){for(var u=s.scope,c=0;f.length>c;){if(n=f[c],(u.hasLink(n)||u.hasUpdate(n))&&-1==r.indexOf(s.id)){r.push(s.id);var l=this.getDependencies(s.id);e._merge(r,l)}u.hasDef(n)&&(f.splice(c,1),c--),c++}s=s.nextNode}}return r},e.prototype.getExpr=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';return t.getExpr()},e.prototype.getResult=function(e){var t=this.nodes[e];if(!t)throw'Node with id "'+e+'" not found';return t.getResult()},e.prototype._update=function(e){this.updateSeq++;for(var t=this.updateSeq,n=this.nodes,r=0,i=e.length;i>r;r++){var a=e[r],o=n[a];o&&(o.eval(),o.updateSeq=t)}},e.prototype.getChanges=function(e){var t=[],n=this.firstNode;for(e=e||0;n;)n.updateSeq>e&&t.push(n.id),n=n.nextNode;return{ids:t,updateSeq:this.updateSeq}},e.prototype._getNewId=function(){return this.idMax++,this.idMax},e.prototype.toString=function(){return JSON.stringify(this.toJSON())},e.prototype.toJSON=function(){for(var e=[],t=this.firstNode;t;){var n={id:t.id,expression:t.expression,dependencies:this.getDependencies(t.id)};try{n.result=t.getResult()}catch(r){n.result="Error: "+((r.message||r)+"")}e.push(n),t=t.nextNode}return e}}(),K.abs=function(e){if(1!=arguments.length)throw x("abs",arguments.length,1);if(v(e))return Math.abs(e);if(e instanceof t)return Math.sqrt(e.re*e.re+e.im*e.im);if(e instanceof Array||e instanceof n)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 t)return new t(e+r.re,r.im)}else if(e instanceof t){if(v(r))return new t(e.re+r,e.im);if(r instanceof t)return new t(e.re+r.re,e.im+r.im)}else if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Units do not match");if(null==e.value)throw Error("Unit on left hand side of operator + has an undefined value");if(null==r.value)throw 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 n||r instanceof Array||r instanceof n)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 t)return new t(Math.ceil(e.re),Math.ceil(e.im));if(e instanceof Array||e instanceof n)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 t)return K.multiply(K.multiply(e,e),e);if(e instanceof Array||e instanceof n)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 t)return z(new t(e,0),r)}if(e instanceof t){if(v(r))return z(e,new t(r,0));if(r instanceof t)return z(e,r)}if(e instanceof E&&v(r)){var i=e.clone();return i.value/=r,i}if(e instanceof Array||e instanceof n)return r instanceof Array||r instanceof n?K.multiply(e,K.inv(r)):Z.map2(e,r,K.divide);if(r instanceof Array||r instanceof n)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 J(e,r){if(2!=arguments.length)throw x("equal",arguments.length,2);if(v(e)){if(v(r))return e==r;if(r instanceof t)return e==r.re&&0==r.im}if(e instanceof t){if(v(r))return e.re==r&&0==e.im;if(r instanceof t)return e.re==r.re&&e.im==r.im}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value==r.value}if(y(e)||y(r))return e==r;if(e instanceof Array||e instanceof n||r instanceof Array||r instanceof n)return Z.map2(e,r,K.equal);if(e.valueOf()!==e||r.valueOf()!==r)return J(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 t){var r=Math.exp(e.re);return new t(r*Math.cos(e.im),r*Math.sin(e.im))}if(e instanceof Array||e instanceof n)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 t)return new t(e.re>0?Math.floor(e.re):Math.ceil(e.re),e.im>0?Math.floor(e.im):Math.ceil(e.im));if(e instanceof Array||e instanceof n)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 t)return new t(Math.floor(e.re),Math.floor(e.im));if(e instanceof Array||e instanceof n)return Z.map(e,K.floor);if(e.valueOf()!==e)return K.floor(e.valueOf());throw O("floor",e)},K.gcd=function(){var e,t=arguments[0],r=arguments[1];if(2==arguments.length){if(v(t)&&v(r)){if(!g(t)||!g(r))throw Error("Parameters in function gcd must be integer numbers");for(;0!=r;)e=r,r=t%e,t=e;return Math.abs(t)}if(t instanceof Array||t instanceof n||r instanceof Array||r instanceof n)return Z.map2(t,r,K.gcd);if(t.valueOf()!==t||r.valueOf()!==r)return K.gcd(t.valueOf(),r.valueOf());throw O("gcd",t,r)}if(arguments.length>2){for(var i=1;arguments.length>i;i++)t=K.gcd(t,arguments[i]);return t}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 t)return e>K.abs(r)}if(e instanceof t){if(v(r))return K.abs(e)>r;if(r instanceof t)return K.abs(e)>K.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value>r.value}if(y(e)||y(r))return e>r;if(e instanceof Array||e instanceof n||r instanceof Array||r instanceof n)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 t)return e>=K.abs(r)}if(e instanceof t){if(v(r))return K.abs(e)>=r;if(r instanceof t)return K.abs(e)>=K.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value>=r.value}if(y(e)||y(r))return e>=r;if(e instanceof Array||e instanceof n||r instanceof Array||r instanceof n)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,t=arguments[0],r=arguments[1];if(2==arguments.length){if(v(t)&&v(r)){if(!g(t)||!g(r))throw Error("Parameters in function lcm must be integer numbers");for(var i=t*r;0!=r;)e=r,r=t%e,t=e;return Math.abs(i/t)}if(t instanceof Array||t instanceof n||r instanceof Array||r instanceof n)return Z.map2(t,r,K.lcm);if(t.valueOf()!==t||r.valueOf()!==r)return K.lcm(t.valueOf(),r.valueOf());throw O("lcm",t,r)}if(arguments.length>2){for(var a=1;arguments.length>a;a++)t=K.lcm(t,arguments[a]);return t}throw new SyntaxError("Function lcm expects two or more arguments")},K.log=function(e,r){if(1!=arguments.length&&2!=arguments.length)throw x("log",arguments.length,1,2);if(void 0!==r)return K.divide(K.log(e),K.log(r));if(v(e))return e>=0?Math.log(e):K.log(new t(e,0));if(e instanceof t)return new t(Math.log(Math.sqrt(e.re*e.re+e.im*e.im)),Math.atan2(e.im,e.re));if(e instanceof Array||e instanceof n)return Z.map(e,K.log);if(e.valueOf()!==e||r.valueOf()!==r)return K.log(e.valueOf(),r.valueOf());throw O("log",e,r)},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 t(e,0));if(e instanceof t)return new t(Math.log(Math.sqrt(e.re*e.re+e.im*e.im))/Math.LN10,Math.atan2(e.im,e.re)/Math.LN10);if(e instanceof Array||e instanceof n)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 t&&0==r.im)return e%r.re}else if(e instanceof t&&0==e.im){if(v(r))return e.re%r;if(r instanceof t&&0==r.im)return e.re%r.re}if(e instanceof Array||e instanceof n||r instanceof Array||r instanceof n)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(e,r){if(2!=arguments.length)throw x("multiply",arguments.length,2);if(v(e)){if(v(r))return e*r;if(r instanceof t)return q(new t(e,0),r);if(r instanceof E)return o=r.clone(),o.value*=e,o}else if(e instanceof t){if(v(r))return q(e,new t(r,0));if(r instanceof t)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 Error("Can only multiply a 2 dimensional matrix (A has "+i.length+" dimensions)");if(2!=a.length)throw Error("Can only multiply a 2 dimensional matrix (B has "+a.length+" dimensions)");if(i[1]!=a[0])throw new RangeError("Dimensions mismatch in multiplication. Columns of A must match rows of B (A is "+i[0]+"x"+i[1]+", B is "+a[0]+"x"+a[1]+", "+a[1]+" != "+a[0]+")");for(var o=[],f=i[0],s=a[1],u=i[1],c=K.multiply,l=K.add,h=0;f>h;h++){o[h]=[];for(var m=0;s>m;m++){for(var p=null,g=0;u>g;g++){var d=c(e[h][g],r[g][m]);p=null==p?d:l(p,d)}o[h][m]=p}}return o}return r instanceof n?new n(K.multiply(e.valueOf(),r.valueOf())):Z.map2(e,r,K.multiply)}if(e instanceof n)return new n(K.multiply(e.valueOf(),r.valueOf()))}if(r instanceof Array)return Z.map2(e,r,K.multiply);if(r instanceof n)return new n(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 t(e,0),new t(r,0));if(r instanceof t)return I(new t(e,0),r)}else if(e instanceof t){if(v(r))return I(e,new t(r,0));if(r instanceof t)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 Error("For A^b, A must be 2 dimensional (A has "+i.length+" dimensions)");if(i[0]!=i[1])throw Error("For A^b, A must be square (size is "+i[0]+"x"+i[1]+")");if(0==r)return K.eye(i[0]);for(var a=e,o=1;r>o;o++)a=K.multiply(e,a);return a}if(e instanceof n)return new n(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 t)return new t(Math.round(e.re),Math.round(e.im));if(e instanceof Array||e instanceof n)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 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 t)return new t(P(e.re,r),P(e.im,r));if(e instanceof Array||e instanceof n||r instanceof Array||r instanceof n)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(e){if(1!=arguments.length)throw x("sign",arguments.length,1);if(v(e)){var r;return r=e>0?1:0>e?-1:0}if(e instanceof t){var i=Math.sqrt(e.re*e.re+e.im*e.im);return new t(e.re/i,e.im/i)}if(e instanceof Array||e instanceof n)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 t)return K.abs(r)>e}if(e instanceof t){if(v(r))return r>K.abs(e);if(r instanceof t)return K.abs(e)<K.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value<r.value}if(y(e)||y(r))return r>e;if(e instanceof Array||e instanceof n||r instanceof Array||r instanceof n)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 t)return K.abs(r)>=e}if(e instanceof t){if(v(r))return r>=K.abs(e);if(r instanceof t)return K.abs(e)<=K.abs(r)}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value<=r.value}if(y(e)||y(r))return r>=e;if(e instanceof Array||e instanceof n||r instanceof Array||r instanceof n)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 t(e,0));if(e instanceof t){var r=Math.sqrt(e.re*e.re+e.im*e.im);return e.im>=0?new t(.5*Math.sqrt(2*(r+e.re)),.5*Math.sqrt(2*(r-e.re))):new t(.5*Math.sqrt(2*(r+e.re)),-.5*Math.sqrt(2*(r-e.re)))}if(e instanceof Array||e instanceof n)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 t)return K.multiply(e,e);if(e instanceof Array||e instanceof n)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 t)return new t(e-r.re,r.im)}else if(e instanceof t){if(v(r))return new t(e.re-r,e.im);if(r instanceof t)return new t(e.re-r.re,e.im-r.im)}else if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Units do not match");if(null==e.value)throw Error("Unit on left hand side of operator - has an undefined value");if(null==r.value)throw 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 n||r instanceof Array||r instanceof n)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 t)return new t(-e.re,-e.im);if(e instanceof E){var r=e.clone();return r.value=-e.value,r}if(e instanceof Array||e instanceof n)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 t)return e==r.re&&0==r.im}if(e instanceof t){if(v(r))return e.re==r&&0==e.im;if(r instanceof t)return e.re==r.re&&e.im==r.im}if(e instanceof E&&r instanceof E){if(!e.equalBase(r))throw Error("Cannot compare units with different base");return e.value==r.value}if(y(e)||y(r))return e==r;if(e instanceof Array||e instanceof n||r instanceof Array||r instanceof n)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 $(e,t){if(2==arguments.length){if(v(e)&&v(t)){if(!g(e)||!g(t))throw Error("Parameters in function xgcd must be integer numbers");if(0==t)return[e,1,0];var n=$(t,e%t),r=n[0],i=n[1],a=n[2];return[r,a,i-a*Math.floor(e/t)]}throw O("xgcd",e,t)}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 t?Math.atan2(e.im,e.re):e instanceof Array||e instanceof n?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 t?new t(e.re,-e.im):e instanceof Array||e instanceof n?Z.map(e,K.conj):e.valueOf()!==e?K.conj(e.valueOf()):et(e)},K.im=function(e){if(1!=arguments.length)throw x("im",arguments.length,1);return v(e)?0:e instanceof t?e.im:e instanceof Array||e instanceof n?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 t?e.re:e instanceof Array||e instanceof n?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 t(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 n=t.parse(e);if(n)return n;throw new SyntaxError('String "'+e+'" is no valid complex number');case 2:return new t(arguments[0],arguments[1]);default:throw x("complex",arguments.length,0,2)}},K.matrix=function(e){if(arguments.length>1)throw x("matrix",arguments.length,0,1);return new n(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 t=d.parse(e);if(t)return t;throw new SyntaxError('String "'+t+'" is no valid range');case 2:return new d(arguments[0],null,arguments[1]);case 3:return new d(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 t=E.parse(e);if(t)return t;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,t,r=arguments.length,i=-1,a=!1,o=[];for(e=0;r>e;e++){var f=arguments[e];if(f instanceof n&&(a=!0),e==r-1&&v(f)){if(t=i,i=f,!g(i)||1>i)throw new TypeError("Dimension number must be a positive integer (dim = "+i+")");if(e>0&&i>t)throw new RangeError("Dimension out of range ("+i+" > "+t+")")}else{if(!(f instanceof Array||f instanceof n))throw O("concat",f);var s=K.clone(f.valueOf()),u=K.size(f);if(o[e]=s,t=i,i=u.length,e>0&&i!=t)throw new RangeError("Dimension mismatch ("+t+" != "+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 n(c):c},K.det=function(e){if(1!=arguments.length)throw x("det",arguments.length,1);var t=K.size(e);switch(t.length){case 0:return K.clone(e);case 1:if(1==t[0])return K.clone(e.valueOf()[0]);throw new RangeError("Matrix must be square (size: "+K.format(t)+")");case 2:var n=t[0],r=t[1];if(n==r)return C(e.valueOf(),n,r);throw new RangeError("Matrix must be square (size: "+K.format(t)+")");default:throw new RangeError("Matrix must be two dimensional (size: "+K.format(t)+")")}},K.diag=function(e,t){var r,i,a,o;if(1!=arguments.length&&2!=arguments.length)throw x("diag",arguments.length,1,2);if(t){if(!v(t)||!g(t))throw new TypeError("Second parameter in function diag must be an integer")}else t=0;var f=t>0?t:0,s=0>t?-t:0;e instanceof n||e instanceof d||(e=new n(e));var u;switch(e.isVector()?(e=e.toVector(),u=[e.length]):u=e.size(),u.length){case 1:i=e.valueOf();var c=new n;for(c.resize([i.length+s,i.length+f]),r=c.valueOf(),o=i.length,a=0;o>a;a++)r[a+s][a+f]=K.clone(i[a]);return c;case 2:for(i=[],r=e.valueOf(),o=Math.min(u[0]-s,u[1]-f),a=0;o>a;a++)i[a]=K.clone(r[a+s][a+f]);return new n(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 t=e[0],r=e[1];if(!v(t)||!g(t)||1>t)throw Error("Parameters in function eye must be positive integers");if(r&&(!v(r)||!g(r)||1>r))throw Error("Parameters in function eye must be positive integers");var i=new n;i.resize(e);for(var a=K.min(e),o=i.valueOf(),f=0;a>f;f++)o[f][f]=1;return i},K.inv=function(e){if(1!=arguments.length)throw x("inv",arguments.length,1);var t=K.size(e);switch(t.length){case 0:return K.divide(1,e);case 1:if(1==t[0])return e instanceof n?new n([K.divide(1,e.valueOf()[0])]):[K.divide(1,e[0])];throw new RangeError("Matrix must be square (size: "+K.format(t)+")");case 2:var r=t[0],i=t[1];if(r==i)return e instanceof n?new n(D(e.valueOf(),r,i)):D(e,r,i);throw new RangeError("Matrix must be square (size: "+K.format(t)+")");default:throw new RangeError("Matrix must be two dimensional (size: "+K.format(t)+")")}},K.ones=function(){var e=Z.argsToArray(arguments);0==e.length?e=[1,1]:1==e.length&&(e[1]=e[0]);var t=new n,r=1;return t.resize(e,r),t},K.size=function(e){if(1!=arguments.length)throw x("size",arguments.length,1);if(v(e)||e instanceof t||e instanceof E||null==e)return[];if(y(e))return[e.length];if(e instanceof Array)return Z.size(e);if(e instanceof n)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?V(K.clone(e)):e instanceof n?V(e.toArray()):e.valueOf()instanceof Array?V(K.clone(e.valueOf())):K.clone(e)},K.transpose=function(e){if(1!=arguments.length)throw x("transpose",arguments.length,1);var t=K.size(e);switch(t.length){case 0:return K.clone(e);case 1:return K.clone(e);case 2:for(var r,i=t[1],a=t[0],o=e instanceof n,f=e.valueOf(),s=[],u=K.clone,c=0;i>c;c++){r=s[c]=[];for(var l=0;a>l;l++)r[l]=u(f[l][c])}return 0==a&&(s[0]=[]),o?new n(s):s;default:throw new RangeError("Matrix must be two dimensional (size: "+K.format(t)+")")}},K.zeros=function(){var e=Z.argsToArray(arguments);0==e.length?e=[1,1]:1==e.length&&(e[1]=e[0]);var t=new n;return t.resize(e),t},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 t=e,r=t;for(t--;t>1;)r*=t,t--;return 0==r&&(r=1),r}if(e instanceof Array||e instanceof n)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 Error("Function max requires one or more parameters (0 provided)");if(e instanceof Array||e instanceof n||e instanceof d){if(arguments.length>1)throw Error("Wrong number of parameters (1 matrix or multiple scalars expected)");var t=K.size(e);if(1==t.length){if(0==e.length)throw Error("Cannot calculate max of an empty vector");return k(e.valueOf())}if(2==t.length){if(0==t[0]||0==t[1])throw Error("Cannot calculate max of an empty matrix");if(e instanceof Array)return F(e,t[0],t[1]);if(e instanceof n||e instanceof d)return new n(F(e.valueOf(),t[0],t[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 Error("Function min requires one or more parameters (0 provided)");if(e instanceof Array||e instanceof n||e instanceof d){if(arguments.length>1)throw Error("Wrong number of parameters (1 matrix or multiple scalars expected)");var t=K.size(e);if(1==t.length){if(0==e.length)throw Error("Cannot calculate min of an empty vector");return j(e.valueOf())}if(2==t.length){if(0==t[0]||0==t[1])throw Error("Cannot calculate min of an empty matrix");if(e instanceof Array)return H(e,t[0],t[1]);if(e instanceof n||e instanceof d)return new n(H(e.valueOf(),t[0],t[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 t(e,0));if(e instanceof t){var r=new t(e.im*e.im-e.re*e.re+1,-2*e.re*e.im),i=K.sqrt(r),a=new t(i.re-e.im,i.im+e.re),o=K.log(a);return new t(1.5707963267948966-o.im,o.re)}if(e instanceof Array||e instanceof n)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 t(e,0));if(e instanceof t){var r=e.re,i=e.im,a=new t(i*i-r*r+1,-2*r*i),o=K.sqrt(a),f=new t(o.re-i,o.im+r),s=K.log(f);return new t(s.im,-s.re)}if(e instanceof Array||e instanceof n)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 t){var r=e.re,i=e.im,a=r*r+(1-i)*(1-i),o=new t((1-i*i-r*r)/a,-2*r/a),f=K.log(o);return new t(-.5*f.im,.5*f.re)}if(e instanceof Array||e instanceof n)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 t&&v(r))return Math.atan2(e.re,r);if(e instanceof Array||e instanceof n||r instanceof Array||r instanceof n)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 t)return new t(.5*Math.cos(e.re)*(Math.exp(-e.im)+Math.exp(e.im)),.5*Math.sin(e.re)*(Math.exp(-e.im)-Math.exp(e.im)));if(e instanceof 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 n)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 t){var r=Math.exp(-4*e.im)-2*Math.exp(-2*e.im)*Math.cos(2*e.re)+1;return new t(2*Math.exp(-2*e.im)*Math.sin(2*e.re)/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 n)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 t){var r=.25*(Math.exp(-2*e.im)+Math.exp(2*e.im))-.5*Math.cos(2*e.re);return new t(.5*Math.sin(e.re)*(Math.exp(-e.im)+Math.exp(e.im))/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 n)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 t){var r=.25*(Math.exp(-2*e.im)+Math.exp(2*e.im))+.5*Math.cos(2*e.re);return new t(.5*Math.cos(e.re)*(Math.exp(-e.im)+Math.exp(e.im))/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 n)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 t)return new t(.5*Math.sin(e.re)*(Math.exp(-e.im)+Math.exp(e.im)),.5*Math.cos(e.re)*(Math.exp(e.im)-Math.exp(-e.im)));if(e instanceof 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 n)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 t){var r=Math.exp(-4*e.im)+2*Math.exp(-2*e.im)*Math.cos(2*e.re)+1;return new t(2*Math.exp(-2*e.im)*Math.sin(2*e.re)/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 n)return Z.map(e,K.tan);if(e.valueOf()!==e)return K.tan(e.valueOf());throw O("tan",e)},K["in"]=function(e,t){if(2!=arguments.length)throw x("in",arguments.length,2);if(e instanceof E&&(t instanceof E||y(t)))return e["in"](t);if(e instanceof Array||e instanceof n||t instanceof Array||t instanceof n)return Z.map2(e,t,K["in"]);if(e.valueOf()!==e||t.valueOf()!==t)return K["in"](e.valueOf(),t.valueOf());throw O("in",e,t)},K.clone=function et(t){if(1!=arguments.length)throw x("clone",arguments.length,1);if(null==t)return t;if("function"==typeof t.clone)return t.clone();if(v(t)||y(t)||e(t))return t;if(t instanceof Array){var n=K.clone;return t.map(function(e){return n(e)})}if(t instanceof Object)return Z.mapObject(t,K.clone);throw O("clone",t)},K.eval=function(e){if(1!=arguments.length)throw x("eval",arguments.length,1);if(y(e))return tt.eval(e);if(e instanceof Array||e instanceof n)return Z.map(e,K.eval);throw new TypeError("String or matrix expected")};var tt=new K.expr.Parser({readonly:!0});K.format=function(e,t){var n=arguments.length;if(1!=n&&2!=n)throw x("format",n,1,2);if(1==n){var r=arguments[0];return v(r)?Z.formatNumber(r):r instanceof Array?Z.formatArray(r):y(r)?'"'+r+'"':r instanceof Object?""+r:r+""}if(!y(e))throw new TypeError("String expected as first parameter in function format");if(!(t instanceof Object))throw new TypeError("Object expected as first parameter in function format");return e.replace(/\$([\w\.]+)/g,function(e,n){for(var r=n.split("."),i=t[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,t){var n,r={override:!1};if(t&&t instanceof Object&&Z.extend(r,t),y(e)){if("undefined"==typeof require)throw Error("Cannot load file: require not available.");var i=require(e);K["import"](i)}else if(W(e)){if(n=e.name,!n)throw Error("Cannot import an unnamed function or object");(r.override||void 0===K[n])&&Y(n,e)}else if(e instanceof Object)for(n in e)if(e.hasOwnProperty(n)){var a=e[n];W(a)?(r.override||void 0===K[n])&&Y(n,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 t,n=typeof e;if("object"==n){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(t in K)if(K.hasOwnProperty(t)&&e.constructor==K[t])return t.toLowerCase();for(t in K.type)if(K.type.hasOwnProperty(t)&&e.constructor==K.type[t])return t.toLowerCase();if(e.constructor.name)return e.constructor.name.toLowerCase()}}return n};for(var nt in K)K.hasOwnProperty(nt)&&nt&&w(nt,K[nt])})(); |