Debian Codegraph @

texmf-dist/tex/latex/tkz-elements/tkz_elements_function.lua

raw
1 -- tkz_elements_function.lua
2 -- Copyright (c) 2026 Alain Matthes
3 -- SPDX-License-Identifier: LPPL-1.3c
4 -- Maintainer: Alain Matthes
5
6
7 -- Usage:
8 -- F.f = fct("sin(x)+x^2", math)
9 -- y = F.f:eval(1.2)
10 -- PA.c = F.f:path(-2, 5, 200)
11
12
13 fct = {}
14 fct.__index = fct
15
16 -- Constructor
17 function fct:new(expr_or_fn)
18 local o = setmetatable({}, self)
19 o.expr = nil
20 o.fn = nil
21 o.env = math -- environnement implicite
22 if type(expr_or_fn) == "function" then
23 o.fn = expr_or_fn
24 else
25 o.expr = tostring(expr_or_fn or "0")
26 end
27 return o
28 end
29
30 setmetatable(fct, { __call = function(_, ...) return fct:new(...) end })
31
32 -- Compile expression if needed
33 function fct:compile()
34 if self.fn then return self.fn end
35
36 local chunk = "return function(x) return (" .. self.expr .. ") end"
37 local loader, err = load(chunk, "tkz.fct", "t", self.env)
38 if not loader then
39 if tex and tex.error then
40 tex.error("Invalid function expression", { self.expr, err })
41 end
42 self.fn = function(_) return 0 end
43 return self.fn
44 end
45
46 local ok, fn_or_err = pcall(loader)
47 if not ok or type(fn_or_err) ~= "function" then
48 if tex and tex.error then
49 tex.error("Invalid function expression", { self.expr, fn_or_err })
50 end
51 self.fn = function(_) return 0 end
52 return self.fn
53 end
54
55 self.fn = fn_or_err
56 return self.fn
57 end
58
59 -- Evaluate f(x)
60 function fct:eval(x)
61 x = tonumber(x)
62 if not x then return 0 end
63 local f = self:compile()
64 local ok, y = pcall(f, x)
65 if not ok then return 0 end
66 y = tonumber(y)
67 if not y or y ~= y or y == math.huge or y == -math.huge then
68 return 0
69 end
70 return y
71 end
72
73
74
75 function fct:path(xmin, xmax, n)
76 xmin = tonumber(xmin) or 0
77 xmax = tonumber(xmax) or 1
78 n = tonumber(n) or 100
79 if n <= 0 then n = 1 end
80
81 local p = path()
82 local f = self:compile()
83 local step = (xmax - xmin) / n
84
85 for i = 0, n do
86 local x = xmin + i * step
87 local ok, y = pcall(f, x)
88 if ok then
89 y = tonumber(y)
90 if y and y == y and y ~= math.huge and y ~= -math.huge then
91 -- on ajoute le point AVEC la même logique que fct:point
92 local sx = self.sx or 1
93 local sy = self.sy or 1
94 p:add_point(point(sx * x, sy * y))
95 -- (ou bien: p:add_point(self:point(x)) mais ça recalculerait eval(x))
96 end
97 end
98 end
99
100 return p
101 end
102
103 function fct:set_scale(sx, sy)
104 self.sx = tonumber(sx) or 1
105 self.sy = tonumber(sy) or 1
106 return self
107 end
108
109
110
111 -- ------------------------------------------------------------
112 -- scalar evaluations
113 -- ------------------------------------------------------------
114
115 function fct:point(x)
116 x = tonumber(x) or 0
117 local y = self:eval(x)
118 local sx = self.sx or 1
119 local sy = self.sy or 1
120 return point(sx * x, sy * y)
121 end
122
123
124 function fct:half_tangents(x0, h)
125 h = h or (1e-4 * (1 + math.abs(x0)))
126
127 local P = self:point(x0)
128 local X0, Y0 = P:get()
129
130 local Xp, Yp = self:point(x0 + h):get()
131 local Xm, Ym = self:point(x0 - h):get()
132
133 local mr = (Yp - Y0) / (Xp - X0)
134 local ml = (Y0 - Ym) / (X0 - Xm)
135
136 local function unit_point(m, dir)
137 local d = 1 / math.sqrt(1 + m*m)
138 return point(X0 + dir*d, Y0 + dir*m*d)
139 end
140
141 local Pl = unit_point(ml, -1)
142 local Pr = unit_point(mr, 1)
143
144 return Pl, Pr, ml, mr
145 end
146
147
148
149 function fct:tangent(x0, h, tol)
150 tol = tol or 1e-3
151 local Pl, Pr, ml, mr = self:half_tangents(x0, h)
152
153 if math.abs(mr - ml) > tol then
154 return Pl, Pr
155 end
156
157 local P = self:point(x0)
158 local X0, Y0 = P:get()
159
160 local m = 0.5 * (ml + mr)
161 local d = 1 / math.sqrt(1 + m*m)
162
163 local Pr2 = point(X0 + d, Y0 + m*d)
164 local Pl2 = point(X0 - d, Y0 - m*d)
165
166 return Pl2, Pr2
167 end
168
169
170 --
171 -- return fct