{"record":{"id":"444ffff77fc15ca6","repo":"TheAlgorithms/Python","slug":"expected-a-coeffs-to-have-self-order-1-element","errorCode":null,"errorMessage":"Expected a_coeffs to have {self.order + 1} elements for {self.order}-order filter, got {len(a_coeffs)}","messagePattern":"Expected a_coeffs to have (.+?) elements for (.+?)-order filter, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"audio_filters/iir_filter.py","lineNumber":63,"sourceCode":"        This method works well with scipy's filter design functions\n\n        >>> # Make a 2nd-order 1000Hz butterworth lowpass filter\n        >>> import scipy.signal\n        >>> b_coeffs, a_coeffs = scipy.signal.butter(2, 1000,\n        ...                                          btype='lowpass',\n        ...                                          fs=48000)\n        >>> 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","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/audio_filters/iir_filter.py#L45-L81","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nr = capture_radii(mass_from_sensor, 6.957e8, 30000)\n\n# after\nif mass_from_sensor < 0:\n    raise ValueError(f\"bad sensor mass: {mass_from_sensor}\")\nr = capture_radii(mass_from_sensor, 6.957e8, 30000)","handlingStrategy":"validation","validationCode":"if target_body_mass < 0:\n    raise ValueError(f\"target_body_mass must be >= 0, got {target_body_mass}\")\nradius = capture_radii(target_body_mass, target_body_radius, projectile_velocity)","typeGuard":"def is_valid_mass(m: object) -> bool:\n    return isinstance(m, (int, float)) and not isinstance(m, bool) and m >= 0","tryCatchPattern":"try:\n    r = capture_radii(m, R, v)\nexcept ValueError as e:\n    logger.error(\"capture_radii rejected inputs: %s\", e)\n    raise","preventionTips":["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)."],"tags":["physics","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}