python-adaptive / adaptive

:chart_with_upwards_trend: Adaptive: parallel active learning of mathematical functions
http://adaptive.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.16k stars 60 forks source link

adaptive.Learner2D does not allow return of 2D array #205

Open tomlaeven opened 5 years ago

tomlaeven commented 5 years ago

The following code causes the 2D learner to break, whereas the 1D learner works fine.

import adaptive
import numpy as np
adaptive.notebook_extension()

def f(x):
    return np.array([[x, x**2], [-x, 1/(1+x**2)]])

def g(xy):
    x, y = xy
    return np.array([[x, x**2], [-x, 1/(1+x**2)]])

learner1d = adaptive.Learner1D(f, [0, 1])
learner2d = adaptive.Learner2D(g, [[0, 1], [0, 1]])

runner1d = adaptive.Runner(learner1d, goal=lambda l: l.npoints > 1000)
runner1d.live_info()

runner2d = adaptive.Runner(learner2d, goal=lambda l: l.npoints > 1000)
runner2d.live_info()
akhmerov commented 5 years ago

I don't think learner 1D is meant to work with 2D arrays. You may reshape your outputs to be a 1D array and then reshape them back though. Would that be a satisfactory solution?

tomlaeven commented 5 years ago

The datasaver also allows for any data format, so it isn't really an issue anyway. The only thing is that because it works for a 1D learner, I also expected it to work for the 2D learner, which took me some time to figure out.

I think it might be nice to make it consistent, perhaps even by making it also break in the 1D case.

akhmerov commented 5 years ago

I think the documentation is already stating what the learners expect, and it's uncommon in Python packages to do anything other than duck-typing.