![函数中嵌套数组出错](https://linux22.com/image/410599/%E5%87%BD%E6%95%B0%E4%B8%AD%E5%B5%8C%E5%A5%97%E6%95%B0%E7%BB%84%E5%87%BA%E9%94%99.png)
\usetikzlibrary{calc}
\newcommand{\matmul}[3]{%
\foreach \i [count=\ci] in {#3}{%
\foreach \j [count=\cj] in {#2}{%
\pgfmathparse{{\i}[0]*{\j}[0]+{\i}[1]*{\j}[1]+{\i}[2]*{\j}[2]}%
\ifnum\cj=1%
\xdef\temp{\pgfmathresult}%
\else
\xdef\temp{\temp,\pgfmathresult}%
\fi
}%
\ifnum\ci=1%
\xdef#1{{\temp}}%
\else
\xdef#1{#1,{\temp}}%
\fi
}%
}
\def\rotationX{{1,2,3},{4,5,6}}
\matmul{\yeet}{{\rotationX}}{{1,2,3},{1,2,3}}
$\yeet$
这是将矩阵相乘的代码。当我尝试使用 \rotationX 作为矩阵列表之一来运行它时,它给出了此错误:
! Missing number, treated as zero.
<to be read again>
{
l.86 \matmul{\yeet}{{\rotationX}}{{1,2,3},{1,2,3}}
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)
如果它使用嵌套数组来运行,例如:
\matmul{\yeet}{{1,2,3},{1,2,3}}{{4,5,6},{4,5,6}}$\yeet$
它完全没问题并返回:
32.0,32.0,32.0,32.0
仅供参考,我正在使用 discord 机器人来做我的 tikz,所以我不使用 \begin{document},因为我不知道如何编译 tikz
答案1
我想我们正在讨论这个问题。应对不同的用例总是有挑战的。在这方面,扩大论点是有效的。
\documentclass{article}
\usepackage{pgffor}
\newcommand{\matmul}[3]{%
\begingroup\edef\matA{#2}%
\edef\matB{#3}%
\foreach \i [count=\ci] in \matB{%
\foreach \j [count=\cj] in \matA{%
\pgfmathparse{{\i}[0]*{\j}[0]+{\i}[1]*{\j}[1]+{\i}[2]*{\j}[2]}%
\ifnum\cj=1%
\xdef\temp{\pgfmathresult}%
\else
\xdef\temp{\temp,\pgfmathresult}%
\fi
}%
\ifnum\ci=1%
\xdef#1{{\temp}}%
\else
\xdef#1{#1,{\temp}}%
\fi
}\endgroup%
}
\begin{document}
\edef\rotationX{{1,2,3},{4,5,6}}
\matmul{\yeet}{\rotationX}{{1,2,3},{1,2,3}}
$\yeet$
\end{document}