
為了產生上述偽代碼,我使用了以下程式碼:
\begin{algorithm}
\caption{sample}\label{sample}
\begin{algorithmic}[1]
\Require $\mathbf{D} \in \mathbb{R}^{M \times N}$
\State $\mathbf{V}^{(0)} \in \mathbb{R}^{K \times N} \gets $ random matrix
\For {$t= 1:T$}
\State $\mathbf{U}^{(t)} \gets Update\mathbf{U}(\mathbf{D},\mathbf{V}^{t-1})$
\State $\mathbf{V}^{(t)} \gets Update\mathbf{V}(\mathbf{D},\mathbf{U}^{t})$
\EndFor
\State \Return $\mathbf{U}^{(T)},\mathbf{V}^{(T)}$
\end{algorithmic}
\end{algorithm}
問題是我必須使用\mathbf
for all D
,U
並將V
它們表示為矩陣或向量。是否有其他方法可以一次性定義這些變數而無需\mathbf
全部使用?
答案1
是的,這裡的典型方法是遵循中的建議一致的排版。也就是說,建立一個定義該概念的巨集並在整個文件中使用它。
\documentclass{article}
\usepackage{algorithm,algpseudocode,amsfonts}
\newcommand{\sampleset}{\mathbf{D}}
\newcommand{\universe}[1]{\mathbb{R}^{#1}}
\newcommand{\setA}{\mathbf{U}}
\newcommand{\setB}{\mathbf{V}}
\begin{document}
\begin{algorithm}
\caption{sample}\label{sample}
\begin{algorithmic}[1]
\Require $\sampleset \in \universe{M \times N}$
\State $\setB^{(0)} \in \universe{K \times N} \gets $ random matrix
\For {$t= 1:T$}
\State $\setA^{(t)} \gets Update\setA(\mathbf{D},\setA^{t-1})$
\State $\setB^{(t)} \gets Update\setB(\mathbf{D},\setB^{t})$
\EndFor
\State \Return $\setA^{(T)},\setB^{(T)}$
\end{algorithmic}
\end{algorithm}
\end{document}
上面我定義了一些變數,但您可以對演算法內的函數執行相同的操作。