Message Boards Message Boards

0
|
4688 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Make a simple rule to combine a function xp of plus & minus args?

Posted 9 years ago

Here's a simple function Xpee. I want a rule that combines "Xpee[aa] (+/-) Xpee[-aa]" to give ZipIt[aa]. For example Xpee[-1/5] - Xpee[1/5] yields ZipIt[1/5]. I've used MatchQ to test various parts of the rule. I can MatchQ parts such as Xpee[aa_]. But I've failed with various combination rules such as

XpRule:= Xpee[aa\_] - Xpee[-aa\_] -> ZipIt[aa]

And I've used FullForm to get to this point but I can't quite get to the point of a workable rule to combine -Xpee[-aa] + Xpee[aa] to give ZipIt[aa]. The Times gets in the way. What am I missing to come up with a workable rule to combine Xpee[aa] (+/-) Xpee[-aa]?

POSTED BY: Anthony DeGance
3 Replies

Hi Anthony,

I am just playing around: If you actually want your problem solved by a rule then Bills solution is perfect! My remark here is that the same can be accomplished by proper definition of your function Xpee:

(* definition for symbolic arguments: *)
Xpee /: Xpee[a_] + Xpee[-a_] := Ziplt[a]
Xpee /: Xpee[a_] - Xpee[-a_] := Ziplt[a]
Xpee /: -Xpee[a_] + Xpee[-a_] := Ziplt[-a]
(* definition for numeric arguments: *)
Xpee /: Xpee[a_?Positive] + Xpee[b_?Negative] := Ziplt[a] /; a == -b
Xpee /: Xpee[a_?Positive] - Xpee[b_?Negative] := Ziplt[a] /; a == -b
Xpee /: -Xpee[a_?Positive] + Xpee[b_?Negative] := Ziplt[-a] /; a == -b

(This should be checked carefully!)

Note: What I get here is e.g.:

Xpee[-2] - Xpee[2]
Out :   Ziplt[-2]

which is in disagreement with Bills outcome! You write:

... a rule that combines "Xpee[aa] (+/-) Xpee[-aa]" to give ZipIt[aa]. For example Xpee[-1/5] - Xpee[1/5] yields ZipIt[1/5].

Isn't this a contradiction, i.e. shouldn't this give Xpee[-1/5] or -Xpee[1/5]?

Regards -- Henrik

POSTED BY: Henrik Schachner

Bill,

I appreciate the help. Thank you. The rule works great. I tried something like your rule above but I was stumped because I didn't understand the difference between '=' and '==' or really where to place the condition. Again thank you for your help.

Tony DeGance

POSTED BY: Anthony DeGance
Posted 9 years ago

Test this endlessly before you trust it

In[1]:= xprule = {Xpee[a_] + Xpee[b_] | Xpee[a_] - Xpee[b_] /; b == -a :> ZipIt[b]};
{Xpee[2] + Xpee[-2], Xpee[-2] + Xpee[2], Xpee[-x] + Xpee[x], 
 Xpee[2] - Xpee[-2], Xpee[-2] - Xpee[2], Xpee[-x] - Xpee[x], 
 Xpee[-x] + Xpee[y], Xpee[-x] - Xpee[y]} /. xprule

Out[2]= {ZipIt[2], ZipIt[2], ZipIt[x],
ZipIt[-2], ZipIt[2], ZipIt[x], 
Xpee[-x] + Xpee[y], Xpee[-x] - Xpee[y]}
POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract