numerical-methods
Version:
A library that I made while studying Computer Science at UFPB. Documentation's available through TypeScript.
2 lines (1 loc) • 1.42 kB
JavaScript
import{k as $}from"./chunk-PJBNBDYI.js";import{a as p}from"./chunk-EI7MMDWY.js";import{fixNumber as u,range as c}from"@hyoretsu/utils";import{evaluate as d}from"mathjs";var b={x:"number[]",y:"number[]",targetX:"number|undefined"},M=p(({x:e,y:i,targetX:l})=>{let s=i.map((a,r)=>{let n="",o=1;return e.forEach((t,m)=>{m!==r&&(n+=`(x ${Math.sign(e[m])===1?"-":"+"} ${Math.abs(e[m])})`,o*=e[r]-e[m])}),`${a} * (${n})/${u(o)}`}).reduce((a,r)=>a?`${a} + ${r}`:r,"");return{result:s,details:{...l&&{targetResult:u(d(s,{x:l}))}}}},"lagrangeInterpolation"),y=p(({x:e,y:i,targetX:l})=>{let s=e.length,{result:{solution:a}}=$({coefficients:e.map((n,o)=>c(s).map(t=>n**t)),independentTerms:i,precision:1e-9}),r=a.reduce((n,o,t)=>t===0?String(u(o)):`${n} ${Math.sign(o)===1?"+":"-"} ${Math.abs(u(o))} * x^${t}`,"");return{result:r,details:{...l&&{targetResult:u(d(r,{x:l}))}}}},"vandermondeInterpolation"),v=p(({x:e,y:i,targetX:l})=>{let s=[];e.forEach((r,n)=>{if(n===0){s.push(i);return}let o=[];for(let t=0;t<e.length-n;t++)o.push((s[n-1][t+1]-s[n-1][t])/(e[n+t]-e[t]));s.push(o)});let a=s.map((r,n)=>{if(n===0)return String(r[0]);let o="";for(let t=0;t<n;t++)o+=`(x ${Math.sign(e[t])===1?"-":"+"} ${Math.abs(e[t])})`;return`${Math.sign(r[0])===1?"+":"-"} ${Math.abs(r[0])} * ${o}`}).join(" ");return{result:a,details:{dividedDifferences:s,...l&&{result:d(a,{x:l})}}}},"newtonInterpolation");export{b as a,M as b,y as c,v as d};