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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.11 Windows x86-64

special_functions-0.2.6-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.6-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.6-cp310-cp310-win_amd64.whl (846.4 kB view details)

Uploaded CPython 3.10 Windows x86-64

special_functions-0.2.6-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.6-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.6-cp39-cp39-win_amd64.whl (846.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

special_functions-0.2.6-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.6-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.6.tar.gz.

File metadata

  • Download URL: special_functions-0.2.6.tar.gz
  • Upload date:
  • Size: 381.3 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.6.tar.gz
Algorithm Hash digest
SHA256 adf3f2309cbb26637e4f713365963fd2a90fc38c157a90e2dea3448f0a20619f
MD5 4d412c22511656065a3074ab12f9e31d
BLAKE2b-256 e7927149ad546d68ed7e1ea828e4849f2fc707ae2ce4c47f1b377194cbd6150b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 43b2effbe088f3be27ec7fc6289c97ff24af14be89a4030ef89bee2670d051fe
MD5 285fd15ff01866c5fd1ef1cb07e31e81
BLAKE2b-256 78c0a64016f6e1eb7db6ad5cc2d317e1af16c3d2bd150b86b08be5a7e82dc9d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 19c963e77e22075d4f9a84242b59e7fd405453728ca6e1f948f0aadeccb3d646
MD5 4b84ab727f5594c603a6fb363705abc2
BLAKE2b-256 be30b162a109ae13a43e38cf0560d8efbba373ac7d1c21e5f0e4496c323e6f7e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 47ba05cbbb5c309d5cc3205f7f97f95ca68e5a3c2fda639de970759709a594c0
MD5 ca6096c4e753ca59c3577b811a354f59
BLAKE2b-256 11920eaf2e7bbafe5947829ff5a18db4119adb8c4188b1c49fcd68696869971f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3c9ac4989a83d523268398eb123e1a9f92972a0f90943076419f5edc624772f7
MD5 ff02f1c1e055e38c7d451e624798a8de
BLAKE2b-256 97acdcdc21ef10f50c1294719fae48a5a11ef5127d9c19179620d47dfa062eab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1519cf07e4f2a3c0fc3c6919756c42d40517c98f107b84437e700f859723a952
MD5 02836bba87efb624c44988e3f564d85b
BLAKE2b-256 72ddef1328a7183f3ef83628b976ddb2e00a8ad8ca629ec534ed5105b01a92ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1f525048dc24aa46665dd826820cd6ec351a1c149f9d941cfa963339cf9bd423
MD5 cb68603317781cabf1589dc2757df22e
BLAKE2b-256 bb21e00329bb61aeb6cf0b3c8fbd032de13369946511d8c2f58ce8bdf2894c92

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e7224512465f63b0d37f7fde2656eb3c2d5fdf8e0a414aebcf809113f7621a74
MD5 6f6ed612b26a1e76f4552b1762420eb2
BLAKE2b-256 95a96d6ae8beb79b23516cf09c2a1e93783d7b1a33b38e2470bac8b796a42b08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7367f87b07085385114be1ee97b85761627f83ffee33d6558bcbb8515b3ebe40
MD5 a24c30ebadf84de4c3af6cfec53388db
BLAKE2b-256 0d636f54281345802c544f98faed7f8fdabf6cb47f043e69900da45bcd6ddb02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 7a578c917f8298322b9ace5c0ac724945c6de76d05e63679a9d5fb0f4084a883
MD5 ad13029e3fc6ac131f7f99ac2549d928
BLAKE2b-256 c507c915191c3858fba3a344f63c7d57d7aaf705c9afc09b30cf0d190798138f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 691af59e3776f2d43a7299ed9831801f1cdb1eecdc10850701d8b42427ea7501
MD5 4bb16e2b2f2315159a48c76412754d5a
BLAKE2b-256 11e67abc3e7be74cfac49d9b8fe2dec6c0d44e7278536b5db151627d16d11eb0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for special_functions-0.2.6-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ad32c2646d28685d1a2f17889b7d195600a2aaac1e70f9fe5680dbf7d29d36e2
MD5 ebc88e24e2d5d795c7e28afa739a6665
BLAKE2b-256 ec16ad119dae21153e86a08fcc724d8cf4a92783942685ac388ff51f18b5b5ec

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