Hi Oscar, > The problem is that while SymPy expressions can define __div__(self, > int) to be exact there isn't a way for SymPy to hook into an > expression like 1/2 which is just a Python expression using the int > type whose __div__ returns floats.
The entire point of my proposal is to give SymPy the possibility to "hook into expressions like 1/2". The examples I posted come from an interpreter (and slightly modified SymPy) where I made that possible. The idea is that the Python parser keeps the information where the 1/2 is coming from, and give SymPy a chance to peek into it. And all this in a documented, official way, without the need to do magic. > Note that the Python float 0.1 is not exactly equal to the rational > number 1/10 which can not be represented exactly in binary floating > point. Also in this case my proposal would give SymPy the chance to understand what the user actually entered, and act accordingly. I agree that the case for 0.1 is not as compelling as the one for 1/2: while for the latter the user almost certainly means "one half", this is not clear for the former. > Some other programming languages have special syntax for rational > numbers e.g. 1//2 in Julia or 1%2 in Haskell but I don't know of a > programming language that uses 1/2 syntax for rational numbers apart > from things like Maple/Mathematica. On the thread some days ago we had this discussion, whether we should add a new operator. People didn't seem to like it. > I think that maybe the best solution here is something more like a > domain-specific language that can be used with ipython/jupyter as an > alternative profile. In the DSL 1/2 could be a Fraction and 2x^2 could > be the equivalent of 2*x**2 etc. I think SageMath does exactly this. While this is perfectly fine, it splits the symbolic-math-in-Python world. If everybody uses their favorite DSL, we drift apart. My goal is to look at those DSLs and check what could be integrated into standard Python. I started with the 1/2 problem, because it was a problem I actually stumbled upon, and which I considered simple to solve. > You'd probably want to be able to > write things like √2 and x'' and so on. Maybe you could have a better > syntax for creating matrices than the clunky list of lists. Basically > there are lots of things that you can't quite do in Python but that > you might want to do if you were making a language specifically for > the purpose of doing stuff with equations. For most of the time my opinion was: stop dreaming, get real. But then they pushed structural pattern matching, a super complicated beast of syntax for not much benefit. So I hoped that we could get some syntax for symbolic math as well. Cheers Martin _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/6BJG3KQT26TFRRBKDO7E2YEBY43TSFRV/ Code of Conduct: http://python.org/psf/codeofconduct/