Cython and Python API for special functions.
Project description
This package provides both Python and Cython interfaces for Bessel functions and a few other special functions.
Features
Lightweight: This package requires no python dependency at runtime.
Cython interface: Both Python and Cython interfaces are available.
Releasing GIL: Most importantly, the functions can be used in with nogil: environment, which is essential in parallel OpenMP applications with Cython.
Tutorial
Launch online interactive notebook with Binder.
Links
Install
Supported Platforms
Successful installation and tests have been performed on the following platforms and Python/PyPy versions shown in the table below.
Platform |
Arch |
Python Version |
PyPy Version |
Continuous Integration |
|||||
---|---|---|---|---|---|---|---|---|---|
3.9 |
3.10 |
3.11 |
3.12 |
3.8 |
3.9 |
3.10 |
|||
Linux |
X86-64 |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
|
AARCH-64 * |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
||
macOS |
X86-64 |
✔ |
✔ |
✔ |
✔ |
✖ |
✖ |
✖ |
|
ARM-64 * |
✔ |
✔ |
✔ |
✔ |
✖ |
✖ |
✖ |
||
Windows |
X86-64 |
✔ |
✔ |
✔ |
✔ |
✖ |
✖ |
✖ |
|
ARM-64 * |
✔ |
✔ |
✔ |
✔ |
✖ |
✖ |
✖ |
Python wheels for special_functions for all supported platforms and versions in the above are available through PyPI and Anaconda Cloud. If you need special_functions on other platforms, architectures, and Python or PyPy versions, raise an issue on GitHub and we build its Python Wheel for you.
* Wheels compatible with the ARM-64 (also known as AARCH-64) architecture, including those for Apple Silicon M1 machines, are exclusively available for installation through pip and cannot be installed using conda.
Dependencies
At runtime: This package does not have any dependencies at runtime.
For tests: To run Test, scipy package is required and can be installed by
python -m pip install -r tests/requirements.txt
Install Package
Either Install from PyPi, Install from Anaconda Cloud, or Build and Install from Source Code.
Install from PyPi
The recommended installation method is through the package available at PyPi using pip.
Ensure pip is installed within Python and upgrade the existing pip by
python -m ensurepip python -m pip install --upgrade pip
If you are using PyPy instead of Python, ensure pip is installed and upgrade the existing pip by
pypy -m ensurepip pypy -m pip install --upgrade pip
Install this package in Python by
python -m pip install special_functions
or, in PyPy by
pypy -m pip install special_functions
Install from Anaconda Cloud
Alternatively, the package can be installed through Anaconda could.
In Linux and Windows:
conda install -c s-ameli special_functions
In macOS:
conda install -c s-ameli -c conda-forge special_functions
Build and Install from Source Code
Build dependencies: To build the package from the source code, numpy and cython are required. These dependencies are installed automatically during the build process and no action is needed.
Install both C and Fortran compilers as follows.
Linux: Install gcc, for instance, by apt (or any other package manager on your Linux distro)
sudo apt install gcc gfortran
macOS: Install gcc via Homebrew:
sudo brew install gcc
Note: If gcc is already installed, but Fortran compiler is yet not available on macOS, you may resolve this issue by reinstalling:
sudo brew reinstall gcc
Windows: Install both Microsoft Visual C++ compiler and Intel Fortran compiler (Intel oneAPI). Open the command prompt (where you will enter the installation commands in the next step) and load the Intel compiler variables by
C:\Program Files (x86)\Intel\oneAPI\setvars.bat
Here, we assumed the Intel Fortran compiler is installed in C:\Program Files (x86)\Intel\oneAPI. You may set this directory accordingly to the directory of your Intel compiler.
Clone the source code and install this package by
git clone https://github.com/ameli/special_functions.git cd special_functions python -m pip install .
Warning: After the package is built and installed from the source code, the package cannot be imported properly if the current working directory is the same as the source code directory. To properly import the package, change the current working directory to a directory anywhere else outside of the source code directory. For instance:
cd ..
python
>>> import special_functions
Test
To test package, install tox:
python -m pip install tox
and test the package with
tox
List of Functions
Python API
Syntax |
Symbol |
User guide |
---|---|---|
besselj(nu, z, n) |
||
bessely(nu, z, n) |
Bessel function of the second kind (Weber function) |
|
besseli(nu, z, n) |
||
besselk(nu, z, n) |
||
besselh(nu, k, z, n) |
Bessel function of the third kind (Hankel function) |
|
lngamma(x) |
Typed Arguments:
Argument |
Type |
Symbol |
Description |
---|---|---|---|
nu |
double |
Parameter of Bessel functions. |
|
k |
int |
Can be 1 or 2 and sets the type of Hankel function. |
|
z |
double, double complex |
Real or complex argument of the Bessel functions. |
|
x |
double |
Real argument of the functions. |
|
n |
int |
Order of derivative of function. Zero indicates no derivative. |
Cython API
In Cython interface, the syntax of the real functions are similar to the Python interface. However, the syntax of complex functions start with the letter c in the beginning of each function as shown in the table below.
Symbol |
Real Function |
Complex Function |
---|---|---|
besselj(nu, x, n) |
cbesselj(nu, z, n) |
|
bessely(nu, x, n) |
cbessely(nu, z, n) |
|
besseli(nu, x, n) |
cbesseli(nu, z, n) |
|
besselk(nu, x, n) |
cbesselk(nu, z, n) |
|
besselh(nu, k, x, n) |
cbesselh(nu, k, z, n) |
|
lngamma(x) |
N/A |
Examples
Using in Cython Code
The codes below should be used in a .pyx file and compiled with Cython.
As shown in the codes below, the python’s global lock interpreter, or gil, can be optionally released inside the scope of with nogil: statement. This is especially useful in parallel OpenMP environments.
Real Function
This example shows the real function besselk to compute the modified Bessel function of the second kind for a real argument z. The output variables d0k, d1k, and d2k represent the values of modified Bessel function and its first and second derivatives, respectively.
>>> # cimport module in a *.pyx file
>>> from special_functions cimport besselk
>>> # Declare typed variables
>>> cdef double nu = 2.5
>>> cdef double z = 2.0
>>> cdef double d0k, d1k, d2k
>>> # Releasing gil to secure maximum cythonic speedup
>>> with nogil:
... d0k = besselk(nu, z, 0) # no derivative
... d1k = besselk(nu, z, 1) # 1st derivative
... d2k = besselk(nu, z, 2) # 2nd derivative
Complex Function
The example below is similar to the above, except, the complex function cbesselk with complex argument z is used. The output variables d0k, d1k, and d2k are also complex.
>>> # cimport module in a *.pyx file
>>> from special_functions cimport cbesselk
>>> # Declare typed variables
>>> cdef double nu = 2.5
>>> cdef double complex z = 2.0 + 1.0j
>>> cdef double complex d0k, d1k, d2k
>>> # Releasing gil to secure maximum cythonic speedup
>>> with nogil:
... d0k = cbesselk(nu, z, 0) # no derivative
... d1k = cbesselk(nu, z, 1) # 1st derivative
... d2k = cbesselk(nu, z, 2) # 2nd derivative
Using in Python Code
The codes below should be used in a .py file and no compilation is required. The python’s global lock interpreter, or gil, cannot be released.
Real Function
The example below uses the function besselk with the real argument z to compute the modified Bessel function of the second kind and its first and second derivatives.
>>> # import module in a *.py file
>>> from special_functions import besselk
>>> nu = 2.5
>>> z = 2.0
>>> d0k = besselk(nu, z) # no derivative
>>> d1k = besselk(nu, z, 1) # 1st derivative
>>> d2k = besselk(nu, z, 2) # 2nd derivative
Complex Function
To use a complex input argument z in the Python interface, the same function besselk as the previous example can be used. This is unlike the Cython interface in which cbesselk should be used.
>>> # import module in a *.py file
>>> from special_functions import besselk
>>> nu = 2.5
>>> z = 2.0 + 1.0j
>>> d0k = besselk(nu, z) # no derivative
>>> d1k = besselk(nu, z, 1) # 1st derivative
>>> d2k = besselk(nu, z, 2) # 2nd derivative
Acknowledgements
National Science Foundation #1520825
American Heart Association #18EIA33900046
Citation
Credit
This package uses the following libraries:
Amos, D. E. (1986). Algorithm 644: A portable package for Bessel functions of a complex argument and nonnegative order. ACM Trans. Math. Softw. 12, 3 (Sept. 1986), 265-273. DOI: https://doi.org/10.1145/7921.214331. Available at http://netlib.org/amos/.
Moshier, S. L. (1989). C language library with special functions for mathematical physics. Available at http://www.netlib.org/cephes/index.html.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file special_functions-0.2.17-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: special_functions-0.2.17-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 951.5 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4c07c3c3b6a10c5ba0503cce10716c942e9bcadeae0880fc6bc34aea780b078 |
|
MD5 | b2610fb45295c67a09a23f2b9217bc27 |
|
BLAKE2b-256 | 83e1298cc2fb4316c89fb9a7330f9919638680f0c7c86ebf59ae03f40efa152f |
File details
Details for the file special_functions-0.2.17-cp312-cp312-macosx_11_0_arm64.whl
.
File metadata
- Download URL: special_functions-0.2.17-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c574ce8666e4e6c0073af3eb67f392db2560aebef45d924c6835e953fe63310 |
|
MD5 | 97c8667500dbd5d105837a2080232007 |
|
BLAKE2b-256 | 25c78b9d12cfa8e340fef6127ff0a375789679ade45344dd6d46369737ee4a9a |
File details
Details for the file special_functions-0.2.17-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: special_functions-0.2.17-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 948.9 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b329d5cde7f14e1e189feee38fa6e5603e30a12a9e09a829aea516cd646ebe1a |
|
MD5 | d16ff2b7f38a9819896f8db02c108114 |
|
BLAKE2b-256 | 13e6594b3c9a80485aa505c16d0fcddc895da3e941b6db906e411096562b4253 |
File details
Details for the file special_functions-0.2.17-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: special_functions-0.2.17-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd4501adcfe5f745a675bcd708084838ec01b2b7f6ffe7a3084e118b7f3c32d8 |
|
MD5 | 2dd27d4b1a4d9d3926de25dd9787e836 |
|
BLAKE2b-256 | debe94565d5ed8160e8428ada9a42dd12fad8a23f1f1021db25c90f3e637b616 |
File details
Details for the file special_functions-0.2.17-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: special_functions-0.2.17-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 949.5 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e89ed85ede04fe7902ff186b040e4bb11fc6bdfdbb96cc37730646e5713ee407 |
|
MD5 | 7c52ee16f04aeee523d802e29d36fb58 |
|
BLAKE2b-256 | 13a0ec18dd368b1998245ce3f0e9f74b08407c73170224156d6270d7899ec4a8 |
File details
Details for the file special_functions-0.2.17-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: special_functions-0.2.17-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e34c76ec68efeeeeb4f14fc666e046bc73c06dda04c47df8e7a2212cbb89685 |
|
MD5 | b8fc024197c7320807750926ec886cd0 |
|
BLAKE2b-256 | 66779f7d9e9e55d8629a40ae7be425e839c826c65d5dbd17397db10c4c7a0da8 |
File details
Details for the file special_functions-0.2.17-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: special_functions-0.2.17-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 949.1 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a789357375fb1cd3d18a926b2c3955489596eb715c8ac8f6c476a4efd21bb32 |
|
MD5 | fabeb94a50283c2af68d2b5aa6ea5332 |
|
BLAKE2b-256 | 907167f14d6ff11a0cd2bafaeaa71326cb03e2531a34318ba678b5f3bfb26130 |
File details
Details for the file special_functions-0.2.17-cp39-cp39-macosx_11_0_arm64.whl
.
File metadata
- Download URL: special_functions-0.2.17-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 827b7937994e48861ffd1347fbb943e9d1a2d1d3f1dc61db2e70a7b417d85320 |
|
MD5 | 337c5ed642a559e70c259a6fe284d99d |
|
BLAKE2b-256 | a357952421161509ddeb2d80d1295bd324244714bf14be1115b3f8e7713ec57f |