Re: A way around the limitations of Re[] and Im[]
- To: mathgroup at smc.vnet.net
- Subject: [mg51103] Re: [mg51086] A way around the limitations of Re[] and Im[]
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 5 Oct 2004 04:36:46 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 4 Oct 2004, at 19:17, Carlos Felippa wrote: > *This message was transferred with a trial version of CommuniGate(tm) > Pro* > As you know, Re[expr] and Im[expr] are left unevaluated when expr is > not numeric. I had good luck in simple cases with the following > substitution trick: > > Real[c_]:=c/.I->0; Imag[c_]:=(c-Real[c])/.I->1; > > Do you see any problem with these definitions? > > First of all, you can't use Real as it is already used and has the Attribute Protected. And secondly, this won't work in cases of expressions involving any numerical complex numbers. And secondly, I assume that you are intending to use it when c is some symbolic expression in which all symbols except I are taken to be real? (Which, by the way, is exactlyw hat ComplexExpand is for). Well, let's try your approach on a very simple algebraic computation. real[c_] := c /. I -> 0; imag[c_] := (c - real[c]) /. I -> 1; Consider a complex number: c = a + b*I; Let's find the real part: real[c] a O.K. So far so good. Let's try something else, like c^2: real[c^2] a^2 Not so good, I am afraid. The right answer is: ComplexExpand[Re[c^2]] a^2 - b^2 Andrzej Kozlowski Chiba, Japan http://www.akikoz.net/~andrzej/ http://www.mimuw.edu.pl/~akoz/