I am trying to write a set of homework questions.

I would like the output to evaluate the constants but not the sum, so that I may present the homework question as something like this:


Here's what I have:

c1 = RandomInteger[{-5, 5}];
c2 = RandomInteger[{-5, 5}];
c3 = RandomInteger[{-5, 5}];
c4 = RandomInteger[{1, 50}]*20
f[x_] := c1*x^2 + c2*x + c3 
(* 340
   3 - n - 4 n^2 *)

Defer[Sum[f[n], {n, 1, c4}]]

$\sum _{n=1}^{\text{c4}} f(n)$

It seems like I am not using Defer correctly. How can I fix this?


You just need to make sure that f and c4 are evaluated. This can be done in several ways, one of which was shown by Mark. Another equally valid approach is to use With to inject the evaluated form inside the held expression:

With[{f = f[n], c4 = c4}, Defer[Sum[f, {n, 1, c4}]]]
  • $\begingroup$ i would bump up your answer if I could.. but I need better reputation. thank you rm-rf thx for your help.. much appreciated $\endgroup$ – userX Mar 15 '13 at 20:01
  • $\begingroup$ Everyone has their favorite approach, although, I note that Evaluate doesn't work here. $\endgroup$ – Mark McClure Mar 15 '13 at 20:03
  • $\begingroup$ @MarkMcClure Although I usually use With, I almost posted an answer that was exactly the same as yours when you submitted it... so I scrapped it and went ahead with With :) Btw, what did you mean re: Evaluate? $\endgroup$ – rm -rf Mar 15 '13 at 20:07
  • $\begingroup$ Oftentimes, Evaluate is used for this sort of thing, e.g. Plot[Evaluate[list],___], where list might involve a Table or some such. In this problem, you can't wrap Evaluate around the f[n]. $\endgroup$ – Mark McClure Mar 15 '13 at 20:14
  • $\begingroup$ @MarkMcClure Yeah, Evaluate won't work in this case because it is too deep in the expression. $\endgroup$ – rm -rf Mar 15 '13 at 20:16

Defer blocks evaluation of the entire input expression, so f[n] stays as f[n]. You want to evaluate certain subparts of the expression but not the whole thing. One way to achieve this is as follows:

Defer[Sum[#1, {n, 1, #2}]]&[f[n], c4]

or, if you want traditional ordering of the polynomial,

Defer[Sum[#1, {n, 1, #2}]] &[TraditionalForm[f[n]], c4]

The first part (up to and including the ampersand &) is a pure function of two variables. The rest indicates that you're plugging f[n] and c4 into that function. Since they initially lie outside the scope of Defer, they are evaluated to what you want and then plugged in.

  • $\begingroup$ thank you Mark! that works beautifully, thank you, thank you! If I may.. one more question, is there a way to force printing of polynomial in descending order? $\endgroup$ – userX Mar 15 '13 at 19:59
  • $\begingroup$ @userX See edit. $\endgroup$ – Mark McClure Mar 15 '13 at 20:01
  • $\begingroup$ i would bump up your answer if I could.. but I need better reputation. $\endgroup$ – userX Mar 15 '13 at 20:03
  • $\begingroup$ perfect! thank you both, much appreciated! $\endgroup$ – userX Mar 15 '13 at 20:14

A few additional methods for you to chew on:

Block[{Defer, Sum},
  Sum[f[n], {n, 1, c4}] // Defer

  Defer @@ {Sum[f[n], {n, 1, c4}]}

Composition[Defer, Sum] @@ {f[n], {n, 1, c4}}

Defer @ Sum @ ## & @@ {f[n], {n, 1, c4}}

Each produces:

Mathematica graphics

Or with // TraditionalForm:

Mathematica graphics


I'd go for rm-rf's approach. Since that's taken though:

expr = Defer[Sum[f[n], {n, 1, c4}]];
ReplacePart[expr, # -> expr[[Sequence @@ #]]] &@{1, 1}

(but use With, not this).


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.