Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.6K Views
|
8 Replies
|
1 Total Like
View groups...
Share
Share this post:

How to check expressions for symbolic equality?

Posted 2 years ago

Can Mathematica compare two symbolic expressions and check for equality? I tried a few test cases at Wolfram Alpha (probably different, but I don't have Mathematica to test with) and ran into some issues.

This query returns True, as expected:

SameQ[(a-b)+c,(c-b)+a]

The following two inputs seem to confuse Wolfram Alpha:

SameQ["(a-b)+c","(c-b)+a"]

SameQ[ToExpression["(a-b)+c"],ToExpression["(c-b)+a"]]

I plan on generating expressions programmatically, but don't know how to generate something that Mathematica can then check for equality.

Does anyone have advice on how to get Mathematica to check two strings, containing expressions, for equality? Or, how can I programmatically create expressions that Mathematica can compare?

POSTED BY: W T
8 Replies
Posted 2 years ago

I apologize for the tone of that post. I was getting frustrated that I couldn't accurately explain what I was looking for and that I felt like we weren't making progress.

However, thanks to your output from Mathematica, I see that Equal[ ] will do what I need it to, and that I also do need to use Simplify[ ]. So, thank you very much for providing that output and also for adding Simplify[ ] to the tests! And luckily, it looks like I can programmatically generate strings and use the ToExpression[ ] function to compare them. In my program, I'll check for True to know if two expressions are equal, and if the comparison does not return True (because it looks like Mathematica returns an expression in this case) I'll know they were different.

Thanks again for your help!

POSTED BY: W T
Posted 2 years ago

You're welcome! Glad to help!

POSTED BY: Eric Rimbey
Posted 2 years ago

POSTED BY: Eric Rimbey
Posted 2 years ago

Yes, I have Mathematica, and I would expect the vast majority of people in this forum to also have it. I ran all of your test cases in Mathematica. I didn't bother with WolframAlpha, because that's not going to be the right context to do this kind of thing (but I understand that you're just experimenting right now). I'll attach a notebook with Mathematica results for your test cases.

WolframAlpha does not have the same capabilities as Mathematica, and given its purpose, I'm not surprised that it cannot interpret ToExpression. For now, it's probably not worth explaining it until you decide to actually acquire Mathematica.

So I need some function in Mathematica to return False when comparing...

You will need to define your own function. I'm not aware of any Mathematica function that will do what you're specifying "out of the box".

I need symbolic comparisons of mathematical expressions. Symbolically, b is not the same as c. And to be verbose, since this seems like a difficult concept...

You're welcome to define your own semantic for what "symbolic comparison" means. I'm not aware of your definition being a standard mathematical one, but I suppose that could be due to my ignorance. Nevertheless, next time you post here, please check your arrogance at the door.

POSTED BY: Eric Rimbey
Posted 2 years ago

I need symbolic comparisons of mathematical expressions. Symbolically, b is not the same as c. And to be verbose, since this seems like a difficult concept:

  • Symbolically a is the same as a
  • Symbolically b is the same as b
  • Symbolically c is the same as c
  • Symbolically a is different from b
  • Symbolically a is different from c
  • Symbolically b is different from c
  • etc for all other letters I use in an expression

So I need some function in Mathematica to return False when comparing

Function[(a - b) + c, (a - c) + b]

The following comparison already works fine in Wolfram Alpha, it doesn't need Simplify[ ]

Equal[(a - b)/(c - d), (b - a)/(d - c)]

This may be because they are the same symbolic mathematical expression. When I give two different symbolic mathematical expressions, Wolfram Alpha tries to "solve" it, which you can see with this example:

Equal[(a - b)/(c - d), (a - b)/(d - c)]

I don't know what the function ToExpression actually does, but when I give Wolfram Alpha the following code:

Equal[ToExpression["(a-b)+c"],ToExpression["(c-b)+a"]]

It gets confused and tells me the meaning of the word equal. And actually, giving it the same input for both sides of the comparison again just gives me the definition of the word equal, which you can see with this:

Equal[ToExpression["(a-b)+c"],ToExpression["(a-b)+c"]]

By the way do you, or anyone else reading this, have Mathematica? I'm curious what Mathematica returns with my set of test cases, ie:

  • Equal[(a-b)+c,(c-b)+a] : Expecting True, Wolfram Alpha returns True
  • Equal[(a-b)+c,(a-c)+b] : Expecting False, Wolfram Alpha returns forms and solutions
  • Equal[(a-b)/(c-d),(b-a)/(d-c)] : Expecting True, Wolfram Alpha returns True
  • Equal[(a-b)/(c-d),(b-a)/(c-d)] : Expecting False, Wolfram Alpha returns forms and solutions
  • Equal[(a-b)/((c-d)^(d-e)), (a-b)*((c-d)^(e-d))] : Expecting True, Wolfram Alpha returns True
  • Equal[(a-b)/((c-d)^(d-e)), (a-b)*((d-c)^(e-d))] : Expecting False, Wolfram Alpha returns forms and solutions
  • Equal[ToExpression["(a-b)+c"],ToExpression["(c-b)+a"]] : Expecting True, Wolfram Alpha defines the word "equal"
  • Equal[ToExpression["(a-b)+c"],ToExpression["(a-c)+b"]] : Expecting False, Wolfram Alpha defines the word "equal"
POSTED BY: W T
Posted 2 years ago

Okay, so you're going to need to clarify your semantics even more, because your test cases pose some problems. For example, you expect

Equal[(a - b) + c, (a - c) + b]

to be False, but if c and b are the same, then the equality would be true. Since you want this functionality to be general, I think it's going to be difficult to specify, for an arbitrary (randomly generated?) expression, what the constraints are in order to determine equality.

Now, for some cases, like

Equal[(a - b)/(c - d), (b - a)/(d - c)]

you could just apply Simplify or FullSimplify:

Simplify[Equal[(a - b)/(c - d), (b - a)/(d - c)]]

True

There might be some way to use Solve for some of your expressions as an indirect way to determine equality, but that's just a hare-brained idea that I haven't fleshed out yet.

As as side note related to this

it fails to correctly interpret Equal["(a-b)+c","(c-b)+a"]

you need to be careful not to conflate the semantics here. Previously, you were using ToExpression which is a way to turn a string into a Mathematica expression, but just applying Equal to strings is certainly not what you want. The strings "(a-b)+c" and "(c-b)+a" are not equal as strings, and you can't expect Mathematica to know that you want to interpret those strings differently.

POSTED BY: Eric Rimbey
Posted 2 years ago

I want to write a program to generate some expressions, and then compare those expressions to see if they are symbolically the same. I won't be assigning values to the "variables" in these expressions. I just need true/false on whether two symbolic expressions are equal.

I've had problems with both Equal and SameQ. I just checked a few more examples and SameQ fails to correctly compare the following symbolic equations: SameQ[(a-b)/(c-d), (b-a)/(d-c)]. Equal[] correctly identifies that those two symbolic equations are equal.

However, Equal[] doesn't always return a True/False answer like I am looking for. When I run Equal[(a-b)+c,(a-c)+b], it returns a plot and different forms/solutions of the input. If there was an EqualQ, that would probably be what I need. Is there some way to get Equal[] to just return True/False instead of trying to "solve" the input?

Also, does Mathematica do anything different when running the above statements? Does Equal[] in Mathematica just return True/False, or does it try to "solve" the input?

Here are some sample inputs and associated outputs I would like to get:

  • Equal[(a-b)+c,(c-b)+a] -> True
  • Equal[(a-b)+c,(a-c)+b] -> False
  • Equal[(a-b)/(c-d),(b-a)/(d-c)] -> True
  • Equal[(a-b)/(c-d),(b-a)/(c-d)] -> False
  • Equal[(a-b)/((c-d)^(d-e)), (a-b)*((c-d)^(e-d))] -> True
  • Equal[(a-b)/((c-d)^(d-e)), (a-b)*((d-c)^(e-d))] -> False

And, this is just finding the correct function to get the results I would like. Then there is the question of how to programmatically generate "something" that can be fed into that function to symbolically compare for equality. It would be easy to generate strings, but Equal[] (at least on Wolfram Alpha) doesn't seem to understand strings. ie, it fails to correctly interpret Equal["(a-b)+c","(c-b)+a"].

POSTED BY: W T
Posted 2 years ago

Mathematica has two functions for comparing expressions for "equal-ness". SameQ checks for structural identity. The two expressions must be identical as expressions for SameQ to return True (and otherwise it returns False). On the other hand, Equal checks for logical equality (for lack of a better term). It's easiest to explain with a counter-example. Let's say you have a variable, x that you have not defined a value for. Now try x == "abc". This won't resolve to True/False--it's considered an indeterminate case. However, x === "abc" is definitively False.

I'm pretty sure that Mathematica can handle your case, but I'd rather not spout off a lot of advice. It would be better to get more concrete examples from you first, or at least more specifics about what you're trying to accomplish.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard