4

From searching, I see that calling R from Sage is pretty seamless. However, I cannot find information on calling Sage from R. For example, suppose that I have an R expression that I would like to differentiate, say

temp <- expression(x + x^2)

How can I send that to sage and have it differentiate it and send back an R expression? I would like the result to be pretty much equivalent to

D(temp,'x')

I have searched CRAN and google and have not found anything. I'm hoping there's a better solution than something based on the system function. I have seen that there is support from R for yacas, but I am interested in Sage.

I use 64-bit Ubuntu.

Thanks!

Xu Wang
  • 10,199
  • 6
  • 44
  • 78
  • I suspect there is not a solution other than `system` -- but will be pleasantly surprised if someone offers one. It doesn't look like there's a very easy way to adapt the Sage "notebook server" to a simpler socket-based thing (as is used by `Ryacas`) – Ben Bolker Nov 09 '11 at 13:30
  • @BenBolker interesting, do you think there's a chance that there will be a package in the future or is the way that they're set up fundamentally incompatible? – Xu Wang Nov 09 '11 at 19:01
  • I don't know, not having looked into it in detail, but at a glance it seems harder than integrating `yacas` -- looking briefly at the `yacas` documentation (`man yacas`), one quickly discovers a way to set up a client-server relationship, which communicates with the server via sockets. A similarly quick look at Sage does not suggest a similarly easy strategy ... going via `rJython` as described below seems more fruitful. – Ben Bolker Nov 15 '11 at 19:08

1 Answers1

2

I don't know if this would work, but Sage libraries can be imported into Python, and Python can be interfaced with the rJython package. Could you call Sage functions through rJython? It might not be as clean as the Ryacas or rSymPy packages, but perhaps it will be good enough.

Edit Since Sage uses SymPy, you could interface with SymPy directly:

require(rSymPy)
x <- Var('x') # Convenience function for: sympy("var(’x’)")
sympy("diff(x + x**2, x, 1)")
# [1] "1 + 2*x"
jthetzel
  • 3,603
  • 3
  • 25
  • 38
  • @jhetzel, interesting -- it looks like that's the only way. I wonder if it would be better to do that or through `system`. Thanks – Xu Wang Nov 09 '11 at 19:03
  • @Xu Wang, I suppose the choice is your preference. However, if you do use `rJython`, you can probably easily develop your own convenience wrapper functions, and then publish them as a package for future SageRs. You can have a look at the `rSymPy` package as an example, which I believe is a package of convenience functions for calling SymPy library functions through the `rJython` interface. – jthetzel Nov 09 '11 at 19:29
  • thanks, if you have some time, could you please show me how I would use your way to solve my example? – Xu Wang Nov 09 '11 at 19:32
  • @Xu Wang, I'll try to put together a short example to get you started later tonight. In the meantime, you can view rSymPy's source code at: http://code.google.com/p/rsympy/source/browse/#svn%2Ftrunk . The sympy.R functions seem to establish the rJython interface with the SymPy library, and the Sym.R functions look like they construct common SymPy commands for convenience. – jthetzel Nov 09 '11 at 19:45
  • thanks! Only do it if you have time and it's not a pain though. – Xu Wang Nov 09 '11 at 20:25
  • @Xu Wang: Unfortunately, I've been unsuccessful so far with this strategy. Importing `sage.all` modules into python (except for sage's custom python `./sage -python`) always leads to errors with `libcsage.so` for me. I asked about it on 'Ask Sage', and William Stein, Sage's founder, replied that it probably can't be done this way ( http://ask.sagemath.org/question/897/from-sageall-import-results-in-libcsageso-cannot ). – jthetzel Nov 15 '11 at 16:31
  • @Xu Wang: Also, I believe that Sage uses SymPy for a lot of its CAS. So, if all you need is differentiation, you can just use `rSymPy`. – jthetzel Nov 15 '11 at 16:45
  • interesting. Well it was a good idea! I've been meaning to look into rSymPy so maybe now is the time. Thanks for your help! – Xu Wang Nov 15 '11 at 19:13