Closed 62e4eb80-de3a-49e1-9cca-aae48333ddc1 closed 11 years ago
Attachment: kbessel_fixes.patch.gz
Changed keywords from bessel, bessel_K to bessel, bessel_K, editor_gfurnish
Regarding bessel_K being real for real argument and real or imaginary order, see, e.g. the appendix to H. Then, Maass cusp forms for large eigenvalues, Math. Comp. Volume 74, Number 249, pp. 363 - 381: "The K-Bessel function K_ir(x) is ... real for real arguments x and real or imaginary order ir."
I can say that I agree that this is 100% correct.
I think a solution of the following type would be better.
try:
from sage.libs.pari.all import pari
RR,a = _setup(prec)
b = RR(pari(nu).besselk(z))
pari.set_real_precision
except TypeError:
CC,a = _setup(prec)
b = CC(pari(nu).besselk(z))
pari.set_real_precision(a)
Probably the correct code would be
try:
from sage.libs.pari.all import pari
RR,a = _setup(prec)
b = RR(pari(nu).besselk(z))
pari.set_real_precision
except TypeError:
CC,a = _setup_CC(prec)
b = CC(pari(nu).besselk(z))
pari.set_real_precision(a)
Since Rishi commented on this it might be a good idea to discuss his comments.
Cheers,
Michael
The try code does not in my opinion work. The issue here is correcting numerical instabilities, which attempting to coerce into RR will not do. Positive review still.
I am pretty confident that the try code works. Consider the following gp output. The current patch will try to coerce this to RR. If pari is right then this input after applying the patch will give a TypeError.
? besselk(2,-1.121)
%1 = 1.234141459629829380224386595 - 0.5472316582663064541169798027*I
We probably eliminate the double evaluation of bessel function using pari which my current suggestion does.
I don't understand what double evaluation of the bessel function you are talking about. Furthermore the entire point is that Pari can give wrong answers in some cases because of numerical cases. Therefore we want to manually correct the numerical noise. Trying to coerce into RR instead of clearing it completely misses the point.
I refuse to believe such a large numerical instabilities. In fact maple gives the same answer as pari. I will investigate further and figure out. You cannot Willy nilly ignore the imaginary part.
And Mathematica does the same as pari.
I suggested the try: except: statement because it does not require deep understanding of Bessel's K function and probably in the end we might end up with this solution.
The try except statement does not significantly help the original complaint. Either the identity is true or we should give the patch a negative review and close it as invalid.
besselk(nu,x,{flag = 0})
K-Bessel function of index nu (which can be complex) and argument x. Only real and positive arguments x are allowed in the present version 2.3.3. If flag is equal to 1, uses another implementation of this function which is faster when x >> 1.
The library syntax is kbessel(nu,x,prec) and kbessel2(nu,x,prec) respectively.
Therefore pari gives incorrect answers for your negative x. Perhaps this is another bug and we should merge this ticket and open another one for adding some error checking to besselk?
The complaint is valid. In fact the current version is lot more broken that the patched version. The current version does not take any complex argument. The patched version only fails on negative real axis.
Ok lets patch this then and open a new ticket for the negative real axis case.
I think
if (real(nu) == 0 or imag(nu) == 0) and (imag(z) == 0)
should become
if (real(nu) == 0 or imag(nu) == 0) and (imag(z) == 0 and real(z) > 0)
Following the philosophy that wrong answers are worse than errors, this patch should not go in as is. Probably the code in Rishi's most recent comment is all that is needed for a fix, as long as we're not missing something else. I can't fix this at exactly this moment, but the fix should be trivial. Anyway, even though I posted the original patch, I have to give this a -1.
rishi's code does not prevent brokenness at all (in fact it is 100% equivalent to attempting to trying to return RR(answer, prec). The patch as is makes the answer "more correct," and then we can go back and write code (that makes use of this patch) to make it 100% correct. Alternatively, if someone wants to make a new patch that checks for real(z)>=0 in all cases and throws an error otherwise, I would give that a positive review. However, the modification of real(z)>0 is not sufficient to ensure correctness.
This ticket has been sitting around for a while without any movement. Change the title so that the reports pick up this ticket correctly.
Cheers,
Michael
I note that Alex added himself to the CC for this. Whatever is done for this issue absolutely must take into account the work done for #4096, so at the least I suggest that the author of this patch looks at that one and reworks this. Anything relying on Sage/pari precision questions is likely to be useless otherwise.
I looked up the definition and properties of the Bessel functions in several references (Section 7.2 of the "Bateman Manuscript Project", for instance).
I uploaded a brand new patch that implements the behavior described there, namely returning a real number if the result is theoretically known to be real, and a complex number otherwise. I added doctests that document this behavior, and checked all of them against Mathematica. I did this for all three Bessel functions that are implemented in special.py using Pari, namely J, K, and I. I also put in a workaround for a silly Pari buglet that complains about negative integer values of nu.
In the process I uncovered a couple of unrelated issues with special.py and Bessel functions, for which I'll open separate tickets.
The patch is made against 3.1.3.alpha2.
Thanks for catching this. It actually comes from a bug in Pari:
? besselj(0, 0)
%1 = 1.000000000000000000000000000
? besselj(0.E-19, 0)
*** besselj: gpow: 0 to a non positive exponent.
I've reported it upstream, but I will post a patch with a workaround while we wait.
apply instead of the previous patch
Attachment: trac3426-fix-bessel-fns.patch.gz
OK, I've replaced my patch with one that fixes the issue reported by Dan.
See #4626, which at least fixes the bessel_J
problem.
This ticket is a huge mess :)
I now think that we should just use mpmath to evaluate Bessel functions, see
http://mpmath.googlecode.com/svn/trunk/doc/build/functions/bessel.html
For the examples that Dan gave:
sage: from mpmath import *
sage: mp.dps = 25; mp.pretty = True
sage: besselk(0, -1)
(0.4210244382407083333356274 - 3.97746326050642263725661j)
sage: besselk(-1*I - 1, 0)
+inf
sage: besselk(-1, -1)
(-0.60190723019723457473754 - 1.775499689212180946878577j)
sage: besselk(0, -1-I)
(-1.479697108749625193260947 + 2.588306443392007370808151j)
Changed keywords from bessel, bessel_K, editor_gfurnish to bessel, bessel_K
From my quick experiments with the issues Bober was dealing with a year ago, I see that do not arise if we use mpmath, even when I set the precision to 5000.
I agree with Alex Ghitza.
I should say that the bessels functions for non integer indices have always bothered me. I believe computing will involve a log, and how do you consistently choose a branch.
With some experiments, I saw that the branch of the log taken is the negative real axis. We should mention this in the documentation when it is implemented. I believe ddrake is working up a patch.
This would most likely be fixed by #4102.
Yes, it will be fixed by #4102. I'll make a note to add a related doctest to that effect.
Reviewer: Karl-Dieter Crisman, Benjamin Jones
Everything here is now in a doctest in #4102, including the stuff in the thread from three (!) years ago.
Currently we have
In this case the result actually should be a real number, so we fix this by discarding the imaginary part of the result from pari. In other cases, however, the result is actually a complex number, and we shouldn't always be attempting to cast it to a real number (which the attached patch also fixes).
CC: @burcin @kcrisman @benjaminfjones
Component: calculus
Keywords: bessel, bessel_K
Reviewer: Karl-Dieter Crisman, Benjamin Jones
Issue created by migration from https://trac.sagemath.org/ticket/3426