행렬용 클래스 구축

행렬용 클래스 구축

다음 코드에는 많은 문제가 있을 수 있지만 컴파일됩니다. 메타메소드(+,-,*)를 도입하기 위해 (의사) 클래스를 구축하고 싶지만 먼저 내 패키지의 다른 클래스에서 발생하지 않은 약간의 문제가 있습니다. 행렬 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}

관련 정보