Lua API for the GNU MPFR Library

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:

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.

Application Examples

Abscissas and Weights for Gauss-Legendre Quadrature

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:

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:

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).

Some Constant needed for Calculation of the Inductance of a Helical Wire

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.

Have fun!

This page first put online 23 January 2016.

Copyright © 2001 - 2017 by Hartmut Henkel

Last file change: