在伪代码中插入方程式的正确方法是什么
What is the right way to insert an equation within a pseudo code
我开始用 Latex 写笔记,我正在尝试根据这个等式编写伪代码:
这是我到目前为止尝试过的代码。
\begin{addmargin}[10mm]{0mm}
\textbf{Repeat until convergence:} \ \{
\[
\theta_j := \theta_j + \alpha \sum_{i=1}^m(y^{(i)}-h_\theta(x^{(i)})) \ x_j^{(i)} \quad (\text{for every} \ j).
\]
\}
\end{addmargin}
我还是觉得有点乱。你有更简单的写法吗?
这个问题可能应该移到 tex.stackexchange。
存在几个不同的整洁的 Latex 包,专门用于编写伪代码。一种更简单的方法是使用 Algorithmicx
package (package documentation here),特别是其中的 algpseudocode
部分。
这是使用 Algoritmicx 的伪代码的 MWE:
\documentclass{article}
\usepackage{amsmath}
\usepackage{algpseudocode}
\renewcommand{\algorithmicforall}{\textbf{for each}}
\begin{document}
\noindent Let $\mathcal{J} := \{1, ..., n\}$.
\begin{algorithmic}
\While {Some convergence criteria is not fulfilled ...}
\ForAll {$j \in \mathcal{J}$}
\State $\theta_j \gets \theta_j + \alpha \sum_{i=1}^m(y^{(i)}-h_\theta(x^{(i)})) \ x_j^{(i)}$
\EndFor
\State {... update some convergence measure} \Comment{Describe convergence measure}
\EndWhile
\end{algorithmic}
\end{document}
请注意,在第四个非空行中,我已经重载了 algorithmicx (\algorithmicforall
) 中 \ForAll
命令的文本,变成了“for .. . each” 而不是“for ... all”.
另请注意,通常只有评论可能包含伪代码中的文本,因此句子“未满足某些收敛标准...”和“” ...更新一些收敛措施 " 以上应该替换为您定义的 algorithm/equation.
收敛
这是您想要的,来自软件包 algorithm
和 algorithmic
\begin{algorithm}
\caption{Gradient Descent}\label{alg-gd}
\begin{algorithmic}[1]
\STATE Initialize \( \theta_{0} = k\in \R^d \)
\STATE Initialize \( t \leftarrow 0 \)
\REPEAT
\STATE \( \forall j \in \{1,..., d\}, \theta_{j,t+1} \leftarrow \theta_{j,t} - \alpha \frac{\p }{\p \theta_{j} } L(\theta )\bigg|_{\theta = \theta_{t} } \)
\STATE \( t \leftarrow t + 1 \)
\UNTIL{convergence}
这给出了,
注意:如果您在 beamer 中使用它,请将 \begin{algorithm}[H]
放在上面的行中,而不仅仅是 \begin{algorithm}
我开始用 Latex 写笔记,我正在尝试根据这个等式编写伪代码:
这是我到目前为止尝试过的代码。
\begin{addmargin}[10mm]{0mm}
\textbf{Repeat until convergence:} \ \{
\[
\theta_j := \theta_j + \alpha \sum_{i=1}^m(y^{(i)}-h_\theta(x^{(i)})) \ x_j^{(i)} \quad (\text{for every} \ j).
\]
\}
\end{addmargin}
我还是觉得有点乱。你有更简单的写法吗?
这个问题可能应该移到 tex.stackexchange。
存在几个不同的整洁的 Latex 包,专门用于编写伪代码。一种更简单的方法是使用 Algorithmicx
package (package documentation here),特别是其中的 algpseudocode
部分。
这是使用 Algoritmicx 的伪代码的 MWE:
\documentclass{article}
\usepackage{amsmath}
\usepackage{algpseudocode}
\renewcommand{\algorithmicforall}{\textbf{for each}}
\begin{document}
\noindent Let $\mathcal{J} := \{1, ..., n\}$.
\begin{algorithmic}
\While {Some convergence criteria is not fulfilled ...}
\ForAll {$j \in \mathcal{J}$}
\State $\theta_j \gets \theta_j + \alpha \sum_{i=1}^m(y^{(i)}-h_\theta(x^{(i)})) \ x_j^{(i)}$
\EndFor
\State {... update some convergence measure} \Comment{Describe convergence measure}
\EndWhile
\end{algorithmic}
\end{document}
请注意,在第四个非空行中,我已经重载了 algorithmicx (\algorithmicforall
) 中 \ForAll
命令的文本,变成了“for .. . each” 而不是“for ... all”.
另请注意,通常只有评论可能包含伪代码中的文本,因此句子“未满足某些收敛标准...”和“” ...更新一些收敛措施 " 以上应该替换为您定义的 algorithm/equation.
收敛这是您想要的,来自软件包 algorithm
和 algorithmic
\begin{algorithm}
\caption{Gradient Descent}\label{alg-gd}
\begin{algorithmic}[1]
\STATE Initialize \( \theta_{0} = k\in \R^d \)
\STATE Initialize \( t \leftarrow 0 \)
\REPEAT
\STATE \( \forall j \in \{1,..., d\}, \theta_{j,t+1} \leftarrow \theta_{j,t} - \alpha \frac{\p }{\p \theta_{j} } L(\theta )\bigg|_{\theta = \theta_{t} } \)
\STATE \( t \leftarrow t + 1 \)
\UNTIL{convergence}
这给出了,
注意:如果您在 beamer 中使用它,请将 \begin{algorithm}[H]
放在上面的行中,而不仅仅是 \begin{algorithm}