將乳膠術語方程式轉換為適合 Wolfram alpha 的版本

將乳膠術語方程式轉換為適合 Wolfram alpha 的版本

我問了一個關於數學堆疊交換的組合問題並收到了以下信息LaTeX 風格的答案。我試著將這個方程式轉換成 Wolfram alpha 可以理解的東西;然而,它似乎不起作用。關於如何編輯方程式的語法有什麼想法嗎?我已在下面發布了所使用的語法。

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

目錄:

  • 最初的答案使用新泰普和OP的公式。實作是可擴展的...

  • 使用給出的遞歸公式的第二個答案http://oeis.org/A002464。第二種方法一次建構給定表達式的值數組。它使用包數值表達式用於大整數計算。


您可以使用 TeX 或 LaTeX 來計算它。

程式碼註解中詳細介紹了一些有點令人痛苦的細節:主要的一個是,如果 y>x,binomial(x,y) 函數目前會發出錯誤,而不是默默地傳回零。

\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

這是一種基於 LuaLaTeX 的方法來計算F(n)的函數n。相對於您提供的版本,該公式稍微改寫為(a)添加大括號和方括號以提供一些視覺分組,以及(b)一些解釋性文本以突出顯示外部(“i”)和內部(“k” )循環。

該表以“ n=2since ”開頭F(1)=1。 (F(1) 順便說一句,計算正確n_{\max}。事實上,只要 的計算方法就不會產生溢位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}

相關內容