TheAlgorithms/Python · error · ValueError
Expected a_coeffs to have {self.order + 1} elements for {sel
Error message
Expected a_coeffs to have {self.order + 1} elements for {self.order}-order filter, got {len(a_coeffs)} What it means
Raised by capture_radii() in physics/basic_orbital_capture.py when the target body's mass is negative. The function computes the gravitational capture radius of a body (mass, radius) for a projectile at a given velocity, and a negative mass makes the escape-velocity term physically meaningless. This is a pure input-validation guard fired before any math runs.
Source
Thrown at audio_filters/iir_filter.py:63
This method works well with scipy's filter design functions
>>> # Make a 2nd-order 1000Hz butterworth lowpass filter
>>> import scipy.signal
>>> b_coeffs, a_coeffs = scipy.signal.butter(2, 1000,
... btype='lowpass',
... fs=48000)
>>> filt = IIRFilter(2)
>>> filt.set_coefficients(a_coeffs, b_coeffs)
"""
if len(a_coeffs) < self.order:
a_coeffs = [1.0, *a_coeffs]
if len(a_coeffs) != self.order + 1:
msg = (
f"Expected a_coeffs to have {self.order + 1} elements "
f"for {self.order}-order filter, got {len(a_coeffs)}"
)
raise ValueError(msg)
if len(b_coeffs) != self.order + 1:
msg = (
f"Expected b_coeffs to have {self.order + 1} elements "
f"for {self.order}-order filter, got {len(a_coeffs)}"
)
raise ValueError(msg)
self.a_coeffs = a_coeffs
self.b_coeffs = b_coeffs
def process(self, sample: float) -> float:
"""
Calculate :math:`y[n]`
>>> filt = IIRFilter(2)
>>> filt.process(0)
0.0View on GitHub (pinned to f5988cc097)
Solutions
- Check the sign of target_body_mass before the call and fix the data source if it is negative.
- If the value comes from a difference, clamp or validate the intermediate result (e.g. abs() only if physically valid).
- Wrap the call in try/except ValueError and report which argument was invalid to the caller.
Example fix
# before
r = capture_radii(mass_from_sensor, 6.957e8, 30000)
# after
if mass_from_sensor < 0:
raise ValueError(f"bad sensor mass: {mass_from_sensor}")
r = capture_radii(mass_from_sensor, 6.957e8, 30000) Defensive patterns
Strategy: validation
Validate before calling
if target_body_mass < 0:
raise ValueError(f"target_body_mass must be >= 0, got {target_body_mass}")
radius = capture_radii(target_body_mass, target_body_radius, projectile_velocity) Type guard
def is_valid_mass(m: object) -> bool:
return isinstance(m, (int, float)) and not isinstance(m, bool) and m >= 0 Try / catch
try:
r = capture_radii(m, R, v)
except ValueError as e:
logger.error("capture_radii rejected inputs: %s", e)
raise Prevention
- Validate mass sign at the data source, not at the physics call site.
- Never use negative sentinels for mass; use None.
- Add unit tests with boundary mass=0 (valid) and mass=-1 (raises).
When it happens
Trigger: Calling capture_radii(target_body_mass=-1.99e30, target_body_radius=6.957e8, projectile_velocity=30000), or passing a mass computed from a subtraction/difference (e.g. mass_lost = m1 - m2) that goes negative.
Common situations: Unit-conversion mistakes (grams vs kilograms, negative exponents typos like -1.99e30), sign errors when deriving mass from momentum/velocity data, or feeding parsed CSV/simulation data with missing values defaulted to negative sentinels.
Related errors
- n must not be negative
- Depth cannot be less than 0
- Invalid velocity. Should be a positive number.
- All input parameters must be positive
- Invalid inputs. Enter positive value.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/444ffff77fc15ca6.
Report an issue: GitHub.