cosmo123
cosmo123

Reputation: 9

Creating array from two variable function with one variable being an array

As I did in cases zArr and rzArr I want to use function dl(z) to create an array or list dlArr that contains the generated values from dl given z from zArr and rz from rzArr. I tried doing it like this but the syntax is wrong:

dlArr= [(dl(z,rz) for z in zArr & rz in rzArr)]

Relating code for reference:

zArr = np.linspace(0.01, 2.0, 1048)

def dist_integrand(z):
   dist_integrand = 1.0 / np.sqrt(Omegam * (1 + z) ** 3 + Omegal)
   return dist_integrand

def rz(z):
   rz = integrate.quad(dist_integrand, 0, z)
   return rz

rzArr = (rz(z) for z in zArr)

def dl(z,rz):
    dl = (1+z) * rz
    return dl

#!!
dlArr= [(dl(z,rz) for z in zArr & rz in rzArr)]

Upvotes: 1

Views: 136

Answers (1)

J_H
J_H

Reputation: 20435

You want to iterate over a zipped pair of arrays:

... for z, rz in zip(zArr, rzArr)

https://docs.python.org/3/library/functions.html#zip

The zip( ... ) call is generating a sequence of tuples, and then a tuple unpack operation binds the z, rz names to each generated value.


To better see the details of what's going on, try this:

from pprint import pp

pp(list(zip(zArr, rzArr)))

Upvotes: 1

Related Questions