The goal of this experimental code was to allow the use of the GNU MPFR library from within the scripting language Lua. I could not find any Lua API for the GNU MPFR library in the internet, so i wrote one myself, which was rather straight-forward owing to the clear interface designs of the MPFR library and Lua.

The sources for the Lua API can be downloaded here:

- lmpfrlib.c The GNU MPFR API source written in C.
- Makefile A simple Makefile for generating of the binary library file.
- test.lua A simple library test program in Lua.

The codes presented here come under the GNU LGPL licence. Tested with debian (jessie) Linux. Works with Lua 5.3.2, but is currently not compatible with LuaJIT. Sorry, there is no documentation here. Consulting the official MPFR library documentation together with the above files lmpfrlib.c, test.lua, and the files further below should allow to basically use the API.

One side aim while writing the Lua Api for the GNU MPFR was to have a means for calculation of the abscissas and weights needed for Gauss-Legendre quadrature in Lua. A plethora of documents and program codes, over many decades, for Gaussian node calculation can be found in the internet. One example from 1956 is the publication Abscissas and Weights for Gaussian Quadratures of High Order by P. Davis and P. Rabinowitz, which lists abscissas and weights for n between 2 and 48. Jumping to 2014, this topic got a significant boost by the seminal paper (with source code) Iteration-Free Computation of Gauss-Legendre Quadrature Nodes and Weights by I. Bogaert. This calculation method excels with unsurpassed calculation speed for rules with many nodes.

The requirements for my spare time purposes were different:

- Have something at hand to calculate a Gauss-Legendre quadrature function in Lua, ad hoc and off-line, for a reasonably small given number of nodes (maybe up to 16384 nodes).
- Refrain from using any node table or source code offering with unclear or non-free licence, as can also be found in the internet.
- Have some short, simple Lua code, e. g., without the need for included extensive auxiliary tables.
- Use a high MPFR precision with many excess bits (and experiment with this parameter) to get abscissa and weight values that are exact to the last bit of a C double type.
- Allow to calculate abscissa and weight values for applications needing even higher precision than is possible by standard C types.
- End up with a free, open-source solution (LGPL), even if its backward, outdated.
- Calculation speed for the nodes shall be not an issue here at all.
- Just have something to play with Gauss-Legendre quadrature in Lua (and Fortran).

I rather wanted to go for some published (possibly antiquary) primary document, and to type the Lua code right from the formulas in the paper. The calculation method used here is based on the paper "Minimax approximations to the zeros of Pn(x) and Gauss-Legendre quadrature" by F. G. Lether and P. R. Wenston, Journal of Computational and Applied Mathematics 59 (1995), pp. 245--252, which can be found as PDF file in the internet.

The Lua code for calculating nodes for Gauss-Legendre quadrature can be downloaded here:

- lether-wenston-mpfr.lua Lua application library for generation of a custom Lua or Fortran file containing a function for Gauss-legendre quadrature. This could easily be extended, if needed, to generate also a custom C (or other programming language) file containing such a quadrature function.
- lether-wenston-mpfr-test.lua Lua driver file generating a few Lua function and Fortran subroutine files for Gauss-Legendre quadrature.
- dqg64.lua Resulting Lua function example file for 64-point Gauss-Legendre quadrature.
- dqg64.f Resulting Fortran subroutine example file for 64-point Gauss-Legendre quadrature. This is modeled after the dqg32.f subroutine from the IBM System/360 Scientific Subroutine Package (360A-CM-03X) Version III, see http://dx.doi.org/10.3247/sl2soft08.001.
- dqg256.f A Fortran subroutine example file for 256-point Gauss-Legendre quadrature.

The numerical precision of the initial abscissa guesses (cf. Example 5 in the document by Lether and Wenston) seems to be still sufficient for tables with an order of 16384 nodes. This calculates already very slowly, and i have not tried higher orders yet.

The above Lua application library and C API library, as they are written here, are making heavy use of the Lua garbage collector, since variables are freshly generated when assigned. This slows down calculation and bloats memory. Therefore this style is discouraged by the MPFR library, but it is sufficient for a start, and it keeps the Lua source code better readable.

Calculation can be sped up and memory use reduced at the same time, when inner loops are written in the MPFR object style, as can be seen by comparing function P_faster() against function P() in file lether-wenston-mpfr.lua. Readability suffers a bit. The example below is also written in the MPFR object style for a faster calculation.

It should be straight-forward to modify the function in lmpfrlib.c, which calls the Lua garbage collector, so that the unused variables are not collected but instead kept in some list (of presettable maximum length) for their re-use. This should increase the library execution speed considerably (not tried yet).

This is a rather contrived test case for the MPFR Lua API, taken from the venerable paper Formula for the inductance of a helix made with wire of any section by Chester Snow, February 1926. The Lua code (a quick hack) is very near to the original GNU MPFR C interface, but it is clumsy to write in Lua. The advantage is that there is almost no reallocation of variables, which increases calculation speed.

- b2-mpfr.lua Excruciatingly slow calculation of the constant B2 (-0.60835223673..., precision does not actually matter here) by formula (90) on page 458 of above document. This runs nicely nohup for weeks on a Raspberry Pi :-)

Have fun!

This page first put online 23 January 2016.