forked from duetosymmetry/Polynomial.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial.min.js
20 lines (19 loc) · 7.04 KB
/
polynomial.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
Polynomial.js v1.4.0 13/12/2017
Copyright (c) 2015, Robert Eisele ([email protected])
Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function(u){function x(a){return function(){var b=this.coeff,c=[];for(e in b)c.push(+e);if(0===c.length)return"0";c.sort(function(a,d){return a-d});for(var d,f="",l=c.length;l--;){var e=c[l];var k=b[e];d=k;if(null!==d&&void 0!==d)if(m&&d instanceof m){if(0!==d.re){""!==f&&0<d.re&&(f+="+");if(-1===d.re&&0!==e)f+="-";else if(1!==d.re||0===e)f+=d.re;1===e?f+="x":0!==e&&(f+="x^"+e)}0!==d.im&&(""!==f&&0<d.im&&(f+="+"),-1===d.im?f+="-":1!==d.im&&(f+=d.im),f+="i",1===e?f+="x":0!==e&&(f+="x^"+e))}else if(d=
d.valueOf(),0!==d){""!==f&&0<d&&(f+="+");if(-1===d&&0!==e)f+="-";else if(1!==d||0===e)f+=k[a]?k[a]():k.toString();1===e?f+="x":0!==e&&(f+="x^"+e)}}return""===f?k[a]?k[a]():k.toString():f}}function n(a){var b={};if(null===a||void 0===a)a=0;switch(typeof a){case "object":a.coeff&&(a=a.coeff);if(q&&a instanceof q||m&&a instanceof m||r&&a instanceof r)b[0]=a;else for(var c in a)g.empty(a[c])||(b[c]=g.parse(a[c]));return b;case "number":return{0:g.parse(a)};case "string":for(var d,f;null!==(c=C.exec(a));){f=
d=1;void 0!==c[4]?(d=c[4],f=0):void 0!==c[2]&&(d=c[2]);var e=c[1];d=String(d).match(/[^*/]+|[*/]/g);e=g.parse(e+d[0]);for(var h=1;h<d.length;h+=2)"*"===d[h]?e=g.mul(e,g.parse(d[h+1]||1)):"/"===d[h]&&(e=g.div(e,g.parse(d[h+1]||1)));d=e;void 0!==c[3]&&(f=parseInt(c[3],10));b[f]=void 0===b[f]?d:g.add(b[f],d)}return b}throw"Invalid Param";}function w(a,b){for(var c={},d=p(a),f=p(b),l=[];d>=f;){var h=c[d-f]=g.div(a[d]||0,b[f]||0),k;for(k in b)a[+k+d-f]=g.sub(a[+k+d-f]||0,g.mul(b[k]||0,h));if(null!==e.trace){var y=
{};for(k in b)y[+k+d-f]=g.mul(b[k]||0,h);l.push(new e(y))}d=p(a)}null!==e.trace&&(l.push(new e(a)),e.trace=l);return c}function z(a,b){var c=1;for(b=a-b;b<a;a--)c*=a;return c}function e(a){if(!(this instanceof e))return new e(a);this.coeff=n(a)}function t(a){var b={},c;for(c in a)b[c]=a[c];return b}function A(a,b){var c={},d;for(d in a)c[d]=1;for(d in b)c[d]=1;return c}function p(a){var b=Number.NEGATIVE_INFINITY,c;for(c in a)g.empty(a[c])||(b=Math.max(c,b));return b}function v(a){var b=null,c;for(c in a)!g.empty(a[c])&&
(null===b||+b<+c)&&(b=c);return b}function B(a,b){if(null!==b)for(var c in a)a[c]=g.div(a[c],a[b]);return a}var g={add:function(a,b){return a+b},sub:function(a,b){return a-b},mul:function(a,b){return a*b},div:function(a,b){if(0===b)throw"DIV/0";return a/b},parse:function(a){return parseFloat(a)},empty:function(a){return!a},pow:function(a,b){return Math.pow(a,b)},equals:function(a,b){return a==b}},D=g,C=/([+-]?)(?:([^+x-]+)?(?:x(?:\^([\d\/]+))?)|([^+x-]+))/g;e.trace=null;e.prototype.coeff={};e.prototype.gcd=
function(a){var b=t(this.coeff);for(a=n(a);p(a)!==Number.NEGATIVE_INFINITY;){var c=t(b);w(c,a);b=a;a=c}a=v(b);return new e(B(b,a))};e.prototype.neg=function(){var a={},b=this.coeff,c;for(c in b)a[c]=g.mul(b[c],-1);return new e(a)};e.prototype.reciprocal=function(){var a={},b=this.coeff,c=p(b),d;for(d in b)a[c-d]=b[d];return new e(a)};e.prototype.eval=function(a){var b=this.coeff,c=p(b),d=b[c];for(--c;0<=c;c--)d=g.mul(d,a),g.empty(b[c])||(d=g.add(d,b[c]));return d};e.prototype.lc=function(){var a=
v(this.coeff);return this.coeff[a]};e.prototype.lm=function(){var a=v(this.coeff),b={};b[a]=this.coeff[a];return new e(b)};e.prototype.monic=function(){return new e(B(t(this.coeff),v(this.coeff)))};e.prototype.add=function(a){a=n(a);var b={},c=this.coeff,d=A(a,c),f;for(f in d)b[f]=g.add(c[f]||0,a[f]||0);return new e(b)};e.prototype.sub=function(a){a=n(a);var b={},c=this.coeff,d=A(a,c),f;for(f in d)b[f]=g.sub(c[f]||0,a[f]||0);return new e(b)};e.prototype.mul=function(a){a=n(a);var b={},c=this.coeff;
for(d in a){var d=+d;for(var f in c)f=+f,b[d+f]=g.add(b[d+f]||0,g.mul(a[d]||0,c[f]||0))}return new e(b)};e.prototype.addmul=function(a,b){var c=n(a),d=n(b),f={};for(e in c){var e=+e;for(var h in d)h=+h,f[e+h]=g.add(f[e+h]||0,g.mul(c[e]||0,d[h]||0))}return this.add(f)};e.prototype.div=function(a){return new e(w(t(this.coeff),n(a)))};e.prototype.pow=function(a){if(isNaN(a)||0>a||a%1)throw"Invalid";for(var b=new e(1),c=this;0<a;)a&1&&(b=b.mul(c)),c=c.mul(c),a>>=1;return b};e.prototype.mod=function(a){var b=
t(this.coeff);w(b,n(a));return new e(b)};e.prototype.derive=function(a){if(void 0===a)a=1;else if(0>a)return null;var b=this.coeff,c={},d;for(d in b)+d>=a&&(c[d-a]=g.mul(b[d]||0,z(+d,a)));return new e(c)};e.prototype.integrate=function(a){if(void 0===a)a=1;else if(0>a)return null;var b=this.coeff,c={},d;for(d in b)c[+d+a]=g.div(b[d]||0,z(+d+a,a));return new e(c)};e.prototype.result=e.prototype.eval;e.fromRoots=function(a){var b=a.length,c=g.parse(0);a=a.filter(function(a){return!g.equals(a,c)});b-=
a.length;var d=new e(g.parse(1));var f=function(a){switch(a.length){case 0:return d;case 1:return new e([g.mul(a[0],-1),1]);default:var b=Math.floor(a.length/2),c=a.slice(b,a.length);return f(a.slice(0,b)).a(f(c))}};a=f(a).coeff;var l={},h;for(h in a)l[b+parseInt(h,10)]=a[h];return new e(l)};e.prototype.toString=x("toString");e.prototype.toLatex=x("toLatex");e.prototype.toHorner=function(){function a(a,d){var b="";m&&a instanceof m?0===a.im?(0<a.re&&d&&(b+="+"),b+=a.re):0===a.re?(-1===a.im?b+="-":
1!==a.im?(0<a.im&&d&&(b+="+"),b+=a.im):0<a.im&&d&&(b+="+"),b+="i"):(d&&(b+="+"),b=b+"("+a.toString(),b+=")"):(0<a&&d&&(b+="+"),b+=a.toString());return b}function b(d,f){var e=d.length-f-1,g=d[e]-(d[e-1]||0),h="",l="";0<g&&(h="x",1<g&&(h+="^"+g));0<e&&(h+=a(c[d[e-1]],!0));if(0===f)return a(c[d[e]],!1)+h;0<=e&&d[e]&&(l+="(");l+=b(d,f-1);0<=e&&d[e]&&(l+=")");return l+h}var c=this.coeff,d=[],f;for(f in c)g.empty(c[f])||d.push(+f);if(0===d.length)return"0";d.sort(function(a,d){return a-d});return b(d,
d.length-1)};e.prototype.clone=function(){return new e(this)};e.prototype.degree=function(){return p(this.coeff)};e.setField=function(a){var b={Q:q,C:m,H:r}[a];if(void 0!==b)g={add:function(a,c){return(new b(a)).add(c)},sub:function(a,c){return(new b(a)).sub(c)},mul:function(a,c){return(new b(a)).mul(c)},div:function(a,c){return(new b(a)).div(c)},parse:function(a){return new b(a)},empty:function(a){return(new b(a)).equals(0)},pow:function(a,c){return(new b(a)).pow(c)},equals:function(a,c){return(new b(a)).equals(c)}};
else if(!a||"R"===a)g=D;else if("object"===typeof a)g=a;else if("Z"===a.charAt(0)){var c=+a.slice(1);g={add:function(a,b){return((a+b)%c+c)%c},sub:function(a,b){return((a-b)%c+c)%c},mul:function(a,b){return(a*b%c+c)%c},div:function(a,b){var d=b;for(var e=c,f=0,g=1,m=1,n=0;0!==d;){var p=e/d|0,q=e%d,r=f-m*p;p=g-n*p;e=d;d=q;f=m;g=n;m=r;n=p}d=[e,f,g];if(1!==d[0])throw"DIV/-";return(a*d[1]%c+c)%c},parse:function(a){return parseInt(a,10)},empty:function(a){return void 0===a||0===a},pow:function(a,b){for(var d=
1;0<b;a=(a*a%c+c)%c,b>>=1)b&1&&(d=(d*a%c+c)%c);return d},equals:function(a,b){return a==b}}}};if("function"===typeof define&&define.amd)define(["fraction.js","complex.js","quaternion"],function(a,b,c){q=a;m=b;r=c;return e});else if("object"===typeof exports){var q=require("fraction.js");var m=require("complex.js");var r=require("quaternion");module.exports=e}else q=u.Fraction,m=u.Complex,r=u.Quaternion,u.Polynomial=e})(this);