How to define a function that returns the derivative to be later passed in a lambda function

Viewed 36

Consider the following Python code

def Hubble_a(a):
    ...
    return

def t_of_a(a):
    res = np.zeros_like(a) 
    for i,ai in enumerate(a):
        t,err = quad(lambda ap : 1.0/(ap*Hubble_a(ap)),0,ai) 
        res[i] = t
        return res

a = np.logspace(-8,1,100)

What I want to do is to define a function Hubble_a(a) that gives the derivative of a divided by a, in order to integrate over it with quad. I tried to define it in this way:

def Hubble_a(a):
    da = diff(a,1)
    da_over_a = da/a
    return da_over_a

where diff is the FFT derivative imported from scipy.fftpack. Then, if I execute t_of_a(a) I get a object of type 'float' has no len() error, presumably because quad doesn't take arrays? However I don't think this definition makes any sense in the first place because I want to pass a function such that lambda maps to 1.0/(ap*Hubble_a(ap) and know I'm passing the derivative of an array instead of a function that can then by integrated over. So I'm looking for help on how to implement a function that maps to something like (da/dt)/a.

0 Answers
Related