constant function which can be evaluated for any value but cannot be integrated numerically

58 views
Skip to first unread message

Piotr Sniady

unread,
Jul 5, 2019, 4:58:12 PM7/5/19
to sage-devel
Dear Developers,

the code below produces a function which for any real u<3 gives as value \pi/3.
One can check that for any given number Sage has no problems to evaluate this function
and that it gives the right value.

For strange reason I cannot integrate this function numerically.
Also, I cannot plot this function.

Magically, while for any given value Sage has no problems to give a complex root of a negative number,
for numerical integration the behavior of the root changes.

Any help would be appreciated.

Yours,

Piotr



def F(u):
    return arg( (-3 + u )^(1/3) ).n()

numerical_integral(F,1,2)




Error message:

negative number cannot be raised to a fractional power

Matt (trac: gh-Torrencem)

unread,
Jul 5, 2019, 5:20:35 PM7/5/19
to sage-devel
Hi, Piotr, If you cast u to be a real number, as in the below example, it produces the intended behavior:

def F(u):
    return arg( (-3 + RR(u) )^(1/3) )

numerical_integral(F,1,2)

This is because in your previous code example, u is a python floating point, and by default python throws an error when taking a fractional negative power of a float. However, Sage's real floating point numbers don't have the same restriction, which is probably what you were expecting instead. Hope this solves the problem!

Matt

Piotr Sniady

unread,
Jul 6, 2019, 1:33:30 AM7/6/19
to sage-devel
Thank you,

this solves not only the minimal non-working example but the original non-working example.
As a side effect I have learned something interesting about Sage.

Yours,

Piotr

Emmanuel Charpentier

unread,
Jul 6, 2019, 11:14:58 AM7/6/19
to sage-devel
This seems to be a follow-up to this ask.sahemath.org question.

  1. As already pointed out by Serge Lelièvre, this anomaly doesn't happent in a Python3-based Sage. Possibly, the behaviour of floats raised to a negative fractional power are different ?
  2. I took the liberty of pointing future  ask.sagemath readers to this sage-devel thread for future reference.

HTH,
Reply all
Reply to author
Forward
0 new messages