# legendre_P(6,0) different evaluations

I tried to evaluate

legendre_P(6,0)


and

 legendre_P(6,x).subs(x=0)


in the sagecell. The first one gives 5/16 but the second one gives -5/16. Why are these two results different?

edit retag close merge delete

Sort by ยป oldest newest most voted

This is a partial answer, i have to submit now. (Maybe somebody can continue...)

The function legendre_P is "implemented" in sage by calling corresponding other function in other systems.

On my machine, asking in the sage interpreter for the help on this function gives:

Type:           Func_legendre_P
String form:    legendre_P
File:           /usr/lib/python3.8/site-packages/sage/functions/orthogonal_polys.py
Docstring:
EXAMPLES:

sage: legendre_P(4, 2.0)
55.3750000000000
sage: legendre_P(1, x)
x


and many further other lines. The line with the File: shows the location of the code, and the corresponding code is...

legendre_P = Func_legendre_P()


So we need Func_legendre_P instead, which is a class, and the constructor __init__ does the following job:

BuiltinFunction.__init__(self, 'legendre_P', nargs=2, latex_name=r"P",
conversions={'maxima':'legendre_p',
'mathematica':'LegendreP',
'maple':'LegendreP',
'giac':'legendre'})


Now the maxima function legendre_p returns used for the conversion on my machine...

sage: maxima("legendre_p(6, 0);")
-5/16


And if we call the same function with the arguments 6 and x we get...

sage: maxima("legendre_p(6, x);")
(-21*(1-x))+(231*(1-x)^6)/16-(693*(1-x)^5)/8+(1575*(1-x)^4)/8-210*(1-x)^3+105*(1-x)^2+1
sage: p = (-21*(1-x))+(231*(1-x)^6)/16-(693*(1-x)^5)/8+(1575*(1-x)^4)/8-210*(1-x)^3+105*(1-x)^2+1
sage: p.subs({x:0})
-5/16
sage: p.subs(x=0)
-5/16


Now asking for the sage legendre_P in the same session leads to a crash. This is a good sign! (This depends on perspective. Well, it is good since it must be the case of a maxima collision...) In a new session:

sage: p = (-21*(1-x))+(231*(1-x)^6)/16-(693*(1-x)^5)/8+(1575*(1-x)^4)/8-210*(1-x)^3+105*(1-x)^2+1
sage: legendre_P(6, x)
231/16*x^6 - 315/16*x^4 + 105/16*x^2 - 5/16
sage: bool( p == legendre_P(6, x) )
True


So we have problems with

sage: legendre_P(6, 0)
5/16


Let's see which values are giving the difference:

sage: for k in [-10..10]:
....:     legendre_P6k, pk = legendre_P(6, k), p.subs(x=k)
....:     if legendre_P6k == pk:
....:         print(f"k = {k} :: same value {pk}")
....:     else:
....:         print(f"k = {k} :: different values {legendre_P6k} versus {pk}")
....:
k = -10 :: same value 227860495/16
k = -9 :: same value 7544041
k = -8 :: same value 59271739/16
k = -7 :: same value 1651609
k = -6 :: same value 10373071/16
k = -5 :: same value 213445
k = -4 :: same value 867211/16
k = -3 :: same value 8989
k = -2 :: same value 10159/16
k = -1 :: same value 1
k = 0 :: different values 5/16 versus -5/16
k = 1 :: same value 1
k = 2 :: same value 10159/16
k = 3 :: same value 8989
k = 4 :: same value 867211/16
k = 5 :: same value 213445
k = 6 :: same value 10373071/16
k = 7 :: same value 1651609
k = 8 :: same value 59271739/16
k = 9 :: same value 7544041
k = 10 :: same value 227860495/16
sage:


OK, only the value 0 of the second argument leads to a difference, and the code was printing quickly the values for k from -10 to -1, then took a "long time" (2s maybe) to give me the false value for k = 0 then all the print was there.

I suspect a bad rerouting inside the classes
GinacFunction and BuiltinFunction, but i have to stop here the investigations.

more