1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
-- Copyright 2011 by Christophe Jorssen
--
-- This file may be distributed and/or modified
--
-- 1. under the LaTeX Project Public License and/or
-- 2. under the GNU Public License.
--
-- See the file doc/generic/pgf/licenses/LICENSE for more details.
--
-- $Id: pgfluamath.functions.lua,v 1.9 2013/07/25 10:39:34 tantau Exp $
--
local pgfluamathfunctions = pgfluamathfunctions or {}
local mathabs, mathacos, mathasin = math.abs, math.acos, math.asin
local mathatan, mathatan2, mathceil = math.atan, math.atan2, math.ceil
local mathcos, mathcosh, mathdeg = math.cos, math.cosh, math.deg
local mathexp, mathfloor, mathfmod = math.exp, math.floor, math.fmod
local mathfrexp, mathhuge, mathldexp = math.frexp, math.huge, math.ldexp
local mathlog, mathlog10, mathmax = math.log, math.log10, math.max
local mathmin, mathmodf, mathpi = math.min, math.modf, math.pi
local mathpow, mathrad, mathrandom = math.pow, math.rad, math.random
local mathrandomseed, mathsin = math.randomseed, math.sin
local mathsinh, mathsqrt, mathtanh = math.sinh, math.sqrt, math.tanh
local mathtan = math.tan
function pgfluamathfunctions.add(x,y)
return x+y
end
function pgfluamathfunctions.substract(x,y)
return x-y
end
function pgfluamathfunctions.neg(x)
return -x
end
function pgfluamathfunctions.multiply(x,y)
return x*y
end
function pgfluamathfunctions.divide(x,y)
return x/y
end
function pgfluamathfunctions.pow(x,y)
return mathpow(x,y)
end
function pgfluamathfunctions.factorial(x)
-- TODO: x must be an integer
if x == 0 then
return 1
else
return x * factorial(x-1)
end
end
function pgfluamathfunctions.deg(x)
return mathdeg(x)
end
function pgfluamathfunctions.ifthenelse(x,y,z)
if x~= 0 then
return y
else
return z
end
end
function pgfluamathfunctions.equal(x,y)
if x == y then
return 1
else
return 0
end
end
function pgfluamathfunctions.greater(x,y)
if x > y then
return 1
else
return 0
end
end
function pgfluamathfunctions.less(x,y)
if x < y then
return 1
else
return 0
end
end
function pgfluamathfunctions.notequal(x,y)
if x ~= y then
return 1
else
return 0
end
end
function pgfluamathfunctions.notless(x,y)
if x >= y then
return 1
else
return 0
end
end
function pgfluamathfunctions.notgreater(x,y)
if x <= y then
return 1
else
return 0
end
end
function pgfluamathfunctions.andPGF(x,y)
if (x ~= 0) and (y ~= 0) then
return 1
else
return 0
end
end
function pgfluamathfunctions.orPGF(x,y)
if (x ~= 0) or (y ~= 0) then
return 1
else
return 0
end
end
function pgfluamathfunctions.notPGF(x)
if x == 0 then
return 1
else
return 0
end
end
function pgfluamathfunctions.pi()
return mathpi
end
function pgfluamathfunctions.e()
return mathexp(1)
end
function pgfluamathfunctions.abs(x)
return mathabs(x)
end
function pgfluamathfunctions.floor(x)
return mathfloor(x)
end
function pgfluamathfunctions.ceil(x)
return mathceil(x)
end
function pgfluamathfunctions.exp(x)
return mathexp(x)
end
function pgfluamathfunctions.log(x)
return mathlog(x)
end
function pgfluamathfunctions.log10(x)
return mathlog10(x)
end
function pgfluamathfunctions.sqrt(x)
return mathsqrt(x)
end
function pgfluamathfunctions.rnd()
return mathrandom()
end
function pgfluamathfunctions.rand()
return mathrandom(-1,1)
end
function pgfluamathfunctions.deg(x)
return mathdeg(x)
end
function pgfluamathfunctions.rad(x)
return mathrad(x)
end
function pgfluamathfunctions.round(x)
if x<0 then
return -mathceil(mathabs(x))
else
return mathceil(x)
end
end
function pgfluamathfunctions.gcd(a, b)
if b == 0 then
return a
else
return gcd(b, a%b)
end
end
function pgfluamathfunctions.isprime(a)
local ifisprime = true
if a == 1 then
ifisprime = false
elseif a == 2 then
ifisprime = true
-- if a > 2 then
else
local i, imax = 2, mathceil(mathsqrt(a)) + 1
while ifisprime and (i < imax) do
if gcd(a,i) ~= 1 then
ifisprime = false
end
i = i + 1
end
end
if ifisprime then
return 1
else
return 0
end
end
function pgfluamathfunctions.split_braces_to_explist(s)
-- (Thanks to mpg and zappathustra from fctt)
-- Make unpack available whatever lua version is used
-- (unpack in lua 5.1 table.unpack in lua 5.2)
local unpack = table.unpack or unpack
local t = {}
for i in s:gmatch('%b{}') do
table.insert(t, tonumber(i:sub(2, -2)))
end
return unpack(t)
end
function pgfluamathfunctions.split_braces_to_table(s)
local t = {}
for i in s:gmatch('%b{}') do
table.insert(t, tonumber(i:sub(2, -2)))
end
return t
end
function pgfluamathfunctions.mod(x,y)
if x/y < 0 then
return -(mathabs(x)%mathabs(y))
else
return mathabs(x)%mathabs(y)
end
end
function pgfluamathfunctions.Mod(x,y)
return mathabs(x)%mathabs(y)
end
function pgfluamathfunctions.Sin(x)
return mathsin(mathrad(x))
end
function pgfluamathfunctions.Cos(x)
return mathcos(mathrad(x))
end
function pgfluamathfunctions.Tan(x)
return mathtan(mathrad(x))
end
function pgfluamathfunctions.aSin(x)
return mathdeg(mathasin(x))
end
function pgfluamathfunctions.aCos(x)
return mathdeg(mathacos(x))
end
function pgfluamathfunctions.aTan(x)
return mathdeg(mathatan(x))
end
function pgfluamathfunctions.aTan2(y,x)
return mathdeg(mathatan2(y,x))
end
function pgfluamathfunctions.pointnormalised (pgfx, pgfy)
local pgfx_normalised, pgfy_normalised
if pgfx == 0. and pgfy == 0. then
-- Orginal pgf macro gives this result
tex.dimen['pgf@x'] = "0pt"
tex.dimen['pgf@y'] = "1pt"
else
pgfx_normalised = pgfx/math.sqrt(pgfx^2 + pgfy^2)
pgfx_normalised = pgfx_normalised - pgfx_normalised%0.00001
pgfy_normalised = pgfy/math.sqrt(pgfx^2 + pgfy^2)
pgfy_normalised = pgfy_normalised - pgfy_normalised%0.00001
tex.dimen['pgf@x'] = tostring(pgfx_normalised) .. "pt"
tex.dimen['pgf@y'] = tostring(pgfy_normalised) .. "pt"
end
return nil
end
return pgfluamathfunctions
|