13
$\begingroup$

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[...]]?

$\endgroup$
1

2 Answers 2

14
$\begingroup$

#1

Trott-Strzebonski in-place evaluation:

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

Injector pattern:

{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
$\endgroup$
8
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Commented Jun 3, 2012 at 12:21
  • $\begingroup$ 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]}? $\endgroup$ Commented Jun 3, 2012 at 12:21
  • $\begingroup$ @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] $\endgroup$
    – Mr.Wizard
    Commented Jun 3, 2012 at 12:23
  • $\begingroup$ Looks like you saw the comment I accidentally added and deleted soon afterwards. $\endgroup$ Commented Jun 3, 2012 at 12:25
  • 1
    $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Commented Jun 4, 2012 at 2:31
9
$\begingroup$

You might want

HoldForm[Plus[##]] & @@ (List @@ taylor /. x -> 1)
$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Commented Jun 3, 2012 at 7:10
  • $\begingroup$ I just looked up SlotSequence. This is a useful trick to remember. $\endgroup$ Commented Jun 3, 2012 at 10:54
  • 1
    $\begingroup$ 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]] $\endgroup$ Commented Jun 3, 2012 at 12:26
  • $\begingroup$ @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? $\endgroup$ Commented Jun 3, 2012 at 14:21
  • $\begingroup$ @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... $\endgroup$ Commented Jun 3, 2012 at 15:32

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.