Title: linear_algebra/polynom_for_points: ZeroDivisionError on valid points containing x=0 (Gaussian elimination without pivoting) · Issue #14817 · TheAlgorithms/Python · GitHub
Open Graph Title: linear_algebra/polynom_for_points: ZeroDivisionError on valid points containing x=0 (Gaussian elimination without pivoting) · Issue #14817 · TheAlgorithms/Python
X Title: linear_algebra/polynom_for_points: ZeroDivisionError on valid points containing x=0 (Gaussian elimination without pivoting) · Issue #14817 · TheAlgorithms/Python
Description: Description linear_algebra/src/polynom_for_points.py::points_to_polynomial crashes with ZeroDivisionError for valid inputs that include a point with x = 0. It solves the interpolation system with Gaussian elimination without pivoting, so...
Open Graph Description: Description linear_algebra/src/polynom_for_points.py::points_to_polynomial crashes with ZeroDivisionError for valid inputs that include a point with x = 0. It solves the interpolation system with G...
X Description: Description linear_algebra/src/polynom_for_points.py::points_to_polynomial crashes with ZeroDivisionError for valid inputs that include a point with x = 0. It solves the interpolation system with G...
Opengraph URL: https://github.com/TheAlgorithms/Python/issues/14817
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"linear_algebra/polynom_for_points: ZeroDivisionError on valid points containing x=0 (Gaussian elimination without pivoting)","articleBody":"### Description\n\n`linear_algebra/src/polynom_for_points.py::points_to_polynomial` crashes with `ZeroDivisionError` for valid inputs that include a point with `x = 0`. It solves the interpolation system with **Gaussian elimination without pivoting**, so a zero on the diagonal divides by zero:\n\n```python\nfor count in range(x):\n for number in range(x):\n if count == number:\n continue\n fraction = matrix[number][count] / matrix[count][count] # /0 when matrix[count][count] == 0\n ...\n```\n\n`matrix[count][count] = coordinates[count][0] ** (x - (count + 1))`. When an x-coordinate is `0`, that diagonal entry is `0 ** positive = 0`, so the very first elimination step divides by zero. The input passes every validation in the function (non-empty, all pairs length-2, distinct points, distinct x-values) — it is a genuinely fittable set — and then crashes in the solver.\n\n### Steps to reproduce (verified)\n\n```python\nfrom linear_algebra.src.polynom_for_points import points_to_polynomial\n\npoints_to_polynomial([[0, 1], [1, 2], [2, 5]]) # f(x) = x^2 + 1 passes through these\n# -> ZeroDivisionError: division by zero\n\npoints_to_polynomial([[0, 0], [1, 1], [2, 4]]) # f(x) = x^2\n# -> ZeroDivisionError: division by zero\n```\n\nThe existing doctests only use x-values `1, 2, 3`, so the `x = 0` case is uncovered.\n\n### Expected behavior\n\nFor `[[0, 1], [1, 2], [2, 5]]` it should return the fitting polynomial (`f(x)=x^2*1.0+x^1*0.0+x^0*1.0`), not crash. Any set of points with distinct x-values has a unique interpolating polynomial.\n\n### Actual behavior\n\n`ZeroDivisionError` whenever any point has `x = 0` (and generally whenever a zero pivot appears on the diagonal during elimination).\n\n### Suggested fix\n\nUse **partial pivoting**: before each elimination step, swap in a row whose `matrix[r][count]` is non-zero (largest magnitude). This removes the zero-pivot crash and also improves numerical stability. A doctest with an `x = 0` point (e.g. `points_to_polynomial([[0, 1], [1, 2], [2, 5]])`) should be added to cover it.\n","author":{"url":"https://github.com/aimasteracc","@type":"Person","name":"aimasteracc"},"datePublished":"2026-06-18T12:33:59Z","url":"https://github.com/TheAlgorithms/Python/issues/14817"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4ddc7475-96de-4a18-4f1b-032e2f077782 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 812C:3C556:11F9E33:18983C4:6AFB6B6F |
| html-safe-nonce | 9a62f201361729dbace7594c8aeaa951762d2ba090eff387ff8af414ba431c24 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTJDOjNDNTU2OjExRjlFMzM6MTg5ODNDNDo2QUZCNkI2RiIsInZpc2l0b3JfaWQiOiI0MjU5OTg4NzQwNTM4ODU0MjU1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1473e01641d3919e0eded6586d1b95f1bc73d5bbfba75f6338b212f94689be2a |
| hovercard-subject-tag | issue:4692375621 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/TheAlgorithms/Python/14817/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1f11687ecc326405f057cfb949e2f80529b3e7f8e0b60a69d8666acd6a7b9810/TheAlgorithms/Python/issues/14817 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1f11687ecc326405f057cfb949e2f80529b3e7f8e0b60a69d8666acd6a7b9810/TheAlgorithms/Python/issues/14817 |
| og:image:alt | Description linear_algebra/src/polynom_for_points.py::points_to_polynomial crashes with ZeroDivisionError for valid inputs that include a point with x = 0. It solves the interpolation system with G... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aimasteracc |
| hostname | github.com |
| expected-hostname | github.com |
| None | cca289ecd54650fcf3c0b037a58b97186dee2cc4406819d87fab93728fd83700 |
| turbo-cache-control | no-preview |
| go-import | github.com/TheAlgorithms/Python git https://github.com/TheAlgorithms/Python.git |
| octolytics-dimension-user_id | 20487725 |
| octolytics-dimension-user_login | TheAlgorithms |
| octolytics-dimension-repository_id | 63476337 |
| octolytics-dimension-repository_nwo | TheAlgorithms/Python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 63476337 |
| octolytics-dimension-repository_network_root_nwo | TheAlgorithms/Python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 88808b1fae5ab8a526a73e7ce5542a10eb9d6971 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width