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.10.tar.gz (389.2 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.12 Windows x86-64

special_functions-0.2.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

special_functions-0.2.10-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.10-cp311-cp311-win_amd64.whl (713.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

special_functions-0.2.10-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.10-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.10-cp310-cp310-win_amd64.whl (712.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

special_functions-0.2.10-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.10-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.10-cp39-cp39-win_amd64.whl (712.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

special_functions-0.2.10-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.10-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.10.tar.gz.

File metadata

  • Download URL: special_functions-0.2.10.tar.gz
  • Upload date:
  • Size: 389.2 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.10.tar.gz
Algorithm Hash digest
SHA256 35ec5ffd0b503597549a8fee4ee3a31830aff38a12a8eaf39cd65d35cb6d7d45
MD5 83e0f2439baf2db75b49c05f38f1eb47
BLAKE2b-256 ce9b09f197e664f73eac33679ce7721da526ced978560c8cc40961e428afe6ef

See more details on using hashes here.

File details

Details for the file special_functions-0.2.10-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.10-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9ed76b44057a8b06f84ee0cc559d0edb664801aed2a29b66c93c7d5e8e9f5635
MD5 55b4a71d1bf24aba9eba59cc0677da87
BLAKE2b-256 4023c8c4c50ae230b8d924b1c043371263cc0afe05f8a8d1b443ef7b7cf568e7

See more details on using hashes here.

File details

Details for the file special_functions-0.2.10-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.10-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5fa2097f752d980aece7ecb50f816ab63e1f5cf252f3280de79350e559daf730
MD5 99b7f2681a40c15dc63d3bf1c7664e77
BLAKE2b-256 1440d3c9bd82a71f0234e81cdee1fb3d11e85377301e7fe9a497de74fe58b948

See more details on using hashes here.

File details

Details for the file special_functions-0.2.10-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for special_functions-0.2.10-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5744d7938a98bcc0858b87a6cb82018cf1b00ab2ce5497d99d6954ed5ecd5760
MD5 a6ed5561e84cfe68e8cecae00b58b4df
BLAKE2b-256 0b6ab4bc1de30fb93291d674307980b4e33f00680786ed66caee539133180c37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e1f4b42de72b47fe2e0b03f4fb21cfec97e7faf4bf2a744fdb5c6d2021a7a3eb
MD5 f88df584201cf8b3ea9c743a5eed3451
BLAKE2b-256 fb43305848687199b4dcff8070b533e6b174d527e85dcd28757c81b473b1a31c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3ab97013f84048c1b504b9df1e8576b1460c1a5ce21087456c32d8ea7f03d0c3
MD5 9652912c4c8e458977ff04d8c852f67b
BLAKE2b-256 9f3b52d06eda04a21ba56d258dd276bd1c49ad618f3084c5715fc9974e15bdc0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 efead926ef0e6812d8829a1ac651cdb62bb9409900ba5cd47e350b2ba70010d5
MD5 31b15feb458b40727ab0331cd60f9e70
BLAKE2b-256 9323efc21bea864d3384ee372737e6e4dc453e67210ae1a855bb8ddc904ab930

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 992605a30225d7b7034ca94b8ce214570a68b5f8ae20d13fc4f0253ac8ac20fb
MD5 e5c5f2bbccf4e272626d9a8c5dc65ea5
BLAKE2b-256 7f73fd34ac28c8fc3b009526bf838dbf0900f53f43a7a94cc012c7bee454a62a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0bc54a894a3beab1ccdb14d1f7197d075b1c3cbc6523345d8756ee6346a997da
MD5 9119d4497855fe21161f348030ce9d58
BLAKE2b-256 d1e08b2d07db3bed477b9ebeed418f19fe5864f12e02a538785221eb6bb73691

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2f05a8e0fe414b72d7d5baeb95b93b466db77d06389d98abd9d0531e37ca89a5
MD5 2034f846b1d3ce184de588730b40d505
BLAKE2b-256 435b2a36c5c87a5796f501262543c5604d533b0f150741c1ba541cfa668757bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0bbeb67bd05b059ecbec27c6f448a9a4cd207d620cc061e93948cf62b1e4bb86
MD5 c021800b00ceae9bab7a3929165fff37
BLAKE2b-256 dfe40babcaef89d9747a2f7cdf1c5e62ddf881604450799f3b50bf0e9bfc96a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c6d2660821eee34ca8f623804880112b5a2e6d9d8ef2c809ec794783cd6c1303
MD5 10da636678ad9e4896f56381f25eb9f7
BLAKE2b-256 beaa0b0d4bf5c5fdcc2acb49bf06fcfcffcd327bcc41b4899c4f77909d15d2b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4b1dd22968a5147ccf47c666a9835f41f7f46b0ff54c78c8af6e621ffc2ad14b
MD5 96e78f49dfb16bae7f146f05077302aa
BLAKE2b-256 a416eb52a656da2c281ba85bbf96513dfc6db6fbd2f89b7ed6893bc824d7c9c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 82afb12e56f1f1e892419444227dd015f7a9bb5c28c04054d80693d3e3b047ae
MD5 b76176b64b4fa7870f7e8589e2382f33
BLAKE2b-256 2ecd82084389de7680d8712beeede888b905058c802152be1eac2a45a7245dc0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b36790d4d9585f415284f6d1a0f7cfd1c4d3238fe3b53e11f61a642e52bb5bc4
MD5 793653f8cf3495f62d9d77d1ce128f1a
BLAKE2b-256 728ee3abef28bbc689e7e34a24e681ebd76f1a0541b53dcb795d8807e4333079

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.10-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1b603bc260ab3f55af8d2c4706f131949e3713b959c2ecc3443ec1b80897d009
MD5 f98575d3a346792fb82aa19a95895114
BLAKE2b-256 2354d7ade89ed82bf37f25b963d6b2feccc81d770da3132612b3eeb3ff69fdd2

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