Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How to remove the "0." from "0. + 1.41774i"
Replies: 9   Last Post: Jun 22, 2013 8:41 PM

 Messages: [ Previous | Next ]
 Christoph Lhotka Posts: 41 Registered: 3/2/10
Re: How to remove the "0." from "0. + 1.41774i"
Posted: Jun 22, 2013 8:40 PM

Dear mathgroup,

there is indeed a very elegant solution to these kind of problems. It took me some while (looking up the documentation center) but I think this works fine:

Step 1: define your notation (here on the output format of complex numbers):

In[1]:= format[c_Complex] :=
Interpretation[
Which[
Re[c] == 0 && Im[c] == 0, "0",
Re[c] == 0 && Im[c] != 0, ToString[Im[c] // N] <> "\[ImaginaryI]",
Re[c] != 0 && Im[c] == 0, ToString[Re[c] // N],
True, ToString[c // N, StandardForm]], c]

Step 2: Tell Mathematica to apply this functions to any output expression

In[2]:= \$Post = # /. c_Complex :> format[c] &

Out[2]= #1 /. c_Complex :> format[c] &

Step 3: Work as usual, you will always see the format you defined in
Step 1....

In[4]:= x = {0. + I, 0 + 1. I, 1 + 0. I, 1. + 2. I}

Out[4]= {1.I,1.I,1.,1. +2. I}

...but you will always be able to continue your calculations independent
of the format you
may define:

In[5]:= Total[x]

Out[5]= Complex[2., 4.]

In[6]:= Clear[\$Post]

Hope, that helps,

Christoph

PS: Here is a nice joke you can implement in your friends Mathematica
version:

In[1]:= format[c_Complex]:=
Interpretation[
Im[c]+Re[c]I,c]

In[2]:= \$Post=#/.c_Complex:>format[c]&
Out[2]= #1/. c_Complex:>format[c]&

In[3]:= x={0.+I,0+1.I,1+0.I,1.+2.I}
Out[3]= {1. +0. I,1. +0. I,0. +1. I,2. +1. I}

In[4]:= y=Total[x]
Out[4]= 4. +2. I

Do not forget to release him! With

In[5]:= Clear[\$Post]

In[6]:= x

Out[6]= {0. +1. I,0. +1. I,1. +0. I,1. +2. I}

In[7]:= y
Out[7]= 2. +4. I

...everything turns out to be fine ;o)

On 06/22/2013 09:33 AM, Richard Fateman wrote:
> 1. Mathematica 7.0 performs differently.
>
> 4.0*I looks like 4.I
>
> Mathematica 9. does this..
> 4.0*I looks like
> 0.+4.I
>
> 2. If you want to do something different, consider something
> like this:
>
> f[x_Complex]: If [VerySmall[Re[x]], Im[x]*i, Re[x]+Im[x]*i]
>
> where VerySmall is defined as you wish. Could be VerySmall(x_]:=x==0.0
> for example.
>
> Note that the answers are no longer complex numbers because they involve
> the symbol i not I.
>
> You can do %/. i->I
> but then you will see 0.+4.I again.
>
> RJF
>
>
>
>
>

Date Subject Author
6/21/13 Sseziwa Mukasa
6/22/13 Richard Fateman
6/22/13 Christoph Lhotka
6/22/13 Richard Fateman
6/22/13 Christoph Lhotka