2

enter image description here

I have tried the listings package, but the question is that I can not type math equation in it. (The line number on the left start from the first line, i.e. type val is also OK for me.)

3
  • 3
    For things like this, you might be interested in algorithm2e and/or other related pseudocode packages (algorithmicx, algpseudocode, etc.). Jan 2, 2015 at 15:12
  • @PaulGessler: Thanks, I am trying the algorithm2e package.
    – Yulong Ao
    Jan 2, 2015 at 16:11
  • @PaulGessler I think you should turn your comment into an answer? Feb 15, 2015 at 3:14

1 Answer 1

3

An attempt with algorithm2e. There's surely a better way to do the first three lines, and with the better method I bet it is possible to have them unnumbered. But anyway, my first shot (not an expert in typesetting algorithms :-)):

\documentclass{article}
\usepackage{amsmath} % for \text{} in math
\usepackage{geometry} % margin adjust to fit wide comment in one line
\usepackage[
  linesnumbered, % number lines
  noline,        % no vertical rule delimiting blocks
  noend,         % do not typeset "end" of blocks
  nofillcomment, % do not stretch comment width to fill the line
]{algorithm2e}
\SetCommentSty{normalfont}
\SetArgSty{normalfont}
\SetKwComment{tcc}{\texttt{/*} }{ \texttt{*/}}
\SetKwFunction{ptr}{ptr}
\SetKwFunction{ind}{ind}
\SetKwFunction{val}{val}
\SetKwInput{InType}{}
\SetKw{type}{type}
\SetKw{real}{real}
\SetKw{int}{int}

\begin{document}
\begin{algorithm}[H]
  \DontPrintSemicolon
  {\type $\val : \real [K_{rc} \cdot r \cdot c]$}\;
  {\type $\ind : \int [K_{rc}]$}\;
  {\type $\ptr : \int [\frac{m}{r} + 1]$}\;
  \ForEach{block row $I$}{
    $i_o \leftarrow I \cdot r$ \tcc{starting row}
    Let $\hat{y} \leftarrow y_{i_{0}:(i_0)+r-1}$ \tcc{Can store in registers}
    \For{$L=\ptr [I]$ \KwTo $\ptr [I+1] - 1$}{
      $j_0 \leftarrow \ind [L] \cdot c$ \tcc{starting column}
      Let $\hat{x} \leftarrow x_{j_0:(j_0+c-1)}$ \tcc{Can store in registers}
      Let $\hat{A} \leftarrow a_{i_0:(i_o+r-1), j_0:(j_0+c-1)}$
      \tcc{$\hat{A} = \text{block of $A$ stored in $\val [(L \cdot r \cdot c) : ((L+1) \cdot r \cdot c -1)]$}$}
      Perform $r \times c$ block multiply, $\hat{y} \leftarrow \hat{y} + \hat{A} \cdot \hat{x}$
    }
    Store $\hat{y}$
  }
\end{algorithm}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .