[SciPy-User] maping a float to a function value
Wed Dec 9 18:48:08 CST 2009
2009/12/9 nicky van foreest <email@example.com>:
>> The question is: why do you want to use floats as keys in a dictionary?
> One argument is that I like it conceptually. The technical argument
> is as follows. I need the (numerical) solution of the integral
> gamma(x) = c(x) + \int_0^\infty gamma(x-y) G(y) dy,
> for given functions c(x) and G(x). (There are some technical
> conditions on G and c such that I can prove that the integral equation
> has a solution.) Now I like to store the values gamma(x) as keys of
> x, as it feels natural. Moreover, I need gamma in a second integral
> equation. Sure I can store gamma as an array, but then I have to
> convert the index i to the key x, and I dislike this, as it is less
> elegant, and requires extra code.
> I hope the above clarifies my point, but I am not quite sure...
I think you may be going about this the wrong way around. Solving
integral equations is generally hard, so you should be thinking about
the algorithm you will use. Whatever algorithm you choose will imply a
representation of gamma(x), necessarily in some finite amount of
space. I would worry less about how "natural" your representation is
than about whether it suits how you want to solve the problem.
In this particular case, gamma(x) can be written entirely in terms of
c(x) and values of gamma(z) for z<x. So I imagine you will build gamma
from left to right (hopefully you have some sort of "initial
conditions" that allow this). If this works, you may want to represent
gamma on some predefined grid; I would not worry about the costs
involved in working with a pair of arrays (x, gamma(x)), particularly
since you will probably traverse them in order.
If your problem can't be worked out "left-to-right" you might have to
discretize it into a linear algebra problem. The most natural approach
is again to represent gamma(x) at a discrete set of points, but you
might find orthogonal polynomials, sinusoids, or even wavelets give
better performance. With a little luck and cleverness you might be
able to make the linear system sparse.
Finally, there may be a natural iterative solution to the problem.
Here too, then, the problem is to represent the function gamma(x) in
such a way that the operator you want to iterate is easy to apply.
Natural objects that are easy to integrate against are
discretely-sampled functions, splines, sinusoids, and orthogonal
polynomials among others.
In any case, I think the representation you choose will depend heavily
on the algorithm you plan to use. If you choose to use a
representation in terms of pairs (x,gamma(x)), allow me to recommend
using a pair of arrays, xi, gamma(xi), with the xi sorted. This is
much more space-efficient than a dictionary and quite fast to search
using searchsorted; in the very likely case that you are looking up an
x value that is not already in the array, you have easy access to the
neighbors if you want some form of interpolation.
More information about the SciPy-User