UNPKG

numerical-methods

Version:

A library that I made while studying Computer Science at UFPB. Documentation's available through TypeScript.

2 lines (1 loc) 1.47 kB
import{a as f}from"./chunk-EI7MMDWY.js";import{fixNumber as a}from"@hyoretsu/utils";import{evaluate as r}from"mathjs";var S={conditionsWhitelist:"[boolean,boolean]",maxIterations:"number",origFunc:"string",relativeError:"number"},v={func:"string",interval:"[number,number]",target:'"min"|"max"',precision:"number",options:S},y=f(({func:m,interval:[t,i],target:w,precision:h,options:{conditionsWhitelist:N=[!0,!0],maxIterations:b=Number.POSITIVE_INFINITY,origFunc:I,relativeError:s}={}})=>{h===0&&(h=1e-5);let P=[],l=-1,c={a:[0,0],b:[0,0]},u=typeof s=="number"&&s;for(;;){let e=[r(m,{x:t}),r(m,{x:i})],n=(t+i)/2,g=r(m,{x:n});l+=1;let p=Math.abs(i-t),x=f(M=>{s&&(u||(M==="a"?u=t:u=i),s=Math.abs(n-u)/Math.abs(n)),M==="a"?t=n:i=n},"swapPoints"),o=w==="max"?1:-1;if(Math.sign(g-e[0])===o?(c.a=[t,r(m,{x:(n+i)/2})],Math.sign(c.a[1]-g)===o&&x("a")):Math.sign(g-e[1])===o?(c.b=[i,r(m,{x:(n+t)/2})],Math.sign(c.b[1]-g)===o&&x("b")):Math.sign(e[0]-e[1])===o?x("b"):Math.sign(e[1]-e[0])===o&&x("a"),P.push({iteration:l,interval:[a(t),a(i)],results:e.map(M=>a(M)),x:a(n),...I&&{y:r(I,{x:n})},...s&&{relativeError:a(s)},condition1:a(p)}),!N[0]||p<h||l>=b)break}let d=Math.ceil((Math.log10(i-t)-Math.log10(h))/Math.log10(2));if(l<d&&b===Number.POSITIVE_INFINITY)throw new Error(`Something went wrong, less iterations than the minimum (${d}) were done.`);return{result:{iterations:l,interval:[t.toPrecision(21),i.toPrecision(21)]},details:P}},"minMaxBisection");export{v as a,y as b};