라텍스 용어 방정식을 Wolfram Alpha에 적합한 버전으로 변환

라텍스 용어 방정식을 Wolfram Alpha에 적합한 버전으로 변환

수학 스택 교환에 대한 조합론 질문을 했고 다음을 받았습니다.LaTeX 맛의 답변. 나는 그 방정식을 볼프람 알파가 이해할 수 있는 것으로 변환하려고 시도했습니다. 그러나 작동하지 않는 것 같습니다. 방정식의 구문을 편집하는 방법에 대한 아이디어가 있습니까? 사용된 구문을 아래에 게시했습니다.

\[
n!+\sum_{i=1}^{n-1}(-1)^i\sum_{k=1}^i\binom{i-1}{i-k}\binom{n-i}{k}2^k(n-i)!
\]

답변1

목차:

  • 다음을 사용한 초기 답변xintexpr그리고 OP의 공식. 구현 확장 가능…

  • 다음에 주어진 반복 공식을 사용한 두 번째 대답http://oeis.org/A002464. 이 두 번째 접근 방식은 주어진 표현식의 모든 값 배열을 한 번에 구성합니다. 패키지를 사용합니다bnumexpr큰 정수 계산의 경우.


TeX 또는 LaTeX를 사용하여 이를 계산할 수 있습니다.

약간 고통스러운 세부 사항은 코드 주석에 자세히 설명되어 있습니다. 주요 내용은 binomial(x,y) 함수가 현재 자동으로 0을 반환하는 대신 y>x인 경우 오류를 발생시킨다는 것입니다.

\documentclass{article}

\usepackage{xintexpr}[2016/03/12]% 1.2f or more recent needed for binomial

\begin{document}

% unfortunately the default binomial(x,y) function raises an error
% if y>x. Hence define wrapper to intercept the case.
\xintdefiifunc bbinomial(x,y):=if(y>x,0,binomial(x,y));

% unfortunately we can not use \xintdefiifunc F(n):=....; syntax because the
% function variable "n" appears in the summation range. This is current
% limitation, documented in §10.10.3 of xint.pdf.

% Hence we use a macro interface, which will need braces: \myF{10}.

% We employ parentheses around #1 in case
% it is itself some math expression.

% we choose \xintiiexpr rather than \xinttheiiexpr, for efficiency
% if used in other expressions.

\newcommand\myF [1]{% n = #1
   \xintiiexpr (#1)!+
   add((-1)^i % probably faster: ifodd(i, -1, +1)
       *add(binomial(i-1,i-k)*bbinomial((#1)-i,k)*2^k, 
       k=1..i)*((#1)-i)!, 
   i=1..(#1)-1)\relax }

% unfortunately in xintiiexpr, currently 1..0 does not evaluate
% to empty range, but proceeds by negative steps, hence evaluate to
% 1, 0 which we don't want.

% 1..[1]..0 would create such an empty range.
% but further problem is that add(<expression>, i = <range>) syntax
% currently does not work with range being empty.

% Consequently the above expression requires n > 1.

% test
% \xinttheiiexpr seq(\myF{n}, n=2..10)\relax

\medskip
\begin{tabular}{c|r}
  $n$&$F(n)$\\
\hline
\xintFor* #1 in {\xintSeq{2}{20}}
\do
{$#1$&$\xintthe\myF{#1}$\\}
\end{tabular}

\end{document}

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


조합 값으로 확장되는 매크로를 단번에 정의하는 보다 현실적인 접근 방식입니다. 반복 공식을 기반으로 합니다.

\documentclass{article}

\usepackage{bnumexpr}

% http://oeis.org/A002464

% If n = 0 or 1 then a(n) = 1; if n = 2 or 3 then a(n) = 0; otherwise a(n) =
% (n+1)*a(n-1) - (n-2)*a(n-2) - (n-5)*a(n-3) + (n-3)*a(n-4)

% 1, 1, 0, 0, 2, 14, 90, 646, 5242, 47622, 479306, 5296790, 63779034

\begin{document}

\begingroup
\makeatletter

\@namedef{F<0>}{1}
\@namedef{F<1>}{1}
\@namedef{F<2>}{0}
\@namedef{F<3>}{0}

\count@=4

\loop
% no \@nameedef in latex
  \expandafter\edef\csname F<\the\count@>\endcsname
       {\thebnumexpr (\count@+1)*\@nameuse{F<\the\numexpr\count@-1>}
                    -(\count@-2)*\@nameuse{F<\the\numexpr\count@-2>}
                    -(\count@-5)*\@nameuse{F<\the\numexpr\count@-3>}
                    +(\count@-3)*\@nameuse{F<\the\numexpr\count@-4>}\relax}%
\ifnum\count@<30
\advance\count@\@ne
\repeat

\count@=0

\ttfamily

http://oeis.org/A002464

\loop
\the\count@: \@nameuse{F<\the\count@>}\endgraf
\ifnum\count@<30
\advance\count@\@ne
\repeat

\endgroup

\end{document}

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

답변2

F(n)다음은 의 함수로서 컴퓨팅에 대한 LuaLaTeX 기반 접근 방식입니다 n. 제공한 버전과 관련하여 수식은 (a) 시각적 그룹화를 제공하기 위해 중괄호와 대괄호를 추가하고 (b) 외부("i") 및 내부("k")를 강조하기 위한 일부 설명 텍스트로 약간 변경되었습니다. 루프.

테이블은 사소하게 n=2부터 시작됩니다 F(1)=1. (F(1) ~이다그런데 정확하게 계산되었습니다.) 테이블의 경우 n_{\max}15로 설정했습니다. 실제로 계산 방법은 n<20.

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

\documentclass{article}
\usepackage{amsmath} % for "\binom" macro
\usepackage{luacode} % for "luacode" env. and "\luaexec" macro
\begin{luacode}
-- First, define two helper functions: "factorial" and "mchoose"
function factorial ( n )
   local k
   if n==0 or n==1 then
      return 1
   else
      return n * factorial(n-1)
   end
end

-- 'mchoose' is patterned after the posting in http://stackoverflow.com/a/15302448. 
-- Thanks, @egreg, for pointing me to this posting!
function mchoose( n, k )
     if ( k == 0 or k == n ) then
        return 1
     else
        return ( n * mchoose(n - 1, k - 1)) / k 
     end
end

-- Second, set up the function "F"
function F ( n )
   local i, k, result, kterm
   result = factorial ( n ) 
   for i=1,n-1 do           -- outer loop is entered only if n>1
      kterm=0  -- (re)set "kterm" to 0
      for k=1,i do
         kterm = kterm + mchoose(i-1,i-k) * mchoose(n-i,k) * 2^k 
      end
      result = result + ((-1)^i) * factorial(n-i) * kterm
   end
   return result
end

\end{luacode}

\begin{document}
\[
F(n)=n!+\biggl\{\,
     \underbrace{ \sum_{i=1}^{n-1} (-1)^i (n-i)! \biggl[\,
         \underbrace{\sum_{k=1}^i \binom{i-1}{i-k} \binom{n-i}{k} 2^k}_%
         {\text{inner or $k$ loop}}\biggr]}_%
     {\text{outer or $i$ loop}}\biggr\}
\]

\bigskip
% print values of n and F(n) for n=2,...,15
\[
\begin{array}{@{}rr@{}}
\hline
n & F(n) \\
\hline
\luaexec{for n=2,15,1 do
           tex.sprint(n .. "&" .. math.floor(F(n)) .. "\\\\")
         end}
\hline
\end{array}
\]
\end{document}

관련 정보