List of all available Websheets
Viewing cpp/F14/lambert by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | <i>Lambert's W function</i> is a mathematical function with a <a href="https://cs.uwaterloo.ca/research/tr/1993/03/W.pdf">diverse range of applications in math and science</a>. It is an inverse function to $x = w \times \mathrm{e}^w$. In other words, for a given $x$, the value of the Lambert function $W(x)$ is defined as the real number satisfying $$x = W(x) \times \mathrm{e}^{W(x)}.$$ <p> For example, <i>W</i>(126) is approximately 3.5651 since 126 = 3.5651 × e<sup>3.5651</sup>. <p> Use binary search to define a static method <tt>W(double x)</tt> that evaluates the Lambert function at <i>x</i>. You may assume $x \ge 0$. To help get the binary search started, you may use the fact that $0 \le W(x) \le \max(1, \ln(x))$. Terminate the binary search when the search endpoints differ by 10<sup>-8</sup> or less. <p> <i>Hint</i>: you will need <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#exp-double-">several</a> <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#log-double-">methods</a> from the <tt>Math</tt> library. |
Public permissions | |
Engine | |
Template / Reference solution |
using namespace std; // evaluate Lambert's W function at x. Assumes x >= 0. double W(double x) { // we know that W(x) >= 0 and W(x) <= max(1, ln(x)) double lo = 0; \[double hi = max(1.0, log(x));]\ // continue until search endpoints differ by 1E-8 or less while (\[hi - lo >= 1E-8]\) { // evaluate midpoint times e^midpoint \[ double mid = (lo + hi) / 2; double midval = mid * exp(mid); ]\ // if this value is smaller than x, search above, else search below \[ if (midval < x) lo = mid; else hi = mid; ]\ } return lo; // return some point in final range } int main() { double x; cin >> x; cout << W(x); } |
C++ test suite json list of stdin/args tests e.g. [{"stdin":"hi", "args":["4", "5"]}, {"stdin":"noargs"}] to just run once with no input use [{}] | [ {"stdin": "126"}, {"stdin": "2.718281828459045"}, {"stdin": "23423.348"}, {"stdin": "1E25"}, {"stdin": "1E-5"} ] |
Solution visibility |
Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.