Skip to main content

Fast numerical expression evaluator for NumPy

Project description

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.

* re_evaluate(local_dict=None):
  Re-evaluate the previous executed array expression without any
  check.  This is meant for accelerating loops that are
  re-evaluating the same expression repeatedly without changing
  anything else than the operands.  If unsure, use evaluate() which
  is safer.

* 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numexpr-2.6.1.tar.gz (90.8 kB view details)

Uploaded Source

Built Distributions

numexpr-2.6.1-cp36-cp36m-manylinux1_x86_64.whl (388.3 kB view details)

Uploaded CPython 3.6m

numexpr-2.6.1-cp36-cp36m-manylinux1_i686.whl (384.7 kB view details)

Uploaded CPython 3.6m

numexpr-2.6.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (170.0 kB view details)

Uploaded CPython 3.6m macOS 10.10+ intel macOS 10.10+ x86-64 macOS 10.6+ intel macOS 10.9+ intel macOS 10.9+ x86-64

numexpr-2.6.1-cp35-cp35m-manylinux1_x86_64.whl (388.2 kB view details)

Uploaded CPython 3.5m

numexpr-2.6.1-cp35-cp35m-manylinux1_i686.whl (384.7 kB view details)

Uploaded CPython 3.5m

numexpr-2.6.1-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 (138.9 kB view details)

Uploaded CPython 3.5m macOS 10.10+ intel macOS 10.10+ x86-64 macOS 10.6+ intel macOS 10.9+ intel macOS 10.9+ x86-64

numexpr-2.6.1-cp34-cp34m-manylinux1_x86_64.whl (387.8 kB view details)

Uploaded CPython 3.4m

numexpr-2.6.1-cp34-cp34m-manylinux1_i686.whl (384.4 kB view details)

Uploaded CPython 3.4m

numexpr-2.6.1-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 (138.8 kB view details)

Uploaded CPython 3.4m macOS 10.10+ intel macOS 10.10+ x86-64 macOS 10.6+ intel macOS 10.9+ intel macOS 10.9+ x86-64

numexpr-2.6.1-cp27-cp27mu-manylinux1_x86_64.whl (361.9 kB view details)

Uploaded CPython 2.7mu

numexpr-2.6.1-cp27-cp27mu-manylinux1_i686.whl (370.3 kB view details)

Uploaded CPython 2.7mu

numexpr-2.6.1-cp27-cp27m-manylinux1_x86_64.whl (361.9 kB view details)

Uploaded CPython 2.7m

numexpr-2.6.1-cp27-cp27m-manylinux1_i686.whl (370.4 kB view details)

Uploaded CPython 2.7m

numexpr-2.6.1-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 (138.5 kB view details)

Uploaded CPython 2.7m macOS 10.10+ intel macOS 10.10+ x86-64 macOS 10.6+ intel macOS 10.9+ intel macOS 10.9+ x86-64

File details

Details for the file numexpr-2.6.1.tar.gz.

File metadata

  • Download URL: numexpr-2.6.1.tar.gz
  • Upload date:
  • Size: 90.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for numexpr-2.6.1.tar.gz
Algorithm Hash digest
SHA256 db2ee72f277b23c82d204189290ea4b792f9bd5b9d67744b045f8c2a8e929a06
MD5 6365245705b446426df9543ad218dd8e
BLAKE2b-256 c6f011628fa4d332d8fe9ab0ba8e9bfe0e065fb6b5324859171ee72d84e079c0

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 449d5a9e4fe464e08ab39ddaa23c0baafcfadef7bc4b5212acbe2c44c98611fc
MD5 77a5895a3ace76dfeeec8b739f44ae4d
BLAKE2b-256 fba9b028c841653aee5d90bd5eb901d7f50342eb8a5f5816f498de3c7a4a57a7

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6fbb38c928d4f734fbbeb60084134e8cb25eeec48ad791aeb1d2ab4c186ee8bf
MD5 876d717a5aa2b7809869226c014e94bd
BLAKE2b-256 22a46d7ba174b03815c9dcedbbf54cd7122aae57d23c9505205b35d5e3122e39

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 49bbf67ea4081bf49038c074f2525ca4629042ef3d406b51100d227864cf8fa8
MD5 4028d87b455dc8ecd871e6796f74f30c
BLAKE2b-256 1abb3139036553e93bbd17467aa536a6c8b4a12edd25913ec6f7f167c89e329e

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1edcbc7a35d6c62c871d5071a6d431567db57a83d79cfa6ace7a87f6c4b46379
MD5 08139c8696f7dfcac41693e5f4ddf30d
BLAKE2b-256 66295cdba68c2a0dd9de89422f9edbc903ec339bf2fb413034adc61f2f95264f

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 4dbe8c9699e24ce519b65b7a9e796c272d2b18c17ec397e1032644f1e68cdbdd
MD5 7cd7ca53b43059c779bbb4373f5bcb88
BLAKE2b-256 45c48f217fdbc612cc19ada9cd821934e27a844bc0f29cae142b5547dac9f2f3

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-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.

File metadata

File hashes

Hashes for numexpr-2.6.1-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
Algorithm Hash digest
SHA256 25fcdb166d6264ad42858c6b3391056fab68b50973cb2aa333701bda935c6ff7
MD5 d9fab43ab3a050b4eae19f05a32d7c60
BLAKE2b-256 961a6f3ab1e3a5ee3c39e62a68d133d5a8a953f260d7bbca934690b569e9dc2a

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a1fbfb13b5a38a601d7a086912672eabad474c3c33d68520f16e85de1436ad98
MD5 01e6abfe177a9cee376336790168e24d
BLAKE2b-256 7573b697e14391d1e77ef225bcf4c9052258fc3644fa8faee066c9b12a450290

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 9ce3c10363b8410719e6a3a9b501991d48df0c6ea2885ccecbcc8d5270d9eaba
MD5 1beccecf58389c8eb1ea1ec5b6ee8bc0
BLAKE2b-256 edea4353e060695f9414fc99176708127a17f7655222cd0b5e71b2b2e6bc2e51

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-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.

File metadata

File hashes

Hashes for numexpr-2.6.1-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
Algorithm Hash digest
SHA256 8d92b91591ddb46d619c140c68d1d6f81baaef17c88fcc3b03435d0ef2744537
MD5 893674d277f4a2b927f8b28071ac2542
BLAKE2b-256 b21ddb5ae05877386a091c8e505d7e8b7b9927d9c60c143cfd9445b13f678454

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fd17461a756b69d29be0ed1c1102bc579f3ff3f15c19d4a02f3ac3ca64ed0639
MD5 9559e8e65eda5607a834d2a42f2a0421
BLAKE2b-256 3a98b92e2361061465c6f893b074faba595a391811cd68cbfd69c56c80b59289

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp27-cp27mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 7aafbe04249c03b1b254bad3e493df972b3f0f37ed4a60076276c64395ed47ec
MD5 6115a6353f59306bcf6d7699e4d48c8f
BLAKE2b-256 a6df92e5d22fe34183b1bff6e4a3f2013a61d99e5ef0a4ed109c54f1f5e2d05e

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5243e68bb0ff5f5d14db10dd5161c83c3cc8298d9216bbcf4e636417da764be0
MD5 b30eb0ac94a633d75f0bd5c33c0345f2
BLAKE2b-256 26f2a482fc5f927599a2dcf577cbd9e9301307daf086e6ffb56308041aba1521

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-cp27-cp27m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for numexpr-2.6.1-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8dbc94f9e2f1158d615f6b05940675b4edba5ab0a3097e3ab3a561a0e62ec4f1
MD5 0f21eab0a5f9f75cad450ce0ec7a1d22
BLAKE2b-256 522d0bb315a92c65547a24f5c10fdcd58e4df1a7d05ece3e3f74b975e555dafe

See more details on using hashes here.

File details

Details for the file numexpr-2.6.1-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.

File metadata

File hashes

Hashes for numexpr-2.6.1-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
Algorithm Hash digest
SHA256 538b52892e1d42a84f2924edf37f9f8236e553623918805c36e00d7d31f2522a
MD5 03bef30d9d6ba134ebe0c7835e3546f4
BLAKE2b-256 a5992ce090eab61bbf95abd9fe37cfccf56a82de66cdd3949fe347e87af24831

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page