@@ -3,8 +3,6 @@ NumPy core libraries
3
3
4
4
.. sectionauthor :: David Cournapeau
5
5
6
- .. versionadded :: 1.3.0
7
-
8
6
Starting from numpy 1.3.0, we are working on separating the pure C,
9
7
"computational" code from the python dependent code. The goal is twofolds:
10
8
making the code cleaner, and enabling code reuse by other extensions outside
@@ -16,10 +14,19 @@ NumPy core math library
16
14
The numpy core math library ('npymath') is a first step in this direction. This
17
15
library contains most math-related C99 functionality, which can be used on
18
16
platforms where C99 is not well supported. The core math functions have the
19
- same API as the C99 ones, except for the npy_* prefix.
17
+ same API as the C99 ones, except for the ``npy_* `` prefix.
18
+
19
+ The available functions are defined in ``<numpy/npy_math.h> `` - please refer to
20
+ this header when in doubt.
21
+
22
+ .. note ::
20
23
21
- The available functions are defined in <numpy/npy_math.h> - please refer to this header when
22
- in doubt.
24
+ An effort is underway to make ``npymath `` smaller (since C99 compatibility
25
+ of compilers has improved over time) and more easily vendorable or usable as
26
+ a header-only dependency. That will avoid problems with shipping a static
27
+ library built with a compiler which may not match the compiler used by a
28
+ downstream package or end user. See
29
+ `gh-20880 <https://github.com/numpy/numpy/issues/20880 >`__ for details.
23
30
24
31
Floating point classification
25
32
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -75,8 +82,6 @@ Floating point classification
75
82
as y. Works for any value, including inf and nan. Single and extended
76
83
precisions are available with suffix f and l.
77
84
78
- .. versionadded :: 1.4.0
79
-
80
85
Useful math constants
81
86
~~~~~~~~~~~~~~~~~~~~~
82
87
@@ -140,41 +145,29 @@ Those can be useful for precise floating point comparison.
140
145
floating point value from x in the direction of y. Single and extended
141
146
precisions are available with suffix f and l.
142
147
143
- .. versionadded:: 1.4.0
144
-
145
148
.. c:function:: double npy_spacing(double x)
146
149
147
150
This is a function equivalent to Fortran intrinsic. Return distance between
148
151
x and next representable floating point value from x, e.g. spacing(1) ==
149
152
eps. spacing of nan and +/- inf return nan. Single and extended precisions
150
153
are available with suffix f and l.
151
154
152
- .. versionadded:: 1.4.0
153
-
154
155
.. c:function:: void npy_set_floatstatus_divbyzero()
155
156
156
157
Set the divide by zero floating point exception
157
158
158
- .. versionadded:: 1.6.0
159
-
160
159
.. c:function:: void npy_set_floatstatus_overflow()
161
160
162
161
Set the overflow floating point exception
163
162
164
- .. versionadded:: 1.6.0
165
-
166
163
.. c:function:: void npy_set_floatstatus_underflow()
167
164
168
165
Set the underflow floating point exception
169
166
170
- .. versionadded:: 1.6.0
171
-
172
167
.. c:function:: void npy_set_floatstatus_invalid()
173
168
174
169
Set the invalid floating point exception
175
170
176
- .. versionadded:: 1.6.0
177
-
178
171
.. c:function:: int npy_get_floatstatus()
179
172
180
173
Get floating point status. Returns a bitmask with following possible flags:
@@ -188,8 +181,6 @@ Those can be useful for precise floating point comparison.
188
181
aggressive compiler optimizations reordering the call relative to
189
182
the code setting the status, which could lead to incorrect results.
190
183
191
- .. versionadded:: 1.9.0
192
-
193
184
.. c:function:: int npy_get_floatstatus_barrier(char*)
194
185
195
186
Get floating point status. A pointer to a local variable is passed in to
@@ -214,8 +205,6 @@ Those can be useful for precise floating point comparison.
214
205
prevents aggressive compiler optimizations reordering the call relative to
215
206
the code setting the status, which could lead to incorrect results.
216
207
217
- .. versionadded:: 1.9.0
218
-
219
208
.. c:function:: int npy_clear_floatstatus_barrier(char*)
220
209
221
210
Clears the floating point status. A pointer to a local variable is passed in to
@@ -227,11 +216,10 @@ Those can be useful for precise floating point comparison.
227
216
Complex functions
228
217
~~~~~~~~~~~~~~~~~
229
218
230
- .. versionadded:: 1.4.0
231
-
232
219
C99-like complex functions have been added. Those can be used if you wish to
233
220
implement portable C extensions. Since we still support platforms without C99
234
- complex type, you need to restrict to C90-compatible syntax, e.g.:
221
+ complex type (most importantly Windows, where MSVC doesn't support C99 complex
222
+ types as of Nov 2022), you need to restrict to C90-compatible syntax, e.g.:
235
223
236
224
.. code-block:: c
237
225
@@ -241,13 +229,31 @@ complex type, you need to restrict to C90-compatible syntax, e.g.:
241
229
242
230
b = npy_log(a);
243
231
232
+ .. _linking-npymath :
233
+
244
234
Linking against the core math library in an extension
245
235
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
246
236
247
- .. versionadded :: 1.4.0
237
+ To use the core math library that NumPy ships as a static library in your own
238
+ Python extension, you need to add the npymath compile and link options to your
239
+ extension. The exact steps to take will depend on the build system you are using.
240
+ The generic steps to take are:
241
+
242
+ 1. Add the numpy include directory (= the value of ``np.get_include() ``) to
243
+ your include directories,
244
+ 2. The ``npymath `` static library resides in the ``lib `` directory right next
245
+ to numpy's include directory (i.e., ``pathlib.Path(np.get_include()) / '..'
246
+ / 'lib' ``). Add that to your library search directories,
247
+ 3. Link with ``libnpymath `` and ``libm ``.
248
248
249
- To use the core math library in your own extension, you need to add the npymath
250
- compile and link options to your extension in your setup.py:
249
+ .. note ::
250
+
251
+ Keep in mind that when you are cross compiling, you must use the ``numpy ``
252
+ for the platform you are building for, not the native one for the build
253
+ machine. Otherwise you pick up a static library built for the wrong
254
+ architecture.
255
+
256
+ When you build with ``numpy.distutils `` (deprecated), then use this in your ``setup.py ``:
251
257
252
258
.. hidden in a comment so as to be included in refguide but not rendered documentation
253
259
>>> import numpy.distutils.misc_util
@@ -258,15 +264,37 @@ compile and link options to your extension in your setup.py:
258
264
>>> info = get_info(' npymath' )
259
265
>>> _ = config.add_extension(' foo' , sources = [' foo.c' ], extra_info = info)
260
266
261
- In other words, the usage of info is exactly the same as when using blas_info
262
- and co.
267
+ In other words, the usage of ``info `` is exactly the same as when using
268
+ ``blas_info `` and co.
269
+
270
+ When you are building with `Meson <https://mesonbuild.com >`__, use::
271
+
272
+ # Note that this will get easier in the future, when Meson has
273
+ # support for numpy built in; most of this can then be replaced
274
+ # by `dependency('numpy')`.
275
+ incdir_numpy = run_command(py3,
276
+ [
277
+ '-c',
278
+ 'import os; os.chdir(".."); import numpy; print(numpy.get_include())'
279
+ ],
280
+ check: true
281
+ ).stdout().strip()
282
+
283
+ inc_np = include_directories(incdir_numpy)
284
+
285
+ cc = meson.get_compiler('c')
286
+ npymath_path = incdir_numpy / '..' / 'lib'
287
+ npymath_lib = cc.find_library('npymath', dirs: npymath_path)
288
+
289
+ py3.extension_module('module_name',
290
+ ...
291
+ include_directories: inc_np,
292
+ dependencies: [npymath_lib],
263
293
264
294
Half-precision functions
265
295
~~~~~~~~~~~~~~~~~~~~~~~~
266
296
267
- .. versionadded :: 1.6.0
268
-
269
- The header file <numpy/halffloat.h> provides functions to work with
297
+ The header file ``<numpy/halffloat.h> `` provides functions to work with
270
298
IEEE 754-2008 16-bit floating point values. While this format is
271
299
not typically used for numerical computations, it is useful for
272
300
storing values which require floating point but do not need much precision.
@@ -281,7 +309,7 @@ between the different signed zeros, you will get -0.0 != 0.0
281
309
(0x8000 != 0x0000), which is incorrect.
282
310
283
311
For these reasons, NumPy provides an API to work with npy_half values
284
- accessible by including <numpy/halffloat.h> and linking to ' npymath' .
312
+ accessible by including `` <numpy/halffloat.h> `` and linking to `` npymath `` .
285
313
For functions that are not provided directly, such as the arithmetic
286
314
operations, the preferred method is to convert to float
287
315
or double and back again, as in the following example.
0 commit comments