UNPKG

numerical-methods

Version:

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

2 lines (1 loc) 3.85 kB
import{a as E}from"./chunk-EI7MMDWY.js";import{fixNumber as d,range as b,swap as M}from"@hyoretsu/utils";import{evaluate as I}from"mathjs";var x="number[][]",v={l:x,u:x},w=E(({l:n,u:m})=>{if(n.length!==n[0].length||m.length!==m[0].length)throw new Error("Both matrices must be square");let r=[...n.map(o=>o.map(()=>0))];m[0].forEach((o,i)=>{r[0][i]=o});let l=[];return m.forEach((o,i)=>{o.forEach((s,t)=>{s!==0&&(l[t]===void 0&&(l[t]=[]),l[t].push(s))})}),r.slice(1).forEach((o,i)=>{i+=1;let s=[];n[i].forEach((t,a)=>{a<i&&s.push(t)}),o.forEach((t,a)=>{r[i][a]=l[a].reduce((h,e,u)=>u>i?h:u===i?h+e:h+s[u]*e,0)})}),r},"luComposition"),F={matrix:x},N=E(n=>{if(n.length!==n[0].length)throw new Error("Matrix must be square");let m=[...n.map((l,o)=>l.map((i,s)=>o===s?1:0))],r=[...n.map(l=>l.map(()=>0))];return n.forEach((l,o)=>{l.forEach((i,s)=>{if(s<o){if(s===0){m[o][0]=i/r[0][0];return}m[o][s]=(i-b(s).map(t=>m[o][t]*r[t][s]).reduce((t,a)=>a+t,0))/r[s][s];return}if(o===0){r[0][s]=n[0][s];return}r[o][s]=i-b(o).map(t=>m[o][t]*r[t][s]).reduce((t,a)=>a+t,0)})}),{result:{l:m,u:r}}},"doolittleLuDecomposition"),$={coefficients:x,independentTerms:"number[]"},A=$,G=E(({coefficients:n,independentTerms:m})=>{let r=[...n],l=[...m],o=[],i={},s=[];for(let t=0;t<n.length;t++){let a=t;for(let e=t;e<n.length;e++)e!==t&&r[e][t]>r[a][t]&&(a=e);M(r,t,a),M(l,t,a);for(let e of r){let u={coefficients:[],independentTerms:[]};for(let[c,f]of e.entries())u.independentTerms.push(f.toFixed(2)),u.independentTerms.push(l[c].toFixed(2));s.push(u)}let h=[];for(let e=t+1;e<n.length;e++)h[e]=r[e][t]/r[t][t];for(let e=t+1;e<n.length;e++){for(let u=t;u<n.length;u++)r[e][u]=d(r[e][u])-d(h[e]*r[t][u]);l[e]-=h[e]*l[t]}}for(let t=n.length-1;t>=0;t--){let a="";for(let h=0;h<n.length;h++)if(r[t][h]!==0){if(a===""){a=`${String.fromCharCode(97+t)} = () / ${r[t][h]}`;continue}a=a.replace(/\((.*)\)/,`($1-${r[t][h]}${String.fromCharCode(97+h)} + )`)}a=a.replace(/\((.*)\)/,`($1${l[t]})`),o.push(a),I(a,i)}return{result:i,details:{transformedFuncs:o,steps:s}}},"gaussianElimination"),_={coefficients:x},L=E(n=>Math.max(...n.map((m,r)=>m.reduce((l,o)=>l+Math.abs(o),0)/Math.abs(m[r]))),"spectralRadius"),O={...$,precision:"number",options:{maxIterations:"number"}},R=E(({coefficients:n,independentTerms:m,precision:r,options:{maxIterations:l=Number.POSITIVE_INFINITY}={}})=>{r===0&&(r=1e-5);let o=[...n],i=[...m],s=[],t=0,a=m.length,h=i.map((u,c)=>[...b(a-1).map(f=>{let p=f>=c?f+1:f;return`${-o[c][p]/o[c][c]} * ${String.fromCharCode(97+p)} + `}),String(u/o[c][c])].join("")),e=i.map((u,c)=>u/o[c][c]);for(;;){let u=e;e=h.map((p,C)=>I(p,e.reduce((g,S)=>(g[String.fromCharCode(97+Object.entries(g).length)]=S,g),{}))),t+=1;let c=Math.max(...e.map((p,C)=>Math.abs(p-u[C]))),f=c/Math.max(...e);if(s.push({iteration:t,currentGuess:u.map(p=>d(p)),nextGuess:e.map(p=>d(p)),absoluteError:d(c),relativeError:d(f)}),f<r||t>=l)break}return{result:{iterations:t,iterationFunc:h,spectralRadius:L(n),solution:e},details:s}},"gaussJacobi"),k=E(({coefficients:n,independentTerms:m,precision:r,options:{maxIterations:l=Number.POSITIVE_INFINITY}={}})=>{r===0&&(r=1e-5);let o=[...n],i=[...m],s=[],t=0,a=m.length,h=i.map((u,c)=>[...b(a-1).map(f=>{let p=f>=c?f+1:f;return`${-o[c][p]/o[c][c]} * ${String.fromCharCode(97+p)} + `}),String(u/o[c][c])].join("")),e=i.map(u=>0);for(;;){let u=[...e];h.forEach((p,C)=>{e[C]=I(p,e.reduce((g,S)=>(g[String.fromCharCode(97+Object.entries(g).length)]=S,g),{}))}),t+=1;let c=Math.max(...e.map((p,C)=>Math.abs(p-u[C]))),f=c/Math.max(...e);if(s.push({iteration:t,currentGuess:u.map(p=>d(p)),nextGuess:e.map(p=>d(p)),absoluteError:d(c),relativeError:d(f)}),t>0&&f<r||t>=l)break}return{result:{iterations:t,iterationFunc:h,spectralRadius:L(n),solution:e},details:s}},"gaussSeidel");export{v as a,w as b,F as c,N as d,A as e,G as f,_ as g,L as h,O as i,R as j,k};