Professional Documents
Culture Documents
Numerical Methods With Lualatex: January 2014
Numerical Methods With Lualatex: January 2014
net/publication/272354171
CITATION READS
1 1,189
4 authors:
Some of the authors of this publication are also working on these related projects:
W-methods and their application to the time integration PDEs View project
Strongly A-stable, first stage explicit, Runge-Kutta methods for stiff ODEs and DAEs View project
All content following this page was uploaded by Juan Luis Varona on 17 February 2015.
Numerical methods with LuaLATEX was undertaken a few years ago. (The leaders of the
project and main developers are Taco Hoekwater,
Juan I. Montijano, Mario Pérez, Luis Rández
Hartmut Henkel and Hans Hagen.) The idea was
and Juan Luis Varona
to enhance TEX with a previously existing general
Abstract purpose programming language. After careful eval-
uation of possible candidates, the language chosen
An extension of TEX known as LuaTEX has been in
was Lua (see http://www.lua.org), a powerful, fast,
development for the past few years. Its purpose is to
lightweight, embeddable scripting language that has,
allow TEX to execute scripts written in the general
of course, a free software license suitable to be used
purpose programming language Lua. There is also
with TEX. Moreover, Lua is easy to learn and use,
LuaLATEX, which is the corresponding extension for
and anyone with basic programming skills can use it
LATEX.
without difficulty. (Many examples of Lua code can
In this paper, we show how LuaLATEX can be
be found later in this article, and also, for example,
used to perform tasks that require a large amount
at http://rosettacode.org/wiki/Category:Lua,
of mathematical computation. With LuaLATEX in-
and http://lua-users.org/.)
stead of LATEX, we achieve important improvements:
LuaTEX is not TEX, but an extension of TEX, in
since Lua is a general purpose language, rendering
the same way that pdfTEX or XETEX are extensions.
documents that include evaluation of mathematical
In fact, LuaTEX includes pdfTEX (it is an extension
algorithms is much easier, and generating the PDF
of pdfTEX, and offers backward compatibility), and
file becomes much faster.
also has many of the features of XETEX.
LuaTEX is still in a beta stage, but the current
Introduction versions are usable (the first public beta was launched
TEX (and LATEX) is a document markup language in 2007, and when this paper was written in January
used to typeset beautiful papers and books. Al- 2013, the release used was version 0.74).
though it can also do programming commands such It has many new features useful for typographic
as conditional execution, it is not a general purpose composition; and examples can be seen at the project
programming language. Thus there are many tasks web site http://www.luatex.org, and some papers
that are easily done with other programming lan- using development versions have been published in
guages, but are very complicated or very slow when TUGboat, among them [3, 2, 4, 5, 7, 11]. Most
done with TEX. Due to this limitation, auxiliary of those articles are devoted to the internals and
programs have been developed to assist TEX with are very technical, only for true TEX wizards; we
common tasks related to document preparation. For do not deal with this in this paper. Instead, our
instance, bibtex or biber to build bibliographies, and goal is to show how the mathematical power of the
makeindex or xindy to generate indexes. In both embedded language Lua can be used in LuaTEX. Of
cases, sorting a list alphabetically is a relatively sim- course, when we build LATEX over LuaTEX, we get
ple task for most programming languages, but it is so-called LuaLATEX, which will be familiar to regular
very complicated to do with TEX, hence the desire LATEX users.
for auxiliary applications. All the examples in this paper are done with
Another shortcoming of TEX is the computa- LuaLATEX. It is important to note that the current
tion of mathematical expressions. One of the most version of LuaTEX is not meant for production and
common uses of TEX is to compose mathematical beta users are warned of possible future changes in
formulas, and it does this extremely well. However the syntax. However, the examples in this article
TEX is not good at computing mathematics. For use only a few general Lua-specific commands, so it
instance, TEX itself does not have built-in functions is likely these examples will continue to work with
to compute a square root or a sine. Although it is future versions.
possible to compute mathematical functions with the To process a LuaLATEX document we perform
help of auxiliary packages written in TEX, internally the following steps: First, we must compile with
these packages must compute functions using only LuaLATEX, not with LATEX; how to do this depends on
addition, subtraction, multiplication and division op- the editor being used. Second, we must load the pack-
erations — and a very large number of them. This age luacode with \usepackage{luacode}. Then, in-
is difficult to program (for package developers) and side LuaLATEX, we can jump into Lua mode with the
slow in execution. command \directlua; moreover, we can define Lua
To address the need to do more complex func- routines in a \begin{luacode}. . . \end{luacode}
tions within TEX, an extension of TEX called LuaTEX environment (also {luacode*} instead of {luacode}
can be used); the precise syntax can be found in the x sin(x) cos(x) tan(x)
manual “The luacode package” (by Manuel Pégourié- 0◦ 0.00000 0.00000 1.00000 0.00000
Gonnard) [10]. In the examples, we do not explain 3◦ 0.05236 0.05234 0.99863 0.05241
6◦ 0.10472 0.10453 0.99452 0.10510
all the details of the code; they are left to the reader’s
9◦ 0.15708 0.15643 0.98769 0.15838
intuition.
12◦ 0.20944 0.20791 0.97815 0.21256
In this paper we present four examples. The 15◦ 0.26180 0.25882 0.96593 0.26795
first is very simple: the computation of a trigonomet- 18◦ 0.31416 0.30902 0.95106 0.32492
ric table. In the other examples we use the LATEX 21◦ 0.36652 0.35837 0.93358 0.38386
packages tikz and pgfplots to show Lua’s ability to 24◦ 0.41888 0.40674 0.91355 0.44523
produce graphical output. Some mathematical skill 27◦ 0.47124 0.45399 0.89101 0.50953
may be necessary to fully understand the examples, 30◦ 0.52360 0.50000 0.86603 0.57735
but the reader can nevertheless see how Lua is able 33◦ 0.57596 0.54464 0.83867 0.64941
to manage the computation-intensive job. In any 36◦ 0.62832 0.58779 0.80902 0.72654
case, we do not explore the more complex possibili- 39◦ 0.68068 0.62932 0.77715 0.80978
42◦ 0.73304 0.66913 0.74314 0.90040
ties, which involve writing Lua programs that load
45◦ 0.78540 0.70711 0.70711 1.00000
existing Lua modules or libraries to perform a wide
range of functions and specialized tasks. Figure 1: A trigonometric table.
Juan I. Montijano, Mario Pérez, Luis Rández and Juan Luis Varona
TUGboat, Volume 35 (2014), No. 1 53
−4 \begin{luacode*}
-- Fourier series
function partial_sum(n,x)
Error
partial = 0;
for k = 1, n, 1 do
−6 partial = partial + math.sin(k*x)/k
end;
return partial
end
−8
-- Code to write PGFplots data as coordinates
2 3 4 5 6 7 8 function print_partial_sum(n,xMin,xMax,npoints,
Cost option)
local delta = (xMax-xMin)/(npoints-1)
Figure 2: Plotting of a data table with pgfplots. local x = xMin
if option~=[[]] then
tex.sprint("\\addplot[" .. option
\addplot[color=red,mark=x] coordinates {
.. "] coordinates{")
(2,-2.8559703) (3,-3.5301677) (4,-4.3050655)
else
(5,-5.1413136) (6,-6.0322865) (7,-6.9675052)
tex.sprint("\\addplot coordinates{")
(8,-7.9377747)
end
};
for i=1, npoints do
\end{axis}
y = partial_sum(n,x)
\end{tikzpicture}
tex.sprint("("..x..","..y..")")
generates the plot in Figure 2. Before going on, note x = x+delta
that in future versions the packages PGF/TikZ and end
pgfplots could, internally, use LuaLATEX themselves tex.sprint("}")
in a way transparent to the user. This would allow end
extra power, calculating speed, and simplicity, but \end{luacode*}
this is not yet available and we will not worry about Then, we also define the command
it in this paper. \newcommand\addLUADEDplot[5][]{%
In the next example we consider the Gibbs phe- \directlua{print_partial_sum(#2,#3,#4,#5,
nomenon. Using LuaLATEX, the idea is to compute a [[#1]])}%
data table with Lua (easy to program, powerful and }
fast in the execution), and plot it with pgfplots. which will be used to call the data from pgfplots.
The Gibbs phenomenon is the peculiar way in Here, the parameters have the following meaning: #2
which the Fourier series of a piecewise continuously indicates the number of terms to be added (n = 30
differentiable periodic function behaves at a jump dis- in our case); the plot will be done in the interval [#3,
continuity, where the n-th partial sum of the Fourier #4] (from x = 0 to 10π) sampled with #5 points (to
series has large oscillations near the jump. It is ex- get a very smooth graphic and to show the power of
plained in many harmonic analysis texts, but for the the method we use 1000 points); finally, the optional
purpose of this paper the reader can refer to [13]. argument #1 is used to manage optional arguments
In our case we consider the function f (x) = (π − in the \addplot environment (for instance color
x)/2 in the interval (0, 2π) extended by periodicity [grayscaled for TUGboat], width of the line, . . . ).
to the whole real line (it has discontinuity jumps at Now, the plot is generated by
2jπ for every integer j). Its Fourier series is
∞ \pgfplotsset{width=.9\hsize}
X sin(kx) \begin{tikzpicture}\small
f (x) = .
k \begin{axis}[
k=1
xmin=-0.2, xmax=31.6,
To show the Gibbs phenomenon, we evaluate the
Pn ymin=-1.85, ymax=1.85,
partial sum k=1 sin(kx)k (for n = 30) with Lua to xtick={0,5,10,15,20,25,30},
generate a table of data, and we plot it with pgfplots. ytick={-1.5,-1.0,-0.5,0.5,1.0,1.5},
In the .tex file, we include the following Lua minor x tick num=4,
to compute the partial sum (function partial_sum) minor y tick num=4,
where
1.5
k1 = hf (ti , yi ),
1
k = hf (t + 1 h, y + 1 k ),
2 i 2 i 2 1
0.5 k3 = hf (ti + 2 h, yi + 12 k2 ),
1
k4 = hf (ti + h, yi + k3 ).
5 10 15 20 25 30
−0.5 See, for instance, [15].
−1 Here, we consider the initial value problem
( p
y 0 (t) = y(t) cos t + 1 + y(t) ,
−1.5
y(0) = 1.
P30 sin(kx)
Figure 3: The partial sum k=1 k
of the Fourier
series of f (x) = (π − x)/2 illustrating the Gibbs
In the Lua part of our .tex file, we compute
phenomenon. the values {(ti , yi )} and export them with pgfplots
syntax by means of
\begin{luacode*}
-- Differential equation y'(t) = f(t,y)
axis lines=middle,
-- with f(t,y) = y * cos(t+sqrt(1+y)).
axis line style={-}
-- Initial condition: y(0) = 1
]
function f(t,y)
% SYNTAX: Partial sum 30, from x = 0 to 10*pi,
return y * math.cos(t+math.sqrt(1+y))
% sampled in 1000 points.
end
\addLUADEDplot[color=blue,smooth]{30}
{0}{10*math.pi}{1000};
-- Code to write PGFplots data as coordinates
\end{axis}
function print_RKfour(tMax,npoints,option)
\end{tikzpicture}
local t0 = 0.0
See the output in Figure 3. local y0 = 1.0
local h = (tMax-t0)/(npoints-1)
local t = t0
3 Third example: Runge-Kutta method local y = y0
A differential equation is an equation that links an if option~=[[]] then
unknown function and its derivatives, and these equa- tex.sprint("\\addplot[" .. option
tions play a prominent role in engineering, physics, .. "] coordinates{")
else
economics, and other disciplines. When the value of
tex.sprint("\\addplot coordinates{")
the function at an initial point is fixed, a differential
end
equation is known as an initial value problem. The tex.sprint("("..t0..","..y0..")")
mathematical theory of differential equations shows for i=1, npoints do
that, under very general conditions, an initial value k1 = h * f(t,y)
problem has a unique solution. Usually, it is not k2 = h * f(t+h/2,y+k1/2)
possible to find the exact solution in an explicit form, k3 = h * f(t+h/2,y+k2/2)
and it is necessary to approximate it by means of k4 = h * f(t+h,y+k3)
numerical methods. y = y + (k1+2*k2+2*k3+k4)/6
One of the most popular methods to integrate t = t + h
numerically an initial value problem tex.sprint("("..t..","..y..")")
end
tex.sprint("}")
y 0 (t) = f (t, y(t)),
end
y(t0 ) = y0 \end{luacode*}
Also, we define the command
is the classical Runge-Kutta method of order 4. With
\newcommand\addLUADEDplot[3][]{%
it, we compute in an approximate way the values
\directlua{print_RKfour(#2,#3,[[#1]])}%
yi ' y(ti ) at a set of points {ti } starting from i = 0 }
and with ti+1 = ti + h for every i by the algorithm
to call the Lua routine from the LATEX part (the
1 parameter #2 indicates the final value of t, and #3 is
yi+1 = yi + (k1 + 2k2 + 2k3 + k4 ), the number of sampled points).
6
Juan I. Montijano, Mario Pérez, Luis Rández and Juan Luis Varona
TUGboat, Volume 35 (2014), No. 1 55
0.8
0.6 40
0.4
20
20
0.2
−10 0
0
5 10 15 20 25 30 10 −20
Figure 4: Solution p of the differential equation Figure 5: The Lorentz attractor (six orbits starting at
y 0 (t) = y(t) cos t + 1 + y(t) with initial condition
several initial points).
y(0) = 1.
Juan I. Montijano, Mario Pérez, Luis Rández and Juan Luis Varona