Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

591 views
Skip to first unread message

Mike Bayville

unread,
Jun 20, 2013, 4:01:33 AM6/20/13
to
Can anyone suggest a built-in function for removing the "0." from "0. + 1.41774i"?

Thanks!

Mike

INPUT
A={{-1.2, 1.5}, {-2.3, 1.2}};
Clear[eigenvalue];
{eigenvalue[1], eigenvalue[2]} = Chop[Eigenvalues[A]]
Chop[eigenvalue[1]]
Chop[eigenvalue[2]]

OUTPUT
{0. + 1.41774i, 0. - 1.41774i}
0. + 1.41774i
0. - 1.41774i

Bob Hanlon

unread,
Jun 21, 2013, 5:41:31 AM6/21/13
to

A pure imaginary number is represented as a complex number. If it is
inexact, it will have 0. as the real part.


I//FullForm


Complex[0,1]


I//N


0. + 1.*I


To suppress display of the zero real part, the number needs to be exact
(e.g., rationals).


A={{-1.2,1.5},{-2.3,1.2}};


eigenvalues=Eigenvalues[A]//Rationalize[#,10^-16]&


{(116833629*I)/82408088,

-((116833629*I)/82408088)}


eigenvalues//FullForm


List[Complex[0,Rational[116833629,82408088]],Complex[0,Rational[-116833629,82408088]]]


eigenvalues//N


{0. + 1.4177446878757822*I,

0. - 1.4177446878757822*I}


Alternatively, use exact numbers throughout


A=Rationalize[A]


{{-(6/5), 3/2}, {-(23/10), 6/5}}


eigenvalues=Eigenvalues[A]


{(I*Sqrt[201])/10,

-((I*Sqrt[201])/10)}


eigenvalues//N


{0. + 1.4177446878757827*I,

0. - 1.4177446878757827*I}



Bob Hanlon

Harvey P. Dale

unread,
Jun 21, 2013, 5:42:32 AM6/21/13
to
Im[0.+1.41774I] gives 1.41774.
Best,
Harvey

cj.g...@gmail.com

unread,
Jun 21, 2013, 5:43:53 AM6/21/13
to
Try this

ToExpression[StringReplace[ToString[0. + 1.41774 i], "0." -> "" ]]

Effective but not elegant

Bill Rowe

unread,
Jun 21, 2013, 5:44:34 AM6/21/13
to
On 6/20/13 at 4:44 AM, mike.b...@gmail.com (Mike Bayville) wrote:

>Can anyone suggest a built-in function for removing the "0." from
>"0. + 1.41774i"?

Chop. That is:

In[1]:= Chop[0. + 1.41774 i]

Out[1]= 1.41774 i



Mike Bayville

unread,
Jun 21, 2013, 5:44:54 AM6/21/13
to

Thanks, but Rationalize works in the manner you executed it; however, it
does not work like this...

In[216]:= nuChop[t_] = Rationalize[t];
A = {{-1.2, 1.5}, {-2.3, 1.2}};
eigval1 = Eigenvalues[A][[1]]
nuChop[eigval1]

Out[218]= 2.7755576*10^-17 + 1.4177447 I

Out[219]= 2.7755576*10^-17 + 1.4177447 I


On Thu, Jun 20, 2013 at 7:54 AM, Sseziwa Mukasa <muk...@gmail.com> wrote:

> (Debug) In[3]:= Rationalize[0. + 1.41774 I]
> (Debug) Out[3]= (70887 I)/50000
>
> That has it's own problems with respect to large values in the numerator
> or denominator.
>
> On Jun 20, 2013, at 4:44 AM, Mike Bayville <mike.b...@gmail.com>
> wrote:
>
> > Can anyone suggest a built-in function for removing the "0." from "0. +
> 1.41774i"?
> >

Sseziwa Mukasa

unread,
Jun 21, 2013, 5:45:35 AM6/21/13
to

It's a precision issue, the documentation on rationalize says:

Rationalize[x] yields x unchanged if there is no rational number close enough to x to satisfy the condition =E2=88=AA p/q - x =E2=88=AA < c/q^2, with c chosen to be 10^-4.
Rationalize[x, 0] converts any x to rational form.

So:

(Debug) In[17]:= nuChop[t_] := Rationalize[Chop[t], 0];
A = {{-1.2, 1.5}, {-2.3, 1.2}};
eigval1 = Eigenvalues[A][[1]]
nuChop[eigval1]
(Debug) Out[19]= 2.77556*10^-17 + 1.41774 I
(Debug) Out[20]= (116833629 I)/82408088

But I don't find (116833629 I)/82408088 more visually useful than 0. + 1.41774 I.

However here's another approach:

(Debug) In[32]:= suppressMachinePrecisionZeros[Complex[a_, b_]] :=
SetPrecision[Chop[a], $MachinePrecision] +
SetPrecision[Chop[b], $MachinePrecision] I
(Debug) In[33]:= suppressMachinePrecisionZeros /@ Eigenvalues[A]
(Debug) Out[33]= {1.417744687875782 I, -1.417744687875782 I}

Nasser M. Abbasi

unread,
Jun 21, 2013, 5:42:12 AM6/21/13
to
Seems related to the thread "Multiplication by 0" 2 days ago?

I do not it is allowed to have a number with exact real part
and machine reals imaginary part. The complex system has to have
both parts either exact or both parts be machine reals. Can't mix
and match.

So you can't have

0 + 2. I

But you can have

0 + 2 I ===> which is 2I

and can have this

0. + 2. I

btw, I tested this on that other system, that starts its name
by "M" and ends with "E" and it behaves the same way:

-------------------------------
A:=Matrix([[-1.2, 1.5], [-2.3, 1.2]]):
a:=eig(A):
a[1];
-17
2.77555756156289 10 + 1.41774468787578 I

fnormal(%);
0. + 1.417744688 I
-----------------------------------

see:

In[33]:= 2 + 2. I
Out[33]= 2. + 2. I

Why is it important for you to remove the 0. from the display?
You can use Im and Re to obtain the real and imaginary parts of
complex number any time.

--Nasser




Mike Bayville

unread,
Jun 21, 2013, 5:43:13 AM6/21/13
to
I have some to realize that I failed to describe the context of my request.

I am hope to identify or create a function, e.g., mikechop[t_], such that the argument could be a real or complex number, and if it is a complex number such as:

0. + 1.41774i

or

1.2313 x 10^(-16) + 1.41774i

the output will be:

1.41774i

otherwise it will be the real or complex number that was input.

This is for inclusion in code that I write.

Sorry for not properly describing the end use.

Humbly,

Mike





On Thursday, June 20, 2013 4:01:33 AM UTC-4, Mike Bayville wrote:

Murray Eisenberg

unread,
Jun 22, 2013, 3:27:19 AM6/22/13
to
No, Chop won't do that -- assuming you use the proper symbol I (or its equivalent Esc ii Esc):

Chop[0. + 1.41774 I]
0. + 1.41774 I

On Jun 21, 2013, at 6:00 AM, Bill Rowe <read...@sbcglobal.net> wrote:

> On 6/20/13 at 4:44 AM, mike.b...@gmail.com (Mike Bayville) wrote:
>
>> Can anyone suggest a built-in function for removing the "0." from
>> "0. + 1.41774i"?
>
> Chop. That is:
>
> In[1]:= Chop[0. + 1.41774 i]
>
> Out[1]= 1.41774 i
>
>

---
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2838 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305






Richard Fateman

unread,
Jun 22, 2013, 3:29:21 AM6/22/13
to
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





Sseziwa Mukasa

unread,
Jun 22, 2013, 3:26:18 AM6/22/13
to
(Debug) In[3]:= Rationalize[0. + 1.41774 I]
(Debug) Out[3]= (70887 I)/50000

That has it's own problems with respect to large values in the numerator or denominator.

On Jun 20, 2013, at 4:44 AM, Mike Bayville <mike.b...@gmail.com> wrote:

> Can anyone suggest a built-in function for removing the "0." from "0. + 1.41774i"?
>

Murray Eisenberg

unread,
Jun 22, 2013, 3:27:39 AM6/22/13
to
Unless you'd be satisfied to have just a display form with no "0." included, e.g., as a string, I don't see how you can do what you ask. You might be tempted to try something like:

imchop[z_] :=
Which[z \[Element] Reals, z,
Head[z] == Complex && Re[z] != 0, z,
Head[z] == Complex && Re[z] == 0, Im[z] I]

But it won't handle the case at issue, e.g.:

imchop[0. + 1.41774 I]
0. + 1.41774 I

And the reason is that after extracting the imaginary part 1.41774 and multiplying by I, you have a number whose FullForm is

Complex[0.`, 1.417741]

and hence will display as the very same 0. + 1.41774 I.


> I have some to realize that I failed to describe the context of my request.
>
> I am hope to identify or create a function, e.g., mikechop[t_], such that the argument could be a real or complex number, and if it is a complex number such as:
>
> 0. + 1.41774i
>
> or
>
> 1.2313 x 10^(-16) + 1.41774i
>
> the output will be:
>
> 1.41774i
>
> otherwise it will be the real or complex number that was input.
>
> This is for inclusion in code that I write.
>
> Sorry for not properly describing the end use.
>
> Humbly,
>
> Mike
>
>
>
>
>
> On Thursday, June 20, 2013 4:01:33 AM UTC-4, Mike Bayville wrote:

Christoph Lhotka

unread,
Jun 22, 2013, 8:40:20 PM6/22/13
to
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)

Richard Fateman

unread,
Jun 22, 2013, 8:40:41 PM6/22/13
to
On 6/22/2013 8:40 AM, Christoph Lhotka wrote:
> 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:
Well, there are lots of little problems with this solution, some of
which have to do
with the semantics of N, so that 1/2+2I looks like floats, and
2^5000+2I looks like
1.4124...x10^1505 + 0. I. with a red box around the 0.

but also there is an issue that FullForm[%] is peculiar.

So the idea of changing the display format in $Post processing is a possibility, it rapidly becomes less elegant if you expect it to cover every eventuality, and
probably not possible if you want FullForm to work.

RJF




Christoph Lhotka

unread,
Jun 22, 2013, 8:41:41 PM6/22/13
to
Hello,

On 06/22/2013 10:21 PM, Richard Fateman wrote:
> On 6/22/2013 8:40 AM, Christoph Lhotka wrote:
>> 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:
> Well, there are lots of little problems with this solution, some of
> which have to do
> with the semantics of N, so that 1/2+2I looks like floats, and
> 2^5000+2I looks like
> 1.4124...x10^1505 + 0. I. with a red box around the 0.
>

of course my definition of format did not take your cases into account (it was meant to work on realistic approximate complex numbers), but for sure you can modify the definition to satisfy your needs.

> but also there is an issue that FullForm[%] is peculiar.
>

FullForm works as expected, also pattern matching works (in terms of the
interpreted expression).

> So the idea of changing the display format in $Post processing is a
> possibility,
> it rapidly becomes less elegant if you expect it to cover every
> eventuality, and
> probably not possible if you want FullForm to work.
> RJF
>

I think, that you did not see the main point. It is not the definition of format it is the mechanism that allows to implement any output format in terms of interpreted expressions.

Here I merge your approach of a previous post with mine to work in a more
elegant way:

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

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

In[3]:= c1=1/2+2I
Out[3]= 1/2+2 i

In[4]:= c1^2
Out[4]= -(15/4)+2 i

In[5]:= c1=2^5000+2I;

In[6]:= Im[c1]
Out[6]= 2

In[7]:= FullForm[0.+1.i]
Out[7]//FullForm= Plus[0.`,Times[1.`,i]]

In[8]:= c1/.c_Complex:>Im[c]
Out[8]= 2

In[9]:= Clear[$Post]

I like this!


Fred Simons

unread,
Jun 23, 2013, 10:50:09 PM6/23/13
to
Want you want to do can be achieved by changing the way Mathematica
prints complex numbers, for example in the following way:

In[1]:= 0.+4.23I
4.23+0. I
3.5+4.2 I

Out[1]= 0. +4.23 I
Out[2]= 4.23 +0. I
Out[3]= 3.5 +4.2 I

In[4]:= MakeBoxes[Complex[a_ /; Chop[a]==0., b_] , StandardForm] :=
RowBox[{MakeBoxes[b, StandardForm]," ", MakeBoxes[I,StandardForm]}];

MakeBoxes[Complex[a_ , b_/;Chop[b]==0] , StandardForm] := MakeBoxes[a,
StandardForm];

In[6]:= 0.+4.23I
4.23+0. I
3.5+4.2 I

Out[6]= 4.23 I
Out[7]= 4.23
Out[8]= 3.5 +4.2 I

Be careful; there might be side effects.

Fred Simons
Eindhoven University of Technology

0 new messages