MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to remove the "0." from "0. + 1.41774i"


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.]

Step 4: clear $Post to return to normal Mathematica behaviour:

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
>
>
>
>
>




  • Prev by Date: Re: automatic grid
  • Next by Date: Re: How to remove the "0." from "0. + 1.41774i"
  • Previous by thread: Re: How to remove the "0." from "0. + 1.41774i"
  • Next by thread: Re: How to remove the "0." from "0. + 1.41774i"