[Date Index]
[Thread Index]
[Author Index]
Re: Differencing two equations
*To*: mathgroup at smc.vnet.net
*Subject*: [mg129795] Re: Differencing two equations
*From*: John Doty <noqsiaerospace at gmail.com>
*Date*: Thu, 14 Feb 2013 02:10:18 -0500 (EST)
*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com
*Delivered-to*: l-mathgroup@wolfram.com
*Delivered-to*: mathgroup-newout@smc.vnet.net
*Delivered-to*: mathgroup-newsend@smc.vnet.net
*References*: <kf7m3q$fgi$1@smc.vnet.net>
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[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[a-b==r-s]
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 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.
Prev by Date:
**Autocomplete popup with Mathematica 9.0.1 on Xubuntu**
Next by Date:
**Re: Stephen Wolfram's recent blog**
Previous by thread:
**Re: Differencing two equations**
Next by thread:
**Mathematica and System Modeler**
| |