[R] some questions about sympy (that is, rSymPy)

Uwe Ligges ligges at statistik.tu-dortmund.de
Sat Apr 14 18:09:52 CEST 2012



On 14.04.2012 04:41, Kjetil Halvorsen wrote:
> see below.
>
> On Fri, Apr 13, 2012 at 8:50 PM, Kjetil Halvorsen
> <kjetilbrinchmannhalvorsen at gmail.com>  wrote:
>> I am experimenting with rSymPy, and it seems to work nice.
>>
>>
>> However, I dislike the need to wrap all sympy expressions within
>> quotes, it leads to ugly calls like
>> library(rSymPy)
>> Var("x,y,z")
>> sympy("(x+y)**2")
>> and so on.
>>
>> Inspired by the function cq from mvbutiles package:
>> library(mvbutils)
>>> cq
>> function (...)
>> {
>>     as.character(sapply(as.list(match.call(expand.dots = TRUE))[-1],
>>         as.character))
>> }
>> <bytecode: 0x7fca88443f78>
>> <environment: namespace:mvbutils>
>>
>> I tried to write
>>> sympyq
>> function(...) {
>>    arg<- as.character(match.call(expand.dots=TRUE)[-1])
>>    thiscall<- as.call(list(as.name("sympy"), arg))
>>    print( thiscall ) # for debugging
>>    eval(thiscall, parent.frame() )
>> }
>>
>> Some examples:
>> (After doing
>> Var("x,y,z") )
>>> sympyq(4+4)
>> sympy("4 + 4")
>> [1] "8"
>>
>>> sympyq(3*x+4*y+89*z-6*x)
>> sympy("3 * x + 4 * y + 89 * z - 6 * x")
>> [1] "-3*x + 4*y + 89*z"
>>>
>>
>> But then:
>>
>>> sympyq( (x+y)**2 )
>> sympy("(x + y)^2")
>> Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl,  :
>>   Traceback (most recent call last):
>>   File "<string>", line 1, in<module>
>> TypeError: unsupported operand type(s) for ^: 'Add' and 'int'
>>
>> Note that R has changed the  syntax **2 to ^2, which sympy does not
>> seem to like!
>>
>> Any ideas for avoiding this, or more generally, better ideas for
>> achieving what I am trying to do?
>>
>> Kjetil
>
> Ok, I am trying now with
>
>> sympyq
> function(...) {
>     arg<- as.character(match.call(expand.dots=TRUE)[-1])
>     arg<- gsub('^', x=arg, replacement='**', fixed=TRUE)
>     thiscall<- as.call(list(as.name("sympy"), arg))
>     print( thiscall )
>     eval(thiscall, parent.frame() )
> }
>
> which gives:
>
>> sympyq( (x+y)**2 )
> sympy("(x + y)**2")
> [1] "(x + y)**2"
>
>> sympyq ( sin(pi) )
> sympy("sin(pi)")
> [1] "0"
>
>> sympyq( limit(sin(x)/x, x, 0) )
> sympy("limit(sin(x)/x, x, 0)")
> [1] "1"
>
>> sympyq( diff(sin(2*x), x, 2) )
> sympy("diff(sin(2 * x), x, 2)")
> [1] "-4*sin(2*x)"
>
> so seems to work, but then, tyhe following I do not understand:
>
>> sympyq( ((x+y)**2).expand() )
> Error: unexpected symbol in "sympyq( ((x+y)**2).expand"
>


That goes through the R parser but is not valid R syntax, hence it 
cannot work.

Uwe Ligges



>> sympyq( sin(x+y).expand(trig=True) )
> Error: unexpected symbol in "sympyq( sin(x+y).expand"
>
> ???
>
> Kjetil
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list