Debian Codegraph @

texmf-dist/tex/generic/pgf/libraries/luamath/pgflibraryluamath.code.tex

raw
1 % Copyright 2019 by Christophe Jorssen and Mark Wibrow
2 % Copyright 2019 by Christian Feuersänger
3 %
4 % This file may be distributed and/or modified
5 %
6 % 1. under the LaTeX Project Public License and/or
7 % 2. under the GNU Public License.
8 %
9 % See the file doc/generic/pgf/licenses/LICENSE for more details.
10 %
11 % $Id$
12 %
13 %
14 % This is a library for a LUA math parser and LUA math operations.
15 % Advantage compared to its TeX pendant: it is FASTER and has HIGHER
16 % ACCURACY.
17 % Disadvantage: any function declared by means of
18 % \pgfmathdeclarefunction is NOT automatically transported to the LUA
19 % side (at the time of this writing).
20 % LUA functions need to be defined by means of LUA code
21 %
22 % function pgfluamathfunctions.myoperation(a,b)
23 % return a*2*b
24 % end
25 %
26 % this will automatically set up 'myoperation' for use in
27 % \pgfluamathparse{myoperation(4,2)}
28 %
29 % The library has TWO use-cases which are more or less distinct: one
30 % is to use LUA for all math function, but not for math expression
31 % parsing -- this was still under control of the TeX math parser.
32 % This approach works like the 'fpu' library.
33 %
34 % The second approach is to use LUA to parse all math expressions -
35 % but if someone calls \pgfmathadd@{1}{2}, it would use the TeX
36 % command. This approach is relatively lightweight because it does not
37 % need to substitute all \pgfmath* macros.
38 %
39 % One can mix both modes.
40
41 % *******************************************************************
42 % Some luatex stuff. Should be put elsewhere (e.g. in
43 % pgfutil-luatex-engine).
44
45 \edef\pgfliblua@oldcatcodedoublequote{\the\catcode`\"}%
46 \catcode`\"=12
47
48 % We assume luatex version > 0.39:
49 % - \directlua <general text> will work
50 % - \directlua is the only luatex primitive that we can assume
51 % accessible without being prefixed by the format via
52 % tex.enableprimitives.
53 % Ideas taken from the ifluatex package (Heiko Oberdiek)
54 \let\pgfutil@ifluatex\iffalse
55 \begingroup\expandafter\expandafter\expandafter\endgroup
56 \expandafter\ifx\csname directlua\endcsname\relax
57 \else
58 \expandafter\let\csname pgfutil@ifluatex\expandafter\endcsname
59 \csname iftrue\endcsname
60 \fi
61
62 \pgfutil@ifluatex
63 \let\pgfutil@directlua\directlua
64 \pgfutil@directlua{%
65 tex.enableprimitives('pgfutil@',{'luaescapestring'})}%
66 \else
67 \def\pgfutil@directlua#1{}%
68 \def\pgfutil@luaescapestring#1{}%
69 \fi
70
71 \pgfutil@directlua{%
72 pgfluamathfunctions = require("pgf.luamath.functions")
73 pgfluamathparser = require("pgf.luamath.parser")}%
74
75
76 % Patch some configuration macros such that the modifications are
77 % available in LUA as well:
78 \pgfkeys{
79 /pgf/trig format/deg/.add code={}{\directlua{pgfluamathfunctions.setTrigFormat("deg")}\aftergroup\pgfmath@settrigformat},
80 /pgf/trig format/rad/.add code={}{\directlua{pgfluamathfunctions.setTrigFormat("rad")}\aftergroup\pgfmath@settrigformat},
81 }%
82 % ... and reactivate the key:
83 \pgfmathiftrigonometricusesdeg{%
84 \pgfkeys{/pgf/trig format/deg}%
85 }{%
86 \pgfkeys{/pgf/trig format/rad}%
87 }%
88
89 % re-activates the current trig format. This is important after a TeX
90 % group has been closed.
91 \def\pgfmath@settrigformat{%
92 \pgfmathiftrigonometricusesdeg{%
93 \directlua{pgfluamathfunctions.setTrigFormat("deg")}%
94 }{%
95 \directlua{pgfluamathfunctions.setTrigFormat("rad")}%
96 }%
97 }%
98
99 \let\pgfmathsetseed@pgfbasic = \pgfmathsetseed
100 \def\pgfmathsetseed#1{%
101 \pgfmathsetseed@pgfbasic{#1}%
102 \directlua{pgfluamathfunctions.setRandomSeed(pgfluamathfunctions.tonumber("\pgfmathresult"))}%
103 }%
104
105 % Patch 'declare function' such that it communicates the function
106 % directly to LUA.
107 \let\pgfmathnotifynewdeclarefunction@orig=\pgfmathnotifynewdeclarefunction
108 \def\pgfmathnotifynewdeclarefunction#1#2#3{%
109 \pgfmathnotifynewdeclarefunction@orig{#1}{#2}{#3}%
110 %
111 % we have to check if '#3' contains control sequences.
112 % this is highly tricky as it may contain '#1'...
113 \begingroup
114 \toks0={#3}%
115 \xdef\pgf@marshal@glob{\the\toks0 }%
116 \endgroup
117 \pgfutil@command@to@string\pgf@marshal@glob\pgf@marshal
118 \expandafter\pgfutilifcontainsmacro\expandafter{\pgf@marshal}{%
119 \def\pgf@temp{1}%
120 }{%
121 \def\pgf@temp{0}%
122 }%
123 \if1\pgf@temp
124 % let lua produce an error when evaluating this function -- we cannot possibly expand the macro to its current state:
125 \directlua{pgfluamathparser.pushLocalExpressionFunction(%
126 "\pgfutil@luaescapestring{#1}",%
127 #2,%
128 "functionMustBeEvaluatedInTeX")}%
129 \else
130 \directlua{pgfluamathparser.pushLocalExpressionFunction(%
131 "\pgfutil@luaescapestring{#1}",%
132 #2,%
133 "\pgfutil@luaescapestring{#3}")}%
134 \fi
135 %
136 % ensure that the local function is removed at the end of the
137 % scope. To this end, we maintain a stack on the LUA side.
138 \aftergroup\pgfluamathparse@pop@local@function
139 }%
140
141 \def\pgfluamathparse@pop@local@function{%
142 \directlua{pgfluamathparser.popLocalExpressionFunction()}%
143 }%
144
145 % End of luatex stuff
146 % *******************************************************************
147
148 % Loading part: based on fpu library
149
150
151 % if LUA failed to evaluate the expression, it will be evaluated in
152 % TeX as fallback. This boolean defines if the error message shall be
153 % shown or suppressed. The default is to suppress it and show any
154 % resulting TeX errors.
155 %
156 % If LUA fails \pgfmathresult will be empty.
157 %
158 % This is actually only for debugging; it will be set implicitly when
159 % activating/deactivating TeX fallback.
160 \newif\ifpgfluamathshowerrormessage
161
162 % Defines what happens if LUA failed to evaluate the expression: if
163 % this is true, the TeX parser will be invoked as fallback.
164 %
165 % This happens
166 % - for \chardef'ed boxes for which LUA lacks evaluation capabilities
167 % - if the expression includes some function which is unavailable in
168 % LUA (defined only in TeX)
169 % - some special cases which simply haven't been added to the LUA
170 % parser (yet).
171 % At the time of this writing, this includes
172 % -- arrays created via '{}' and indexed with '[]'
173 % -- strings with "<str>"
174 % -- 'scalar' function
175 % -- hex/octal/binary input
176 \newif\ifpgfluamathenableTeXfallback
177
178 \newif\ifpgfluamathcomputationactive
179 \newif\ifpgfluamathparseractive
180
181 \def\pgfluamath@makecomputationactive{%
182 \ifpgfluamathcomputationactive
183 \else
184 \pgfluamath@checkuninstallcmd%
185 \pgfluamath@install%
186 \pgfluamathcomputationactivetrue
187 \fi}%
188
189 \def\pgfluamath@makecomputationinactive{%
190 \ifpgfluamathcomputationactive
191 \pgfluamath@uninstall%
192 \pgfluamathcomputationactivefalse
193 \fi}%
194
195 \let\pgfluamath@pgfmathparse\pgfmathparse
196 \def\pgfluamath@makeparseractive{%
197 \ifpgfluamathparseractive
198 \else
199 \let\pgfluamath@pgfmathparse\pgfmathparse
200 \let\pgfmathparse\pgfluamathparse
201 \let\pgfmath@iftrue=\pgfmathluamath@iftrue
202 \pgfluamathparseractivetrue
203 \fi}%
204
205 \let\pgfmath@iftrue@basic=\pgfmath@iftrue
206
207 \def\pgfluamath@makeparserinactive{%
208 \ifpgfluamathparseractive
209 \let\pgfmathparse\pgfluamath@pgfmathparse
210 \let\pgfmath@iftrue=\pgfmath@iftrue@basic
211 \pgfluamathparseractivefalse
212 \fi}%
213
214 \pgfqkeys{/pgf}{%
215 % Enable lua-side computation of \pgfmathresult
216 % (will be deactivated after the current TeX group)
217 luamath/.is choice,
218 luamath/only computation/.code={%
219 \pgfutil@ifluatex
220 \pgfluamath@makecomputationactive
221 \pgfluamath@makeparserinactive
222 \else
223 \pgfmath@error{Sorry, you need the luaTeX engine to use the
224 luamath library}{}%
225 \fi},
226 luamath/parser and computation/.code={%
227 \pgfutil@ifluatex
228 \pgfluamath@makecomputationactive
229 \pgfluamath@makeparseractive
230 \else
231 \pgfmath@error{Sorry, you need the luaTeX engine to use the
232 luamath library}{}%
233 \fi},
234 luamath/off/.code={%
235 \pgfluamath@makecomputationinactive
236 \pgfluamath@makeparserinactive},
237 luamath/.default=only computation,
238 %
239 % activates ONLY the parser. This is fast and does not replace the
240 % \pgfmath* functions.
241 % @see |parser and computation| which also allows to write
242 % \pgfmathadd{1}{2} to call LUA
243 luamath/parser/.code={%
244 \pgfutil@ifluatex
245 \pgfluamath@makeparseractive
246 \else
247 \pgfmath@error{Sorry, you need the luaTeX engine to use the
248 luamath library}{}%
249 \fi
250 },
251 luamath/output format/.is choice,
252 luamath/output format/fixed/.code= {\def\pgfluamath@outputformat@choice{0}},
253 % returns results for use in the FPU
254 luamath/output format/float/.code= {\def\pgfluamath@outputformat@choice{1}},
255 luamath/output format/fixed,
256 % this is merely useful for debugging purposes, I guess.
257 luamath/show error message/.is if=pgfluamathshowerrormessage,
258 luamath/enable TeX fallback/.is choice,
259 luamath/enable TeX fallback/true/.code={\pgfluamathenableTeXfallbacktrue\pgfluamathshowerrormessagefalse},
260 luamath/enable TeX fallback/false/.code={\pgfluamathenableTeXfallbackfalse\pgfluamathshowerrormessagetrue},
261 luamath/enable TeX fallback/.default=true,
262 luamath/enable TeX fallback=true,
263 }%
264
265 \def\pgfluamath@uninstall@appendcmd#1{%
266 \expandafter\gdef\expandafter\pgfluamath@uninstall\expandafter{%
267 \pgfluamath@uninstall #1}}%
268
269 % If the uninstall command is already assembled, it will skip the
270 % uninstall assemblation.
271 \def\pgfluamath@checkuninstallcmd{%
272 \pgfutil@ifundefined{pgfluamath@uninstall}{%
273 \global\let\pgfluamath@uninstall=\pgfutil@empty
274 }{%
275 % We already HAVE an uninstall command (prepared globally).
276 % So: don't waste time assembling one!
277 \def\pgfluamath@uninstall@appendcmd##1{}%
278 \def\pgfluamath@prepareuninstallcmd##1{}%
279 }%
280 }%
281
282 % This assembles an uninstall command globally ON FIRST USAGE.
283 % See \pgfmathfloat@plots@checkuninstallcmd
284 \def\pgfluamath@prepareuninstallcmd#1{%
285 \expandafter\global\expandafter\let
286 \csname pgfluamath@backup@\string#1\endcsname=#1%
287 \expandafter\gdef\expandafter\pgfluamath@uninstall\expandafter{%
288 \pgfluamath@uninstall
289 \expandafter\let\expandafter#1\csname pgfluamath@backup@\string#1\endcsname}%
290 }%
291
292 \def\pgfluamath@install@function#1=#2{%
293 \pgfluamath@prepareuninstallcmd{#1}%
294 \let#1=#2%
295 }%
296
297 \def\pgfluamath@install{%
298 \pgfluamath@install@function\pgfmathadd@=\pgfluamathadd@%
299 \pgfluamath@install@function\pgfmathsubtract@=\pgfluamathsubtract@%
300 \pgfluamath@install@function\pgfmathneg@=\pgfluamathneg@%
301 \pgfluamath@install@function\pgfmathmultiply@=\pgfluamathmultiply@%
302 \pgfluamath@install@function\pgfmath@iftrue=\pgfmathluamath@iftrue%
303 \pgfluamath@install@function\pgfmathdivide@=\pgfluamathdivide@%
304 % \pgfluamath@install@function\pgfmathdiv@=\pgfluamathdiv@%
305 \pgfluamath@install@function\pgfmathfactorial@=\pgfluamathfactorial@%
306 \pgfluamath@install@function\pgfmathsqrt@=\pgfluamathsqrt@%
307 % \pgfluamath@install@function\pgfmathpow@=\pgfluamathpow@%
308 \pgfluamath@install@function\pgfmathe@=\pgfluamathe@%
309 \pgfluamath@install@function\pgfmathexp@=\pgfluamathexp@%
310 \pgfluamath@install@function\pgfmathln@=\pgfluamathln@%
311 \pgfluamath@install@function\pgfmathlogten@=\pgfluamathlogten@%
312 % \pgfluamath@install@function\pgfmathlogtwo@=\pgfluamathlogtwo@%
313 \pgfluamath@install@function\pgfmathabs@=\pgfluamathabs@%
314 \pgfluamath@install@function\pgfmathmod@=\pgfluamathmod@%
315 \pgfluamath@install@function\pgfmathMod@=\pgfluamathMod@%
316 \pgfluamath@install@function\pgfmathround@=\pgfluamathround@%
317 \pgfluamath@install@function\pgfmathfloor@=\pgfluamathfloor@%
318 \pgfluamath@install@function\pgfmathceil@=\pgfluamathceil@%
319 % \pgfluamath@install@function\pgfmathint@=\pgfluamathint@%
320 % \pgfluamath@install@function\pgfmathfrac@=\pgfluamathfrac@%
321 % \pgfluamath@install@function\pgfmathreal@=\pgfluamathreal@%
322 \pgfluamath@install@function\pgfmathgcd@=\pgfluamathgcd@%
323 \pgfluamath@install@function\pgfmathisprime@=\pgfluamathisprime@%
324 \pgfluamath@install@function\pgfmathpi@=\pgfluamathpi@%
325 \pgfluamath@install@function\pgfmathrad@=\pgfluamathrad@%
326 \pgfluamath@install@function\pgfmathdeg@=\pgfluamathdeg@%
327 \pgfluamath@install@function\pgfmathsin@=\pgfluamathsin@%
328 \pgfluamath@install@function\pgfmathcos@=\pgfluamathcos@%
329 \pgfluamath@install@function\pgfmathtan@=\pgfluamathtan@%
330 \pgfluamath@install@function\pgfmathsec@=\pgfluamathsec@%
331 \pgfluamath@install@function\pgfmathcosec@=\pgfluamathcosec@%
332 \pgfluamath@install@function\pgfmathcot@=\pgfluamathcot@%
333 \pgfluamath@install@function\pgfmathasin@=\pgfluamathasin@%
334 \pgfluamath@install@function\pgfmathacos@=\pgfluamathacos@%
335 \pgfluamath@install@function\pgfmathatan@=\pgfluamathatan@%
336 \pgfluamath@install@function\pgfmathatantwo@=\pgfluamathatantwo@%
337 \pgfluamath@install@function\pgfmathmax@=\pgfluamathmax@%
338 \pgfluamath@install@function\pgfmathmin@=\pgfluamathmin@%
339 % \pgfluamath@install@function\pgfmath@pi=\pgfluamathpi@%
340 % \pgfluamath@install@function\pgfmathpi=\pgfluamathpi@%
341 % \pgfluamath@install@function\pgfmathe@=\pgfluamathe@%
342 % \pgfluamath@install@function\pgfmathe=\pgfluamathe@%
343 % \pgfluamath@install@function\pgfmathlessthan@=\pgfluamathlessthan@%
344 % \pgfluamath@install@function\pgfmathless@=\pgfluamathlessthan@%
345 % \pgfluamath@install@function\pgfmathgreaterthan@=\pgfluamathgreaterthan@%
346 % \pgfluamath@install@function\pgfmathgreater@=\pgfluamathgreaterthan@%
347 % \pgfluamath@install@function\pgfmathpow@=\pgfluamathpow@
348 \pgfluamath@install@function\pgfmathrand@=\pgfluamathrand@
349 \pgfluamath@install@function\pgfmathrand=\pgfluamathrand@
350 \pgfluamath@install@function\pgfmathrnd@=\pgfluamathrnd@
351 \pgfluamath@install@function\pgfmathrnd=\pgfluamathrnd@
352 % \pgfluamath@install@function\pgfmathtrue@=\pgfluamathtrue@
353 % \pgfluamath@install@function\pgfmathfalse@=\pgfluamathfalse@
354 % \pgfluamath@install@function\pgfmathnot@=\pgfluamathnot@
355 % \pgfluamath@install@function\pgfmathhex@=\pgfluamathhex@
356 % \pgfluamath@install@function\pgfmathHex@=\pgfluamathHex@
357 % \pgfluamath@install@function\pgfmathoct@=\pgfluamathoct@
358 % \pgfluamath@install@function\pgfmathbin@=\pgfluamathbin@
359 % \pgfluamath@install@function\pgfmathand@=\pgfluamathand@
360 % \pgfluamath@install@function\pgfmathor@=\pgfluamathor@
361 % \pgfluamath@install@function\pgfmathfactorial@=\pgfluamathfactorial@
362 % \pgfluamath@install@function\pgfmathveclen@=\pgfluamathveclen@
363 % \pgfluamath@install@function\pgfmathcosh@=\pgfluamathcosh@
364 % \pgfluamath@install@function\pgfmathsinh@=\pgfluamathsinh@
365 % \pgfluamath@install@function\pgfmathtanh@=\pgfluamathtanh@
366 % \pgfluamath@install@function@unimplemented{ceil}%
367 % \pgfluamath@install@function@unimplemented{frac}%
368 % \pgfluamath@install@function@unimplemented{log2}%
369 % \pgfluamath@install@function@unimplemented{log10}%
370 % \pgfluamath@install@function@unimplemented{equalto}%
371 % \pgfluamath@install@function@unimplemented{random}%
372 % \pgfluamath@install@function@unimplemented{setseed}%
373 % \pgfluamath@install@function@unimplemented{Mod}%
374 % \pgfluamath@install@function@unimplemented{real}%
375 % \pgfluamath@install@function@unimplemented{notequal}%
376 \pgfluamath@install@function\pgfmathreciprocal=\pgfluamathreciprocal%
377 \pgfluamath@install@function\pgfpointnormalised=\pgfluamathpointnormalised
378 }%
379
380 \def\pgfluamathgetresult#1{%
381 \edef\pgfmathresult{\pgfutil@directlua{tex.print(-1,#1)}}}%
382
383 \def\pgfmathluamath@iftrue{%
384 \if 0\pgfluamath@outputformat@choice
385 \let\pgfmathluamath@@iftrue@v=\pgfluamathone
386 \else
387 \let\pgfmathluamath@@iftrue@v=\pgfluamathfloatone
388 \fi
389 \pgfmathluamath@iftrue@
390 }%
391 \def\pgfluamathone{1.0}%
392 \def\pgfluamathfloatone{1Y1.0e+00]}%
393 \def\pgfmathluamath@iftrue@#1#2{%
394 \ifx\pgfmathresult\pgfmathluamath@@iftrue@v
395 \def\pgfmath@next{#1}%
396 \else
397 \def\pgfmath@next{#2}%
398 \fi
399 \pgfmath@next
400 }%
401
402 \def\pgfluamathpi@{%
403 \pgfluamathgetresult{pgfluamathfunctions.pi()}}%
404 \def\pgfluamathe@{%
405 \pgfluamathgetresult{pgfluamathfunctions.e()}}%
406 \def\pgfluamathadd@#1#2{%
407 \pgfluamathgetresult{pgfluamathfunctions.add(#1,#2)}}%
408 \def\pgfluamathsubtract@#1#2{%
409 \pgfluamathgetresult{pgfluamathfunctions.subtract(#1,#2)}}%
410 \def\pgfluamathneg@#1{%
411 \pgfluamathgetresult{pgfluamathfunctions.neg(#1)}}%
412 \def\pgfluamathmultiply@#1#2{%
413 \pgfluamathgetresult{pgfluamathfunctions.multiply(#1,#2)}}%
414 \def\pgfluamathdivide@#1#2{%
415 \pgfluamathgetresult{pgfluamathfunctions.divide(#1,#2)}}%
416 \def\pgfluamathabs@#1{%
417 \pgfluamathgetresult{pgfluamathfunctions.abs(#1)}}%
418 \def\pgfluamathround@#1{%
419 \pgfluamathgetresult{pgfluamathfunctions.round(#1)}}%
420 \def\pgfluamathfloor@#1{%
421 \pgfluamathgetresult{pgfluamathfunctions.floor(#1)}}%
422 \def\pgfluamathceil@#1{%
423 \pgfluamathgetresult{pgfluamathfunctions.ceil(#1)}}%
424 \def\pgfluamathgcd@#1#2{%
425 \pgfluamathgetresult{pgfluamathfunctions.gcd(#1,#2)}}%
426 \def\pgfluamathisprime@#1{%
427 \pgfluamathgetresult{pgfluamathfunctions.isprime(#1)}}%
428 \def\pgfluamathmax@#1{%
429 \pgfluamathgetresult{%
430 math.max(pgfluamathfunctions.split_braces_to_explist("#1"))}}%
431 \def\pgfluamathmin@#1{%
432 \pgfluamathgetresult{%
433 math.min(pgfluamathfunctions.split_braces_to_explist("#1"))}}%
434 \def\pgfluamathsin@#1{%
435 \pgfluamathgetresult{pgfluamathfunctions.Sin(#1)}}%
436 \def\pgfluamathcos@#1{%
437 \pgfluamathgetresult{pgfluamathfunctions.Cos(#1)}}%
438 \def\pgfluamathtan@#1{%
439 \pgfluamathgetresult{pgfluamathfunctions.Tan(#1)}}%
440 \def\pgfluamathmod@#1#2{%
441 \pgfluamathgetresult{pgfluamathfunctions.mod(#1,#2)}}%
442 \def\pgfluamathMod@#1#2{%
443 \pgfluamathgetresult{pgfluamathfunctions.Mod(#1,#2)}}%
444 \def\pgfluamathrad@#1{%
445 \pgfluamathgetresult{pgfluamathfunctions.rad(#1)}}%
446 \def\pgfluamathdeg@#1{%
447 \pgfluamathgetresult{pgfluamathfunctions.deg(#1)}}%
448 \def\pgfluamathatan@#1{%
449 \pgfluamathgetresult{pgfluamathfunctions.aTan(#1)}}%
450 \def\pgfluamathatantwo@#1#2{%
451 \pgfluamathgetresult{pgfluamathfunctions.aTan2(#1,#2)}}%
452 \def\pgfluamathasin@#1{%
453 \pgfluamathgetresult{pgfluamathfunctions.aSin(#1)}}%
454 \def\pgfluamathacos@#1{%
455 \pgfluamathgetresult{pgfluamathfunctions.aCos(#1)}}%
456 \def\pgfluamathcot@#1{%
457 \pgfluamathgetresult{1/pgfluamathfunctions.Tan(#1)}}%
458 \def\pgfluamathsec@#1{%
459 \pgfluamathgetresult{1/pgfluamathfunctions.Cos(#1)}}%
460 \def\pgfluamathcosec@#1{%
461 \pgfluamathgetresult{1/pgfluamathfunctions.Sin(#1)}}%
462 \def\pgfluamathexp@#1{%
463 \pgfluamathgetresult{pgfluamathfunctions.exp(#1)}}%
464 \def\pgfluamathln@#1{%
465 \pgfluamathgetresult{pgfluamathfunctions.log(#1)}}%
466 \def\pgfluamathlogten@#1{%
467 \pgfluamathgetresult{pgfluamathfunctions.log10(#1)}}%
468 \def\pgfluamathsqrt@#1{%
469 \pgfluamathgetresult{pgfluamathfunctions.sqrt(#1)}}%
470 \def\pgfluamathrnd@{%
471 \pgfluamathgetresult{pgfluamathfunctions.rnd()}}%
472 \def\pgfluamathrand@{%
473 \pgfluamathgetresult{pgfluamathfunctions.rand(-1,1)}}%
474 \def\pgfluamathfactorial@#1{%
475 \pgfluamathgetresult{pgfluamathfunctions.factorial(#1)}}%
476 \def\pgfluamathreciprocal#1{%
477 \pgfluamathgetresult{1/#1}}%
478 % \pgfluamath@install@function\pgfmath@pi=\pgfluamathpi@%
479 % \pgfluamath@install@function\pgfmathpi=\pgfluamathpi@%
480 % \pgfluamath@install@function\pgfmathe@=\pgfluamathe@%
481 % \pgfluamath@install@function\pgfmathe=\pgfluamathe@%
482 % \pgfluamath@install@function\pgfmathlessthan@=\pgfluamathlessthan@%
483 % \pgfluamath@install@function\pgfmathless@=\pgfluamathlessthan@%
484 % \pgfluamath@install@function\pgfmathgreaterthan@=\pgfluamathgreaterthan@%
485 % \pgfluamath@install@function\pgfmathgreater@=\pgfluamathgreaterthan@%
486 % \pgfluamath@install@function\pgfmathpow@=\pgfluamathpow@
487 % \pgfluamath@install@function\pgfmathrand@=\pgfluamathrand@
488 % \pgfluamath@install@function\pgfmathrand=\pgfluamathrand@
489 % \pgfluamath@install@function\pgfmathrnd@=\pgfluamathrnd@
490 % \pgfluamath@install@function\pgfmathrnd=\pgfluamathrnd@
491 % \pgfluamath@install@function\pgfmathtrue@=\pgfluamathtrue@
492 % \pgfluamath@install@function\pgfmathfalse@=\pgfluamathfalse@
493 % \pgfluamath@install@function\pgfmathnot@=\pgfluamathnot@
494 % \pgfluamath@install@function\pgfmathhex@=\pgfluamathhex@
495 % \pgfluamath@install@function\pgfmathHex@=\pgfluamathHex@
496 % \pgfluamath@install@function\pgfmathoct@=\pgfluamathoct@
497 % \pgfluamath@install@function\pgfmathbin@=\pgfluamathbin@
498 % \pgfluamath@install@function\pgfmathand@=\pgfluamathand@
499 % \pgfluamath@install@function\pgfmathor@=\pgfluamathor@
500 % \pgfluamath@install@function\pgfmathfactorial@=\pgfluamathfactorial@
501 % \pgfluamath@install@function\pgfmathveclen@=\pgfluamathveclen@
502 % \pgfluamath@install@function\pgfmathcosh@=\pgfluamathcosh@
503 % \pgfluamath@install@function\pgfmathsinh@=\pgfluamathsinh@
504 % \pgfluamath@install@function\pgfmathtanh@=\pgfluamathtanh@
505
506 \def\pgfluamathpointnormalised#1{%
507 \pgf@process{#1}%
508 \pgfutil@directlua{%
509 pgfluamathfunctions.pointnormalised(%
510 \pgf@sys@tonumber{\pgf@x},\pgf@sys@tonumber{\pgf@y})}%
511 \ignorespaces}%
512
513 % Parser
514
515 \newif\ifpgfluamathunitsdeclared
516
517 % true if and only if LUA failed to evaluate the expression and the
518 % expression was evaluated by means of the TeX parser as fallback.
519 %
520 % This happens
521 % - for \chardef'ed boxes for which LUA lacks evaluation capabilities
522 % - if the expression includes some function which is unavailable in
523 % LUA (defined only in TeX)
524 % - some special cases which simply haven't been added to the LUA
525 % parser (yet).
526 % At the time of this writing, this includes
527 % -- arrays created via '{}' and indexed with '[]'
528 % -- strings with "<str>"
529 % -- 'scalar' function
530 % -- hex/octal/binary input
531 \newif\ifpgfluamathusedTeXfallback
532
533 % Parses the math expression and defines \pgfmathresult and
534 % \ifpgfmathunitsdeclared to contain the result.
535 %
536 % If \ifpgfluamathparseractive then the result is also assigned to
537 % \pgfmathresult. Furthermore, if the expression cannot be evaluated (because LUA does not support
538 % it), it will be evaluated by mean of TeX as fallback.
539 %
540 % @see \ifpgfluamathusedTeXfallback
541 % @see \ifpgfluamathshowerrormessage
542 \def\pgfluamathparse#1{%
543 \pgfluamathusedTeXfallbackfalse
544 \pgfutil@directlua{pgfluamathparser.texCallParser(
545 "\pgfutil@luaescapestring{#1}",
546 \pgfluamath@outputformat@choice,
547 \ifpgfluamathshowerrormessage true\else false\fi)%
548 }%
549 \ifx\pgfmathresult\pgfutil@empty
550 \ifpgfluamathenableTeXfallback
551 \pgfluamathusedTeXfallbacktrue
552 \pgfluamath@pgfmathparse{#1}%
553 \fi
554 \fi
555 }%
556
557 \catcode`\"=\pgfliblua@oldcatcodedoublequote
558 \endinput