This question already has an answer here:

Say I have the following expression:

0.355329 + 0.355329 Cos[0.445723 u + (0. + 0.445723 I) v]

How can I simplify it so that the expression is simplified to

0.355329 + 0.355329 Cos[0.445723 u + 0.445723 I v]

Thanks for your help in advance!


marked as duplicate by Oleksandr R., user9660, Yves Klett, MarcoB, rhermans Jan 11 '16 at 14:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ Usually you can do it with Chop; but look at Possible Issuesin the documention $\endgroup$ – user36273 Dec 21 '15 at 15:16
  • 2
    $\begingroup$ expr // Rationalize[#, 0] & will get rid of the 0 but must be kept as rationals. $\endgroup$ – Bob Hanlon Dec 21 '15 at 16:31
Format[Complex[0., y_]] := HoldForm[y I]

res = 0.355329 + 0.355329 Cos[0.445723 u + (0. + 0.445723 I) v]

enter image description here

If you want to use res for other than display purposes use ReleaseHold

Plot[Re@ReleaseHold@res /. v :> 2, {u, -1, 1}]

enter image description here


Chop won't work here. I'll try to explain why.

0.445723 I is of type Complex. Internally, both of its real and imaginary parts are stored. This is reflected by it FullForm:

Mathematica graphics

Thus internally it cannot be simplified any further. This is the simplest possible form.

Formatting the expression on screen is a different matter. By default Mathematica always shows both the imaginary and real part of a floating point complex number, even if the real part is 0.. I suspect this choice has to do with the fact that 0. is considered an inexact zero. It is comparable to how the symbolic expression 0. x is not automatically simplified to an exact 0. An inexact zero can be the result of roundoff errors in a numerical calculation and may be inaccurate (i.e. doing the same calculation with higher precision may in principle yield a very small but nonzero result). Thus Mathematica always displays inexact zeros.

This situation differs from something like 1.2 + 0. I. Chop does work here: it will convert this expression of type Complex to one of type Real. This is not possible for 0. + 1.2 I because Mathematica has no separate type to represent a purely imaginary number with an exact zero real part and an inexact imaginary part.

To sum up: the two expressions you ask about are in fact exactly the same thing internally. When you input them, both forms will be represented as the same internal data structure. When Mathematica prints it out, it chooses to use the first printed form.

You could ask about how to cause it to print the very same expression without the 0. (and not about how to simplify its internal structure: that cannot be done). I'll leave this to other answers, like the one by @george2079.

  • $\begingroup$ The OP is asking how to get it to print in the simplified form; the internal representation is irrelevant. $\endgroup$ – Lightness Races in Orbit Dec 21 '15 at 18:22
  • 1
    $\begingroup$ @LightnessRacesinOrbit That's not clear at all to me form his question. He said, "How can I simplify it so that the expression is simplified to ...". $\endgroup$ – Szabolcs Dec 21 '15 at 18:48
  • $\begingroup$ Well it's pretty clear to me!! $\endgroup$ – Lightness Races in Orbit Dec 21 '15 at 19:18
  • 3
    $\begingroup$ @LightnessRacesinOrbit In customary Mathematica terminology, expression = internal representation. We say that Complex[1,2] and 1 + 2I are the same expression, even if they look different. Of course the OP may not be aware of this. He may also not be aware that the two things he wrote are the very same expression. Instead of second guessing what he meant and how much he knows about Mathematica, I tried to be helpful and explain a point that (at the time of my post) was missing form other answers. I don't really understand the point of your comment. $\endgroup$ – Szabolcs Dec 21 '15 at 19:32
  • $\begingroup$ OP never replied to my comment, but judging by the accepted answer, the intent seems to have been formatting after all. $\endgroup$ – Oleksandr R. Jan 9 '16 at 4:03

inexact purely imaginary complex numbers are always displayed with the zero real part. Even if you do

 1. I

you get:

>  0. + 1. I 

For display purposes you can do something like this:

0.355329 + 0.355329 Cos[0.445723 u + (0. + 0.445723 I) v] /. 
       Complex[0., im_] :> im i  (* or im "I" *)

0.355329 + 0.355329 Cos[0.445723 u + 0.445723 i v]

You can use Chop to deal with the numerical zero issue:

Chop[0.355329 + 0.355329 Cos[0.445723 u + (5.`*^-16 + 0.445723 I) v]] /. 
       Complex[0., im_] :> im i

0.355329 + 0.355329 Cos[0.445723 u + 0.445723 i v]

or like this if you don't want to Chop the whole thing:

0.355329 + 0.355329 Cos[0.445723 u + (5.`*^-16 + 0.445723 I) v] /. 
     Complex[x_ /; Chop[x] == 0, im_] -> i im
  • 2
    $\begingroup$ It will probably not work in reality because the near-zero will not MatchQ to 0.. Rather one should check that this value is below some threshold. $\endgroup$ – Oleksandr R. Dec 21 '15 at 15:41
  • $\begingroup$ good point. ... $\endgroup$ – george2079 Dec 21 '15 at 15:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.