Skip to content

Commit 74aa9ef

Browse files
authored
Added a Hubble Parameter calculator file (TheAlgorithms#7921)
1 parent 7addbcc commit 74aa9ef

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

physics/hubble_parameter.py

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""
2+
Title : Calculating the Hubble Parameter
3+
4+
Description : The Hubble parameter H is the Universe expansion rate
5+
in any time. In cosmology is customary to use the redshift redshift
6+
in place of time, becausethe redshift is directily mensure
7+
in the light of galaxies moving away from us.
8+
9+
So, the general relation that we obtain is
10+
11+
H = hubble_constant*(radiation_density*(redshift+1)**4
12+
+ matter_density*(redshift+1)**3
13+
+ curvature*(redshift+1)**2 + dark_energy)**(1/2)
14+
15+
where radiation_density, matter_density, dark_energy are the relativity
16+
(the percentage) energy densities that exist
17+
in the Universe today. Here, matter_density is the
18+
sum of the barion density and the
19+
dark matter. Curvature is the curvature parameter and can be written in term
20+
of the densities by the completeness
21+
22+
23+
curvature = 1 - (matter_density + radiation_density + dark_energy)
24+
25+
Source :
26+
https://www.sciencedirect.com/topics/mathematics/hubble-parameter
27+
"""
28+
29+
30+
def hubble_parameter(
31+
hubble_constant: float,
32+
radiation_density: float,
33+
matter_density: float,
34+
dark_energy: float,
35+
redshift: float,
36+
) -> float:
37+
38+
"""
39+
Input Parameters
40+
----------------
41+
hubble_constant: Hubble constante is the expansion rate today usually
42+
given in km/(s*Mpc)
43+
44+
radiation_density: relative radiation density today
45+
46+
matter_density: relative mass density today
47+
48+
dark_energy: relative dark energy density today
49+
50+
redshift: the light redshift
51+
52+
Returns
53+
-------
54+
result : Hubble parameter in and the unit km/s/Mpc (the unit can be
55+
changed if you want, just need to change the unit of the Hubble constant)
56+
57+
>>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,
58+
... matter_density=-0.3, dark_energy=0.7, redshift=1)
59+
Traceback (most recent call last):
60+
...
61+
ValueError: All input parameters must be positive
62+
63+
>>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,
64+
... matter_density= 1.2, dark_energy=0.7, redshift=1)
65+
Traceback (most recent call last):
66+
...
67+
ValueError: Relative densities cannot be greater than one
68+
69+
>>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,
70+
... matter_density= 0.3, dark_energy=0.7, redshift=0)
71+
68.3
72+
"""
73+
parameters = [redshift, radiation_density, matter_density, dark_energy]
74+
if any(0 > p for p in parameters):
75+
raise ValueError("All input parameters must be positive")
76+
77+
if any(1 < p for p in parameters[1:4]):
78+
raise ValueError("Relative densities cannot be greater than one")
79+
else:
80+
curvature = 1 - (matter_density + radiation_density + dark_energy)
81+
82+
e_2 = (
83+
radiation_density * (redshift + 1) ** 4
84+
+ matter_density * (redshift + 1) ** 3
85+
+ curvature * (redshift + 1) ** 2
86+
+ dark_energy
87+
)
88+
89+
hubble = hubble_constant * e_2 ** (1 / 2)
90+
return hubble
91+
92+
93+
if __name__ == "__main__":
94+
import doctest
95+
96+
# run doctest
97+
doctest.testmod()
98+
99+
# demo LCDM approximation
100+
matter_density = 0.3
101+
102+
print(
103+
hubble_parameter(
104+
hubble_constant=68.3,
105+
radiation_density=1e-4,
106+
matter_density=matter_density,
107+
dark_energy=1 - matter_density,
108+
redshift=0,
109+
)
110+
)

0 commit comments

Comments
 (0)