Skip to main content

approxmath is a Python library of fast, approximate math functions for operations on NumPy arrays. On average it is 4-5x faster than NumPy and has drop-in replacement functions for log, exp, cos, and sin.

The approximations were originally made by Jacques-Henri Jourdan and are explained on Cambium’s blog.

Performance & Accuracy

approxmath vs NumPy performance

approxmath vs NumPy performance

If your program doesn’t depend on the full precision of floating point operations, the performance-accuracy trade-off will probably be worth it, increasing FLOPS throughput 4-5x. However, in some instances numerical instability will result: for example, approxmath.log(1.0) is nearly, but not exactly 0.0.

Function

Relative Accuracy

ops / second

exp

3e-9

647 M/sec

log

5e-9

896 M/sec

cos

2e-10

470 M/sec

sin

2e-9

463 M/sec

Benchmarks were run on a 2019 MacBook Pro (1.4 GHz Quad-Core Intel Core i5), with Python 3.9.9 and NumPy 1.21.4.

Drop-in Replacement for NumPy

import approxmath.np as npa
import numpy as np

npa.sin(np.array([-np.pi/2, 0.0, np.pi/2]))
# array([-1.,  0.,  1.])

npa.log(np.array([1.0, np.e, np.e**2]))
# array([4.53119942e-09, 1.00000000e+00, 2.00000000e+00])

npa.cos(np.array([-2*np.pi/3, 0.0, 2*np.pi/3]))
# array([-0.5,  1. , -0.5])

npa.exp(np.array([-1., 0., 1.]))
# array([0.36787944, 1.        , 2.71828183])

Drop-in Replacement for Aesara Tensor

import approxmath.aesara as att
import aesara.tensor as tt

Functional equivalents:

Aesara

Approxmath

Relative Speed-Up

tt.exp

att.exp

15x

tt.log

att.log

15x

tt.cos

att.cos

13x

tt.sin

att.sin

13x

Installation

$ pip install approxmath

approxmath is released under the MIT license.

Metadata

Release files for approxmath 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for approxmath 2.0.0
File Size Uploaded
approxmath-2.0.0.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for approxmath 2.0.0
File Interpreter ABI Platform
approxmath-2.0.0-py3.9-macosx-10.15-x86_64.egg Legacy Egg format - - Details

Total release size: 17.3 kB

Release files / approxmath-2.0.0.tar.gz

Download URL approxmath-2.0.0.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9aa8fe3b5aa62938a95c35a7b2897b0a56d4f60af0fcfed492e28f78fef19a99
BLAKE2b-256 checksum
How to use checksums
965ef2ac2595876f166ff73e129bdff941de0e3e73c37d3bd399a16ec3fa4172
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.9

Release files / approxmath-2.0.0-py3.9-macosx-10.15-x86_64.egg

Download URL approxmath-2.0.0-py3.9-macosx-10.15-x86_64.egg
Size 11.4 kB
Tags Egg
SHA-256 checksum
How to use checksums
7d384fd6da17bb320231c289db6c5ef03c9ce3b1ec9bc77a8096339fa5a4f93e
BLAKE2b-256 checksum
How to use checksums
d00df29cbbf1d769f53cabb30821287be6c19ae0ffdace193678471f850e37eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.9

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page