[Numpy-discussion] any interest in including a second-ordergradient?

Robert Kern robert.kern@gmail....
Tue Oct 28 16:36:16 CDT 2008


On Tue, Oct 28, 2008 at 16:28, Andrew Hawryluk <HAWRYLA@novachem.com> wrote:
> I agree that the gradient functions should be combined, especially considering how much redundant code would be added by keeping them separate. Here is one possible implementation, but I don't like the signature yet as it departs from the current behaviour. At the risk of demonstrating my ignorance, is there no way to place the named parameter (order) after the variable-length parameter (dx) in Python?

There's no good way to do it. It's unfortunate that the current
function uses *args for this. It's just ... not good for many reasons.
A better one would be like so:

def gradient(f, dx=None, order=1):
    N = len(f.shape)
    if dx is None:
        dx = np.ones([N])
    elif np.isscalar(dx):
        dx = np.array([dx] * N)
    elif len(dx) == N:
        dx = np.asarray(dx)
    else:
        raise ValueError("dx must be blah blah blah")
    ...

Personally, I would make a new function with this API, and deprecate
the current gradient(). Dunno what the name should be, though.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list