[SciPy-User] maping a float to a function value

Rob Clewley rob.clewley@gmail....
Wed Dec 9 15:42:40 CST 2009


> The question is: why do you want to use floats as keys in a dictionary?
>
> Chuck
>
>

As the OP said, the goal is to store a general function as a set of
(x, f(x)) pairs to look up later (i.e. to store the mapping). The
issue is that f(x) may well not be known as an explicit function, e.g.
the values may have been generated from a numerical calculation (e.g.
a solution to an ODE).

One good solution is to create a 1-d Curve class that automatically
interpolates values not given in the definition by an array of (x,
f(x)) pairs. This avoids the OP's problem with dicts except when x
values are computed by an arithmetic op that puts them slightly
outside the domain of the Curve class's known range of x values. I
would catch that using allclose since it should generally be a very
small error (so you wouldn't have to worry about remembering the
precision). Class Curve could be achieved with this minor addition of
allclose by extending the scipy.interpolate.interp1d class.

-Rob


More information about the SciPy-User mailing list