Message Boards Message Boards

0
|
4743 Views
|
8 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How to check expressions for symbolic equality?

Posted 1 year 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 1 year 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 1 year ago

You're welcome! Glad to help!

POSTED BY: Eric Rimbey
Posted 1 year ago

POSTED BY: Eric Rimbey
Posted 1 year 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 1 year 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 1 year ago
POSTED BY: Eric Rimbey
Posted 1 year ago
POSTED BY: W T
Posted 1 year ago
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

Group Abstract Group Abstract