Skip to main content

Cython and Python API for special functions.

Project description

licence docs

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

binder

Launch online interactive notebook with Binder.

Install

Supported Platforms

Successful installation and tests have been performed on the following platforms and Python/PyPy versions shown in the table below.

Platform

Python version

PyPy version

Status

3.9

3.10

3.11

3.12

3.8

3.9

3.10

Linux

build-linux

macOS

build-macos

Windows

build-windows

  • For the Python/PyPy versions indicated by ✔ in the above, this package can be installed using either pip or conda (see Install Package below.)

  • This package cannot be installed via pip or conda on the Python/PyPy versions indicated by ✖ in the above table.

  • To install on the older Python 3 versions that are not listed in the above (for example Python 3.5), you should build this package from the source code (see Build and Install from Source Code).

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

pypi format implementation pyversions

The recommended installation method is through the package available at PyPi using pip.

  1. 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
  2. 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

conda-version conda-platform

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

release

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.

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

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

codecov-devel

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)

image06

Bessel function of the first kind

bessely(nu, z, n)

image07

Bessel function of the second kind (Weber function)

besseli(nu, z, n)

image08

Modified Bessel function of the first kind

besselk(nu, z, n)

image09

Modified Bessel function of the second kind

besselh(nu, k, z, n)

image10

Bessel function of the third kind (Hankel function)

lngamma(x)

image11

Natural logarithm of Gamma function

Typed Arguments:

Argument

Type

Symbol

Description

nu

double

image01

Parameter of Bessel functions.

k

int

image02

Can be 1 or 2 and sets the type of Hankel function.

z

double, double complex

image03

Real or complex argument of the Bessel functions.

x

double

image04

Real argument of the functions.

n

int

image05

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

image06

besselj(nu, x, n)

cbesselj(nu, z, n)

image07

bessely(nu, x, n)

cbessely(nu, z, n)

image08

besseli(nu, x, n)

cbesseli(nu, z, n)

image09

besselk(nu, x, n)

cbesselk(nu, z, n)

image10

besselh(nu, k, x, n)

cbesselh(nu, k, z, n)

image11

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

  • Ameli, S. (2022). ameli/special_functions: (v0.1.0). Zenodo. code-doi

Credit

This package uses the following libraries:

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

special_functions-0.2.8.tar.gz (388.6 kB view details)

Uploaded Source

Built Distributions

special_functions-0.2.8-cp312-cp312-win_amd64.whl (717.0 kB view details)

Uploaded CPython 3.12 Windows x86-64

special_functions-0.2.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

special_functions-0.2.8-cp312-cp312-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

special_functions-0.2.8-cp311-cp311-win_amd64.whl (713.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

special_functions-0.2.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

special_functions-0.2.8-cp311-cp311-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

special_functions-0.2.8-cp310-cp310-win_amd64.whl (712.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

special_functions-0.2.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

special_functions-0.2.8-cp310-cp310-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

special_functions-0.2.8-cp39-cp39-win_amd64.whl (712.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

special_functions-0.2.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

special_functions-0.2.8-cp39-cp39-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

File details

Details for the file special_functions-0.2.8.tar.gz.

File metadata

  • Download URL: special_functions-0.2.8.tar.gz
  • Upload date:
  • Size: 388.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for special_functions-0.2.8.tar.gz
Algorithm Hash digest
SHA256 2f89cd426d6bd639b1956599ef68f9b1a2cc71fcba9acca319c4a00be09b62ed
MD5 66a8767d56b6495bd74f78dbe1628968
BLAKE2b-256 67a4f0043d4c66bf6cf5958f97cced41e75041634a110afd80cedad83505e0c0

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6a7c67cb16f9a0c4e55184cc834f8c38cd6e29b538a1bb5b9def39f1174767e3
MD5 4e1ab828739bdc27ee0c9f4ebd88685b
BLAKE2b-256 6499920202e90c55abfac68f3c7316b9628e4e17b50c1e038af7121d78274f03

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cbb4d74532e858092c4a5c0ead02220861751df66dda1ec0b90757594ed0e91b
MD5 9a177a0eff4265391f0deeaf3dc8fb4f
BLAKE2b-256 63109b25f5bd55d7375bcfa6b03a3e23140aa276d7110269646f9d97c5506716

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a424eaf40e0466fd7f65d8d632017577804abdf42e881bd122ef10147e8d1a51
MD5 2bc65d4b5357d742ade864dab3d17fce
BLAKE2b-256 b171e584b93c3b9a47ba10c6461ddcab66b00946d9b1f68b1319cd8a0ad0db05

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4fb103e2a2737ef3b97796fed69ef60a88193b2887bc8623bfc95d2651c2c0c5
MD5 df18498f85c2e84309f1129ede1615a0
BLAKE2b-256 2d2ab2edc70f1d41eb0e214d91c43b45de32e0011a897fe2be36a69c85ac5d10

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 348d6a88a5b8014fb34f1481714f54aa1f71a3bac1317665e5d655d4e6653978
MD5 b95e254ec57c6a36958b0ed137ad12cd
BLAKE2b-256 ebdce813a9978a81cb3925891697cf79dfd3bb9632a235afce9c8a49cd1d4baa

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c0dd8fe1a48ff01a95e31028559cf10796b70deb53e4ad61c51db18a8cc4ff09
MD5 788423dfa9b002064f4efa35f21b27f0
BLAKE2b-256 e5876d8e4d7a023e1e876f8a19bbd560d7c2256c3a67fe923fa2dcdbfde9ee81

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 027d973a149145b26b469c6aef65ebb225e9bc1b0144495a92dd22402ac864b8
MD5 1b963c5148453c071c067477501ec360
BLAKE2b-256 7ee334f908421fa3a3a14b9b8c06b4f19487359faa2944102391b9babe92f024

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 39bd11666870034992e6448a67cb0ec62b0c9cdd908710f3cd617cb50dce0faa
MD5 5687c8660065f147def28990206b6109
BLAKE2b-256 8fb70d1e9273fbfdee30325d1777c8c779529c02dbe023a94eb4f2ae91121656

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ef1f0f028d1b55984c7c8fb61da90770ca50d093443395b45fd300ef3ecbc448
MD5 4aa4bed4a8de5d09bcc2f300666d5762
BLAKE2b-256 1f76d7820d37a4f015562de4ecb012463f5ac156f235d5d04aa11fcb70540ad8

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 dd6811940d817c460398a114360b276c1abc3653d72a7a373517b93f811eb595
MD5 43702e42fb3f9487c705cc8127f06fe7
BLAKE2b-256 92cdca74db1d9306297f7715c3eb877af7de2f7426e658255cc938cd4d2a0bb5

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c26d978485d66b354adb1c4a3d08d5e007dc8e1f72a88c1bfa4e1f28efe90576
MD5 d23f7c547f2a91cddb131e98ba2947cc
BLAKE2b-256 771b3534b4129c59b6b5b5ff7bad896020790b53a325d49b3bf8c8573f9e728e

See more details on using hashes here.

File details

Details for the file special_functions-0.2.8-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.8-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b4dc79f08d31feda41a3f18eaa1225528c4a52faf515946bf056821c63c304e0
MD5 bc1b3513824d09ea5b3dbcc6a0b18455
BLAKE2b-256 addb376555ab3f882a54ef2f16ccbe83adfe336718cba67494d7e0aaea2128aa

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