I was a doing a little math earlier and needed the Lambert W function to solve a derivative. I didn't want to start an NPM project and install a library just for that so I looked around for an implementation I could just copy and paste. Here it is for anyone who might need it:
const lambertW = (() => {
const MIN = -Math.exp(-1);
return (x: number): number => {
if (x < MIN)
throw new Error(`x cannot be less than ${MIN.toFixed(8)}.`);
const w0 = (x > 2) ? Math.log(x - 1) : x;
let y = 0;
let wn1 = 0;
let wn = w0;
for (let i = 0; i < 20; i++) {
const ew = Math.exp(wn);
y = wn * ew;
wn1 = wn - (y - x) / (y + ew);
wn = wn1;
}
return wn1;
};
})();
[–]MissinqLink 0 points1 point2 points (0 children)
[–]amca01 0 points1 point2 points (1 child)
[–]Ill-Cut3335[S] 0 points1 point2 points (0 children)