# Displaying a series obtained by evaluating a Taylor series

### Description of problem

I would like to use Mathematica to display the series obtained by substituting a value for $x$ in a Taylor series expansion. The terms of the series will be rational numbers, so they should be in their reduced forms.

For example, consider the following partial sum:

In[1]:= taylor=Series[Log[1+x],{x,0,6}] // Normal
Out[1]= x-x^2/2+x^3/3-x^4/4+x^5/5-x^6/6

If I use ReplaceAll to carry out the substitution $x=1$, then I will get

In[2]:= taylor /. {x->1}
Out[2]= 37/60

as opposed to $1-\frac{1}{2}+\frac{1}{3}-\frac{1}{4}+\frac{1}{5}-\frac{1}{6}$.

### First attempt

The first thing which comes to mind is the following:

In[3]:= HoldForm[Evaluate[taylor]] /. {x->1}
Out[3]= 1-1^2/2+1^3/3-1^4/4+1^5/5-1^6/6

Now I just need to replace each of the terms with its simplified versions. The first problem is that Evaluate only works on level 1, so the following code does not work.

In[4]:= Map[Evaluate,%,{2}]
Out[4]= Evaluate[1]+Evaluate[-(1^2/2)]+Evaluate[1^3/3]+Evaluate[-(1^4/4)]+Evaluate[1^5/5]+Evaluate[-(1^6/6)]

Is there a way to carry out an evaluation deep inside HoldForm?

I also tried applying Replace at level 2, but the correct replacement rule still eludes me.

In[5]:= Replace[
HoldForm[Evaluate[taylor]] /. {x->1},
{x_->Evaluate[x]},
{2}]
Out[5]= 1-1^2/2+1^3/3-1^4/4+1^5/5-1^6/6

If I knew the appropriate replacements to carry out without peeking inside HoldForm, say x_->0, then the code above would work.

How can I carry out the idea behind In[5]?

### Second attempt

If I am satisfied with just having a list of the terms, the following is certainly good enough:

In[6]:= List@@taylor/.{x->1}
Out[6]= {1,-(1/2),1/3,-(1/4),1/5,-(1/6)}

I can use HoldForm to prevent Plus from collapsing the series while retaining the pretty typesetting on the front end:

In[7]:= HoldForm[Plus[1,-(1/2),1/3,-(1/4),1/5,-(1/6)]]
Out[7]= 1+-(1/2)+1/3+-(1/4)+1/5+-(1/6)

So splicing the sequence portion of Out[6] into HoldForm[Plus[...]] is just what I need. My plan was to use the splicing trick with Sequence, but that does not work within HoldForm. For example,

In[8]:= HoldForm[Plus[Sequence[1,2]]]
Out[8]= +Sequence[1,2]

Is there a way to splice Out[6] into HoldForm[Plus[...]]?

### #1

hf = HoldForm[1 - 1^2/2 + 1^3/3 - 1^4/4 + 1^5/5 - 1^6/6]

hf /. x_Times :> With[{eval = x}, eval /; True]
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6
Replace[hf, x_ :> With[{eval = x}, eval /; True], {2}]
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6

One may simplify this method using the undocumented function RuleCondition as WReach shows:

Replace[hf, x_ :> RuleCondition[x], {2}]
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6

### #2

{1, -(1/2), 1/3, -(1/4), 1/5, -(1/6)} /. {x__} :> HoldForm[+x]
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6
• @Michael ReplaceAll replaces subexpressions in any expression, not only a list. The "injector" is turning the usual replacement upside down with the "target" expression being the RHS of the rule. If the LHS of the rule does not match the full expression to the left of /. you will find that substitutions are still being made to that expression. Commented Jun 3, 2012 at 12:21
• The documentation says that it "applies a rule or list of rules in an attempt to transform each subpart of an expression expr". In my mind, ReplaceAll is about locally replacing subexpressions in a list. The injector pattern does not return anything to the original list, so I find it really strange. The example {a,b} /. {x__} :> x, which returns Sequence[a,b] shows that my understanding is off the mark. Why does it not return {Sequence[a,b]}? Commented Jun 3, 2012 at 12:21
• @Michael The entire expression {a,b} is matched by the pattern {x__} therefore the entire thing is replaced with x ; compare to {a,b} /. {x__} :> g[x] or thing[{a,b}, {1,2,3}] /. {x__} :> Plus[x] Commented Jun 3, 2012 at 12:23
• Looks like you saw the comment I accidentally added and deleted soon afterwards. Commented Jun 3, 2012 at 12:25
• @Michael that's an advanced technique; I didn't learn about it until last year. See this section in the documentation, and also my answer here. It allows a certain non-standard evaluation in With or Module which is used in the Trott-Strzebonski method to force an evaluation that otherwise would not happen. Commented Jun 4, 2012 at 2:31

You might want

HoldForm[Plus[##]] & @@ (List @@ taylor /. x -> 1)
• This is a simple way but I'm not sure it answers the OP's questions. I focused on the questions he emphasized in bold. Nevertheless, +1 for pragmatism. Commented Jun 3, 2012 at 7:10
• I just looked up SlotSequence. This is a useful trick to remember. Commented Jun 3, 2012 at 10:54
• Here is a slightly neater way to do Mike's construction: With[{x0 = 0, n = 6, x = 1}, HoldForm[Plus[##]] & @@ DeleteCases[CoefficientList[Series[Log[1 + \[FormalX]], {\[FormalX], x0, n}], \[FormalX]] Prepend[x^Range[n], 1], 0]] Commented Jun 3, 2012 at 12:26
• @J.M. I am not sure I understand why your code is neater given that it is significantly longer. Is there a fringe case where List @@ Series[Log[1+x],{x,0,n}] /. x->x0 fails? Also is there a reason why the use of With is advisable in this case? Commented Jun 3, 2012 at 14:21
• @Michael: the snippet as you gave it doesn't work. List @@ Normal[Series[Log[1 + \[FormalX]], {\[FormalX], x0, n}]] /. \[FormalX] -> x does work, but I think it's cleaner to use CoefficientList[] to extract the coefficients to be subsequently turned into terms... Commented Jun 3, 2012 at 15:32