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

[BUG] error when handling coefficients with fractions #2594

Open
VincentTam opened this issue Jun 11, 2022 · 6 comments
Open

[BUG] error when handling coefficients with fractions #2594

VincentTam opened this issue Jun 11, 2022 · 6 comments

Comments

@VincentTam
Copy link
Contributor

Steps to reproduce:

  1. URL encode simplify('w/5-(2w+3)/2-3w/5') to simplify%28%27w%2F5-%282w%2B3%29%2F2-3w%2F5%27%29 using any URL encoder (I used https://www.urlencoder.io/).
  2. Hit https://api.mathjs.org/v4/?expr=simplify%28%27w%2F5-%282w%2B3%29%2F2-3w%2F5%27%29 in the address bar in any web browser (I'm using Google Chrome).
  3. Got output w * -2 / 5 + (-(2 * w) - 3) / 2.

Expected output:
$$\frac{w}{5}-\frac{2w+3}{2}-\frac{3w}{5} = -\frac75w-\frac32$$

@josdejong
Copy link
Owner

Thanks, would be nice to improve simplify to also simplify this case. So in short:

math.simplify('w * -2 / 5 + (-(2 * w) - 3) / 2').toString()
// actual output:   "w * -2 / 5 + (-(2 * w) - 3) / 2"
// expected output: "w * -7 / 5 + -3 / 2"

Anyone interested in writing additional rules for simplify to address cases like these?

@luckyayush777
Copy link

Hi, I would like to take a shot at this. Haven't contributed to an open source project yet so please excuse the obvious mistakes if any. The simplify function breaks ( no further simplification is done ) if the inner string is of the form n1 + ( n1 * c1 + c2) / c3. The approach would then be to simplify that to something like n1+((c1*n1)/c3+c2/c3). Is that correct ?

@josdejong
Copy link
Owner

Thanks Ayush. It will indeed be a solution around expanding the two terms inside the parenthesis, and/or extracting the constants out of it and then simplifying the constant terms. It will require some trial and error I think to see how things work out and if there are no unwanted side effects.

@luckyayush777
Copy link

So adding one extra rule in the form of s: '(c1*v+c2)/c3 -> ((c1*v)/c3)+c2/c3', makes the above mentioned expression work if there is no subtraction (If it was 'w/5+(2w+3)/2+3w/5'). That results in the correct w * 9 / 5 + 3 / 2

@stephenlrandall
Copy link

stephenlrandall commented Jul 16, 2022

Came to report a similar issue and found this one already open. A simpler example of the problem, if it helps:

simplify("(-0.5) x + y")
    x * -1 / 2 + y
simplify("(-0.5) x + y").toTex()
    "\frac{ x\cdot-1}{2}+ y"

@gwhitney
Copy link
Collaborator

gwhitney commented Jul 26, 2022

A simpler example of the problem, if it helps:

  simplify("(-0.5) x + y")
    x * -1 / 2 + y

Sorry, what's the desired output in this case? -x/2 + y? I don't see anything wrong with the current actual output, so what it should simplify to may be a matter of judgment, so it would be good to know your view about what you expected.

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