kld-polynomial
Version:
A class of simple polynomial functionality including root finding
3 lines (2 loc) • 11.7 kB
JavaScript
function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e,t){for(var r=0;r<t.length;r++){var a=t[r];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}function r(e,r,a){return r&&t(e.prototype,r),a&&t(e,a),e}function a(e){return(a=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function n(e,t){return(n=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function s(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function o(e,t,r){return(o="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,t,r){var n=function(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=a(e)););return e}(e,t);if(n){var s=Object.getOwnPropertyDescriptor(n,t);return s.get?s.get.call(r):s.value}})(e,t,r||e)}function i(e){return"number"==typeof e?e?e<0?-1:1:e==e?e:NaN:NaN}var u=function(){function t(){e(this,t),this.coefs=[];for(var r=arguments.length-1;r>=0;r--)this.coefs.push(r<0||arguments.length<=r?void 0:arguments[r]);this._variable="t",this._s=0}return r(t,[{key:"clone",value:function(){var e=new t;return e.coefs=this.coefs.slice(),e}},{key:"eval",value:function(e){if(isNaN(e))throw new TypeError("Parameter must be a number. Found '".concat(e,"'"));for(var t=0,r=this.coefs.length-1;r>=0;r--)t=t*e+this.coefs[r];return t}},{key:"add",value:function(e){for(var r=new t,a=this.getDegree(),n=e.getDegree(),s=Math.max(a,n),o=0;o<=s;o++){var i=o<=a?this.coefs[o]:0,u=o<=n?e.coefs[o]:0;r.coefs[o]=i+u}return r}},{key:"multiply",value:function(e){for(var r=new t,a=0;a<=this.getDegree()+e.getDegree();a++)r.coefs.push(0);for(var n=0;n<=this.getDegree();n++)for(var s=0;s<=e.getDegree();s++)r.coefs[n+s]+=this.coefs[n]*e.coefs[s];return r}},{key:"divideEqualsScalar",value:function(e){for(var t=0;t<this.coefs.length;t++)this.coefs[t]/=e}},{key:"simplifyEquals",value:function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1e-12,t=this.getDegree();t>=0&&Math.abs(this.coefs[t])<=e;t--)this.coefs.pop()}},{key:"removeZerosEquals",value:function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1e-15,t=this.coefs,r=10*e*Math.abs(t.reduce(function(e,t){return Math.abs(t)>Math.abs(e)?t:e})),a=0;a<t.length-1;a++)Math.abs(t[a])<r&&(t[a]=0);return this}},{key:"monicEquals",value:function(){var e=this.coefs;return 1!==e[e.length-1]&&this.divideEqualsScalar(e[e.length-1]),this}},{key:"toString",value:function(){for(var e=[],t=[],r=this.coefs.length-1;r>=0;r--){var a=Math.round(1e3*this.coefs[r])/1e3;if(0!==a){var n=a<0?" - ":" + ";a=Math.abs(a),r>0&&(1===a?a=this._variable:a+=this._variable),r>1&&(a+="^"+r),t.push(n),e.push(a)}}t[0]=" + "===t[0]?"":"-";for(var s="",o=0;o<e.length;o++)s+=t[o]+e[o];return s}},{key:"bisection",value:function(e,t){var r,a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:1e-6,n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:15,s=this.eval(e),o=this.eval(t);if(Math.abs(s)<=a)r=e;else if(Math.abs(o)<=a)r=t;else if(s*o<=0)for(var i=Math.log(t-e),u=Math.LN10*n,h=Math.ceil((i+u)/Math.LN2),f=0;f<h;f++){r=.5*(e+t);var c=this.eval(r);if(Math.abs(c)<=a)break;c*s<0?(t=r,o=c):(e=r,s=c)}return r}},{key:"trapezoid",value:function(e,t,r){if(isNaN(e)||isNaN(t)||isNaN(r))throw new TypeError("Parameters must be numbers");var a=t-e;if(1===r){var n=this.eval(e),s=this.eval(t);this._s=.5*a*(n+s)}else{for(var o=1<<r-2,i=a/o,u=e+.5*i,h=0,f=0;f<o;f++)h+=this.eval(u),u+=i;this._s=.5*(this._s+a*h/o)}if(isNaN(this._s))throw new TypeError("this._s is NaN");return this._s}},{key:"simpson",value:function(e,t){if(isNaN(e)||isNaN(t))throw new TypeError("Parameters must be numbers");for(var r=t-e,a=.5*r*(this.eval(e)+this.eval(t)),n=a,s=4*a/3,o=s,i=a,u=1,h=2;h<=20;h++){for(var f=r/u,c=e+.5*f,l=0,v=1;v<=u;v++)l+=this.eval(c),c+=f;if(s=(4*(a=n=.5*(n+r*l/u))-i)/3,Math.abs(s-o)<1e-7*Math.abs(o))break;o=s,i=a,u<<=1}return s}},{key:"romberg",value:function(e,r){if(isNaN(e)||isNaN(r))throw new TypeError("Parameters must be numbers");var a=new Array(21),n=new Array(21),s={y:0,dy:0};n[0]=1;for(var o=1;o<=20&&(a[o-1]=this.trapezoid(e,r,o),!(o>=3&&(s=t.interpolate(n,a,3,o-3,0),Math.abs(s.dy)<=1e-6*s.y)));o++)a[o]=a[o-1],n[o]=.25*n[o-1];return s.y}},{key:"zeroErrorEstimate",value:function(e){if(void 0===e){var t=this.bounds();e=Math.max(Math.abs(t.minX),Math.abs(t.maxX))}if(e<.001)return 2*Math.abs(this.eval(1e-15));var r=this.coefs.length-1,a=this.coefs[r];return 10*1e-15*this.coefs.reduce(function(t,r,n){var s=r/a*Math.pow(e,n);return s>t?s:t},0)}},{key:"boundsUpperRealFujiwara",value:function(){var e=this.coefs,t=e.length-1,r=e[t];1!==r&&(e=this.coefs.map(function(e){return e/r}));var a,n=e.map(function(e,r){return r<t?Math.pow(Math.abs(0===r?e/2:e),1/(t-r)):e}),s=function(e,t,r){return a(r)&&(e.max<t?(e.nearmax=e.max,e.max=t):e.nearmax<t&&(e.nearmax=t)),e};a=function(r){return r<t&&e[r]<0};var o=n.reduce(s,{max:0,nearmax:0});return a=function(r){return r<t&&(t%2==r%2?e[r]<0:e[r]>0)},{negX:-2*n.reduce(s,{max:0,nearmax:0}).max,posX:2*o.max}}},{key:"boundsLowerRealFujiwara",value:function(){var e=new t;e.coefs=this.coefs.slice().reverse();var r=e.boundsUpperRealFujiwara();return r.negX=1/r.negX,r.posX=1/r.posX,r}},{key:"bounds",value:function(){var e=this.boundsUpperRealFujiwara(),t={minX:e.negX,maxX:e.posX};return 0===e.negX&&0===e.posX?t:(0===e.negX?t.minX=this.boundsLowerRealFujiwara().posX:0===e.posX&&(t.maxX=this.boundsLowerRealFujiwara().negX),t.minX>t.maxX&&(t.minX=t.maxX=0),t)}},{key:"boundUpperAbsRouche",value:function(){var e=this.coefs,t=e.length-1;return 1+e.reduce(function(e,r,a){return a!==t&&e<(r=Math.abs(r))?r:e},0)/Math.abs(e[t])}},{key:"boundLowerAbsRouche",value:function(){var e=this.coefs,t=e.reduce(function(e,t,r){return 0!==r&&e<(t=Math.abs(t))?t:e},0);return Math.abs(e[0])/(Math.abs(e[0])+t)}},{key:"boundsRealLaguerre",value:function(){var e=this.coefs,t=e.length-1,r=-e[t-1]/(t*e[t]),a=e[t-1]*e[t-1]-2*t/(t-1)*e[t]*e[t-2],n=(t-1)/(t*e[t])*Math.sqrt(a);return n<0&&(n=-n),{minX:r-n,maxX:r+n}}},{key:"countRootsDescartes",value:function(){var e=this.coefs,t=e.length-1,r=e.reduce(function(e,t,r){return 0!==e.prev_a&&0!==t&&(e.prev_a<0==t>0&&e.pos++,r%2==0!=e.prev_a<0==(r%2==1!=t>0)&&e.neg++),e.prev_a=t,e},{pos:0,neg:0,prev_a:0});return{maxRealPos:r.pos,maxRealNeg:r.neg,minComplex:t-(r.pos+r.neg)}}},{key:"getDegree",value:function(){return this.coefs.length-1}},{key:"getDerivative",value:function(){for(var e=new t,r=1;r<this.coefs.length;r++)e.coefs.push(r*this.coefs[r]);return e}},{key:"getRoots",value:function(){var e;switch(this.simplifyEquals(),this.getDegree()){case 0:e=[];break;case 1:e=this.getLinearRoot();break;case 2:e=this.getQuadraticRoots();break;case 3:e=this.getCubicRoots();break;case 4:e=this.getQuarticRoots();break;default:e=[]}return e}},{key:"getRootsInInterval",value:function(e,t){var r=[];function a(e){"number"==typeof e&&r.push(e)}if(0===this.getDegree())throw new RangeError("Unexpected empty polynomial");if(1===this.getDegree())a(this.bisection(e,t));else{var n=this.getDerivative().getRootsInInterval(e,t);if(n.length>0){a(this.bisection(e,n[0]));for(var s=0;s<=n.length-2;s++)a(this.bisection(n[s],n[s+1]));a(this.bisection(n[n.length-1],t))}else a(this.bisection(e,t))}return r}},{key:"getLinearRoot",value:function(){var e=[],t=this.coefs[1];return 0!==t&&e.push(-this.coefs[0]/t),e}},{key:"getQuadraticRoots",value:function(){var e=[];if(2===this.getDegree()){var t=this.coefs[2],r=this.coefs[1]/t,a=r*r-4*(this.coefs[0]/t);if(a>0){var n=Math.sqrt(a);e.push(.5*(-r+n)),e.push(.5*(-r-n))}else 0===a&&e.push(.5*-r)}return e}},{key:"getCubicRoots",value:function(){var e=[];if(3===this.getDegree()){var t=this.coefs[3],r=this.coefs[2]/t,a=this.coefs[1]/t,n=(3*a-r*r)/3,s=(2*r*r*r-9*a*r+27*(this.coefs[0]/t))/27,o=r/3,i=s*s/4+n*n*n/27,u=s/2,h=this.zeroErrorEstimate();if(Math.abs(i)<=h&&(i=0),i>0){var f,c=Math.sqrt(i),l=-u+c;f=l>=0?Math.pow(l,1/3):-Math.pow(-l,1/3),(l=-u-c)>=0?f+=Math.pow(l,1/3):f-=Math.pow(-l,1/3),e.push(f-o)}else if(i<0){var v=Math.sqrt(-n/3),p=Math.atan2(Math.sqrt(-i),-u)/3,b=Math.cos(p),g=Math.sin(p),y=Math.sqrt(3);e.push(2*v*b-o),e.push(-v*(b+y*g)-o),e.push(-v*(b-y*g)-o)}else{var m;m=u>=0?-Math.pow(u,1/3):Math.pow(-u,1/3),e.push(2*m-o),e.push(-m-o)}}return e}},{key:"getQuarticRoots",value:function(){var e=[],r=this.getDegree();if(4===r){var a=new t;a.coefs=this.coefs.slice(),a.divideEqualsScalar(a.coefs[r]);Math.abs(a.coefs[0])<10*1e-15*Math.abs(a.coefs[3])&&(a.coefs[0]=0);for(var n=a.getDerivative(),s=n.getRoots().sort(function(e,t){return e-t}),o=[],u=s.length-1,h=this.bounds(),f=Math.max(Math.abs(h.minX),Math.abs(h.maxX)),c=this.zeroErrorEstimate(f),l=0;l<=u;l++)o.push(a.eval(s[l]));for(var v=0;v<=u;v++)Math.abs(o[v])<c&&(o[v]=0);var p=0,b=Math.max(.1*(h.maxX-h.minX)/r,1e-15),g=[],y=[];if(u>-1){for(0!==o[0]?i(o[0])!==i(a.eval(s[0]-b)-o[0])&&(g.push(s[0]-b),y.push([h.minX,s[0]])):(e.push(s[0],s[0]),p++);p<u;p++)0===o[p+1]?(e.push(s[p+1],s[p+1]),p++):i(o[p])!==i(o[p+1])&&(g.push((s[p]+s[p+1])/2),y.push([s[p],s[p+1]]));0!==o[u]&&i(o[u])!==i(a.eval(s[u]+b)-o[u])&&(g.push(s[u]+b),y.push([s[u],h.maxX]))}var m=function(e){return a.eval(e)},w=function(e){return n.eval(e)};if(g.length>0)for(p=0;p<g.length;p++)g[p]=t.newtonSecantBisection(g[p],m,w,32,y[p][0],y[p][1]);e=e.concat(g)}return e}}],[{key:"interpolate",value:function(e,t,r,a,n){if(e.constructor!==Array||t.constructor!==Array)throw new TypeError("xs and ys must be arrays");if(isNaN(r)||isNaN(a)||isNaN(n))throw new TypeError("n, offset, and x must be numbers");var s,o,i=0,u=new Array(r),h=new Array(r),f=0,c=Math.abs(n-e[a]);for(s=0;s<r;s++){var l=Math.abs(n-e[a+s]);l<c&&(f=s,c=l),u[s]=h[s]=t[a+s]}o=t[a+f],f--;for(var v=1;v<r;v++){for(s=0;s<r-v;s++){var p=e[a+s]-n,b=e[a+s+v]-n,g=u[s+1]-h[s],y=p-b;if(0===y)throw new RangeError("Unable to interpolate polynomial. Two numbers in n were identical (to within roundoff)");y=g/y,h[s]=b*y,u[s]=p*y}o+=i=2*(f+1)<r-v?u[f+1]:h[f--]}return{y:o,dy:i}}},{key:"newtonSecantBisection",value:function(e,t,r,a,n,s){var o,u,h,f,c,l,v,p=0,b=0;o=e;var g=Math.pow(10,-14),y="number"==typeof n&&"number"==typeof s;if(y){if(n>s)throw new RangeError("Min must be greater than max");if(l=t(n),v=t(s),i(l)===i(v))throw new RangeError("Y values of bounds must be of opposite sign")}for(var m=function(){return Math.abs(h)<=g*Math.abs(o)||b===o-h-o},w=0;w<a;w++){if(0===(u=r(o))){if(0===p)throw new RangeError("df(x) is zero");u=p}if(p=u,c=t(o),f=o-(h=c/u),m())break;if(y){if(i(c)===i(v))s=o,v=c;else{if(i(c)!==i(l)){o=f;break}n=o,l=c}if(f<n||f>s){if(i(l)===i(v))break;var M=v-l,d=s-n;if(h=0===M?o-(n+.5*d):Math.abs(M/Math.min(l,v))>50?o-(n+d*(.5+(Math.abs(l)<Math.abs(v)?-.25:.25))):o-(n-l/M*d),f=o-h,m())break}}b=o-f,o=f}return o}}]),t}(),h=function(t){function i(){return e(this,i),s(this,a(i).apply(this,arguments))}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&n(e,t)}(i,u),r(i,[{key:"eval",value:function(e){var t=o(a(i.prototype),"eval",this).call(this,e);if(Math.abs(t)<1e-7&&(t=0),t<0)throw new RangeError("Cannot take square root of negative number");return Math.sqrt(t)}},{key:"toString",value:function(){return"sqrt("+o(a(i.prototype),"toString",this).call(this)+")"}}]),i}();export{u as Polynomial,h as SqrtPolynomial};
//# sourceMappingURL=index-esm.min.js.map