How can I compute the partial sum of a sequence of integers? This is what I have so far
\documentclass{article}
\usepackage{calc, forloop, xparse}
\ExplSyntaxOn
\seq_new:N \g_tmp_seq
\seq_new:N \g_partialsum_seq
\seq_set_from_clist:Nn \g_tmp_seq {3, 1, 2, 4, 0, 1}
\newcounter{i}
\newcounter{j}
\newcounter{lengthplusone}
\setcounter{lengthplusone}{\seq_count:N \g_tmp_seq + 1}
\forloop{i}{1}{ \value{i} < \value{lengthplusone} }{
\seq_gput_right:Nn \g_partialsum_seq {0
\forloop{j}{1}{ \value{j} < \value{i} }{
+ \seq_item:Nn \g_tmp_seq { \value{j} - 1}
}
}
}
\cs_new:Npn \partialsum #1 {
\seq_item:Nn \g_partialsum_seq { #1 }
}
\ExplSyntaxOff
\begin{document}
\partialsum{1} % should be 0
\partialsum{2} % should be 3
\partialsum{3} % should be 4
\partialsum{4} % should be 6
\partialsum{5} % should be 10
\partialsum{6} % should be 10
\end{document}
Unfortunately the sum doesn't get expanded, plus all the values of \partialsum
are equal to the last summation.
\partialsum{1} % should be 0
: Shouldn't it be 3 (i.e., the first item in the sequence)?\partialsum{n} = \sum_{i = 1}^{n - 1} \g_tmp_seq{i}
forn > 1
and\partialsum{1} = 0
.