Could someone point me to the code that supports math on symbolic equations?

112 views
Skip to first unread message

Jonathan

unread,
May 21, 2020, 9:30:42 AM5/21/20
to sage-devel
Dear All,

I have a use case where I need something lighter weight than the whole of Sagemath. I think SymPy + the ability to handle math on symbolic equations as Sagemath does it might be enough. Thus I wanted to see if I could extract from Sagemath the code supporting math on symbolic expressions and overlay that on SymPy or at least use that as a template. Can somebody please point me to the place to start looking in the codebase?

To make sure people understand what I am interested in, here is a simple example of the ability I would like to extract:
>>>eq1 = p*V==n*R*T
>>>eq1
p
*V=n*R*T
>>>eq2=eq1/V
>>>eq2
p
=n*R*T/V

Thanks,
Jonathan

Emmanuel Charpentier

unread,
May 22, 2020, 12:54:19 PM5/22/20
to sage-devel
Well, you  might consider working on the expressions <lef-hend part>-<right-hand part>. A quick test with Sympy:

Python 3.8.3 (default, May 14 2020, 11:03:12) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> python.el: native completion setup loaded
>>> from sympy import *
>>> p,V,n,R,T=symbols("p,V,n,R,T")
>>> Ex1=p*V-n*R*T
>>> Ex1
-R*T*n + V*p
>>> Ex1/V
(-R*T*n + V*p)/V
>>> solve(Ex1,p)
[R*T*n/V]

But Sympy has the Eq operator, which allows you to build, store and use symbolic equations :

>>> Eq1=Eq(p*V, n*R*T)
>>> Eq1
Eq(V*p, R*T*n)
>>> solve(Eq1,p)
[R*T*n/V]

OTOH, Sage isn't *that* much heavier than Sympy...

HTH,

Jonathan

unread,
May 22, 2020, 7:07:47 PM5/22/20
to sage-devel
Emmanuel,
 
Thanks, that is one of the places I was starting. It turns out that doesn't quite pick up the necessary stuff from the `Expr` type. I have had better luck extending the base type `Expr`. It was not hard to get the arithmetic parts (+, -, /,*, pow) working. I'm still looking for/working on a robust way of extending all the SymPy functions to operate on both the lhs and the rhs.

The idea here is not to use solve, but allow students to use it to aid them in doing algebra without making silly errors. We still need them to decide on all the steps themselves. This also lets them include units in calculations in a way that is familiar to physical scientists.

Anyway, my hope is to get some inspiration from how it is done in Sagemath.

Although my preference is to use Sagemath because of the inherent power, this application needs to play nice with *conda and pip installs. So I think it has to be an extension of SymPy rather than trying to convince people to install the other tools they are using in a Sagemath environment.

I'm definitely thankful for any suggestions people have.

Jonathan

dim...@gmail.com

unread,
May 22, 2020, 8:14:58 PM5/22/20
to sage-...@googlegroups.com
On Fri, May 22, 2020 at 04:07:47PM -0700, Jonathan wrote:
> Emmanuel,
>
> Thanks, that is one of the places I was starting. It turns out that doesn't
> quite pick up the necessary stuff from the `Expr` type. I have had better
> luck extending the base type `Expr`. It was not hard to get the arithmetic
> parts (+, -, /,*, pow) working. I'm still looking for/working on a robust
> way of extending all the SymPy functions to operate on both the lhs and the
> rhs.
>
> The idea here is not to use solve, but allow students to use it to aid them
> in doing algebra without making silly errors. We still need them to decide
> on all the steps themselves. This also lets them include units in
> calculations in a way that is familiar to physical scientists.
>
> Anyway, my hope is to get some inspiration from how it is done in Sagemath.
>
> Although my preference is to use Sagemath because of the inherent power,
> this application needs to play nice with *conda and pip installs. So I

Conda does have Sagemath available.
Not 100% sure how it works on Windows, though.

We're planning for this year to get Sagemath pip-installable too.


> think it has to be an extension of SymPy rather than trying to convince
> people to install the other tools they are using in a Sagemath environment.
>
> I'm definitely thankful for any suggestions people have.
>
> Jonathan
> On Friday, May 22, 2020 at 11:54:19 AM UTC-5, Emmanuel Charpentier wrote:
> >
> > Well, you might consider working on the expressions <lef-hend
> > part>-<right-hand part>. A quick test with Sympy:
> >
> > Python 3.8.3 (default, May 14 2020, 11:03:12)
> > [GCC 9.3.0] on linux
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> python.el: native completion setup loaded
> > >>> from sympy import *
> > >>> p,V,n,R,T=symbols("p,V,n,R,T")
> > >>> Ex1=p*V-n*R*T
> > >>> Ex1
> > -R*T*n + V*p
> > >>> Ex1/V
> > (-R*T*n + V*p)/V
> > >>> solve(Ex1,p)
> > [R*T*n/V]
> >
> > But Sympy *has* the Eq operator, which allows you to build, store and use
> --
> 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+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/f8fcb045-450d-49fa-b05f-501407083544%40googlegroups.com.

signature.asc

Samuel Lelievre

unread,
May 22, 2020, 8:47:35 PM5/22/20
to sage-devel
Le samedi 23 mai 2020 02:14:58 UTC+2, Dima:

>
> Conda does have Sagemath available.
> Not 100% sure how it works on Windows, though.

One can install SageMath from Conda on Linux and macOS.
Not on Windows.

rjf

unread,
May 24, 2020, 1:00:00 PM5/24/20
to sage-devel
It seems to me that the obvious thing is not to extract parts from SageMath, but
just use Maxima, which is a part, but also an entire symbolic math system, 

Your example looks like this:   ( assignment is ":"   equations use "=".  a command is terminated by ";" )

eq1 : p*V = n*r*t ;
eq1/V;

   returns p = (n*r*t)/V

RJF

Jonathan

unread,
May 24, 2020, 6:49:18 PM5/24/20
to sage-devel
Although a good idea, I don't think I can make it simple enough to set up inside a data acquisition environment that depends on Python. This would require installing Maxima and all the connector software. The people using this are unlikely to do anything that requires more than a `pip install...`. Once Sagemath can cleanly install using pip, this problem will be solved on systems with enough processing power and memory. I've almost get everything needed working already. I will post a link to the github repository as soon as I post the first version.

Thanks to all for the suggestions.
Jonathan

rjf

unread,
May 25, 2020, 2:02:42 PM5/25/20
to sage-devel
You haven't provided enough information about your environment, but it is
generally feasible to acquire data in Maxima from a file or a socket or a stream.
Or compute numerical stuff, generate random numbers, read from a
keyboard, web page...etc.

Given that you are familiar with a particular programming language (here, Python)
that is not a symbolic math system your approach probably is structured as follows:

   Your (mostly already written?) python program
     does stuff  A 
      then somehow calls your symbolic math program as a subroutine on data B <your inquiry about Sage>
     then does something with the returned answer  C

.  There are a few questions here:  your program must format something
to be input to the symbolic math program.  It could be a string, or maybe
something that looks like a tree with pointers, or lisp list.  Any of these
forms may be tricky to produce correctly. You need to know about the symbolic math pgm.

Later, your program must receive an answer from the symbolic program.
Maybe it is just  "true"  or 43.  But if it is a symbolic expression, then
it is maybe a string or a tree with pointers.  Or maybe you are just
displaying it?? Anyway, you may need to know even more about the symbolic math pgm.

Generally, it may be tricky for your program to accept the answer.

(this is really a standard question for the ages --  
Physicist:  can you do symbolic math for me.
<discussion>
Computer scientist:   OK, we can compute "sin(x)*exp(x^2)"  
  What are you going to do with this in your Fortran program?
Physicist:  uh, evaluate it at x=.1, .2, .3 ...
Computer scientist:  you mean to call a compiler??  you can just
let the symbolic math program do the evaluation, write it into a file,
or just plot the curve directly..
Physicist: You can do all that?  But my program runs ... < excuses...>
)


So you are possibly committing yourself to writing a parser,
a string output display,  calling a compiler??? ... what else?

Here's another structure:

Start up Maxima and load a program that does "stuff A".   It might
even do "stuff A" by calling Python.  Maxima is written in Lisp,
and there are ways of calling Python.  Or the code could be
written in Lisp (compiled, maybe faster than Python?) or in the
Maxima language.

  Any of these languages can acquire info from a user, if
that is what is in your task. Error checking of symbolic
formula input is already written.

  Having done stuff A, the symbolic part is ready to roll..
What to do with the answer? 
 Whatever.   Maxima can decompose the answer, or display
it or write to a file ... If it is
necessary to run in Python, it is again possible to
call Python.  (And hand it a string or lisp/maxima structure).
If python is called,   It can return to Maxima, and Maxima can loop back
for the next iteration.

.............
If you absolutely have to have Python as "the boss"  you could still do this:

Start up a python system that does almost nothing but initialize
Maxima and call it.
   Then use the structure outlined above.

For what it is worth, I have directly called python library routines
(interfaces to multiple-precision arithmetic, as it happens) from lisp.
Whether this approach is feasible is probably not a technical
question -- just depends on what you are comfortable doing.

It may be too late to consider this kind of change, and it may be
a bad fit for your application for some other reason.  In which
case maybe think about this for your next project.

If you insist on calling the symbolic math program as a subroutine,
perhaps the simplest interface is to invoke Maxima on some input
from a command line.  If your task is simple enough.



RJF

Jonathan

unread,
May 25, 2020, 3:49:09 PM5/25/20
to sage-devel
As promised here is a git repository with a myBinder demonstration of what I have so far. Once I extend it to handling inequalities, it will more than meet my use case needs.

Some have asked for more specifics. Here is a list of some of the more important requirements:

1) Can be installed in a plain vanilla python3 virtual environment via pip or simply as a python file to be loaded.
2) Does not conflict with SymPy or NumPy.
3) Will load and run fast enough to avoid user complaints on a Raspberry Pi. One initial use case is being combined with Pi data acquisition hardware and python tools for controlling them.
4) Makes sense to scientists in the fields of Physics, Chemistry and Biology.

Thanks,
Jonathan

rjf

unread,
May 25, 2020, 6:39:04 PM5/25/20
to sage-devel
It looks like you have written a recursive descent parser. And a display.
If you were running Maxima on a Pi, (see sourceforge for download)
you would have a parser and a display without writing it yourself.

  Just looking at the code briefly, I think you have to decide
if you actually meet your own specs.  I don't know what sympy will
provide, so maybe it is really OK.

For example,

a*b*c*d*e = a*g*c*d*f
divide by b*d  do you get

a*c*e = a*g*c*f/b  ?


or more seriously,  (x^2-1)/(x+1)   to get (x-1) ?

I would be surprised if you were the first person to write
a parser like this in Python, but it is a learning experience.

Good luck.

Dima Pasechnik

unread,
May 26, 2020, 3:47:09 AM5/26/20
to sage-devel
On Mon, May 25, 2020 at 11:39 PM rjf <fat...@gmail.com> wrote:
>
> It looks like you have written a recursive descent parser. And a display.
> If you were running Maxima on a Pi, (see sourceforge for download)
> you would have a parser and a display without writing it yourself.
>
> Just looking at the code briefly, I think you have to decide
> if you actually meet your own specs. I don't know what sympy will
> provide, so maybe it is really OK.
>
> For example,
>
> a*b*c*d*e = a*g*c*d*f
> divide by b*d do you get
>
> a*c*e = a*g*c*f/b ?
>
>
> or more seriously, (x^2-1)/(x+1) to get (x-1) ?
>
> I would be surprised if you were the first person to write
> a parser like this in Python, but it is a learning experience.

as well as a Groebner basis implementation, and a multivariate
polynomial factorisation
implementation...
Well, both are in Sympow:
https://docs.sympy.org/latest/modules/polys/reference.html
https://docs.sympy.org/latest/tutorial/simplification.html

>
> Good luck.
>
>
>
>
>
> On Monday, May 25, 2020 at 12:49:09 PM UTC-7, Jonathan wrote:
>>
>> As promised here is a git repository with a myBinder demonstration of what I have so far. Once I extend it to handling inequalities, it will more than meet my use case needs.
>>
>> Some have asked for more specifics. Here is a list of some of the more important requirements:
>>
>> 1) Can be installed in a plain vanilla python3 virtual environment via pip or simply as a python file to be loaded.
>> 2) Does not conflict with SymPy or NumPy.
>> 3) Will load and run fast enough to avoid user complaints on a Raspberry Pi. One initial use case is being combined with Pi data acquisition hardware and python tools for controlling them.
>> 4) Makes sense to scientists in the fields of Physics, Chemistry and Biology.
>>
>> Thanks,
>> Jonathan
>>
>> On Thursday, May 21, 2020 at 8:30:42 AM UTC-5, Jonathan wrote:
>>>
>>> Dear All,
>>>
>>> I have a use case where I need something lighter weight than the whole of Sagemath. I think SymPy + the ability to handle math on symbolic equations as Sagemath does it might be enough. Thus I wanted to see if I could extract from Sagemath the code supporting math on symbolic expressions and overlay that on SymPy or at least use that as a template. Can somebody please point me to the place to start looking in the codebase?
>>>
>>> To make sure people understand what I am interested in, here is a simple example of the ability I would like to extract:
>>> >>>eq1 = p*V==n*R*T
>>> >>>eq1
>>> p*V=n*R*T
>>> >>>eq2=eq1/V
>>> >>>eq2
>>> p=n*R*T/V
>>>
>>> Thanks,
>>> Jonathan
>
> --
> 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+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/579775eb-c74a-4dcc-a76c-8a8ebe6a5ca6%40googlegroups.com.

Jonathan

unread,
May 26, 2020, 9:56:52 AM5/26/20
to sage-devel
@rjf,

As much as possible I am trying to avoid reinventing the wheel. I have not even really written a parser. All I have done is extended the parser already written in SymPy to understand algebraic equalities that have both a lhs and a rhs. By leveraging what the SymPy team has done I got relatively robust manipulations and math display with less than 150 lines of code. It does not do everything, but provides almost exactly the tools needed for my use case with students at the undergraduate level. My initial target is for laboratory notebooks/exercises in general chemistry. The students will collect data and then have to manipulate some simple relations before substituting in values derived from the collected data. I am hoping this will reduce the problems I have trying to help students who do not precisely record their algebraic manipulations nor keep very good track of how they punch numbers into equations on their calculators. Jupyter notebooks using this code, SymPy and Python based data acquisition software should keep a more reliable record of what they have done. Maybe they will even be able to find the mistakes more easily without my assistance.

I understand you are a strong booster for Maxima. I have used SageMath for years and thus have taken advantage of Maxima's capabilities as they are called upon from SageMath. In my case there are a number of  issues that make Maxima a poorer choice than SymPy:
  1. The support code necessary to make it work in the python/Jupyter environment does not seem to exist in an easy to install form (e.g. SageMath)
  2. My research suggested that recreating or extracting the code to talk to Maxima, would be much more involved than the simple extension of SymPy, which I have developed.
  3. Sometimes as much fun as it is to drive a fancy sports car, a VW-bug may be more appropriate and may prevent less experienced drivers from having a disaster. See the discussion of branch choices and integrals in SageMath, that I've been involved in here. What presently happens is correct, but confusing to those used to working in a specific domain (the real domain for many physical scientists).
And yes, this was a learning experience. I now have a much better understanding of the approach SymPy takes to doing symbolic math.

Jonathan
Reply all
Reply to author
Forward
0 new messages