This package provides both Python and Cython interfaces for Bessel functions and a few other special functions.
- 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.
Launch online interactive notebook with Binder.
Successful installation and tests have been performed on the following platforms and Python/PyPy versions shown in the table below.
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.
At runtime: This package does not have any dependencies at runtime.
For tests: To run Test,
scipy
package is required and can be installed bypython -m pip install -r tests/requirements.txt
Either Install from PyPi, Install from Anaconda Cloud, or Build and Install from Source Code.
The recommended installation method is through the package available at PyPi using pip
.
Ensure
pip
is installed within Python and upgrade the existingpip
bypython -m ensurepip python -m pip install --upgrade pip
If you are using PyPy instead of Python, ensure
pip
is installed and upgrade the existingpip
bypypy -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
Alternatively, the package can be installed through Anaconda could.
In Linux and Windows:
conda install s-ameli::special_functions
In macOS:
conda install -c s-ameli -c conda-forge special_functions
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, byapt
(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
To test package, install tox
:
python -m pip install tox
and test the package with
tox
Syntax | Symbol | User guide |
---|---|---|
besselj(nu, z, n) |
Bessel function of the first kind | |
bessely(nu, z, n) |
Bessel function of the second kind (Weber function) | |
besseli(nu, z, n) |
Modified Bessel function of the first kind | |
besselk(nu, z, n) |
Modified Bessel function of the second kind | |
besselh(nu, k, z, n) |
Bessel function of the third kind (Hankel function) | |
lngamma(x) |
Natural logarithm of Gamma function |
Typed Arguments:
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.
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.
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
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
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.
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
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
- scipy.special: Many special functions were implemented in Scipy's special sub-package. This package is reimplements Bessel functions similar to
scipy.special
, but with simplified python and cython different interfaces. - G-Learn: A python package for machine learning using Gaussian process regression. This package makes use of
special_functions
.
- National Science Foundation #1520825
- American Heart Association #18EIA33900046
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.