[Tex/LaTex] How to make a matrix that wolfram and symbolab can read (ideally through lyx) dsafds

lyxmatrices

I would like to be able to visually write math equations in lyx and then copy them to wolfram and symbolab to compute the answer. For example I can copy

\left(-1-\lambda\right)\cdot\left(1-\lambda\right)\left(-\lambda\right)+3

into those sites and they will be able to read it and give me the answer, however they do not understand

\begin{pmatrix}1 & 0 & 1\\
0 & 0 & 1\\
-3 & 1 & 3
\end{pmatrix}\cdot\begin{pmatrix}1 & -1 & 0\\
3 & -6 & 1\\
0 & 1 & 0
\end{pmatrix}

Do you have any recommendations for how to format it so that those sites will be able to solve the question?

Best Answer

Wolfram Alpha recognizes \cdot, but it is not a full TeX parser. Example for the matrix syntax in Wolfram Alpha (Examples > Mathematics > Matrices & Linear Algebra):

{{6, -7, 10}, {0, 3, -1}, {0, 5, -7}}

The following example defines a macro \wapmatrix, which takes a nested comma separated list as matrix like the syntax for Wolfram Alpha and writes the matrix to the screen/.log file output. Also it translates the matrix to LaTeX and sets the matrix in environment pmatrix:

\documentclass{article}
\usepackage{amsmath}
\usepackage{kvsetkeys}

\makeatletter
\newif\ifwa@first@row
\newif\ifwa@first@cell@in@row
\newcommand*{\wapmatrix}[1]{%
  \toks0={\begin{pmatrix}}%
  \toks2={}%
  \wa@first@rowtrue
  \comma@parse{#1}\wa@parse@rows
  \toks0=\expandafter{\the\toks0 \end{pmatrix}}%
  \toks2=\expandafter{\expandafter{\the\toks2}}%
  \typeout{\the\toks2}%
  \the\toks0 %
}
\newcommand*{\wa@parse@rows}[1]{%
  \ifwa@first@row
    \wa@first@rowfalse
  \else
    \toks0=\expandafter{\the\toks0 \\\relax}%
    \toks2=\expandafter{\the\toks2 ,}%
  \fi
  \wa@first@cell@in@rowtrue
  \toks4={}%
  \comma@parse{#1}\wa@parse@cells@in@row
  \toks2=\expandafter{\the\toks2\expandafter{\the\toks4}}%
}
\newcommand*{\wa@parse@cells@in@row}[1]{%
  \ifwa@first@cell@in@row
    \wa@first@cell@in@rowfalse
  \else
    \toks0=\expandafter{\the\toks0 &}%
    \toks4=\expandafter{\the\toks4 ,}%
  \fi
  \toks0=\expandafter{\the\toks0 #1}%
  \toks4=\expandafter{\the\toks4 #1}%
}

\newcommand*{\wacdot}{%
  \typeout{\string\cdot}%
  \cdot
}

\begin{document}
\[
  \typeout{--- WA begin ---}
  \wapmatrix{
    {0, 0, 1},
    {-3, 1, 3},
    {1, 2, 3}
  }%
  \wacdot
  \wapmatrix{
    {3, -6, 1},
    {0, 1, 0},
    {1, 2, 3}
  }
  \typeout{--- WA end ---}
\]
\end{document}

Result

Screen/.log file:

--- WA begin ---
{{0,0,1},{-3,1,3},{1,2,3}}
\cdot
{{3,-6,1},{0,1,0},{1,2,3}}
--- WA end ---

Then the text between the markers can be cut & pasted as query for Wolfram Alpha:

Copyable plaintext:

(1 | 2 | 3
-6 | 25 | 6
6 | 2 | 10)

Wolfram Language plaintext output:

{{1, 2, 3}, {-6, 25, 6}, {6, 2, 10}}

Then it can be copied back to the LaTeX file:

  \typeout{--- WA end ---}
  =
  \wapmatrix{{1, 2, 3}, {-6, 25, 6}, {6, 2, 10}}
\]

Result