       Re: Conditions on patterns in Flat functions

• To: mathgroup at smc.vnet.net
• Subject: [mg13394] Re: Conditions on patterns in Flat functions
• From: "Allan Hayes" <hay at haystack.demon.cc.uk>
• Date: Thu, 23 Jul 1998 03:32:38 -0400
• References: <6oup6q\$j1o@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Tobias Oed wrote in message <6oup6q\$j1o at smc.vnet.net>...
>Hi all, I have a problem with conditions on patterns in flat functins,
>here is an example:
>
>
>In:= 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:= 4 Cos[x]+4 I Sin[x]
>
>Out= 4 Cos[x] + 4 I Sin[x]
>
>In:= CosPlusISin[%]
>
>           I x
>Out= 4 E
>
>In:= test=4 Cos[x]+4 I Sin[x] + something
>
>Out= something + 4 Cos[x] + 4 I Sin[x]
>
>In:= CosPlusISin[test]
>
>Out= something + 4 Cos[x] + 4 I Sin[x]
>
>
>The solutions I found:
>
>In:= 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:= 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:= CosPlusISin1[test]
>
>            I x
>Out= 4 E    + something
>
>In:= CosPlusISin2[test]
>
>            I x
>Out= 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
>

Tobias,
I don't have the answers, but here are two more puzzling variants

In:=
CosPlusISin2[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:=
CosPlusISin2[4 Cos[x]+4 I Sin[x] + something]

Out=
4*E^(I*x) + something

Although:

In:=
ReplaceList[
4 Cos[x]+4 I Sin[x] + something,
{(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 }]

Out=
{}

-------------------------------------------------------------
Allan Hayes
Training and Consulting
Leicester UK
http://www.haystack.demon.co.uk
hay at haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44(0)116 271 8642

```

• Prev by Date: RE: Re: Re: Non-comm
• Next by Date: Re: Q: Combining NDSolve with FindRoot
• Previous by thread: Re: Conditions on patterns in Flat functions
• Next by thread: Help on (* comments *)