[R] [Fwd: Re: Adding and Multiplying two Unevaluated Expressions]

Benjamin Müller ben_mueller.bm at web.de
Wed Dec 2 18:32:47 CET 2009



-------- Original-Nachricht --------
Betreff: 	Re: [R] Adding and Multiplying two Unevaluated Expressions
Datum: 	Tue, 01 Dec 2009 23:49:39 +0100
Von: 	Benjamin Müller <ben_mueller.bm at web.de>
An: 	Rolf Turner <r.turner at auckland.ac.nz>
Referenzen: 	<20091201144125.316310 at gmx.net> 
<8E40E49F-E8FC-4FBD-8CC5-93789FFB0E53 at auckland.ac.nz>



This works fine for your example, but doesn't work as simple if there's 
more than these expressions.

In my example this would be in line 11:

newexp=as.expression(substitute(a+con1/con2*b^con3,list(a=newexp[[1]],b=expression(dx)[[1]],con1=eval(exp0,ls),con2=factorial(i),con3=1*i)))

this works just fine and you can still evaluate this. thank you very much!

If there's an easier way, let me know.
Greets,
Ben Müller


Rolf Turner schrieb:
>
> On 2/12/2009, at 3:41 AM, Benjamin Müller wrote:
>
>> HI,
>>
>> As I'm trying to compute Taylor series, I'm having problems in adding 
>> and multiplying unevaluated expressions. I searched for a solution 
>> but found none.
>>
>> my Taylor function works fine for evaluating functions as you can see 
>> here:
>>
>>
>> rTaylorVal=function(exp,x0,dx,n) {
>>
>> ls=list(x=x0)
>>
>> newexp=eval(exp,ls)
>>
>> exp0=exp
>>
>> for (i in 1:n){
>> exp0=D(exp0,"x")
>> newexp=newexp+eval(exp0,ls)/factorial(i)*dx^i
>> }
>>
>> return(newexp)
>>
>> }
>>
>> Where exp is an expression like exp=expression(x^2*sin(x)), x0 is the 
>> startvalue, dx the difference between startvalue and searched value 
>> and n is the length of the series.
>>
>> So I tried to remove dx as a value, to get a Taylor series 
>> expression, but it doesn't work as simple multiplication (*) and 
>> accumulation (+) is not good for expressions.
>>
>> That's my point so far, now my question:
>>
>> Is it actually possible to add and/or multiply expressions, and how?
>
> This may well be a case of the blind leading the partially sighted,
> but for what it's worth my answer is ``Yes, but it's a kludge.''
> One needs to use substitute, it seems to me.
>
> E.g.:
>
> e1 <- expression((x+y)^2)
> e2 <- expression(1/(x^2 + y^2))
> e3 <- as.expression(substitute(a+b,list(a=e1[[1]],b=e2[[1]])))
> e3
> D(e3,"x")
> D(e3,"y")
>
> Older and wiser heads may provide you with better counsel.
>
>     cheers,
>
>         Rolf Turner
>
>
>
>
> ######################################################################
> Attention:This e-mail message is privileged and confidential. If you 
> are not theintended recipient please delete the message and notify the 
> sender.Any views or opinions presented are solely those of the author.
>
> This e-mail has been scanned and cleared by 
> MailMarshalwww.marshalsoftware.com
> ######################################################################
>




More information about the R-help mailing list