sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.42k stars 477 forks source link

Meta-ticket: Interoperability with the Julia language, use Julia packages #27762

Open mkoeppe opened 5 years ago

mkoeppe commented 5 years ago

https://julialang.org/

Tickets:

CC: @sebasguts @isuruf @saraedum @nthiery

Component: packages: optional

Keywords: julia

Issue created by migration from https://trac.sagemath.org/ticket/27762

embray commented 5 years ago
comment:1

As the Sage-8.8 release milestone is pending, we should delete the sage-8.8 milestone for tickets that are not actively being worked on or that still require significant work to move forward. If you feel that this ticket should be included in the next Sage release at the soonest please set its milestone to the next release milestone (sage-8.9).

mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -2,4 +2,5 @@

 Tickets:
 - #17230: create a pexpect interface to julia
+- #28754: Make sage run in Julia's `python-jl`
mkoeppe commented 4 years ago

Changed keywords from none to julia

mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -3,4 +3,4 @@
 Tickets:
 - #17230: create a pexpect interface to julia
 - #28754: Make sage run in Julia's `python-jl`
-
+- Use https://github.com/JuliaPackaging/BinaryBuilder.jl
mkoeppe commented 3 years ago

Description changed:

--- 
+++ 
@@ -4,3 +4,5 @@
 - #17230: create a pexpect interface to julia
 - #28754: Make sage run in Julia's `python-jl`
 - Use https://github.com/JuliaPackaging/BinaryBuilder.jl
+- #31350: Julia package providing Sage
+
roed314 commented 3 years ago
comment:6

I'm at the Sage-OSCAR days this week and am interested in being able to use Oscar (a Julia package) from Sage. There is PyJulia, but I wasn't able to get Oscar working from that. There's William's old pexpect interface at #17230. Has anyone tried something like this?

mkoeppe commented 3 years ago
comment:7

I would recommend to start with the other direction, running Sage from Julia. I started #31350, see README at https://github.com/mkoeppe/SageMath.jl

roed314 commented 3 years ago
comment:9

Thanks! I tried Variant 2 out using a normal build of Sage rather than a conda one and it worked. I used

julia> ENV["PYTHON"]=rstrip(read(`sage`, String))
julia> using Pkg
julia> Pkg.build("PyCall")
julia> using PyCall
julia> sage_all = pyimport("sage.all")
julia> sage_all.ZZ("17")
PyObject 17
julia> sage_all.polytopes.cube()
PyObject A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 8 vertices

Have you thought much about going in the other direction?

mkoeppe commented 3 years ago
comment:10

Last time I tried the other direction, in #28754, there were technical problems with cysignals (I think).

If you run the testsuite, I think also some errors involving cysignals show up there.

I think it is best to investigate and fix these problems first before attempting the python-jl route again.

saraedum commented 3 years ago
comment:11

pyjulia works for me somewhat (I had to copy a few header files around so the GAP wrapper builds.)

>>> from julia import Oscar;Oscar.PadicField(2, 30)
<PyCall.jlwrap Field of 2-adic numbers>

Once I imported Oscar, as soon as I press the "up" key in the Python REPL, there's a segfault:

signal (11): Segmentation fault
in expression starting at none:0
HDLR_FUNC at /workspace/srcdir/gap/src/calls.h:134 [inlined]
HDLR_1ARGS at /workspace/srcdir/gap/src/calls.h:245 [inlined]
CALL_1ARGS at /workspace/srcdir/gap/src/calls.h:320 [inlined]
Call1ArgsInNewReader at /workspace/srcdir/gap/src/read.c:2893
GAP_rl_func at /workspace/srcdir/gap/src/sysfiles.c:2087
_rl_dispatch_subseq at /usr/lib/libreadline.so.8 (unknown line)
readline_internal_char at /usr/lib/libreadline.so.8 (unknown line)
rl_callback_read_char at /usr/lib/libreadline.so.8 (unknown line)
unknown function (ip: 0x7fede824c513)
PyOS_Readline at /usr/lib/libpython3.9.so.1.0 (unknown line)
unknown function (ip: 0x7fede7efa718)
unknown function (ip: 0x7fede7fd7f40)
unknown function (ip: 0x7fede7fd60ab)
unknown function (ip: 0x7fede8072ecc)
unknown function (ip: 0x7fede8072a0e)
unknown function (ip: 0x7fede7f1bfc3)
unknown function (ip: 0x7fede7f8e407)
PyRun_InteractiveLoopFlags at /usr/lib/libpython3.9.so.1.0 (unknown line)
PyRun_AnyFileExFlags at /usr/lib/libpython3.9.so.1.0 (unknown line)
unknown function (ip: 0x7fede7f113a2)
Py_BytesMain at /usr/lib/libpython3.9.so.1.0 (unknown line)
__libc_start_main at /usr/lib/libc.so.6 (unknown line)
_start at python (unknown line)
Allocations: 56537538 (Pool: 56492870; Big: 44668); GC: 45
[1]    1202215 segmentation fault (core dumped)  python

It works much better in IPython:

In []: from julia import Oscar

In []: p=5

In []: Qp = Oscar.PadicField(p, 30)

In []: a = 2*p + 4*p^2 + 1*p^3 + Oscar.O(Qp, p**4)
TypeError: unsupported operand type(s) for +: 'int' and 'PyCall.jlwrap'

In []: from julia import Main

In []: plus = Main.eval('+')

In []: from functools import reduce

In []: a = reduce(plus, [2*p, 4*p**2, 1*p**3, Oscar.O(Qp, p**4)])

In []: Main.show(a)
2*5^1 + 4*5^2 + 5^3 + O(5^4)

Disclaimer: I have no idea how this all works…

alexjbest commented 3 years ago
comment:13

I just came across https://github.com/cjdoris/PythonCall.jl which while similar to PyCall is a bit different https://cjdoris.github.io/PythonCall.jl/stable/pycall/ might be worth a look, it was designed by a mathematician with a lot of Magma experience at least so might have some Sage relevant design decisions.