以下代码可能存在很多问题,但它可以编译。我想构建一个(伪)类来引入元方法(+、-、*),但首先我遇到了一个小问题,这是我在包中的其他类中没有遇到过的。为了能够使用矩阵 A,我的代码(我借用了左右几个部分)要求我编写代码A.tbl
来操作它。是否可以修改我的代码以使用A
而不是A.tbl
?备注:chatGPT 讨厌我的代码,可能是对的!
\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}
答案1
如果你愿意,你可以接受外部类或内部tbl
数据,在运行时检查参数类型,这样你就可以传入A
或A.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}