{"record":{"id":"f2c1fd004e03742e","repo":"TheAlgorithms/Python","slug":"expected-b-coeffs-to-have-self-order-1-element","errorCode":null,"errorMessage":"Expected b_coeffs to have {self.order + 1} elements for {self.order}-order filter, got {len(a_coeffs)}","messagePattern":"Expected b_coeffs to have (.+?) elements for (.+?)-order filter, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"audio_filters/iir_filter.py","lineNumber":70,"sourceCode":"        >>> filt = IIRFilter(2)\n        >>> filt.set_coefficients(a_coeffs, b_coeffs)\n        \"\"\"\n        if len(a_coeffs) < self.order:\n            a_coeffs = [1.0, *a_coeffs]\n\n        if len(a_coeffs) != self.order + 1:\n            msg = (\n                f\"Expected a_coeffs to have {self.order + 1} elements \"\n                f\"for {self.order}-order filter, got {len(a_coeffs)}\"\n            )\n            raise ValueError(msg)\n\n        if len(b_coeffs) != self.order + 1:\n            msg = (\n                f\"Expected b_coeffs to have {self.order + 1} elements \"\n                f\"for {self.order}-order filter, got {len(a_coeffs)}\"\n            )\n            raise ValueError(msg)\n\n        self.a_coeffs = a_coeffs\n        self.b_coeffs = b_coeffs\n\n    def process(self, sample: float) -> float:\n        \"\"\"\n        Calculate :math:`y[n]`\n\n        >>> filt = IIRFilter(2)\n        >>> filt.process(0)\n        0.0\n        \"\"\"\n        result = 0.0\n\n        # Start at index 1 and do index 0 at the end.\n        for i in range(1, self.order + 1):\n            result += (\n                self.b_coeffs[i] * self.input_history[i - 1]","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/audio_filters/iir_filter.py#L52-L88","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify target_body_radius is positive before calling; fix the source data.","Confirm the argument order: capture_radii(radius, mass, velocity) — radius is the first positional parameter.","If -1 is used as an 'unknown' sentinel, replace it with explicit None handling before the call."],"exampleFix":"# before\ncapture_radii(radius_km * -1, mass, vel)  # sign slip\n\n# after\ncapture_radii(abs(radius_km) * 1000, mass, vel)  # km -> m, positive","handlingStrategy":"validation","validationCode":"if target_body_radius < 0:\n    raise ValueError(f\"target_body_radius must be >= 0, got {target_body_radius}\")\nradius = capture_radii(mass, target_body_radius, velocity)","typeGuard":"def is_valid_radius(r: object) -> bool:\n    return isinstance(r, (int, float)) and not isinstance(r, bool) and r >= 0","tryCatchPattern":"try:\n    r = capture_radii(m, R, v)\nexcept ValueError as e:\n    if \"Radius\" in str(e):\n        R = abs(R)  # only if sign is a known artifact\n    else:\n        raise","preventionTips":["Use keyword arguments to avoid swapping mass and radius.","Keep units consistent (SI meters) across the pipeline.","Reject negative radii at parse time from external data."],"tags":["physics","input-validation","units"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}