Construindo uma classe para matrizes

Construindo uma classe para matrizes

O código a seguir pode ter muitos problemas, mas é compilado. Quero construir uma (pseudo)classe para introduzir metamétodos (+,-,*), mas primeiro tenho um pequeno problema que não encontrei com as outras classes do meu pacote. Para poder usar a matriz A, sou obrigado pelo meu código (peguei algumas partes emprestadas a torto e a direito) a escrever A.tblpara manipulá-lo. É possível modificar meu código para usar Ae não A.tbl? Observação: chatGPT odeia meu código e provavelmente está certo!

\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}

insira a descrição da imagem aqui

Responder1

insira a descrição da imagem aqui

Você pode, se desejar, aceitar a classe externa ou os tbldados internos, verificando em tempo de execução os tipos de argumentos, para poder passar AouA.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}

informação relacionada