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