LaTeX 3을 사용한 유한 계열의 합

LaTeX 3을 사용한 유한 계열의 합

유한 계열의 값을 계산하는 것이 가능합니까?

합집합

LaTeX 3를 사용하시나요?

답변1

예, 가능하며 매우 쉽습니다.

\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}

세 번째 인수는 #1합계 지수를 나타냅니다.

이 예에서는 가 로 #1^2전달됩니다 . 인수가 현재 인덱스를 나타내는 내부에서 사용되기 때문에 마법이 발생합니다 .#3\svend_compute_sum:nnn\int_step_inline:nnnn#1;-)

인수는 부동 소수점 표현식에 적합한 코드여야 합니다. 따라서 직접 정의하지 않으면 계승을 평가할 수 없습니다.

여기에 이미지 설명을 입력하세요

피합이 항상 정수인 경우 모두 fp를 로 변경할 수 있습니다 int.

또한 로드 siunitx하고 내부 기능을 다음으로 변경 하면

\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 }
 }

그 다음에

$\computesum{0}{300}{#1^2}$

다음과 같이 인쇄됩니다

여기에 이미지 설명을 입력하세요

답변2

로드할 수도 있습니다xintexpr이를 위해 LaTeX3잠시 휴식을 취하십시오.

합계

\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}

관련 정보