Commit 9518e86f authored by Moyuan CHEN's avatar Moyuan CHEN
Browse files

Update utils.py

parent d97aa7df
......@@ -133,3 +133,49 @@ def vector2symmat_diag0(v):
"""
n = int(((1 + 8 * v.shape[0]) ** 0.5 + 1) / 2)
return _vector2symmat_diag0(v, jnp.zeros((n, n), dtype=v.dtype))
def get_species_from_distribution(N, species_dist):
""" Convert an array of probability distributions of M species into an
array with length N and matching distribution of the species.
Args:
N: total number of particles
species_dist: the desired distribution of species
key [Optional]: RNG key to draw random numbers to fill out the remainder
of the species array when N is not a even multiple of M
Return:
1-D Array species that has length N and specified distribution from
species_dist
Example:
N = 10, species_dist = jnp.array([0.2,0.2,0.2,0.2,0.2])
returns: [0,0,1,1,2,2,3,3,4,4]
"""
N_array = np.zeros(shape=(N,))
@jit
def jittable_sfd(species = N_array, species_dist= species_dist):
species_dist = species_dist / np.sum(species_dist)
species_dist_N = np.array(species_dist * N).astype(int)
def fill_in_number_body_fn(ind, sv):
s, v = sv
s = s.at[ind].set(v)
return (s,v)
particle_index = 0
species_index = 0
for n_species_i in species_dist_N:
species, _ = fori_loop(particle_index, particle_index + n_species_i, fill_in_number_body_fn, (species, species_index))
species_index += 1
particle_index += n_species_i
most_popular_specie = np.argsort(-species_dist_N)[0]
species = fori_loop(particle_index, N, fill_in_number_body_fn, (species, most_popular_specie))
return species
species = jittable_sfd()
return species
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment