Skip to main content

Numexpr: Fast numerical expression evaluator for NumPy

Author:

David M. Cooke, Francesc Alted and others

Contact:

faltet@gmail.com

URL:

https://github.com/pydata/numexpr

Travis CI:

travis

Appveyor:

appveyor

PyPi:

version pypi

What it is Numexpr?

Numexpr is a fast numerical expression evaluator for NumPy. With it, expressions that operate on arrays (like “3*a+4*b”) are accelerated and use less memory than doing the same calculation in Python.

In addition, its multi-threaded capabilities can make use of all your cores – which may accelerate computations, most specially if they are not memory-bounded (e.g. those using transcendental functions).

Last but not least, numexpr can make use of Intel’s VML (Vector Math Library, normally integrated in its Math Kernel Library, or MKL). This allows further acceleration of transcendent expressions.

How Numexpr achieves high performance

The main reason why Numexpr achieves better performance than NumPy is that it avoids allocating memory for intermediate results. This results in better cache utilization and reduces memory access in general. Due to this, Numexpr works best with large arrays.

Numexpr parses expressions into its own op-codes that are then used by an integrated computing virtual machine. The array operands are split into small chunks that easily fit in the cache of the CPU and passed to the virtual machine. The virtual machine then applies the operations on each chunk. It’s worth noting that all temporaries and constants in the expression are also chunked.

The result is that Numexpr can get the most of your machine computing capabilities for array-wise computations. Common speed-ups with regard to NumPy are usually between 0.95x (for very simple expressions like ’a + 1’) and 4x (for relatively complex ones like ‘a*b-4.1*a > 2.5*b’), although much higher speed-ups can be achieved (up to 15x in some cases).

Numexpr performs best on matrices that do not fit in CPU cache. In order to get a better idea on the different speed-ups that can be achieved on your platform, run the provided benchmarks.

See more info about how Numexpr works in the wiki.

Examples of use

>>> import numpy as np
>>> import numexpr as ne

>>> a = np.arange(1e6)   # Choose large arrays for better speedups
>>> b = np.arange(1e6)

>>> ne.evaluate("a + 1")   # a simple expression
array([  1.00000000e+00,   2.00000000e+00,   3.00000000e+00, ...,
         9.99998000e+05,   9.99999000e+05,   1.00000000e+06])

>>> ne.evaluate('a*b-4.1*a > 2.5*b')   # a more complex one
array([False, False, False, ...,  True,  True,  True], dtype=bool)

>>> ne.evaluate("sin(a) + arcsinh(a/b)")   # you can also use functions
array([        NaN,  1.72284457,  1.79067101, ...,  1.09567006,
        0.17523598, -0.09597844])

>>> s = np.array(['abba', 'abbb', 'abbcdef'])
>>> ne.evaluate("'abba' == s")   # string arrays are supported too
array([ True, False, False], dtype=bool)

Datatypes supported internally

Numexpr operates internally only with the following types:

* 8-bit boolean (bool)
* 32-bit signed integer (int or int32)
* 64-bit signed integer (long or int64)
* 32-bit single-precision floating point number (float or float32)
* 64-bit, double-precision floating point number (double or float64)
* 2x64-bit, double-precision complex number (complex or complex128)
* Raw string of bytes (str)

If the arrays in the expression does not match any of these types, they will be upcasted to one of the above types (following the usual type inference rules, see below). Have this in mind when doing estimations about the memory consumption during the computation of your expressions.

Also, the types in Numexpr conditions are somewhat more restrictive than those of Python. For instance, the only valid constants for booleans are True and False, and they are never automatically cast to integers.

Casting rules

Casting rules in Numexpr follow closely those of NumPy. However, for implementation reasons, there are some known exceptions to this rule, namely:

* When an array with type `int8`, `uint8`, `int16` or `uint16` is
  used inside Numexpr, it is internally upcasted to an `int` (or
  `int32` in NumPy notation).

* When an array with type `uint32` is used inside Numexpr, it is
  internally upcasted to a `long` (or `int64` in NumPy notation).

* A floating point function (e.g. `sin`) acting on `int8` or
  `int16` types returns a `float64` type, instead of the `float32`
  that is returned by NumPy functions.  This is mainly due to the
  absence of native `int8` or `int16` types in Numexpr.

* In operations implying a scalar and an array, the normal rules
  of casting are used in Numexpr, in contrast with NumPy, where
  array types takes priority.  For example, if 'a' is an array of
  type `float32` and 'b' is an scalar of type `float64` (or Python
  `float` type, which is equivalent), then 'a*b' returns a
  `float64` in Numexpr, but a `float32` in NumPy (i.e. array
  operands take priority in determining the result type).  If you
  need to keep the result a `float32`, be sure you use a `float32`
  scalar too.

Supported operators

Numexpr supports the set of operators listed below:

* Logical operators: &, |, ~
* Comparison operators: <, <=, ==, !=, >=, >
* Unary arithmetic operators: -
* Binary arithmetic operators: +, -, *, /, **, %, <<, >>

Supported functions

Supported functions are listed below:

* where(bool, number1, number2): number
    Number1 if the bool condition is true, number2 otherwise.
* {sin,cos,tan}(float|complex): float|complex
    Trigonometric sine, cosine or tangent.
* {arcsin,arccos,arctan}(float|complex): float|complex
    Trigonometric inverse sine, cosine or tangent.
* arctan2(float1, float2): float
    Trigonometric inverse tangent of float1/float2.
* {sinh,cosh,tanh}(float|complex): float|complex
    Hyperbolic sine, cosine or tangent.
* {arcsinh,arccosh,arctanh}(float|complex): float|complex
    Hyperbolic inverse sine, cosine or tangent.
* {log,log10,log1p}(float|complex): float|complex
    Natural, base-10 and log(1+x) logarithms.
* {exp,expm1}(float|complex): float|complex
    Exponential and exponential minus one.
* sqrt(float|complex): float|complex
    Square root.
* abs(float|complex): float|complex
    Absolute value.
* conj(complex): complex
    Conjugate value.
* {real,imag}(complex): float
    Real or imaginary part of complex.
* complex(float, float): complex
    Complex from real and imaginary parts.
* contains(str, str): bool
    Returns True for every string in `op1` that contains `op2`.

You may add additional functions as needed.

Supported reduction operations

The following reduction operations are currently supported:

* sum(number, axis=None): Sum of array elements over a given axis.
  Negative axis are not supported.

* prod(number, axis=None): Product of array elements over a given
  axis.  Negative axis are not supported.

* min(number, axis=None): Minimum of array elements over a given
  axis.  Negative axis are not supported.

* max(number, axis=None): Maximum of array elements over a given
  axis.  Negative axis are not supported.

General routines

* evaluate(expression, local_dict=None, global_dict=None,
           out=None, order='K', casting='safe', **kwargs):
  Evaluate a simple array expression element-wise.  See docstrings
  for more info on parameters.  Also, see examples above.

* test():  Run all the tests in the test suite.

* print_versions():  Print the versions of software that numexpr
  relies on.

* set_num_threads(nthreads): Sets a number of threads to be used in
  operations.  Returns the previous setting for the number of
  threads.  During initialization time Numexpr sets this number to
  the number of detected cores in the system (see
  `detect_number_of_cores()`).

  If you are using Intel's VML, you may want to use
  `set_vml_num_threads(nthreads)` to perform the parallel job with
  VML instead.  However, you should get very similar performance
  with VML-optimized functions, and VML's parallelizer cannot deal
  with common expressions like `(x+1)*(x-2)`, while Numexpr's one
  can.

* detect_number_of_cores(): Detects the number of cores in the
  system.

Intel’s VML specific support routines

When compiled with Intel’s VML (Vector Math Library), you will be able to use some additional functions for controlling its use. These are outlined below:

* set_vml_accuracy_mode(mode):  Set the accuracy for VML operations.
The mode parameter can take the values:
  • ‘low’: Equivalent to VML_LA - low accuracy VML functions are called

  • ‘high’: Equivalent to VML_HA - high accuracy VML functions are called

  • ‘fast’: Equivalent to VML_EP - enhanced performance VML functions are called

It returns the previous mode.

This call is equivalent to the vmlSetMode() in the VML library.

* set_vml_num_threads(nthreads): Suggests a maximum number of
  threads to be used in VML operations.

This function is equivalent to the call mkl_domain_set_num_threads(nthreads, MKL_DOMAIN_VML) in the MKL library.

See the Intel documentation on VM Service Functions for more information.

  • get_vml_version(): Get the VML/MKL library version.

Authors

See AUTHORS.txt

License

Numexpr is distributed under the MIT license.

Metadata

Release files for numexpr 2.5.2

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

Source distribution (sdist)

Source distribution for numexpr 2.5.2
File Size Uploaded Provenance Actions
numexpr-2.5.2.tar.gz 90.1 kB Without provenance Details for numexpr-2.5.2.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for numexpr 2.5.2
File Actions
numexpr-2.5.2-cp35-none-win_amd64.whl CPython 3.5 none Windows x86-64 Details for numexpr-2.5.2-cp35-none-win_amd64.whl
numexpr-2.5.2-cp35-none-win32.whl CPython 3.5 none Windows x86-32 Details for numexpr-2.5.2-cp35-none-win32.whl
numexpr-2.5.2-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl CPython 3.5 CPython 3.5 pymalloc macOS 10.10+ Intel (x86-64, i386), macOS 10.6+ Intel (x86-64, i386), macOS 10.10+ x86-64, macOS 10.9+ Intel (x86-64, i386), macOS 10.9+ x86-64 Details for numexpr-2.5.2-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
numexpr-2.5.2-cp34-none-win_amd64.whl CPython 3.4 none Windows x86-64 Details for numexpr-2.5.2-cp34-none-win_amd64.whl
numexpr-2.5.2-cp34-none-win32.whl CPython 3.4 none Windows x86-32 Details for numexpr-2.5.2-cp34-none-win32.whl
numexpr-2.5.2-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl CPython 3.4 CPython 3.4 pymalloc macOS 10.10+ Intel (x86-64, i386), macOS 10.6+ Intel (x86-64, i386), macOS 10.10+ x86-64, macOS 10.9+ Intel (x86-64, i386), macOS 10.9+ x86-64 Details for numexpr-2.5.2-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
numexpr-2.5.2-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl CPython 3.3 CPython 3.3 pymalloc macOS 10.6+ Intel (x86-64, i386), macOS 10.9+ Intel (x86-64, i386), macOS 10.10+ x86-64, macOS 10.9+ x86-64, macOS 10.10+ Intel (x86-64, i386) Details for numexpr-2.5.2-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
numexpr-2.5.2-cp27-none-win_amd64.whl CPython 2.7 none Windows x86-64 Details for numexpr-2.5.2-cp27-none-win_amd64.whl
numexpr-2.5.2-cp27-none-win32.whl CPython 2.7 none Windows x86-32 Details for numexpr-2.5.2-cp27-none-win32.whl
numexpr-2.5.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl CPython 2.7 CPython 2.7 pymalloc macOS 10.9+ Intel (x86-64, i386), macOS 10.10+ x86-64, macOS 10.9+ x86-64, macOS 10.10+ Intel (x86-64, i386), macOS 10.6+ Intel (x86-64, i386) Details for numexpr-2.5.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl

Total release size: 1.2 MB

Release files / numexpr-2.5.2.tar.gz

Download URL numexpr-2.5.2.tar.gz
Size 90.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7e85595cf65c8c9a850fdcb073b46f29f52a9a8ab4d0e13161b73bee1229664d
BLAKE2b-256 checksum
How to use checksums
88e9df01e8e220a380780f55b74f0733f9d953200079b7cebe863b9756e43fe3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / numexpr-2.5.2-cp35-none-win_amd64.whl

Download URL numexpr-2.5.2-cp35-none-win_amd64.whl
Size 85.0 kB
Tags CPython 3.5 Windows x86-64
SHA-256 checksum
How to use checksums
dbea23dbd28d731f97423e93129da0e439c6c5c68513215c01e6de635b810354
BLAKE2b-256 checksum
How to use checksums
65d27a2968264c430dbd74e5cd9c2da09770d98b51d18f1d18ce40579b973275
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / numexpr-2.5.2-cp35-none-win32.whl

Download URL numexpr-2.5.2-cp35-none-win32.whl
Size 82.8 kB
Tags CPython 3.5 Windows x86-32
SHA-256 checksum
How to use checksums
fb4a625d1d55add22e00339fcfaedc307d3706518dd4713f4a812423e4482e62
BLAKE2b-256 checksum
How to use checksums
848603d95d9a2309072c501b04b43ce15a1bdedc06fd30768eb171b984854381
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / numexpr-2.5.2-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl

Download URL numexpr-2.5.2-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Size 138.0 kB
Tags CPython 3.5 CPython 3.5 pymalloc macOS 10.10+ Intel (x86-64, i386) macOS 10.10+ x86-64 macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
0343364173ae47b0e1f98890d5ed4c3a046a8eb96b9883aec626f0dffa382db6
BLAKE2b-256 checksum
How to use checksums
62172d48b1651572a394aa063eaf499fe4c7f84ed3bb82e451b45245b7cd054d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / numexpr-2.5.2-cp34-none-win_amd64.whl

Download URL numexpr-2.5.2-cp34-none-win_amd64.whl
Size 81.3 kB
Tags CPython 3.4 Windows x86-64
SHA-256 checksum
How to use checksums
a3f831a5e6abc25fc7079568879ac7d4e5a2f5e2ccce44a7ceca2e4871d56313
BLAKE2b-256 checksum
How to use checksums
8fb2d61e1d08734400f5ac66d304668765d820cc590fe5b62ce4e0097f511178
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / numexpr-2.5.2-cp34-none-win32.whl

Download URL numexpr-2.5.2-cp34-none-win32.whl
Size 82.3 kB
Tags CPython 3.4 Windows x86-32
SHA-256 checksum
How to use checksums
d26126a477369bdff0a0af33c429fbcbc763a73abbd406e5a276e6c453a5756f
BLAKE2b-256 checksum
How to use checksums
e9d0a414dea2db0dcfe6ca4e198cbfd2f42c19b99f6828177214b915d198b5bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / numexpr-2.5.2-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl

Download URL numexpr-2.5.2-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Size 138.0 kB
Tags CPython 3.4 CPython 3.4 pymalloc macOS 10.10+ Intel (x86-64, i386) macOS 10.10+ x86-64 macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
9bc60b669fcdb160f5b6e49dcc1908f4df31420f8c27b9e11062168314a4f51f
BLAKE2b-256 checksum
How to use checksums
446838b19940d6dd2e244ff88c61552002c9a0b76b244a2a3fb664a101847f5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / numexpr-2.5.2-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl

Download URL numexpr-2.5.2-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Size 138.0 kB
Tags CPython 3.3 CPython 3.3 pymalloc macOS 10.10+ Intel (x86-64, i386) macOS 10.10+ x86-64 macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
32907db208b0d1d4cd4ba03360b12aeab47a8326beb0322bb19bba34173b78ad
BLAKE2b-256 checksum
How to use checksums
6814bad9c9170644bf37fd81e0161be3e0a0ed19cad1784b09141637b057aee7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / numexpr-2.5.2-cp27-none-win_amd64.whl

Download URL numexpr-2.5.2-cp27-none-win_amd64.whl
Size 102.7 kB
Tags CPython 2.7 Windows x86-64
SHA-256 checksum
How to use checksums
81aa7305d33435afa6549c48b047e55accca07e245a7322b19df112100598845
BLAKE2b-256 checksum
How to use checksums
53f214eee55d9ccede95177494f25d135965f37cc3484969a7be5124bd465af4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / numexpr-2.5.2-cp27-none-win32.whl

Download URL numexpr-2.5.2-cp27-none-win32.whl
Size 106.5 kB
Tags CPython 2.7 Windows x86-32
SHA-256 checksum
How to use checksums
c8e3b424e235ad210288c4be7fb9aaa05928caee92e7e2aec400b384d18ab26a
BLAKE2b-256 checksum
How to use checksums
cf746dffa2bdec5a8336aa170344e236ad76d63e56ec4d2d441598b1085493c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / numexpr-2.5.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl

Download URL numexpr-2.5.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Size 137.6 kB
Tags CPython 2.7 CPython 2.7 pymalloc macOS 10.10+ Intel (x86-64, i386) macOS 10.10+ x86-64 macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
b5e0ea490d9a379ec4fdb3d10e506fde32e056073efc094351e09510cdb357d5
BLAKE2b-256 checksum
How to use checksums
46596d616a4483c0902464ed0d9e21e6360b878e50289836177e786afc88ce69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for numexpr 2.5.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.5.2 This release

11 release files No provenance

2.2

2.0

1.2

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