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

Refactor implicit parsing #1871

Merged
merged 12 commits into from
Mar 11, 2013
Merged

Conversation

lidavidm
Copy link
Member

@lidavidm lidavidm commented Mar 6, 2013

Changes:

  • Function exponentiation, implicit multiplication, and implicit application are now separate transformation steps.
    • They are still somewhat order-dependent - sin 2x only works if implicit multiplication comes before implicit application.
  • API remains the same as before.
  • sin 2x works now. (Before it gave x*sin(2), now it gives sin(2*x).)
  • Expanded the docs a bit

https://code.google.com/p/sympy/issues/detail?id=3678

@jrioux
Copy link
Member

jrioux commented Mar 6, 2013

SymPy Bot Summary: ✳️ Passed after merging lidavidm/refactor_implicit_parsing (c966c6a) into master (9bc7091).
✳️ PyPy 2.0.0-beta-1; 2.7.3-final-42: pass
✳️ Python 2.7.2-final-0: pass
✳️ Python 3.2.1-final-0: pass
✳️ Sphinx 1.1.3: pass
Docs build command: make clean && make html-errors && make latex && cd _build/latex && xelatex sympy-*.tex

@asmeurer
Copy link
Member

asmeurer commented Mar 6, 2013

Was this originally added before or after the 0.7.2 release? If after, go ahead and break the API without abandon. And even if before, we should consider this module to be still in a development stage, and hence should probably consider the API to be evolving.

symbol = tok[1][1:-1]
if _token_splittable(symbol):
for char in symbol:
result.extend([(NAME, "'{}'".format(char)), (OP, ')'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Causes failures in python 2.5 and 2.6

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess just use +=

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh never mind, it's the format, not the extend. Just use %s.

@asmeurer
Copy link
Member

asmeurer commented Mar 6, 2013

I'm assuming the old tests are still testing the same behavior, but at least add tests that the functions are split correctly.

@asmeurer
Copy link
Member

asmeurer commented Mar 6, 2013

Let's refactor token_splittable so that it is easy to expand it to include custom names that should not be split.

@asmeurer
Copy link
Member

asmeurer commented Mar 6, 2013

It looks like people are already using this functionality. See http://colabti.org/irclogger/irclogger_log/sympy?date=2013-03-06#l166. It's probably because I mentioned it on StackOverflow.

@lidavidm
Copy link
Member Author

lidavidm commented Mar 6, 2013

Changes:

  • split_symbols_custom allows for a custom predicate to determine whether to split a symbol or not.
  • Fixed Python 2.5/2.6 compatibility
  • Added tests for individual steps

@jrioux
Copy link
Member

jrioux commented Mar 7, 2013

SymPy Bot Summary: ✳️ Passed after merging lidavidm/refactor_implicit_parsing (a48a129) into master (6f0e757).
✳️ PyPy 2.0.0-beta-1; 2.7.3-final-42: pass
✳️ Python 2.7.2-final-0: pass
✳️ Python 3.2.1-final-0: pass
✳️ Sphinx 1.1.3: pass
Docs build command: make clean && make html-errors && make latex && cd _build/latex && xelatex sympy-*.tex

@asmeurer
Copy link
Member

SymPy Bot Summary: ✳️ Passed after merging lidavidm/refactor_implicit_parsing (a48a129) into master (49eea7e).
✳️ Python 2.5.6-final-0: pass
✳️ Python 2.6.8-final-0: pass
✳️ Python 2.7.3-final-0: pass
✳️ Python 3.2.3-final-0: pass
✳️ Sphinx 1.1.3: pass

@lidavidm
Copy link
Member Author

Are any other changes needed?

@asmeurer
Copy link
Member

I mean, test things like 2 x should fail for implicit application, and so on.

@asmeurer
Copy link
Member

Add tests for the custom splitting.

@lidavidm
Copy link
Member Author

Changes:

  • Test symbol splitting (make sure all Greek letters are not split by default, make sure custom splitting works)
  • Make sure expressions raise SyntaxError/TypeError as appropriate if the requisite transformations are not used

@jrioux
Copy link
Member

jrioux commented Mar 11, 2013

SymPy Bot Summary: ✳️ Passed after merging lidavidm/refactor_implicit_parsing (5cae26e) into master (07933cd).
✳️ PyPy 2.0.0-beta-1; 2.7.3-final-42: pass
✳️ Python 2.7.2-final-0: pass
✳️ Python 3.2.1-final-0: pass
✳️ Sphinx 1.1.3: pass
Docs build command: make clean && make html-errors && make latex && cd _build/latex && xelatex sympy-*.tex

@asmeurer
Copy link
Member

This looks good.

For the API, we should think of a more extensible way to do this. It is probably more elegant to have an object oriented solution, where you override methods on the base class to get custom behavior (c.f. the way that the ast module works in the standard library).

@asmeurer
Copy link
Member

Also http://greentreesnakes.readthedocs.org/en/latest/ might be a useful read if you find the API docs for ast too dense for a first read.

@asmeurer
Copy link
Member

But as it were, this is fine, so I'm merging. If there are any functional changes here we can update SymPy Gamma (are there?).

asmeurer added a commit that referenced this pull request Mar 11, 2013
@asmeurer asmeurer merged commit bdcacb8 into sympy:master Mar 11, 2013
@lidavidm
Copy link
Member Author

This shouldn't affect Gamma. In any case, I can update Gamma in the next pull I'm working on (steps for derivatives and maybe some integrals).

For the API, you're suggesting some sort of Visitor/Transformer class (but for tokens) akin to the AST module?

@asmeurer
Copy link
Member

Or maybe just use AST, instead of tokenize.

I don't know if it will be literally "visitor" and "transformer". My point is that APIs where you subclass and override what you what to change work quite well for these things. I can point to other completely unrelated things that work this way too. For example, HTMLParser in the standard library.

Doing things this way lets you only define those things that are really different for your case. The rest will just come from inheritance. And if you understand how to properly apply it, you can get some pretty good mileage out of the method resolution order and multiple inheritance.

If it's not immediately obvious to you how to design this (and it probably isn't), I would recommend asking on the list and seeing if the community can develop a good API for it.

@lidavidm
Copy link
Member Author

Okay, I'll look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants