Algorithm2e – Defining 2 separate functions in same document

algorithm2ealgorithms

I am new to using Algorithm2e in Latex, and Latex in general. I am trying to define multiple functions in one document. I have this so far:

\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}


\begin{document}

\begin{algorithm}[!ht]

\DontPrintSemicolon
{
    \KwIn{tags per post ($T_i,_p$) within a given cell ($C$) for the        entire grid ($G$) }
    \KwOut{set of weighted tags within given cell (\^T$_c$) calculated across the entire grid (G)}
}
\SetKwFunction{FMain}{tfidf}
\SetKwProg{Fn}{Function}{:}{}
\Fn{\FMain{$T_i,_p$, $C$, $G$}}
{
\ForAll{$C$ within G}
{
    \ForEach{($T_i,_p$) within C}{
    $TermFrequency$\;
}
{
    \ForEach{tag($t$) across $G$}{
    $InverseDocumentFrequency$\;
}
    \ForEach{($T_i,_p$) within $C$ for $G$}{
    \^T$_c$ $\leftarrow$TF-IDF($t$, $C$,$G$) 
}
}
}
{return \^T$_c$\;}
}

\caption{Calculation of TF-IDF}
\end{algorithm}


\begin{algorithm}[!ht]
\DontPrintSemicolon

{
\KwIn{spatially normalised tags in cell $(V_1)$, spatially normalised tags in cell $(V_2)$}
\KwOut{cosine similarity measure ($C_s$) for $V_1$, $V_2$}
}

\SetKwFunction{FTest}{cosine_similarity}
\SetKwProg{FTest}{Function}{:}{}
\FTest{\FTest{$V_1$, $V_2$}}
{
    \For{$V_1$, $V_2$}{
        transform($V_1$, $V_2$) \tcp*[f]{Transform vectors to same magnitude}}
$C_s$$\leftarrow$ cos($V_1$, $V_2$) = $V_1$ . $V_2$ / ||$V_1$|| * ||$V_2$||
{return $C_s$\;}
}

\caption{Calculation of Semantic Similarity (Cosine-based algorithm)}
\end{algorithm}
\end{document}

It prints out the following. The first function is fine, however the syntax is not doing what I want to in the 2nd algorithm/function. How do I define the algorithm so that is writes "Function Cosine_similarity(V1, V2):" in line 1?

enter image description here

Best Answer

You have

\FTest{\FTest{$V_1$, $V_2$}}

which should be

\Ftest{$V_1,V_2$}

I've made several changes, please take note of them.

\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}


\begin{document}

\begin{algorithm}[!ht]

\DontPrintSemicolon
\KwIn{tags per post ($T_i,_p$) within a given cell ($C$) for the entire grid ($G$) }
\KwOut{set of weighted tags within given cell ($\hat{T}_c$) calculated across the entire grid ($G$)}
\SetKwFunction{FMain}{tfidf}
\SetKwProg{Fn}{Function}{:}{}
\Fn{\FMain{$T_{i,p}$, $C$, $G$}}
  {
   \ForAll{$C$ within $G$}
     {
      \ForEach{$(T_{i,p})$ within $C$}
        {
         TermFrequency\;
        }
        {
         \ForEach{tag$(t)$ across $G$}
           {
            InverseDocumentFrequency\;
           }
        \ForEach{$(T_{i,p})$ within $C$ for $G$}
          {
           $\hat{T}_c\leftarrow\FMain{$t, C,G$}$
          }
        }
     }
     {
      return $\hat{T}_c$\;
     }
  }
\caption{Calculation of TF-IDF}
\end{algorithm}


\begin{algorithm}[!ht]
\DontPrintSemicolon
\KwIn{spatially normalised tags in cell $(V_1)$, spatially normalised tags in cell $(V_2)$}
\KwOut{cosine similarity measure ($C_s$) for $V_1$, $V_2$}
\SetKwFunction{FTest}{cosine_similarity}
\SetKwProg{FTest}{Function}{:}{}
\FTest{$V_1,V_2$}
  {
   \For{$V_1$, $V_2$}
     {
      transform($V_1$, $V_2$) \tcp*[f]{Transform vectors to same magnitude}
     }
   $C_s\leftarrow \cos(V_1, V_2) = V_1 \bullet V_2 / (\|V_1\|\cdot\|V_2\|)$
     {
      return $C_s$\;
     }
  }
\caption{Calculation of Semantic Similarity (Cosine-based algorithm)}
\end{algorithm}

\end{document}

enter image description here