This is a pre-release version (2.5.0.dev0+git20251130.2de293a). Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy 2.5.0.dev0+git20251130.2de293a / docs

Doc

NumPy core math library

docs/reference:c-api:coremath

This library contains most math-related C99 functionality, which can be used on platforms where C99 is not well supported. The core math functions have the same API as the C99 ones, except for the npy_* prefix.

The available functions are defined in <numpy/npy_math.h> - please refer to this header when in doubt.

Floating point classification

Useful math constants

The following math constants are available in npy_math.h. Single and extended precision are also available by adding the f and l suffixes respectively.

Low-level floating point manipulation

Those can be useful for precise floating point comparison.

Support for complex numbers

C99-like complex functions have been added. Those can be used if you wish to implement portable C extensions. Since NumPy 2.0 we use C99 complex types as the underlying type:

typedef double _Complex npy_cdouble;
typedef float _Complex npy_cfloat;
typedef long double _Complex npy_clongdouble;

MSVC does not support the _Complex type itself, but has added support for the C99 complex.h header by providing its own implementation. Thus, under MSVC, the equivalent MSVC types will be used:

typedef _Dcomplex npy_cdouble;
typedef _Fcomplex npy_cfloat;
typedef _Lcomplex npy_clongdouble;

Because MSVC still does not support C99 syntax for initializing a complex number, you need to restrict to C90-compatible syntax, e.g.:

/* a = 1 + 2i \*/
npy_complex a = npy_cpack(1, 2);
npy_complex b;

b = npy_log(a);

A few utilities have also been added in numpy/npy_math.h, in order to retrieve or set the real or the imaginary part of a complex number:

npy_cdouble c;
npy_csetreal(&c, 1.0);
npy_csetimag(&c, 0.0);
printf("%d + %di\n", npy_creal(c), npy_cimag(c));

Linking against the core math library in an extension

To use the core math library that NumPy ships as a static library in your own Python extension, you need to add the npymath compile and link options to your extension. The exact steps to take will depend on the build system you are using. The generic steps to take are:

  • Add the numpy include directory (= the value of np.get_include()) to your include directories,

  • The npymath static library resides in the lib directory right next to numpy's include directory (i.e., pathlib.Path(np.get_include()) / '..' / 'lib'). Add that to your library search directories,

  • Link with libnpymath and libm.

When you build with numpy.distutils (deprecated), then use this in your setup.py:

>>> from numpy.distutils.misc_util import get_info
>>> info = get_info('npymath')
>>> _ = config.add_extension('foo', sources=['foo.c'], extra_info=info)

In other words, the usage of info is exactly the same as when using blas_info and co.

When you are building with Meson, use

# Note that this will get easier in the future, when Meson has
# support for numpy built in; most of this can then be replaced
# by `dependency('numpy')`.
incdir_numpy = run_command(py3,
  [
    '-c',
    'import os; os.chdir(".."); import numpy; print(numpy.get_include())'
  ],
  check: true
).stdout().strip()

inc_np = include_directories(incdir_numpy)

cc = meson.get_compiler('c')
npymath_path = incdir_numpy / '..' / 'lib'
npymath_lib = cc.find_library('npymath', dirs: npymath_path)

py3.extension_module('module_name',
  ...
  include_directories: inc_np,
  dependencies: [npymath_lib],

Half-precision functions

The header file <numpy/halffloat.h> provides functions to work with IEEE 754-2008 16-bit floating point values. While this format is not typically used for numerical computations, it is useful for storing values which require floating point but do not need much precision. It can also be used as an educational tool to understand the nature of floating point round-off error.

Like for other types, NumPy includes a typedef npy_half for the 16 bit float. Unlike for most of the other types, you cannot use this as a normal type in C, since it is a typedef for npy_uint16. For example, 1.0 looks like 0x3c00 to C, and if you do an equality comparison between the different signed zeros, you will get -0.0 != 0.0 (0x8000 != 0x0000), which is incorrect.

For these reasons, NumPy provides an API to work with npy_half values accessible by including <numpy/halffloat.h> and linking to npymath. For functions that are not provided directly, such as the arithmetic operations, the preferred method is to convert to float or double and back again, as in the following example.

npy_half sum(int n, npy_half *array) {
    float ret = 0;
    while(n--) {
        ret += npy_half_to_float(*array++);
    }
    return npy_float_to_half(ret);
}

External Links:

  • 754-2008 IEEE Standard for Floating-Point Arithmetic

  • Half-precision Float Wikipedia Article.

  • OpenGL Half Float Pixel Support

  • The OpenEXR image format.

target__ https://ieeexplore.ieee.org/document/4610935/
target__ https://en.wikipedia.org/wiki/Half-precision_floating-point_format
target__ https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_half_float_pixel.txt
target__ https://www.openexr.com/about.html