MatthewJA / Coffeequate

A computer algebra system for JavaScript.
http://matthewja.com/Coffeequate
MIT License
48 stars 8 forks source link

Simplify common exponent #102

Closed zvxryb closed 9 years ago

zvxryb commented 9 years ago

CQ('a**2 + b**2 = c**2').solve('a') yields the slightly awkward result +/-sqrt(-(1))*sqrt(b**2 - c**2)

This patch collects common exponents in multiplication, resulting in +/-sqrt(c**2 - b**2)

bshlgrs commented 9 years ago

@MatthewJA, we really need some automated tests for Coffeequate PRs.

@zvxryb, good job on this (and fixing our missing newline at end of file).

MatthewJA commented 9 years ago

@zvxryb: Thanks for this! I noticed that a**2 * b**2 seems to fail, though, due to an infinite recursive loop. I think this is because terms in your loop are not being deep-copied and are thus being mutated (nodes in CQ are mutable). I can fix that when I have time, if you'd like, or you can. If you want to, the relevant method is the .copy() method that nodes in CQ have.

zvxryb commented 9 years ago

I tried CQ('a**2 + b**2') with .solve('a'), .simplify() and .toString() in Firefox and Chromium, both seemed to work OK. Is there a specific operation that it fails for? Misread that as a**2 + b**2 rather than a**2 * b**2. I am able to reproduce this issue and I'll look into it further.

zvxryb commented 9 years ago

Fixed the stack overflow, which was caused by grouping terms with no exponent and simplifying the new subexpression. Also cleaned up side-effects which would have necessitated a deep copy.

MatthewJA commented 9 years ago

Looks good! I'll merge it in.