Conditions on patterns in Flat functions
- To: mathgroup at smc.vnet.net
- Subject: [mg13332] Conditions on patterns in Flat functions
- From: Tobias Oed <tobias at physics.odu.edu>
- Date: Mon, 20 Jul 1998 02:49:53 -0400
- Organization: Old Dominion University
- Sender: owner-wri-mathgroup at wolfram.com
Hi all, I have a problem with conditions on patterns in flat functins,
here is an example:
In[1]:= CosPlusISin[expr_]:= expr //. {
((a_. Cos[th_] + b_. Sin[th_] /; b === I a ) :> a E^(I
th)),
((a_. Cos[th_] + b_. Sin[th_] /; b === - I a ) :> a
E^(-I th))
}
In[2]:= 4 Cos[x]+4 I Sin[x]
Out[2]= 4 Cos[x] + 4 I Sin[x]
In[3]:= CosPlusISin[%]
I x
Out[3]= 4 E
In[4]:= test=4 Cos[x]+4 I Sin[x] + something
Out[4]= something + 4 Cos[x] + 4 I Sin[x]
In[5]:= CosPlusISin[test]
Out[5]= something + 4 Cos[x] + 4 I Sin[x]
The solutions I found:
In[10]:= CosPlusISin1[expr_]:= expr //. {
((a_. Cos[th_] + b_. Sin[th_] +c___ /; b === I a ) :> a
E^(I th)+c),
((a_. Cos[th_] + b_. Sin[th_] +c___ /; b === - I a ) :>
a E^(-I th)+c)
}
In[11]:= CosPlusISin2[expr_]:= expr //. {
((a_. Cos[th_] + b_. Sin[th_] +c_. /; b === I a ) :> a
E^(I th)+c),
((a_. Cos[th_] + b_. Sin[th_] +c_. /; b === - I a ) :> a
E^(-I th)+c)
}
In[12]:= CosPlusISin1[test]
I x
Out[12]= 4 E + something
In[13]:= CosPlusISin2[test]
I x
Out[13]= 4 E + something
The questions:
Which solution of the two is better, and why does the original idea not
work since Plus is Flat ?
Tobias