We introduce the ForeignFunctions package for MACAULAY2, which uses LIBFFI to provide the ability to call functions from external libraries without needing to link against them at compile time.As examples, we use the library FFTW to multiply polynomials using fast Fourier transforms, call a LAPACK function to solve a general Gauss-Markov linear model problem, and use JIT compilation to compute Fibonacci numbers.1. INTRODUCTION.Traditionally, there have been two ways to call functions from external libraries in MACAULAY2 [11], a software platform widely used by algebraic geometers and commutative algebraists.One is to write some code wrapping the external library's functions.This code might involve C++ if the library is to be used by the MACAULAY2 engine, and it would also very likely involve the D language used by the interpreter and the top-level MACAULAY2 language.The library would then be statically or dynamically linked at compile time.For example, the roots function for computing the zeros of a polynomial is implemented in this way using the library MPSOLVE [2; 3].The addition of a new library using this method requires rebuilding MACAULAY2 from source.The other is to use the shell to communicate with an external program's command line interface, often creating several temporary files containing the program's input and output in the process.For example, the Normaliz package [4] communicates in this way with NORMALIZ, a software platform for discrete convex geometry [5].However, using the library LIBFFI [12], it is possible to create a "foreign function interface" where users may open a shared library and call functions from it at run time, without needing to link against the library at compile time or rely on the overhead of communicating with a command line interface via the shell.Beginning with version 1.21, MACAULAY2 has been distributed with the ForeignFunctions package, written by author, which provides such an interface.This paper is organized as follows.In Section 2, we describe the interface.In Section 3, we discuss how it was implemented.The last three sections are devoted to examples.In Section 4, we call functions from the FFTW library [9] to compute fast Fourier transforms to multiply polynomials.In Section 5, we use LAPACK to solve a general Gauss-Jordan linear model problem.In Section 6, we use the ForeignFunctions package for JIT compilation to quickly compute Fibonacci numbers.
No takes yet. Share an insight, caveat, or question.
Douglas A. Torrance (2025) studied this question.
Synapse has enriched 5 related papers. Consider them for comparative context: