From p.dalgaard at biostat.ku.dk  Fri Jul 15 15:25:01 2005
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: 15 Jul 2005 15:25:01 +0200
Subject: [Rd] Computer algebra in R - would that be an idea??
In-Reply-To: <86262F64-CB17-4CB4-9281-819EBA83D33E@mac.com>
References: <C83C5E3DEEE97E498B74729A33F6EAEC01AD9A34@DJFPOST01.djf.agrsci.dk>
	<86262F64-CB17-4CB4-9281-819EBA83D33E@mac.com>
Message-ID: <x2br549p76.fsf@turmalin.kubism.ku.dk>

Rob J Goedman <goedman at mac.com> writes:

> A really simple way to play with R & yacas is by using the examples  
> in the subdir
> 'embed' of yacas-1.0.57.
> 
> After building yacas (on Mac OS in my case):
> 
>  > res<-system("~/Projects/yacas-1.0.57/embed/example2", intern=T)
>  > res
> [1] "Cos(x);"
> 
> or
> 
>  > res<-system("~/Projects/yacas-1.0.57/embed/example1 'D(x){{Sin 
> (x),Cos(x)}, {Sin(2*x),-Cos(x)}}'", intern=T)
>  > res
> [1] "Input> D(x){{Sin(x),Cos(x)}, {Sin(2*x),-Cos(x)}}"
> [2] "Output> {{Cos(x),-Sin(x)},{2*Cos(2*x),Sin(x)}};"
> 
> or
> 
>  > system("yacas -pc --execute '[Echo(D(x)Sin(x));Exit();]'")
> Cos(x)
> 
> or
> 
>  > system("echo 'Example()' | yacas_client")
> In> Example()
> Current example : Integrate(x,a,b)Sin(x);
> 
> Integrate a function.
> 
> Out> Cos(a)-Cos(b)
> In>
>  > system("echo 'Type(%)' | yacas_client")
> In> Type(%)
> Out> "-"
> In>
> 
> This last example does show the yacas server stays alive between  
> calls from R. yacas_client is a script.
> Maybe that approach also works for maxima?
> 
> I wonder how difficult it would be to translate expressions back and  
> forth from R to yacas in either R
> or C++. And maybe strip 'In>' and 'Out>' like parts.

You could also just do what those examples do and embed the whole
enchillada in R. The slightly bad news is that the yacas_eval
interface is text-based, which means that to handle an R expression
via yacas you're going through a deparse-parse-operate-deparse-parse
sequence. It would be nicer if you could just convert parse trees
between the two languages. The good news is that there's a Lisp route
(see example3), which should make the parser/deparser coding somewhat
easier.


-- 
   O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



[Rd] Computer algebra in R - would that be an idea??
Peter Dalgaard