jiggzson / nerdamer

a symbolic math expression evaluator for javascript
http://www.nerdamer.com
MIT License
513 stars 82 forks source link

A simplify function #210

Open Happypig375 opened 7 years ago

Happypig375 commented 7 years ago

Another look at Algebrite:

simplify(cos(x)^2 + sin(x)^2) => 1 simplify(a*b+a*c) => a (b + c) simplify(n!/(n+1)!) => 1 / (1 + n)

I don't think nerdamer can currently do this (factor does the middle one only). This function would be really helpful!

Happypig375 commented 7 years ago

(Inserted into #202)

Happypig375 commented 6 years ago

Ideally it would also do the rationalization, and here is a bunch of tests (not sending as PR as the syntax has not been confirmed):

Input My answer
6/sqrt(3) 2sqrt(3)
1/(sqrt(2)-1) sqrt(2)+1
3/(sqrt(5)+sqrt(8)) 2sqrt(2)-sqrt(5)
sqrt(2)/(sqrt(6)-sqrt(2)) (sqrt(3)+1)/2
(sqrt(7)-3)/(sqrt(7)+3) 3sqrt(7)-8
(5-2sqrt(2))/(2sqrt(2)-3) 2sqrt(2)+7/2
(3sqrt(3)+sqrt(7))/(2sqrt(3)-sqrt(7)) 5+sqrt(21)
(3+sqrt(5)/((sqrt(5)+1)(sqrt(5)-2)) (7+3sqrt(5))/2
1/(3sqrt(2)+sqrt(5))-1/(3sqrt(2)-sqrt(5)) -2sqrt(5)/13
2sqrt(5)/(4-sqrt(5))+3/(sqrt(5)+4) 2-5/11sqrt(5)
6/(3+sqrt(3))*6/(sqrt(3)-1) 6sqrt(3)
4/(sqrt(x^2+2x+4)+sqrt(x^2-2x+4)) (sqrt(x^2+2x+4)-sqrt(x^2-2x+4))/x
(x^2-1)/(sqrt(x^2+x+1)-x) x sqrt(x^2+x+1)-sqrt(x^2+x+1)+x^2-x
jiggzson commented 6 years ago

@Happypig375 . I like your suggestions and some of the simplifications in the table above should probably be the default behavior of the library. Other than the initial examples, did you have any other simplifications in mind?

Happypig375 commented 6 years ago

119 (my first ever issue on here pops up again) is already a big simplification. I would not see x^2+4x-45 and x^2+x-30 as having any common factors unless I factorize it specifically.

Happypig375 commented 6 years ago

http://nerdamer.com/functions/Expression.sub.html

x = nerdamer('cos(x)*tan(x)').sub('tan(x)', 'sin(x)/cos(x)').evaluate()

This is also a simplification.

Happypig375 commented 6 years ago

6^(2π)-36^π => -36^pi+6^(2*pi) Can simplify to 0.

Happypig375 commented 6 years ago

floor(floor(x)) can=> floor(x) floor(ceil(x)) can=> ceil(x) ceil(floor(x)) can=> floor(x) trunc(round(ceil(floor(x)))) can=> floor(x) etc.

Happypig375 commented 6 years ago

asin(sin(3)) can=> pi-3

Happypig375 commented 6 years ago

sec(x)cos(x) can=> 1

Happypig375 commented 6 years ago

This one is more important. sqrt(2)*sqrt(6) should=> 4*sqrt(3) (reference to #312)

Happypig375 commented 6 years ago

http://www.dummies.com/education/math/calculus/using-identities-to-express-a-trigonometry-function-as-a-pair-of-functions/ The trig conversion table should be somewhat helpful.

jiggzson commented 6 years ago

@Happypig375 the list you provided contains only basic trig identities. A much more extensive conversion method which includes double angle identities, etc. is already contained in the Calculus.js add-on. I think I'll move that to the core and utilize that one. If you run into more simplifications please do share.

Happypig375 commented 6 years ago

Thought on this: Just like Wolfram Alpha, provide different simplifications with different criteria? Like simplify(x/x) => [0, x <> 0] (Not x!=0 because that is factorial(x)=0)

Note: Currently nerdamer has <, >, <=, >=, =, == but no <>.

jiggzson commented 6 years ago

Sounds good. Keep in mind that equality operators were an idea I was toying with so that's why they're not documented. I guess we can still tweak them as we go.

Happypig375 commented 6 years ago

Ah, and that requires #251 to be implemented. Forgot to say.

Happypig375 commented 6 years ago

((-1/3)*cos(x)*sin(x)^2+(-2/3)*cos(x))-(-cos(x)+(1/3)cos(x)^3) => (-1/3)*cos(x)*sin(x)^2+(-1/3)*cos(x)^3+(1/3)*cos(x) should=> 0

Happypig375 commented 6 years ago

log(1+x)-log(1-x)-log((1+x)/(1-x)) should=> 0

Thought: Maybe defining rules (e.g. replace("log(x)-log(y)", "log(x/y)")) for the simplifier in the parser would help organise and be easier to add new rules.

jiggzson commented 6 years ago

Nerdamer does not use a rule based parser. Remember? Are you talking about when we switch to version 8? Are you familiar when any of the existing JavaScript rule based parsers out there?

Happypig375 commented 6 years ago

Are you talking about when we switch to version 8?

0.8, actually. ( ͡° ͜ʖ ͡°)

Are you familiar when any of the existing JavaScript rule based parsers out there?

Nope, the only JavaScript library that I am familiar with is literally nerdamer. My natural programming language is C#.