Extending 'gradient' to handle function handles

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Extending 'gradient' to handle function handles

Søren Hauberg
Hi,
  The 'gradient' function currently only allows you to estimate the
gradient of discrete data (i.e. data in a matrix). I think it would make
sense if the 'gradient' function was also defined for function handles,
such that you could do something like this:

  f = @sin;
  df_dx = gradient(f, 0); # calculates the gradient at x = 0

Is this something there's interest in? The attached patch implements
this using a simple central difference scheme. For multi-dimensional
functions the API is like this:

  f = @(x,y) sin(x).*cos(x);
  [dx, dy] = gradient(f, rand(7,2)); # calculate the gradient in 7
random points

Thoughts?
Søren

gradient.patch (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Extending 'gradient' to handle function handles

John W. Eaton
Administrator
On  1-Feb-2008, Søren Hauberg wrote:

| Hi,
|   The 'gradient' function currently only allows you to estimate the
| gradient of discrete data (i.e. data in a matrix). I think it would make
| sense if the 'gradient' function was also defined for function handles,
| such that you could do something like this:
|
|   f = @sin;
|   df_dx = gradient(f, 0); # calculates the gradient at x = 0
|
| Is this something there's interest in? The attached patch implements
| this using a simple central difference scheme. For multi-dimensional
| functions the API is like this:
|
|   f = @(x,y) sin(x).*cos(x);
|   [dx, dy] = gradient(f, rand(7,2)); # calculate the gradient in 7
| random points
|
| Thoughts?

This seems like a reasonable extension.  Would you like to turn your
previous patch into an hg changeset with ChangeLog entry?  It could
also use a few style changes for consistency with the rest of Octave.

jwe

Reply | Threaded
Open this post in threaded view
|

Re: Extending 'gradient' to handle function handles

Søren Hauberg
tor, 22 01 2009 kl. 12:31 -0500, skrev John W. Eaton:
> This seems like a reasonable extension.  Would you like to turn your
> previous patch into an hg changeset with ChangeLog entry?  It could
> also use a few style changes for consistency with the rest of Octave.

Okay, I've attached a changeset for this.

Søren

gradient.changeset (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Extending 'gradient' to handle function handles

John W. Eaton
Administrator
On 22-Jan-2009, Søren Hauberg wrote:

| tor, 22 01 2009 kl. 12:31 -0500, skrev John W. Eaton:
| > This seems like a reasonable extension.  Would you like to turn your
| > previous patch into an hg changeset with ChangeLog entry?  It could
| > also use a few style changes for consistency with the rest of Octave.
|
| Okay, I've attached a changeset for this.

I applied this patch with some minor changes.

Thanks,

jwe