
Der folgende Code kann viele Probleme haben, aber er lässt sich kompilieren. Ich möchte eine (Pseudo-)Klasse erstellen, um Metamethoden (+,-,*) einzuführen, aber zuerst habe ich ein kleines Problem, das ich bei den anderen Klassen in meinem Paket nicht festgestellt habe. Um Matrix A verwenden zu können, bin ich aufgrund meines Codes (ich habe mir links und rechts ein paar Teile ausgeliehen) gezwungen, zu schreiben, A.tbl
um sie zu manipulieren. Ist es möglich, meinen Code so zu ändern, dass er verwendet A
und nicht verwendet wird A.tbl
? Anmerkung: chatGPT hasst meinen Code und hat wahrscheinlich recht!
\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}
Antwort1
Sie können, wenn Sie möchten, entweder die äußere Klasse oder die inneren tbl
Daten akzeptieren und zur Laufzeit die Argumenttypen überprüfen, sodass Sie sie in A
oder übergeben könnenA.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}