Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement parsing of expressions #559

Closed
certik opened this issue Jul 21, 2015 · 13 comments
Closed

Implement parsing of expressions #559

certik opened this issue Jul 21, 2015 · 13 comments

Comments

@certik
Copy link
Contributor

certik commented Jul 21, 2015

We'll eventually need to implement a fast parser of expressions like: https://github.com/sympy/symengine/blob/8dd550ba20c0a13fb5a3e34c197c10bfaf79ae8d/benchmarks/expr.txt

Probably the easiest and most robust is to implement our own parser, I don't think it's very difficult, since it is just parsing of expressions. Here is one tutorial that I found (there are many out there): http://www.strchr.com/expression_evaluator, we just need to replace the return type double with RCP<const Basic>, and add support for functions and symbols. I think that's it.

(The main motivation is that SymPy takes a long time to sympify this long expression.)

@srajangarg
Copy link
Contributor

@certik , have researched into this, and will begin working.

I wanted to know how symbols will be represented? Will a user be made to enter
symbol(x)+1 or just x+1
In the latter case, how do we check if it's a symbol or an integer (we receive 'tokens' x and 1, but how do we know what is what)? (This problem won't occur with the symbol() implementation) One way is to see, if token string contains anything other than digits. If it does, then it's a symbol by default, otherwise treated as a number.

Another thing was dealing with double values entered. (2.34 + 1) Here, how should we convert the double into rational? Should there be a function which convertsfloat/double to Rational?

@srajangarg
Copy link
Contributor

@isuruf @Sumith1896 Thoughts? I've already got a basic version running, and need to know specifics to proceed.

@isuruf
Copy link
Member

isuruf commented Jan 19, 2016

For double 2.34 it should be RealDouble or RealMPFR depending on the number of digits.
You can use a regex to check if the token is numeric or not.

@rwst
Copy link
Contributor

rwst commented Jan 19, 2016

This needs thought on how to provide a parser to users of symengine, at all. I mean you don't change parsers within SymPy sessions either. Or is it thought purely for symengine devs to write tests?

@isuruf
Copy link
Member

isuruf commented Jan 19, 2016

@rwst, what do you mean by

you don't change parsers within SymPy sessions either

@rwst
Copy link
Contributor

rwst commented Jan 19, 2016

Usually SymEngine is loaded manually onto an existing SymPy session. At this time the original parser is already active and symbols may exist.

@isuruf
Copy link
Member

isuruf commented Jan 19, 2016

Even if symbols exist, there should be no problem right?
Are you referring to GiNaC's convention of symbol("x") != symbol("x")?

@rwst
Copy link
Contributor

rwst commented Jan 19, 2016

What if in SymPy:

In [1]: ex = x+y

In [2]: from symengine import *

In [3]: sin(ex)

Wouldn't there be a problem? The new parser would take ex as symbol and not call sin(Add).

@isuruf
Copy link
Member

isuruf commented Jan 19, 2016

You are right. That will have to be considered. For the use case though it's not really a problem as it is more of a serialize/deserialize problem.

@isuruf
Copy link
Member

isuruf commented Jan 21, 2016

@rwst, it seems SymPy doesn't do that either.


In [1]: from sympy import *

In [2]: x = 10

In [3]: sympify("x")
Out[3]: x

@certik
Copy link
Contributor Author

certik commented Jan 21, 2016

@srajangarg as the first iteration, treat every name as a symbol, unless it is a known function. I assume we can design it so that the user can customize the list of functions. So if it is a number, let's do integers and rationals only for now, then it just returns Integer or Rational. Othewise if it is some text, it will create a symbol. Finally, if it is like sin(x) or f(x), then it will create either a known function, or a "function symbol".

I would not try to parse code like symbol("x") as initializing a symbol, at least not now.

Let me know if you have any other questions.

@Sumith1896
Copy link
Contributor

Fixed via #772

@certik certik closed this as completed Feb 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants