3
$\begingroup$

The binomial expansion is of the form

$(x+y)^n = \sum\limits_{i=0}^n {n \choose i} x^i y^{n-i}$.

I'd like to generate the appropriate Mathematica output (so I can then convert it to $\LaTeX$ source through TeXForm[]) showing each term. For instance, for $(x+y)^5$ I would like (after conversion and typesetting):

$1 x^0 y^5 + 5 x^1 y^4 + 10 x^2 y^3 + 10 x^3 y^2 + 5 x^4 y^1 + 1 x^5 y^0$.

The first step in my first attempt is:

myelements = Table[{ToString[Binomial[5, i]], StringForm["\!\(\*SuperscriptBox[\(x\), \(``\)]\)", i], StringForm["\!\(\*SuperscriptBox[\(y\), \(``\)]\)", 5 - i]}, {i, 0, 5}]

giving

{{"1", StringForm["\!\(\*SuperscriptBox[\(x\), \(``\)]\)", 0], StringForm["\!\(\*SuperscriptBox[\(y\), \(``\)]\)", 5]}, {"5", StringForm["\!\(\*SuperscriptBox[\(x\), \(``\)]\)", 1], StringForm[ "\!\(\*SuperscriptBox[\(y\), \(``\)]\)", 4]}, {"10", StringForm[ "\!\(\*SuperscriptBox[\(x\), \(``\)]\)", 2], StringForm[ "\!\(\*SuperscriptBox[\(y\), \(``\)]\)", 3]}, {"10", StringForm[ "\!\(\*SuperscriptBox[\(x\), \(``\)]\)", 3], StringForm[ "\!\(\*SuperscriptBox[\(y\), \(``\)]\)", 2]}, {"5", StringForm[ "\!\(\*SuperscriptBox[\(x\), \(``\)]\)", 4], StringForm[ "\!\(\*SuperscriptBox[\(y\), \(``\)]\)", 1]}, {"1", StringForm[ "\!\(\*SuperscriptBox[\(x\), \(``\)]\)", 5], StringForm[ "\!\(\*SuperscriptBox[\(y\), \(``\)]\)", 0]}}

to get the individual terms, but unfortunately these elements are not easily joined (since they are not all Strings or Text), nor is the separating "+" sign appearing. Then:

Flatten@Riffle[myelements, Table["+", {5}]]

There must be a way using Text or Boxes or whatever that yields a form that can be converted to $\LaTeX$, but I'm seeking the most elegant method.

Any ideas?

$\endgroup$
3
$\begingroup$

The order is reversed, but you could try:

Sum[With[{b = Binomial[5, i], xexp = i, yexp = 5 - i}, 
  HoldForm[b] HoldForm[x^xexp] HoldForm[ y^yexp]], {i, 0, 5}];
% // TeXForm

$1 x^5 y^0+5 x^4 y^1+10 x^3 y^2+10 x^2 y^3+5 x^1 y^4+1 x^0 y^5$

$\endgroup$
  • $\begingroup$ With the trivial fix of xexp = 5-i and yexp = i, this is perfect. Thanks! $\endgroup$ – David G. Stork May 5 '17 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.