TheAlgorithms/Python · error · ValueError

Expected b_coeffs to have {self.order + 1} elements for {sel

Error message

Expected b_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 radius is negative. The radius is used both as a divisor in the escape-velocity-squared term and as a multiplier of the capture radius, so a negative value produces a nonsensical (and negative under the sqrt argument) result. The guard fires immediately after the mass check, before the velocity check.

Source

Thrown at audio_filters/iir_filter.py:70

        >>> 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.0
        """
        result = 0.0

        # Start at index 1 and do index 0 at the end.
        for i in range(1, self.order + 1):
            result += (
                self.b_coeffs[i] * self.input_history[i - 1]

View on GitHub (pinned to f5988cc097)

Solutions

  1. Verify target_body_radius is positive before calling; fix the source data.
  2. Confirm the argument order: capture_radii(radius, mass, velocity) — radius is the first positional parameter.
  3. If -1 is used as an 'unknown' sentinel, replace it with explicit None handling before the call.

Example fix

# before
capture_radii(radius_km * -1, mass, vel)  # sign slip

# after
capture_radii(abs(radius_km) * 1000, mass, vel)  # km -> m, positive
Defensive patterns

Strategy: validation

Validate before calling

if target_body_radius < 0:
    raise ValueError(f"target_body_radius must be >= 0, got {target_body_radius}")
radius = capture_radii(mass, target_body_radius, velocity)

Type guard

def is_valid_radius(r: object) -> bool:
    return isinstance(r, (int, float)) and not isinstance(r, bool) and r >= 0

Try / catch

try:
    r = capture_radii(m, R, v)
except ValueError as e:
    if "Radius" in str(e):
        R = abs(R)  # only if sign is a known artifact
    else:
        raise

Prevention

When it happens

Trigger: Calling capture_radii(1.99e30, -6.957e8, 30000); passing a radius in the wrong unit scale or a radius derived as (diameter/2) with a sign typo; passing -1 as a sentinel 'unknown' value.

Common situations: Parsing radius data where a leading minus sign was accidental, mixing up argument order (radius passed where mass belongs), or using negative sentinel values (-1) for 'not configured' inputs.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/f2c1fd004e03742e. Report an issue: GitHub.