Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1998
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Conditions on patterns in Flat functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13401] Re: [mg13332] Conditions on patterns in Flat functions
  • From: David Withoff <withoff>
  • Date: Thu, 23 Jul 1998 03:32:44 -0400
  • 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

The original idea didn't work because the left-hand side of the rule

  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))
         }

is a Condition pattern rather than an expression with a head of Plus. 
One way to address that concern is to put the condition on the
right-hand side of the rule.

In[1]:= CosPlusISin[expr_]:= expr //. {
                (a_. Cos[th_] + b_. Sin[th_]) :> a E^(I th) /; b === I
a,
                (a_. Cos[th_] + b_. Sin[th_]) :> a E^(-I th) /; b === -I
a
               }

In[2]:= CosPlusISin[something + 4 Cos[x] + 4 I Sin[x]]

           I x
Out[2]= 4 E    + something

There is also a built-in function that does this:

In[3]:= TrigToExp[something + 4 Cos[x] + 4 I Sin[x]]

           I x
Out[3]= 4 E    + something

Dave Withoff
Wolfram Research


  • Prev by Date: Erroneous ImageSize?
  • Next by Date: Re: Is this a bug?
  • Previous by thread: Conditions on patterns in Flat functions
  • Next by thread: Re: Conditions on patterns in Flat functions