cortex-js / compute-engine

An engine for symbolic manipulation and numeric evaluation of math formulas expressed with MathJSON
https://cortexjs.io
MIT License
375 stars 47 forks source link

Fix: Expression with square not sorted correctly #20

Open levirs565 opened 3 years ago

levirs565 commented 3 years ago

Expression with square not sorted correctly because square does not get degree 2. You can see the difference before and after this PR in table below.

Expresssion Canocical Form Before this PR Canocical Form After this PR Status
x^2+x^3+x x^{3}+x+x^2 x^{3}+x^2+x After this PR expression sorted correctly
x^2+x^3+\sin(x) x^{3}+x^2+\sin(x) x^{3}+x^2+\sin(x) Sorted correctly.
\sin(x)+x^2+x^3 x^{3}+\sin(x)+x^2 x^{3}+x^2+\sin(x) This expression's cannocial form must equal with above expression's canonical form

Some test's expected result have been changed. You can see changed test in this table.

Expresssion Canocical Form Before this PR Canocical Form After this PR
x^2y^3+x^3y^2+xy^4+x^4y+x^2y^2 x^{4}y+xy^{4}+x^{3}y^2+x^2y^{3}+x^2y^2 x^{4}y+x^{3}y^2+x^2y^{3}+xy^{4}+x^2y^2
(b^3b^2)+(a^3a^2)+(b^6)+(a^5b)+(a^5) a^{5}b+b^{6}+a^{5}+a^2a^{3}+b^2b^{3} a^{5}b+b^{6}+a^2a^{3}+a^{5}+b^2b^{3}

Why I change the test's expected result?. Canocial form of those expression are inconsistent with other expressions that does not have square. You can see this table:

Expresssion Canocical Form Before this PR Canocical Form After this PR Status
x^2y^3+x^3y^2+xy^4+x^4y+x^2y^2 x^{4}y+xy^{4}+x^{3}y^2+x^2y^{3}+x^2y^2 x^{4}y+x^{3}y^2+x^2y^{3}+xy^{4}+x^2y^2 Different
(b^3b^2)+(a^3a^2)+(b^6)+(a^5b)+(a^5) a^{5}b+b^{6}+a^{5}+a^2a^{3}+b^2b^{3} a^{5}b+b^{6}+a^2a^{3}+a^{5}+b^2b^{3} Different
(b^3b^3)+(a^3a^3)+(b^6)+(a^5b)+(a^5) a^{3}a^{3}+a^{5}b+b^{3}b^{3}+b^{6}+a^{5} a^{3}a^{3}+a^{5}b+b^{3}b^{3}+b^{6}+a^{5}` Equal

Also, this change fix some sorting result when there are square in expression and sin function.

arnog commented 3 years ago

Thank you for investigating this issue and submitting this PR. The Compute Engine codebase is undergoing a refactor which unfortunately makes merging this PR directly a little bit tricky. However, I will make sure to incorporate your changes and preserve their effect. Again, thanks for your submission, it is very much appreciated.