Re: Pattern Matching Bug ??

*To*: mathgroup at yoda.physics.unc.edu*Subject*: Re: Pattern Matching Bug ??*From*: tgayley (Todd Gayley)*Date*: Fri, 19 Feb 1993 09:17:26 -0600

Markus Ruppel <m.ruppel at imperial.ac.uk> writes: >I tried to replace expressions of the type > E^(I x) >with the more convenient (for me at least) > Cos[x] + I Sin[x] >using the rule > /. E^(I x_) -> Cos[x] + I Sin[x] > >This works fine until one has an expression like > E ^(-I x) > >In this case, the above replacement rule doesn't match anymore. I assume >this is due to the internal representation of I (which is Complex[0,1] for >I and Complex[0,-1] for -I). >I consider this behavior a bug. Any comments ? > >[It was suggested that he try the Trigonometry.m package -sma] > >>You should look at the Algebra/Trigonometry.m package >>... > >I am aware of this package and the ComplexToTrig function (Should have >mentioned it, sorry). May be I did not make my point clear. If you write -I >this is mathematically (-1) * I. So the pattern matching should replace I >appropriately as it does do for e.g. -x. >The situation is even a bit worse: > >Enter: > E^(I (-x)) // FullForm >and you get: > Power[E, Times[Complex[0, -1], x]] > >i.e. it pulls the -1 into the internal definition of I and makes I >therefore unacessible to pattern matching as soon as you have a (-1) >somewhere in your exponent. This happens before patterns are tested for: > E^(I (-x)) /. I->newI >gives you > E^(-I x) >but it works for other symbols just fine: > E^(y (-x)) /. y->newy >results in > E^-(newy x) >as expected. > >-I should be rather represented as Times[-1,Complex[0,1]] >internally as it is done for -x (Times[-1,x]). > >Markus Ruppel Complex numbers are represented as Complex[a,b], period. -I is a _number_, so why would we want its head to be Times? Should -3 be represented as Times[-1,3]? x is a symbol, so -x can only be represented as Times[-1,x]. I hardly see how this could be considered a bug. The constant I is useful, but it's simply not intended to be used for pattern-matching. Instead use the head Complex, as in E^(-I x) /. E^(Complex[0,y_] x_.) -> Cos[x y] + I Sin[x y] or the more complete rule in ComplexToTrig. --Todd