On Sunday, February 10, 2013 1:35:06 AM UTC-7, G B wrote:
> I'm trying to figure out how to difference two equations. Basically if I have: > > a==r > > b==s
Equal is essentially a predicate in Mathematica, potentially yielding True or False when evaluated. Using ordinary arithmetic on Equal expressions is therefore strange: what should True-False mean?
But Mathematica makes it easy to define new objects. One way to make equations that can be manipulated the way one does with pencil and paper is to define a wrapper for Equal that prevents evaluation and enables the other things you need. Call it "equation".
First, prevent evaluation of the ecapsulated equation:
In:= SetAttributes[equation, HoldAll]
Define behavior of addition rather than subtraction, since Minus won't survive in a "standard" Mathematica expression.
What's going on here? The pattern to the left of ":=" will recognize and deconstruct expressions like "equation[a==r]+equation[b==s]". We associate the pattern with our container, "equation", using "/:". Then, to the right, With evaluates the left hand and right hand sides of the resulting equation, and constructs a new encapsulated equation.
Another thing we may want is to add the same expression to both sides of the equation:
To get back to Equal as a predicate, strip the container:
In:= Evaluate@@% Out= False
As others have demonstrated, you can also treat Mathematica expressions as the data structures of a Lisp-like functional programming language. But I think that if the expressions represent mathematical formulae, it is better to use the pattern/replacement paradigm that is fundamental to Mathematica.