def powerlaw(r,L,extra=False):
    """
    POWERLAW Powerlaw function and derivatives.

       f = powerlaw(R,L) is the powerlaw function with length scale
       parameter L evaluated at R.

       The length scale L is defined here as L = sqrt(-1/f''(0)).

       f,df,ddf = powerlaw(R,L,extra=True) also returns df = (1/r) df/dr and
       ddf = r d/dr (1/r) df/dr = r d/dr df.

       Based on Matlab script of 14Nov97 by Dick Dee.
    """

    f = 1./(1. + 0.5*(r/L)**2)

    if extra:
        df  = (-1/(L**2))*(f**2);
        ddf = (-2*(r/L)**2)*(f*df);
        return (f,df,ddf)
    else:
        return f