我正在尝试用 LaTeX 编写伪代码算法,其中一个函数需要矩阵作为参数。我需要让它看起来像这样:
我一直在使用算法包,但如果我尝试在参数字段中使用矩阵,它将无法编译。
\begin{algorithm}
\floatname{algorithm}{Algoritmo \arabic{chapter}.}
\label{coordenatesGenerator}
\caption{Generador de una variable aleatoria normal estándar}
\begin{algorithmic}[1]
\Function{GeneradorNormalBivariada}{$\mu \in \Re^{2}, \Sigma \in \Re^{2} \times \Re^{2}; \{ u_{k}\}^{4}_{k-1} \in (0,1)^{4}$}
\State $\Sigma \gets CC^{t}$ \Comment{Descomposición de Cholesky}
\State $z_{1} \gets GeneradorNormalEstandar(u_{1},u_{2});$
\State $z_{2} \gets GeneradorNormalEstandar(u_{3},u_{4});$
\State $x_{1} \gets \mu_{1} + c_{1,1}z_{1};$
\State $x_{2} \gets \mu_{2} + c_{1,1}z_{1} + c_{2,1}z_{1} + c_{2,2}z_{2};$
\State \textbf{return} $\left( \begin{bmatrix} x_{1} \\ x_{2} \end{bmatrix} \right);$
\EndFunction
\end{algorithmic}
\end{algorithm}
我需要替换矩阵指定行中的\mu
和值。\Sigma
\Function{GeneradorNormalBivariada}{$\mu \in \Re^{2}, \Sigma \in \Re^{2} \times \Re^{2}; \{ u_{k}\}^{4}_{k-1} \in (0,1)^{4}$}
感谢您就此问题提供的任何帮助。谢谢。
答案1
由于函数调用的参数是通过条件测试的\ifthenelse
,因此最好将矩阵的构造存储在已设置的框中。下面我创建了它,\mumatrix
并通过函数的第二个参数\sigmamatrix
使用它。\usebox
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage{amsmath}
% https://tex.stackexchange.com/a/145738/5764
\algdef{SE}[FUNCTION]{Function}{EndFunction}%
[2]{\algorithmicfunction\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
{\algorithmicend\ \algorithmicfunction}%
\newcommand{\functioncall}{\textproc}
\begin{document}
\begin{algorithm}
\caption{An algorithm}
\newsavebox\mumatrix
\savebox{\mumatrix}{$\begin{bmatrix} \mu_1 \\ \mu_2 \end{bmatrix}$}%
\newsavebox\sigmamatrix
\savebox{\sigmamatrix}{$\begin{bmatrix} \sigma_1^2 & \sigma_{1,2} \\ \sigma_{2,1} & \sigma_2^2 \end{bmatrix}$}%
\begin{algorithmic}[1]
\Function{FunctionA}{$
\usebox{\mumatrix} \in \Re^2,
\usebox{\sigmamatrix} \in \Re^2 \times \Re^2,
\{ u_k \}^4_{k - 1} \in (0,1)^4$}
\State $\Sigma \gets CC^t$ \Comment{A comment}
\State $z_1 \gets \functioncall{FunctionB}(u_1, u_2);$
\State $z_2 \gets \functioncall{FunctionB}(u_3, u_4);$
\State $x_1 \gets \mu_1 + c_{1,1} z_1;$
\State $x_2 \gets \mu_2 + c_{1,1} z_1 + c_{2,1} z_1 + c_{2,2} z_2;$
\State \Return $\left( \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \right);$
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}
答案2
您可以使用smallmatrix
内联设置矩阵:
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage{mathtools}
\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
\Function{GeneradorNormalBivariada}{$\mu \in \Re^{2}, \Sigma \in \Re^{2} \times \Re^{2}; \{ u_{k}\}^{4}_{k-1} \in (0,1)^{4}$}
\State $\Sigma \gets CC^{t}$ \Comment{Descomposición de Cholesky}
\State $z_{1} \gets GeneradorNormalEstandar(u_{1},u_{2});$
\State $z_{2} \gets GeneradorNormalEstandar(u_{3},u_{4});$
\State $x_{1} \gets \mu_{1} + c_{1,1}z_{1};$
\State $x_{2} \gets \mu_{2} + c_{1,1}z_{1} + c_{2,1}z_{1} + c_{2,2}z_{2};$
\State \textbf{return} $\left(\bigl[\begin{smallmatrix} x_{1} \\ x_{2} \end{smallmatrix}\bigr]\right);$
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}