Message Boards Message Boards

0
|
2892 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Use the output of the Normal function as input to a function def?

Posted 5 years ago

The Normal function is supposed to truncate the output of the Series function to a normal expression. But when I try to use that expression to define another function it fails: seems to keep some series attrfibutes. So I have to copy the expression and paste it to the right side of the new function definition. Here is an example from a Mathematica notebook:

In[242]:= (*Let's try the Series function with variable exponent k.
Want to truncate with the Normal function, then use the resulting \
expression as input to define a new function. But the output of \
Normal does not behave like a normal expression. Have to copy and \
paste instead.*)

In[209]:= xpand[k_, r_, n_] := Series[(1 + r)^(1/k), {r, 0, n}]

In[223]:= xpand[k, r, 5]

Out[223]= SeriesData[r, 0, {
 1, k^(-1), 
  Rational[1, 2] ((-1 + k^(-1))/k), ((Rational[1, 6] (-1 + k))
     k^(-3)) (-1 + 2 k), (((Rational[-1, 24] (-1 + k))
      k^(-4)) (-1 + 2 k)) (-1 + 3 k), ((((Rational[1, 120] (-1 + k))
       k^(-5)) (-1 + 2 k)) (-1 + 3 k)) (-1 + 4 k)}, 0, 6, 1]

In[227]:= xpandN5[k_, r_] := Normal[xpand[k, r, 5]]

In[230]:= xpandN5[2, r]

Out[230]= 1 + r/2 - r^2/8 + r^3/16 - (5 r^4)/128 + (7 r^5)/256

In[234]:= pand[r_] := xpandN5[2, r]

In[235]:= pand[r]

Out[235]= 1 + r/2 - r^2/8 + r^3/16 - (5 r^4)/128 + (7 r^5)/256

In[236]:= pand[.2]

During evaluation of In[236]:= General::ivar: 0.2` is not a valid variable.

During evaluation of In[236]:= General::ivar: 0.2` is not a valid variable.

Out[236]= Series[1.09545, {0.2, 0, 5}]

(*Failed. Can't get rid of some of the series attributes, so that the \
alleged normal expression could be used as the righthand side in \
function definitions. So try it with copy and paste instead.*)

In[238]:= 
pand1[r_] := 1 + r/2 - r^2/8 + r^3/16 - (5 r^4)/128 + (7 r^5)/256

In[239]:= pand1[r]

Out[239]= 1 + r/2 - r^2/8 + r^3/16 - (5 r^4)/128 + (7 r^5)/256

In[240]:= pand1[.2]

Out[240]= 1.09545

In[241]:= (*So after I copy and paste the displayed output of Normal, \
I can use it to define a new function. But this is cumbersome when \
the expression is very long.*)
POSTED BY: Karl Petersen
3 Replies

Yes, pand should be xpand.

POSTED BY: Daniel Lichtblau

Daniel-- Thank you for looking at this. First, with regard to the "pattern variable", in my Mathematica notebook I did type pand[r_]::= but on one screen I saw it seemed that it did not come across that way with copy and paste.

Second, when on line 238 I define pand1[r_] by copying line 235 (the text explicit literal output of Normal applied to xpand[k,r,5] with k=2) I get a desired actual function of r which I can evaluate on line 240 at r=.2 and get the numerical result.

However, when on line 234 I try to define the function pand[r_] by setting it to the alleged normal expression xpandN5[k,r] with k=2, I do not get a function of r that can be evaluated at r=.2 (line 236). Mathematica still thinks there's a series involved, even though Normal has been applied to the series.By rights pand[r] should no longer be a series, but a function of r.

Third, in your next to last line should xpand be pand? Otherwise how else can pand[.2] be defined? And wouldn't xpand as a function of one variable conflict with its definition above as a function of three variables?

Finally, I'm not yet up to using Module, but maybe that's what's needed to get Normal to work the way I expect? In that case U'll try harder. Thanks again, --Karl

POSTED BY: Karl Petersen

If you trace the computation you will find that there is an attempt to pass .2 as a variable to Series. This fails, so no SeriesData is created, hence Normal has no effect.

Also be aware that some of the definitions are not using pattern variables. I'm guessing this was not by intent. So something along the lines of the code below might be what's wanted.

xpand[k_, r_, n_] := Series[(1 + r)^(1/k), {r, 0, n}]
xpandN5[k_, r_] := 
 Module[{k2, r2}, Normal[xpand[k2, r2, 5]] /. {k2 -> k, r2 -> r}]

xpand[r_] := xpandN5[2, r]
pand[.2]

(* Out[124]= 1.09544625 *)
POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract