Call sage from R

40 views
Skip to first unread message

Carlos Antunes

unread,
Apr 5, 2021, 10:10:10 AMApr 5
to sage-support
I have some functions and scripts written in sage that uses symmetric functions, integer partitions, some poset and matrix stuff. I want to offer this functions to R users but since the code is large and complicated enough I'd like to avoid rewriting it.

Is there any way I can reuse what I have in sage? I know about the existence of SymPy, but I don't know if that's useful, because even while sympy has some code for, e.g, dealing with symmetric functions, it is probably not the same as the sage one, and still forces me rewrite a large amount of code.

I don't even know if this can actually be done, but it would be really useful.

Thanks in advance.

Dima Pasechnik

unread,
Apr 5, 2021, 4:47:22 PMApr 5
to sage-support
R has a package to use Python in its path,
https://cran.r-project.org/web/packages/reticulate/vignettes/calling_python.html

So yes, you can make sure that Sage's Python comes first,
and then you need to do

from sage.all import *

to load all Sage classes.
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/07a6ede2-73d4-4a0b-8abc-e53fa0f09c18n%40googlegroups.com.

Emmanuel Charpentier

unread,
Apr 6, 2021, 3:39:57 AMApr 6
to sage-support
I am somewhat skeptical about getting such an R package in CRAN : the dependency on Sagemath is probably a bit heavy for its platforms... and heavily platform dependent (we have serious implementation differences between Linux Mac and Windows versions).

A few remarks below :

Le lundi 5 avril 2021 à 22:47:22 UTC+2, dim...@gmail.com a écrit :
R has a package to use Python in its path,
https://cran.r-project.org/web/packages/reticulate/vignettes/calling_python.html

So yes, you can make sure that Sage's Python comes first,
and then you need to do

from sage.all import *

to load all Sage classes.

Be aware, however, that the Sage preprocessor won’t be “automatically available” ; you’ll have to preprocess your code in order to get what you mean. Compare :

## In Sage
sage: (2/3).parent()
Rational Field

with

## In Sage's Python
>>> from sage.all import *
>>> (2/3).parent()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'parent'

“Preprocessing” by hand allows you to get what you mean…,

>>> QQ(2/3).parent()
Rational Field

or by using the preparse Sage function :

>>> eval(preparse("2/3")).parent()
Rational Field

In the (hopefully not too far) future, an alternative would be to use Sage distributed as a library in the system's Python interpreter. See

this and the pages it points to for further information. This would also alleviate the platforms inconsistency problem...

HTH,

Carlos Antunes

unread,
Apr 7, 2021, 7:33:42 PMApr 7
to sage-support
Thank you both, I will dive into this and see if it works.
Reply all
Reply to author
Forward
0 new messages