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

Uploaded Source

Built Distributions

special_functions-0.2.5-cp311-cp311-win_amd64.whl (847.1 kB view details)

Uploaded CPython 3.11 Windows x86-64

special_functions-0.2.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

special_functions-0.2.5-cp311-cp311-macosx_10_9_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

special_functions-0.2.5-cp310-cp310-win_amd64.whl (846.4 kB view details)

Uploaded CPython 3.10 Windows x86-64

special_functions-0.2.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

special_functions-0.2.5-cp310-cp310-macosx_10_9_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

special_functions-0.2.5-cp39-cp39-win_amd64.whl (846.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

special_functions-0.2.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

special_functions-0.2.5-cp39-cp39-macosx_10_9_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for special_functions-0.2.5.tar.gz
Algorithm Hash digest
SHA256 67340a144161b8c50f49ce64ac4ff81d1f19f3ab09ea937ffc2bd9086111b980
MD5 1a7f9d23a22b22d759a70df3196f8109
BLAKE2b-256 b388fdef03655b31734fb4768ee8f6d784a090a0b62344a4a8cd7b198979c345

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 db9eceede946d045fcf9ddb704a9c22f6e9b3b0cb9f6f0f862b325e875d2d2ee
MD5 dd814d02f6209c859786d95223d89ebf
BLAKE2b-256 47ff6927d881086728505a86ca7bd2b1c7ea5b4effb5c657a3b9db2c31fa6836

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5182c7a2dc4a3de27d47b2f98b1a5f42d84935f06e8525d572ff61630e07c176
MD5 1b1ef1a4501a4c99bcd170a1e1c46450
BLAKE2b-256 43c17ed72d8a07962d6377cea3cd4c343770b3226c1d294240251bfbc8d78e40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c3370861ff0abd806bec7a10f82e9946067152e8f562478966560688c95e57cb
MD5 3a35e28b540562354e8d34d9426cef37
BLAKE2b-256 55cc6ced52f738f12498dca5f7e0278a2bef7dd0f7c7a0f3b6bc4457aaca4b8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1ae011cc929f19861e8409e1e69d08081f1663729687d93e465385c1f49fee32
MD5 ecbfe9c1498c5203a42752684a1e15a6
BLAKE2b-256 ef1fe6fc78374b7d59c3ebe62d4c79fc2d5c0d81ba2c2f91c91c83a3310d8085

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e9083a76d31ba9bcfb661761a6c7188d52a8c53173e117f65a41d2c758488e4b
MD5 6e6851abccc06a8d3d788120a478ae7f
BLAKE2b-256 335039691206335f9969517ddddb4d79a04cbacfe12eed6f366b746ea398682e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a3dcb103b10aef3026b13574548e9a21db8da32766e147521e7995f0c8edb046
MD5 5d0626c0cac1c79938b06583dd8e7f76
BLAKE2b-256 ccd621bc09ca0f515e199695fcf3bde7386f120b210f7919a6ca412eb9f365f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 904a06864b697b2336d22c1f9683d95dee42f642bbf23db12935028f45f07606
MD5 725fb52414804296cf772ed133daa314
BLAKE2b-256 4f7674d8470bbff8be74668f3cb1e045d431eb0e0c5acc86eadbd2dd8e5f507a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0d3c8036366d10e3b4c0a5b95e5ca5f5ff17008ffb04a235253217f0e91395d0
MD5 2830a50e92880062693d44fb11cd78e4
BLAKE2b-256 eb8e773d4b0de77d9358018c412cac93dcb44dece9e032c141e5fe28514ef1c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 481d0e98a9fe560598a2487d13e79a07300f07a0b323b80e17e8e2b5a29ea35a
MD5 67f6c7c65a3eaf0f586d7aee7641624f
BLAKE2b-256 25d64ba172a3362871f7719f45ea824c99e5b98721fb687b6be82a27ecb275ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d935c6289deeaa8adea052f6de88fd4e0877fd0d9e8b5062cac900cdeccaa9db
MD5 6af0ddc3cb3ea746fddb6b9c5d03be9d
BLAKE2b-256 34d8b0e6f353811c11cbff50b4671aed42350f3cd459cf51a45d2419397bbf88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.5-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a391b3b90af63352cf7e6361e8f64ea9bfa4821e3dea9fed9f0b068b80f06759
MD5 00b806b28ed4f65e40bf0386e3a9b4ac
BLAKE2b-256 b0b63ead4852fe1c93212be6864d02c1a1e1f74753d317bdfc502a7dc765b823

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