2

Here is code for matrix multiplication.

\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function MatMul( m1, m2 )
    if #m1[1] ~= #m2 then       
        return nil
    end

    local res = {}

    for i = 1, #m1 do
        res[i] = {}
        for j = 1, #m2[1] do
            res[i][j] = 0
            for k = 1, #m2 do
                res[i][j] = res[i][j] + m1[i][k] * m2[k][j]
            end
        end
    end

    for i = 1, #res do
    for j = 1, #res[1] do
        tex.sprint( res[i][j] )
        tex.sprint("  ")
    end
    tex.sprint("\n")
end
end
\newcommand\matrixmult[2]{\directlua{MatMul(#1,#2)}}

\begin{document}
\matrixmult{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}
\end{document}

I think this throws error because of incorrect passing of arguments. The arguments are in array or table form in lua. I tested the code in lua and it works fine. So is it possible to pass array of values in lualatex?

6

You are missing \end{luacode}. And \n is not a tex command. With e.g. \\par it works fine:

\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function MatMul( m1, m2 )
    if #m1[1] ~= #m2 then
        return nil
    end

    local res = {}

    for i = 1, #m1 do
        res[i] = {}
        for j = 1, #m2[1] do
            res[i][j] = 0
            for k = 1, #m2 do
                res[i][j] = res[i][j] + m1[i][k] * m2[k][j]
            end
        end
    end

    for i = 1, #res do
    for j = 1, #res[1] do
        tex.sprint( res[i][j] )
        tex.sprint("  ")
    end
    tex.sprint("\\par")
end
end
\end{luacode}
\newcommand\matrixmult[2]{\directlua{MatMul(#1,#2)}}

\begin{document}
\matrixmult{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}
\end{document}

enter image description here

|improve this answer|||||
  • Great...!!! Can code be modified to get the display in parentheses? Something similar to \bmatrix. – user61681 Jul 22 '19 at 12:33
3

Quick solution if you want to be able to generate a matrix with a given environment (such as matrix, pmatrix, bmatrix etc.):

\documentclass{article}
\usepackage{luacode}
\usepackage{amsmath}
\begin{luacode}
function MatMul( m1, m2, matrixtype)
  if #m1[1] ~= #m2 then
    return nil
  end

  local res = {}

  for i = 1, #m1 do
    res[i] = {}
    for j = 1, #m2[1] do
      res[i][j] = 0
      for k = 1, #m2 do
        res[i][j] = res[i][j] + m1[i][k] * m2[k][j]
      end
    end
  end

  tex.sprint("\\begin{" .. matrixtype .. "}")
    for i = 1, #res do
      for j = 1, #res[1] do
        tex.sprint( res[i][j] )
        if j < #res[1] then
          tex.sprint(" & ")
        end
      end
      tex.sprint("\\\\")
    end
    tex.sprint("\\end{" .. matrixtype .. "}")
  end
\end{luacode}
\newcommand\matrixmult[3][matrix]{\directlua{MatMul(#2,#3,"#1")}} % Input the default matrix-environment here ("matrix")

\begin{document}

Display math, with default \verb|matrix|-environment:
\begin{equation}
\matrixmult{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}
\end{equation}

Matrix with parentheses:
$\matrixmult[pmatrix]{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}$

Matrix with braces:
$\matrixmult[Bmatrix]{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}$

Matrix with brackets:
$\matrixmult[bmatrix]{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}$

\end{document}

Output

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.