| 1 |
-- File: tkz_elements_functions_matrices.lua |
| 2 |
-- Copyright (c) 2026 Alain Matthes |
| 3 |
-- SPDX-License-Identifier: LPPL-1.3c |
| 4 |
-- Maintainer: Alain Matthes |
| 5 |
|
| 6 |
-- ---------------------------------------------------------------------------- |
| 7 |
function print_matrix(m, mstyle, fmt) |
| 8 |
mstyle = mstyle or "bmatrix" |
| 9 |
fmt = fmt or 0 |
| 10 |
|
| 11 |
-- si on reçoit un objet matrix, on récupère son set |
| 12 |
if m.type == "matrix" then |
| 13 |
m = m.set |
| 14 |
end |
| 15 |
|
| 16 |
tex.sprint("$") |
| 17 |
tex.sprint("\\begin{" .. mstyle .. "}") |
| 18 |
for i = 1, #m do |
| 19 |
for j = 1, #m[1] do |
| 20 |
local x = m[i][j] |
| 21 |
-- si tkz_display_ accepte un format, on lui passe fmt |
| 22 |
local st = tkz_display_(x, fmt) |
| 23 |
tex.sprint(st) |
| 24 |
if j < #m[1] then |
| 25 |
tex.sprint(" & ") |
| 26 |
end |
| 27 |
end |
| 28 |
tex.sprint("\\\\") |
| 29 |
end |
| 30 |
tex.sprint("\\end{" .. mstyle .. "}") |
| 31 |
tex.sprint("$") |
| 32 |
end |
| 33 |
|
| 34 |
function print_array(matrix) |
| 35 |
local mdata = (matrix.type == "matrix" and matrix.set or matrix) |
| 36 |
tex.sprint("\\{%") |
| 37 |
for i = 1, #mdata do |
| 38 |
local row = mdata[i] |
| 39 |
local row_str = "{\\{" |
| 40 |
for j = 1, #row do |
| 41 |
row_str = row_str .. " " .. tostring(row[j]) |
| 42 |
if j < #row then |
| 43 |
row_str = row_str .. " ," |
| 44 |
end |
| 45 |
end |
| 46 |
if (i ~= #mdata) and (j ~= #row) then |
| 47 |
if i > 1 then |
| 48 |
row_str = row_str .. " \\}}," |
| 49 |
else |
| 50 |
row_str = row_str .. " \\}}," |
| 51 |
end |
| 52 |
tex.sprint(row_str) |
| 53 |
else |
| 54 |
if i > 1 then |
| 55 |
row_str = row_str .. " \\}}" |
| 56 |
else |
| 57 |
row_str = row_str .. " \\}}" |
| 58 |
end |
| 59 |
tex.sprint(row_str) |
| 60 |
end |
| 61 |
end |
| 62 |
tex.sprint("\\}") |
| 63 |
end |
| 64 |
|
| 65 |
function mul_matrix(A, B) |
| 66 |
local adata = (A.type == "matrix" and A.set or A) |
| 67 |
local bdata = (B.type == "matrix" and B.set or B) |
| 68 |
local C = {} |
| 69 |
for i = 1, #adata do |
| 70 |
C[i] = {} |
| 71 |
for j = 1, #bdata[1] do |
| 72 |
local num = adata[i][1] * bdata[1][j] |
| 73 |
for k = 2, #adata[1] do |
| 74 |
num = num + adata[i][k] * bdata[k][j] |
| 75 |
end |
| 76 |
C[i][j] = num |
| 77 |
end |
| 78 |
end |
| 79 |
return matrix:new(C) |
| 80 |
end |
| 81 |
|
| 82 |
function add_matrix(A, B) |
| 83 |
local adata = (A.type == "matrix" and A.set or A) |
| 84 |
local bdata = (B.type == "matrix" and B.set or B) |
| 85 |
local S = {} |
| 86 |
for i = 1, #adata do |
| 87 |
local T = {} |
| 88 |
S[i] = T |
| 89 |
for j = 1, #adata[1] do |
| 90 |
T[j] = adata[i][j] + bdata[i][j] |
| 91 |
end |
| 92 |
end |
| 93 |
return matrix:new(S) |
| 94 |
end |
| 95 |
|
| 96 |
function k_mul_matrix(n, A) |
| 97 |
local adata = (A.type == "matrix" and A.set or A) |
| 98 |
local S = {} |
| 99 |
for i = 1, #adata, 1 do |
| 100 |
local T = {} |
| 101 |
S[i] = T |
| 102 |
for j = 1, #adata[1], 1 do |
| 103 |
T[j] = n * adata[i][j] |
| 104 |
end |
| 105 |
end |
| 106 |
return matrix:new(S) |
| 107 |
end |
| 108 |
|
| 109 |
function matrix.identity(n) |
| 110 |
return id_matrix(n) |
| 111 |
end |
| 112 |
|
| 113 |
|
| 114 |
function transposeMatrix(A) |
| 115 |
local mdata = (A.type == "matrix" and A.set or A) |
| 116 |
local transposedMatrix = {} |
| 117 |
for i = 1, #mdata[1] do |
| 118 |
transposedMatrix[i] = {} |
| 119 |
for j = 1, #mdata do |
| 120 |
transposedMatrix[i][j] = mdata[j][i] |
| 121 |
end |
| 122 |
end |
| 123 |
return matrix:new(transposedMatrix) |
| 124 |
end |
| 125 |
|
| 126 |
-- Function to calculate the determinant of a square matrix |
| 127 |
function determinant(A) |
| 128 |
local matrix = (A.type == "matrix" and A.set or A) |
| 129 |
if #matrix == #matrix[1] then |
| 130 |
local n = #matrix |
| 131 |
if n == 1 then |
| 132 |
return matrix[1][1] -- Base case for 1x1 matrix |
| 133 |
elseif n == 2 then |
| 134 |
return matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1] -- Base case for 2x2 matrix |
| 135 |
else |
| 136 |
local det = 0 |
| 137 |
for j = 1, n do |
| 138 |
local minor = {} |
| 139 |
for i = 2, n do |
| 140 |
minor[i - 1] = {} |
| 141 |
for k = 1, n do |
| 142 |
if k ~= j then |
| 143 |
minor[i - 1][#minor[i - 1] + 1] = matrix[i][k] |
| 144 |
end |
| 145 |
end |
| 146 |
end |
| 147 |
det = det + ((-1) ^ (j + 1)) * matrix[1][j] * determinant(minor) -- Recursive call for larger matrices |
| 148 |
end |
| 149 |
return det |
| 150 |
end |
| 151 |
else |
| 152 |
return nil |
| 153 |
end |
| 154 |
end |
| 155 |
|
| 156 |
function check_square_matrix(A) |
| 157 |
local matrix = (A.type == "matrix" and A.set or A) |
| 158 |
if #matrix == #matrix[1] then |
| 159 |
return true |
| 160 |
else |
| 161 |
return false |
| 162 |
end |
| 163 |
end |
| 164 |
|
| 165 |
function id_matrix(n) |
| 166 |
local id = {} |
| 167 |
for i = 1, n do |
| 168 |
id[i] = {} |
| 169 |
for j = 1, n do |
| 170 |
-- Set diagonal elements to 1, others to 0 |
| 171 |
id[i][j] = (i == j) and 1 or 0 |
| 172 |
end |
| 173 |
end |
| 174 |
return matrix:new(id) |
| 175 |
end |
| 176 |
|
| 177 |
function adjugate_(A) |
| 178 |
local m = (A.type == "matrix" and A.set or A) |
| 179 |
if #m == 2 then |
| 180 |
local a, b, c, d = m[2][2], -m[1][2], -m[2][1], m[1][1] |
| 181 |
return matrix:new({ { a, b }, { c, d } }) |
| 182 |
elseif #m == 3 then |
| 183 |
local a, b, c = m[1][1], m[1][2], m[1][3] |
| 184 |
local d, e, f = m[2][1], m[2][2], m[2][3] |
| 185 |
local g, h, i = m[3][1], m[3][2], m[3][3] |
| 186 |
return transposeMatrix(matrix:new({ |
| 187 |
{ e * i - f * h, -(d * i - f * g), d * h - e * g }, |
| 188 |
{ -(b * i - c * h), a * i - c * g, -(a * h - b * g) }, |
| 189 |
{ b * f - c * e, -(a * f - c * d), a * e - b * d }, |
| 190 |
})) |
| 191 |
else |
| 192 |
return nil |
| 193 |
end |
| 194 |
end |
| 195 |
|
| 196 |
function inv_matrix(A) |
| 197 |
local tolerance = 1e-10 -- Définir une tolérance |
| 198 |
if math.abs(A.det) < tolerance then |
| 199 |
tex.print("Matrix not invertible: det is close to 0") |
| 200 |
return nil |
| 201 |
else |
| 202 |
local n = #A.set |
| 203 |
local augmentedData = {} |
| 204 |
|
| 205 |
-- Create the augmented matrix [matrix | identity] |
| 206 |
local identityMatrix = matrix.identity(n) |
| 207 |
for i = 1, n do |
| 208 |
augmentedData[i] = {} |
| 209 |
for j = 1, 2 * n do |
| 210 |
if j <= n then |
| 211 |
-- Copy original matrix data |
| 212 |
augmentedData[i][j] = A.set[i][j] |
| 213 |
else |
| 214 |
-- Append identity matrix data |
| 215 |
augmentedData[i][j] = identityMatrix.set[i][j - n] |
| 216 |
end |
| 217 |
end |
| 218 |
end |
| 219 |
|
| 220 |
-- Apply Gauss-Jordan to the augmented matrix |
| 221 |
local augmentedMatrix = matrix:new(augmentedData) |
| 222 |
augmentedMatrix:gauss_jordan() |
| 223 |
|
| 224 |
-- Extract the inverse matrix from the augmented matrix |
| 225 |
local inverseData = {} |
| 226 |
for i = 1, n do |
| 227 |
inverseData[i] = {} |
| 228 |
for j = n + 1, 2 * n do |
| 229 |
-- Extract the right part of the augmented matrix |
| 230 |
inverseData[i][j - n] = augmentedMatrix.set[i][j] |
| 231 |
end |
| 232 |
end |
| 233 |
|
| 234 |
return matrix:new(inverseData) |
| 235 |
end |
| 236 |
end |
| 237 |
|
| 238 |
function diagonalize_(A) |
| 239 |
local m = (A.type == "matrix" and A.set or A) |
| 240 |
local trace = m[1][1] + m[2][2] |
| 241 |
local a, b = m[1][1], m[1][2] |
| 242 |
local det = A.det |
| 243 |
local D = trace * trace - 4 * det |
| 244 |
if D > 0 then |
| 245 |
local va1 = (trace + math.sqrt(D)) / 2 |
| 246 |
local va2 = (trace - math.sqrt(D)) / 2 |
| 247 |
return matrix:new({ { va1, 0 }, { 0, va2 } }), matrix:new({ { 1, 1 }, { (va1 - a) / b, (va2 - a) / b } }) |
| 248 |
else |
| 249 |
local va1 = point(trace / 2, math.sqrt(-D) / 2) |
| 250 |
local va2 = point(trace / 2, -math.sqrt(-D) / 2) |
| 251 |
return matrix:new({ { va1, 0 }, { 0, va2 } }) --, |
| 252 |
-- matrix : new ({{1,1},{ (va1 - a )/b, (va2 - a)/b}}) |
| 253 |
end |
| 254 |
end |
| 255 |
|
| 256 |
function isDiagonal_(A) |
| 257 |
local matrix = (A.type == "matrix" and A.set or A) |
| 258 |
if check_square_matrix(A) == true then |
| 259 |
for i = 1, #matrix do |
| 260 |
for j = 1, #matrix[1] do |
| 261 |
if i ~= j and matrix[i][j] ~= 0 then |
| 262 |
return false |
| 263 |
end |
| 264 |
end |
| 265 |
end |
| 266 |
return true |
| 267 |
else |
| 268 |
return false |
| 269 |
end |
| 270 |
end |
| 271 |
|
| 272 |
function isOrthogonal_(A) |
| 273 |
local m = (A.type == "matrix" and A.set or A) |
| 274 |
if (check_square_matrix(A) == true) and (A.det ~= 0) then |
| 275 |
local mT = transposeMatrix(A) |
| 276 |
local mI = inv_matrix(A) |
| 277 |
if mT == mI then |
| 278 |
return true |
| 279 |
else |
| 280 |
return false |
| 281 |
end |
| 282 |
else |
| 283 |
return false |
| 284 |
end |
| 285 |
end |
| 286 |
|
| 287 |
function homogenization_(A) |
| 288 |
local m = (A.type == "matrix" and A.set or A) |
| 289 |
if A.cols ~= 1 then |
| 290 |
return nil |
| 291 |
else |
| 292 |
local a, b, c |
| 293 |
a = m[1][1] |
| 294 |
b = m[2][1] |
| 295 |
c = 1 |
| 296 |
return matrix:new({ { a }, { b }, { c } }) |
| 297 |
end |
| 298 |
end |
| 299 |
|
| 300 |
function get_element_(A, i, j) |
| 301 |
local m = (A.type == "matrix" and A.set or A) |
| 302 |
if m[i] and m[i][j] then |
| 303 |
return m[i][j] |
| 304 |
end |
| 305 |
end |
| 306 |
|
| 307 |
function get_htm_point(A) |
| 308 |
local m = (A.type == "matrix" and A.set or A) |
| 309 |
return point:new(m[1][1], m[2][1]) |
| 310 |
end |
| 311 |
|
| 312 |
function htm_apply_(A, z) |
| 313 |
local V = homogenization_(z.mtx) |
| 314 |
local W = A * V |
| 315 |
return get_htm_point(W) |
| 316 |
end |
| 317 |
|
| 318 |
function htm_apply_L_(A, obj) |
| 319 |
local x, y |
| 320 |
x = htm_apply_(A, obj.pa) |
| 321 |
y = htm_apply_(A, obj.pb) |
| 322 |
return line:new(x, y) |
| 323 |
end |
| 324 |
|
| 325 |
function htm_apply_C_(A, obj) |
| 326 |
local x, y |
| 327 |
x = htm_apply_(A, obj.center) |
| 328 |
y = htm_apply_(A, obj.through) |
| 329 |
return circle:new(x, y) |
| 330 |
end |
| 331 |
|
| 332 |
function htm_apply_T_(A, obj) |
| 333 |
local x, y, z |
| 334 |
x = htm_apply_(A, obj.pa) |
| 335 |
y = htm_apply_(A, obj.pb) |
| 336 |
z = htm_apply_(A, obj.pc) |
| 337 |
return triangle:new(x, y, z) |
| 338 |
end |
| 339 |
|
| 340 |
function htm_apply_Q_(A, obj) |
| 341 |
local x, y, z, t |
| 342 |
x = htm_apply_(A, obj.pa) |
| 343 |
y = htm_apply_(A, obj.pb) |
| 344 |
z = htm_apply_(A, obj.pc) |
| 345 |
t = htm_apply_(A, obj.pd) |
| 346 |
if obj.type == "square" then |
| 347 |
return square:new(x, y, z, t) |
| 348 |
elseif obj.type == "rectangle" then |
| 349 |
return rectangle:new(x, y, z, t) |
| 350 |
elseif obj.type == "parallelogram" then |
| 351 |
return parallelogram:new(x, y, z, t) |
| 352 |
elseif obj.type == "quadrilateral" then |
| 353 |
return quadrilateral:new(x, y, z, t) |
| 354 |
end |
| 355 |
end |
| 356 |
|
| 357 |
function matrix:augment_right(B) |
| 358 |
local m = #self.set |
| 359 |
local n = #self.set[1] |
| 360 |
local p = #B.set[1] |
| 361 |
local R = {} |
| 362 |
|
| 363 |
for i = 1, m do |
| 364 |
R[i] = {} |
| 365 |
-- copie A |
| 366 |
for j = 1, n do |
| 367 |
R[i][j] = self.set[i][j] |
| 368 |
end |
| 369 |
-- colle B à droite |
| 370 |
for j = 1, p do |
| 371 |
R[i][n + j] = B.set[i][j] |
| 372 |
end |
| 373 |
end |
| 374 |
|
| 375 |
return matrix(R) |
| 376 |
end |
| 377 |
|
| 378 |
|
| 379 |
function matrix:right_block(k) |
| 380 |
local m = #self.set |
| 381 |
local n = #self.set[1] |
| 382 |
local R = {} |
| 383 |
local c0 = n - k + 1 |
| 384 |
|
| 385 |
for i = 1, m do |
| 386 |
R[i] = {} |
| 387 |
for j = 1, k do |
| 388 |
R[i][j] = self.set[i][c0 + j - 1] |
| 389 |
end |
| 390 |
end |
| 391 |
|
| 392 |
return matrix(R) |
| 393 |
end |
| 394 |
|
| 395 |
function matrix:left_block(k) |
| 396 |
local m = #self.set |
| 397 |
local R = {} |
| 398 |
for i = 1, m do |
| 399 |
R[i] = {} |
| 400 |
for j = 1, k do |
| 401 |
R[i][j] = self.set[i][j] |
| 402 |
end |
| 403 |
end |
| 404 |
return matrix(R) |
| 405 |
end |
| 406 |
|
| 407 |
function matrix:submatrix(r1, r2, c1, c2) |
| 408 |
local R = {} |
| 409 |
for i = r1, r2 do |
| 410 |
local row = {} |
| 411 |
for j = c1, c2 do |
| 412 |
row[#row+1] = self.set[i][j] |
| 413 |
end |
| 414 |
R[#R+1] = row |
| 415 |
end |
| 416 |
return matrix(R) |
| 417 |
end |
| 418 |
|
| 419 |
|
| 420 |
|