Re: how to make Sum[n,{n,1,5}] to print 1+2+3+4+5? HoldForm?
- To: mathgroup at smc.vnet.net
- Subject: [mg79390] Re: how to make Sum[n,{n,1,5}] to print 1+2+3+4+5? HoldForm?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 25 Jul 2007 06:08:12 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f86qdq$o7p$1@smc.vnet.net>
Nasser Abbasi wrote: > hi, I am using version 6 > > what is the trick to make Sum[] just print the terms first, before > adding them? Say I want to do > > r= Sum[n,{n,1,5}] > > But I want to see 1+2+3+4+5, i.e. the terms printed out before they > are actually added. > > then later I can do Evaluate[r] or N[r] something like this to get it > to actually do the sum. > > I tried the different Hold commands, but non seems to do it. > > thanks, > Nasser As illustrated by In[2], you could use *HoldForm* at the element level before changing the head of the list to *Plus* from *List* via *Apply*. (Note that *Sum* is _not_ expanding the sum as one plus two plus ... before doing the summation; for Mathematica there exists more efficient way to get the job done.) In[1]:= HoldForm[Sum[n, {n, 1, 5}]] Out[1]= Sum[n, {n, 1, 5}] In[2]:= Plus @@ HoldForm /@ Range[1, 5] Out[2]= 1 + 2 + 3 + 4 + 5 In[3]:= ReleaseHold[%] Out[3]= 15 Moreover, you could use the following function as a template to handle more generic summations and also adjust the display to fit exactly your needs (like adding *TraditionalForm*, for instance.) In[4]:= mySum[f_, rng:{i_, start_Integer, end_Integer}] := HoldForm[Sum[f, rng]] == Plus @@ HoldForm /@ Table[f, rng] In[5]:= mySum[n, {n, 1, 5}] Out[5]= Sum[n, {n, 1, 5}] == 1 + 2 + 3 + 4 + 5 In[6]:= mySum[1/n, {n, 1, 5}] Out[6]= 1 1 1 1 1 Sum[-, {n, 1, 5}] == - + - + - + - + 1 n 5 4 3 2 HTH, Jean-Marc