I tried to write basically the same question when I ended up here. However, I found some ideas that are not mentioned here. Firstly, I agree with @nelshh that in some specific cases you can find canonical forms that allow you to check the equality of expressions.
I found some examples of canonical forms:
- The most famous is probably the minterm canonical form in Boolean algebra, which is used, for example, in the synthesis or verification of a circuit.
- Polynomial expressions also admit canonical form as the sum of monomials. This may solve your examples:
- The canonical form for rational numbers is an irreplaceable share .
Your examples:
- Both are already in canonical form, you just need to sort them in increasing order.
2*x is in its canonical form, x+x not (since both operands of addition have the same degree).- Both are already in canonical form (monomials of degree -1), except that the coefficient is
4/(x/2) , which 4/(1/2) not in canonical forms for rational numbers.
If you are still interested in this, I would suggest that you experiment with a computer algebra system such as sympy for python (it probably also exists for java). However, I also believe that you should remove the java and floating-point tags (this has nothing to do with how the computer stores real numbers) and add the computer-science tag.
For example, sympy might say things like this:
>>> Rational(3,4)*(x+y)**2 2 3β
(x + y) ββββββββββ 4 >>> Rational(3,4)*(x**2+y**2)+Rational(1,4)*2*x*y+Rational(4,8)*2*x*y 2 2 3β
x 3β
xβ
y 3β
y ββββ + βββββ + ββββ 4 2 4 >>> expand(Rational(3,4)*(x+y)**2)==expand(Rational(3,4)*(x**2+y**2)+Rational(1,4)*2*x*y+Rational(4,8)*2*x*y) True
source share