Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   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
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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







Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.