
El siguiente código puede tener muchos problemas, pero se compila. Quiero crear una (pseudo)clase para introducir metamétodos (+,-,*), pero primero tengo un pequeño problema que no he encontrado con las otras clases de mi paquete. Para poder utilizar la matriz A, mi código me obliga (he tomado prestadas algunas partes a izquierda y derecha) a escribir A.tbl
para manipularla. ¿Es posible modificar mi código para usarlo A
y no A.tbl
? Observación: chatGPT odia mi código y probablemente tenga razón.
\documentclass{article}
\usepackage{tkz-euclide}
\usepackage{tkz-elements}
\begin{document}
\LuaCodeDebugOn
\begin{luacode}
matrix={}
function matrix: new (value)
local type = 'matrix'
local rows = #value
local cols = #value[1]
local tbl = value
local o = {tbl = tbl,
rows = rows,
cols = cols,
type = type }
setmetatable(o, self)
self.__index = self
return o
end
return matrix
\end{luacode}
\begin{luacode}
function print_matrix(matrix)
tex.print("\\{%")
for i = 1, #matrix do
local row = matrix[i]
local row_str = "{\\{"
for j = 1, #row do
row_str = row_str .. " " .. tostring(row[j])
if j < #row then
row_str = row_str .. ","
end
end
row_str = row_str .. "\\}}"
tex.sprint(row_str)
end
tex.print("\\}")
end
function product_matrix(A, B)
local C = {}
for i = 1, #A do
C[i] = {}
for j = 1, #B[1] do
local num = A[i][1] * B[1][j]
for k = 2, #A[1] do
num = num + A[i][k] * B[k][j]
end
C[i][j] = num
end
end
return C
end
\end{luacode}
\begin{tkzelements}
a = point(1,0)
b = point(0,1)
c = point(1,1)
d = point(1,-1)
A = matrix : new ({{1, 2}, {1,3}})
B = matrix : new ({{2, 3}, {-5, 4}})
Z = {{a, b}, {c,d}}
Z = product_matrix(Z,Z)
W = product_matrix(A.tbl,Z)
\end{tkzelements}
\parindent=0pt
\verb|A = matrix : new ({{1, 2}, {1,3}})|
Matrix A:\directlua{print_matrix(A.tbl)}
\verb|Z = {{a, b}, {c,d}}|
Matrix Z*Z:\directlua{print_matrix(Z)}
Matrix W=A*Z:\directlua{print_matrix(W)}
\end{document}
Respuesta1
Si lo desea, puede aceptar la clase externa o los tbl
datos internos, verificando en tiempo de ejecución los tipos de argumentos, para poder pasar A
oA.tbl
\documentclass{article}
\usepackage{tkz-euclide}
\usepackage{tkz-elements}
\begin{document}
\LuaCodeDebugOn
\begin{luacode}
matrix={}
function matrix: new (value)
local type = 'matrix'
local rows = #value
local cols = #value[1]
local tbl = value
local o = {tbl = tbl,
rows = rows,
cols = cols,
type = type }
setmetatable(o, self)
self.__index = self
return o
end
return matrix
\end{luacode}
\begin{luacode}
function print_matrix(matrix)
local mdata = (matrix.type=='matrix' and matrix.tbl or matrix)
tex.print("\\{%")
for i = 1, #mdata do
local row = mdata[i]
local row_str = "{\\{"
for j = 1, #row do
row_str = row_str .. " " .. tostring(row[j])
if j < #row then
row_str = row_str .. ","
end
end
row_str = row_str .. "\\}}"
tex.sprint(row_str)
end
tex.print("\\}")
end
function product_matrix(A, B)
local adata = (A.type=='matrix' and A.tbl or A)
local bdata = (B.type=='matrix' and B.tbl or B)
local C = {}
for i = 1, #adata do
C[i] = {}
for j = 1, #bdata[1] do
local num = adata[i][1] * bdata[1][j]
for k = 2, #adata[1] do
num = num + adata[i][k] * bdata[k][j]
end
C[i][j] = num
end
end
return C
end
\end{luacode}
\begin{tkzelements}
a = point(1,0)
b = point(0,1)
c = point(1,1)
d = point(1,-1)
A = matrix : new ({{1, 2}, {1,3}})
B = matrix : new ({{2, 3}, {-5, 4}})
Z = {{a, b}, {c,d}}
Z = product_matrix(Z,Z)
W = product_matrix(A.tbl,Z)
WW = product_matrix(A,Z)
\end{tkzelements}
\parindent=0pt
\verb|A = matrix : new ({{1, 2}, {1,3}})|
Matrix A:\directlua{print_matrix(A)}
Matrix A (tbl):\directlua{print_matrix(A.tbl)}
\verb|Z = {{a, b}, {c,d}}|
Matrix Z*Z:\directlua{print_matrix(Z)}
Matrix tbl W=A*Z:\directlua{print_matrix(WW)}
Matrix W=A*Z:\directlua{print_matrix(W)}
\end{document}