Created
February 25, 2014 07:52
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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