| 1 |
-- File: tkz_elements_tkz.lua |
| 2 |
-- Copyright (c) 2026 Alain Matthes |
| 3 |
-- SPDX-License-Identifier: LPPL-1.3c |
| 4 |
-- Maintainer: Alain Matthes |
| 5 |
|
| 6 |
-- tkz-settings and math |
| 7 |
|
| 8 |
tkz = tkz or {} |
| 9 |
tkz.nb_dec = 10 |
| 10 |
tkz.epsilon = 0.1 ^ tkz.nb_dec |
| 11 |
tkz.dc = 2 |
| 12 |
tkz.phi = (1 + math.sqrt(5)) / 2 -- golden number φ |
| 13 |
tkz.invphi = (math.sqrt(5) - 1) / 2 |
| 14 |
tkz.sqrtphi = math.sqrt(tkz.phi) |
| 15 |
tkz.rad = 180 / math.pi |
| 16 |
tkz.deg = math.pi / 180 |
| 17 |
tkz.pt = 254 / 7227 |
| 18 |
tkz.tau = 2 * math.pi |
| 19 |
tkz.maxdimen_pt = 16383.99999 |
| 20 |
tkz.max_coord_cm = tkz.maxdimen_pt * tkz.pt -- ≈ 575.8 cm |
| 21 |
tkz.max_coord_safe = 0.9 * tkz.max_coord_cm -- ≈ 518 cm |
| 22 |
|
| 23 |
tkz.has_coord_warning = false |
| 24 |
tkz.coord_warnings = {} |
| 25 |
|
| 26 |
if not tkz._seeded then |
| 27 |
math.randomseed(os.time()) |
| 28 |
tkz._seeded = true |
| 29 |
end |
| 30 |
|
| 31 |
|
| 32 |
function tkz.register_coord_warning(name, x, y) |
| 33 |
tkz.has_coord_warning = true |
| 34 |
table.insert(tkz.coord_warnings, { |
| 35 |
name = name, |
| 36 |
x = x, |
| 37 |
y = y, |
| 38 |
limit = tkz.max_coord_safe |
| 39 |
}) |
| 40 |
end |
| 41 |
|
| 42 |
function tkz.flush_coord_warnings() |
| 43 |
if not tkz.has_coord_warning or #tkz.coord_warnings == 0 then |
| 44 |
return |
| 45 |
end |
| 46 |
|
| 47 |
local bs = string.char(92) |
| 48 |
|
| 49 |
for _, w in ipairs(tkz.coord_warnings) do |
| 50 |
local msg = string.format( |
| 51 |
"Point `%s' has very large coordinates (%.6f, %.6f). " .. |
| 52 |
"Safe limit is about |x|,|y| <= %.2f (TikZ units).", |
| 53 |
w.name, w.x, w.y, w.limit |
| 54 |
) |
| 55 |
|
| 56 |
-- 1) Log / terminal |
| 57 |
texio.write_nl("term and log", |
| 58 |
"tkz-elements warning (coordinates): " .. msg) |
| 59 |
|
| 60 |
-- 2) Warning LaTeX (hors tikzpicture si on appelle flush après) |
| 61 |
tex.print( |
| 62 |
bs .. "PackageWarning{tkz-elements}{" .. msg .. "}" |
| 63 |
) |
| 64 |
end |
| 65 |
|
| 66 |
tkz.has_coord_warning = false |
| 67 |
tkz.coord_warnings = {} |
| 68 |
end |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
-- real |
| 73 |
function tkz.approx(x, y, EPS) |
| 74 |
EPS = EPS or tkz.epsilon |
| 75 |
return math.abs(x - y) <= EPS |
| 76 |
end |
| 77 |
|
| 78 |
function tkz.midpoint(a, b) |
| 79 |
return (a + b) / 2 |
| 80 |
end |
| 81 |
|
| 82 |
function tkz.reset_defaults() |
| 83 |
tkz.nb_dec = 10 |
| 84 |
tkz.epsilon = 0.1 ^ tkz.nb_dec |
| 85 |
tkz.dc = 2 |
| 86 |
end |
| 87 |
|
| 88 |
function tkz.set_nb_dec(n) |
| 89 |
tkz.nb_dec = n |
| 90 |
tkz.epsilon = 0.1 ^ n |
| 91 |
end |
| 92 |
|
| 93 |
function tkz.midpoints(...) |
| 94 |
return midpoints_(...) |
| 95 |
end |
| 96 |
|
| 97 |
function tkz.length(a, b) |
| 98 |
return point.abs(a - b) |
| 99 |
end |
| 100 |
|
| 101 |
function tkz.parabola(a, b, c) --bug local |
| 102 |
local xa, xb, xc, ya, yb, yc |
| 103 |
xa = a.re |
| 104 |
ya = a.im |
| 105 |
xb = b.re |
| 106 |
yb = b.im |
| 107 |
xc = c.re |
| 108 |
yc = c.im |
| 109 |
return parabola_(xa, ya, xb, yb, xc, yc) |
| 110 |
end |
| 111 |
----------------------------------------- |
| 112 |
-------------- angles ------------------- |
| 113 |
----------------------------------------- |
| 114 |
function tkz.get_angle(a, b, c) |
| 115 |
return get_angle_(a, b, c) |
| 116 |
end |
| 117 |
|
| 118 |
function tkz.inner_angle(pa, pb, pc) |
| 119 |
return inner_angle_(pa, pb, pc) |
| 120 |
end |
| 121 |
|
| 122 |
function tkz.angle_normalize(an) |
| 123 |
return angle_normalize_(an) |
| 124 |
end |
| 125 |
|
| 126 |
function tkz.is_direct(pa, pb, pc) |
| 127 |
return get_angle_(pa, pb, pc) > 0 |
| 128 |
end |
| 129 |
|
| 130 |
function tkz.get_angle_normalize(a, b, c) |
| 131 |
return get_angle_normalize_(a, b, c) |
| 132 |
end |
| 133 |
|
| 134 |
function tkz.angle_between_vectors(a, b, c, d) |
| 135 |
return tkz_angle_between_vectors_(a, b, c, d) |
| 136 |
end |
| 137 |
|
| 138 |
|
| 139 |
function tkz.solve_linear_system(M, N) |
| 140 |
if not M or not N then |
| 141 |
return nil, "Error: Input matrices are nil." |
| 142 |
end |
| 143 |
|
| 144 |
local m, n = M.rows, M.cols |
| 145 |
|
| 146 |
if N.rows ~= m or N.cols ~= 1 then |
| 147 |
return nil, "Error: Incompatible dimensions between M and N." |
| 148 |
end |
| 149 |
|
| 150 |
local Aug = {} |
| 151 |
|
| 152 |
-- Construction of the augmented matrix |
| 153 |
for i = 1, m do |
| 154 |
Aug[i] = {} |
| 155 |
for j = 1, n do |
| 156 |
Aug[i][j] = M.set[i][j] |
| 157 |
end |
| 158 |
Aug[i][n + 1] = N.set[i][1] -- Add column vector |
| 159 |
end |
| 160 |
|
| 161 |
local MA = matrix:new(Aug) |
| 162 |
|
| 163 |
local rankM = M:rank() |
| 164 |
local rankMA = MA:rank() |
| 165 |
|
| 166 |
-- System compatibility check |
| 167 |
if rankM < rankMA then |
| 168 |
return nil, "Incompatible system (no solution)" |
| 169 |
elseif rankM < n then |
| 170 |
return nil, "Indeterminate system (infinite solutions)" |
| 171 |
end |
| 172 |
|
| 173 |
-- Résolution par Gauss-Jordan |
| 174 |
local sol = MA:gauss_jordan() |
| 175 |
|
| 176 |
-- |
| 177 |
local S = {} |
| 178 |
for i = 1, m do |
| 179 |
S[i] = { sol.set[i][n + 1] } -- n + 1 car n=3, on veut la colonne 4 |
| 180 |
end |
| 181 |
|
| 182 |
return matrix:new(S) |
| 183 |
end |
| 184 |
|
| 185 |
function tkz.bisector(a, b, c) |
| 186 |
local i = in_center_(a, b, c) |
| 187 |
return line:new(a, intersection_ll_(a, i, b, c)) |
| 188 |
end |
| 189 |
|
| 190 |
function tkz.altitude(a, b, c) |
| 191 |
local o, p |
| 192 |
-- Get the orthocenter (which is the point of concurrency of the altitudes) |
| 193 |
o = ortho_center_(a, b, c) |
| 194 |
|
| 195 |
-- Get the perpendicular projection of point 'a' onto the line defined by 'b' and 'c' |
| 196 |
p = projection_(b, c, a) |
| 197 |
|
| 198 |
-- Return the altitude, which is the line from point 'a' to the point 'p' |
| 199 |
return line:new(a, p) |
| 200 |
end |
| 201 |
|
| 202 |
function tkz.bisector_ext(a, b, c) -- n=1 swap n=2 swap 2 |
| 203 |
local i, p |
| 204 |
-- Get the incenter of the triangle |
| 205 |
i = in_center_(a, b, c) |
| 206 |
|
| 207 |
-- Rotate the incenter by 90 degrees around point 'a' to compute the external bisector |
| 208 |
p = rotation_(a, math.pi / 2, i) |
| 209 |
|
| 210 |
-- Return the external bisector as the line passing through points 'a' and 'p' |
| 211 |
return line:new(a, p) |
| 212 |
end |
| 213 |
|
| 214 |
function tkz.trisector(pa, pb, pc) |
| 215 |
if tkz.is_direct(pa, pb, pc) then |
| 216 |
local a = get_angle_(pa, pb, pc) |
| 217 |
local i = report_(pa, pb, 1) |
| 218 |
local j = rotation_(pa, a / 3, i) |
| 219 |
local k = rotation_(pa, a / 3, j) |
| 220 |
return j, k |
| 221 |
else |
| 222 |
tex.error("angle must be direct") |
| 223 |
tex.print("stop") |
| 224 |
end |
| 225 |
end |
| 226 |
|
| 227 |
function tkz.solve(...) |
| 228 |
local params = { ... } |
| 229 |
local np = #params |
| 230 |
if np == 2 then |
| 231 |
local a = params[1] |
| 232 |
local b = params[2] |
| 233 |
return -b / a |
| 234 |
elseif np == 3 then |
| 235 |
return tkz.solve_quadratic_(table.unpack(params)) |
| 236 |
elseif np == 4 then |
| 237 |
return tkz.solve_cubic_(table.unpack(params)) |
| 238 |
else |
| 239 |
tex.error("solve: Invalid number of parameters (" .. np .. "). Expected 2, 3, or 4.") |
| 240 |
end |
| 241 |
end |
| 242 |
|
| 243 |
function tkz.solve_cx_quadratic(a, b, c) |
| 244 |
local d = b * b - 4 * a * c |
| 245 |
local dcx = point.sqrt(d) |
| 246 |
local root1 = (-b + dcx) / (2 * a) |
| 247 |
local root2 = (-b - dcx) / (2 * a) |
| 248 |
return root1, root2 |
| 249 |
end |
| 250 |
|
| 251 |
function tkz.solve_quadratic_(a, b, c) |
| 252 |
local root1, root2, delta, sqrtdelta |
| 253 |
if (type(a) == "number") and (type(b) == "number") and (type(c) == "number") then |
| 254 |
delta = b * b - 4 * a * c |
| 255 |
if math.abs(delta) < tkz.epsilon then |
| 256 |
delta = 0 |
| 257 |
end |
| 258 |
if delta < 0 then |
| 259 |
root1, root2 = nil, nil --solve_cx_quadratic(a, b, c) |
| 260 |
elseif delta == 0 then |
| 261 |
root1 = -b / (2 * a) |
| 262 |
root2 = -b / (2 * a) |
| 263 |
else |
| 264 |
sqrtdelta = math.sqrt(delta) |
| 265 |
|
| 266 |
root1 = (-b + sqrtdelta) / (2 * a) |
| 267 |
root2 = (-b - sqrtdelta) / (2 * a) |
| 268 |
end |
| 269 |
else |
| 270 |
root1, root2 = tkz.solve_cx_quadratic(a, b, c) |
| 271 |
end |
| 272 |
return root1, root2 -- Two real roots |
| 273 |
end |
| 274 |
|
| 275 |
function tkz.solve_cubic_(a, b, c, d) |
| 276 |
local p = (3 * a * c - b * b) / (3 * a * a) |
| 277 |
local q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a) |
| 278 |
|
| 279 |
local delta = q * q / 4 + p * p * p / 27 |
| 280 |
local epsilon = 1e-10 -- Tolérance pour les comparaisons de nombres flottants |
| 281 |
|
| 282 |
local function cubert(x) |
| 283 |
return x >= 0 and x ^ (1 / 3) or -(-x) ^ (1 / 3) |
| 284 |
end |
| 285 |
|
| 286 |
if delta > epsilon then |
| 287 |
local u = cubert((-q + math.sqrt(delta)) / 2) |
| 288 |
local v = cubert((-q - math.sqrt(delta)) / 2) |
| 289 |
local x1 = u + v - b / (3 * a) |
| 290 |
|
| 291 |
return x1, nil, nil |
| 292 |
elseif math.abs(delta) <= epsilon then |
| 293 |
local u = cubert(-q / 2) |
| 294 |
local x1 = 2 * u - b / (3 * a) |
| 295 |
local x2 = -u - b / (3 * a) |
| 296 |
|
| 297 |
return x1, x2, x2 |
| 298 |
else |
| 299 |
local r = math.sqrt(-p * p * p / 27) |
| 300 |
local t = -q / (2 * r) |
| 301 |
|
| 302 |
if t > 1 then |
| 303 |
t = 1 |
| 304 |
end |
| 305 |
if t < -1 then |
| 306 |
t = -1 |
| 307 |
end |
| 308 |
|
| 309 |
local theta = math.acos(t) |
| 310 |
local x1 = 2 * cubert(r) * math.cos(theta / 3) - b / (3 * a) |
| 311 |
local x2 = 2 * cubert(r) * math.cos((theta + tkz.tau) / 3) - b / (3 * a) |
| 312 |
local x3 = 2 * cubert(r) * math.cos((theta + 2 * tkz.tau) / 3) - b / (3 * a) |
| 313 |
|
| 314 |
return x1, x2, x3 |
| 315 |
end |
| 316 |
end |
| 317 |
|
| 318 |
-- straight line from a through b |
| 319 |
function tkz.line_coefficients(xa, ya, xb, yb) |
| 320 |
return (yb - ya) / (xb - xa), (xb * ya - xa * yb) / (xb - xa) |
| 321 |
end |
| 322 |
|
| 323 |
|
| 324 |
function tkz.barycenter(...) |
| 325 |
return barycenter_(...) |
| 326 |
end |
| 327 |
|
| 328 |
function tkz.is_linear(z1, z2, z3) |
| 329 |
return math.abs((z2 - z1) ^ (z3 - z1)) < tkz.epsilon |
| 330 |
end |
| 331 |
tkz.aligned = tkz.is_linear |
| 332 |
|
| 333 |
function tkz.is_ortho(z1, z2, z3) |
| 334 |
return math.abs((z2 - z1) .. (z3 - z1)) < tkz.epsilon |
| 335 |
end |
| 336 |
|
| 337 |
function tkz.is_zero(x) |
| 338 |
return is_zero_(x) |
| 339 |
end |
| 340 |
|
| 341 |
function tkz.set_zero(x) |
| 342 |
return set_zero_(x) |
| 343 |
end |
| 344 |
|
| 345 |
function tkz.residue(x) |
| 346 |
return residue_(x) |
| 347 |
end |
| 348 |
|
| 349 |
-- internal utility: test for NaN |
| 350 |
function tkz.is_NaN(x) |
| 351 |
return x ~= x |
| 352 |
end |
| 353 |
|
| 354 |
-- internal utility: test for Infinity |
| 355 |
function tkz.is_Inf(x) |
| 356 |
return (x == math.huge) or (x == -math.huge) |
| 357 |
end |
| 358 |
|
| 359 |
-- internal utility: detect invalid numeric results (NaN or ±Inf) |
| 360 |
function tkz.notDef(x) |
| 361 |
return tkz.is_NaN(x) or tkz.is_Inf(x) |
| 362 |
end |
| 363 |
|
| 364 |
function tkz.is_integer(x) |
| 365 |
return type(x) == "number" |
| 366 |
and x == x -- exclude NaN |
| 367 |
and x < math.huge -- exclude +inf |
| 368 |
and -x < math.huge -- exclude -inf |
| 369 |
and is_zero_(x - tkz_round_(x)) |
| 370 |
end |
| 371 |
|
| 372 |
function tkz.near_integer(x, EPS) |
| 373 |
EPS = EPS or tkz.epsilon |
| 374 |
if type(x) ~= "number" then return false end |
| 375 |
if x ~= x or x == math.huge or x == -math.huge then return false end -- NaN / ±Inf |
| 376 |
return math.abs(x - math.floor(x + 0.5)) <= EPS |
| 377 |
end |
| 378 |
|
| 379 |
function tkz.orient2d(x1, y1, x2, y2, x3, y3) |
| 380 |
return (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1) |
| 381 |
end |
| 382 |
|
| 383 |
function tkz.cramer(...) |
| 384 |
local t = { ... } |
| 385 |
local n = #t |
| 386 |
if n == 4 then |
| 387 |
local a1, a2, b1, b2 = table.unpack(t) |
| 388 |
return a1 * b2 - a2 * b1 |
| 389 |
elseif n == 9 then |
| 390 |
local a1, a2, a3, b1, b2, b3, c1, c2, c3 = table.unpack(t) |
| 391 |
return a1 * b2 * c3 + a3 * b1 * c2 + a2 * b3 * c1 - a3 * b2 * c1 - a1 * b3 * c2 - a2 * b1 * c3 |
| 392 |
else |
| 393 |
tex.error("cramer: invalid number of arguments (expected 4 or 9)") |
| 394 |
end |
| 395 |
end |
| 396 |
|
| 397 |
function tkz.dot_product(a, b, c) |
| 398 |
return (b - a) .. (c - a) |
| 399 |
end |
| 400 |
|
| 401 |
function tkz.round(num, idp) |
| 402 |
return tkz_round_(num, idp) |
| 403 |
end |
| 404 |
|
| 405 |
function tkz.display(z) |
| 406 |
return tkz_display_(z) |
| 407 |
end |
| 408 |
|
| 409 |
function tkz.poncelet_point(a, b, c, d) |
| 410 |
local e1 = euler_center_(a, b, c) |
| 411 |
local e2 = euler_center_(a, c, d) |
| 412 |
local m = midpoint_(a, c) |
| 413 |
local x, y = intersection_cc_(e1, m, e2, m) |
| 414 |
|
| 415 |
if not x or not y then |
| 416 |
tex.xs("Poncelet construction failed: no intersection.") |
| 417 |
end |
| 418 |
|
| 419 |
if x == m then |
| 420 |
return y |
| 421 |
else |
| 422 |
return x |
| 423 |
end |
| 424 |
end |
| 425 |
|
| 426 |
function tkz.orthopole(a, b, c, l) |
| 427 |
local p, q = l:get() |
| 428 |
local ap = projection_(p, q, a) |
| 429 |
local bp = projection_(p, q, b) |
| 430 |
local app = projection_(b, c, ap) |
| 431 |
local bpp = projection_(a, c, bp) |
| 432 |
local la = line:new(ap, app) |
| 433 |
local lb = line:new(bp, bpp) |
| 434 |
return intersection(la, lb) |
| 435 |
end |
| 436 |
|
| 437 |
function tkz.nodes_from_paths(PAcenters, PAthrough, wbase, tbase, indice) |
| 438 |
wbase = wbase or "w" |
| 439 |
tbase = tbase or "t" |
| 440 |
local n = #PAcenters |
| 441 |
local k0 = (indice or 1) - 1 -- offset d’indice |
| 442 |
|
| 443 |
for k = 1, n do |
| 444 |
local i = k0 + k |
| 445 |
z[wbase .. i] = PAcenters:get(k) |
| 446 |
z[tbase .. i] = PAthrough:get(k) |
| 447 |
end |
| 448 |
end |
| 449 |
|
| 450 |
-- tkz.fsolve |
| 451 |
-- Cherche des racines de f sur [a,b] en découpant l'intervalle en n sous-intervales |
| 452 |
-- et en raffinant par une itération de type Newton avec dérivée numérique. |
| 453 |
-- Arguments : |
| 454 |
-- f : function(x) -> number |
| 455 |
-- a, b : bornes (numbers) |
| 456 |
-- n : nombre de sous-intervalles (par défaut 25) |
| 457 |
-- opts (optionnel, table) : |
| 458 |
-- tol : tolérance sur |f(r)| (déf. 1e-8) |
| 459 |
-- step_tol : tolérance sur le déplacement de Newton (déf. 1e-10) |
| 460 |
-- h : pas pour la dérivée finie (déf. 1e-6) |
| 461 |
-- max_iter : itérations max par graine (déf. 8) |
| 462 |
-- merge_eps : distance pour fusionner deux racines proches (déf. 1e-5) |
| 463 |
function tkz.fsolve(f, a, b, n, opts) |
| 464 |
if type(f) ~= "function" or type(a) ~= "number" or type(b) ~= "number" then return end |
| 465 |
if a == b then return end |
| 466 |
if a > b then a, b = b, a end |
| 467 |
|
| 468 |
n = n or 25 |
| 469 |
opts = opts or {} |
| 470 |
local tol = opts.tol or 1e-8 |
| 471 |
local step_tol = opts.step_tol or 1e-10 |
| 472 |
local h = opts.h or 1e-6 |
| 473 |
local max_iter = opts.max_iter or 8 |
| 474 |
local merge_eps = opts.merge_eps or 1e-5 |
| 475 |
|
| 476 |
local function isfinite(x) |
| 477 |
return (x == x) and (x ~= math.huge) and (x ~= -math.huge) |
| 478 |
end |
| 479 |
|
| 480 |
local function safe_f(x) |
| 481 |
local ok, y = pcall(f, x) |
| 482 |
if not ok or not isfinite(y) then return nil end |
| 483 |
return y |
| 484 |
end |
| 485 |
|
| 486 |
local function push_unique(S, r) |
| 487 |
for i = 1, #S do |
| 488 |
if math.abs(S[i] - r) <= merge_eps then |
| 489 |
-- remplace par la moyenne pour stabiliser |
| 490 |
S[i] = 0.5 * (S[i] + r) |
| 491 |
return |
| 492 |
end |
| 493 |
end |
| 494 |
S[#S+1] = r |
| 495 |
end |
| 496 |
|
| 497 |
local S = {} |
| 498 |
local delta = (b - a) / n |
| 499 |
local x = a |
| 500 |
local fin = x + delta |
| 501 |
|
| 502 |
while x < b do |
| 503 |
-- Graine : milieu de [x, fin] |
| 504 |
local r = 0.5 * (x + fin) |
| 505 |
|
| 506 |
-- Optionnel : si f(x) et f(fin) existent et n'encadrent rien, |
| 507 |
-- on tente quand même (peut attraper un zéro au sommet). |
| 508 |
local fr = safe_f(r) |
| 509 |
if fr ~= nil then |
| 510 |
for _ = 1, max_iter do |
| 511 |
local frh = safe_f(r + h) |
| 512 |
if frh == nil then break end |
| 513 |
local denom = frh - fr |
| 514 |
if denom == 0 then break end |
| 515 |
|
| 516 |
local step = fr * h / denom |
| 517 |
if not isfinite(step) then break end |
| 518 |
|
| 519 |
local r_next = r - step |
| 520 |
if not isfinite(r_next) then break end |
| 521 |
|
| 522 |
-- Arrêt sur petit déplacement |
| 523 |
if math.abs(step) <= step_tol * (1 + math.abs(r)) then |
| 524 |
r = r_next |
| 525 |
break |
| 526 |
end |
| 527 |
|
| 528 |
r = r_next |
| 529 |
fr = safe_f(r) |
| 530 |
if fr == nil then break end |
| 531 |
-- Arrêt sur résidu |
| 532 |
if math.abs(fr) <= tol then break end |
| 533 |
end |
| 534 |
|
| 535 |
-- Valide si : dans [x, fin) et résidu petit |
| 536 |
local fr_ok = fr ~= nil and math.abs(fr) <= tol |
| 537 |
if fr_ok and (r >= x) and (r < fin) then |
| 538 |
push_unique(S, r) |
| 539 |
end |
| 540 |
end |
| 541 |
|
| 542 |
x = fin |
| 543 |
fin = x + delta |
| 544 |
end |
| 545 |
|
| 546 |
if #S > 0 then return S end |
| 547 |
end |
| 548 |
|
| 549 |
-- tkz.derivative |
| 550 |
-- Compute the numerical derivative f'(x0) |
| 551 |
-- using the central difference formula. |
| 552 |
-- Optional parameter 'accuracy' controls precision (default = 1e-6). |
| 553 |
function tkz.derivative(f, x0, accuracy) |
| 554 |
if type(f) ~= "function" or type(x0) ~= "number" then return nil end |
| 555 |
accuracy = accuracy or 1e-6 |
| 556 |
local f1, f2 = f(x0 + accuracy), f(x0 - accuracy) |
| 557 |
if (f1 == nil) or (f2 == nil) then return nil end |
| 558 |
return (f1 - f2) / (2 * accuracy) |
| 559 |
end |
| 560 |
|
| 561 |
function tkz.range(a, b, step) |
| 562 |
step = step or 1 |
| 563 |
if step == 0 then error("step must not be zero") end |
| 564 |
local tbl = {} |
| 565 |
if (a <= b and step > 0) or (a >= b and step < 0) then |
| 566 |
for k = a, b, step do |
| 567 |
table.insert(tbl, k) |
| 568 |
end |
| 569 |
end |
| 570 |
return tbl |
| 571 |
end |
| 572 |
|
| 573 |
function tkz.GetNodes() |
| 574 |
local bs = string.char(92) |
| 575 |
|
| 576 |
-- reset pour cet appel |
| 577 |
tkz.has_coord_warning = false |
| 578 |
tkz.coord_warnings = {} |
| 579 |
|
| 580 |
for K, V in pairs(z) do |
| 581 |
local name = K |
| 582 |
local n = string.len(name) |
| 583 |
|
| 584 |
-- Gestion des 'p' → ' et '' |
| 585 |
if n > 1 then |
| 586 |
local ft, sd = string.match(name, "(.+)(.)") |
| 587 |
if sd == "p" then |
| 588 |
name = ft .. "'" |
| 589 |
local xft, xsd = string.match(ft, "(.+)(.)") |
| 590 |
if xsd == "p" then |
| 591 |
name = xft .. "''" |
| 592 |
end |
| 593 |
end |
| 594 |
end |
| 595 |
|
| 596 |
local x = V.re |
| 597 |
local y = V.im |
| 598 |
local ox, oy = x, y -- garder trace des originales |
| 599 |
|
| 600 |
-- Coordonnées hors limite → on enregistre, et on remplace par (0,0) |
| 601 |
if math.abs(x) > tkz.max_coord_safe or math.abs(y) > tkz.max_coord_safe then |
| 602 |
tkz.register_coord_warning(name, ox, oy) |
| 603 |
x, y = 0, 0 |
| 604 |
end |
| 605 |
|
| 606 |
-- Sortie TikZ *neutre* : aucune chance de casser des groupes |
| 607 |
tex.sprint( |
| 608 |
bs .. "coordinate (" .. name .. ") at (" .. x .. "," .. y .. ");" |
| 609 |
) |
| 610 |
end |
| 611 |
end |
| 612 |
|
| 613 |
|
| 614 |
return tkz |