{"id":13715080,"url":"https://github.com/sigma-py/accupy","last_synced_at":"2025-08-16T18:10:05.167Z","repository":{"id":47254131,"uuid":"121792855","full_name":"sigma-py/accupy","owner":"sigma-py","description":":dart: Accurate sums and dot products for Python.","archived":false,"fork":false,"pushed_at":"2021-09-06T15:54:02.000Z","size":1117,"stargazers_count":106,"open_issues_count":2,"forks_count":15,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-31T19:32:35.817Z","etag":null,"topics":["accuracy","engineering","mathematics","numerical-methods","numpy","pypi","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sigma-py.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-16T19:41:13.000Z","updated_at":"2025-07-31T09:32:03.000Z","dependencies_parsed_at":"2022-09-13T04:50:33.121Z","dependency_job_id":null,"html_url":"https://github.com/sigma-py/accupy","commit_stats":null,"previous_names":["nschloe/accupy"],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/sigma-py/accupy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sigma-py%2Faccupy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sigma-py%2Faccupy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sigma-py%2Faccupy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sigma-py%2Faccupy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sigma-py","download_url":"https://codeload.github.com/sigma-py/accupy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sigma-py%2Faccupy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270749514,"owners_count":24638749,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-16T02:00:11.002Z","response_time":91,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["accuracy","engineering","mathematics","numerical-methods","numpy","pypi","python"],"created_at":"2024-08-03T00:00:53.815Z","updated_at":"2025-08-16T18:10:05.134Z","avatar_url":"https://github.com/sigma-py.png","language":"Python","funding_links":[],"categories":["Other libraries and tools"],"sub_categories":["Mesh tools"],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/nschloe/accupy\"\u003e\u003cimg alt=\"accupy\" src=\"https://nschloe.github.io/accupy/logo-with-text.svg\" width=\"40%\"\u003e\u003c/a\u003e\n  \u003cp align=\"center\"\u003eAccurate sums and (dot) products for Python.\u003c/p\u003e\n\u003c/p\u003e\n\n[![PyPi Version](https://img.shields.io/pypi/v/accupy.svg?style=flat-square)](https://pypi.org/project/accupy)\n[![PyPI pyversions](https://img.shields.io/pypi/pyversions/accupy.svg?style=flat-square)](https://pypi.org/pypi/accupy/)\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1185173.svg?style=flat-square)](https://doi.org/10.5281/zenodo.1185173)\n[![GitHub stars](https://img.shields.io/github/stars/nschloe/accupy.svg?style=flat-square\u0026logo=github\u0026label=Stars\u0026logoColor=white)](https://github.com/nschloe/accupy)\n[![PyPi downloads](https://img.shields.io/pypi/dm/accupy.svg?style=flat-square)](https://pypistats.org/packages/accupy)\n\n[![Discord](https://img.shields.io/static/v1?logo=discord\u0026label=chat\u0026message=on%20discord\u0026color=7289da\u0026style=flat-square)](https://discord.gg/hnTJ5MRX2Y)\n\n[![gh-actions](https://img.shields.io/github/workflow/status/nschloe/accupy/ci?style=flat-square)](https://github.com/nschloe/accupy/actions?query=workflow%3Aci)\n[![codecov](https://img.shields.io/codecov/c/github/nschloe/accupy.svg?style=flat-square)](https://codecov.io/gh/nschloe/accupy)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square)](https://github.com/psf/black)\n\n### Sums\n\nSumming up values in a list can get tricky if the values are floating point\nnumbers; digit cancellation can occur and the result may come out wrong. A\nclassical example is the sum\n\n```\n1.0e16 + 1.0 - 1.0e16\n```\n\nThe actual result is `1.0`, but in double precision, this will result in `0.0`.\nWhile in this example the failure is quite obvious, it can get a lot more\ntricky than that. accupy provides\n\n```python\np, exact, cond = accupy.generate_ill_conditioned_sum(100, 1.0e20)\n```\n\nwhich, given a length and a target condition number, will produce an array of\nfloating point numbers that is hard to sum up.\n\nGiven one or two vectors, accupy can compute the condition of the sum or dot product via\n\n```python\naccupy.cond(x)\naccupy.cond(x, y)\n```\n\naccupy has the following methods for summation:\n\n- `accupy.kahan_sum(p)`: [Kahan\n  summation](https://en.wikipedia.org/wiki/Kahan_summation_algorithm)\n\n- `accupy.fsum(p)`: A vectorization wrapper around\n  [math.fsum](https://docs.python.org/3/library/math.html#math.fsum) (which\n  uses Shewchuck's algorithm [[1]](#references) (see also\n  [here](https://code.activestate.com/recipes/393090/))).\n\n- `accupy.ksum(p, K=2)`: Summation in K-fold precision (from [[2]](#references))\n\nAll summation methods sum the first dimension of a multidimensional NumPy array.\n\nLet's compare them.\n\n#### Accuracy comparison (sum)\n\n![](https://nschloe.github.io/accupy/accuracy-sum.svg)\n\nAs expected, the naive\n[sum](https://docs.python.org/3/library/functions.html#sum) performs very badly\nwith ill-conditioned sums; likewise for\n[`numpy.sum`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html)\nwhich uses pairwise summation. Kahan summation not significantly better; [this,\ntoo, is\nexpected](https://en.wikipedia.org/wiki/Kahan_summation_algorithm#Accuracy).\n\nComputing the sum with 2-fold accuracy in `accupy.ksum` gives the correct\nresult if the condition is at most in the range of machine precision; further\nincreasing `K` helps with worse conditions.\n\nShewchuck's algorithm in `math.fsum` always gives the correct result to full\nfloating point precision.\n\n#### Runtime comparison (sum)\n\n![](https://nschloe.github.io/accupy/speed-comparison1.svg)\n\n![](https://nschloe.github.io/accupy/speed-comparison2.svg)\n\nWe compare more and more sums of fixed size (above) and larger and larger sums,\nbut a fixed number of them (below). In both cases, the least accurate method is\nthe fastest (`numpy.sum`), and the most accurate the slowest (`accupy.fsum`).\n\n### Dot products\n\naccupy has the following methods for dot products:\n\n- `accupy.fdot(p)`: A transformation of the dot product of length _n_ into a\n  sum of length _2n_, computed with\n  [math.fsum](https://docs.python.org/3/library/math.html#math.fsum)\n\n- `accupy.kdot(p, K=2)`: Dot product in K-fold precision (from\n  [[2]](#references))\n\nLet's compare them.\n\n#### Accuracy comparison (dot)\n\naccupy can construct ill-conditioned dot products with\n\n```python\nx, y, exact, cond = accupy.generate_ill_conditioned_dot_product(100, 1.0e20)\n```\n\nWith this, the accuracy of the different methods is compared.\n\n![](https://nschloe.github.io/accupy/accuracy-dot.svg)\n\nAs for sums, `numpy.dot` is the least accurate, followed by instanced of `kdot`.\n`fdot` is provably accurate up into the last digit\n\n#### Runtime comparison (dot)\n\n![](https://nschloe.github.io/accupy/speed-comparison-dot1.svg)\n![](https://nschloe.github.io/accupy/speed-comparison-dot2.svg)\n\nNumPy's `numpy.dot` is _much_ faster than all alternatives provided by accupy.\nThis is because the bookkeeping of truncation errors takes more steps, but\nmostly because of NumPy's highly optimized dot implementation.\n\n### References\n\n1. [Richard Shewchuk, _Adaptive Precision Floating-Point Arithmetic and Fast\n   Robust Geometric Predicates_, J. Discrete Comput. Geom. (1997), 18(305),\n   305–363](https://doi.org/10.1007/PL00009321)\n\n2. [Takeshi Ogita, Siegfried M. Rump, and Shin'ichi Oishi, _Accurate Sum and Dot\n   Product_, SIAM J. Sci. Comput. (2006), 26(6), 1955–1988 (34\n   pages)](https://doi.org/10.1137/030601818)\n\n### Dependencies\n\naccupy needs the C++ [Eigen\nlibrary](http://eigen.tuxfamily.org/index.php?title=Main_Page), provided in\nDebian/Ubuntu by\n[`libeigen3-dev`](https://packages.ubuntu.com/search?keywords=libeigen3-dev).\n\n### Installation\n\naccupy is [available from the Python Package Index](https://pypi.org/project/accupy/), so with\n\n```\npip install accupy\n```\n\nyou can install.\n\n### Testing\n\nTo run the tests, just check out this repository and type\n\n```\nMPLBACKEND=Agg pytest\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsigma-py%2Faccupy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsigma-py%2Faccupy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsigma-py%2Faccupy/lists"}