[SciPy-user] Partial Derivatives

Travis Oliphant oliphant at ee.byu.edu
Fri Feb 25 17:56:21 CST 2005


R. Padraic Springuel wrote:

> Is there a way to use the "derivative" function to take a partial 
> derivative?  What if one of the inputs is a list of numbers instead of 
> a single number?
>
> Example:
> def y(x,p):
>    return p[0] + p[1]*x + p[2]*x**2
>
> I can get the derivative with respect to x fairly easily with the 
> following:
>
> derivative(y, 3, args=([[0, 0, 1]])
>
> where the numbers can be arbitrarily replaced.  However I can't seem 
> to get the derivative with respect to say p[0] or any other member of 
> p.  I keep getting a TypeError regarding the number of arguments for 
> y.  Any suggestions?


derivative is hard-wired for the first argument.  This could be changed 
relatively easily and is a nice suggestion.

In the mean-time, you can define intermediate functions:

def y2(p0,x,p1,p2):
       return y(x,[p0,p1,p2])

def y3(p1, x, p0, p2):
       return y(x,[p0,p1,p2])

def y4(p2, x, p0, p1):
       return y(x, [p0, p1, p2])

and use derivative on those functions.


-Travis




More information about the SciPy-user mailing list