lintegrate

lintegrate: A C/Python numerical integration library for working in log-space - Published in JOSS (2022)

https://github.com/mattpitkin/lintegrate

Science Score: 95.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 8 DOI reference(s) in README and JOSS metadata
  • Academic publication links
    Links to: joss.theoj.org
  • Committers with academic emails
    2 of 4 committers (50.0%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
    Published in Journal of Open Source Software

Keywords

gsl gsl-functions integral python wrapper

Keywords from Contributors

pulsar mesh
Last synced: 6 months ago · JSON representation

Repository

A numerical integration routine that works for the natural logarithm of functions

Basic Info
  • Host: GitHub
  • Owner: mattpitkin
  • License: gpl-3.0
  • Language: C
  • Default Branch: master
  • Size: 494 KB
Statistics
  • Stars: 12
  • Watchers: 3
  • Forks: 3
  • Open Issues: 0
  • Releases: 16
Topics
gsl gsl-functions integral python wrapper
Created over 8 years ago · Last pushed over 1 year ago
Metadata Files
Readme Contributing License

README.md

lintegrate

A numerical integration library for when you want/need to work with the natural logarithm of the function requiring integration.

This library provides three numerical integration functions, heavily based on GSL functions, to integrate a function when only its natural logarithm is given, and return the natural logarithm of that integral. The three functions:

  • lintegrate_qag
  • lintegrate_qng
  • lintegrate_cquad

are equivalents of the GSL functions:

respectively. These can be useful when, e.g., you can calculate the natural logarithm of a Gaussian likelihood function (in cases where the exponentiation of the Gaussian function would lead to zeros or infinities) and you want to numerically find the integral of the Gaussian function itself.

The functions lintegrate_qag, lintegrate_qng, and lintegrate_cquad, all have wrappers functions (with _split appended to their names) that allow the user to specify a set of intervals that the integrals will be split into when performing the calculation. The intervals could, for example, be spaced evenly in log-space, for cases where the integral function has a very pronounced peak as it approaches zero.

The full API documentation and examples can be found here.

Example

An example of the use the functions is:

```C /* example using lintegrate functionality */

include

include

include

include

include

include

/* create function for integration */ double lintegrand(double x, void *params);

struct intparams { double mu; double sig; };

double lintegrand(double x, void *params){ struct intparams * p = (struct intparams *)params; double mu = p->mu; double sig = p->sig;

return -0.5(mu-x)(mu-x)/(sig*sig); }

double integrand(double x, void *params){ struct intparams * p = (struct intparams *)params; double mu = p->mu; double sig = p->sig;

return exp(-0.5(mu-x)(mu-x)/(sig*sig)); }

int main( int argv, char **argc ){ gslfunction F; struct intparams params; gslintegrationworkspace *w = gslintegrationworkspacealloc (100); gslintegrationcquadworkspace *cw = gslintegrationcquadworkspacealloc(50); double qaganswer = 0., qnganswer = 0., cquadanswer = 0., answer = 0.; double abserr = 0.; sizet neval = 0;

double minlim = -6.; /* minimum for integration range / double maxlim = 6.; / maximum for integration range */

double abstol = 1e-10; /* absolute tolerance / double reltol = 1e-10; / relative tolerance */

params.mu = 0.; params.sig = 1.;

F.function = &lintegrand; F.params = ¶ms;

/* integrate log of function using QAG */ lintegrationqag(&F, minlim, maxlim, abstol, reltol, 100, GSLINTEG_GAUSS31, w, &qaganswer, &abserr);

/* integrate log of function using QNG */ lintegration_qng(&F, minlim, maxlim, abstol, reltol, &qnganswer, &abserr, &neval);

/* integrate log of function using CQUAD */ lintegration_cquad(&F, minlim, maxlim, abstol, reltol, cw, &cquadanswer, &abserr, &neval);

/* integrate function using GSL QAG */ F.function = &integrand; gslintegrationqag(&F, minlim, maxlim, abstol, reltol, 100, GSLINTEGGAUSS31, w, &answer, &abserr);

gslintegrationworkspacefree(w); gslintegrationcquadworkspace_free(cw);

fprintf(stdout, "Answer \"lintegrate QAG\" = %.8lf\n", qaganswer); fprintf(stdout, "Answer \"lintegrate QNG\" = %.8lf\n", qnganswer); fprintf(stdout, "Answer \"lintegrate CQUAD\" = %.8lf\n", cquadanswer); fprintf(stdout, "Answer \"gslintegrateqag\" = %.8lf\n", log(answer)); fprintf(stdout, "Analytical answer = %.8lf\n", log(sqrt(2.*M_PI)));

return 0; } ```

Requirements

  • GSL - on Debian/Ubuntu (16.04) install with e.g. sudo apt-get install libgsl-dev

Installation

The library can be built using scons by just typing sudo scons in the base directory. To install the library system-wide (in /usr/local/lib by default) run: sudo scons sudo scons install

A Python module containing wrappers to the functions can be built and installed from source for the user by running, e.g.: bash pip install . from within the repository directory.

The Python module can also be installed from PyPI using pip with: bash pip install lintegrate

or in a Conda environment with: bash conda install -c conda-forge lintegrate

Python

If the Python module has been installed it has the following functions: * lqng - a wrapper to lintegration_qng * lqag - a wrapper to lintegration_qag * lcquad - a wrapper to lintegration_cquad * logtrapz - using the trapezium rule for integration on a grid of values

The lqng, lqag, and lcquad functions are used in a similar way to the scipy quad function.

An example of their use would be:

```python from lintegrate import lqag, lqng, lcquad, logtrapz import numpy as np

define the log of the function to be integrated

def integrand(x, args): mu, sig = args # unpack extra arguments return -0.5((x-mu)/sig)*2

set integration limits

xmin = -6. xmax = 6.

set additional arguments

mu = 0. sig = 1.

resqag = lqag(integrand, xmin, xmax, args=(mu, sig)) resqng = lqng(integrand, xmin, xmax, args=(mu, sig)) rescquad = lcquad(integrand, xmin, xmax, args=(mu, sig)) restrapz = logtrapz(integrand, np.linspace(xmin, xmax, 100), args=(mu, sig)) ```

R

In R one can use the reticulate package to call the functions in lintegrate. The above example would be: R library(reticulate) py_install("lintegrate", pip = TRUE) ## run once to make sure lintegrate is installed and visible to reticulate. lint <- import("lintegrate", convert = FALSE) integrand <- function(x, args){ mu = args[1] sig = args[2] return(-.5 * ((x-mu)/sig)^2 ) } integrand <- Vectorize(integrand) mu <- 0 sig <- 1 mmin <- -10 mmax <- 10 lint$lqag(py_func(integrand), r_to_py(mmin), r_to_py(mmax), c(mu, sig))

Citation

If using lintegrate in your research, I would be grateful if you cite the associated JOSS paper for the software. The following BibTeX citation can be used:

bibtex @article{Pitkin2022, doi = {10.21105/joss.04231}, url = {https://doi.org/10.21105/joss.04231}, year = {2022}, publisher = {The Open Journal}, volume = {7}, number = {73}, pages = {4231}, author = {Matthew Pitkin}, title = {lintegrate: A C/Python numerical integration library for working in log-space}, journal = {Journal of Open Source Software} }

You may also want to cite the GSL reference "M. Galassi et al, GNU Scientific Library Reference Manual (3rd Ed.), ISBN 0954612078" and the URL http://www.gnu.org/software/gsl/.

DOI Build Status PyPI version Anaconda-Server Badge Documentation Status

© 2017 Matthew Pitkin

Owner

  • Name: Matt Pitkin
  • Login: mattpitkin
  • Kind: user

(Amateur) Astrophysicist

JOSS Publication

lintegrate: A C/Python numerical integration library for working in log-space
Published
May 25, 2022
Volume 7, Issue 73, Page 4231
Authors
Matthew Pitkin ORCID
Department of Physics, Lancaster University, Lancaster, UK, LA1 4YB
Editor
Mehmet Hakan Satman ORCID
Tags
numerical integration GSL

GitHub Events

Total
  • Issues event: 2
  • Watch event: 2
  • Issue comment event: 2
Last Year
  • Issues event: 2
  • Watch event: 2
  • Issue comment event: 2

Committers

Last synced: 7 months ago

All Time
  • Total Commits: 186
  • Total Committers: 4
  • Avg Commits per committer: 46.5
  • Development Distribution Score (DDS): 0.124
Past Year
  • Commits: 1
  • Committers: 1
  • Avg Commits per committer: 1.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Matthew Pitkin m****n@l****g 163
Duncan Macleod d****d@l****g 18
Luiz Max F. Carvalho l****a@g****m 4
dependabot[bot] 4****] 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 9
  • Total pull requests: 25
  • Average time to close issues: 5 months
  • Average time to close pull requests: about 11 hours
  • Total issue authors: 5
  • Total pull request authors: 5
  • Average comments per issue: 2.0
  • Average comments per pull request: 0.12
  • Merged pull requests: 25
  • Bot issues: 0
  • Bot pull requests: 1
Past Year
  • Issues: 1
  • Pull requests: 1
  • Average time to close issues: 3 days
  • Average time to close pull requests: about 10 hours
  • Issue authors: 1
  • Pull request authors: 1
  • Average comments per issue: 2.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 1
Top Authors
Issue Authors
  • mattpitkin (5)
  • maxbiostat (1)
  • Het-Shah (1)
  • quadrikel (1)
  • horta (1)
Pull Request Authors
  • mattpitkin (17)
  • duncanmmacleod (4)
  • dependabot[bot] (2)
  • maxbiostat (2)
  • jbytecode (1)
Top Labels
Issue Labels
enhancement (2)
Pull Request Labels
dependencies (2) github_actions (2)

Packages

  • Total packages: 2
  • Total downloads:
    • pypi 153 last-month
  • Total dependent packages: 2
    (may contain duplicates)
  • Total dependent repositories: 2
    (may contain duplicates)
  • Total versions: 40
  • Total maintainers: 1
pypi.org: lintegrate

Python functions implementing numerical integration of functions in log-space.

  • Versions: 27
  • Dependent Packages: 1
  • Dependent Repositories: 1
  • Downloads: 153 Last month
Rankings
Dependent packages count: 4.8%
Average: 16.3%
Forks count: 16.9%
Stargazers count: 17.1%
Downloads: 21.1%
Dependent repos count: 21.6%
Maintainers (1)
Last synced: 6 months ago
conda-forge.org: lintegrate

lintegrate provides Python functions for numerical integration of functions in log-space. This is useful for functions that have a very large dynamic range, or intrinsically would include very large or small numbers, and therefore it is more practical to work with the natural logarithm of the function. In these cases it is useful to also stay within log-space when integrating the function, which is what this package enables. The functions provided by lintegrate are equivalent to, and based on, several numerical integration routines provided within the GNU Scientific Library (GSL).

  • Versions: 13
  • Dependent Packages: 1
  • Dependent Repositories: 1
Rankings
Dependent repos count: 24.2%
Dependent packages count: 29.0%
Average: 41.5%
Forks count: 56.4%
Stargazers count: 56.4%
Last synced: 6 months ago

Dependencies

.github/workflows/build.yml actions
  • actions/checkout v2 composite
  • conda-incubator/setup-miniconda v2 composite
.github/workflows/pypi.yml actions
  • actions/checkout v2 composite
  • actions/download-artifact v2 composite
  • actions/setup-python v2 composite
  • actions/upload-artifact v2 composite
  • pypa/gh-action-pypi-publish master composite
pyproject.toml pypi
setup.py pypi