1
$\begingroup$

Is there a way to make a formula display in a human readable form? I have something like

Binomial[(10000000 - 1), x]*(1/2^24)^x

and I'd like it to display using C notation or variants thereof.

Thanks

$\endgroup$
7
  • $\begingroup$ Take a look at TraditionalForm (and HoldForm in combination if you don't want evaluation) $\endgroup$
    – ciao
    Commented Feb 22, 2015 at 7:26
  • $\begingroup$ @rasher Thanks. Is there a way to leave the output as fractions as well? TraditionalForm seems to evaluate the fractions. $\endgroup$
    – Kar
    Commented Feb 22, 2015 at 8:18
  • $\begingroup$ HoldForm[Binomial[(10000000 - 1), x]*(1/2^24)^x] // TraditionalForm does not do what you're after? $\endgroup$
    – ciao
    Commented Feb 22, 2015 at 8:21
  • $\begingroup$ @rasher Well, not entirely. TraditionalForm evaluates (1/2^24)^x to 16777216^-x whereas HoldForm doesn't use a C notation but outputs Binomial[10000000-1, x]. I wonder if there's some kind of merge of the two? :) $\endgroup$
    – Kar
    Commented Feb 22, 2015 at 9:12
  • $\begingroup$ Perhaps you need to clarify what you're after. Do you want the binomial to be 9999999,x? $\endgroup$
    – ciao
    Commented Feb 22, 2015 at 9:19

2 Answers 2

4
$\begingroup$

I hope I understand the question. I have interpreted this is wishing to display an expression in a particular form.

f /: MakeBoxes[f[n_, x_, num_], StandardForm] := 
 RowBox[{SubscriptBox[
    RowBox[{AdjustmentBox[SuperscriptBox["", MakeBoxes[n]], 
       BoxMargins -> -0.15, BoxBaselineShift -> -1], 
      MakeBoxes[Style["C", Italic, 20], StandardForm]}], 
    MakeBoxes[x]], 
   SuperscriptBox[
    RowBox[{"(", FractionBox[1, SuperscriptBox[2, MakeBoxes[num]]], 
      ")"}], MakeBoxes[x]]}]

So,

Grid[{{f[a, b, c], Rasterize@TraditionalForm[f[a, b, c]]},
  {f[10000000 - 1, x, 24], 
   Rasterize@TraditionalForm[f[10000000 - 1, x, 24]]}}, Frame -> All]

enter image description here

$\endgroup$
2
$\begingroup$

If it is C-like syntax you are after, CForm is your friend:

CForm[Binomial[(10000000-1),x]*(1/2^24)^x]

outputs

Binomial(9999999,x)/Power(16777216,x)

To better preserve the original formula you gave, you can use:

CForm@HoldForm[Binomial[(10000000-1),x]*(1/2^24)^x]

which outputs

Binomial(10000000 - 1,x)*Power(1/Power(2,24),x)

Update

As you seemed to look for only the Binomial's arguments to be evaluated, while the fractions should stay there, please try:

Replace[HoldForm[
 Binomial[(10000000-1),x]*(1/2^24)^x],{Times[x_, y_] :> 
  Times[HoldForm[x], HoldForm[y]], Binomial->C},{3, \[Infinity]},
 Heads -> True] // ReleaseHold

giving the possibly desired result. Of course you can then call CForm again, in order to get the round brackets, but this time with only the Binomial's parameters having been simplified:

C(9999999,x)*Power(1*1/Power(2,24),x)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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