Metadata-Version: 2.4
Name: pylevin
Version: 2.0.1
Summary: Implementing the Levin method to calculate integrals over product of up to three Bessel functions
Home-page: https://github.com/rreischke/levin_bessel
Author: Robert Reischke
Author-email: reischke@posteo.net
Description-Content-Type: text/markdown
Dynamic: author
Dynamic: author-email
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: summary

This library implements Levin's method for integrals containing products of up to three spherical or cylindrical Bessel functions.

## Features
- Integrals over products of up to three spherical or cylindrical Bessel functions, for many integrands and arguments at once.
- [Fast repeated integrals](https://levin-bessel.readthedocs.io/en/latest/guides/running.html#repeated-integrals), as needed in inference: after the first two calls, every integral is a stored quadrature rule, and updating the integrand and integrating again takes a fraction of a millisecond for thousands of integrals.
- [Differentiable integrals with ``jax``](https://levin-bessel.readthedocs.io/en/latest/guides/running.html#exporting-the-quadrature-rules): the stored quadrature rules can be exported and evaluated in ``jax``, jit-compiled, differentiable and on a GPU, with the integrand evaluated directly at the nodes.

See the [release notes](https://github.com/rreischke/levin_bessel/releases) for what changed in each version.

## Installation
The installation has been tested on linux-based systems as well as Mac OS. For windows users, one would need to modify the ``setupy.py`` file accordingly. 
### With pip
The library is available via PyPi installation, so you can just install it using 
```shell
pip install pylevin
```
which installs the levin package and you are ready to go. Note that you need certain packages which you have to install via ``conda-forge``, in particular you will need the GSL, BOOST and GXX (on some linux servers) to be installed. You can grab them all via
```shell
conda install -c conda-forge gsl
conda install -c conda-forge gxx_linux-64
conda install conda-forge::boost
```
if they are not installed already.

### From source
Alternatively you first clone the directory via:
```shell
git clone git@github.com:rreischke/levin_bessel.git
```

Then navigate to the cloned directory
```shell
cd levin_bessel
conda env create -f conda_env.yaml
conda activate levin_env
pip install .
```
On some Linux servers you will have to install ``gxx_linux-64`` by hand and the installation will not work. This usually shows the following error message in the terminal:
``
gcc: fatal error: cannot execute 'cc1plus': execvp: No such file or directory
``
If this is the case just install it by typing
```shell
 conda install -c conda-forge gxx_linux-64
```
and redo the ``pip`` installation.

On Mac OS, the code is compiled with Apple clang and OpenMP (``libomp``). Recent versions of Apple clang need an OpenMP runtime of version 19 or newer; otherwise importing ``pylevin`` fails with ``symbol not found ... __kmpc_dispatch_deinit``. In this case update it and reinstall:
```shell
conda install -c conda-forge "llvm-openmp>=19"
```
When reinstalling, remove the ``build`` directory first, as ``pip`` otherwise reuses an extension built before, possibly for another environment. 

### Testing the installation
You can run ``pytest`` to test the installation.


## Video-Tutorial: Cosmology from Home 2025 recording
Feel free to watch my recording for Cosmology from Home 2025 on YouTube for a brief tutorial and a general explanation of the method and summary of the corresponding paper.
[![my recording for Cosmology from Home 2025 on youtube](./docs/levin_talk.png)](https://www.youtube.com/watch?v=RcZVc6emnQU&t=2s)


## Tutorials
The [usage guide](https://levin-bessel.readthedocs.io/en/latest/guides/running.html) on [levin-bessel.readthedocs.io](https://levin-bessel.readthedocs.io/en/latest/) is the best place to start, and the documentation there describes all methods. The notebooks in the ``tutorial`` directory contain the full examples:

- [``levin_tutorial.ipynb``](https://github.com/rreischke/levin_bessel/blob/main/tutorial/levin_tutorial.ipynb): integrals over one, two and three Bessel functions compared with quadrature, updating the integrand, diagonal mode and shared integrands, exporting the quadrature rules to ``numpy`` and ``jax``, and integrating many integrand sets at once.
- [``levin_cosmology_tutorial.ipynb``](https://github.com/rreischke/levin_bessel/blob/main/tutorial/levin_cosmology_tutorial.ipynb): repeated integrals across cosmologies. For the correlation function multipoles, ``update_integrand``, the exported rules and the batched matrix product are compared with new instances, ``hankl`` and the direct integral.
- [``levin_colab_gpu.ipynb``](https://github.com/rreischke/levin_bessel/blob/main/tutorial/levin_colab_gpu.ipynb) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/rreischke/levin_bessel/blob/main/tutorial/levin_colab_gpu.ipynb): ``jax`` on a GPU, with the comparisons to ``hankl`` and ``CCL`` from the paper, and the angular power spectra of many narrow top-hat shells (as needed by GLASS) across cosmologies.

## Contributing
If you want other types of oscillatory integrals implemented in ``pylevin``, I am very happy to help navigating the code. Note that these integrals need to satisfy some kind recursion relation for their derivatives (Eq. 9 in the arxiv version of the paper). This relation can technically be numerical but requires very stable derivatives.

## Reporting issues and support
If you encounter any issue or need help with the code you can either open an issue directly on github, or drop me a mail to reischke@posteo.net.


## Citing pylevin
If you use this code please cite the implementation described in [2502.12142](https://joss.theoj.org/papers/10.21105/joss.08618)
You can find references to the original method therein.

