-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add lambertw_pvlib #2723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add lambertw_pvlib #2723
Changes from 6 commits
18ab529
33be0e3
e2fa011
e0bc631
935ff35
c22c763
f0897ba
610bbb2
b8e1e3d
9a0cf85
eba1777
189d8fb
eb94583
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -544,3 +544,60 @@ def astm_e1036(v, i, imax_limits=(0.75, 1.15), vmax_limits=(0.75, 1.15), | |
| result['mp_fit'] = mp_fit | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| def _log_lambertw(logx): | ||
| r'''Computes W(x) starting from log(x). | ||
|
|
||
| Parameters | ||
| ---------- | ||
| logx : numeric | ||
| Log(x) of | ||
|
|
||
| Returns | ||
| ------- | ||
| numeric | ||
| Lambert's W(x) | ||
|
|
||
| ''' | ||
| # handles overflow cases, but results in nan for x <= 1 | ||
| w = logx - np.log(logx) # initial guess, w = log(x) - log(log(x)) | ||
|
|
||
| for _ in range(0, 3): | ||
| # Newton's. Halley's is not substantially faster or more accurate | ||
| # because f''(w) = -1 / (w**2) is small for large w | ||
| w = w * (1. - np.log(w) + logx) / (1. + w) | ||
| return w | ||
|
|
||
|
|
||
| def _lambertw_pvlib(x): | ||
| r'''Lambert's W function principal branch, :math:`W_0(x)`, for :math:`x` | ||
| real valued. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x : np.array | ||
| Must be real numbers. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will requiring
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should accept float, added that |
||
|
|
||
| Returns | ||
| ------- | ||
| np.array | ||
|
|
||
| ''' | ||
| w = np.full_like(x, np.nan) | ||
| small = x <= 10 | ||
| # for large x, solve 0 = f(w) = w + log(w) - log(x) using Newton's | ||
| w[~small] = _log_lambertw(np.log(x[~small])) | ||
|
|
||
| # w will contain nan for these numbers due to log(w) = log(log(x)) | ||
| # for small x, solve 0 = g(w) = w * exp(w) - x using Halley's method | ||
| if any(small): | ||
| z = x[small] | ||
| g = np.log(x[small] + 1) - np.log(np.log(x[small] + 1) + 1) | ||
| for _ in range(0, 3): | ||
| expg = np.exp(g) | ||
| g = g - (g*expg - z) * (g + 1) / \ | ||
| (expg * (g + 1)**2 - 0.5*(g + 2)*(expg*g - z)) | ||
|
cwhanse marked this conversation as resolved.
Outdated
|
||
| w[small] = g | ||
|
|
||
| return w | ||
Uh oh!
There was an error while loading. Please reload this page.