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

SymPy translation to JavaScript #165

Open
2 of 6 tasks
Kreijstal opened this issue Mar 10, 2019 · 15 comments
Open
2 of 6 tasks

SymPy translation to JavaScript #165

Kreijstal opened this issue Mar 10, 2019 · 15 comments
Labels
Developer tooling Help your fellow developers out by making their job a bit more enjoyable with good tooling. Intermediate Projects that require a medium level of understanding. Doesn't require much prior knowledge. Much work This project takes little time to complete. (ETA several weeks+)

Comments

@Kreijstal
Copy link
Contributor

Kreijstal commented Mar 10, 2019

Project description

As far as I know there are only three JavaScript CASes with various issues, these are

However none of those are at the level of sympy. They can't do set theory, simplify boolean expressions, solve inequalities, etc. So I figured out there could be a translation of sympy into JavaScript and there are many paths to realize this.

  1. Translate NumPy into JavaScript by hand
    • I like this one a lot, however it needs a lot of time, and manpower, and you would finally have a native good quality CAS on JavaScript, the cons are, you would have to mantain it, and update it at the same time as sympy.
    • If this option is chosen, it would be wise to know how to start, would we try to translate it top to bottom or vice-versa? Would we start with core?
    • If this is done there won't be a need to rewrite mpmath for arbitrary precision arithmethic, we can rely on mathjs that allows that with bignumbers.
  2. Use transcription software to transcribe python into JavaScript
  3. Just compile Python into JavaScript with emscripten.
    • This would mean that you wouldn't have to spend so much time mantaining your port of Sympy, but depending on how you compiled it, it will take a really long time to load your libraries and might not work on all browsers (This one is what pyodide has done but currently there is no module on npm available, also pyodide seems to be dependent on the browser considering wasm support for node)
      Edit: Done, it works, but I get out of memory exceptions on some computers, it is extremely slow to load, it takes around 30 seconds to download a huge javascript python "binary", it doesn't work in mobile either, but, it works, yes, it does, it works, sympy in JavaScript. See also this project: https://minireference.com/static/tmp/pypyjs_sympy_demo/
  4. Use a Python intepreter in JavaScript
    • I think that is what brython has done

[Write what technology is relevant. What language, what platform, any particular library/framework/existing project it is based on?]
Convert sympy to javascript
Pyodide

Complexity and required time

[Please only tick off one box in each category by changing [ ] to [x]. The labels on the project will then be updated by the maintainers as soon as possible.]

Complexity

  • Beginner - This project requires no or little prior knowledge of the technolog(y|ies) specified to contribute to the project
  • Intermediate - The user should have some prior knowledge of the technolog(y|ies) to the point where they know how to use it, but not necessarily all the nooks and crannies of the technology
  • Advanced - The project requires the user to have a good understanding of all components of the project to contribute

Required time (ETA)

  • Little work - A couple of days
  • Medium work - A week or two
  • Much work - The project will take more than a couple of weeks and serious planning is required
@FredrikAugust FredrikAugust added Intermediate Projects that require a medium level of understanding. Doesn't require much prior knowledge. Much work This project takes little time to complete. (ETA several weeks+) Developer tooling Help your fellow developers out by making their job a bit more enjoyable with good tooling. labels May 15, 2019
@stefnotch
Copy link

I would like to point out that this is already sort of a thing using pyodide, which fully supports sympy! 🎉

@hodovani
Copy link

@Kreijstal isn't it easier to extend an existed lib than create a new one? It might be faster to add set theory, simplify boolean expressions, solve inequalities to Math.JS. What do you think?

@Kreijstal
Copy link
Contributor Author

@Kreijstal isn't it easier to extend an existed lib than create a new one? It might be faster to add set theory, simplify boolean expressions, solve inequalities to Math.JS. What do you think?

I think currently, it really makes little difference. Since the amount of effort to make an alternative to sympy is around the same, but of course, if you decide to update existing math libraries, then, by all means. Please do so. Also it doesn't mean we cannot use their functions that they define if it saves time.

@hodovani
Copy link

@Kreijstal do you know if someone worked on this feature? I would like to learn more about these algorithms by doing tools.

@Kreijstal
Copy link
Contributor Author

In order to do inequalities you need to have a way of using operation on sets of numbers see this for example
jiggzson/nerdamer#456
jiggzson/nerdamer#350 (comment)_
of course these are just implementations of things that have been done on sympy, there are of course many CAS (Computer Algebra systems, see matlab, mathematica) But of course, they're not open source, and not supported on the browser.

@stefnotch
Copy link

I checked out the different computer algebra systems for QuantumSheet (WIP - searching for collaborators). Basically, this is what I found

Mathematics Backends

Less Important

Far future

@Kreijstal
Copy link
Contributor Author

Kreijstal commented Jan 20, 2021

I would like to point out that this is already sort of a thing using pyodide, which fully supports sympy!

I mean, sure I've done this and it's pretty nice, but of course it's a bit heavy since it has to use nasm, and painfully slow, on phones, for example, would be nice native javascript but of course, its still an option.
like idk, about you but this takes like around 30 seconds to load. https://stefnotch.github.io/quantum-sheet/


just timed it
image I was wrong it takes more, I mean sure, pyodide works, and works right now which is important, but longterm it'd be nice to implement this in native javascript I'm pretty sure it'd help for researchers when they want to share mathematical models, and they don't have to wait 3 minutes for the programs to load.

@stefnotch
Copy link

stefnotch commented Jan 20, 2021

Yes, the size of pyodide is definitely a big issue. It should be possible to slash the size at least in half, as documented here pyodide/pyodide#646

If you look at the network tab, you can see that it transfers multiple megabytes of data (especially on first load, on subsequent loads most of it should be cached)
image

Though, for development purposes, this is actually not an issue. If you use a shared worker and make sure to keep one tab open, you can keep the worker alive instead of reloading it every time.

Regarding a native Javascript version, yes, that would be quite interesting indeed. The main issue is obviously finding someone who would put in the time and effort to make sure that the CAS really does end up being a top-notch CAS. Currently, projects like Giac/Xcas seem like the closest thing to a proper web-based CAS.

@hodovani
Copy link

In order to do inequalities you need to have a way of using operation on sets of numbers see this for example
jiggzson/nerdamer#456
jiggzson/nerdamer#350 (comment)_
of course these are just implementations of things that have been done on sympy, there are of course many CAS (Computer Algebra systems, see matlab, mathematica) But of course, they're not open source, and not supported on the browser.

sorry, I am not sure that I understand your message. Do you know if we have something that is similar to SymPy? If not. Should we start creating this?

@liudonghua123
Copy link

If we want to implement some features like python's sympy, the operator overload should be necessary.

@stefnotch
Copy link

If we want to implement some features like python's sympy, the operator overload should be necessary.

What do you mean? If you're referring to Javascript operator overloading, then no, that's not necessary at all.

@stefnotch
Copy link

  1. Use a Python intepreter in JavaScript
    I think that is what brython has done

Regarding point 4, the Brython developer has attempted that and ran into a few blocking issues that would have to be sorted out on the Sympy side
sympy/sympy#18889

@liudonghua123
Copy link

liudonghua123 commented Apr 23, 2021

If we want to implement some features like python's sympy, the operator overload should be necessary.

What do you mean? If you're referring to Javascript operator overloading, then no, that's not necessary at all.

If there isn't operator overloading, some arithmetic operation like the following python code would implement and write using a complicate and indirectly method.

# this is a sample python sympy code
x = symbols('x')
expr = x**2 + 2*x + 1

The correspond js code maybe like this

import symbols, {add, multiply, exp} from 'symbols';
const x = symbols('x')
const expr = add(add(exp(x, 2), multiply(x, 2)), 1)

The form of const expr = add(add(exp(x, 2), multiply(x, 2)), 1) is too bad and not easy readable.

@stefnotch
Copy link

@liudonghua123 As far as I know, you would need more than just operator overloading. You would need something closer to expression trees.

For example, take the following expression (Javascript)

const expr = 3/7;

Since it's a fraction that can't be represented in base 2 nor in base 10, you would want to store the actual fraction 3 / 7 and not the result (0.4285...).

To do that with operator overloading, you have to overload the division operator for the built-in number type. Javascript might never get the ability to do that.

A more doable approach right now would be parsing string-expressions and turning them into sympy code. Something like

const expr = sympy("3/7"); // Generates an expression tree

@KOLANICH
Copy link

I guess another way to achieve it is to compile https://github.com/symengine/symengine into WASM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Developer tooling Help your fellow developers out by making their job a bit more enjoyable with good tooling. Intermediate Projects that require a medium level of understanding. Doesn't require much prior knowledge. Much work This project takes little time to complete. (ETA several weeks+)
Projects
None yet
Development

No branches or pull requests

6 participants