TheAlgorithms/Python · error · ValueError
All input parameters must be positive
Error message
All input parameters must be positive
What it means
Raised by hubble_parameter() when any of redshift, radiation_density, matter_density, or dark_energy is negative. The function computes the Hubble parameter H(z) from density parameters that are physically non-negative fractions. Zero is allowed (the doctest shows redshift=0 returning hubble_constant unchanged).
Source
Thrown at physics/hubble_parameter.py:74
>>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,
... matter_density=-0.3, dark_energy=0.7, redshift=1)
Traceback (most recent call last):
...
ValueError: All input parameters must be positive
>>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,
... matter_density= 1.2, dark_energy=0.7, redshift=1)
Traceback (most recent call last):
...
ValueError: Relative densities cannot be greater than one
>>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,
... matter_density= 0.3, dark_energy=0.7, redshift=0)
68.3
"""
parameters = [redshift, radiation_density, matter_density, dark_energy]
if any(p < 0 for p in parameters):
raise ValueError("All input parameters must be positive")
if any(p > 1 for p in parameters[1:4]):
raise ValueError("Relative densities cannot be greater than one")
else:
curvature = 1 - (matter_density + radiation_density + dark_energy)
e_2 = (
radiation_density * (redshift + 1) ** 4
+ matter_density * (redshift + 1) ** 3
+ curvature * (redshift + 1) ** 2
+ dark_energy
)
hubble = hubble_constant * e_2 ** (1 / 2)
return hubble
if __name__ == "__main__":View on GitHub (pinned to f5988cc097)
Solutions
- Clamp or validate all four parameters (redshift, radiation_density, matter_density, dark_energy) to >= 0 before the call.
- If you need past-epoch (negative redshift) values, compute them outside this function or patch the guard, since the library intentionally forbids z < 0.
- Catch ValueError and report which parameter was negative.
Example fix
# before hubble_parameter(68.3, 1e-4, 0.3, 0.7, -0.5) # ValueError # after hubble_parameter(68.3, 1e-4, 0.3, 0.7, 0.0) # z >= 0 only
Defensive patterns
Strategy: validation
Validate before calling
params = {'redshift': z, 'radiation_density': wr, 'matter_density': wm, 'dark_energy': wl}
bad = [k for k, v in params.items() if v < 0]
if bad:
raise ValueError(f'negative cosmology params: {bad}')
hubble_parameter(hubble_constant, wr, wm, wl, z) Try / catch
try:
hubble_parameter(h0, wr, wm, wl, z)
except ValueError as e:
# distinguish the two guards by message
print('cosmology input invalid:', e) Prevention
- Remember negative redshifts (past epochs) are intentionally unsupported.
- Store density parameters as fractions in [0, 1] from the start.
- Validate at the data-ingestion layer, not at the physics call site.
When it happens
Trigger: hubble_parameter(hubble_constant=68.3, radiation_density=-1e-4, matter_density=0.3, dark_energy=0.7, redshift=1), or a negative redshift such as redshift=-0.5 intended to model an earlier epoch.
Common situations: Using negative redshifts for past cosmological epochs (physically standard, rejected here); sign errors when converting between density conventions; data-entry mistakes in parameter tables.
Related errors
- Expected a_coeffs to have {self.order + 1} elements for {sel
- n must not be negative
- Depth cannot be less than 0
- Invalid velocity. Should be a positive number.
- Relative densities cannot be greater than one
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/d7b9febbee8f5e22.
Report an issue: GitHub.