All Projects → nschloe → Accupy

nschloe / Accupy

Licence: gpl-3.0
Accurate sums and dot products for Python.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Accupy

Meshio
input/output for many mesh formats
Stars: ✭ 814 (+1152.31%)
Mutual labels:  engineering, mathematics, pypi
Pygmsh
Gmsh for Python
Stars: ✭ 418 (+543.08%)
Mutual labels:  engineering, mathematics, pypi
Pybotics
The Python Toolbox for Robotics
Stars: ✭ 192 (+195.38%)
Mutual labels:  numpy, mathematics, pypi
Optimesh
Mesh optimization, mesh smoothing.
Stars: ✭ 261 (+301.54%)
Mutual labels:  engineering, mathematics, pypi
Alyn
Detect and fix skew in images containing text
Stars: ✭ 202 (+210.77%)
Mutual labels:  numpy, pypi
Python Bigdata
Data science and Big Data with Python
Stars: ✭ 112 (+72.31%)
Mutual labels:  numpy, numerical-methods
Pygalmesh
A Python frontend to CGAL's mesh generators.
Stars: ✭ 245 (+276.92%)
Mutual labels:  engineering, mathematics
Python-Matematica
Explorando aspectos fundamentais da matemática com Python e Jupyter
Stars: ✭ 41 (-36.92%)
Mutual labels:  numpy, mathematics
Riemann book
An interactive book about the Riemann problem for hyperbolic PDEs, using Jupyter notebooks. Work in progress.
Stars: ✭ 160 (+146.15%)
Mutual labels:  mathematics, numerical-methods
meshgen-comparison
🕸️ A comparison of mesh generators.
Stars: ✭ 25 (-61.54%)
Mutual labels:  engineering, mathematics
Ai Learn
人工智能学习路线图,整理近200个实战案例与项目,免费提供配套教材,零基础入门,就业实战!包括:Python,数学,机器学习,数据分析,深度学习,计算机视觉,自然语言处理,PyTorch tensorflow machine-learning,deep-learning data-analysis data-mining mathematics data-science artificial-intelligence python tensorflow tensorflow2 caffe keras pytorch algorithm numpy pandas matplotlib seaborn nlp cv等热门领域
Stars: ✭ 4,387 (+6649.23%)
Mutual labels:  numpy, mathematics
Studybook
Study E-Book(ComputerVision DeepLearning MachineLearning Math NLP Python ReinforcementLearning)
Stars: ✭ 1,457 (+2141.54%)
Mutual labels:  numpy, mathematics
Stats Maths With Python
General statistics, mathematical programming, and numerical/scientific computing scripts and notebooks in Python
Stars: ✭ 381 (+486.15%)
Mutual labels:  numpy, mathematics
Quadpy
Numerical integration (quadrature, cubature) in Python
Stars: ✭ 471 (+624.62%)
Mutual labels:  engineering, mathematics
Orthopy
Orthogonal polynomials in all shapes and sizes.
Stars: ✭ 75 (+15.38%)
Mutual labels:  engineering, mathematics
Double pendulum
Animations of random double pendulums
Stars: ✭ 73 (+12.31%)
Mutual labels:  numpy, numerical-methods
Gonum
开源Go语言数值算法库(An open numerical library purely based on Go programming language)
Stars: ✭ 128 (+96.92%)
Mutual labels:  mathematics, numerical-methods
Polymath
Scientific Computing with Pharo
Stars: ✭ 135 (+107.69%)
Mutual labels:  mathematics, numerical-methods
Learn Something Every Day
📝 A compilation of everything that I learn; Computer Science, Software Development, Engineering, Math, and Coding in General. Read the rendered results here ->
Stars: ✭ 362 (+456.92%)
Mutual labels:  engineering, mathematics
Awesome Scientific Computing
😎 Curated list of awesome software for numerical analysis and scientific computing
Stars: ✭ 476 (+632.31%)
Mutual labels:  engineering, mathematics

accupy

Accurate sums and (dot) products for Python.

PyPi Version PyPI pyversions DOI GitHub stars PyPi downloads

gh-actions codecov Code style: black

Sums

Summing up values in a list can get tricky if the values are floating point numbers; digit cancellation can occur and the result may come out wrong. A classical example is the sum

1.0e16 + 1.0 - 1.0e16

The actual result is 1.0, but in double precision, this will result in 0.0. While in this example the failure is quite obvious, it can get a lot more tricky than that. accupy provides

p, exact, cond = accupy.generate_ill_conditioned_sum(100, 1.0e20)

which, given a length and a target condition number, will produce an array of floating point numbers that is hard to sum up.

accupy has the following methods for summation:

  • accupy.kahan_sum(p): Kahan summation

  • accupy.fsum(p): A vectorization wrapper around math.fsum (which uses Shewchuck's algorithm [1] (see also here)).

  • accupy.ksum(p, K=2): Summation in K-fold precision (from [2])

All summation methods sum the first dimension of a multidimensional NumPy array.

Let's compare them.

Accuracy comparison (sum)

As expected, the naive sum performs very badly with ill-conditioned sums; likewise for numpy.sum which uses pairwise summation. Kahan summation not significantly better; this, too, is expected.

Computing the sum with 2-fold accuracy in accupy.ksum gives the correct result if the condition is at most in the range of machine precision; further increasing K helps with worse conditions.

Shewchuck's algorithm in math.fsum always gives the correct result to full floating point precision.

Runtime comparison (sum)

We compare more and more sums of fixed size (above) and larger and larger sums, but a fixed number of them (below). In both cases, the least accurate method is the fastest (numpy.sum), and the most accurate the slowest (accupy.fsum).

Dot products

accupy has the following methods for dot products:

  • accupy.fdot(p): A transformation of the dot product of length n into a sum of length 2n, computed with math.fsum

  • accupy.kdot(p, K=2): Dot product in K-fold precision (from [2])

Let's compare them.

Accuracy comparison (dot)

accupy can construct ill-conditioned dot products with

x, y, exact, cond = accupy.generate_ill_conditioned_dot_product(100, 1.0e20)

With this, the accuracy of the different methods is compared.

As for sums, numpy.dot is the least accurate, followed by instanced of kdot. fdot is provably accurate up into the last digit

Runtime comparison (dot)

NumPy's numpy.dot is much faster than all alternatives provided by accupy. This is because the bookkeeping of truncation errors takes more steps, but mostly because of NumPy's highly optimized dot implementation.

References

  1. Richard Shewchuk, Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates, J. Discrete Comput. Geom. (1997), 18(305), 305–363

  2. Takeshi Ogita, Siegfried M. Rump, and Shin'ichi Oishi, Accurate Sum and Dot Product, SIAM J. Sci. Comput. (2006), 26(6), 1955–1988 (34 pages)

Dependencies

accupy needs the C++ Eigen library, provided in Debian/Ubuntu by libeigen3-dev.

Installation

accupy is available from the Python Package Index, so with

pip install accupy

you can install.

Testing

To run the tests, just check out this repository and type

MPLBACKEND=Agg pytest

License

accupy is published under the GPLv3+ license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].