# Easy Octave Algorithm 2

$\def\A{\mathscr{A}}\def\B{\mathscr{B}}\def\C{\mathscr{C}}$ This page contains an Octave version of Algorithm 2, on page 57 of the B-series book. This function is intended to run equally well on Matlab, SciLab, as well as Octave. Let \begin{align} \A(x) &= \sum_{i=1}^{\infty} a_i x^i,\\ \B(x) &= \sum_{i=1}^{\infty} b_i x^i,\\ \C(x) &= \sum_{i=1}^{\infty} c_i x^i, \end{align} where \begin{align} a_i &\text{ is the number of trees of order $i$},\\ b_i &\text{ is the number of unrooted trees of order $i$},\\ c_i &\text{ is the number of non-superflous}\\ &\hspace{2cm}\text{ unrooted trees of order $i$}. \end{align} Algorithm 2 calculates the coefficients in $\B$ and $\C$ using the formulae \begin{align} \B(x)&= \A(x)-\tfrac12\big(\A(x)^2-\A(x^2)\big),\\ \C(x) &= \A(x)-\tfrac12\big(\A(x)^2+\A(x^2)\big). \end{align}

#### Octave function definition

function [Utrees,  UNtrees] = alg2(pmax)
Ntrees=alg1(pmax)
Xtrees=0 * Ntrees
Ytrees=0 * Ntrees
for i=1: pmax - 1
for j=1: pmax - i
Xtrees(i+j)=Xrees(i+j)+Ntrees(i)*Ntrees(j)
end
end
for i=1: pmax/2
Ytrees(2*i)=Ntrees(i)
end
Utrees=Utrees-(Xtrees-Ytrees)/2
UNtrees=Utrees-(Xtrees+Ytrees)/2
end


$\to$ B-series book
$\to$ Homepage