Opened 8 years ago

Closed 7 years ago

#11888 closed defect (fixed)

Sage is missing the lambert_w function

Reported by: benjaminfjones Owned by: burcin
Priority: minor Milestone: sage-5.1
Component: symbolics Keywords: lambert_w symbolics conversion maxima sd35.5 sd40.5
Cc: kcrisman, ktkohl Merged in: sage-5.1.beta4
Authors: Benjamin Jones Reviewers: Keshav Kini, Karl-Dieter Crisman, Fredrik Johansson, Burcin Erocal, Douglas McNeil, William Stein
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by benjaminfjones)

Maxima returns solutions to some exponential equations in terms of the lambert_w function. Sage is missing a conversion for this function:

sage: solve(e^(5*x)+x==0, x, to_poly_solve=True)
[x == -1/5*lambert_w(5)]
sage: S = solve(e^(5*x)+x==0, x, to_poly_solve=True)
sage: z = S[0].rhs()
sage: z
-1/5*lambert_w(5)
sage: N(z)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/jonesbe/sage/sage-4.7.2.alpha2/devel/sage-test/sage/<ipython console> in <module>()

/Users/jonesbe/sage/latest/local/lib/python2.6/site-packages/sage/misc/functional.pyc in numerical_approx(x, prec, digits)
   1264             prec = int((digits+1) * 3.32192) + 1
   1265     try:
-> 1266         return x._numerical_approx(prec)
   1267     except AttributeError:
   1268         from sage.rings.complex_double import is_ComplexDoubleElement

/Users/jonesbe/sage/latest/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression._numerical_approx (sage/symbolic/expression.cpp:17950)()

TypeError: cannot evaluate symbolic expression numerically
sage: lambert_w(5)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

/Users/jonesbe/sage/sage-4.7.2.alpha2/devel/sage-test/sage/<ipython console> in <module>()

NameError: name 'lambert_w' is not defined
sage: 

mpmath can evaluate the lambert_w function, so it should be easy to add a new symbolic function to Sage that will fix this issue.


Apply:

Attachments (13)

trac_11888.patch (3.8 KB) - added by benjaminfjones 8 years ago.
add lambert_w symbolic function
trac_11888-doctests.patch (7.0 KB) - added by kini 8 years ago.
apply to $SAGE_ROOT/devel/sage
trac_11888_v2.patch (8.8 KB) - added by benjaminfjones 8 years ago.
proof of concept patch (not ready for review)
trac_11888_v3.patch (9.3 KB) - added by benjaminfjones 8 years ago.
adds lambert_w and lambert_w_branch functions
trac_11888_v4.patch (5.6 KB) - added by benjaminfjones 8 years ago.
addressed reviewer issues, changed order of arguments to be consistant with Mma/Maple?
trac_11888-random-tests.patch (2.4 KB) - added by benjaminfjones 8 years ago.
fixes random tests after rebasing against #9130
trac_11888_v5.patch (7.9 KB) - added by benjaminfjones 8 years ago.
adds lambert_w function
trac_11888_v6.patch (8.2 KB) - added by benjaminfjones 8 years ago.
added custom latex printing and Maxima initialization
trac_11888_v7.patch (10.3 KB) - added by benjaminfjones 8 years ago.
adds lambert_w function, including integration
trac_11888_v7.2.patch (10.3 KB) - added by benjaminfjones 7 years ago.
removed trailing whitespace
trac_11888_v8.patch (10.7 KB) - added by benjaminfjones 7 years ago.
fixed spelling / grammer mistakes, returned parent(Integer(...)) for special values
trac_11888_v9.patch (11.8 KB) - added by dsm 7 years ago.
post-review version of lambertw sf
trac_11888_v10.patch (11.8 KB) - added by dsm 7 years ago.
minor edits

Download all attachments as: .zip

Change History (78)

comment:1 Changed 8 years ago by kcrisman

  • Cc kcrisman added

Changed 8 years ago by benjaminfjones

add lambert_w symbolic function

comment:2 Changed 8 years ago by benjaminfjones

  • Authors set to Benjamin Jones
  • Keywords sd35.5 added
  • Status changed from new to needs_review

Preliminary patch needs review. The function has been added using the template developed as part of #11143. The issue described in the description is addressed in one of the doctests.

Changed 8 years ago by kini

apply to $SAGE_ROOT/devel/sage

comment:3 Changed 8 years ago by kini

  • Description modified (diff)

Running make ptestlong now. I fixed a couple of doctests that broke, and fixed some typos and rST syntax problems in your docstring.

comment:4 Changed 8 years ago by kini

All tests pass.

comment:5 Changed 8 years ago by ktkohl

  • Cc ktkohl added

comment:6 Changed 8 years ago by benjaminfjones

Thanks for the fixes, kini. I've run make ptestlong with the patches applied and verified that all tests pass. Maybe I can get @kcrisman to finish a review this afternoon.

comment:7 Changed 8 years ago by kcrisman

I don't see any obvious problems, but the random expression usually doesn't change much with these new functions and this one is really different.

It's also spread across many lines, and I'm not sure if this is appropriate (just in this one case, of course).

comment:8 Changed 8 years ago by kini

I spread it across lines because 1) I was trying to keep within the recommended PEP 8 guidelines for line length, and 2) because of this

[2012-01-10 22:54:53] <kini> while I was fixing the second doctest, some weird
stuff started happening to vim
[2012-01-10 22:55:02] <kini> I thought my terminal had frozen or something
[2012-01-10 22:56:02] <kini> but it turns out that apparently opening a new line
after a line with a 1800-character-long Sage symbolic expression on it causes
vim to take a full 12 seconds to compute the correct indentation level for the
next line
[2012-01-10 22:56:20] <benjaminfjones> ha!
[2012-01-10 22:56:30] <kini> on a 4.5 GHz Core i5-2500K and utilizing three
cores!
[2012-01-10 22:56:39] <benjaminfjones> wow

What is inappropriate about adding line breaks?

As for the length of the expression, it seems to be a fluke. With the patches applied, starting with random seeds other than 2 gives expressions of a more "normal" length.

comment:9 Changed 8 years ago by benjaminfjones

I agree, it looks like a fluke that the expression grows so large. I did some testing of random_expr and found that it "normally" produces output around 200 - 400 character long, but occasionally the outputs can be 10 times that (I saw a few around 2500 characters long!)

comment:10 follow-up: Changed 8 years ago by fredrik.johansson

I strongly recommend implementing the general version of the Lambert W function (taking a branch parameter).

comment:11 in reply to: ↑ 10 Changed 8 years ago by kcrisman

I strongly recommend implementing the general version of the Lambert W function (taking a branch parameter).

Can you be more specific? (Is this standard with other multivalued functions in Sage?) Maybe this could be a separate ticket, unless the change was really easy.

comment:12 Changed 8 years ago by benjaminfjones

The change should be simple. mpmath implements the a branch W_k(z) for each integer k. It's just a matter of adding a second parameter to the wrapper and putting in some tests. I'm sitting on the train from Beverly MA to Logan airport now, I'll see if I can get it uploaded before the train stops (or my battery dies).

comment:13 Changed 8 years ago by kcrisman

Sweet, I didn't realize it was that quick. I love doing Sage development on that train :) There is also free wifi at Logan, I believe.

comment:14 Changed 8 years ago by kcrisman

Ping. I'd love to review this but sounds like Fredrik's point is good and if it's pretty easy for you to add that, we might as well.

comment:15 Changed 8 years ago by fredrik.johansson

Yes, it should be easy; just add an optional branch parameter, lambertw(z, branch=0).

Another suggestion is to use scipy.special.lambertw for evaluation over RDF and CDF. The SciPy implementation is a Cython translation of the double precision version in mpmath; it supports all branches and has excellent numerical stability, and runs quite a bit faster.

import scipy.special
import mpmath
timeit("mpmath.lambertw(-35.0r+4.6jr,2r)")
timeit("mpmath.fp.lambertw(-35.0r+4.6jr,2r)")
timeit("scipy.special.lambertw(-35.0r+4.6jr,2r)")
print repr(complex(mpmath.lambertw(-35.0r+4.6jr,2r)))
print repr(mpmath.fp.lambertw(-35.0r+4.6jr,2r))
print repr(scipy.special.lambertw(-35.0r+4.6jr,2r))

625 loops, best of 3: 301 µs per loop
625 loops, best of 3: 65.1 µs per loop
625 loops, best of 3: 6.75 µs per loop
(0.91763023745202721+14.071606637742889j)
(0.91763023745202721+14.071606637742889j)
(0.91763023745202721+14.071606637742889j)

comment:16 Changed 8 years ago by kcrisman

  • Reviewers set to Keshav Kini, Karl-Dieter Crisman, Fredrik Johansson
  • Status changed from needs_review to needs_work
  • Work issues set to add second parameter, RDF/CDF stuff

Nice; I wonder if there are more places we are beginning to default to mpmath where SciPy could be useful for the double fields.

comment:17 Changed 8 years ago by benjaminfjones

Thanks for the ping. I'm still here (and I have a patch pretty much ready to go) I just got buried under teaching. I'll try to upload a patch this evening.

comment:18 Changed 8 years ago by benjaminfjones

After looking at this a bit more, it may be more involved than I initially envisioned to implement arbitrary branches of the lambert_w function in one symbolic function. Right now, the patch from SD 35.5 implements a subclass of `BuiltinFunction`. The underlying assumptions about subclasses of BuiltinFunction include: (from sage/symbolic/function.pyx)

We assume that each subclass of this class will define one symbolic function.

One issue is that there isn't a way (as far as I can see) to pass a branch parameter to `BuiltinFunction`'s _call_ method. (Perhaps burcin or other authority on Sage symbolics can comment on this.)

Changing the evaluation numerical eval to use SciPy would be an easy change, that's for sure. I can do that quickly and upload a patch that implements the principle branch only.

Another idea I just had was to do something like what we have for the Bessel functions, in particular the `Bessel` class in sage/functions/special.py which is just a basic python class returning one of the Bessel (I,J,Y) functions of a given order.

comment:19 Changed 8 years ago by kcrisman

Ok, that makes sense. I feel like there should be a way to do that nonetheless - see incomplete_gamma, with

        BuiltinFunction.__init__(self, "gamma", nargs=2, latex_name=r"\Gamma",
                conversions={'maxima':'gamma_incomplete', 'mathematica':'Gamma',
                    'maple':'GAMMA'})

and then use _eval_ and _evalf_, but I don't have time to try looking into whether that would work here now.

Based on Fredrik's comment, make sure to only use SciPy for RDF/CDF - hopefully there is a good model elsewhere to use for that.

Changed 8 years ago by benjaminfjones

proof of concept patch (not ready for review)

comment:20 Changed 8 years ago by benjaminfjones

OK, I made a second attempt. The patch isn't complete (I need to fix and add docstrings and do more testing) and is *not* ready for review, but if the reviewers will take a look at the basic implementation and give me feedback, I'd appreciate it.

In trac_11888_v2.patch there is a new symbolic function lambert_w_branch which takes two arguments, a complex number z and an integer branch n. This is implemented using scipy.special.lambertw for RDF/CDF arguments z and using mpmath otherwise.

There is also a wrapper function lambert_w that accepts either one or two arguments. For one argument it returns the principle branch lambert_w_branch(z,0), for two it returns lambert_w_branch(z,n). I still need to add the conversion from Maxima (by hand now, since lambert_w doesn't inherit from BuiltinFunction any more).

comment:21 Changed 8 years ago by benjaminfjones

  • Status changed from needs_work to needs_review
  • Work issues add second parameter, RDF/CDF stuff deleted

I fixed the doctests and added lambert_w to the symbol table. I verified that all tests pass including the random_tests.py ones. The patch trac_11888_v3.patch is ready for review.

comment:22 Changed 8 years ago by benjaminfjones

  • Description modified (diff)

comment:23 Changed 8 years ago by fredrik.johansson

principle -> principal, branchs -> branches

Otherwise, from looking at the patch, seems good.

comment:24 Changed 8 years ago by benjaminfjones

Thanks for looking at the patch, Fredrik. I've fixed the mistakes and replaced the latest patch.

Changed 8 years ago by benjaminfjones

adds lambert_w and lambert_w_branch functions

comment:25 Changed 8 years ago by kcrisman

  • typo
      SciPy is used to evalute
    
  • Will this conflict with the beta function patch when it comes to the random test?
  • I'm wondering whether we should add a couple conversions to Mma/Maple? in the init (apparently Maxima is working fine, though see this possible enhancement). Mathematica apparently calls it ProductLog...

comment:26 Changed 8 years ago by benjaminfjones

  • I'll fix the typo (tomorrow)
  • I'll make the beta function ticket a dependency so the random test will come out correctly after the two patches are merged in order
  • ProductLog in Mathematica puts the branch parameter first. I guess it makes sense to be consistent with that convention as well as the discussion on the Maxima list. I can't figure out whether the generalized lambert function ever made it into Maxima..

Changed 8 years ago by benjaminfjones

addressed reviewer issues, changed order of arguments to be consistant with Mma/Maple?

Changed 8 years ago by benjaminfjones

fixes random tests after rebasing against #9130

comment:27 Changed 8 years ago by benjaminfjones

  • Dependencies set to #9130
  • Description modified (diff)

comment:28 follow-up: Changed 8 years ago by burcin

  • Reviewers changed from Keshav Kini, Karl-Dieter Crisman, Fredrik Johansson to Keshav Kini, Karl-Dieter Crisman, Fredrik Johansson, Burcin Erocal
  • Summary changed from Sage is missing the lambert_w function conversion from Maxima to Sage is missing the lambert_w function

Do we really want to call this function lambert_w_branch()? Can we name it lambert_w()? I would even suggest to add custom printing methods (_print_() and _print_latex_()) to avoid printing the branch argument if it is 0.

If the function is named lambert_w, you can remove the wrapper function lambert_w() and the manual manipulation of the symbol table. In this case, a custom __call__() method would take the place of the wrapper method.

BTW, we should either open a new ticket to add known exact evaluations to _eval_() or do this here:

  • 0 -> 0
  • e -> 1
  • -1/e -> -1

comment:29 Changed 8 years ago by burcin

  • Status changed from needs_review to needs_work

I have one more comment. Sorry for multiple emails.

You should check if the parent is RDF or CDF using is, not ==. In this context, parent is an argument to the _evalf_() method, which overrides the parent() function imported from sage.structure.coerce. I suggest naming the argument parent_d instead of parent. Then you can do:

R = parent_d or parent(z)
if R is float or R is complex or R is RDF or R is CDF: 
    import scipy.special 
    return scipy.special.lambertw(z, n) 
else: 
    import mpmath 
    return mpmath_utils.call(mpmath.lambertw, z, n, parent=parent) 

comment:30 in reply to: ↑ 28 Changed 8 years ago by kcrisman

Replying to burcin:

Do we really want to call this function lambert_w_branch()? Can we name it lambert_w()? I would even suggest to add custom printing methods (_print_() and _print_latex_()) to avoid printing the branch argument if it is 0.

That's a great idea.

comment:31 follow-up: Changed 8 years ago by benjaminfjones

  • Dependencies changed from #9130 to #12507

I've written a new patch that includes significant changes compared to the last one. I think I've included all of burcin's suggestions and I think it's much improved now. All tests pass with the patch applied on 5.0.beta4 + #12507.

One thing I haven't managed to figure out is how to get integration to work, e.g.

sage: integrate(lambert_w(x), x)
...
RuntimeError: ECL says: Error executing code in Maxima: lambert_w: wrong number of arguments.

I guess that's because there isn't a two-argument version of lambert_w defined in maxima. The conversion maxima -> Sage works (as shown in one of the doctests) but it looks like the other way doesn't. Another example:

sage: maxima(lambert_w(5))
Maxima ERROR:
	
lambert_w: wrong number of arguments.
 -- an error. To debug this try: debugmode(true);

Q: How do I get around this?

Numerical integration also fails unless I pass a lambda function:

sage: numerical_integral(lambert_w(x), 0, 1)
Exception TypeError: "function not supported for these types, and can't coerce safely to supported types" in 'sage.gsl.integration.c_ff' ignored
...
(0.0, 0.0)

but ....

sage: numerical_integral(lambda x: lambert_w(x), 0, 1)
(0.33036612476168054, 3.667800782666048e-15)

Q: How do I fix this?

Changed 8 years ago by benjaminfjones

adds lambert_w function

comment:32 Changed 8 years ago by benjaminfjones

  • Description modified (diff)

comment:33 in reply to: ↑ 31 ; follow-up: Changed 8 years ago by burcin

Replying to benjaminfjones:

I've written a new patch that includes significant changes compared to the last one. I think I've included all of burcin's suggestions and I think it's much improved now. All tests pass with the patch applied on 5.0.beta4 + #12507.

Thanks! The patch looks really good. When checking if the input is 0 in _eval_, you might want to return z instead of Integer(0) to preserve the type of the input. Similarly, we should return parent(z)(1) or parent(z)(-1) in the other branches.

<snip>

I guess that's because there isn't a two-argument version of lambert_w defined in maxima. The conversion maxima -> Sage works (as shown in one of the doctests) but it looks like the other way doesn't. Another example:

sage: maxima(lambert_w(5))
Maxima ERROR:
	
lambert_w: wrong number of arguments.
 -- an error. To debug this try: debugmode(true);

Q: How do I get around this?

You need to define _maxima_init_evaled_(). See line 895 of sage/fuctions/other.py:

http://hg.sagemath.org/sage-main/file/c239be1054e0/sage/functions/other.py#l895

comment:34 in reply to: ↑ 33 Changed 8 years ago by benjaminfjones

Replying to burcin:

You need to define _maxima_init_evaled_(). See line 895 of sage/fuctions/other.py:

http://hg.sagemath.org/sage-main/file/c239be1054e0/sage/functions/other.py#l895

It seems that adding _maxima_init_evaled_() solves one issue, converting to Maxima with _maxima_(),

sage: lambert_w(x)._maxima_()
lambert_w(x)
sage: lambert_w(1,x)._maxima_()
...
NotImplementedError: Non-principal branch lambert_w[1](x) is not implemented in Maxima

but integration still doesn't work (same error is raised as before). Looking closer it seems that the issue is here:

sage: z = lambert_w(x)
sage: z.operands()
[0, x]
sage: z.operator()
lambert_w

because when sr_to_max is called in the integration code, I get:

sage: from sage.interfaces.maxima_lib import sr_to_max
sage: sr_to_max(lambert_w(x))
<ECL: ((%LAMBERT_W) 0 $X)>
sage: sr_to_max(lambert_w(1, x))
<ECL: ((%LAMBERT_W) 1 $X)>

and Maxima barfs because it doesn't know what to do with ((%LAMBERT_W) 0 $X).

Changed 8 years ago by benjaminfjones

added custom latex printing and Maxima initialization

comment:35 Changed 8 years ago by benjaminfjones

  • Status changed from needs_work to needs_review

I've posted my latest patch in case anyone wants to play around with getting integration of lambert_w to work.

This issue could be a new ticket. One solution I can see is to add lambert_w to the special_sage_to_max dictionary in sage/intefaces/maxima_lib.py. There are a few other special functions listed there (like Ei and polylog) that need special conversions to maxima.

So, I propose that we either:

  1. Review patch trac_11888_v6.patch and open a new ticket for the integration issue
  2. Agree on a simple workaround like adding lambert_w to special_sage_to_max and I'll add it to the patch.

comment:36 Changed 8 years ago by kcrisman

I think that b. makes sense. You'd also have to add max_lambert_w at about this spot but having numerical integrals would be worth it.

I assume that this is a pretty easy change? If not, I guess we could just document that this doesn't work yet. In either case something should be documented, though, since half of our bug reports seem to be people using new, cool functionality who then expect that new functionality to be fully featured as well - i.e., it's not really a bug report at all, but a feature request. Having good doc for what we don't do will help with that.

comment:37 Changed 8 years ago by kcrisman

  • Status changed from needs_review to needs_work

'Needs work' for b.

comment:38 Changed 8 years ago by benjaminfjones

  • Status changed from needs_work to needs_review

Success!

Symbolic and numerical integration now work as expected for the principle branch. I added doctests to indicate what is and is not implemented.


One other comment, to indicate what causes errors, I want to add doctests to lambert_w such as:

sage: integral(lambert_w(1,x), x)                                                                   
ERROR: An unexpected error occurred while tokenizing input                                          
...                                                                                                 
RuntimeError: ECL says: Error executing code in Maxima: lambert_w: expected exactly 1 arguments.   

and

sage: numerical_integral(lambert_w(x), 0, 1)                                                        
Exception TypeError: "function not supported for these types, and can't coerce safely to supported types" in 'sage.gsl.integration.c_ff' ignored                                                    
...                                                                                                 
(0.0, 0.0)

but the doctest framework doesn't recognize the Exception TypeError and it seems to automatically fail if a RuntimeError is raised. If I put the latter 4 lines in the docstring for lambert_w, it fails doctesting, the framework only sees the (0.0, 0.0) part at the end. Is there a way around either of these issues?

Changed 8 years ago by benjaminfjones

adds lambert_w function, including integration

comment:39 Changed 8 years ago by benjaminfjones

  • Description modified (diff)

comment:40 Changed 7 years ago by davidloeffler

Apply trac_11888_v7.patch

(for patchbot, which is trying to apply all nine patches at once)

comment:41 Changed 7 years ago by benjaminfjones

  • Description modified (diff)

comment:42 Changed 7 years ago by benjaminfjones

  • Dependencies #12507 deleted

I removed #12507 from dependencies since it was merged in 5.0.beta5 and now the patchbot is getting confused trying to apply #12507 to 5.0.beta12 before testing.

I verified that trac_11888_v7.2.patch applies cleanly to 5.0.beta12 and I'm rerunning a patchbot instance on it.

comment:43 Changed 7 years ago by kcrisman

I'm sure we can finish this off next week in Seattle. Meanwhile, an interesting update from the Maxima developers about coming attractions:

Message: 4
Date: Thu, 17 May 2012 04:31:13 +0000 (UTC)
From: Robert Dodier <robert.dodier@gmail.com>
To: maxima@math.utexas.edu
Subject: Re: [Maxima] Generalized Lambert W function - premature
       commit
Message-ID: <jp1uuh$jv8$1@dough.gmane.org>

On 2012-05-17, David Billinghurst <dbmaxima@gmail.com> wrote:

> Oops. I have accidentally committed some code for Generalized Lambert
> W function to src/specfn.lisp.  Still getting my head around git.

> The code seems functionally correct, and passes tests in
> tests/rtest_lambert_w.mac, but I hadn't finished polishing it and it
> is still undocumented.  Unless anyone objects, I will leave it in
> place for the time being.

No problem, OK by me.

> There is a new function generalized_lambert_w(k,z) that returns the
> kth branch W_k(z).  There are float and bigfloat routines for complex
> z.  generalized_lambert_w(0,z) is not (yet) simplified to
> lambert_w(z), as I hadn't decided if this should be done
> unconditionally or controlled by a flag.  Thoughts?

Is it more convenient to simplify W_0(z) instead of W(z) ? If not, then
it seems reasonable to just go ahead and simplify it.

If you decide against automatically simplifying W_0(z) to W(z), I guess
I hope you don't make it controlled by a flag; flags cause trouble,
because one can't guess by looking at some code how it's going to turn
out. How about a function to carry out the simplification.

comment:44 Changed 7 years ago by benjaminfjones

That will be good; should allow us to integrate the non-principle branch. Although, in Sage 5.0, we're still a major version behind the current Maxima release.

comment:45 Changed 7 years ago by rbeezer

I am reviewing an expository paper about the Lambert W function and it says "Maple this" and "Maple that". Let's get this into Sage and stay competitive with the M's! ;-)

Rob, in cheerleader mode

Changed 7 years ago by benjaminfjones

removed trailing whitespace

comment:46 Changed 7 years ago by benjaminfjones

I agree! In that spirit, here is a rebase of the patch for Sage-5.0.

comment:47 follow-up: Changed 7 years ago by zimmerma

this ticket is pointed out on the SD40.5 wiki page. Is there any particular thing one should review?

Paul

comment:48 Changed 7 years ago by dsm

It looks like a few principle/principal mixups made it through.

comment:49 in reply to: ↑ 47 Changed 7 years ago by kcrisman

this ticket is pointed out on the SD40.5 wiki page. Is there any particular thing one should review?

I think that just checking everything still works and that syntax is proper (and spelling, thanks dsm) is good. Checking some random values against another program would be helpful, especially for the branches. Making sure documentation builds and looks good. But this has been looked at by a lot of eyes, so I don't think it needs to be gone over completely from scratch, especially since I think Ben has doctested a lot of the issues raised in previous comments (which are worth scanning).

comment:50 Changed 7 years ago by benjaminfjones

  • Keywords sd40.5 added

comment:51 Changed 7 years ago by benjaminfjones

I can make a quick spelling fix patch, but I'll wait and see if anyone else has changes to suggest.

comment:52 Changed 7 years ago by dsm

After some real-world discussions, I'd prefer to avoid falling into Python ints for (some of) the special values:

sage: parent(lambert_w(0))
Integer Ring
sage: parent(lambert_w(e))
<type 'int'>
sage: parent(lambert_w(-1/e))
<type 'int'>
sage: parent(lambert_w(SR(-1/e)))
<type 'int'>
sage: parent(lambert_w(SR(0)))
Integer Ring

Mysteriously enough, instrumenting it reveals that _eval_ is actually returning SR(1) which then in some part of the code I don't understand becomes int(1) before we get it back. If we explicitly return Integer(1) then it seems to stay as Integer(1). This isn't the biggest deal in the world but there have been several bug reports caused by something falling out of Sagespace into Pythonspace.

comment:53 Changed 7 years ago by dsm

It looks like whatever happens after _eval_ might "dereference" the SR; the call seems to give Integer(1) if _eval_ returns SR(Integer(1)). Probably someone who actually knows what's going on could explain it in one line.

comment:54 Changed 7 years ago by benjaminfjones

One solution is to change the return statements in these special cases where the automatic simplification returns an integer to just explicitly return Integer(1), etc..

How does that sound?

comment:55 Changed 7 years ago by benjaminfjones

  • Description modified (diff)

New patch is ready for review. I hope this can be the final revision!

Patchbot: apply trac_11888_v8.patch to $SAGE_ROOT/devel/sage

comment:56 Changed 7 years ago by dsm

Looking at it now.

Changed 7 years ago by benjaminfjones

fixed spelling / grammer mistakes, returned parent(Integer(...)) for special values

comment:57 Changed 7 years ago by dsm

Okay, this looks good. Two copyedits, an extra doc describing the behaviours of the derivative function, some tests making sure we can't differentiate with respect to the branch number, and the addition of lambert_W(-pi/2) = pi/2*I as a special value.

I give positive review to the preexisting parts of v8; if the new bits of v9 look okay I think we're good to go.

Changed 7 years ago by dsm

post-review version of lambertw sf

comment:58 Changed 7 years ago by benjaminfjones

New changes look good; good catch about the derivative w.r.t. branch. All relavent tests pass for me on sage-5.0. I would say this is ready to go in! Thanks for the very thorough review, Doug.

comment:59 Changed 7 years ago by dsm

  • Reviewers changed from Keshav Kini, Karl-Dieter Crisman, Fredrik Johansson, Burcin Erocal to Keshav Kini, Karl-Dieter Crisman, Fredrik Johansson, Burcin Erocal, Douglas McNeil
  • Status changed from needs_review to positive_review

comment:60 Changed 7 years ago by dsm

+1. Tx for the work!

comment:61 Changed 7 years ago by was

  • Reviewers changed from Keshav Kini, Karl-Dieter Crisman, Fredrik Johansson, Burcin Erocal, Douglas McNeil to Keshav Kini, Karl-Dieter Crisman, Fredrik Johansson, Burcin Erocal, Douglas McNeil, William Stein
  • Status changed from positive_review to needs_work

This patch is awesome! It's also a great example of how to make a well-documented new symbolic function that illustrates many issues. Here are a few trivial nitpicks:

  • What is "simplication"?
    When automatic simplication occurs, the parent of the output value should be 
    
  • This docstring should start with r" since it contains a backslash:
            646	        """ 
     	647	        The derivative of `W_n(x)` is `W_n(x)/(x \cdot W_n(x) + x)`. 
    

(check for similar instances throughout).

  • Don't use periods at the end of exceptions (also don't capitalize). Many instances of this being wrong, e.g.,
     	679	            raise ValueError("Derivative not defined with respect to the branch number.") 
    

Here's a good example of what an exception string should look like (built into python):

>>> 1/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

comment:62 Changed 7 years ago by dsm

Updated version taking into account comments of was.

comment:63 Changed 7 years ago by dsm

  • Status changed from needs_work to needs_review

Changed 7 years ago by dsm

minor edits

comment:64 Changed 7 years ago by was

  • Status changed from needs_review to positive_review

comment:65 Changed 7 years ago by jdemeyer

  • Merged in set to sage-5.1.beta4
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.