{"id":22142712,"url":"https://github.com/mattpitkin/lintegrate","last_synced_at":"2025-07-25T23:32:17.085Z","repository":{"id":21652183,"uuid":"93165960","full_name":"mattpitkin/lintegrate","owner":"mattpitkin","description":"A numerical integration routine that works for the natural logarithm of functions ","archived":false,"fork":false,"pushed_at":"2024-05-21T13:05:57.000Z","size":514,"stargazers_count":10,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-22T11:31:27.850Z","etag":null,"topics":["gsl","gsl-functions","integral","python","wrapper"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mattpitkin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-02T13:02:03.000Z","updated_at":"2024-05-22T11:31:27.851Z","dependencies_parsed_at":"2024-05-20T09:43:41.509Z","dependency_job_id":null,"html_url":"https://github.com/mattpitkin/lintegrate","commit_stats":{"total_commits":161,"total_committers":6,"mean_commits":"26.833333333333332","dds":0.2919254658385093,"last_synced_commit":"e66c0ffb12e01b5ac2766b63e133fefa93cb2602"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpitkin%2Flintegrate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpitkin%2Flintegrate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpitkin%2Flintegrate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpitkin%2Flintegrate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattpitkin","download_url":"https://codeload.github.com/mattpitkin/lintegrate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227629004,"owners_count":17796054,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gsl","gsl-functions","integral","python","wrapper"],"created_at":"2024-12-01T21:17:46.708Z","updated_at":"2024-12-01T21:17:48.237Z","avatar_url":"https://github.com/mattpitkin.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lintegrate\n\nA numerical integration library for when you want/need to work with the natural logarithm of the function requiring integration.\n\nThis library provides three numerical integration functions, heavily based on [GSL](https://www.gnu.org/software/gsl/) functions, to integrate a function when only its natural logarithm is given, and return the natural logarithm of that integral. The three functions:\n\n * `lintegrate_qag`\n * `lintegrate_qng`\n * `lintegrate_cquad`\n\n are equivalents of the GSL functions:\n\n * [`gsl_integration_qag`](https://www.gnu.org/software/gsl/doc/html/integration.html#qag-adaptive-integration)\n * [`gsl_integration_qng`](https://www.gnu.org/software/gsl/doc/html/integration.html#qng-non-adaptive-gauss-kronrod-integration)\n * [`gsl_integration_cquad`](https://www.gnu.org/software/gsl/doc/html/integration.html#cquad-doubly-adaptive-integration)\n\nrespectively. 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.\n\nThe 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.\n\nThe full API documentation and examples can be found [here](https://lintegrate.readthedocs.io/).\n\n## Example\n\nAn [example](example/example.c) of the use the functions is:\n\n```C\n/* example using lintegrate functionality */\n\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \u003cmath.h\u003e\n#include \u003cgsl/gsl_math.h\u003e\n#include \u003cgsl/gsl_integration.h\u003e\n\n#include \u003clintegrate.h\u003e\n\n/* create function for integration */\ndouble lintegrand(double x, void *params);\n\nstruct intparams {\n  double mu;\n  double sig;\n};\n\ndouble lintegrand(double x, void *params){\n  struct intparams * p = (struct intparams *)params;\n  double mu = p-\u003emu;\n  double sig = p-\u003esig;\n\n  return -0.5*(mu-x)*(mu-x)/(sig*sig);\n}\n\ndouble integrand(double x, void *params){\n  struct intparams * p = (struct intparams *)params;\n  double mu = p-\u003emu;\n  double sig = p-\u003esig;\n\n  return exp(-0.5*(mu-x)*(mu-x)/(sig*sig));\n}\n\nint main( int argv, char **argc ){\n  gsl_function F;\n  struct intparams params;\n  gsl_integration_workspace *w = gsl_integration_workspace_alloc (100);\n  gsl_integration_cquad_workspace *cw = gsl_integration_cquad_workspace_alloc(50);\n  double qaganswer = 0., qnganswer = 0., cquadanswer = 0., answer = 0.;\n  double abserr = 0.;\n  size_t neval = 0;\n\n  double minlim = -6.; /* minimum for integration range */\n  double maxlim = 6.;  /* maximum for integration range */\n\n  double abstol = 1e-10; /* absolute tolerance */\n  double reltol = 1e-10; /* relative tolerance */\n\n  params.mu = 0.;\n  params.sig = 1.;\n\n  F.function = \u0026lintegrand;\n  F.params = \u0026params;\n\n  /* integrate log of function using QAG */\n  lintegration_qag(\u0026F, minlim, maxlim, abstol, reltol, 100, GSL_INTEG_GAUSS31, w, \u0026qaganswer, \u0026abserr);\n\n  /* integrate log of function using QNG */\n  lintegration_qng(\u0026F, minlim, maxlim, abstol, reltol, \u0026qnganswer, \u0026abserr, \u0026neval);\n\n  /* integrate log of function using CQUAD */\n  lintegration_cquad(\u0026F, minlim, maxlim, abstol, reltol, cw, \u0026cquadanswer, \u0026abserr, \u0026neval);\n\n  /* integrate function using GSL QAG */\n  F.function = \u0026integrand;\n  gsl_integration_qag(\u0026F, minlim, maxlim, abstol, reltol, 100, GSL_INTEG_GAUSS31, w, \u0026answer, \u0026abserr);\n\n  gsl_integration_workspace_free(w);\n  gsl_integration_cquad_workspace_free(cw);\n\n  fprintf(stdout, \"Answer \\\"lintegrate QAG\\\" = %.8lf\\n\", qaganswer);\n  fprintf(stdout, \"Answer \\\"lintegrate QNG\\\" = %.8lf\\n\", qnganswer);\n  fprintf(stdout, \"Answer \\\"lintegrate CQUAD\\\" = %.8lf\\n\", cquadanswer);\n  fprintf(stdout, \"Answer \\\"gsl_integrate_qag\\\" = %.8lf\\n\", log(answer));\n  fprintf(stdout, \"Analytical answer = %.8lf\\n\", log(sqrt(2.*M_PI)));\n\n  return 0;\n}\n```\n\n## Requirements\n\n* [GSL](https://www.gnu.org/software/gsl/) - on Debian/Ubuntu (16.04) install with e.g. `sudo apt-get install libgsl-dev`\n\n## Installation\n\nThe library can be built using [scons](http://scons.org) by just typing `sudo scons` in the base directory. To install\nthe library system-wide (in `/usr/local/lib` by default) run:\n```\nsudo scons\nsudo scons install\n```\n\nA Python module containing wrappers to the functions can be built and installed from source for the user by running, e.g.:\n```bash\npip install .\n```\nfrom within the repository directory.\n\nThe Python module can also be installed from [PyPI](https://pypi.org/project/lintegrate/) using pip with:\n```bash\npip install lintegrate\n```\n\nor in a Conda environment with:\n```bash\nconda install -c conda-forge lintegrate\n```\n\n## Python\n\nIf the Python module has been installed it has the following functions:\n * `lqng` - a wrapper to `lintegration_qng`\n * `lqag` - a wrapper to `lintegration_qag`\n * `lcquad` - a wrapper to `lintegration_cquad`\n * `logtrapz` - using the trapezium rule for integration on a grid of values\n\nThe `lqng`, `lqag`, and `lcquad` functions are used in a similar way to the scipy [`quad`](https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.integrate.quad.html) function.\n\nAn example of their use would be:\n\n```python\nfrom lintegrate import lqag, lqng, lcquad, logtrapz\nimport numpy as np\n\n# define the log of the function to be integrated\ndef integrand(x, args):\n    mu, sig = args # unpack extra arguments\n    return -0.5*((x-mu)/sig)**2\n\n# set integration limits\nxmin = -6.\nxmax = 6.\n\n# set additional arguments\nmu = 0.\nsig = 1.\n\nresqag = lqag(integrand, xmin, xmax, args=(mu, sig))\nresqng = lqng(integrand, xmin, xmax, args=(mu, sig))\nrescquad = lcquad(integrand, xmin, xmax, args=(mu, sig))\nrestrapz = logtrapz(integrand, np.linspace(xmin, xmax, 100), args=(mu, sig))\n```\n\n## R\n\nIn [R](https://www.r-project.org/) one can use the [**reticulate**](https://github.com/rstudio/reticulate) package to call the functions in `lintegrate`.\nThe above example would be:\n```R\nlibrary(reticulate)\npy_install(\"lintegrate\", pip = TRUE) ## run once to make sure lintegrate is installed and visible to reticulate.\nlint \u003c- import(\"lintegrate\", convert = FALSE)\nintegrand \u003c- function(x, args){\n  mu = args[1]\n  sig = args[2]\n  return(-.5 * ((x-mu)/sig)^2 )\n} \nintegrand \u003c- Vectorize(integrand)\nmu \u003c- 0\nsig \u003c- 1\nmmin \u003c- -10\nmmax \u003c- 10\nlint$lqag(py_func(integrand), r_to_py(mmin), r_to_py(mmax), c(mu, sig))\n```\n\n## Citation\n\nIf using `lintegrate` in your research, I would be grateful if you cite the associated [JOSS paper](https://joss.theoj.org/papers/10.21105/joss.04231) for the software. The following BibTeX citation can be used:\n\n```bibtex\n@article{Pitkin2022,\n  doi = {10.21105/joss.04231},\n  url = {https://doi.org/10.21105/joss.04231},\n  year = {2022},\n  publisher = {The Open Journal},\n  volume = {7},\n  number = {73},\n  pages = {4231},\n  author = {Matthew Pitkin},\n  title = {lintegrate: A C/Python numerical integration library for working in log-space},\n  journal = {Journal of Open Source Software}\n}\n```\n\nYou may also want to cite the [GSL](https://www.gnu.org/software/gsl/) reference \"_M. Galassi et al, GNU Scientific Library Reference Manual (3rd Ed.), ISBN 0954612078_\" and the URL http://www.gnu.org/software/gsl/.\n\n\n[![DOI](https://joss.theoj.org/papers/10.21105/joss.04231/status.svg)](https://doi.org/10.21105/joss.04231)\n[![Build Status](https://github.com/mattpitkin/lintegrate/workflows/build/badge.svg)](https://github.com/mattpitkin/lintegrate/actions?query=workflow%3Abuild)\n[![PyPI version](https://badge.fury.io/py/lintegrate.svg)](https://badge.fury.io/py/lintegrate)\n[![Anaconda-Server Badge](https://anaconda.org/conda-forge/lintegrate/badges/version.svg)](https://anaconda.org/conda-forge/lintegrate)\n[![Documentation Status](https://readthedocs.org/projects/lintegrate/badge/?version=latest)](http://lintegrate.readthedocs.io/en/latest/?badge=latest)\n\n\u0026copy; 2017 Matthew Pitkin\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattpitkin%2Flintegrate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattpitkin%2Flintegrate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattpitkin%2Flintegrate/lists"}