Debian Codegraph @

texmf-dist/tex/generic/pgfplots/lua/pgfplotsoldpgfsupp/luamath/functions.lua

raw
1 --------------------------------------------------------------------------------------------------
2 ------ This file is a copy of some part of PGF/Tikz.
3 ------ It has been copied here to provide :
4 ------ - compatibility with older PGF versions
5 ------ - availability of PGF contributions by Christian Feuersaenger
6 ------ which are necessary or helpful for pgfplots.
7 ------
8 ------ For reasons of simplicity, I have copied the whole file, including own contributions AND
9 ------ PGF parts. The copyrights are as they appear in PGF.
10 ------
11 ------ Note that pgfplots has compatible licenses.
12 ------
13 ------ This copy has been modified in the following ways:
14 ------ - nested \input commands have been updated
15 ------
16 --
17 -- Support for the contents of this file will NOT be done by the PGF/TikZ team.
18 -- Please contact the author and/or maintainer of pgfplots (Christian Feuersaenger) if you need assistance in conjunction
19 -- with the deployment of this patch or partial content of PGF. Note that the author and/or maintainer of pgfplots has no obligation to fix anything:
20 -- This file comes without any warranty as the rest of pgfplots; there is no obligation for help.
21 ----------------------------------------------------------------------------------------------------
22 -- Date of this copy: Mi 6. Jan 11:32:04 CET 2016 ---
23
24
25
26 -- Copyright 2011 by Christophe Jorssen
27 --
28 -- This file may be distributed and/or modified
29 --
30 -- 1. under the LaTeX Project Public License and/or
31 -- 2. under the GNU Public License.
32 --
33 -- See the file doc/generic/pgf/licenses/LICENSE for more details.
34 --
35 -- $Id: functions.lua,v 1.3 2015/05/10 20:34:13 cfeuersaenger Exp $
36 --
37
38 local pgfluamathfunctions = pgfluamathfunctions or {}
39
40 -- Maps function names to their function.
41 --
42 -- Note that this allows to register functions which are not in pgfluamathfunctions.
43 --
44 -- Note that the string keys are not necessarily the same as the function
45 -- names. In particular, the math expression "not(1,1)" will execute notPGF(1,1)
46 --
47 -- Note that each function which is added to pgfluamathfunctions will _automatically_ be inserted into this map, see __newindex.
48 -- (I fear it will not be erased directly...)
49 pgfluamathfunctions.stringToFunctionMap = {}
50
51 local newFunctionAllocatedCallback = function(table,key,value)
52 local keyName = tostring(key):gsub("PGF","")
53 if not value then
54 stringToFunctionMap[keyName] = nil
55 elseif type(value) == 'function' then
56 -- remember it, and strip PGF suffix (i.e. remember 'not' instead of 'notPGF')
57 pgfluamathfunctions.stringToFunctionMap[keyName] = value
58 end
59 rawset(table,key,value)
60 end
61
62 setmetatable(pgfluamathfunctions, { __newindex = newFunctionAllocatedCallback })
63
64 local mathabs, mathacos, mathasin = math.abs, math.acos, math.asin
65 local mathatan, mathceil = math.atan, math.ceil
66 local mathcos, mathdeg = math.cos, math.deg
67 local mathexp, mathfloor, mathfmod = math.exp, math.floor, math.fmod
68 local mathlog, mathmax = math.log, math.max
69 local mathmin, mathpi = math.min, math.pi
70 local mathrad, mathrandom = math.rad, math.random
71 local mathrandomseed, mathsin = math.randomseed, math.sin
72 local mathsqrt = math.sqrt
73 local mathtan = math.tan
74
75 local trigFormatToRadians = mathrad
76
77 local radiansToTrigFormat = mathdeg
78
79 pgfluamathfunctions.TrigFormat = { 'deg', 'rad' }
80 pgfluamathfunctions.stringToFunctionMap["TrigFormat"] = nil
81
82 -- choice is one of the valid choices in TrigFormat.
83 function pgfluamathfunctions.setTrigFormat(choice)
84 if choice == 'deg' then
85 trigFormatToRadians = mathrad
86 radiansToTrigFormat = mathdeg
87 elseif choice == 'rad' then
88 local identity = function(x) return x end
89 trigFormatToRadians = identity
90 radiansToTrigFormat = identity
91 else
92 error("The argument '" .. tostring(choice) .. "' is no valid choice for setTrigFormat.")
93 end
94 end
95 pgfluamathfunctions.stringToFunctionMap["setTrigFormat"] = nil
96
97 pgfluamathfunctions.setRandomSeed = mathrandomseed
98 pgfluamathfunctions.stringToFunctionMap["setRandomSeed"] = nil
99
100 -------------------------------------------
101
102 function pgfluamathfunctions.add(x,y)
103 return x+y
104 end
105
106 function pgfluamathfunctions.subtract(x,y)
107 return x-y
108 end
109
110 function pgfluamathfunctions.neg(x)
111 return -x
112 end
113
114 function pgfluamathfunctions.multiply(x,y)
115 return x*y
116 end
117
118 function pgfluamathfunctions.veclen(x,y)
119 return mathsqrt(x*x+y*y)
120 end
121
122 function pgfluamathfunctions.divide(x,y)
123 return x/y
124 end
125
126 function pgfluamathfunctions.div(x,y)
127 return mathfloor(x/y)
128 end
129
130 function pgfluamathfunctions.pow(x,y)
131 -- do not use math.pow -- it is deprecated as of LUA 5.3
132 return x^y
133 end
134
135 function pgfluamathfunctions.factorial(x)
136 -- TODO: x must be an integer
137 if x == 0 then
138 return 1
139 else
140 return x * pgfluamathfunctions.factorial(x-1)
141 end
142 end
143
144 function pgfluamathfunctions.ifthenelse(x,y,z)
145 if x~= 0 then
146 return y
147 else
148 return z
149 end
150 end
151
152 function pgfluamathfunctions.equal(x,y)
153 if x == y then
154 return 1
155 else
156 return 0
157 end
158 end
159
160 function pgfluamathfunctions.greater(x,y)
161 if x > y then
162 return 1
163 else
164 return 0
165 end
166 end
167
168 function pgfluamathfunctions.less(x,y)
169 if x < y then
170 return 1
171 else
172 return 0
173 end
174 end
175
176 function pgfluamathfunctions.min(x,y)
177 return mathmin(x,y)
178 end
179
180 function pgfluamathfunctions.max(x,y)
181 return mathmax(x,y)
182 end
183
184 function pgfluamathfunctions.notequal(x,y)
185 if x ~= y then
186 return 1
187 else
188 return 0
189 end
190 end
191
192 function pgfluamathfunctions.notless(x,y)
193 if x >= y then
194 return 1
195 else
196 return 0
197 end
198 end
199
200 function pgfluamathfunctions.notgreater(x,y)
201 if x <= y then
202 return 1
203 else
204 return 0
205 end
206 end
207
208 function pgfluamathfunctions.andPGF(x,y)
209 if (x ~= 0) and (y ~= 0) then
210 return 1
211 else
212 return 0
213 end
214 end
215
216 function pgfluamathfunctions.orPGF(x,y)
217 if (x ~= 0) or (y ~= 0) then
218 return 1
219 else
220 return 0
221 end
222 end
223
224 function pgfluamathfunctions.notPGF(x)
225 if x == 0 then
226 return 1
227 else
228 return 0
229 end
230 end
231
232 function pgfluamathfunctions.pi()
233 return mathpi
234 end
235
236 function pgfluamathfunctions.e()
237 return mathexp(1)
238 end
239
240 function pgfluamathfunctions.abs(x)
241 return mathabs(x)
242 end
243
244 function pgfluamathfunctions.floor(x)
245 return mathfloor(x)
246 end
247
248 function pgfluamathfunctions.ceil(x)
249 return mathceil(x)
250 end
251
252 function pgfluamathfunctions.exp(x)
253 return mathexp(x)
254 end
255
256 function pgfluamathfunctions.ln(x)
257 return mathlog(x)
258 end
259
260 local logOf10 = mathlog(10)
261 function pgfluamathfunctions.log10(x)
262 return mathlog(x) / logOf10
263 end
264
265 local logOf2 = mathlog(2)
266 function pgfluamathfunctions.log2(x)
267 return mathlog(x) / logOf2
268 end
269
270 function pgfluamathfunctions.sqrt(x)
271 return mathsqrt(x)
272 end
273
274 function pgfluamathfunctions.sign(x)
275 if x < 0 then
276 return -1.0
277 elseif x == 0 then
278 return 0.0
279 else
280 return 1.0
281 end
282 end
283 function pgfluamathfunctions.real(x)
284 -- "ensure that x contains a decimal point" is kind of a no-op here, isn't it!?
285 return x
286 end
287
288 function pgfluamathfunctions.rnd()
289 return mathrandom()
290 end
291
292 function pgfluamathfunctions.rand()
293 return -1 + mathrandom() *2
294 end
295
296 function pgfluamathfunctions.random(x,y)
297 if x == nil and y == nil then
298 return mathrandom()
299 elseif y == nil then
300 return mathrandom(x)
301 else
302 return mathrandom(x,y)
303 end
304 end
305
306 function pgfluamathfunctions.deg(x)
307 return mathdeg(x)
308 end
309
310 function pgfluamathfunctions.rad(x)
311 return mathrad(x)
312 end
313
314 function pgfluamathfunctions.round(x)
315 if x<0 then
316 return -mathfloor(mathabs(x)+0.5)
317 else
318 return mathfloor(x + 0.5)
319 end
320 end
321
322 function pgfluamathfunctions.gcd(a, b)
323 if b == 0 then
324 return a
325 else
326 return pgfluamathfunctions.gcd(b, a%b)
327 end
328 end
329
330 function pgfluamathfunctions.isprime(a)
331 local ifisprime = true
332 if a == 1 then
333 ifisprime = false
334 elseif a == 2 then
335 ifisprime = true
336 -- if a > 2 then
337 else
338 local i, imax = 2, mathceil(mathsqrt(a)) + 1
339 while ifisprime and (i < imax) do
340 if pgfluamathfunctions.gcd(a,i) ~= 1 then
341 ifisprime = false
342 end
343 i = i + 1
344 end
345 end
346 if ifisprime then
347 return 1
348 else
349 return 0
350 end
351 end
352
353
354 function pgfluamathfunctions.split_braces_to_explist(s)
355 -- (Thanks to mpg and zappathustra from fctt)
356 -- Make unpack available whatever lua version is used
357 -- (unpack in lua 5.1 table.unpack in lua 5.2)
358 local unpack = table.unpack or unpack
359 local t = {}
360 for i in s:gmatch('%b{}') do
361 table.insert(t, tonumber(i:sub(2, -2)))
362 end
363 return unpack(t)
364 end
365
366 function pgfluamathfunctions.split_braces_to_table(s)
367 local t = {}
368 for i in s:gmatch('%b{}') do
369 table.insert(t, tonumber(i:sub(2, -2)))
370 end
371 return t
372 end
373
374 function pgfluamathfunctions.mathtrue()
375 return 1.0
376 end
377 pgfluamathfunctions.stringToFunctionMap["true"] = pgfluamathfunctions.mathtrue
378
379 function pgfluamathfunctions.mathfalse()
380 return 0.0
381 end
382 pgfluamathfunctions.stringToFunctionMap["false"] = pgfluamathfunctions.mathfalse
383
384 function pgfluamathfunctions.frac(a)
385 -- should be positive, apparently
386 return mathabs(a - pgfluamathfunctions.int(a))
387 end
388
389 function pgfluamathfunctions.int(a)
390 if a < 0 then
391 return -mathfloor(mathabs(a))
392 else
393 return mathfloor(a)
394 end
395 end
396
397 function pgfluamathfunctions.iseven(a)
398 if (a % 2) == 0 then
399 return 1.0
400 else
401 return 0.0
402 end
403 end
404
405 function pgfluamathfunctions.isodd(a)
406 if (a % 2) == 0 then
407 return 0.0
408 else
409 return 1.0
410 end
411 end
412
413 function pgfluamathfunctions.mod(x,y)
414 if x/y < 0 then
415 return -(mathabs(x)%mathabs(y))
416 else
417 return mathabs(x)%mathabs(y)
418 end
419 end
420
421 function pgfluamathfunctions.Mod(x,y)
422 local tmp = pgfluamathfunctions.mod(x,y)
423 if tmp < 0 then
424 tmp = tmp + y
425 end
426 return tmp
427 end
428
429 function pgfluamathfunctions.Sin(x)
430 return mathsin(trigFormatToRadians(x))
431 end
432 pgfluamathfunctions.sin=pgfluamathfunctions.Sin
433
434
435 function pgfluamathfunctions.cosh(x)
436 -- math.cosh is deprecated as of LUA 5.3 . reimplement it:
437 return 0.5* (mathexp(x) + mathexp(-x))
438 end
439 function pgfluamathfunctions.sinh(x)
440 -- math.sinh is deprecated as of LUA 5.3 . reimplement it:
441 return 0.5* (mathexp(x) - mathexp(-x))
442 end
443
444 local sinh = pgfluamathfunctions.sinh
445 local cosh = pgfluamathfunctions.cosh
446 function pgfluamathfunctions.tanh(x)
447 -- math.tanh is deprecated as of LUA 5.3 . reimplement it:
448 return sinh(x)/cosh(x)
449 end
450
451 function pgfluamathfunctions.Cos(x)
452 return mathcos(trigFormatToRadians(x))
453 end
454 pgfluamathfunctions.cos=pgfluamathfunctions.Cos
455
456 function pgfluamathfunctions.Tan(x)
457 return mathtan(trigFormatToRadians(x))
458 end
459 pgfluamathfunctions.tan=pgfluamathfunctions.Tan
460
461 function pgfluamathfunctions.aSin(x)
462 return radiansToTrigFormat(mathasin(x))
463 end
464 pgfluamathfunctions.asin=pgfluamathfunctions.aSin
465
466 function pgfluamathfunctions.aCos(x)
467 return radiansToTrigFormat(mathacos(x))
468 end
469 pgfluamathfunctions.acos=pgfluamathfunctions.aCos
470
471 function pgfluamathfunctions.aTan(x)
472 return radiansToTrigFormat(mathatan(x))
473 end
474 pgfluamathfunctions.atan=pgfluamathfunctions.aTan
475
476 local mathatan2
477 if math.atan2 == nil then
478 -- math.atan2 has been deprecated since LUA 5.3
479 mathatan2 = function (y,x) return mathatan(y,x) end
480 else
481 mathatan2 = math.atan2
482 end
483
484 function pgfluamathfunctions.aTan2(y,x)
485 return radiansToTrigFormat(mathatan2(y,x))
486 end
487 pgfluamathfunctions.atan2=pgfluamathfunctions.aTan2
488 pgfluamathfunctions.atantwo=pgfluamathfunctions.aTan2
489
490 function pgfluamathfunctions.cot(x)
491 return pgfluamathfunctions.cos(x) / pgfluamathfunctions.sin(x)
492 end
493 function pgfluamathfunctions.sec(x)
494 return 1 / pgfluamathfunctions.cos(x)
495 end
496 function pgfluamathfunctions.cosec(x)
497 return 1 / pgfluamathfunctions.sin(x)
498 end
499
500 function pgfluamathfunctions.pointnormalised (pgfx, pgfy)
501 local pgfx_normalised, pgfy_normalised
502 if pgfx == 0. and pgfy == 0. then
503 -- Original pgf macro gives this result
504 tex.dimen['pgf@x'] = "0pt"
505 tex.dimen['pgf@y'] = "1pt"
506 else
507 pgfx_normalised = pgfx/math.sqrt(pgfx^2 + pgfy^2)
508 pgfx_normalised = pgfx_normalised - pgfx_normalised%0.00001
509 pgfy_normalised = pgfy/math.sqrt(pgfx^2 + pgfy^2)
510 pgfy_normalised = pgfy_normalised - pgfy_normalised%0.00001
511 tex.dimen['pgf@x'] = tostring(pgfx_normalised) .. "pt"
512 tex.dimen['pgf@y'] = tostring(pgfy_normalised) .. "pt"
513 end
514 return nil
515 end
516
517 local isnan = function(x)
518 return x ~= x
519 end
520
521 pgfluamathfunctions.isnan = isnan
522
523 local infty = 1/0
524 pgfluamathfunctions.infty = infty
525
526 local nan = math.sqrt(-1)
527 pgfluamathfunctions.nan = nan
528
529 local stringlen = string.len
530 local globaltonumber = tonumber
531 local stringsub=string.sub
532 local stringformat = string.format
533 local stringsub = string.sub
534
535 -- like tonumber(x), but it also accepts nan, inf, infty, and the TeX FPU format
536 function pgfluamathfunctions.tonumber(x)
537 if type(x) == 'number' then return x end
538 if not x then return x end
539
540 local len = stringlen(x)
541 local result = globaltonumber(x)
542 if not result then
543 if len >2 and stringsub(x,2,2) == 'Y' and stringsub(x,len,len) == ']' then
544 -- Ah - some TeX FPU input of the form 1Y1.0e3] . OK. transform it
545 local flag = stringsub(x,1,1)
546 if flag == '0' then
547 -- ah, 0.0
548 result = 0.0
549 elseif flag == '1' then
550 result = globaltonumber(stringsub(x,3, len-1))
551 elseif flag == '2' then
552 result = globaltonumber("-" .. stringsub(x,3, len-1))
553 elseif flag == '3' then
554 result = nan
555 elseif flag == '4' then
556 result = infty
557 elseif flag == '5' then
558 result = -infty
559 end
560 else
561 local lower = x:lower()
562 if lower == 'nan' then
563 result = nan
564 elseif lower == "-nan" then
565 result = nan
566 elseif lower == 'inf' or lower == 'infty' then
567 result = infty
568 elseif lower == '-inf' or lower == '-infty' then
569 result = -infty
570 end
571 end
572 end
573
574 return result
575 end
576
577 local stringlen = string.len
578 local globaltonumber = tonumber
579 local stringformat = string.format
580 local stringsub = string.sub
581 local stringfind = string.find
582 local stringbyte = string.byte
583 local NULL_CHAR = string.byte("0",1)
584
585 local function discardTrailingZeros(x)
586 local result = x
587 -- printf is too stupid: I would like to have
588 -- 1. a fast method
589 -- 2. a reliable method
590 -- 3. full precision of x
591 -- 4. a fixed point representation
592 -- the 'f' modifier has trailing zeros (stupid!)
593 -- the 'g' modified can switch to scientific notation (no-go!)
594 local periodOff = stringfind(result, '.',1,true)
595 if periodOff ~= nil then
596 -- strip trailing zeros
597 local chars = { stringbyte(result,1,#result) };
598 local lastNonZero = #chars
599 for i = #chars, periodOff, -1 do
600 if chars[i] ~= NULL_CHAR then lastNonZero=i; break; end
601 end
602 if lastNonZero ~= #chars then
603 -- Ah: we had at least one trailing zero.
604 -- discard all but the last.
605 lastNonZero = mathmax(periodOff+1,lastNonZero)
606 end
607 result = stringsub(result, 1, lastNonZero)
608 end
609 return result;
610 end
611
612 local function discardTrailingZerosFromMantissa(x)
613 local mantissaStart = stringfind(x, "e")
614
615 local mantissa = stringsub(x,1,mantissaStart-1)
616 local exponent = stringsub(x,mantissaStart)
617
618 return discardTrailingZeros(mantissa) .. exponent
619 end
620
621
622 -- a helper function which has no catcode issues when communicating with TeX:
623 function pgfluamathfunctions.tostringfixed(x)
624 if x == nil then
625 return ""
626 end
627
628 return discardTrailingZeros(stringformat("%f", x))
629 end
630
631 -- converts an input number to a string which is accepted by the TeX FPU
632 function pgfluamathfunctions.toTeXstring(x)
633 local result = ""
634 if x ~= nil then
635 if x == infty then result = "4Y0.0e0]"
636 elseif x == -infty then result = "5Y0.0e0]"
637 elseif isnan(x) then result = "3Y0.0e0]"
638 elseif x == 0 then result = "0Y0.0e0]"
639 else
640 result = discardTrailingZerosFromMantissa(stringformat("%.10e", x))
641 if x > 0 then
642 result = "1Y" .. result .. "]"
643 else
644 result = "2Y" .. stringsub(result,2) .. "]"
645 end
646 end
647 end
648 return result
649 end
650
651 return pgfluamathfunctions