Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #13436 > unrolled thread

Differencing two equations

Started byg.c.b.at.home@gmail.com
First post2013-02-10 08:35 +0000
Last post2013-09-08 06:56 +0000
Articles 8 — 8 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Differencing two equations g.c.b.at.home@gmail.com - 2013-02-10 08:35 +0000
    Re: Differencing two equations Ray Koopman <koopman@sfu.ca> - 2013-02-11 09:36 +0000
    Re: Differencing two equations G B <g.c.b.at.home@gmail.com> - 2013-02-12 08:23 +0000
    Re: Differencing two equations David Bailey <dave@removedbailey.co.uk> - 2013-02-12 08:23 +0000
    Re: Differencing two equations "djmpark" <djmpark@comcast.net> - 2013-02-13 09:47 +0000
    Re: Differencing two equations Best Gregory <g.c.b.at.home@gmail.com> - 2013-02-13 09:47 +0000
    Re: Differencing two equations John Doty <noqsiaerospace@gmail.com> - 2013-02-14 07:09 +0000
    Re: Differencing two equations Youngjoo Chung <ychung12@gmail.com> - 2013-09-08 06:56 +0000

#13436 — Differencing two equations

Fromg.c.b.at.home@gmail.com
Date2013-02-10 08:35 +0000
SubjectDifferencing two equations
Message-ID<kf7m3q$fgi$1@smc.vnet.net>
I'm brand new to Mathematica, so I apologize for the naive questions...

I'm trying to figure out how to difference two equations.  Basically if I have:
a==r
b==s

I'd like to get:
a-b == r-s

What I'm getting is more like (a==r) - (b==s).  I'm not sure how that's a useful result, but is there a function to do what I'm looking for?

A quick search of the archives seem to bring up ways of doing this from using transformation rules to swap heads to unlocking the Equals operator and hacking its behavior.  I'd like to avoid doing that kind of rewiring for a simple operation, and I'd like to keep the syntax clean.

The Core Language documentation makes a big point of how everything is basically a list with different heads.  In this case, what I'm trying to do would work if it were treated as a list ({a,b}-{r,s} returns {a-b,r-s}) but doesn't work under Equal.

Thanks for any suggestions.

[toc] | [next] | [standalone]


#13452

FromRay Koopman <koopman@sfu.ca>
Date2013-02-11 09:36 +0000
Message-ID<kfae2d$m4d$1@smc.vnet.net>
In reply to#13436
On Feb 10, 12:35 am, g.c.b.at.h...@gmail.com wrote:
> I'm brand new to Mathematica, so I apologize for the naive questions...
>
> I'm trying to figure out how to difference two equations.  Basically if I have:
> a==r
> b==s
>
> I'd like to get:
> a-b == r-s
>
> What I'm getting is more like (a==r) - (b==s).  I'm not sure how that's a useful result, but is there a function to do what I'm looking for?
>
> A quick search of the archives seem to bring up ways of doing this from using transformation rules to swap heads to unlocking the Equals operator and hacking its behavior.  I'd like to avoid doing that kind of rewiring for a simple operation, and I'd like to keep the syntax clean.
>
> The Core Language documentation makes a big point of how everything is basically a list with different heads.  In this case, what I'm trying to do would work if it were treated as a list ({a,b}-{r,s} returns {a-b,r-s}) but doesn't work under Equal.
>
> Thanks for any suggestions.

Inner[Subtract, a == r, b == s, Equal]

a - b == r - s

[toc] | [prev] | [next] | [standalone]


#13459

FromG B <g.c.b.at.home@gmail.com>
Date2013-02-12 08:23 +0000
Message-ID<kfcu5k$srh$1@smc.vnet.net>
In reply to#13436
Thanks for all of the suggestions.

It looks like this idiom is best for me:  Inner[Subtract, a == r, b ==
= s, Equal]

Aesthetically I slightly prefer the syntax of "Equal @@ Subtract @@@ {a == r, b == s}" but it doesn't work consistently on more complex expressions-- I sometimes wind up with a-s==r-b (which is correct, of course, but not what I wanted).

I may wind up with a variant of this in the end:
eqnOperate[eqn1_, eqn2_, f_] :=
  Equal @@ f @@ {List @@ eqn1, List @@ eqn2};

Maybe there's a mathematical reason why simple operations on equations aren't handled as I'd expect.  Mathematica is clearly a powerful tool, but the few times I've tried picking it up in the past I wind up getting stymied by the obtuse syntax for certain simple operations. 

In this case, I could probably use the power of the tool all at once by simply treating my set of equations as a unit and asking Mathematica to reason about them as a group and solve my problem directly.  The problem is that I, and my audience, could probably gain some insight into the problem by working through a few of the intermediate results.  By treating my equations lexically, rather than mathematically, I'm forgoing Mathematica's expertise and only allowing it to ensure I don't make transcription errors.



On Sunday, February 10, 2013 12:35:06 AM UTC-8, G B wrote:
> I'm brand new to Mathematica, so I apologize for the naive questions...
>
>
>
> I'm trying to figure out how to difference two equations.  Basically if I have:
>
> a==r
>
> b==s
>
>
>
> I'd like to get:
>
> a-b == r-s
>
>
>
> What I'm getting is more like (a==r) - (b==s).  I'm not sure how =
that's a useful result, but is there a function to do what I'm looking for?
>
>
>
> A quick search of the archives seem to bring up ways of doing this from using transformation rules to swap heads to unlocking the Equals operator and hacking its behavior.  I'd like to avoid doing that kind of rewiring for a simple operation, and I'd like to keep the syntax clean.
>
>
>
> The Core Language documentation makes a big point of how everything is basically a list with different heads.  In this case, what I'm trying to do would work if it were treated as a list ({a,b}-{r,s} returns {a-b,r-s}) but doesn't work under Equal.
>
>
>
> Thanks for any suggestions.

[toc] | [prev] | [next] | [standalone]


#13460

FromDavid Bailey <dave@removedbailey.co.uk>
Date2013-02-12 08:23 +0000
Message-ID<kfcu5p$srj$1@smc.vnet.net>
In reply to#13436
On 10/02/2013 08:35, g.c.b.at.home@gmail.com wrote:
> I'm brand new to Mathematica, so I apologize for the naive questions...
>
> I'm trying to figure out how to difference two equations.  Basically if I have:
> a==r
> b==s
>
> I'd like to get:
> a-b == r-s
>
> What I'm getting is more like (a==r) - (b==s).  I'm not sure how that's a useful result, but is there a function to do what I'm looking for?
>
> A quick search of the archives seem to bring up ways of doing this from using transformation rules to swap heads to unlocking the Equals operator and hacking its behavior.  I'd like to avoid doing that kind of rewiring for a simple operation, and I'd like to keep the syntax clean.
>
> The Core Language documentation makes a big point of how everything is basically a list with different heads.  In this case, what I'm trying to do would work if it were treated as a list ({a,b}-{r,s} returns {a-b,r-s}) but doesn't work under Equal.
>
> Thanks for any suggestions.
>

I would define a function thus:

equationSubtract[x1_ == y1_, x2_ == y2_] := (x1 - x2) == (y1 - y2);

(If you use it a lot, you might want to put it somewhere do it is auto 
loaded).

In[9]:= equationSubtract[a == r, b == s]

Out[9]= a - b == r - s

A slightly better version might have a third optional argument to supply 
a multiplier:

equationSubtract[x1_ == y1_, x2_ == y2_,
   m_: 1] := (x1 - m x2) == ( y1 - m y2);

In[5]:= equationSubtract[a == r, b == s, 2]

Out[5]= a - 2 b == r - 2 s

This function would still handle the 2-argument case.

In general, I don't believe in writing complicated syntax to do an 
individual algebraic manipulation (because if you get it wrong, you may 
not notice the wrong answer!). It is better to write a function once, 
test it, and use it when you need it - as I have done here.

David Bailey
http://www.dbaileyconsultancy.co.uk


[toc] | [prev] | [next] | [standalone]


#13467

From"djmpark" <djmpark@comcast.net>
Date2013-02-13 09:47 +0000
Message-ID<kffnfs$7jg$1@smc.vnet.net>
In reply to#13436
Sometimes I think Mathematica might better be called MetaMathematica, not so
much a tool for doing mathematics but a tool for making the tools to do
mathematics.

Quite often it will be very useful to "combine and repackage" Mathematica
procedures into forms that are more natural and convenient for your
application. Writing some definitions and functions along these lines can be
very useful. You don't have to limit yourself to the hooks and buttons that
are manifest in plain Mathematica.


David Park
djmpark@comcast.net 
http://home.comcast.net/~djmpark/index.html 



From: G B [mailto:g.c.b.at.home@gmail.com] 


Maybe there's a mathematical reason why simple operations on equations
aren't handled as I'd expect.  Mathematica is clearly a powerful tool, but
the few times I've tried picking it up in the past I wind up getting stymied
by the obtuse syntax for certain simple operations. 

In this case, I could probably use the power of the tool all at once by
simply treating my set of equations as a unit and asking Mathematica to
reason about them as a group and solve my problem directly.  The problem is
that I, and my audience, could probably gain some insight into the problem
by working through a few of the intermediate results.  By treating my
equations lexically, rather than mathematically, I'm forgoing Mathematica's
expertise and only allowing it to ensure I don't make transcription errors.



[toc] | [prev] | [next] | [standalone]


#13469

FromBest Gregory <g.c.b.at.home@gmail.com>
Date2013-02-13 09:47 +0000
Message-ID<kffngc$7jr$1@smc.vnet.net>
In reply to#13436
Thanks for this response that gives me a new and useful way to look at this.  It's amazing how a shift in perspective can help sometimes...


On Feb 12, 2013, at 10:14 AM, "djmpark" <djmpark@comcast.net> wrote:

> Sometimes I think Mathematica might better be called MetaMathematica, not so
> much a tool for doing mathematics but a tool for making the tools to do
> mathematics.
>
> Quite often it will be very useful to "combine and repackage" Mathematica
> procedures into forms that are more natural and convenient for your
> application. Writing some definitions and functions along these lines can be
> very useful. You don't have to limit yourself to the hooks and buttons that
> are manifest in plain Mathematica.
>
>
> David Park
> djmpark@comcast.net
> http://home.comcast.net/~djmpark/index.html
>
>
>
> From: G B [mailto:g.c.b.at.home@gmail.com]
>
>
> Maybe there's a mathematical reason why simple operations on equations
> aren't handled as I'd expect.  Mathematica is clearly a powerful tool, but
> the few times I've tried picking it up in the past I wind up getting stymied
> by the obtuse syntax for certain simple operations.
>
> In this case, I could probably use the power of the tool all at once by
> simply treating my set of equations as a unit and asking Mathematica to
> reason about them as a group and solve my problem directly.  The problem is
> that I, and my audience, could probably gain some insight into the problem
> by working through a few of the intermediate results.  By treating my
> equations lexically, rather than mathematically, I'm forgoing Mathematica's
> expertise and only allowing it to ensure I don't make transcription errors.
>
>
>

[toc] | [prev] | [next] | [standalone]


#13487

FromJohn Doty <noqsiaerospace@gmail.com>
Date2013-02-14 07:09 +0000
Message-ID<kfi2it$d18$1@smc.vnet.net>
In reply to#13436
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.

[toc] | [prev] | [next] | [standalone]


#15387

FromYoungjoo Chung <ychung12@gmail.com>
Date2013-09-08 06:56 +0000
Message-ID<l0h73j$7ip$1@smc.vnet.net>
In reply to#13436
Hi,

You can use SCSubtEqs in the SymbolicComputing package (http://symbcomp.gist.ac.kr, WTC 2011). It is still a beta version, but it might help.

<<SymbolicComputing`

In[199]:= SCSubtEqs[{a == r, b == s}]

Out[199]= a - b == r - s

You can use other functions like SCAddEqs, SCMultEqs, SCDivEqs for similar operations.

The package was designed to handle primarily manipulation of mathematical expressions rather than evaluation of them. This required some changes, though, like deferred on-demand evaluation of 2-D forms of integrals, products, sums, etc., which are normally evaluated immediately in Mathematica. Please refer to the homepage for further details.

Sincerely,
Youngjoo Chung

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web