text: if("undefined"!=typeof module){var nerdamer=require("./nerdamer.core.js");require("./Calculus.js")}!function(){"use strict";var e=nerdamer.getCore(),r=e.PARSER,t=e.groups.N,i=e.groups.P,n=e.groups.S,a=e.groups.EX,o=e.groups.FN,s=e.groups.PL,l=e.groups.CP,u=e.groups.CB,f=e.Utils.keys,c=e.Utils.variables,h=e.Utils.round,p=e.Frac,v=e.Utils.isInt,m=e.Symbol,d=e.Settings.CONST_HASH,g=e.Utils.importFunctions(),b=e.Utils.evaluate;function w(t,i,n){if(e.Utils.isSymbol(t))this.parse(t);else if(isNaN(t))"string"==typeof t&&this.parse(r.parse(t));else{if(n=n||0,void 0===i)throw new e.exceptions.InvalidVariableNameError("Polynomial expects a variable name when creating using order");this.coeffs=[],this.coeffs[n]=t,this.fill(t)}}function y(){this.factors={}}function M(r,t,i){this.terms=t||[],this.coeff=r,this.map=i,this.sum=new e.Frac(0),this.image=void 0}w.fromArray=function(r,t){if(void 0===t)throw new e.exceptions.InvalidVariableNameError("A variable name must be specified when creating polynomial from array");var i=new w;return i.coeffs=r,i.variable=t,i},w.fit=function(e,r,t,i,n,a){var o=new Array(n+1),s=t-r;o[0]=r,o[n]=e,s-=e*Math.pow(i,n);for(var l=n-1;l>0;l--){var u=Math.pow(i,l),f=s/u,c=Math.sign(f)*Math.floor(Math.abs(f));s-=c*u,o[l]=c}if(0!==s)return null;for(l=0;l<o.length;l++)o[l]=new p(o[l]);return w.fromArray(o,a)},w.prototype={parse:function(i,n){if(this.variable=c(i)[0],!i.isPoly())throw e.exceptions.NerdamerTypeError("Polynomial Expected! Received "+e.Utils.text(i));if(n=n||[],i.power.absEquals(1)||(i=r.expand(i)),i.group===e.groups.N)n[0]=i.multiplier;else if(i.group===e.groups.S)n[i.power.toDecimal()]=i.multiplier;else for(var a in i.symbols){var o=i.symbols[a],s=o.power;if(e.Utils.isSymbol(s))throw new e.exceptions.NerdamerTypeError("power cannot be a Symbol");s=o.group===t?0:s.toDecimal(),o.symbols?this.parse(o,n):n[s]=o.multiplier}this.coeffs=n,this.fill()},fill:function(e){e=Number(e)||0;for(var r=this.coeffs.length,t=0;t<r;t++)void 0===this.coeffs[t]&&(this.coeffs[t]=new p(e));return this},trim:function(){for(var e=this.coeffs.length;e--;){var r=this.coeffs[e],t=r.equals(0);if(!r||!t)break;if(0===e)break;this.coeffs.pop()}return this},modP:function(e){for(var r=this.coeffs.length,t=0;t<r;t++){var i=this.coeffs[t];if(i<0){for(var n,a=t;a<r;a++)if(this.coeffs[a]>0){n=this.coeffs[a];break}if(n){for(;a>t;a--)this.coeffs[a]=this.coeffs[a].subtract(new p(1)),this.coeffs[a-1]=this.coeffs[a-1].add(new p(e));i=this.coeffs[t]}}var o=i.mod(e),s=i.subtract(o).divide(e);if(!s.equals(0)){var l=t+1,u=this.coeffs[l]||new p(0);u=u.add(s),this.coeffs[l]=new p(u),this.coeffs[t]=new p(o)}}return this},add:function(e){for(var r=Math.max(this.coeffs.length,e.coeffs.length),t=0;t<r;t++){var i=this.coeffs[t]||new p(0),n=e.coeffs[t]||new p(0);this.coeffs[t]=i.add(n)}return this},subtract:function(e){for(var r=Math.max(this.coeffs.length,e.coeffs.length),t=0;t<r;t++){var i=this.coeffs[t]||new p(0),n=e.coeffs[t]||new p(0);this.coeffs[t]=i.subtract(n)}return this},divide:function(r){for(var t=this.variable,i=e.Utils.arrayClone(this.coeffs),n=e.Utils.arrayClone(r.coeffs),a=i.length,o=n.length-1,s=[],l=0;l<a;l++){var u=a-(l+1),f=u-o,c=i[u].divide(n[o]);if(f<0)break;s[f]=c;for(var h=0;h<=o;h++)i[h+f]=i[h+f].subtract(n[h].multiply(c))}var p=w.fromArray(i,t||"x").trim();return[w.fromArray(s,t||"x"),p]},multiply:function(e){for(var r=this.coeffs.length,t=e.coeffs.length,i=[],n=0;n<r;n++)for(var a=this.coeffs[n],o=0;o<t;o++){var s=n+o,l=e.coeffs[o],u=i[s]||new p(0);i[s]=u.add(a.multiply(l))}return this.coeffs=i,this},isZero:function(){for(var e=this.coeffs.length,r=0;r<e;r++){if(!this.coeffs[r].equals(0))return!1}return!0},sub:function(e){for(var r=new p(0),t=this.coeffs.length,i=0;i<t;i++){var n=this.coeffs[i];n.equals(0)||(r=r.add(n.multiply(new p(Math.pow(e,i)))))}return r},clone:function(){var e=new w;return e.coeffs=this.coeffs,e.variable=this.variable,e},deg:function(){return this.trim(),this.coeffs.length-1},lc:function(){return this.coeffs[this.deg()].clone()},monic:function(){for(var e=this.lc(),r=this.coeffs.length,t=0;t<r;t++)this.coeffs[t]=this.coeffs[t].divide(e);return this},gcd:function(r){if(this.coeffs.length-1<r.coeffs.length-1)return r.gcd(this);for(var t=this;!r.isZero();){var i=r.clone();r=(t=t.clone()).divide(i)[1],t=i}var n=e.Math2.QGCD.apply(null,t.coeffs);if(!n.equals(1))for(var a=t.coeffs.length,o=0;o<a;o++)t.coeffs[o]=t.coeffs[o].divide(n);return t},diff:function(){for(var e=[],r=this.coeffs.length,t=1;t<r;t++)e.push(this.coeffs[t].multiply(new p(t)));return this.coeffs=e,this},integrate:function(){for(var e=[0],r=this.coeffs.length,t=0;t<r;t++){var i=new p(t+1);e[i]=this.coeffs[t].divide(i)}return this.coeffs=e,this},gcf:function(r){for(var t=[],i=0;i<this.coeffs.length;i++){var n=this.coeffs[i];n.equals(0)||-1!==t.indexOf(n)||t.push(n)}var a=[e.Math2.QGCD.apply(void 0,t),function(e){for(var r=0;r<e.length;r++)if(!e[r].equals(0))return r}(this.coeffs)].toDecimal();if(r){var o=[];o[a[1]-1]=a[0],a=w.fromArray(o,this.variable).fill()}return a},quad:function(e){var r=[];if(this.coeffs.length>3)throw new Error("Cannot calculate quadratic order of "+(this.coeffs.length-1));if(0===this.coeffs.length)throw new Error("Polynomial array has no terms");var t=this.coeffs[2]||0,i=this.coeffs[1]||0,n=i*i-4*t*this.coeffs[0];return n<0&&!e?r:(r[0]=(-i+Math.sqrt(n))/(2*t),r[1]=(-i-Math.sqrt(n))/(2*t),r)},squareFree:function(){for(var e=this.clone(),r=1,t=e.clone().diff(),i=e.clone().gcd(t),n=e.divide(i)[0],a=w.fromArray([new p(1)],e.variable);!i.equalsNumber(1);){var o=n.gcd(i),s=n.divide(o)[0];if(!s.equalsNumber(1)&&r>1){for(var l=s.clone(),u=1;u<r;u++)l.multiply(s.clone());s=l}a=a.multiply(s),r++,n=o,i=i.divide(o)[0]}return[a,n,r]},toSymbol:function(){var t=this.coeffs.length,i=this.variable;if(0===t)return new e.Symbol(0);for(var n=t-1,a="",o=0;o<t;o++){var s=o===n?"":"+",l=this.coeffs[o];l.equals(0)||(a+=l+"*"+i+"^"+o+s)}return r.parse(a)},equalsNumber:function(e){return this.trim(),1===this.coeffs.length&&this.coeffs[0].toDecimal()===String(e)},toString:function(){return this.toSymbol().toString()}},m.prototype.coeffs=function(e,t){t&&!this.isPoly(!0)&&r.error("Polynomial expected when requesting coefficients with order"),e=e||[];var i=this.clone().distributeMultiplier();if(i.isComposite())for(var n in i.symbols){var a=i.symbols[n];a.isComposite()?a.clone().distributeMultiplier().coeffs(e,t):t?e[a.isConstant()?0:a.power.toDecimal()]=a.multiplier:e.push(a.multiplier)}else t?e[i.isConstant()?0:i.power.toDecimal()]=i.multiplier:e.push(i.multiplier);if(t)for(var o=0;o<e.length;o++)void 0===e[o]&&(e[o]=new p(0));return e},m.prototype.tBase=function(e){if(void 0===e)throw new Error("Symbol.tBase requires a map object!");for(var r=[],t=this.collectSymbols(null,null,null,!0),i=t.length,n=0;n<i;n++){var a=t[n],o=a.group,s=new M(a.multiplier,[],e);if(o===u)for(var l in a.symbols){var f=a.symbols[l];s.terms[e[l]]=f.power}else s.terms[e[a.value]]=a.power;r.push(s.fill()),s.updateCount()}return r},m.prototype.altVar=function(e){var r=this.multiplier.toString(),t=this.power.toString();return("1"===r?"":r+"*")+e+("1"===t?"":"^"+t)},m.prototype.sameVars=function(e){if(!this.symbols&&this.group!==e.group)return!1;for(var r in this.symbols){var t=this.symbols[r],i=e.symbols[r];if(!i)return!1;if(t.value!==i.value)return!1}return!0},m.prototype.groupTerms=function(t){var i,n,a;t=String(t);var o=[];return this.each(function(l){if(l.group===s){a=l.groupTerms(t);for(var u=0;u<a.length;u++){var f=a[u];f&&(o[u]=f)}}else i=e.Utils.decompose_fn(l,t,!0),n=i.x.value===t?Number(i.x.power):0,o[n]=r.add(o[n]||new m(0),i.a)}),o},m.prototype.collectFactors=function(){var e=[];return this.group===u?this.each(function(r){e.push(r.clone())}):e.push(this.clone()),e},y.prototype.add=function(e){if(e.equals(0))return this;if(e.group===u){var t=this;e.multiplier.equals(1)||t.add(new m(e.multiplier)),e.each(function(e){t.add(e)})}else{this.preAdd&&(e=this.preAdd(e)),this.pFactor&&(e=r.pow(e,new m(this.pFactor)));var i=e.isConstant();if(i&&e.equals(1))return this;var n=i?e.value:e.text();n in this.factors?this.factors[n]=r.multiply(this.factors[n],e):this.factors[n]=e}return this},y.prototype.toSymbol=function(){var t=new m(1);for(var i in this.factors){var n=this.factors[i],a=(n.group,n.power.equals(1)?r.symfunction(e.PARENTHESIS,[this.factors[i]]):this.factors[i]);t=r.multiply(t,a)}return""===t.fname&&(t=m.unwrapPARENS(t)),t},y.prototype.merge=function(e){for(var t in e)t in this.factors?this.factors[t]=r.multiply(this.factors[t],e[t]):this.factors[t]=e[t];return this},y.prototype.each=function(r){for(var t in this.factors){var i=this.factors[t];i.fname===e.PARENTHESIS&&i.isLinear()&&(i=i.args[0]),r.call(this,i,t)}return this},y.prototype.count=function(){return f(this.factors).length},y.prototype.clean=function(){try{var r=e.Settings.CONST_HASH;this.factors[r].lessThan(0)&&(this.factors[r].equals(-1)?delete this.factors[r]:this.factors[r].negate(),this.each(function(e){e.negate()}))}catch(e){}},y.prototype.toString=function(){return this.toSymbol().toString()},M.prototype.updateCount=function(){this.count=this.count||0;for(var e=0;e<this.terms.length;e++)this.terms[e].equals(0)||this.count++;return this},M.prototype.getVars=function(){for(var e=[],r=0;r<this.terms.length;r++){var t=this.terms[r];this.getRevMap();t.equals(0)||e.push(this.rev_map[r])}return e.join(" ")},M.prototype.len=function(){return void 0===this.count&&this.updateCount(),this.count},M.prototype.toSymbol=function(e){e=e||this.getRevMap();for(var t=new m(this.coeff),i=0;i<this.terms.length;i++){var n=e[i],a=this.terms[i];if(!a.equals(0)&&n!==d){var o=new m(n);o.power=a,t=r.multiply(t,o)}}return t},M.prototype.getRevMap=function(){if(this.rev_map)return this.rev_map;var e={};for(var r in this.map)e[this.map[r]]=r;return this.rev_map=e,e},M.prototype.generateImage=function(){return this.image=this.terms.join(" "),this},M.prototype.getImg=function(){return this.image||this.generateImage(),this.image},M.prototype.fill=function(){for(var r=this.map.length,t=0;t<r;t++)void 0===this.terms[t]?this.terms[t]=new e.Frac(0):this.sum=this.sum.add(this.terms[t]);return this},M.prototype.divide=function(e){for(var r=this.coeff.divide(e.coeff),t=this.terms.length,i=new M(r,[],this.map),n=0;n<t;n++)i.terms[n]=this.terms[n].subtract(e.terms[n]),i.sum=i.sum.add(i.terms[n]);return i},M.prototype.multiply=function(e){for(var r=this.coeff.multiply(e.coeff),t=this.terms.length,i=new M(r,[],this.map),n=0;n<t;n++)i.terms[n]=this.terms[n].add(e.terms[n]),i.sum=i.sum.add(i.terms[n]);return i},M.prototype.isZero=function(){return this.coeff.equals(0)},M.prototype.toString=function(){return"{ coeff: "+this.coeff.toString()+", terms: ["+this.terms.join(",")+"]: sum: "+this.sum.toString()+", count: "+this.count+"}"},e.Utils.toMapObj=function(e){for(var r=0,t={},i=0;i<e.length;i++){var n=e[i];void 0===t[n]&&(t[n]=r,r++)}return t.length=r,t},e.Utils.filledArray=function(e,r,t){for(var i=[];r--;)i[r]=t?new t(e):e;return i},e.Utils.arrSum=function(e){for(var r=0,t=e.length,i=0;i<t;i++)r+=e[i];return r},e.Utils.haveIntersection=function(e,r){var t;return r.length>e.length&&(t=r,r=e,e=t),e.some(function(e){return r.indexOf(e)>-1})},e.Utils.subFunctions=function(r,t){t=t||{};var i=[];return r.each(function(r){if(r.group===o||r.previousGroup===o){var n=e.Utils.text(r,"hash"),a=t[n];if(a)i.push(r.altVar(a));else{var c=r.fname+f(t).length;t[n]=c,i.push(r.altVar(c))}}else r.group===u||r.group===s||r.group===l?i.push(e.Utils.subFunctions(r,t)):i.push(r.text())}),r.group===l||r.group===s?r.altVar(e.Utils.inBrackets(i.join("+"))):r.group===u?r.altVar(e.Utils.inBrackets(i.join("*"))):r.text()},e.Utils.getFunctionsSubs=function(e){var t={};for(var i in e)t[e[i]]=r.parse(i);return t};var q=e.Algebra={version:"1.4.5",init:void 0,proots:function(t,i){i=i||7;var a=[],o=function(e,r,t){for(var i=C(e,r,t).concat(a),n=0;n<0;n++)i.unshift(0);return i};if(t instanceof m&&t.isPoly()){if(t.distributeMultiplier(),t.group===s){var l=e.Utils.arrayMin(f(t.symbols)),u=t.symbols[l].clone().toUnitMultiplier();t=r.expand(r.divide(t,u)),a.push(0)}if(t.group===e.groups.S)return[0];if(t.group===e.groups.PL){var c=f(t.symbols),p=e.Utils.arrayMin(c);t=e.PARSER.divide(t,e.PARSER.parse(t.value+"^"+p))}for(var v=f(t.symbols).sort().pop(),g=t.group===e.groups.PL?t.symbols:t.symbols[v],b=g.group,w=(c=b===n?[g.power.toDecimal()]:f(g.symbols),[]),y=e.Utils.arrayMax(c),M=1;M<=y;M++){var q=0;-1!==c.indexOf(M+"")&&(q=b===n?g.multiplier:g.symbols[M].multiplier),w.unshift(q)}return w.push(t.symbols[d].multiplier),g.group===n&&(w[0]=g.multiplier),o(w,c,y)}if(e.Utils.isArray(t)){var S=t,U=(w=[],c=[],0);for(M=0;M<S.length;M++){for(var F=S[M][0],x=S[M][1],N=x-U-1,A=0;A<N;A++)w.unshift(0);w.unshift(F),0!==x&&c.push(x),U=x}return o(w,c,y=Math.max.apply(void 0,c))}throw new e.exceptions.NerdamerTypeError("Cannot calculate roots. Symbol must be a polynomial!");function C(r,t,n){var a=r.slice(0);if(r.unshift(n),n>100)throw new e.exceptions.ValueLimitExceededError("This utility accepts polynomials of degree up to 100. ");var o=[],s={};for(s.Degree=n,M=0;M<n;M++)o.push(0);var l=o.slice(0);function u(e,r,t,i,n,a){n[0]=a.b=i[0],n[1]=a.a=-r*a.b+i[1];for(var o=2;o<e;o++)n[o]=-(r*a.a+t*a.b)+i[o],a.b=a.a,a.a=n[o]}function f(e,r,t,i,n,a,o,s,l){var f=new Object,c=3;return f.b=f.a=0,u(r,o,s,a,l,f),n.c=f.a,n.d=f.b,Math.abs(n.c)<=100*e*Math.abs(a[r-1])&&Math.abs(n.d)<=100*e*Math.abs(a[r-2])?c:(n.h=s*i,Math.abs(n.d)>=Math.abs(n.c)?(c=2,n.e=t/n.d,n.f=n.c/n.d,n.g=o*i,n.a3=n.e*(n.g+t)+n.h*(i/n.d),n.a1=-t+n.f*i,n.a7=n.h+(n.f+o)*t):(c=1,n.e=t/n.c,n.f=n.d/n.c,n.g=n.e*o,n.a3=n.e*t+(n.g+n.h/n.c)*i,n.a1=-t*(n.d/n.c)+i,n.a7=n.g*n.d+n.h*n.f+t),c)}function c(e,r,t,i,n,a,o,s,l){var u;if(3!=t)if(u=1==t?n:i,Math.abs(a.a1)>10*e*Math.abs(u)){a.a7/=a.a1,a.a3/=a.a1,o[0]=l[0],o[1]=-l[0]*a.a7+l[1];for(f=2;f<r;f++)o[f]=-l[f-1]*a.a7+s[f-2]*a.a3+l[f]}else{o[0]=0,o[1]=-l[0]*a.a7;for(f=2;f<r;f++)o[f]=-l[f-1]*a.a7+s[f-2]*a.a3}else{o[1]=o[0]=0;for(var f=2;f<r;f++)o[f]=s[f-2]}}function p(e,r,t,i,n,a,o,s,l,u,f,c,h,p,v,m,d){var g,b,w,y,M,q,S,U;r.b=r.a=0,3!=e&&(2!=e?(g=t+h*o+c*u,b=s+(h+p*u)*l):(g=(t+f)*u+c,b=(u+h)*s+p*l),w=-v[m-1]/d[m],y=-(v[m-2]+w*d[m-1])/d[m],0!=(U=-(S=-((M=w*a)+(q=w*w*n))+p*y*i)+b+w*g)&&(r.a=-(h*(q+M)+p*(w*i+y*a))/U+h,r.b=p*(1+S/U)))}function v(e,r,t,i){var n,a,o;i.sr=i.si=i.lr=i.li=0,0!=e?0!=t?(n=r/2,Math.abs(n)<Math.abs(t)?(o=-(o=t>=0?e:-e)+n*(n/Math.abs(t)),a=Math.sqrt(Math.abs(o))*Math.sqrt(Math.abs(t))):(o=-e/n*(t/n)+1,a=Math.sqrt(Math.abs(o))*Math.abs(n)),o>=0?(a=n>=0?-a:a,i.lr=(-n+a)/e,i.sr=0!=i.lr?t/i.lr/e:i.sr):(i.lr=i.sr=-n/e,i.si=Math.abs(a/e),i.li=-i.si)):i.lr=-r/e:i.sr=0!=r?-t/r:i.sr}function m(e,r,t,i,n,a,o,s,l,h,m,d){var g,b,w,y,M,q,S,U,F,x,N,A=new Object,C=0,E=0;t.NZ=0,q=i,U=n;do{if(A.li=A.lr=A.si=A.sr=0,v(1,q,U,A),t.szr=A.sr,t.szi=A.si,t.lzr=A.lr,t.lzi=A.li,Math.abs(Math.abs(t.szr)-Math.abs(t.lzr))>.01*Math.abs(t.lzr))break;for(u(o,q,U,l,a,s),b=Math.abs(-t.szr*s.b+s.a)+Math.abs(t.szi*s.b),x=Math.sqrt(Math.abs(U)),g=2*Math.abs(a[0]),M=-t.szr*s.b,N=1;N<r;N++)g=g*x+Math.abs(a[N]);if(b<=20*(g=(9*(g=g*x+Math.abs(M+s.a))+2*Math.abs(M)-7*(Math.abs(s.a+M)+x*Math.abs(s.b)))*e)){t.NZ=2;break}if(++C>20)break;if(C>=2&&y<=.01&&b>=w&&!E){for(u(o,q-=q*(y=y<e?Math.sqrt(e):Math.sqrt(y)),U+=U*y,l,a,s),N=0;N<5;N++)c(e,r,f(e,r,s.a,s.b,m,d,q,U,h),s.a,s.b,m,d,h,a);E=1,C=0}w=b,c(e,r,f(e,r,s.a,s.b,m,d,q,U,h),s.a,s.b,m,d,h,a),p(f(e,r,s.a,s.b,m,d,q,U,h),s,s.a,m.a1,m.a3,m.a7,s.b,m.c,m.d,m.f,m.g,m.h,q,U,d,r,l),S=s.a,0!=(F=s.b)&&(y=Math.abs((-U+F)/F),q=S,U=F)}while(0!=F)}function d(e,r,t,i,n,a,o,s,l){var u,f,c,h,p,v,m,d,g,b,w,y=i-1;for(r.NZ=w=g=0,m=t.a;;){for(v=n[0],o[0]=v,b=1;b<a;b++)o[b]=v=v*m+n[b];for(c=Math.abs(v),h=Math.abs(m),u=.5*Math.abs(o[0]),b=1;b<a;b++)u=u*h+Math.abs(o[b]);if(c<=20*e*(2*u-c)){r.NZ=1,r.szr=m,r.szi=0;break}if(++w>10)break;if(w>=2&&Math.abs(d)<=.001*Math.abs(-d+m)&&c>p){g=1,r.a=m;break}for(p=c,l[0]=f=s[0],b=1;b<i;b++)l[b]=f=f*m+s[b];if(Math.abs(f)>10*Math.abs(s[y])*e)for(d=-v/f,s[0]=o[0],b=1;b<i;b++)s[b]=d*l[b-1]+o[b];else for(s[0]=0,b=1;b<i;b++)s[b]=l[b-1];for(f=s[0],b=1;b<i;b++)f=f*m+s[b];m+=d=Math.abs(f)>10*Math.abs(s[y])*e?-v/f:0}return g}function g(e,r,t,i,n,a,o,s,l,h,v,g){var b,w,y,M,q,S,U,F,x,N,A,C,E,P,k,D,T,L,V,R,z,I,_,j,O,Z=new Object,B=new Object,G=new Array(r),H=new Array(r),Q=1;for(g.NZ=0,M=y=.25,q=i,F=n,Z.b=Z.a=0,u(l,v,n,s,h,Z),b=Z.a,w=Z.b,B.h=B.g=B.f=B.e=B.d=B.c=B.a7=B.a3=B.a1=0,_=f(e,o,b,w,B,a,v,n,G),R=0;R<t;R++){if(L=1,c(e,o,_,b,w,B,a,G,h),p(_=f(e,o,b,w,B,a,v,n,G),Z,b,B.a1,B.a3,B.a7,w,B.c,B.d,B.f,B.g,B.h,v,n,a,o,s),k=Z.a,T=D=Z.b,N=0!=a[o-1]?-s[o]/a[o-1]:0,A=E=1,0!=R&&3!=_&&(E=0!=T?Math.abs((T-F)/T):E,A=0!=N?Math.abs((N-q)/N):A,P=E<U?E*U:1,C=A<S?A*S:1,j=P<M?1:0,(z=C<y?1:0)||j)){for(V=0;V<o;V++)H[V]=a[V];for(x=N,I=O=0;;){if(L&&0==(L=0)&&z&&(!j||C<P));else{if(m(e,o,g,k,D,h,l,Z,s,G,B,a),b=Z.a,w=Z.b,g.NZ>0)return;if(Q=O=1,M*=.25,I||!z)Q=0;else for(V=0;V<o;V++)a[V]=H[V]}if(0!=Q){if(Z.a=x,Q=d(e,g,Z,o,s,l,h,a,G),x=Z.a,g.NZ>0)return;if(I=1,y*=.25,0!=Q){k=-(x+x),D=x*x;continue}}for(V=0;V<o;V++)a[V]=H[V];if(!j||O)break}u(l,v,n,s,h,Z),_=f(e,o,b=Z.a,w=Z.b,B,a,v,n,G)}F=T,q=N,U=E,S=A}}!function(e,r,t,i){var n,a,o,s,l,u,f,c,h,p,m,d,b,w,y,M,q,S,U,F,x,N,A,C=e.Degree,E=Math.LN2,P=e.Degree+1,k=new Array(P),D=new Array(P),T=new Array(P),L=new Array(P),V=new Object,R=new Object;d=1;do{a=d,b=1+(d/=2)}while(b>1);var z=Number.MIN_VALUE/a,I=Math.cos(1.6406094968746698),_=Math.sin(1.6406094968746698),j=Math.sqrt(.5),O=-j;for(R.NZ=S=0,R.szr=R.szi=R.lzr=R.lzi=0;0==r[C];)t[S]=i[S]=0,C--,S++;for(N=C+1;C>=1;){if(C<=2){C<2?(t[e.Degree-1]=-r[1]/r[0],i[e.Degree-1]=0):(V.li=V.lr=V.si=V.sr=0,v(r[0],r[1],r[2],V),t[e.Degree-2]=V.sr,i[e.Degree-2]=V.si,t[e.Degree-1]=V.lr,i[e.Degree-1]=V.li);break}for(f=0,c=Number.MAX_VALUE,Z=0;Z<N;Z++)(p=Math.abs(r[Z]))>f&&(f=p),0!=p&&p<c&&(c=p);if(((h=z/c)<=1&&f>=10||h>1&&Number.MAX_VALUE/h>=f)&&(h=0==h?Number.MIN_VALUE:h,F=Math.floor(Math.log(h)/E+.5),1!=(l=Math.pow(2,F))))for(Z=0;Z<N;Z++)r[Z]*=l;for(var Z=0;Z<N;Z++)D[Z]=Math.abs(r[Z]);D[C]=-D[C],x=C-1,p=Math.exp((Math.log(-D[C])-Math.log(D[0]))/C),0!=D[x]&&(p=(m=-D[C]/D[x])<p?m:p),m=p;do{for(m=.1*(p=m),u=D[0],Z=1;Z<N;Z++)u=u*m+D[Z]}while(u>0);s=p;do{for(o=u=D[0],Z=1;Z<C;Z++)o=p*o+(u=p*u+D[Z]);p-=s=(u=p*u+D[C])/o}while(Math.abs(s/p)>.005);for(n=p,Z=1;Z<C;Z++)k[Z]=(C-Z)*r[Z]/C;for(k[0]=r[0],d=r[C],b=r[x],A=0==k[x]?1:0,U=0;U<5;U++)if(w=k[x],A){for(Z=0;Z<x;Z++)k[S=x-Z]=k[S-1];k[0]=0,A=0==k[x]?1:0}else{for(M=-d/w,Z=0;Z<x;Z++)k[S=x-Z]=M*k[S-1]+r[S];k[0]=r[0],A=Math.abs(k[x])<=Math.abs(b)*a*10?1:0}for(Z=0;Z<C;Z++)L[Z]=k[Z];for(U=1;U<=20;U++){if(q=-_*O+I*j,O=_*j+I*O,g(a,P,20*U,y=n*(j=q),n,k,C,r,N,T,-2*y,R),0!=R.NZ){for(t[S=e.Degree-C]=R.szr,i[S]=R.szi,C=(N-=R.NZ)-1,Z=0;Z<N;Z++)r[Z]=T[Z];1!=R.NZ&&(t[S+1]=R.lzr,i[S+1]=R.lzi);break}for(Z=0;Z<C;Z++)k[Z]=L[Z]}if(U>20){e.Degree-=C;break}}}(s,a,l,o);var b=o.length;for(M=0;M<b;M++){var w=h(o[M],i+8),y=h(l[M],i);y=i-String(y).length>2?y:l[M];var q=w<0?"-":"";0===y&&(y=""),0===w&&(w=""),w=1===Math.abs(w)?q+"i":w?w+"*i":"";var S=y&&w?y+"+"+w:y+w;l[M]=S.replace(/\+\-/g,"-")}return l}},roots:function(t){var i=q.proots(t).map(function(e){return r.parse(e)});return e.Vector.fromArray(i)},froot:function(r,t,i){return function(t){for(var n=i||e.Utils.build(e.Calculus.diff(r.clone())),a=r instanceof Function?r:e.Utils.build(r),o=!1,s=0;!o;){var l=t-a(t)/n(t),u=Math.abs(l)-Math.abs(t);t=l,Math.abs(u)<1e-12?o=!0:s>1e4&&(t=null,o=!0),s++}return t}(Number(t))},quad:function(e,t,i){var n=function(e,t,i,n){return r.parse("-("+t+"+"+n+"*sqrt(("+t+")^2-4*("+e+")*("+i+")))/(2*"+e+")")};return[n(e,t,i,1),n(e,t,i,-1)]},sumProd:function(e,r){return q.quad(-r,e,-1).map(function(e){return e.invert()})},coeffs:function(e,t,i){t=String(t),e=r.expand(e),i=i||[new m(0)],e.group===a&&e.contains(t,!0)&&r.error("Unable to get coefficients using expression "+e.toString());var n=c(e);if(n.length<=1&&n[0]===t)for(var o=new w(e).coeffs.map(function(e){return new m(e)}),s=0,f=o.length;s<f;s++){var h=o[s];(d=i[s])&&(h=r.add(d,h)),i[s]=h}else if(t||r.error("Polynomial contains more than one variable. Please specify which variable is to be used!"),-1===n.indexOf(t))i[0]=r.add(e,i[0]);else if(i=i||[new m(0)],e.group===u){var p=e.symbols[t];p||r.error("Expression is not a polynomial!");var d,g=Number(p.power);((h=r.divide(e.clone(),p.clone())).contains(t,!0)||g<0||!v(g))&&r.error("Expression is not a polynomial!"),(d=i[g])&&(h=r.add(d,h)),i[g]=h}else e.group===l&&e.each(function(e){q.coeffs(e.clone(),t,i)},!0);for(s=0,f=i.length;s<f;s++)void 0===i[s]&&(i[s]=new m(0));return i},polyPowers:function(r,i,a){if(a=a||[],(h=h=r.group)===s&&i===r.value)a=a.concat(f(r.symbols));else if(h===l)for(var o in r.symbols){var c=r.symbols[o],h=c.group,p=c.value;if(h===n&&i===p)a.push(c.power);else if(h===s||h===l)a=q.polyPowers(c,i,a);else if(h===u&&c.contains(i)){var v=c.symbols[i];v&&a.push(v.power)}else h!==t&&i===p||a.push(0)}else h===u&&r.contains(i)&&a.push(e.Utils.decompose_fn(r,i,!0).x.power);return e.Utils.arrayUnique(a).sort()},Factor:{mix:function(e,r){for(var t=f(e),i=t.length,n=[],a=0;a<i;a++){for(var o=t[a],s=e[o],l=n.length,u=0;u<l;u++){var c=n[u]*o;n.push(c),r&&n.push(-c)}for(u=1;u<=s;u++)n.push(Math.pow(o,u))}return n},common:function(t,i){try{if(t.group===l){for(var n=r.expand(t.clone(),!0).collectSymbols(null,null,function(e,r){return(r.length||1)-(e.length||1)}),a={},o=[],s=0;s<n.length;s++){var u=n[s];o.push(u.multiplier.clone()),u.each(function(e){var r=Number(e.power);if(isNaN(r))throw new Error("exiting");e.value in a?(r<a[e.value][0]&&(a[e.value][0]=r),a[e.value][1].push(e)):a[e.value]=[r,[e]]})}var f=new m(1);for(var c in a)a[c][1].length===n.length&&(f=r.multiply(f,r.pow(new m(c),new m(a[c][0]))));var h=e.Math2.QGCD.apply(null,o);if(!h.equals(1)){i.add(new m(h));for(s=0;s<n.length;s++)n[s].multiplier=n[s].multiplier.divide(h)}if(!f.equals(1)){i.add(f),t=new m(0);for(s=0;s<n.length;s++)t=r.add(t,r.divide(n[s],f.clone()))}}}catch(e){}return t},zeroes:function(t,i){var n=function(){throw new e.exceptions.ValueLimitExceededError("Exiting")};try{var a,o,s,u,f;f=(t=r.expand(t.clone())).toString(),a=c(t),s=new m(0);for(var h=[],d=[],g=0,b=a.length;g<a.length;g++){for(var w={},y=0;y<b;y++)g!==y&&(w[a[y]]=0);var M=(o=r.parse(f,w)).power;v(M)||n(),h.push(o),d.push(o.power)}if(e.Utils.allSame(d)){var S=t.length,U=h.length;2===U&&(u=new p(d[0]/(S-1))),3===U&&(u=new p(d[0]/Math.round((Math.sqrt(8*S-1)-3)/2)))}else u=e.Math2.QGCD.apply(null,d);v(u)||n();for(g=0;g<h.length;g++){var F=h[g],x=F.power.clone().divide(u);F.multiplier=new p(Math.pow(F.multiplier,1/x)),F.power=u.clone(),s=r.add(s,F)}if(s.group!==l)return t;for(;;){var N=q.div(t.clone(),s.clone());if(!N[1].equals(0))break;if(t=N[0],i.add(s.clone()),t.equals(1))break}}catch(f){}return t},factor:function(t,i){if(t.group===l){var a=new m(0);t.each(function(e){(e.group===l&&e.power.greaterThan(1)||e.group===u)&&(e=r.expand(e)),a=r.add(a,e)}),a.power=t.power,t=a}t.group===o&&"sqrt"!==t.fname&&(t=e.Utils.evaluate(t));try{if(t.group===u){var s=!0;return(i=i||new y).add(new m(t.multiplier)),t.each(function(e){e.group!==n&&(s=!1);var r=q.Factor.factor(e.clone());i.add(r)}),s?t:i.toSymbol()}if(t.group===n)return t;if(t.isConstant())return t.equals(1)?t.clone():e.Math2.factor(t);var h=t.power.clone();if(v(h)&&(!h.lessThan(0)||t.group!==o)){var p=h.sign();t.toLinear(),i=i||new y;var d={};t=r.parse(e.Utils.subFunctions(t,d)),f(d).length>0&&(i.preAdd=function(t){return r.parse(t,e.Utils.getFunctionsSubs(d))}),t.isLinear()||(i.pFactor=t.power.toString(),t.toLinear());var g=c(t).length>1;if(g){s=!0;var b=!0;if(t.each(function(e){e.group!==n&&(s=!1),e.multiplier.equals(1)||(b=!1)}),s&&b)return r.pow(r.parse(t,e.Utils.getFunctionsSubs(d)),r.parse(h))}var w=new y;t=q.Factor.coeffFactor(t,w),w.each(function(e){p<0&&e.invert(),i.add(e)});var M=new y;if(t=q.Factor.powerFactor(t,M),M.each(function(e){p<0&&e.invert(),i.add(e)}),g)t=q.Factor.mfactor(t,i),i.each(function(e){p<0&&e.power.negate()});else{t=q.Factor.squareFree(t,i);var S=new y;for(var U in t=q.Factor.trialAndError(t,S),S.factors)i.add(r.pow(S.factors[U],r.parse(h)))}return t=r.parse(t,e.Utils.getFunctionsSubs(d)),i.add(r.pow(t,r.parse(h))),i.toSymbol()}return t}catch(e){return t}},reduce:function(e,t){if(e.group===l&&2===e.length){var i=e.collectSymbols().sort(function(e,r){return r.multiplier-e.multiplier});if(i[0].power.equals(i[1].power)){var n=r.parse(i[0].power),a=i[0].clone().toLinear(),o=i[1].clone().toLinear();t.add(r.add(a.clone(),o.clone())),o.negate();for(var s=Number(n),u=new m(0),f=1;f<=s;f++){var c=r.pow(a.clone(),r.subtract(n.clone(),new m(f))),h=r.pow(o.clone(),r.subtract(new m(f),new m(1)));u=r.add(u,r.multiply(c,h))}return u}}return e},squareFree:function(e,r){if(e.isConstant()||e.group===n)return e;var t=new w(e).squareFree(),i=t[2];if(1!==i){var a=t[1].toSymbol();return a.power=a.power.multiply(new p(i)),r.add(q.Factor.factor(a)),q.Factor.squareFree(t[0].toSymbol(),r)}return e},powerFactor:function(t,i){if(t.group!==s)return t;var n=e.Utils.arrayMin(f(t.symbols)),a=new m(0),o=r.parse(t.value+"^"+n);return t.each(function(e){e=r.divide(e,o.clone()),a=r.add(a,e)}),i.add(o),a},coeffFactor:function(r,t){if(r.isComposite()){var i=e.Math2.QGCD.apply(null,r.coeffs());i.equals(1)||r.each(function(e){e.isComposite()?e.each(function(e){e.multiplier=e.multiplier.divide(i)}):e.multiplier=e.multiplier.divide(i)}),r.updateHash(),t&&t.add(new m(i))}return r},trialAndError:function(r,t){if(r.isConstant()||r.group===n)return r;for(var i=new w(r),a=i.coeffs[0],o=e.Math2.ifactor(a),s=q.proots(r),l=0;l<s.length;l++){var u=s[l],f=1;if(!isNaN(u)){for(var c in o){var h=e.Utils.round(Math.log(c)/Math.log(Math.abs(u)),8);if(v(h)){u=c,f=h;break}}var m=new p(u),d=[new p(m.num).negate()];d[f]=new p(m.den);var g=w.fromArray(d,i.variable).fill(),b=i.divide(g);b[1].equalsNumber(0)&&(i=b[0],t.add(g.toSymbol()))}}return i.equalsNumber(1)||(i=q.Factor.search(i,t)),i.toSymbol()},search:function(r,t,i){i=i||10;var n=r.variable,a=function(e,a,o,s){var l=w.fit(e,a,o,i,s,n);if(l&&l.coeffs.length>1){var u=r.divide(l);if(u[1].equalsNumber(0))return t.add(l.toSymbol()),[u[0],l]}return null},o=r.coeffs[0],s=e.Math2.ifactor(o),l=r.lc(),u=e.Math2.ifactor(l),f=r.sub(i),c=q.Factor.mix(e.Math2.ifactor(f),f<0),h=Math.ceil(r.coeffs.length/2),p=l.lessThan(0),v=o.lessThan(0);for(u[1]=1,s[1]=1;h--;)for(var m in u)for(var d in s)for(var g=0;g<c.length;g++){var b=a(m,d,c[g],h);if(b)return r=b[0],e.Utils.isPrime(r.sub(i))||(r=q.Factor.search(r,t)),r;b||(p&&v?b=a(-m,-d,c[g],h):p?b=a(-m,d,c[g],h):v&&(b=a(m,-d,c[g],h)))}return r},mSqfrFactor:function(r,t){if(r.group!==o)for(var i=c(r).reverse(),n=0;n<i.length;n++)do{if(i[n]!==r.value){var a=q.Factor.coeffFactor(e.Calculus.diff(r,i[n]));if(a.equals(0))break;var s=q.div(r,a.clone()),l=s[1].equals(0);if(s[0].isConstant()){t.add(s[0]);break}l&&(t.add(s[0]),r=a)}else t.add(r),r=new m(1)}while(l);return r},sqdiff:function(t,i){try{var a,o,s=function(r){return e.Utils.block("POSITIVE_MULTIPLIERS",function(){return m.unwrapPARENS(g.sqrt(g.abs(r)))},!0)},l=e.Utils.separate(t.clone()),u=[];for(var f in l)"constants"!==f&&u.push(l[f]);if(u.sort(function(e,r){return r.power-e.power}),2===u.length)if(a=u.pop(),o=u.pop(),a.isComposite()&&o.power.equals(2)){o=s(o);var c=q.Factor.factor(r.add(a,l.constants));c.power.equals(2)&&(c.toLinear(),i.add(r.subtract(c.clone(),o.clone())),i.add(r.add(c,o)),t=new m(1))}else if(a=a.powSimp(),o=o.powSimp(),(a.group===n||""===a.fname)&&a.power.equals(2)&&(o.group===n||""===o.fname)&&o.power.equals(2)){if(a.multiplier.lessThan(0)){var h=o;o=a,a=h}a.multiplier.greaterThan(0)&&(a=s(a),o=s(o)),i.add(r.subtract(a.clone(),o.clone())),i.add(r.add(a,o)),t=new m(1)}}catch(e){}return t},mfactor:function(t,i){if(t.group===o)if("sqrt"===t.fname){var n=new y,a=q.Factor.common(t.args[0].clone(),n);a=q.Factor.coeffFactor(a,n),t=r.symfunction("sqrt",[a]),n.each(function(i){t=r.multiply(t,r.parse(e.Utils.format("sqrt({0})",i)))})}else i.add(t);else{t=q.Factor.mSqfrFactor(t,i);for(var s=c(t),l=t.collectSymbols().map(function(e){return m.unwrapSQRT(e)}),u={},f={},h=s.length,p=l.length,d=0;d<h;d++){var g=s[d];u[g]=new m(0);for(var b=0;b<p;b++){var w=l[b];if(w.contains(g)){var M=w.value===g?w.power.toDecimal():w.symbols[g].power.toDecimal();(!f[g]||M<f[g])&&(f[g]=M),u[g]=r.add(u[g],w.clone())}}}for(var S in u){var U=r.parse(S+"^"+f[S]),F=r.divide(u[S],U),x=r.expand(F);if(x.equals(1))break;var N=q.div(t.clone(),x);if(N[0].equals(0))break;var A=v(x)&&x.lessThan(0);if(N[1].equals(0)&&!A){var C=q.div(t.clone(),N[0].clone());U=C[0];if(t=C[1],U.equals(-1))return t;var E=N[0];if(t.equals(E)){var P=q.Factor.reduce(E,i);if(!t.equals(P))return q.Factor.mfactor(P,i)}else if(i.add(E),t.equals(0))return U;return U.isConstant("all")?(i.add(U),U):q.Factor.mfactor(U,i)}}}return t=q.Factor.sqdiff(t,i),t=q.Factor.zeroes(t,i)}},allLinear:function(e){for(var r=e.length,t=0;t<r;t++)if(!q.isLinear(e[t]))return!1;return!0},isLinear:function(e){var r=!1,i=e.group;if(i===s||i===l)for(var f in r=!0,e.symbols){var c=e.symbols[f],h=c.group;if(h===o||h===a||h===u)r=!1;else if(h===s||h===l)r=q.isLinear(c);else if(c.group!==t&&"1"!==c.power.toString()){r=!1;break}}else i===n&&1===e.power&&(r=!0);return r},gcd:function(){var t;if(0==(t=1===arguments.length&&arguments[0]instanceof e.Vector?arguments[0].elements:e.Utils.arguments2Array(arguments)).length)return new m(1);if(1==t.length)return t[0];for(var i=[],n=!1,a=0;a<t.length;a++)if(t[a].group===o&&"gcd"===t[a].fname)(t=t.concat(arguments[a].args)).splice(a,1);else{var s=c(t[a]);if(e.Utils.haveIntersection(s,i)){n=!0;break}i=i.concat(s)}if(n||0===i.length){if(t.every(function(e){return e.getDenom().equals(1)})){var l=t[0];for(a=1;a<t.length;a++)l=q.gcd_(t[a],l);return l}return r.divide(q.gcd.apply(null,t.map(function(e){return e.getNum()})),q.lcm.apply(null,t.map(function(e){return e.getDenom()})))}return r.symfunction("gcd",t)},gcd_:function(t,n){if(t.group!==o&&t.group!==i||(t=e.Utils.block("PARSE2NUMBER",function(){return r.parse(t)})),n.group===o&&(n=e.Utils.block("PARSE2NUMBER",function(){return r.parse(n)})),t.isConstant()&&n.isConstant())return new m(e.Math2.QGCD(new p(+t),new p(+n)));var s=r.multiply(t.getDenom()||new m(1),n.getDenom()||new m(1)).invert();if(t=r.multiply(t.clone(),s.clone()),n=r.multiply(n.clone(),s.clone()),t=r.expand(t),n=r.expand(n),t.group===u||n.group===u){var l=r.divide(t.clone(),n.clone());if(!(g=r.multiply(n.clone(),l.getDenom().invert())).equals(1))return g}if(t.group===a||n.group===a){var f=new m(e.Math2.GCD(t.multiplier,n.multiplier)),h=q.gcd_(t.value===d?new m(1):r.parse(t.value),n.value===d?new m(1):r.parse(n.value)),v=q.gcd_(r.parse(t.power),r.parse(n.power));return r.multiply(f,r.pow(h,v))}if(t.length<n.length){var g=t;t=n,n=g}var b,y=c(t),M=c(n);if(y.length===M.length&&1===y.length&&y[0]===M[0]||1===y.length&&0===M.length||0===y.length&&1===M.length)return t=new w(t),n=new w(n),r.divide(t.gcd(n).toSymbol(),s);for(;!n.equals(0);){g=n.clone();if(t=t.clone(),b=q.div(t,g),n=b[1],b[0].equals(0))return r.divide(new m(e.Math2.QGCD(t.multiplier,n.multiplier)),s);t=g}var S=[];t.each(function(e){S.push(e.multiplier)});var U=e.Math2.QGCD.apply(void 0,S);return U.equals(1)||t.each(function(e){e.multiplier=e.multiplier.divide(U)}),!t.equals(1)||t.isConstant()||n.isConstant()?r.divide(t,s):r.divide(r.symfunction("gcd",arguments),s)},lcm:function(){var t;1===arguments.length?arguments[0]instanceof e.Vector?t=arguments[0].elements:r.error("lcm expects either 1 vector or 2 or more arguments"):t=e.Utils.arguments2Array(arguments);var i=t.reduce(function(e,t){return r.multiply(e,t.clone())},new m(1)),n=function(e,r){var t,i,n,a=[],o=Math.pow(2,e.length);for(i=r;i<o;i++){t=[],n=e.length-1;do{0!=(i&1<<n)&&t.push(e[n])}while(n--);t.length==r&&a.push(t)}return a}(arguments,arguments.length-1).map(function(e){return e.reduce(function(e,t){return r.multiply(e,t.clone())},new m(1))});if(t.every(function(r){return e.Utils.isVariableSymbol(r)}))var a=r.symfunction("gcd",e.Utils.arrayUnique(n));else a=q.gcd.apply(null,n);return r.divide(i,a)},divide:function(t,i){var n,a,o,s;return(s=(o=e.Algebra.Factor.factor(t.clone())).getDenom()).isConstant("all")?s=new m(1):t=r.expand(m.unwrapPARENS(r.multiply(o,s.clone()))),n=q.div(t,i),a=r.divide(n[1],i),r.divide(r.add(n[0],a),s)},div:function(t,i){if(i.isConstant())return t.each(function(e){e.multiplier=e.multiplier.divide(i.multiplier)}),[t,new m(0)];if(t=r.expand(t),i=r.expand(i),t.group===n&&i.group===l){var a=t.value,o=e.Utils.decompose_fn(i.clone(),a,!0);if(t.isLinear()&&o.x&&o.x.isLinear()&&i.isLinear()){var s=m.create(t.multiplier);return[r.divide(s.clone(),o.a.clone()),r.divide(r.multiply(s,o.b),o.a).negate()]}}if(t.group===n&&i.group===n){var u=r.divide(t.clone(),i.clone());return u.isConstant()?[u,new m(0)]:[new m(0),t.clone()]}var f=t.hasFunc(),h=i.hasFunc(),v=!1;if(f||h){v=!0;var g={},b=(t=r.parse(e.Utils.subFunctions(t,g)),i=r.parse(e.Utils.subFunctions(i,g)),e.Utils.getFunctionsSubs(g))}var y,q,S,U,F=e.Utils.arrayUnique(c(t).concat(c(i)));if(1===F.length){y=(Q=new w(t).divide(new w(i)))[0].toSymbol(),q=Q[1].toSymbol()}else{F.push(d);var x=function(e){for(var t=new m(0),i=0;i<e.length;i++){var n=e[i].toSymbol();t=r.add(t,n)}return t},N=function(e,r){var t,i=Math.max.apply(null,e.terms),n=0;if(!r)for(var a=0;a<e.terms.length;a++)if(e.terms[a].equals(i)&&(t=a,n++),n>1)return;if(r)for(a=0;a<e.terms.length;a++)if(e.terms[a].equals(i)){t=a;break}return[i,t,e]},A=function(e,r){var t=e[r=r||0],i=e.length;if(t){for(var n,a,o=N(t),s=r+1;s<i;s++){var l=e[s],u=t.sum.equals(l.sum);if(!u&&o)break;if(u){for(var f,c,h,p,v=t.terms.length,m=0;m<v;m++){var d=t.terms[m],g=l.terms[m];(void 0===f||d.greaterThan(f))&&(f=d,h=m),(void 0===c||g.greaterThan(c))&&(c=g,p=m)}var b=f.subtract(l.terms[h]),w=c.subtract(t.terms[p]);if(w>b){o=[c,p,l];break}if(b>w){o=[f,h,t];break}}else if(o=N(l))break;o=N(l)}if(!o)return N(e[0],!0);for(s=0;s<D.length;s++){var y=D[s].terms;if((a=o[1])===y.length-1)return;if(!(n=y[a]).equals(0))break}return n.equals(0)?A(e,++r):o}},C=e.Utils.toMapObj(F),E=function(e,r){return r.sum.subtract(e.sum)},P=function(e,r){if(!e||!r)return!1;for(var t=0;t<e.terms.length;t++)if(e.terms[t].lessThan(r.terms[t]))return!1;return!0},k=t.tBase(C).sort(E),D=i.tBase(C).sort(E),T=P(k[0],D[0])&&k[0].count>D[0].count?D:k,L=A(T),V=[];if(L){var R=L[1],z=function(e,r){var t=e.len(),i=r.len(),n=r.terms[R],a=e.terms[R];return i>t&&n.greaterThan(a)?i-t:n.subtract(a)};R=function(e,r,t){for(var i=[],n=0;n<e.length;n++)for(var a=e[n],o=0;o<a.terms.length;o++){var s=i[o],l=a.terms[o];0===n?i[o]=l:s&&!s.equals(l)&&(i[o]=void 0)}for(n=0;n<i.length;n++)if((a=i[n])&&!a.equals(0))return n;return t}(k,0,R),k.sort(z),D.sort(z);var I=D[0],_=k[0],j=new M(new p(1),[],_.map);if(I.sum.greaterThan(_.sum)&&_.len()>1)for(var O=0;O<_.terms.length;O++){var Z=I.terms[O].subtract(_.terms[O]);if(Z.equals(0))j.terms[O]=new p(0);else{var B=Z.add(new p(1));j.terms[O]=Z;for(var G=0;G<k.length;G++)k[G].terms[O]=k[G].terms[O].add(B)}}for(var H=P(k[0],D[0]);H&&(U=D,!(S=k)[0].sum.equals(U[0].sum)||S.length>=U.length);){var Q=k[0].divide(D[0]);V.push(Q),k.shift();for(O=1;O<D.length;O++){var X=D[O].multiply(Q).generateImage(),Y=k.length;0===Y&&(X.coeff=X.coeff.neg(),k.push(X),k.sort(z));for(G=0;G<Y;G++){var J=k[G];if(J.getImg()===X.getImg()){J.coeff=J.coeff.subtract(X.coeff),J.coeff.equals(0)&&(e.Utils.remove(k,G),G--);break}G===Y-1&&(X.coeff=X.coeff.neg(),k.push(X),k.sort(z))}}if(!(H=P(k[0],D[0]))&&k.length>=D.length)for(O=1;O<k.length;O++)if(H=P(k[O],D[0])){k.unshift(e.Utils.remove(k,O));break}}}y=x(V),q=x(k),void 0!==j&&(j=j.toSymbol(),y=r.divide(y,j.clone()),q=r.divide(q,j))}return v&&(y=r.parse(y.text(),b),q=r.parse(q.text(),b)),[y,q]},line:function(t,i,n){e.Utils.isArray(t)&&(t=e.Utils.convertToVector(t)),e.Utils.isArray(i)&&(i=e.Utils.convertToVector(i)),n=r.parse(n||"x"),e.Utils.isVector(t)&&e.Utils.isVector(i)||r.error('Line expects a vector! Received "'+t+'" & "'+i+'"');var a=r.subtract(i.e(1).clone(),t.e(1).clone()),o=r.subtract(i.e(2).clone(),t.e(2).clone()),s=r.divide(o,a),l=r.multiply(n,s.clone()),u=r.multiply(t.e(1).clone(),s);return r.add(r.subtract(l,u),t.e(2).clone())},PartFrac:{createTemplate:function(e,t,i,a){var s,l,u,f,c,h,p;(e=q.Factor.factor(e)).each(function(r,t){if(r.group===o&&""===r.fname&&r.args[0].group===n){var i=r.args[0];this.symbols?(delete this.symbols[t],this.symbols[i.value]=i):e=r.args[0]}}),s=t.collectFactors(),l=[],h=[],p=new m(1);for(var v=0;v<s.length;v++){var d=m.unwrapPARENS(s[v]);if(d.power.greaterThan(1)){f=Number(d.power),u=d.clone().toLinear(),c=Number(q.degree(u,a));for(var g=0;g<f;g++){var b=r.pow(u.clone(),new m(g+1));i.push(b.clone());var w=r.divide(e.clone(),b.clone());h.push(c),l.push(w)}}else{c=Number(q.degree(d,a)),i.push(d);w=r.divide(e.clone(),d.clone());w=r.expand(m.unwrapPARENS(w)),h.push(c),l.push(w)}}return[i=i.map(function(e){return r.multiply(e,p.clone())}),l,h]},partfrac:function(t,i,n){var a=c(t);i=i||r.parse(a[0]);try{var o,s,l,u,f,h,p,v,d,g,b,w,y,M,S,U,F,x,N;if(o=r.expand(t.getNum()),s=r.expand(t.getDenom().toUnitMultiplier()),o.multiplier=t.multiplier,f=o.groupTerms(i),Number(q.degree(o,i))>=Number(q.degree(s,i))?(y=(w=q.div(o.clone(),r.expand(s.clone())))[0],f=(o=w[1]).groupTerms(i)):y=new m(0),1===Number(q.degree(s,i))){var A=r.divide(o,s);return n?[y,A]:r.add(y,A)}u=q.Factor.factor(s),U=q.PartFrac.createTemplate(s.clone(),u,[],i),F=U[0],M=U[1],h=U[2],b=[f.length],p=[],l=[],S=[],M.map(function(e,t){x=F[t],N=h[t];for(var n=0;n<N;n++){l.push(x.clone());var a=m.create(i,n),o=r.expand(r.multiply(e,a.clone())).groupTerms(i),s=o.length;b.push(s),p.push(o),S.push(a.clone())}}),v=e.Utils.arrayMax(b),g=new e.Matrix(e.Utils.fillHoles(f,v)).transpose(),d=new e.Matrix;for(var C=0;C<p.length;C++)d.elements.push(e.Utils.fillHoles(p[C],v));var E=r.multiply(d.transpose().invert(),g),P=n?[y]:y;return E.each(function(e,t){var i=r.multiply(S[t],r.divide(e,l[t]));n?P.push(i):P=r.add(P,i)}),P}catch(e){}return t}},degree:function(t,i,o){if(o=o||{nd:[],sd:[],depth:0},!i){var s=c(t);if(s.length>1)throw new Error("You must specify the variable for multivariate polynomials!");if(0===s.length)return new m(0);i=r.parse(s[0])}var l=t.group;t.isComposite()?((t=t.clone()).distributeExponent(),t.each(function(e){o.depth++,q.degree(e,i,o),o.depth--})):t.group===u?t.each(function(e){o.depth++,q.degree(e,i,o),o.depth++}):l===a&&t.value===i.value?o.sd.push(t.power.clone()):l===n&&t.value===i.value?o.nd.push(r.parse(t.power)):o.nd.push(new m(0));var f=o.nd.length>0?e.Utils.arrayMax(o.nd):void 0;return 0===o.depth&&o.sd.length>0?(void 0!==f&&o.sd.unshift(f),r.symfunction("max",o.sd)):(e.Utils.isSymbol(f)||(f=r.parse(f)),f)},sqComplete:function(t,i,n){e.Utils.isSymbol(i)||(i=r.parse(i));var a,o,s,l,u,f,c,h,p,v,d,b=function(r){throw r=r||"Stopping",new e.exceptions.ValueLimitExceededError(r)};return t.isPoly()||b("Must be a polynomial!"),p=e.Utils.inBrackets,t=t.clone(),(a=e.Algebra.degree(t,i)).equals(2)||b("Cannot complete square for degree "+a),o=(c=e.Algebra.coeffs(t,i))[2],h=c[1].sign(),s=r.divide(c[1],new m(2)),l=r.pow(s.clone(),new m(2)),n?[o,s,u]:(d=g.sqrt(o),f=r.divide(g.sqrt(l),d.clone()),u=r.subtract(c[0],r.pow(f.clone(),new m(2))),{a:v=r.parse(p(d.clone()+"*"+i+(h<0?"-":"+")+f)),c:u,f:r.add(r.pow(v.clone(),new m(2)),u.clone())})},trigSimp:function(r,t){for(var i in r=r.clone(),t=t||{},r.each(function(e){if(e.group===o){var r=e.args.toString(),i=t[r];i?i.push(e):t[r]=[e]}},!0),t){var n,a;if(t[i].map(function(e,r){e.power.equals(2)&&!e.multiplier.isNegative()&&("cos"===e.fname?a=e:"sin"===e.fname&&(n=e))}),n&&a){var s=a.multiplier.add(n.multiplier),l=s.mod(new e.Frac(s.greaterThan(new e.Frac(2))?2:1)),u=s.subtract(l).divide(new e.Frac(2));a.multiplier.greaterThan(n.multiplier)?(n.power=new e.Frac(0),n.multiplier=u,a.multiplier=l):n.multiplier.greaterThan(a.multiplier)?(a.power=new e.Frac(0),a.multiplier=u,n.multiplier=l):(a.multiplier=u,a.power=new e.Frac(0),n.multiplier=new e.Frac(0))}}return r},simplify:function(t){if(t.isConstant()||t.group===e.groups.S)return t;var i;if(t=t.clone(),i=q.trigSimp(t),(i=q.Factor.factor(i)).group===e.groups.CP&&i.isLinear()){var n=i.multiplier.clone();i.toUnitMultiplier();var a=new m(0);i.each(function(e){var t=q.simplify(e);a=r.add(a,t)}),a.multiplier=a.multiplier.multiply(n),i=a}return b(i)},Classes:{Polynomial:w,Factors:y,MVTerm:M}};nerdamer.useAlgebraDiv=function(){var t=q.divideFn=r.divide,i=0;r.divide=function(r,n){var a;return a=1===++i?e.Algebra.divide(r,n):t(r,n),i=0,a}},nerdamer.useParserDiv=function(){q.divideFn&&(r.divide=q.divideFn),delete q.divideFn},nerdamer.register([{name:"factor",visible:!0,numargs:1,build:function(){return q.Factor.factor}},{name:"simplify",visible:!0,numargs:1,build:function(){return q.simplify}},{name:"gcd",visible:!0,numargs:[1],build:function(){return q.gcd}},{name:"lcm",visible:!0,numargs:[1],build:function(){return q.lcm}},{name:"roots",visible:!0,numargs:-1,build:function(){return q.roots}},{name:"divide",visible:!0,numargs:2,build:function(){return q.divide}},{name:"div",visible:!0,numargs:2,build:function(){return q.div}},{name:"partfrac",visible:!0,numargs:[1,2],build:function(){return q.PartFrac.partfrac}},{name:"deg",visible:!0,numargs:[1,2],build:function(){return q.degree}},{name:"coeffs",visible:!0,numargs:[1,2],build:function(){return function(){var r=q.coeffs.apply(q,arguments);return new e.Vector(r)}}},{name:"line",visible:!0,numargs:[2,3],build:function(){return q.line}},{name:"sqcomp",visible:!0,numargs:[1,2],build:function(){return function(e,r){try{return r=r||c(e)[0],q.sqComplete(e.clone(),r).f}catch(r){return e}}}}]),nerdamer.api()}();