
É possível calcular o valor de uma série finita, digamos,
usando LaTeX 3?
Responder1
Sim, você pode, e com bastante facilidade também.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\computesum}{mmm}
{% pass control to an internal function
\svend_compute_sum:nnn { #1 } { #2 } { #3 }
}
% a variable for storing the partial sums
\fp_new:N \l_svend_partial_sum_fp
\cs_new_protected:Npn \svend_compute_sum:nnn #1 #2 #3
{
% clear the variable
\fp_zero:N \l_svend_partial_sum_fp
% for k from #1 to #2 ...
\int_step_inline:nnnn { #1 } { 1 } { #2 }
{
% ... add the current value to the partial sum so far
\fp_add:Nn \l_svend_partial_sum_fp { #3 }
}
% deliver the value
\fp_use:N \l_svend_partial_sum_fp
}
\ExplSyntaxOff
\begin{document}
$\computesum{0}{0}{#1^2}$\par
$\computesum{0}{1}{#1^2}$\par
$\computesum{0}{2}{#1^2}$\par
$\computesum{0}{3}{#1^2}$\par
$\computesum{0}{4}{#1^2}$\par
$\computesum{0}{5}{#1^2}$\par
$\computesum{0}{6}{#1^2}$\par
$\computesum{0}{7}{#1^2}$\par
$\computesum{0}{8}{#1^2}$\par
\end{document}
Observe que no terceiro argumento #1
representa o índice de soma.
No exemplo, #1^2
é passado #3
como \svend_compute_sum:nnn
; como o argumento é usado dentro de \int_step_inline:nnnn
, onde #1
representa o índice atual, a mágica acontece.;-)
O argumento deve ser um código legal para expressões de ponto flutuante. Portanto, não há esperança de avaliar fatoriais, a menos que você mesmo os defina.
Se suas somas forem sempre números inteiros, você poderá transformar tudo fp
em int
.
Se você carregar também siunitx
e alterar a função interna para
\cs_new_protected:Npn \svend_compute_sum:nnn #1 #2 #3
{
\fp_zero:N \l_svend_partial_sum_fp
\int_step_inline:nnnn { #1 } { 1 } { #2 }
{
\fp_add:Nn \l_svend_partial_sum_fp { #3 }
}
\num { \fp_use:N \l_svend_partial_sum_fp }
}
então
$\computesum{0}{300}{#1^2}$
vai imprimir como
Responder2
Você pode carregarxintexprpara isso, e permita LaTeX3
um pouco de descanso.
\documentclass[12pt]{article}
\usepackage[hscale=0.75]{geometry}
\usepackage{xintexpr}
\usepackage{siunitx}
\usepackage{shortvrb}
\begin{document}
$$\sum_{i=1}^{300} i^2=\num{\xinttheexpr add(i^2, i=1..300)\relax }$$
% For some reason, this doesn't go through:
% \num{\xintthefloatexpr [14] add(1/i^2,i=1..50)\relax}
% one needs to first expand the \num argument:
% \expandafter\num\expandafter
% {\romannumeral-`0\xintthefloatexpr [14] add(1/i^2,i=1..50)\relax}
%
The float version does each addition with 16 digits floats, hence the last
digit may be a bit off.
$$\sum_{i=1}^{50} \frac1{i^2}=
\xintFrac{\xinttheexpr reduce(add(1/i^2,i=1..50))\relax}
\approx \xintthefloatexpr add(1/i^2,i=1..50)\relax$$
If one has anyhow computed an exact value, it is better to deduce the
float from it rather than evaluating the sum as a sum of floats.
\noindent\verb|\oodef\MySum {\xinttheexpr reduce(-add((-1)^i/i^2,i=1..50))\relax }|
\oodef\MySum {\xinttheexpr reduce(-add((-1)^i/i^2,i=1..50))\relax }
$$\sum_{i=1}^{50} \frac{(-1)^{i-1}}{i^2}=
\xintFrac{\MySum}$$
\verb|$$\xintDigits:=48; \approx\xintthefloatexpr \MySum\relax$$|
$$\xintDigits:=48; \approx\xintthefloatexpr \MySum\relax$$
\end{document}