Subtraction of complex numbers in Python
# Subtraction can be defined in terms of addition
# and the additive inverse.
#
# If
#
# w = a + bj
#
# and
#
# z = c + dj,
#
# then
#
# w - z = w + (-z)
#
# = (a + bj) + (-c - dj)
#
# = (a - c) + (b - d)j.
#
# Thus, complex numbers are subtracted by
# subtracting their real components and their
# imaginary components separately.
w = 5.3 + 3.2j
z = 3.2 - 4.7j
print( "\nCalculations with subtraction:" )
print( w - z )
# We can obtain the same result by adding the
# additive inverse of z to w.
print( w + (-z) )
# Addition is commutative, so we can also add
# the two numbers in the opposite order.
print( (-z) + w )
# Thus,
#
# w - z = w + (-z) = (-z) + w.
print( "\nEquivalent ways of calculating w - z:" )
print( w - z )
print( w + (-z) )
print( (-z) + w )
# We can also see how subtraction is performed
# by separately subtracting the real and
# imaginary components.
print( w.real - z.real )
print( w.imag - z.imag )
print( "Re(w) - Re(z) =",
w.real - z.real )
print( "Im(w) - Im(z) =",
w.imag - z.imag )
# Putting these two components back together
# gives the difference of the two complex
# numbers.
print( "(Re(w) - Re(z)) + j(Im(w) - Im(z)) =",
(w.real - z.real)
+ 1j*(w.imag - z.imag) )
# Thus,
#
# Re(w - z) = Re(w) - Re(z)
#
# and
#
# Im(w - z) = Im(w) - Im(z).
# Unlike addition, subtraction is not
# commutative. In general,
#
# w - z != z - w.
#
# Instead, reversing the order gives the
# additive inverse:
#
# w - z = -(z - w).
print( "\nReversing the order of subtraction:" )
print( w - z )
print( z - w )
print( -(z - w) )
# Confirm that
#
# w - z = -(z - w).
print( (w - z) == -(z - w) )
# Subtracting a number from itself gives zero.
print( "\nSubtracting a number from itself:" )
print( w - w )
print( z - z )
# Subtracting zero does not change a number.
print( "\nSubtracting zero:" )
print( w - 0 )
print( z - 0 )
# Subtracting a number from zero gives its
# additive inverse.
print( "\nSubtracting a number from zero:" )
print( 0 - w )
print( -w )
print( 0 - z )
print( -z )
# Thus,
#
# w - 0 = w
#
# while
#
# 0 - w = -w.
# Subtracting an additive inverse is equivalent
# to addition:
#
# w - (-z) = w + z.
print( "\nSubtracting an additive inverse:" )
print( w - (-z) )
print( w + z )
print( (w - (-z)) == (w + z) )
# Parentheses are important when more than two
# numbers are being subtracted. Subtraction is
# not associative. In general,
#
# (w - z) - v != w - (z - v).
v = -1.8 + 2.1j
print( "\nSubtraction is not associative:" )
print( (w - z) - v )
print( w - (z - v) )
# The first expression can be written as
#
# (w - z) - v
# = w + (-z) + (-v)
# = w - z - v.
#
# The second expression is
#
# w - (z - v)
# = w - z + v.
#
# Consequently, these expressions generally
# have different values.
print( ((w - z) - v) == (w - (z - v)) )