forked from steger/pr3-sose2026
2.3 KiB
2.3 KiB
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, wherei^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:
(2 + 3i) + (4 + 5i) = 6 + 8i(0 + 3i) + (2 + 0i) = 2 + 3i(7 + 2i) - (3 + 6i) = 4 - 4i(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 + 2i)(3 + 4i) = -5 + 10i(2 + 3i)(1 - i) = 5 + i(4 + i)(2 - 3i) = 11 - 10i(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:
\frac{1 + i}{1 - i} = 0 + 1i\frac{3 + 2i}{4 + 3i} = \frac{18 + i}{25} = 0.72 - 0.04i\frac{2 + i}{1 + i} = \frac{3 - i}{2} = 1.5 - 0.5i
4. Complex Conjugate
- The conjugate of
a + biisa - bi, used for simplifications and magnitude calculation.
Examples:
- Conjugate of
3 + 4iis3 - 4i. - Conjugate of
5 - iis5 + i. - Conjugate of
-2 + 3iis-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:
|3 + 4i| = \sqrt{3^2 + 4^2} = 5|1 - i| = \sqrt{1^2 + (-1)^2} = \sqrt{2}|0 + 5i| = \sqrt{0^2 + 5^2} = 5