How to use Sympy from java?

1,084 views
Skip to first unread message

Raymond Gong

unread,
May 7, 2015, 2:16:05 PM5/7/15
to sy...@googlegroups.com
Hey, this is the first time for me to be here, tried to search any examples or comments or whatever information on usage of sympy in java project.

Wonderying, anybody ever worked on the similar project? Any example can share?  It seems all examples in the documentation is using sympy live,

Any help would be appreciated.

Ondřej Čertík

unread,
May 7, 2015, 2:43:12 PM5/7/15
to sympy
Hi Raymond,
Is using the Java C interface an option for you? If so, you can then
call Python from Java using it, and thus SymPy. Or one can wrap our
C++ core SymEngine (https://github.com/sympy/symengine) directly in
Java, but SymEngine has only the basic features, compared to SymPy. If
you are interested in this, we can see how to get this working.

Ondrej

Joachim Durchholz

unread,
May 7, 2015, 4:18:00 PM5/7/15
to sy...@googlegroups.com
Am 07.05.2015 um 20:10 schrieb Raymond Gong:
> Hey, this is the first time for me to be here, tried to search any examples
> or comments or whatever information on usage of sympy in java project.

The standard Python interpreter is a standalone program, interacting
with that from Java is always tricky because you have to convert all
data structures to and from string.

Running SymPy inside Jython might be an easier path.
Jython is a Java reimplementation of Python.
It is quite possible that SymPy cannot run under Jython - last time we
tried using it, it would fail because Jython didn't implement all the
Python features that SymPy uses. However, Jython might have implemented
enough of Python to run SymPy today, so it may be worth a shot. Be sure
to run the test suite to see how much breaks, and whether any of the
breakage is going to affect your project.
I think it's worth a shot; if it works, you should have SymPy running
inside a JVM that can be fed with Java data structures directly.

> Wonderying, anybody ever worked on the similar project?

I think we did some Jython testing a while ago.
Maybe a search for "Jython" in the GitHub search on the sympy project
will turn up what happened.

Raymond Gong

unread,
May 7, 2015, 4:24:15 PM5/7/15
to sy...@googlegroups.com
Hi Ondrej,

Thanks a lot for kind response.  I am trying to use sympy via Jython from java.  I am facing a few challenges:

1.  Although I can call python (*.py, not sympy yet) functions from Jython, but I could not find any example or information how to load sympy lib into my java project.
2.  From Sympy documentation, there are many examples to use sympi in sympy live, but I didn't find any information yet regarding sympy API, for example, if I need
     do calculation for symbolic expression:  exp1=x**2+y**2, exp2=x**2, the symbolic calculation: exp1-exp2 = y**2,
     how can I know which modules/functions of sympy should be used to do this? Obviously, going through codes is not best way to do this.
3.  If we call sympy from java, we need a way to passed in / return values between sympy and java, how to do that? In Jython, we can set values, but how to set variables?
     I mean for the symbolic expression, sympy need to define variable first (please correct me if I am wrong), how to do this from java/Jython and passed into sympy?

I started to evaluate sympy and started to touch it two days ago and I have no experience to use python, maybe I need to spend more time on this. But not sure if or not the solution is workable on high level.

Thanks again and looking forward to any further help.  

Raymond

Ondřej Čertík

unread,
May 7, 2015, 4:25:11 PM5/7/15
to sympy
On Thu, May 7, 2015 at 2:17 PM, Joachim Durchholz <j...@durchholz.org> wrote:
> Am 07.05.2015 um 20:10 schrieb Raymond Gong:
>>
>> Hey, this is the first time for me to be here, tried to search any
>> examples
>> or comments or whatever information on usage of sympy in java project.
>
>
> The standard Python interpreter is a standalone program, interacting with
> that from Java is always tricky because you have to convert all data
> structures to and from string.

Actually, you just call SymPy using Python C/API, i.e. from C. Then
you call this C wrapper from Java, so no string conversions.

Ondrej

>
> Running SymPy inside Jython might be an easier path.
> Jython is a Java reimplementation of Python.
> It is quite possible that SymPy cannot run under Jython - last time we tried
> using it, it would fail because Jython didn't implement all the Python
> features that SymPy uses. However, Jython might have implemented enough of
> Python to run SymPy today, so it may be worth a shot. Be sure to run the
> test suite to see how much breaks, and whether any of the breakage is going
> to affect your project.
> I think it's worth a shot; if it works, you should have SymPy running inside
> a JVM that can be fed with Java data structures directly.
>
>> Wonderying, anybody ever worked on the similar project?
>
>
> I think we did some Jython testing a while ago.
> Maybe a search for "Jython" in the GitHub search on the sympy project will
> turn up what happened.
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/554BC873.4050203%40durchholz.org.
>
> For more options, visit https://groups.google.com/d/optout.

Raymond Gong

unread,
May 7, 2015, 4:29:24 PM5/7/15
to sy...@googlegroups.com
Hi Joachim,

Thanks a lot for your nice response.  Actually I am doing this, I just replied to Ondrej's message on this, but I didn't run Jython testing against sympy, I even don't know how to do it,
could you provide some more information or link on this?  I have no python background at all previously. Just started to touch python two days ago and trying to pick up it. Thanks again 
for your response.

Raymond

Raymond Gong

unread,
May 7, 2015, 4:40:53 PM5/7/15
to sy...@googlegroups.com
Hi Ondrej,

I think one of challenge (please correct me) for using sympy is how I can quickly 
know which API I should call for the given context? for example, 
if I need to do subtraction for two symbolic expression,
 which function I should call without spending too much time to go through the source codes?  
I think this is also issue even if we call sympy api directly in python apps, any advices?

Thanks.

Raymond

Ondřej Čertík

unread,
May 7, 2015, 4:47:41 PM5/7/15
to sympy
On Thu, May 7, 2015 at 2:40 PM, Raymond Gong <raymon...@gmail.com> wrote:
> Hi Ondrej,
>
> I think one of challenge (please correct me) for using sympy is how I can
> quickly
> know which API I should call for the given context? for example,
> if I need to do subtraction for two symbolic expression,
> which function I should call without spending too much time to go through
> the source codes?
> I think this is also issue even if we call sympy api directly in python
> apps, any advices?

You just put what you see in the tutorial into a some.py file and
execute it using "python some.py", that's it. E.g.:

# a.py
from sympy import symbols
x, y = symbols("x y")
e = x+y
f = x-y
print e-f

And execute it:

$ python a.py
2*y

You can see the last line, where I subtracted the two expressions. So
you just need to call "e-f" in Jython, from Java somehow.

Ondrej

Raymond Gong

unread,
May 7, 2015, 4:52:28 PM5/7/15
to sy...@googlegroups.com
Thanks Ondrej.

Joachim Durchholz

unread,
May 7, 2015, 4:57:43 PM5/7/15
to sy...@googlegroups.com
Am 07.05.2015 um 22:24 schrieb Ondřej Čertík:
> On Thu, May 7, 2015 at 2:17 PM, Joachim Durchholz <j...@durchholz.org> wrote:
>> Am 07.05.2015 um 20:10 schrieb Raymond Gong:
>>>
>>> Hey, this is the first time for me to be here, tried to search any
>>> examples
>>> or comments or whatever information on usage of sympy in java project.
>>
>>
>> The standard Python interpreter is a standalone program, interacting with
>> that from Java is always tricky because you have to convert all data
>> structures to and from string.
>
> Actually, you just call SymPy using Python C/API, i.e. from C. Then
> you call this C wrapper from Java, so no string conversions.

Ah okay.
Interfacing C and Java is still quite a PITA though.

Joachim Durchholz

unread,
May 7, 2015, 5:17:52 PM5/7/15
to sy...@googlegroups.com
Am 07.05.2015 um 22:29 schrieb Raymond Gong:
> Hi Joachim,
>
> Thanks a lot for your nice response. Actually I am doing this, I just
> replied to Ondrej's message on this, but I didn't run Jython testing
> against sympy, I even don't know how to do it,
> could you provide some more information or link on this?

My approach to installing Jython (no doubt biased by my other Python
workflows) would be (all done from the shell):
- Use pythonz to download Jython
- Use virtualenv to install it into a project directory
(so the pythonz download stays unaltered for future experiments)
- Use virtualenv to activate the Jython install
- cd to the sympy directory and do the normal bin/isympy.

This should install Jython and give you a first smoke test whether SymPy
is even able to start under Jython. If it doesn't, report the error
messages in whatever the bug tracker has for Jython and try some other
approach (unless the messages indicate very easily solved issues: the
SymPy project may consider officially supporting Jython if it turns out
to be little work to keep it that way).

Now if isympy can start, Ctrl-D out of isympy and try bin/test.
If that reports no errors, or errors only for modules that don't
interest you anyway, Jython is a viable path.
(Oh. If your project needs to remain executable in the future, you'll
also want that the SymPy project officially supports Jython.)

Once this all is done, I'd start exploring how to call into Python code
from Java.
Calling SymPy functions should be easy: They are all available in the
"sympy" module. I.e. if you see "exp()" somewhere, it will be available
as "sympy.exp()". You do not need to worry which module defines a
specific function; actually, those that do not make it into the "sympy"
module are not part of SymPy's public API and might go away in future
SymPy releases (you can still use them if you're willing to take the
risk, Python allows you to override all access restrictions at will).

Aaron Meurer

unread,
May 7, 2015, 5:20:24 PM5/7/15
to sy...@googlegroups.com
As far as I know, SymPy still does not work in Jython.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/554BD644.8050806%40durchholz.org.

Raymond Gong

unread,
May 7, 2015, 5:31:01 PM5/7/15
to sy...@googlegroups.com
Hi Aaron,

Thanks a lot for your information. I ever saw your message posted in google site regarding this,
 but the message are posted some time ago (around January?), 
wondering, where I can find more information on this? Any special web sites on this topic? 
any open source project to make sympy workable on Jython or java?  I could miss some points,
but I could not find it.  Thanks again for your nice response.

Raymond

Raymond Gong

unread,
May 7, 2015, 5:32:29 PM5/7/15
to sy...@googlegroups.com
Hi Joachim,

Thanks a lot for your detailed response.  Really appreciate. 

Raymond

Ondřej Čertík

unread,
May 7, 2015, 6:32:59 PM5/7/15
to sympy
Hi Raymond,

On Thu, May 7, 2015 at 3:31 PM, Raymond Gong <raymon...@gmail.com> wrote:
> Hi Aaron,
>
> Thanks a lot for your information. I ever saw your message posted in google
> site regarding this,
> but the message are posted some time ago (around January?),
> wondering, where I can find more information on this? Any special web sites
> on this topic?
> any open source project to make sympy workable on Jython or java? I could
> miss some points,
> but I could not find it. Thanks again for your nice response.

Long time ago (perhaps 7 years ago), I sat down with a Jython
developer at a Google Mentor Summit to get SymPy working in Jython. I
think there were definitely times when SymPy sort of worked, meaning
that it imported and you could do a simple calculation, and most (but
not all) tests running. But it was never working on a robust enough
level like PyPy, that we could run regular tests on Travis for this,
so we don't know what the current status is, unless you try it.

Ondrej

Raymond Gong

unread,
May 7, 2015, 6:41:38 PM5/7/15
to sy...@googlegroups.com
Thanks OnDrej, I have another question is, in SymPy live, I can type in the expression like:

# a.py 
from sympy import symbols 
x, y = symbols("x y") 
e = x+y 
f = x-y 
print e-f 

The thing is how I can pass in the expression such as x+y (or whatever math expression)  from java to Sympy (via Jython)? do you have any example? Thanks.

Raymond

Aaron Meurer

unread,
May 7, 2015, 7:57:57 PM5/7/15
to sy...@googlegroups.com
The easiest way to serialize and deserialize SymPy expressions is
using str() and sympify().

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/45ec6492-cb0c-4f61-8302-4269133bdcdd%40googlegroups.com.

Ondřej Čertík

unread,
May 7, 2015, 8:05:38 PM5/7/15
to sympy
On Thu, May 7, 2015 at 5:57 PM, Aaron Meurer <asme...@gmail.com> wrote:
> The easiest way to serialize and deserialize SymPy expressions is
> using str() and sympify().

That's one way, but I generally prefer to avoid it for robustness, so
you need to just call Jython API from Java and construct the
expression term by term.

Can you write what exactly you are trying to do in Java?

Ondrej

Joachim Durchholz

unread,
May 8, 2015, 3:27:21 AM5/8/15
to sy...@googlegroups.com
On reconsideration, I recognize I may have been assuming too much.

My question now is: For the project that you have in mind, what would be
the task of the Java side? What kind of data would you want to exchange
between the Java and SymPy side?
I think that has a deep impact on what you actually need.

Raymond Gong

unread,
May 8, 2015, 12:53:12 PM5/8/15
to sy...@googlegroups.com
Because of the skill sets of the team, we hope to wrap sympy using java, 
and other java modules will interact with wrapper directly and don't need to know 
anything about sympy and python. Our purpose is to evaluate equivalence of two 
different expression.  For example, (a+b)(a+b) is equivalent to a^2+2ab+b^2. Another example is
sin(2x) is equivalent to 2sin(x)cos(x) etc. One of solution to do this is do the subtraction for two 
symbolic expression, if the result is zero, then means they are equivalent to each other.

Raymond Gong

unread,
May 8, 2015, 12:57:00 PM5/8/15
to sy...@googlegroups.com
from Java side, we are hoping just pass in the symbolic math expression
such as polynomials or whatever K12 math expression, the sympy will return 
the result that java can consume. From java side, the expression is a java string
we can construct the expression based on java string, but the key issue is what kind of
format that Sympy can consume. I cannot find this kind of information from the tutorial
and documentation of sympy.

Raymond Gong

unread,
May 8, 2015, 1:04:06 PM5/8/15
to sy...@googlegroups.com
As mentioned previously, one of the reasons to wrap sympy is skills set, but I found I like python already although I only touched it for a few
days (I have tons of experiences using java).  But the thing is decision  cannot be made purely from the technical perspective.  I believe java wrapper 
should be useful for many other people. 

On Friday, May 8, 2015 at 12:27:21 AM UTC-7, Joachim Durchholz wrote:

Joachim Durchholz

unread,
May 8, 2015, 1:15:52 PM5/8/15
to sy...@googlegroups.com
Am 08.05.2015 um 18:57 schrieb Raymond Gong:
> from Java side, we are hoping just pass in the symbolic math expression
> such as polynomials or whatever K12 math expression, the sympy will return
> the result that java can consume. From java side, the expression is a java
> string
> we can construct the expression based on java string, but the key issue is
> what kind of
> format that Sympy can consume. I cannot find this kind of information from
> the tutorial
> and documentation of sympy.

That's essentially just the class of the Python object, i.e. Basic,
Expr, Add, Mul, Pow, whatever.

I don't know how to construct, write, or read these from the Java side;
I'd expect that information to live in the Jython docs if you can use
Jython.

If Jython does not work, you can use some string representation.
Either normal expressions, or via JSON - there's a json module in the
Python standard library, from the Java side, you'd use one of the
various available JSON libraries.

Joachim Durchholz

unread,
May 8, 2015, 1:18:26 PM5/8/15
to sy...@googlegroups.com
Am 08.05.2015 um 19:04 schrieb Raymond Gong:
> I believe java wrapper
> should be useful for many other people.

Important advice: Don't try to second-guess the needs of other people,
get your needs covered well. Making code more general is a lot of
additional work (Fred Brooks assumes a factor of 3), and if you don't
guess right, that additional work will be wasted.
Unless, of course, you already have concrete other uses in mind. I'd
still be rather cautious about trying to be general - you'll be learning
lessons in your first, non-general prototype, that's usually enough work.

YMMV ;-)

Sudhanshu Mishra

unread,
May 8, 2015, 1:50:52 PM5/8/15
to sy...@googlegroups.com

Hi,

Creating a full featured wrapper for SymPy in Java is an amazing idea. It will open a lot of opportunities and increase use cases of SymPy in the Android community.

Sudhanshu Mishra

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

Raymond Gong

unread,
May 8, 2015, 1:54:52 PM5/8/15
to sy...@googlegroups.com
Hi Joachim,

Definitely I agree with your points, don't try to guess other people's needs.  But if we google
"how to use / call SymPy from java", we found some people already asked similar questions.
Many companies already built their site using J2EE/Java technologies, usually if want to build
new services using new technologies such as python ( I mean new to the company, not new to 
the market), they need a complicated process to approve it per my past experiences.  So the best 
way to work around it it to wrap the new using "old" solution.

Joachim Durchholz

unread,
May 8, 2015, 2:19:55 PM5/8/15
to sy...@googlegroups.com
Am 08.05.2015 um 19:50 schrieb Sudhanshu Mishra:
> Creating a full featured wrapper for SymPy in Java is an amazing idea. It
> will open a lot of opportunities and increase use cases of SymPy in the
> Android community.

I'm very sceptical about that. Python's and Java's data model are
essentially incompatible, and Java wrapper will essentially have to
represent everything from the Python side as a PythonObject, and all
Java-side code will have the feel of doing everything through reflection
(which means it's going to feel like clunkiness hell).

No less clunky than simply exchanging data via JSON, anyway - but that's
going to be one-time upfront work.

Both alternatives require a running Python interpreter on Android. I
googled a bit around, I'd be rather worried about how compatible these
interpreters are and how much work installing them on Android would be
for the user (some embed the interpreter in the APK so that's less of a
problem).

Ondřej Čertík

unread,
May 9, 2015, 12:10:52 AM5/9/15
to sympy

Does your company allow you to call C from Java? Or does it only allow pure Java programs.

Sent from my mobile phone.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
Reply all
Reply to author
Forward
0 new messages