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

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.
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.
– Szabolcs
Dec 21 '15 at 19:32
Unprotect@Complex;
Format[Complex[0., y_]] := HoldForm[y I]
Protect@Complex;
res = 0.355329 + 0.355329 Cos[0.445723 u + (0. + 0.445723 I) v]
If you want to use res for other than display purposes use ReleaseHold
Plot[Re@ReleaseHold@res /. v :> 2, {u, -1, 1}]
ReleaseHold doesn't seem to be necessary, at least in Mathematica 11.2. And indeed, why would it, if we only modified the way the output looks, not the output itself?
– Ruslan
Aug 16 '20 at 09:15
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
MatchQ to 0.. Rather one should check that this value is below some threshold.
– Oleksandr R.
Dec 21 '15 at 15:41
Chop; but look atPossible Issuesin the documention – Dec 21 '15 at 15:16expr // Rationalize[#, 0] &will get rid of the 0 but must be kept as rationals. – Bob Hanlon Dec 21 '15 at 16:31