UNPKG

cnum

Version:

Represent rational numbers in comfortably numeric way.

3 lines (2 loc) 9.94 kB
var t="0.1.3";class n{constructor(t,n){this.type=void 0,this.s=void 0,this.type=t,this.s=n}toString(){return`${this.type}(${this.s})`}}const i={"+":"add","-":"sub","*":"mul","/":"div","**":"pow","^":"pow","=":"set","==":"equals","<":"isLessThan",">":"isGreaterThan"};class s{constructor(t){this.s=void 0,this.s=t}*lex(){let t=!1,s=!1,e=!1,r=!1,o="";for(let l=0;l<this.s.length;l++){var h;const u=null!=(h=this.s[l])?h:"?";if(/^\s$/.exec(u))s?(yield new n(4,o),s=!1,o=""):e&&(yield new n(0,o),e=!1,o=""),t=!0,o+=u;else if(t&&(yield new n(2,""),t=!1,o=""),/^\d$/.exec(u))e||(s=!0),o+=u;else if(s&&(yield new n(4,o),s=!1,o=""),/^[a-z]$/.exec(u)||e&&/^\d$/.exec(u))e=!0,o+=u;else if(e&&(yield new n(0,o),e=!1,o=""),o+=u,o in i)yield new n(3,o),r=!1,o="";else{if(r)throw`Invalid operator "${o}"`;r=!0}}t&&(yield new n(2,"")),s&&(yield new n(4,o)),e&&(yield new n(0,o))}}class e{constructor(t){this.tokens=void 0,this.identifiers={},this.tokens=t}evaluate(){let t=0n;for(const i of this.tokens)if(0===i.type){var n;if(!(i.s in this.identifiers))throw`"${i.s}" is undefined`;t+=BigInt(null!=(n=this.identifiers[i.s])?n:-1)}else 4===i.type&&(t+=BigInt(i.s));return t}toString(){return this.evaluate().toString()}}const r=t=>{if(1n===t)return!1;for(let n=2n;n*n<=t;n++)if(t%n===0n)return!1;return!0},o=t=>{const n=[];for(const i of function*(){for(let t=2n;;t++)r(t)&&(yield t)}()){let s=0n;for(;t%i===0n;)s++,t/=i;if(s&&n.push([i,s]),1n===t)break}return n};class h{constructor(t=0n,n=1n){this.n=void 0,this.d=void 0,this.n=BigInt(t),this.d=BigInt(n),this.normalize()}valueOf(){return Number(this.n)/Number(this.d)}toString(){return this.n.toString()+(1n===this.d?"":`/${this.d.toString()}`)}get profile(){const t=[`Rat: ${this.toString()} (≈${+this})`];return t.push(`Mixed: ${this.mixedFractionString()}`),t.push(`Continued: ${this.continuedFractionString()}`),t.push(`Factorization: ${this.primeFactorizationString()}`),t.push(`Egyptian: ${this.egyptianFractionString()}`),t.push(`Babylonian: ${this.babylonianFractionString()}`),t.push(`psin(t): ${this.psin().toString()}`),t.push(`pcos(t): ${this.pcos().toString()}`),t.push(`ptan(t): ${this.ptan().toString()}`),t.join("\n")}clone(){return new h(this.n,this.d)}normalize(){if(0n===this.n)return void(0n!==this.d&&(this.d=1n));if(0n===this.d)return void(this.n=this.n>0n?1n:-1n);if(this.n===this.d)return void(this.n=this.d=1n);this.d<0n&&(this.n=-this.n,this.d=-this.d);const t=((t,n)=>{if(1n===n||1n===t)return 1n;for(;0n!==n;){const i=t%n;t=n,n=i}return t<0n?-t:t})(this.n,this.d);this.n/=t,this.d/=t}add(t){const n=new h(this.n*t.d+t.n*this.d,this.d*t.d);return n.normalize(),n}sub(t){return this.add(t.neg())}mul(t){const n=new h(this.n*t.n,this.d*t.d);return n.normalize(),n}div(t){const n=new h(this.n*t.d,this.d*t.n);return n.normalize(),n}mediant(t){const n=new h(this.n+t.n,this.d+t.d);return n.normalize(),n}min(t){return this.isLessThan(t)?this:t}max(t){return this.isGreaterThan(t)?this:t}pow(t){if(0n===t.n)return new h(1n);if(1n===t.d)return new h(Number(this.n)**Number(t.n),Number(this.d)**Number(t.n));const n=Math.pow(+this,+t);return l(n)}dot(t){return this.n*t.n+this.d*t.d}equals(t){return this.n===t.n&&this.d===t.d}approximates(t){return Math.abs(+this-t)<1e-16}isGreaterThan(t){return this.n*t.d>t.n*this.d}isLessThan(t){return this.n*t.d<t.n*this.d}abs(){const t=this.clone();return t.n<0&&(t.n=-t.n),t}neg(){const t=this.clone();return t.n=-t.n,t}isNegative(){return this.n<0}isFinite(){return 0n!==this.d}inv(){return new h(this.d,this.n)}sqrt(){return this.root(2)}root(t){if(0n===this.n||0n===this.d||this.n===this.d)return this.clone();if(this.isNegative())throw`Roots of negative numbers like ${this.toString()} are too complex for this basic library`;return l(Math.pow(+this,1/t))}round(){return BigInt(Math.round(+this))}floor(){return BigInt(Math.floor(+this))}ceil(){return BigInt(Math.ceil(+this))}psin(){if(0n===this.d)return new h(0n);const t=new h(1),n=new h(2),i=n.mul(this),s=t.add(this.pow(n));return i.div(s)}pcos(){if(0n===this.d)return new h(-1n);const t=new h(1),n=new h(2),i=this.pow(n),s=t.sub(i),e=t.add(i);return s.div(e)}ptan(){return this.psin().div(this.pcos())}mixedFractionString(){const t=this.isNegative()?this.ceil():this.floor(),n=this.sub(new h(t)).toString();return t?`${t} + ${n}`:n}*continuedFraction(){if(0n===this.n||0n===this.d)yield+this;else for(const t of function*(t){let n=!0,i=0;for(const s of function*(t){let n=1n,i=0n,s=0n,e=1n;const r=new h(1n);for(let o=0;o<512&&!r.approximates(t);o++){const o=t>+r;yield o,o?(i+=n,e+=s):(n+=i,s+=e),r.n=n+i,r.d=s+e}}(t))s===n?i++:(yield i,i=1,n=s);yield i+1}(+this))yield t}continuedFractionString(){const t=[];for(const n of this.continuedFraction())t.push(n.toString());const n=t.shift();if(void 0!==n&&0n!==this.d){let i=n.toString();return t.length&&(i+=`; ${t.join(", ")}`),`[${i}]`}return"[]"}primeFactorization(){const t=[];return 1n!==this.n&&t.push(...o(this.n)),1n!==this.d&&t.push(...o(this.d).map(t=>(t[1]=-t[1],t))),t.sort((t,n)=>Number(t[0]-n[0]))}primeFactorizationString(){const t=[];for(const n of this.primeFactorization())t.push(1n===n[1]?n[0].toString():`${n[0]}^${n[1]}`);return t.join(" * ")}egyptianFraction(){const t=[],n=new h(1n);let i=this.clone();const s=this.floor();if(s){const n=new h(s);t.push(n),i=i.sub(n)}if(0n===i.n)return t;for(;1n!==i.n;)n.d++,i.isGreaterThan(n)&&(t.push(n.clone()),i=i.sub(n));return t.push(i),t}egyptianFractionString(){return this.egyptianFraction().join(" + ")}babylonianFraction(){const t=[];let n=Number(this.floor()),i=Math.abs(+this-n),s=0;for(let i=0;n>0;i++)s=n%60,0!==s&&t.unshift(`${s} * 60^${i}`),n=(n-s)/60;for(let e=-1;i>1e-10;e--)i*=60,s=Math.floor(i),i-=s,0!==s&&t.push(`${s} * 60^${e}`),n=(n-s)/60;return t}babylonianFractionString(){const t=[],n=this.babylonianFraction();for(const i of n)t.push(`${i}`);return t.join(" + ")}}const l=t=>{if(isNaN(t))return new h(0,0);if(Infinity===t)return new h(1,0);if(-Infinity===t)return new h(-1,0);if(Math.abs(t%1)<1e-16)return new h(Math.round(t));if(Math.abs(1/t%1)<1e-16)return new h(1,Math.round(1/t));const n=t<1,i=function(t){let n=1n,i=0n,s=0n,e=1n;const r=new h(1n);for(let o=0;o<512&&!r.approximates(t);o++)+r>t?(n+=i,s+=e):(i+=n,e+=s),r.n=n+i,r.d=s+e;return r}(Math.abs(t));return n?i.neg():i},u=t=>{if("NaN"===t)return new h(0,0);if("Infinity"===t)return new h(1,0);if("-Infinity"===t)return new h(-1,0);const[n,i]=t.split("/",2);return void 0===i?l(Number(n)):new h(BigInt(null!=n?n:1),BigInt(i))};class c{constructor(t){this.symbols=[],t&&(this.symbols=t.split(""))}*generator(){for(let n=0;;n++){var t;n<this.symbols.length?yield null!=(t=this.symbols[n])?t:"?":yield String.fromCharCode(945+n-this.symbols.length)}}}class a{constructor(t){if(this.coefficents={},this.dimension=0,this.latinSymbols="",this.greekSymbols="",t&&(this.coefficents=t),Object.keys(this.coefficents).length){const t=Object.keys(this.coefficents);this.dimension=t[0]?t[0].split(",").length:0}const n=new c("xyzw").generator();for(let t=0;t<this.dimension;t++)this.latinSymbols+=n.next().value;const i=(new c).generator();for(let t=0;t<this.dimension;t++)this.greekSymbols+=i.next().value}evaluate(t){let n=new h;for(const[e,r]of Object.entries(this.coefficents)){let o=new h(r);const l=e.split(",");for(let n=0;n<l.length;n++)if(n in t){var i,s;const e=null!=(i=t[n])?i:new h(1),r=parseInt(null!=(s=l[n])?s:"1",10);o=o.mul(e.pow(new h(r)))}n=n.add(o)}return n}toString(){return`${this.constructor.name}(${this.toJSON()})`}toJSON(){const t=[];for(const[n,i]of Object.entries(this.coefficents))t.push(`"${n}":"${i.toString()}"`);return`{${t.join(",")}}`}toHTMLFormula(){const t=[];for(const[e,r]of Object.entries(this.coefficents)){const o=[],h=r.toString();"1"!==h&&o.push(h);const l=e.split(",");for(let t=0;t<l.length;t++){var n,i,s;"0"!==l[t]&&o.push("1"===l[t]?null!=(n=this.latinSymbols[t])?n:"?":`${null!=(i=this.latinSymbols[t])?i:"?"}<sup>${parseInt(null!=(s=l[t])?s:"?",10)}</sup>`)}o&&t.push(o.join(""))}return 0===t.length?"0":t.join(" + ")}toStandardAlphaFormHTML(){const t=[],n=[];if(!Object.keys(this.coefficents).length)return"0";for(const[h,l]of Object.entries(this.coefficents)){const u=[],c=[],a=l.toString();"1"!==a&&u.push(a);const f=h.split(",");for(let t=0;t<f.length;t++)if("0"!==f[t])if("1"===f[t]){var i;u.push(null!=(i=this.greekSymbols[t])?i:"?")}else{var s;const n=parseInt(null!=(s=f[t])?s:"0",10);var e;if(n>0)u.push(`${null!=(e=this.greekSymbols[t])?e:"?"}<sup>${n}</sup>`);else if(-1===n){var r;c.push(null!=(r=this.greekSymbols[t])?r:"?")}else{var o;c.push(`${null!=(o=this.greekSymbols[t])?o:"?"}<sup>${-n}</sup>`)}}u.length&&t.push(u.join("")),c.length&&n.push(c.join(""))}return 0===t.length&&t.push("1"),0===n.length?t.join(" + "):`${t.join(" + ")} / ${n.join(" + ")}`}toCalcFormula(){const t=[];for(const[e,r]of Object.entries(this.coefficents)){const o=[],h=r.toString();"1"!==h&&o.push(h);const l=e.split(",");for(let t=0;t<l.length;t++){var n,i,s;"0"!==l[t]&&o.push("1"===l[t]?null!=(n=this.latinSymbols[t])?n:"?":`${null!=(i=this.latinSymbols[t])?i:"?"}^${parseInt(null!=(s=l[t])?s:"0",10)}`)}o.length&&t.push(o.join("*"))}return 0===t.length?"0":t.join(" + ")}toGLSLFormula(){const t=[];for(const[s,e]of Object.entries(this.coefficents)){const r=[],o=e.toString();"1"!==o&&r.push(`${o}.0`);const h=s.split(",");for(let t=0;t<h.length;t++)if("0"!==h[t]){var n,i;let s=parseInt(null!=(n=h[t])?n:"0",10);const e=s<0;e&&(r.push("1.0/(1.0"),s=-s),r.push((null!=(i=this.latinSymbols[t])?i:"?").repeat(s).split("").join("*")),e&&r.push("1.0)")}r.length&&t.push(r.join("*"))}return 0===t.length?"0.0":t.join("+")}clone(){return new a(this.coefficents)}}const f=t=>new a(JSON.parse(t));class d{static get version(){return"0.1.3"}static evaluate(t){const n=new s(t);return new e(n.lex()).toString()}}export{a as Polyrat,h as Rat,d as cnum,l as floatToRat,u as parseRat,f as stringToPolyrat,t as version}; //# sourceMappingURL=index.modern.mjs.map