Skip to content

Problem 1: Complex Number (100pts)

Problem

In mathematics, a complex number is an element of a number system that extends the real numbers by including the imaginary unit \(i\), which is defined by the property that \(i^2 = -1\).

Every complex number can be expressed in the form \(a + bi\), where \(a\) and \(b\) are real numbers. Here, \(a\) is called the real part of the complex number, and \(b\) is called the imaginary part.

We define addition and multiplication for complex numbers as follows:

  • Addition: For any two complex numbers \(c\_1\) and \(c\_2\) where \(c\_1 = a + bi\) and \(c\_2 = c + di\), the addition operator \( + \) is defined as \(c\_1 + c\_2 = (a + c) + (b + d)i\).
  • Multiplication: For any two complex numbers \(c\_1\) and \(c\_2\) where \(c\_1 = a + bi\) and \(c\_2 = c + di\), the multiplication operator \( \times \) is defined as \(c\_1 \times c\_2 = (ac - bd) + (ad + bc)i\).

In this problem, you will implement a Complex class representing complex numbers and their operations. You need to implement specific special methods to support the required functionalities as described in the docstring of the class.

在数学中,复数是一种数系中的元素,该数系通过包含虚数单位 \(i\) 来扩展了实数,虚数单位 \(i\) 被定义为满足 \(i^2 = -1\) 这一性质。

每个复数都可以表示成 \(a + bi\) 的形式,其中 \(a\)\(b\) 是实数。在这里,\(a\) 被称为该复数的实部,而 \(b\) 被称为虚部

我们对复数的加法乘法定义如下:

  • 加法: 对于任意两个复数 \(c_1\)\(c_2\),其中 \(c_1 = a + bi\)\(c_2 = c + di\),加法运算符 $ + $ 被定义为 \(c_1 + c_2 = (a + c) + (b + d)i\)
  • 乘法: 对于任意两个复数 \(c_1\)\(c_2\),其中 \(c_1 = a + bi\)\(c_2 = c + di\),乘法运算符 $ \times $ 被定义为 \(c_1 \times c_2 = (ac - bd) + (ad + bc)i\)

在本问题中,你将实现一个 Complex 类来表示复数及其运算。 你需要实现特定的特殊方法以支持类文档字符串中描述的所需功能。

class Complex:
    """Complex Number.

    >>> a = Complex(1, 2)
    >>> a
    Complex(real=1, imaginary=2)
    >>> print(a)
    1 + 2i
    >>> b = Complex(-1, -2)
    >>> b
    Complex(real=-1, imaginary=-2)
    >>> print(b)
    -1 - 2i
    >>> print(a + b)
    0
    >>> print(a * b)
    3 - 4i
    >>> print(a)
    1 + 2i
    >>> print(b)
    -1 - 2i
    """
    def __init__(self, real, imaginary):
        self.real = real
        self.imaginary = imaginary

    "*** YOUR CODE HERE ***"

When printing a complex number, you should pay attention to the signs of the real and imaginary parts:

  • if the number is zero, you should print it as 0;
  • if the imaginary part is negative, you should print it as a - bi instead of a + -bi;
  • if the imaginary part is zero, you should print it as a instead of a + 0i;
  • if the real part is zero, you should print it as bi instead of 0 + bi.

在打印一个复数时,你需要注意实部和虚部的符号:

  • 如果该复数是,你应该将其打印为 0
  • 如果虚部为负,你应该打印为 \(a - bi\),而不是 \(a + -bi\)
  • 如果虚部为零,你应该打印为 \(a\),而不是 \(a + 0i\)
  • 如果实部为零,你应该打印为 \(bi\),而不是 \(0 + bi\)

Hints

Hint 1: You may find Python string formatting syntax or f-strings useful. A quick example:

>>> ten, twenty, thirty = 10, 'twenty', [30]
>>> '{0} plus {1} is {2}'.format(ten, twenty, thirty)
'10 plus twenty is [30]'

>>> feeling = 'love'
>>> year = 2025
>>> course = 'SICP'
>>> f'I {feeling} {course}-{year}!'
'I love SICP-2025!'

Hint 2:: You may find python special methods __add__ and __mul__ helpful. Please refer to the documentation for more details.

>>> ten, twenty, thirty = 10, 'twenty', [30]
>>> '{0} plus {1} is {2}'.format(ten, twenty, thirty)
'10 plus twenty is [30]'
>>> feeling = 'love'
>>> year = 2025
>>> course = 'SICP'
>>> f'I {feeling} {course}-{year}!'
'I love SICP-2025!'
  • 你可能会发现 Python 特殊方法 __add____mul__ 会有所帮助。 请查阅文档 (documentation) 获取更多详情。