LaTeX伪代码学习笔记 20200902

LaTeX伪代码学习笔记_20200902

参考:

from http://hustsxh.is-programmer.com/posts/38801.html
https://tex.stackexchange.com/questions/229355/algorithm-algorithmic-algorithmicx-algorithm2e-algpseudocode-confused
https://blog.csdn.net/lwb102063/article/details/53046265

经过作者的略微修正并添加了一些自己的看法

algorithmic 和 algorithmicx

from http://hustsxh.is-programmer.com/posts/38801.html

algorithmic宏包和algorithmicx宏包的使用相似,很多命令都是一样的,只是algorithmic的命令都是大写,algorithmicx的命令都是首字母大写,其他小写(EndFor两个大写)。

algorithmic 基本命令:

\STATE <text>

\IF{
   <condition>} \STATE{
   <text>} \ENDIF

\FOR{
   <condition>} \STATE{
   <text>} \ENDFOR

\FOR{
   <condition> \TO <condition> } \STATE{
   <text>} \ENDFOR

\FORALL{
   <condition>} \STATE{
   <text>} \ENDFOR

\WHILE{
   <condition>} \STATE{
   <text>} \ENDWHILE

\REPEAT \STATE{
   <text>} \UNTIL{
   <condition>}

\LOOP \STATE{
   <text>} \ENDLOOP

\REQUIRE <text>

\ENSURE <text>

\RETURN <text>

\PRINT <text>

\COMMENT{
   <text>}

\AND, \OR, \XOR, \NOT, \TO, \TRUE, \FALSE

algorithmicx 基本命令

\State <text>

\If{
   <condition>} <text> \EndIf

\If{
   <condition>} <text> \Else <text> \EndIf

\If{
   <condition>} <text> \ElsIf{
   <condition>}  <text> \Else <text> \EndIf

\For{
   <condition>} <text> \EndFor

\ForAll{
   <condition>} <text> \EndFor

\While{
   <condition>} <text> \EndWhile

\Repeat <text> \Until{
   <condition>}

\Loop <text> \EndLoop

\Require <text>

\Ensure <text>

\Function{
   <name>}{
   <params>} <body> \EndFunction

\State \Return <text>

\Comment{
   <text>}

另外,还有3个修改algorithm标签,require标签,ensure标签显示的三个命令:

\floatname{
   algorithm}{
   算法}

\renewcommand{
   \algorithmicrequire}{
   \textbf{
   输入:}} 

\renewcommand{
   \algorithmicensure}{
   \textbf{
   输出:}}

algorithmicx例子

用algorithmicx写了下归并排序求逆序数,代码如下

\documentclass[11pt]{
   article}
\usepackage{
   ctex}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{
   geometry}
\usepackage{
   algorithm}
%\usepackage{
   algorithmicx} %使用algpsudocode时会自动引入algorithmicx宏包,所以此行不需要
\usepackage{
   algpseudocode}
\usepackage{
   amsmath}
 
\floatname{
   algorithm}{
   算法}
\renewcommand{
   \algorithmicrequire}{
   \textbf{
   输入:}}
\renewcommand{
   \algorithmicensure}{
   \textbf{
   输出:}}
 
\begin{
   document}
    \begin{
   algorithm}           % 注意,要在algorithm环境内先定义一个caption(算法名称)[或者再来一个label用以引用],再包裹一个algorithmic环境
        \caption{
   用归并排序求逆序数}  %算法名
        \label{
   First Algorithm}
        \begin{
   algorithmic}[1] %每行显示行号
            \Require $Array$数组,$n$数组大小  %输入的内容
            \Ensure 逆序数                    %输出的内容
            \Function {
   MergerSort}{
   $Array, left, right$}
                \State $result \gets 0$                      %\gets相当于编程语言中的 "="
                \If {
   $left < right$}
                    \State $middle \gets (left + right) / 2$
                    \State $result \gets result +$ \Call{
   MergerSort}{
   $Array, left, middle$}    %调用函数用 \Call
                    \State $result \gets result +$ \Call{
   MergerSort}{
   $Array, middle, right$}
                    \State $result \gets result +$ \Call{
   Merger}{
   $Array,left,middle,right$}
                \EndIf
                \State \Return{
   $result$}   %函数的返回, 其中\State起到重启一行的作用
            \EndFunction
            \State    %\State起到独占一空行的作用
            \Function{
   Merger}{
   $Array, left, middle, right$}
                \State $i\gets left$
                \State $j\gets middle$
                \State $k\gets 0$
                \State $result \gets 0$
                \While{
   $i<middle$ \textbf{
   and} $j<right$}
                    \If{
   $Array[i]<Array[j]$}
                        \State $B[k++]\gets Array[i++]$
                    \Else
                        \State $B[k++] \gets Array[j++]$
                        \State $result \gets result + (middle - i)$
                    \EndIf
                \EndWhile
                \While{
   $i<middle$}
                    \State $B[k++] \gets Array[i++]$
                \EndWhile
                \While{
   $j<right$}
                    \State $B[k++] \gets Array[j++]$
                \EndWhile
                \For{
   $i = 0 \to k-1$}
                    \State $Array[left + i] \gets B[i]$
                \EndFor
                \State \Return{
   $result$}
            \EndFunction
        \end{
   algorithmic}
    \end{
   algorithm}
\end{
   document}

algorithm例子

from https://blog.csdn.net/lwb102063/article/details/53046265

前期准备:

\usepackage{
   algorithm}
\usepackage{
   algpseudocode}
\usepackage{
   amsmath}
\renewcommand{
   \algorithmicrequire}{
   \textbf{
   Input:}}  % Use Input in the format of Algorithm
\renewcommand{
   \algorithmicensure}{
   \textbf{
   Output:}} % Use Output in the format of Algorithm

Example 1:

\documentclass{
   article}
    

\usepackage{
   algorithm}
\usepackage{
   algpseudocode}
\usepackage{
   amsmath}
\renewcommand{
   \algorithmicrequire}{
   \textbf{
   Input:}}  % Use Input in the format of Algorithm
\renewcommand{
   \algorithmicensure}{
   \textbf{
   Output:}} % Use Output in the format of Algorithm

\begin{
   document}
  \begin{
   algorithm}[htb]
  \caption{
    Framework of ensemble learning for our system.}
  \label{
   alg:Framwork}
  \begin{
   algorithmic}[1]
    \Require
      The set of positive samples for current batch, $P_n$;
      The set of unlabelled samples for current batch, $U_n$;
      Ensemble of classifiers on former batches, $E_{
   n-1}$;
    \Ensure
      Ensemble of classifiers on the current batch, $E_n$;
    \State Extracting the set of reliable negative and/or positive samples $T_n$ from $U_n$ with help of $P_n$;
    \label{
   code:fram:extract}
    \State Training ensemble of classifiers $E$ on $T_n \cup P_n$, with help of data in former batches;
    \label{
   code
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值