Skip to content

Instantly share code, notes, and snippets.

@Prince781
Created February 25, 2014 07:52
Show Gist options
  • Select an option

  • Save Prince781/9204688 to your computer and use it in GitHub Desktop.

Select an option

Save Prince781/9204688 to your computer and use it in GitHub Desktop.
Implements the Lambert W-function (inverse of f(W) = we^w) of z, useful in solving for we^w = z.
// w_lambert.c Lambert W-function
// c99
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double W(double z) {
double EPSILON = 0.000000000000001;
double w = 0, ew, wN = z;
// approximate w_j+1
while (fabs(w - wN) > EPSILON) {
w = wN;
ew = exp(w);
wN = w - (w*ew - z) / (ew + w*ew);
}
return wN;
}
int main(int argc, char **argv) {
double n = 0;
if (argc > 0)
sscanf(argv[1], "%lf", &n);
printf("W(%lf) = %lf\n", n, W(n));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment