Skip to content

Wrong sign for right boundary with Sympy #338

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

Closed
Davide-sd opened this issue Dec 9, 2021 · 3 comments
Closed

Wrong sign for right boundary with Sympy #338

Davide-sd opened this issue Dec 9, 2021 · 3 comments

Comments

@Davide-sd
Copy link
Contributor

Davide-sd commented Dec 9, 2021

Let's consider the following simple example. First, I'm going to show the correct result when using Numpy:

import numpy as np
from adaptive import notebook_extension, Learner1D
from adaptive.runner import simple
import dill

notebook_extension()

def func(x):
    return np.imag(np.sqrt(-(x + 0j)))

def goal(learner):
    return learner.loss() < 0.1

learner = Learner1D(func, bounds=(-1, 1))
simple(learner, goal)
learner.plot()

bokeh_plot

Now, I'm going to use Sympy to generate a function which is going to be evaluated with Numpy:

from sympy import *
var("x")
f = im(sqrt(-x))

# func is going to use Numpy
func = lambdify(x, f)

def goal(learner):
    return learner.loss() < 0.1

learner = Learner1D(func, bounds=(-1, 1))
learner_bytes = dill.dumps(learner)
learner_loaded = dill.loads(learner_bytes)
simple(learner_loaded, goal)
learner_loaded.plot()

bokeh_plot(1)

As you can see, the last evaluation point (the right boundary) has the wrong sign. Even if I change the boundaries, the last point will always have the wrong sign. Why does it happen? Is it something that I can fix with some option in the learner/runner or is it an internal bug?

@basnijholt
Copy link
Member

Thanks for reporting!

That looks like a bug, I will try to look into it today or tomorrow.

@basnijholt
Copy link
Member

basnijholt commented Dec 9, 2021

Without using Adaptive:

from sympy import im, sqrt, lambdify, var
var("x")
f = im(sqrt(-x))
func = lambdify(x, f)
print(func(1))

prints: 1

and even:

from sympy import im, sqrt
im(sqrt(-1))

prints: 1

edit: however, this is the expected result, right?

@Davide-sd
Copy link
Contributor Author

Interesting, Numpy deals with (complex number) branch cuts differently depending on the input data type. For example f_numpy(1), f_numpy(1.0) outputs (1.0, -1.0). It appears that there is no bug with adaptive. Thanks for pointing me in the right direction. All I need to do is to use float boundaries. I'm closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants