Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.


Noqsi
Posts:
51
Registered:
12/8/10


Re: Differencing two equations
Posted:
Feb 14, 2013 2:09 AM


On Sunday, February 10, 2013 1:35:06 AM UTC7, 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 TrueFalse 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[1]:= SetAttributes[equation, HoldAll]
Define behavior of addition rather than subtraction, since Minus[] won't survive in a "standard" Mathematica expression.
In[2]:= equation/: equation[x1_==x2_] +equation[y1_==y2_]:=With[{z1=x1+y1,z2=x2+y2},equation[z1==z2]]
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:
In[3]:= equation/: equation[x1_==x2_] +c_:=With[{z1=x1+c,z2=x2+c},equation[z1==z2]]
Also multiply both sides by the same expression:
In[4]:= equation/: equation[x1_==x2_] *c_:=With[{z1=x1*c,z2=x2*c},equation[z1==z2]]
Raising both sides to a power is also common:
In[5]:= equation/: equation[x1_==x2_]^p_:=With[{z1=x1^p,z2=x2^p},equation[z1==z2]]
And maybe we'll want to apply an arbitrary function to both sides:
In[6]:= equation/:f_[ equation[x1_==x2_]]:=With[{z1=f[x1],z2=f[x2]},equation[z1==z2]]
These definition combine to implement equation subtraction:
In[7]:= equation[a==r]equation[b==s] Out[7]= equation[ab==rs]
Also, division by a common expression:
In[8]:= equation[a==r]/a Out[8]= equation[1==r/a]
Operations that should commute, commute:
In[9]:= a+equation[a==r] Out[9]= equation[0==a+r]
And this approach exhibits the usual pitfalls of thoughtless algebra:
In[10]:= equation[2x==x]/x Out[10]= equation[2==1]
To get back to Equal[] as a predicate, strip the container:
In[11]:= Evaluate@@% Out[11]= False
As others have demonstrated, you can also treat Mathematica expressions as the data structures of a Lisplike 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.



