Simplification of a complex number times its conjugate

469 views
Skip to first unread message

Bogdan Opanchuk

unread,
Nov 14, 2013, 8:40:58 PM11/14/13
to sy...@googlegroups.com
Hello all,

I have a complex-valued symbol z, and I would like its product with its conjugate (z z^*) to be simplified to |z|^2. Currently sympy seems to consider them to be different:

from sympy import *

init_printing
()

z
= Symbol('z', complex=True)
pprint
(simplify(z * z.conjugate() - abs(z) ** 2))

  _      2
z
z - z

Besides simplify() I have also tried powsimp(), with the same result. Is there some other simplification function that does this transformation?

Aaron Meurer

unread,
Nov 15, 2013, 12:10:50 AM11/15/13
to sy...@googlegroups.com
The best bet to simplify something involving complex number relations
is to use expand_complex, which will put the expression into a + b*I
with a, b real form. It looks like expand_complex(abs(x)**2) gives an
overly complicated result, but if you call simplify, it reduces to
im(x)**2 + re(x)**2, which is the same thing that
expand_complex(x*x.conjugate()) gives, simplify(expand_complex())
should reduce your expression to 0.

And by the way, whenever you find something that some part of SymPy
can simplify, but simplify() can't, we consider it to be a bug.
simplify() should be smart enough to do the right thing for you.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.

Bogdan Opanchuk

unread,
Nov 15, 2013, 1:20:10 AM11/15/13
to sy...@googlegroups.com
Hi Aaron,

Thank you for the explanation. Unfortunately, expand_complex() only solves the equality checking problem; it would be also nice to have some automated way of transforming z z^* -> |z|^2. I am working with equations that include a large amount of factors like |z|^2 and |z|^4 (with different variables as 'z'), and the application of expand_complex() will make them quite unreadable.

Aaron Meurer

unread,
Nov 15, 2013, 12:37:56 PM11/15/13
to sy...@googlegroups.com
I agree. The easiest thing is to probably replace z.conjugate with
abs(z)**2/z. We should really implement conjugate.rewrite(Abs) and
Abs.rewrite(conjugate) to make this easier.

Aaron Meurer

Gerardo Suarez

unread,
Jul 5, 2021, 9:11:48 AM7/5/21
to sympy
I was having the same issue 8 years later, is this still the best way?

Gerardo Suarez

unread,
Jul 5, 2021, 9:11:48 AM7/5/21
to sympy
I'm having this same issue 8 years later, is this still the best approach?

El viernes, 15 de noviembre de 2013 a las 18:37:56 UTC+1, asme...@gmail.com escribió:

Aaron Meurer

unread,
Jul 5, 2021, 5:55:24 PM7/5/21
to sympy
Abs.rewrite(conjugate) is now implemented, so you can use
expr.rewrite(conjugate) to replace all abs(x) with
sqrt(x*x.conjugate()). Unfortunately, the reverse rewrite is not yet
implemented, so you will still need to do it manually using replace().

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/9f192dab-726c-43f3-9fda-3f89a4b8109fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages