Dear Jeroen,

Le lundi 13 août 2018 21:01:37 UTC+2, Jeroen Demeyer a écrit :
>
> On 2018-08-13 19:07, Emmanuel Charpentier wrote: 
> > So the question is : how can one *replace* the Python REPL  with Sage's 
> ? 
>
> This seems like an instance of the XY problem: I don't think that 
> "replacing" REPLs is the correct way to deal with this. Can't you 
> configure/patch reticulate to use Sage instead instead of Python? 
>

That's exactly what I'm trying to do. 

Before delving in reticulate surgery, I'm trying to use what it offers. I 
notice :

   - use_python : path of the python binary. I set that to the path of my 
   main sage *script* which sets correctly the various environment variables...
   - repl_python : invokes the repl. Accepts an optional argument which is 
   the name of a module imported before the REPL is launched.
   
So my tentative plan is either :

   - write a module that imports IPython and launches  
   IPython.start_ipython(argv=['-c' '%load ext sage'])
   
This can be emulated by running this interactively with  py_run_string, and 
gives *partal* results, as illustrated below in a session of Sage's R 
interpreter (with comments) :

> library(reticulate)
> use_python("/usr/local/sage/sage", required=TRUE)

# No error : this has been accepted as a Python interpreter

> py_run_string("import IPython ; IPython.start_ipython(argv=['-c', 
'%load_ext sage'])")

# Again no error. And, by the way :

> py_run_string("print arctan(x).diff(x)")
1/(x^2 + 1)

# Sage is present, and gives sensible results to sensible inputs.
# Now, can we communicate between Sage and R ?

> py_run_string("foo=arctan(x).integrate(x)")
> py$foo
x*arctan(x) - 1/2*log(x^2 + 1)

We can fetch in R the value of a Sage variable

> paste("*** ",py_to_r(py$latex(py$foo))," ***")
[1] "***  x \\arctan\\left(x\\right) - \\frac{1}{2} \\, \\log\\left(x^{2} + 
1\\right)  ***"

This result can be "sensibly" converted to R and used here (as a string. It 
would be interesting to try to create an R algebraic expression... But this 
supposes that we clear the Sage/reticulate nameclash. Later...

But there's a (large) fly in that ointment :

> py_run_string("print 2^3")
1
> py_run_string("print 2**3")
8

The  preparser is out to lunch.. And this has consequences...

> py_run_string("print (x^2).diff(x)")
Error in py_run_string_impl(code, local, convert) : 
  RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.

Detailed traceback: 
  File "<string>", line 1, in <module>
  File "sage/structure/element.pyx", line 955, in 
sage.structure.element.Element.__xor__ 
(build/cythonized/sage/structure/element.c:9013)
    raise RuntimeError("Use ** for exponentiation, not '^', which means 
xor\n"+\

# Sage gives us expressions that it won't read.

# But the problem appears to be limited to the preparser :

> py_run_string("print (x**2).diff(x)")
2*x


   - Write a module that wil import * from sage.all and somehow starts a 
   REPL using the preparser.
   
This I can't even emulate well In a new session of Sage's R : 

> library(reticulate)
> use_python("/usr/local/sage/sage")
> py_run_string("from sage.all import *")
> py_run_string("preparser(True)")
> py_run_string("print 2^3")
1

# Preparser out to lunch again...

> py_run_string("print sin(x).diff(x)")
Error in py_run_string_impl(code, local, convert) : 
  NameError: name 'x' is not defined

Detailed traceback: 
  File "<string>", line 1, in <module>

# x has not been defined as the default symbolic variable. This we can fix :

> py_run_string("x=var('x')")
> py_run_string("print sin(x).diff(x)")
cos(x)

Again, reasonable results of reasonable inputs.

Let's try the REPL :

> repl_python()
Python 2.7.15 (/usr/local/sage/sage)
Reticulate 1.10 REPL -- A Python interpreter in R.

This banner can be suppressed if necessary.

>>> 2^3
1

No preparser, again...

>>> tan(x).integrate(x).trig_simplify()
-log(cos(x))

Reasonable. Let's check that we can access that from R :

>>> bar=tan(x).integrate(x).trig_simplify()
>>> exit
> py$bar
-log(cos(x))

Seems so...

So that second solution, seemingly lighter, seems promising. 

But I have two obstacles 

   1. What I know I don't know : how to insert the preparser ?
   2. What I don't know I don't know : what am I forgetting ?
   

Ideas ? Pointers ?

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to