I would like to visually compare two expressions. However, the Diff function does not work with expressions
When I use Diff[expr1, expr2] in Mathematica, it returns an unevaluated expression and print error about heads.
When I use Diff[{expr1}, {expr2}], it displays the entire expressions rather than highlighting the specific sub-expressions that differ
Is this the expected behavior? imho proper behaviour - it should highlight all differences in red except for the term (-1+b^n)^2 within denominator, the expression (-1+b^n-b^(2n)) inside the parentheses in within numerator, and the term (-1+b)b in the numerator for b exponent
If you would post your code as actual code-text using the formatting tool, then we could copy-paste it to play around with it rather than laboriously type out your expressions.
But to answer the question “is this the expected behavior?”, I think the answer is pretty clearly “yes”. The message explicitly tells you that Diff can’t handle and expression with head Times, and the documentation for Diff clearly specifies the kinds of expressions it does work with.
I’m not familiar with ExpressionDifference, but you might be able to look at its implementation and modify it for your purposes.
As for suggestions, I think you need to first specify your requirements very clearly. So, do you want Abs[Sin[a+b]] to differ from Sin[a+b] in just the insertion of a head, or do you want to recognize the whole expression as different?
Once you have some test cases with expected outputs that clarify your expectation, you might try some of these ideas:
Just use ToString on your expressions and Diff the strings.
Similarly, use Level (using All) on the expressions and Diff against the resulting lists.
Use Level with more specific level-specs on each expression and then decide how to compare the results. Maybe compare level by level in parallel, or maybe find a level to pin to by comparing Depth.
Since Diff works with lists, walk through each expression and compare heads at each level and then replace the heads with List and use Diff on those lists.
I’m not really sure what using trees would buy you, since it seems like you’ll still need to walk through the trees just like walking through the expressions directly, but maybe if you play with ExpressionTree’s options you’ll find something that makes the comparison easier.