nerdamer icon indicating copy to clipboard operation
nerdamer copied to clipboard

A simplify function

Open Happypig375 opened this issue 8 years ago • 19 comments

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 avatar Aug 31 '17 03:08 Happypig375

(Inserted into #202)

Happypig375 avatar Aug 31 '17 11:08 Happypig375

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

Happypig375 avatar Sep 20 '17 14:09 Happypig375

@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?

jiggzson avatar Sep 25 '17 19:09 jiggzson

#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 avatar Sep 26 '17 11:09 Happypig375

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 avatar Sep 26 '17 11:09 Happypig375

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

Happypig375 avatar Oct 01 '17 03:10 Happypig375

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 avatar Oct 01 '17 15:10 Happypig375

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

Happypig375 avatar Oct 16 '17 11:10 Happypig375

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

Happypig375 avatar Oct 25 '17 10:10 Happypig375

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

Happypig375 avatar Nov 21 '17 13:11 Happypig375

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.

Happypig375 avatar Dec 02 '17 11:12 Happypig375

@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.

jiggzson avatar Dec 03 '17 21:12 jiggzson

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 <>.

Happypig375 avatar Dec 08 '17 11:12 Happypig375

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.

jiggzson avatar Dec 09 '17 16:12 jiggzson

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

Happypig375 avatar Dec 10 '17 01:12 Happypig375

((-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 avatar Jan 01 '18 11:01 Happypig375

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.

Happypig375 avatar Jan 02 '18 13:01 Happypig375

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?

jiggzson avatar Jan 02 '18 17:01 jiggzson

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#.

Happypig375 avatar Jan 02 '18 18:01 Happypig375