1
0
Fork 0
pr3-sose2026-fork/haskell/06-complex
Sebastian Steger e7f4eb65f4 formatting 2025-08-20 15:30:42 +02:00
..
README.md complex number assignment 2025-08-20 15:29:24 +02:00
complex.hs formatting 2025-08-20 15:30:42 +02:00

README.md

Complex Numbers

Assignment

Define a new type Complex that allows arithmetic calculations in an intuitive fashion. See below for the rules that shall be implemented. Incrementally make sure that all unit tests in 06-complex/complex.hs are passed.

Complex Numbers

Complex numbers are numbers of the form: z = a + bi

  • a: The real part of the complex number.
  • b: The imaginary part of the complex number.
  • i: The imaginary unit, where i^2 = -1.

Complex numbers are used in mathematics, physics, and engineering to extend the real number system and solve equations that have no real solutions, such as x^2 + 1 = 0.

Complex numbers are often represented in both rectangular form a + bi and polar/exponential form r e^{i\theta}.

Rules for Computing with Complex Numbers

1. Addition and Subtraction

  • Add or subtract the real and imaginary parts separately.
  • Rule:
    (a + bi) + (c + di) = (a + c) + (b + d)i
    (a + bi) - (c + di) = (a - c) + (b - d)i

Examples:

  1. (2 + 3i) + (4 + 5i) = 6 + 8i
  2. (0 + 3i) + (2 + 0i) = 2 + 3i
  3. (7 + 2i) - (3 + 6i) = 4 - 4i
  4. (5 + 4i) - (1 + 2i) = 4 + 2i

2. Multiplication

  • Use the distributive property and the rule i^2 = -1.
  • Rule:
    (a + bi)(c + di) = (ac - bd) + (ad + bc)i

Examples:

  1. (1 + 2i)(3 + 4i) = -5 + 10i
  2. (2 + 3i)(1 - i) = 5 + i
  3. (4 + i)(2 - 3i) = 11 - 10i
  4. (3 + 2i)(3 + 2i) = 5 + 12i

3. Division

  • Multiply numerator and denominator by the conjugate of the denominator.
  • Rule:
    \frac{a + bi}{c + di} = \frac{(a + bi)(c - di)}{(c + di)(c - di)}
    Simplifies to:
    \frac{(ac + bd) + (bc - ad)i}{c^2 + d^2}

Examples:

  1. \frac{1 + i}{1 - i} = 0 + 1i
  2. \frac{3 + 2i}{4 + 3i} = \frac{18 + i}{25} = 0.72 - 0.04i
  3. \frac{2 + i}{1 + i} = \frac{3 - i}{2} = 1.5 - 0.5i

4. Complex Conjugate

  • The conjugate of a + bi is a - bi, used for simplifications and magnitude calculation.

Examples:

  1. Conjugate of 3 + 4i is 3 - 4i.
  2. Conjugate of 5 - i is 5 + i.
  3. Conjugate of -2 + 3i is -2 - 3i.

5. Magnitude (Modulus)

  • The magnitude is the distance from the origin in the complex plane.
  • Rule: |a + bi| = \sqrt{a^2 + b^2}

Examples:

  1. |3 + 4i| = \sqrt{3^2 + 4^2} = 5
  2. |1 - i| = \sqrt{1^2 + (-1)^2} = \sqrt{2}
  3. |0 + 5i| = \sqrt{0^2 + 5^2} = 5