| 1 |
-- File: tkz_elements_matrix.lua |
| 2 |
-- Copyright (c) 2026 Alain Matthes |
| 3 |
-- SPDX-License-Identifier: LPPL-1.3c |
| 4 |
-- Maintainer: Alain Matthes |
| 5 |
|
| 6 |
------------------------------------------------------------------------------ |
| 7 |
matrix = {} |
| 8 |
matrix.__index = matrix |
| 9 |
function matrix:new(value) |
| 10 |
local type = "matrix" |
| 11 |
local rows = #value |
| 12 |
local cols = #value[1] |
| 13 |
local set = value |
| 14 |
local det = determinant(value) |
| 15 |
local o = { set = set, rows = rows, cols = cols, det = det, type = type } |
| 16 |
setmetatable(o, self) |
| 17 |
return o |
| 18 |
end |
| 19 |
|
| 20 |
setmetatable(matrix, { |
| 21 |
__call = function(cls, ...) |
| 22 |
return cls:new(...) |
| 23 |
end, |
| 24 |
}) |
| 25 |
|
| 26 |
function matrix.__mul(m1, m2) |
| 27 |
if getmetatable(m1) ~= matrix then |
| 28 |
return k_mul_matrix(m1, m2) |
| 29 |
end |
| 30 |
if getmetatable(m2) ~= matrix then |
| 31 |
return k_mul_matrix(m2, m1) |
| 32 |
end |
| 33 |
return mul_matrix(m1, m2) |
| 34 |
end |
| 35 |
|
| 36 |
function matrix.__add(m1, m2) |
| 37 |
return add_matrix(m1, m2) |
| 38 |
end |
| 39 |
|
| 40 |
function matrix.__sub(m1, m2) |
| 41 |
return add_matrix(m1, k_mul_matrix(-1, m2)) |
| 42 |
end |
| 43 |
|
| 44 |
|
| 45 |
function matrix.__pow(m, exp) |
| 46 |
-- --- Transpose shortcut (A^"T") |
| 47 |
if exp == "T" then |
| 48 |
return transposeMatrix(m) |
| 49 |
end |
| 50 |
|
| 51 |
-- --- Only integer powers are supported |
| 52 |
if type(exp) ~= "number" or exp ~= math.floor(exp) then |
| 53 |
return nil, "matrix.__pow: exponent must be an integer (or 'T' for transpose)" |
| 54 |
end |
| 55 |
|
| 56 |
-- --- Power defined only for square matrices |
| 57 |
if getmetatable(m) ~= matrix then |
| 58 |
return nil, "matrix.__pow: left operand must be a matrix" |
| 59 |
end |
| 60 |
if not m:is_square() then |
| 61 |
return nil, "matrix.__pow: power is defined only for square matrices" |
| 62 |
end |
| 63 |
|
| 64 |
local n = m.rows |
| 65 |
|
| 66 |
-- --- A^0 = I |
| 67 |
if exp == 0 then |
| 68 |
return id_matrix(n) |
| 69 |
end |
| 70 |
|
| 71 |
-- --- If exp < 0, use inverse |
| 72 |
local base = m |
| 73 |
if exp < 0 then |
| 74 |
local inv, err = inv_matrix(m) -- IMPORTANT: do not shadow the function name |
| 75 |
if not inv then |
| 76 |
return nil, err or "matrix.__pow: matrix is not invertible" |
| 77 |
end |
| 78 |
base = inv |
| 79 |
exp = -exp |
| 80 |
end |
| 81 |
|
| 82 |
-- --- Fast exponentiation (binary exponentiation) |
| 83 |
local result = id_matrix(n) |
| 84 |
local p = exp |
| 85 |
local b = base |
| 86 |
|
| 87 |
while p > 0 do |
| 88 |
if (p % 2) == 1 then |
| 89 |
result = mul_matrix(result, b) |
| 90 |
end |
| 91 |
p = math.floor(p / 2) |
| 92 |
if p > 0 then |
| 93 |
b = mul_matrix(b, b) |
| 94 |
end |
| 95 |
end |
| 96 |
|
| 97 |
return result |
| 98 |
end |
| 99 |
|
| 100 |
|
| 101 |
function matrix.__tostring(A) |
| 102 |
local mt = (A.type == "matrix" and A.set or A) |
| 103 |
local k = {} |
| 104 |
for i = 1, #mt do |
| 105 |
local n = {} |
| 106 |
for j = 1, #mt[1] do |
| 107 |
n[j] = tkz_display_(mt[i][j]) |
| 108 |
end |
| 109 |
k[i] = table.concat(n, " ") |
| 110 |
end |
| 111 |
-- à la toute fin |
| 112 |
return table.concat(k, " ; ") |
| 113 |
end |
| 114 |
|
| 115 |
function matrix.__eq(A, B) |
| 116 |
local mt1 = (A.type == "matrix" and A.set or A) |
| 117 |
local mt2 = (B.type == "matrix" and B.set or B) |
| 118 |
if getmetatable(A) ~= matrix or getmetatable(B) ~= matrix then |
| 119 |
return false |
| 120 |
end |
| 121 |
|
| 122 |
|
| 123 |
if #mt1 ~= #mt2 or #mt1[1] ~= #mt2[1] then |
| 124 |
return false |
| 125 |
end |
| 126 |
|
| 127 |
for i = 1, #mt1 do |
| 128 |
for j = 1, #mt1[1] do |
| 129 |
if mt1[i][j] ~= mt2[i][j] then |
| 130 |
return false |
| 131 |
end |
| 132 |
end |
| 133 |
end |
| 134 |
return true |
| 135 |
end |
| 136 |
|
| 137 |
function matrix.square(n, ...) |
| 138 |
local m = {} |
| 139 |
local t = table.pack(...) |
| 140 |
if n * n == #t then |
| 141 |
for i = 1, n do |
| 142 |
m[i] = {} |
| 143 |
for j = 1, n do |
| 144 |
m[i][j] = t[n * (i - 1) + j] |
| 145 |
end |
| 146 |
end |
| 147 |
return matrix:new(m) |
| 148 |
else |
| 149 |
return nil |
| 150 |
end |
| 151 |
end |
| 152 |
|
| 153 |
function matrix.vector(...) |
| 154 |
local m = {} |
| 155 |
local t = table.pack(...) |
| 156 |
for i = 1, #t do |
| 157 |
m[i] = {} |
| 158 |
m[i][1] = t[i] |
| 159 |
end |
| 160 |
return matrix:new(m) |
| 161 |
end |
| 162 |
|
| 163 |
matrix.column = matrix.vector |
| 164 |
|
| 165 |
function matrix.row_vector(...) |
| 166 |
local m = {} |
| 167 |
local t = table.pack(...) |
| 168 |
m[1] = {} |
| 169 |
for j = 1, #t do |
| 170 |
m[1][j] = t[j] |
| 171 |
end |
| 172 |
return matrix:new(m) |
| 173 |
end |
| 174 |
|
| 175 |
function matrix.create(rows, cols) |
| 176 |
local mat = {} |
| 177 |
for i = 1, rows do |
| 178 |
mat[i] = {} |
| 179 |
for j = 1, cols do |
| 180 |
mat[i][j] = 0 |
| 181 |
end |
| 182 |
end |
| 183 |
return matrix:new(mat) |
| 184 |
end |
| 185 |
|
| 186 |
function matrix:homogenization() |
| 187 |
return homogenization_(self) |
| 188 |
end |
| 189 |
|
| 190 |
function matrix:htm_apply(...) |
| 191 |
local obj, nb, t |
| 192 |
local tp = table.pack(...) |
| 193 |
obj = tp[1] |
| 194 |
nb = tp.n |
| 195 |
if nb == 1 then |
| 196 |
if obj.type == "point" then |
| 197 |
return htm_apply_(self, obj) |
| 198 |
elseif obj.type == "line" then |
| 199 |
return htm_apply_L_(self, obj) |
| 200 |
elseif obj.type == "triangle" then |
| 201 |
return htm_apply_T_(self, obj) |
| 202 |
elseif obj.type == "circle" then |
| 203 |
return htm_apply_C(self, obj) |
| 204 |
elseif |
| 205 |
obj.type == "square" |
| 206 |
or obj.type == "rectangle" |
| 207 |
or obj.type == "quadrilateral" |
| 208 |
or obj.type == "parallelogram" |
| 209 |
then |
| 210 |
return htm_apply_Q(self, obj) |
| 211 |
end |
| 212 |
else |
| 213 |
t = {} |
| 214 |
for i = 1, tp.n do |
| 215 |
table.insert(t, htm_apply_(self, tp[i])) |
| 216 |
end |
| 217 |
return table.unpack(t) |
| 218 |
end |
| 219 |
end |
| 220 |
|
| 221 |
function matrix:k_mul(n) |
| 222 |
return k_mul_matrix(n, self) |
| 223 |
end |
| 224 |
|
| 225 |
function matrix:get(i, j) |
| 226 |
if i == nil and j == nil then |
| 227 |
return self.set -- retourne toute la matrice |
| 228 |
else |
| 229 |
return get_element_(self, i, j) |
| 230 |
end |
| 231 |
end |
| 232 |
|
| 233 |
function matrix:inverse() |
| 234 |
return inv_matrix(self) |
| 235 |
end |
| 236 |
|
| 237 |
function matrix:adjugate() |
| 238 |
return adjugate_(self) |
| 239 |
end |
| 240 |
|
| 241 |
function matrix:transpose() |
| 242 |
return transposeMatrix(self) |
| 243 |
end |
| 244 |
|
| 245 |
function matrix:is_square() |
| 246 |
return self.rows == self.cols |
| 247 |
end |
| 248 |
|
| 249 |
function matrix:is_diagonal() |
| 250 |
return isDiagonal_(self) |
| 251 |
end |
| 252 |
|
| 253 |
function matrix:is_orthogonal() |
| 254 |
return isOrthogonal_(self) |
| 255 |
end |
| 256 |
|
| 257 |
function matrix:diagonalize() -- return two matrices D and P |
| 258 |
return diagonalize_(self) |
| 259 |
end |
| 260 |
|
| 261 |
function matrix:print(style, fmt) |
| 262 |
style = style or "bmatrix" |
| 263 |
fmt = fmt or 0 |
| 264 |
return print_matrix(self, style, fmt) |
| 265 |
end |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
------------------------- |
| 270 |
-- homogeneous transformation matrix |
| 271 |
function matrix.htm(phi, a, b, c, d) |
| 272 |
local tx = (a or 0) |
| 273 |
local ty = (b or 0) |
| 274 |
local sx = (c or 1) |
| 275 |
local sy = (d or 1) |
| 276 |
local phi = (phi or 0) |
| 277 |
return matrix.square(3, sx * math.cos(phi), -math.sin(phi), tx, math.sin(phi), sy * math.cos(phi), ty, 0, 0, 1) |
| 278 |
end |
| 279 |
------------------------- |
| 280 |
|
| 281 |
|
| 282 |
function matrix:swap_rows(i, j) |
| 283 |
local A = {} |
| 284 |
for r = 1, self.rows do |
| 285 |
A[r] = {} |
| 286 |
for c = 1, self.cols do |
| 287 |
A[r][c] = self.set[r][c] |
| 288 |
end |
| 289 |
end |
| 290 |
A[i], A[j] = A[j], A[i] |
| 291 |
return matrix:new(A) |
| 292 |
end |
| 293 |
|
| 294 |
function matrix:swap_rows_inplace(i, j) |
| 295 |
self.set[i], self.set[j] = self.set[j], self.set[i] |
| 296 |
self.det = nil |
| 297 |
return self |
| 298 |
end |
| 299 |
|
| 300 |
|
| 301 |
function matrix:k_mul_row(row, k) |
| 302 |
local A = {} |
| 303 |
for i = 1, self.rows do |
| 304 |
A[i] = {} |
| 305 |
for j = 1, self.cols do |
| 306 |
A[i][j] = self.set[i][j] |
| 307 |
end |
| 308 |
end |
| 309 |
|
| 310 |
for j = 1, self.cols do |
| 311 |
A[row][j] = A[row][j] * k |
| 312 |
end |
| 313 |
|
| 314 |
return matrix:new(A) |
| 315 |
end |
| 316 |
|
| 317 |
|
| 318 |
-- Ajoute à la ligne target_row la ligne source_row multipliée par k |
| 319 |
function matrix:add_k_mul_row(target_row, source_row, k) |
| 320 |
local A = {} |
| 321 |
for i = 1, self.rows do |
| 322 |
A[i] = {} |
| 323 |
for j = 1, self.cols do |
| 324 |
A[i][j] = self.set[i][j] |
| 325 |
end |
| 326 |
end |
| 327 |
|
| 328 |
for j = 1, self.cols do |
| 329 |
A[target_row][j] = A[target_row][j] + A[source_row][j] * k |
| 330 |
end |
| 331 |
|
| 332 |
return matrix:new(A) |
| 333 |
end |
| 334 |
|
| 335 |
function matrix:k_mul_row_inplace(row, k) |
| 336 |
for j = 1, self.cols do |
| 337 |
self.set[row][j] = self.set[row][j] * k |
| 338 |
end |
| 339 |
self.det = nil |
| 340 |
return self |
| 341 |
end |
| 342 |
|
| 343 |
function matrix:add_k_mul_row_inplace(target_row, source_row, k) |
| 344 |
for j = 1, self.cols do |
| 345 |
self.set[target_row][j] = self.set[target_row][j] + self.set[source_row][j] * k |
| 346 |
end |
| 347 |
self.det = nil |
| 348 |
return self |
| 349 |
end |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
local function copy2d_(M) |
| 354 |
local C = {} |
| 355 |
for i = 1, #M do |
| 356 |
C[i] = {} |
| 357 |
for j = 1, #M[i] do |
| 358 |
C[i][j] = M[i][j] |
| 359 |
end |
| 360 |
end |
| 361 |
return C |
| 362 |
end |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
function matrix:gauss_jordan() |
| 367 |
local A = {} |
| 368 |
for i = 1, self.rows do |
| 369 |
A[i] = {} |
| 370 |
for j = 1, self.cols do |
| 371 |
A[i][j] = self.set[i][j] |
| 372 |
end |
| 373 |
end |
| 374 |
|
| 375 |
local m, n = self.rows, self.cols |
| 376 |
local r, lead = 1, 1 |
| 377 |
|
| 378 |
while r <= m and lead <= n do |
| 379 |
-- find pivot row i >= r with nonzero in column lead |
| 380 |
local i = r |
| 381 |
while i <= m and math.abs(A[i][lead]) <= tkz.epsilon do |
| 382 |
i = i + 1 |
| 383 |
end |
| 384 |
|
| 385 |
if i > m then |
| 386 |
lead = lead + 1 |
| 387 |
else |
| 388 |
-- swap i <-> r |
| 389 |
A[i], A[r] = A[r], A[i] |
| 390 |
|
| 391 |
-- normalize pivot row |
| 392 |
local pivot = A[r][lead] |
| 393 |
for j = lead, n do |
| 394 |
A[r][j] = A[r][j] / pivot |
| 395 |
end |
| 396 |
|
| 397 |
-- eliminate all other rows |
| 398 |
for k = 1, m do |
| 399 |
if k ~= r then |
| 400 |
local factor = A[k][lead] |
| 401 |
if math.abs(factor) > tkz.epsilon then |
| 402 |
for j = lead, n do |
| 403 |
A[k][j] = A[k][j] - factor * A[r][j] |
| 404 |
end |
| 405 |
end |
| 406 |
end |
| 407 |
end |
| 408 |
|
| 409 |
r = r + 1 |
| 410 |
lead = lead + 1 |
| 411 |
end |
| 412 |
end |
| 413 |
|
| 414 |
return matrix:new(A) |
| 415 |
end |
| 416 |
|
| 417 |
|
| 418 |
-- function matrix:rank() |
| 419 |
-- local A = (self.type == "matrix" and self.set or self) |
| 420 |
-- local m, n = self.rows, self.cols |
| 421 |
-- local rank = 0 |
| 422 |
-- |
| 423 |
-- for col = 1, n do |
| 424 |
-- local pivot_row = rank + 1 |
| 425 |
-- |
| 426 |
-- -- Finding the pivot |
| 427 |
-- while pivot_row <= m and A[pivot_row][col] == 0 do |
| 428 |
-- pivot_row = pivot_row + 1 |
| 429 |
-- end |
| 430 |
-- |
| 431 |
-- if pivot_row <= m then |
| 432 |
-- -- Swap lines if necessary |
| 433 |
-- A[rank + 1], A[pivot_row] = A[pivot_row], A[rank + 1] |
| 434 |
-- |
| 435 |
-- -- Normaliser le pivot à 1 |
| 436 |
-- local pivot = A[rank + 1][col] |
| 437 |
-- for j = 1, n do |
| 438 |
-- A[rank + 1][j] = A[rank + 1][j] / pivot |
| 439 |
-- end |
| 440 |
-- |
| 441 |
-- -- Eliminate the column |
| 442 |
-- for i = 1, m do |
| 443 |
-- if i ~= rank + 1 then |
| 444 |
-- local factor = A[i][col] |
| 445 |
-- for j = 1, n do |
| 446 |
-- A[i][j] = A[i][j] - factor * A[rank + 1][j] |
| 447 |
-- end |
| 448 |
-- end |
| 449 |
-- end |
| 450 |
-- |
| 451 |
-- rank = rank + 1 |
| 452 |
-- end |
| 453 |
-- end |
| 454 |
-- |
| 455 |
-- return rank |
| 456 |
-- end |
| 457 |
|
| 458 |
function matrix:rank() |
| 459 |
-- --- work on a copy (NON destructive) |
| 460 |
local A = {} |
| 461 |
for i = 1, self.rows do |
| 462 |
A[i] = {} |
| 463 |
for j = 1, self.cols do |
| 464 |
A[i][j] = self.set[i][j] |
| 465 |
end |
| 466 |
end |
| 467 |
|
| 468 |
local m, n = self.rows, self.cols |
| 469 |
local rank = 0 |
| 470 |
local row = 1 |
| 471 |
|
| 472 |
for col = 1, n do |
| 473 |
-- --- search a pivot |
| 474 |
local pivot_row = nil |
| 475 |
for i = row, m do |
| 476 |
if math.abs(A[i][col]) > tkz.epsilon then |
| 477 |
pivot_row = i |
| 478 |
break |
| 479 |
end |
| 480 |
end |
| 481 |
|
| 482 |
if pivot_row then |
| 483 |
-- --- swap rows if needed |
| 484 |
A[row], A[pivot_row] = A[pivot_row], A[row] |
| 485 |
|
| 486 |
-- --- normalize pivot row |
| 487 |
local pivot = A[row][col] |
| 488 |
for j = col, n do |
| 489 |
A[row][j] = A[row][j] / pivot |
| 490 |
end |
| 491 |
|
| 492 |
-- --- eliminate below |
| 493 |
for i = row + 1, m do |
| 494 |
local factor = A[i][col] |
| 495 |
if math.abs(factor) > tkz.epsilon then |
| 496 |
for j = col, n do |
| 497 |
A[i][j] = A[i][j] - factor * A[row][j] |
| 498 |
end |
| 499 |
end |
| 500 |
end |
| 501 |
|
| 502 |
rank = rank + 1 |
| 503 |
row = row + 1 |
| 504 |
|
| 505 |
if row > m then break end |
| 506 |
end |
| 507 |
end |
| 508 |
|
| 509 |
return rank |
| 510 |
end |
| 511 |
|
| 512 |
|
| 513 |
return matrix |