Home > Archive > Mathematica > June 2005 > Complex Oddity
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| John Reed 2005-05-28, 8:58 am |
| I was trying to separate the real and imaginary parts of a complicated
expression, and ended up with something strange. Here is a short version of
what happened.
Let z = x + I y, then realPart = z /. {Complex[a_,b_]->a} gives realPart =
x. Great!
Now, try imagPart = z /. {Complex[a_,b_]->b} returns with imagPart = x + y.
Oops
In my original expression, it was harder to see, but the same error was
occuring. What I tried first was using Re[z] and Im[z], but then I have to
work with Im[y] and Im[x]. It seems to me two things need to be done here.
First, be able to assign variables so that they always stay real or else
indicate an error is occuring if they turn out to be complex, and second do
something to avoid errors like the above. I have to say that I don't trust
Mathematica's answers as much as I did before this came up. Now I feel like
I better have a good idea of what the answer is before I trust Mathematica.
John Reed
| |
| David Park 2005-05-29, 4:01 am |
| John,
Just look at the full form and you will see what happened.
z = x + I y;
z // FullForm
Plus[x, Times[Complex[0, 1], y]]
It is not Complex[x,y]!
Use the Re, Im and ComplexExpand function.
{Re[z], Im[z]} // ComplexExpand
{x, y}
When doing complex algebra, you will find ComplexExpand almost
indespensible. Check out its Help page carefully. The TargetFunction option
is often especially useful.
David Park
djmp@earthlink.net
http://home.earthlink.net/~djmp/
From: John Reed [mailto:nospamjreed@alum.mit.edu]
I was trying to separate the real and imaginary parts of a complicated
expression, and ended up with something strange. Here is a short version of
what happened.
Let z = x + I y, then realPart = z /. {Complex[a_,b_]->a} gives realPart =
x. Great!
Now, try imagPart = z /. {Complex[a_,b_]->b} returns with imagPart = x + y.
Oops
In my original expression, it was harder to see, but the same error was
occuring. What I tried first was using Re[z] and Im[z], but then I have to
work with Im[y] and Im[x]. It seems to me two things need to be done here.
First, be able to assign variables so that they always stay real or else
indicate an error is occuring if they turn out to be complex, and second do
something to avoid errors like the above. I have to say that I don't trust
Mathematica's answers as much as I did before this came up. Now I feel like
I better have a good idea of what the answer is before I trust Mathematica.
John Reed
| |
| Bob Hanlon 2005-05-29, 4:01 am |
| Whenever a replacement does not seem to work, look at the FullForm and you
will usually see why it works the way that it does.
(a+b*I)//FullForm
Plus[a,Times[Complex[0,1],b]]
To define a variable as real, use TagSet to define upvalues
x /: Re[x]=x;
x /: Im[x]=0;
y /: Re[y]=y;
y /: Im[y]=0;
z=x+y*I;
{Re[z],Im[z]}
{x,y}
or use Simplify with assumptions
z=a+b*I;
Simplify[{Re[z],Im[z]}, Element[{a,b}, Reals]]
{a,b}
Bob Hanlon
>
> From: "John Reed" <nospamjreed@alum.mit.edu>
> Date: 2005/05/28 Sat AM 05:39:32 EDT
> Subject: Complex Oddity
>
> I was trying to separate the real and imaginary parts of a complicated
> expression, and ended up with something strange. Here is a short version
of
> what happened.
>
> Let z = x + I y, then realPart = z /. {Complex[a_,b_]->a} gives realPart =
> x. Great!
>
> Now, try imagPart = z /. {Complex[a_,b_]->b} returns with imagPart = x +
y.
> Oops
>
> In my original expression, it was harder to see, but the same error was
> occuring. What I tried first was using Re[z] and Im[z], but then I have to
> work with Im[y] and Im[x]. It seems to me two things need to be done
here.
> First, be able to assign variables so that they always stay real or else
> indicate an error is occuring if they turn out to be complex, and second do
> something to avoid errors like the above. I have to say that I don't trust
> Mathematica's answers as much as I did before this came up. Now I feel like
> I better have a good idea of what the answer is before I trust Mathematica.
>
> John Reed
>
>
| |
| Carl K. Woll 2005-05-29, 4:02 am |
| John,
See below.
"John Reed" <nospamjreed@alum.mit.edu> wrote in message
news:d79enu$lbl$1@smc.vnet.net...
>I was trying to separate the real and imaginary parts of a complicated
> expression, and ended up with something strange. Here is a short version
> of
> what happened.
>
> Let z = x + I y, then realPart = z /. {Complex[a_,b_]->a} gives realPart =
> x. Great!
>
> Now, try imagPart = z /. {Complex[a_,b_]->b} returns with imagPart = x +
> y.
> Oops
>
Whenever you have unexpected output, it's usually a good idea to use
FullForm on your input to see if it matches what you expect.
In[4]:=
FullForm[x + I y]
Out[4]//FullForm=
Plus[x, Times[Complex[0, 1], y]]
Now, you can see the problem, and why your expected output doesn't occur.
Basically, you shouldn't think of Complex[a,b] as an expression with head
Complex and a and b as it's arguments. Rather, Complex[a,b] is only defined
when a and b are numbers, and even then it isn't an expression, its an atom.
> In my original expression, it was harder to see, but the same error was
> occuring. What I tried first was using Re[z] and Im[z], but then I have
> to
> work with Im[y] and Im[x]. It seems to me two things need to be done
> here.
> First, be able to assign variables so that they always stay real or else
> indicate an error is occuring if they turn out to be complex, and second
> do
> something to avoid errors like the above. I have to say that I don't
> trust
> Mathematica's answers as much as I did before this came up. Now I feel
> like
> I better have a good idea of what the answer is before I trust
> Mathematica.
>
The function you should look into is ComplexExpand. ComplexExpand assumes
that variables are real and performs simplifications that are possible in
that case.
In[8]:=
ComplexExpand[Re[x + I y]]
ComplexExpand[Im[x + I y]]
Out[8]=
x
Out[9]=
y
> John Reed
>
Carl Woll
| |
| Bill Rowe 2005-05-29, 4:02 am |
| On 5/28/05 at 5:39 AM, nospamjreed@alum.mit.edu (John Reed) wrote:
>I was trying to separate the real and imaginary parts of a
>complicated expression, and ended up with something strange. Here
>is a short version of what happened.
>Let z = x + I y, then realPart = z /. {Complex[a_,b_]->a} gives
>realPart = x. Great!
>Now, try imagPart = z /. {Complex[a_,b_]->b} returns with imagPart
>= x + y. Oops
>In my original expression, it was harder to see, but the same error
>was occuring.
This isn't an error. To see why consider
In[1]:=
z = x + I*y;
FullForm[z]
Out[2]//FullForm=FullForm[x + Complex[0, 1]*y]
In your first example, the rule Complex[a_,b_]->a replaces the expression Complex[0,1] with 0 and the entire expression becomes x + 0 y which of course is x.
In the second example, Complex[0,1] gets replaced by 1 and of course the expression becomes x + y.
--
To reply via email subtract one hundred and four
| |
| Chris Chiasson 2005-05-29, 4:02 am |
| Is the expression, z, atomic (AtomQ[z])? Otherwise, the replacement
code may not work.
Since you said the expression was complicated, I assume that it is not
atomic. You could
always numerically evaluate z and then use the replacement code.
The other (exact) alternative is Im[z] and Re[z], where you will have
to work with Im[x] and Im[y] because you probably have not told
Mathematica anything about x and y.
If you know that x and y are, for example, real, you can try:
{imagpart,realpart}=Refine[{Re@#,Im@#}&@z,{Element[x,Reals],Element[y,Reals]]
Disclaimer: I didn't test any of the above code.
Regards,
On 5/28/05, John Reed <nospamjreed@alum.mit.edu> wrote:
> I was trying to separate the real and imaginary parts of a complicated
> expression, and ended up with something strange. Here is a short version of
> what happened.
>
> Let z = x + I y, then realPart = z /. {Complex[a_,b_]->a} gives realPart =
> x. Great!
>
> Now, try imagPart = z /. {Complex[a_,b_]->b} returns with imagPart = x + y.
> Oops
>
> In my original expression, it was harder to see, but the same error was
> occuring. What I tried first was using Re[z] and Im[z], but then I have to
> work with Im[y] and Im[x]. It seems to me two things need to be done here.
> First, be able to assign variables so that they always stay real or else
> indicate an error is occuring if they turn out to be complex, and second do
> something to avoid errors like the above. I have to say that I don't trust
> Mathematica's answers as much as I did before this came up. Now I feel like
> I better have a good idea of what the answer is before I trust Mathematica.
>
> John Reed
>
>
--
Chris Chiasson
http://chrischiasson.com/
1 (810) 265-3161
| |
| Jean-Marc Gulliet 2005-05-30, 3:59 am |
| John Reed wrote:
> I was trying to separate the real and imaginary parts of a complicated
> expression, and ended up with something strange. Here is a short version of
> what happened.
>
> Let z = x + I y, then realPart = z /. {Complex[a_,b_]->a} gives realPart =
> x. Great!
>
> Now, try imagPart = z /. {Complex[a_,b_]->b} returns with imagPart = x + y.
> Oops
>
> In my original expression, it was harder to see, but the same error was
> occuring. What I tried first was using Re[z] and Im[z], but then I have to
> work with Im[y] and Im[x]. It seems to me two things need to be done here.
> First, be able to assign variables so that they always stay real or else
> indicate an error is occuring if they turn out to be complex, and second do
> something to avoid errors like the above. I have to say that I don't trust
> Mathematica's answers as much as I did before this came up. Now I feel like
> I better have a good idea of what the answer is before I trust Mathematica.
>
> John Reed
>
Hi John,
the expression z = a + I y may not be translated internally as you expect:
In[1]:= z = x + I y
Out[1]= x+\[ImaginaryI] y
In[2]:= FullForm[z]
Out[2]= Plus[x,Times[Complex[0,1],y]]
You could try
In[3]:= imagPart = z/.{a_+I b_\[Rule]b}
Out[3]= y
In[4]:= realPart=z/.{a_+I b_\[Rule]a}
Out[4]= x
Best regards,
/J.M.
| |
| John Reed 2005-05-31, 9:15 am |
| Thanks to all who explained what is happening and how to work this problem
correctly. Now I know one facet of working with complex numbers. I don't
feel much better about this however. I received one e-mail that said this
was my fault for not reading the documentation closely enough. This problem
came up in the book "Mathematica for Physics" second edition by Zimmerman
and Olness. They solve a problem using the Complex[a_,b_]->a rule, ( see
page 91) but not the b part. The b part was my idea. Now I know why they
didn't solve for the imaginary part this way. They get the imaginary part
by subtracting the real part from the complex expression and dividing by I.
How many other gotchas are hidden in the code, waiting to bite the unwary
and relatively new user? What documentation tells about this kind of a
problem or do I just have to find them for myself by hopefully catching the
errors as they occur?
John Reed
"John Reed" <nospamjreed@alum.mit.edu> wrote in message
news:d79enu$lbl$1@smc.vnet.net...
>I was trying to separate the real and imaginary parts of a complicated
> expression, and ended up with something strange. Here is a short version
> of
> what happened.
>
> Let z = x + I y, then realPart = z /. {Complex[a_,b_]->a} gives realPart =
> x. Great!
>
> Now, try imagPart = z /. {Complex[a_,b_]->b} returns with imagPart = x +
> y.
> Oops
>
> In my original expression, it was harder to see, but the same error was
> occuring. What I tried first was using Re[z] and Im[z], but then I have
> to
> work with Im[y] and Im[x]. It seems to me two things need to be done
> here.
> First, be able to assign variables so that they always stay real or else
> indicate an error is occuring if they turn out to be complex, and second
> do
> something to avoid errors like the above. I have to say that I don't
> trust
> Mathematica's answers as much as I did before this came up. Now I feel
> like
> I better have a good idea of what the answer is before I trust
> Mathematica.
>
> John Reed
>
>
| |
| Chris Chiasson 2005-06-01, 9:10 am |
| Sometimes the design of Mathematica is counter intuitive to a person
learning it. I have seen some things in Mathematica that I think are
bugs (despite the claim in the documentation that the probability of
me seeing a bug in Mathematica is low ... consider the source ... ).
Still, I always assume that the error lies first in my understanding
of Mathematica's operation than with Mathematica itself. This attitude
may be a good approach to apply to questions asked on this list
because sometimes people are defensive of the software they use every
day (and for which they provide unpaid technical support) ...
On 5/31/05, John Reed <nospamjreed@alum.mit.edu> wrote:
> Thanks to all who explained what is happening and how to work this problem
> correctly. Now I know one facet of working with complex numbers. I don't
> feel much better about this however. I received one e-mail that said this
> was my fault for not reading the documentation closely enough. This problem
> came up in the book "Mathematica for Physics" second edition by Zimmerman
> and Olness. They solve a problem using the Complex[a_,b_]->a rule, ( see
> page 91) but not the b part. The b part was my idea. Now I know why they
> didn't solve for the imaginary part this way. They get the imaginary part
> by subtracting the real part from the complex expression and dividing by I.
> How many other gotchas are hidden in the code, waiting to bite the unwary
> and relatively new user? What documentation tells about this kind of a
> problem or do I just have to find them for myself by hopefully catching the
> errors as they occur?
>
> John Reed
>
> "John Reed" <nospamjreed@alum.mit.edu> wrote in message
> news:d79enu$lbl$1@smc.vnet.net...
>
>
--
Chris Chiasson
http://chrischiasson.com/
1 (810) 265-3161
| |
| Bill Rowe 2005-06-01, 9:10 am |
| On 5/31/05 at 5:00 AM, nospamjreed@alum.mit.edu (John Reed) wrote:
>Thanks to all who explained what is happening and how to work this
>problem correctly. Now I know one facet of working with complex
>numbers. I don't feel much better about this however. I received
>one e-mail that said this was my fault for not reading the
>documentation closely enough. This problem came up in the book
>"Mathematica for Physics" second edition by Zimmerman and Olness.
>They solve a problem using the Complex[a_,b_]->a rule, ( see page
>91) but not the b part. The b part was my idea. Now I know why
>they didn't solve for the imaginary part this way. They get the
>imaginary part by subtracting the real part from the complex
>expression and dividing by I. How many other gotchas are hidden in
>the code, waiting to bite the unwary and relatively new user? What
>documentation tells about this kind of a problem or do I just have
>to find them for myself by hopefully catching the errors as they
>occur?
I don't think there is anyone who can really say how many other "gotchas" there are. Probably every experienced user has thier own list they have encountered. Also, even experienced users get surpised now and then.
Mathematica is a complex piece of software. And the existing documentation leaves quite a bit to be desired. The only way I know to deal with these "gotchas" is to understand why each occurs as you encounter it.
--
To reply via email subtract one hundred and four
| |
| jfeth@azlink.com 2005-06-02, 9:06 am |
| I use Jones matrices to evaluate optical circuits and always need to
find the intensity of two interfering electric fields. The intensity
(or brightness) of a field is found as a positive real value from a
complex field as the field times the complex conjugate of the field.
As you can see below, Mathematica gives yet another complex value when
one does this straightforward multiplication instead of the real value
that, I might add, is drilled into all students at the first glimpse of
root(-1). For several w s I struggled to use Mathematica in my
circuit evaluation until one inspired Saturday, after several pots of
coffee, dumb luck and iteration brought forth Intensity[expr_]:= below.
I don't know why it works, I don't know how it works, and I
don't know another way to do the job, but, even as ugly as it is, at
least it works and it works very quickly. Importantly, it also gives
me answers as cosines with arguments that are (real)sums and
differences of characteristic delays, misalignments, and phase
modulation terms.
f=E^(I*d)
In[1]:=
f*Conjugate[f]
Out[1]=
E^ (I*d - I*Conjugate[d])
In[2]:=
Intensity[expr_]:=
TrigReduce[ExpToTrig[expr*TrigToExp[Comp
lexExpand[Conjugate[ExpToTrig[expr]]]]]]
In[3]:=
Intensity[f]
Out[3]=
1
With this solution in hand, as an optical engineer, I now have the
luxury of wondering 1) exactly what mathematical elegance (or utility)
is gained by Mathematica's assumption that every variable is always
complex, and 2) why there is apparently no way in Mathematica to
globally define a variable as a real number (i.e., its own conjugate).
Regards,
John Feth
John Reed wrote:
> Thanks to all who explained what is happening and how to work this problem
> correctly. Now I know one facet of working with complex numbers. I don't
> feel much better about this however. I received one e-mail that said this
> was my fault for not reading the documentation closely enough. This problem
> came up in the book "Mathematica for Physics" second edition by Zimmerman
> and Olness. They solve a problem using the Complex[a_,b_]->a rule, ( see
> page 91) but not the b part. The b part was my idea. Now I know why they
> didn't solve for the imaginary part this way. They get the imaginary part
> by subtracting the real part from the complex expression and dividing by I.
> How many other gotchas are hidden in the code, waiting to bite the unwary
> and relatively new user? What documentation tells about this kind of a
> problem or do I just have to find them for myself by hopefully catching the
> errors as they occur?
>
> John Reed
>
| |
| Bob Hanlon 2005-06-03, 9:10 am |
| Clear[f];
f=E^(I*d);
Simplify[Abs[f],Element[d, Reals]]
1
Abs[f]//ComplexExpand
1
d/: Re[d]:=d;
d/:Im[d]:=0;
Abs[f]
1
Bob Hanlon
>
> From: jfeth@azlink.com
> Date: 2005/06/02 Thu AM 05:17:27 EDT
> Subject: Re: Complex Oddity
>
> I use Jones matrices to evaluate optical circuits and always need to
> find the intensity of two interfering electric fields. The intensity
> (or brightness) of a field is found as a positive real value from a
> complex field as the field times the complex conjugate of the field.
> As you can see below, Mathematica gives yet another complex value when
> one does this straightforward multiplication instead of the real value
> that, I might add, is drilled into all students at the first glimpse of
> root(-1). For several w s I struggled to use Mathematica in my
> circuit evaluation until one inspired Saturday, after several pots of
> coffee, dumb luck and iteration brought forth Intensity[expr_]:= below.
> I don't know why it works, I don't know how it works, and I
> don't know another way to do the job, but, even as ugly as it is, at
> least it works and it works very quickly. Importantly, it also gives
> me answers as cosines with arguments that are (real)sums and
> differences of characteristic delays, misalignments, and phase
> modulation terms.
>
> f=E^(I*d)
>
> In[1]:=
> f*Conjugate[f]
>
> Out[1]=
> E^ (I*d - I*Conjugate[d])
>
> In[2]:=
> Intensity[expr_]:=
>
TrigReduce[ExpToTrig[expr*TrigToExp[Comp
lexExpand[Conjugate[ExpToTrig[
expr]]]]]]
>
> In[3]:=
> Intensity[f]
>
> Out[3]=
> 1
>
> With this solution in hand, as an optical engineer, I now have the
> luxury of wondering 1) exactly what mathematical elegance (or utility)
> is gained by Mathematica's assumption that every variable is always
> complex, and 2) why there is apparently no way in Mathematica to
> globally define a variable as a real number (i.e., its own conjugate).
>
> Regards,
>
> John Feth
>
>
> John Reed wrote:
problem[color=darkred]
don't[color=darkred]
problem[color=darkred]
Zimmerman[color=darkred]
see[color=darkred]
they[color=darkred]
I.[color=darkred]
unwary[color=darkred]
the[color=darkred]
>
>
| |
| Carl K. Woll 2005-06-03, 9:10 am |
| <jfeth@azlink.com> wrote in message news:d7mk2i$c36$1@smc.vnet.net...
>I use Jones matrices to evaluate optical circuits and always need to
> find the intensity of two interfering electric fields. The intensity
> (or brightness) of a field is found as a positive real value from a
> complex field as the field times the complex conjugate of the field.
> As you can see below, Mathematica gives yet another complex value when
> one does this straightforward multiplication instead of the real value
> that, I might add, is drilled into all students at the first glimpse of
> root(-1). For several w s I struggled to use Mathematica in my
> circuit evaluation until one inspired Saturday, after several pots of
> coffee, dumb luck and iteration brought forth Intensity[expr_]:= below.
> I don't know why it works, I don't know how it works, and I
> don't know another way to do the job, but, even as ugly as it is, at
> least it works and it works very quickly. Importantly, it also gives
> me answers as cosines with arguments that are (real)sums and
> differences of characteristic delays, misalignments, and phase
> modulation terms.
>
> f=E^(I*d)
>
> In[1]:=
> f*Conjugate[f]
>
> Out[1]=
> E^ (I*d - I*Conjugate[d])
>
> In[2]:=
> Intensity[expr_]:=
> TrigReduce[ExpToTrig[expr*TrigToExp[Comp
lexExpand[Conjugate[ExpToTrig[expr]]]]]]
>
> In[3]:=
> Intensity[f]
>
> Out[3]=
> 1
>
Isn't ComplexExpand[f Conjugate[f]] much simpler?
ComplexExpand[f Conjugate[f]]
1
Another idea is to use Simplify with assumptions:
Simplify[f Conjugate[f], Element[d, Reals]]
1
Is there some expr where the above approaches worked poorly for you?
Carl Woll
> With this solution in hand, as an optical engineer, I now have the
> luxury of wondering 1) exactly what mathematical elegance (or utility)
> is gained by Mathematica's assumption that every variable is always
> complex, and 2) why there is apparently no way in Mathematica to
> globally define a variable as a real number (i.e., its own conjugate).
>
> Regards,
>
> John Feth
>
>
> John Reed wrote:
>
| |
| Sseziwa Mukasa 2005-06-03, 9:10 am |
|
On Jun 2, 2005, at 5:17 AM, jfeth@azlink.com wrote:
> f=E^(I*d)
>
> In[1]:=
> f*Conjugate[f]
>
> Out[1]=
> E^ (I*d - I*Conjugate[d])
>
> In[2]:=
> Intensity[expr_]:=
> TrigReduce[ExpToTrig[expr*TrigToExp[Comp
lexExpand[Conjugate
> [ExpToTrig[expr]]]]]]
>
> In[3]:=
> Intensity[f]
>
> Out[3]=
> 1
>
In Mathematica 5.0 I get
In[1]:=
ComplexExpand[E^(I d) Conjugate[E^(I d)]]
Out[1]=
1
In[4]:=
f = E^(I d);
ComplexExpand[f Conjugate[f]]
Out[5]=
1
Is the actual expression you're working with different?
> 1) exactly what mathematical elegance (or utility)
> is gained by Mathematica's assumption that every variable is always
> complex
Because that's the most general case and the most general case is
always mathematically appropriate while the special cases require the
user to identify that the case is special. Substituting values into
the general expression will result in the same value as the special
case anyway so there is no problem.
> 2) why there is apparently no way in Mathematica to
> globally define a variable as a real number (i.e., its own conjugate).
This actually comes up in the mailing list pretty frequently (http://
forums.wolfram.com/mathgroup/search/?q=define+variable
+real&restrict=MathGroup&x=0&y=0). Here is a pretty good answer
http://forums.wolfram.com/mathgroup.../msg00163.html.
Regards,
Ssezi
| |
| Pratik Desai 2005-06-03, 9:10 am |
| jfeth@azlink.com wrote:
>I use Jones matrices to evaluate optical circuits and always need to
>find the intensity of two interfering electric fields. The intensity
>(or brightness) of a field is found as a positive real value from a
>complex field as the field times the complex conjugate of the field.
>As you can see below, Mathematica gives yet another complex value when
>one does this straightforward multiplication instead of the real value
>that, I might add, is drilled into all students at the first glimpse of
> root(-1). For several w s I struggled to use Mathematica in my
>circuit evaluation until one inspired Saturday, after several pots of
>coffee, dumb luck and iteration brought forth Intensity[expr_]:= below.
> I don't know why it works, I don't know how it works, and I
>don't know another way to do the job, but, even as ugly as it is, at
>least it works and it works very quickly. Importantly, it also gives
>me answers as cosines with arguments that are (real)sums and
>differences of characteristic delays, misalignments, and phase
>modulation terms.
>
>f=E^(I*d)
>
>In[1]:=
>f*Conjugate[f]
>
>Out[1]=
> E^ (I*d - I*Conjugate[d])
>
>In[2]:=
>Intensity[expr_]:=
> TrigReduce[ExpToTrig[expr*TrigToExp[Comp
lexExpand[Conjugate[ExpToTrig[expr]]]]]]
>
>In[3]:=
>Intensity[f]
>
>Out[3]=
>1
>
>With this solution in hand, as an optical engineer, I now have the
>luxury of wondering 1) exactly what mathematical elegance (or utility)
>is gained by Mathematica's assumption that every variable is always
>complex, and 2) why there is apparently no way in Mathematica to
>globally define a variable as a real number (i.e., its own conjugate).
>
>Regards,
>
>John Feth
>
>
>John Reed wrote:
>
>
>
>
>
I have also struggled with the very same questions, I have found the
TagSet defination invaluable here is how I would deal with your problem
TagSet[d, Im[d], 0];
TagSet[d, Re[d], d];
TagSet[d, Conjugate[d], d];
f[d_] := E^(I*d)
f[d]*Conjugate[f[d]][color=darkred]
The fundamental question remains why does Mathematica treat all
variables as complex, well the simple answer may be WHY NOT!!. You can
always tell mathematica which variables are real/complex, by giving them
thier identity(shown above) which is the way it is supposed to work,
isn't it? :-)
Best Regards
Pratik Desai
--
Pratik Desai
Graduate Student
UMBC
Department of Mechanical Engineering
Phone: 410 455 8134
| |
| David Park 2005-06-03, 9:10 am |
| John,
Is your example illustrative of your typical case?
Why not just...
f = E^(I*d)
ComplexExpand[Abs[f]^2]
1
David Park
djmp@earthlink.net
http://home.earthlink.net/~djmp/
From: jfeth@azlink.com [mailto:jfeth@azlink.com]
I use Jones matrices to evaluate optical circuits and always need to
find the intensity of two interfering electric fields. The intensity
(or brightness) of a field is found as a positive real value from a
complex field as the field times the complex conjugate of the field.
As you can see below, Mathematica gives yet another complex value when
one does this straightforward multiplication instead of the real value
that, I might add, is drilled into all students at the first glimpse of
root(-1). For several w s I struggled to use Mathematica in my
circuit evaluation until one inspired Saturday, after several pots of
coffee, dumb luck and iteration brought forth Intensity[expr_]:= below.
I don't know why it works, I don't know how it works, and I
don't know another way to do the job, but, even as ugly as it is, at
least it works and it works very quickly. Importantly, it also gives
me answers as cosines with arguments that are (real)sums and
differences of characteristic delays, misalignments, and phase
modulation terms.
f=E^(I*d)
In[1]:=
f*Conjugate[f]
Out[1]=
E^ (I*d - I*Conjugate[d])
In[2]:=
Intensity[expr_]:=
TrigReduce[ExpToTrig[expr*TrigToExp[Comp
lexExpand[Conjugate[ExpToTrig[expr]]
]]]]
In[3]:=
Intensity[f]
Out[3]=
1
With this solution in hand, as an optical engineer, I now have the
luxury of wondering 1) exactly what mathematical elegance (or utility)
is gained by Mathematica's assumption that every variable is always
complex, and 2) why there is apparently no way in Mathematica to
globally define a variable as a real number (i.e., its own conjugate).
Regards,
John Feth
John Reed wrote:
> Thanks to all who explained what is happening and how to work this problem
> correctly. Now I know one facet of working with complex numbers. I don't
> feel much better about this however. I received one e-mail that said this
> was my fault for not reading the documentation closely enough. This
problem
> came up in the book "Mathematica for Physics" second edition by Zimmerman
> and Olness. They solve a problem using the Complex[a_,b_]->a rule, ( see
> page 91) but not the b part. The b part was my idea. Now I know why they
> didn't solve for the imaginary part this way. They get the imaginary part
> by subtracting the real part from the complex expression and dividing by
I.
> How many other gotchas are hidden in the code, waiting to bite the unwary
> and relatively new user? What documentation tells about this kind of a
> problem or do I just have to find them for myself by hopefully catching
the
> errors as they occur?
>
> John Reed
>
| |
| Chris Chiasson 2005-06-03, 9:10 am |
| f = E^(I*d)
Refine[f*Conjugate[f], Element[d, Reals]]
On 6/2/05, jfeth@azlink.com <jfeth@azlink.com> wrote:
> I use Jones matrices to evaluate optical circuits and always need to
> find the intensity of two interfering electric fields. The intensity
> (or brightness) of a field is found as a positive real value from a
> complex field as the field times the complex conjugate of the field.
> As you can see below, Mathematica gives yet another complex value when
> one does this straightforward multiplication instead of the real value
> that, I might add, is drilled into all students at the first glimpse of
> root(-1). For several w s I struggled to use Mathematica in my
> circuit evaluation until one inspired Saturday, after several pots of
> coffee, dumb luck and iteration brought forth Intensity[expr_]:= below.
> I don't know why it works, I don't know how it works, and I
> don't know another way to do the job, but, even as ugly as it is, at
> least it works and it works very quickly. Importantly, it also gives
> me answers as cosines with arguments that are (real)sums and
> differences of characteristic delays, misalignments, and phase
> modulation terms.
>
> f=E^(I*d)
>
> In[1]:=
> f*Conjugate[f]
>
> Out[1]=
> E^ (I*d - I*Conjugate[d])
>
> In[2]:=
> Intensity[expr_]:=
> TrigReduce[ExpToTrig[expr*TrigToExp[Comp
lexExpand[Conjugate[ExpToTrig[expr]]]]]]
>
> In[3]:=
> Intensity[f]
>
> Out[3]=
> 1
>
> With this solution in hand, as an optical engineer, I now have the
> luxury of wondering 1) exactly what mathematical elegance (or utility)
> is gained by Mathematica's assumption that every variable is always
> complex, and 2) why there is apparently no way in Mathematica to
> globally define a variable as a real number (i.e., its own conjugate).
>
> Regards,
>
> John Feth
>
>
> John Reed wrote:
>
>
--
Chris Chiasson
http://chrischiasson.com/
1 (810) 265-3161
|
|
|
|
|