Ma · Mathematics

How to Find the Real and Imaginary Parts of a Polynomial

Published By the 9to5Science editorial team

At a glance

  1. 1. Substitute the complex input

    Write z as x + iy, with x and y real.

  2. 2. Expand the polynomial

    Use z² = (x + iy)² and simplify powers and products.

  3. 3. Collect components

    Group terms without i for the real part, and coefficients of i for the imaginary part.

  4. 4. Check with a CAS

    In SymPy, expand the expression and apply re and im to the result.

If you want a calculator to split a polynomial into its real and imaginary parts, the key is to tell it what the complex input means. Write the input as z=x+iyz=x+iy, where xx and yy are real. Then expand p(x+iy)p(x+iy) and collect the real terms separately from the terms multiplied by ii.

The result has the form

p(x+iy)=u(x,y)+iv(x,y),p(x+iy)=u(x,y)+i v(x,y),

where uu is the real part and vv is the imaginary part. A computer algebra system such as SymPy can do the expansion and separation symbolically. The same substitution-and-collecting method works by hand for small polynomials.

What “real and complex parts” usually means

For a complex number w=a+ibw=a+ib, its real part is aa and its imaginary part is bb. The imaginary part is the real-valued coefficient of ii; it is not ibib itself. For example, if w=3−2iw=3-2i, then Re⁡(w)=3\operatorname{Re}(w)=3 and Im⁡(w)=−2\operatorname{Im}(w)=-2.

For a polynomial in a complex variable, the parts generally depend on both coordinates of the input. If p(z)p(z) has real coefficients, substituting z=x+iyz=x+iy still usually produces both a real part and an imaginary part. If the polynomial has complex coefficients, split those coefficients too as part of the expansion.

A worked example

Take

p(z)=z3+(2−i)z+1p(z)=z^3+(2-i)z+1

and let z=x+iyz=x+iy. First expand the cubic:

(x+iy)3=(x3−3xy2)+i(3x2y−y3).(x+iy)^3=(x^3-3xy^2)+i(3x^2y-y^3).

Next expand the linear term:

(2−i)(x+iy)=2x+y+i(2y−x).(2-i)(x+iy)=2x+y+i(2y-x).

Adding the constant 11 and grouping the terms gives

p(x+iy)=x3−3xy2+2x+y+1⏟u(x,y)+i(3x2y−y3+2y−x)⏟v(x,y).p(x+iy)=\underbrace{x^3-3xy^2+2x+y+1}_{u(x,y)} +i\underbrace{(3x^2y-y^3+2y-x)}_{v(x,y)}.

Therefore,

Re⁡(p(x+iy))=x3−3xy2+2x+y+1,\operatorname{Re}(p(x+iy))=x^3-3xy^2+2x+y+1,

and

Im⁡(p(x+iy))=3x2y−y3+2y−x.\operatorname{Im}(p(x+iy))=3x^2y-y^3+2y-x.

For a numerical check, choose z=1+2iz=1+2i, so x=1x=1 and y=2y=2. The formulas give u=−6u=-6 and v=1v=1, hence p(1+2i)=−6+ip(1+2i)=-6+i. Direct evaluation gives the same result.

Use SymPy as a symbolic calculator

In Python with SymPy installed, define xx and yy as real symbols. That assumption matters: without it, the system may not be able to simplify real-part and imaginary-part expressions fully.

from sympy import symbols, I, expand, re, im

x, y = symbols('x y', real=True)
z = x + I*y
p = z**3 + (2 - I)*z + 1

expanded = expand(p)
real_part = expand(re(expanded))
imaginary_part = expand(im(expanded))

print(real_part)
print(imaginary_part)

The output is

x**3 - 3*x*y**2 + 2*x + y + 1
3*x**2*y - y**3 - x + 2*y

The order of terms may differ, but the expressions are equivalent. For a different polynomial, replace the definition of p; keep x and y declared real and use z = x + I*y.

Common mistakes

  • Calling the coefficient of ii the imaginary part incorrectly: For a+iba+ib, the imaginary part is bb, not ibib.
  • Forgetting to declare xx and yy real: A symbolic system may retain unevaluated expressions involving real and imaginary-part operators if it does not know the variables are real.
  • Treating a complex coefficient as real: For example, 2−i2-i contributes to both parts after multiplication by x+iyx+iy.
  • Confusing the polynomial’s coefficients with its output: A polynomial with real coefficients can still have a nonzero imaginary part when evaluated at a non-real input.

If the input is a specific complex number rather than x+iyx+iy, substitute its real and imaginary coordinates into uu and vv, or evaluate the polynomial directly and read off the two components.

References

  1. SymPy documentation: Elementary functions (including real and imaginary parts)

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.