|
1 | 1 | # Instructions
|
2 | 2 |
|
3 |
| -A complex number is a number in the form `a + b * i` where `a` and `b` are real and `i` satisfies `i^2 = -1`. |
| 3 | +A **complex number** is expressed in the form `z = a + b * i`, where: |
4 | 4 |
|
5 |
| -`a` is called the real part and `b` is called the imaginary part of `z`. |
6 |
| -The conjugate of the number `a + b * i` is the number `a - b * i`. |
7 |
| -The absolute value of a complex number `z = a + b * i` is a real number `|z| = sqrt(a^2 + b^2)`. The square of the absolute value `|z|^2` is the result of multiplication of `z` by its complex conjugate. |
| 5 | +- `a` is the **real part** (a real number), |
8 | 6 |
|
9 |
| -The sum/difference of two complex numbers involves adding/subtracting their real and imaginary parts separately: |
10 |
| -`(a + i * b) + (c + i * d) = (a + c) + (b + d) * i`, |
11 |
| -`(a + i * b) - (c + i * d) = (a - c) + (b - d) * i`. |
| 7 | +- `b` is the **imaginary part** (also a real number), and |
12 | 8 |
|
13 |
| -Multiplication result is by definition |
14 |
| -`(a + i * b) * (c + i * d) = (a * c - b * d) + (b * c + a * d) * i`. |
| 9 | +- `i` is the **imaginary unit** satisfying `i^2 = -1`. |
15 | 10 |
|
16 |
| -The reciprocal of a non-zero complex number is |
17 |
| -`1 / (a + i * b) = a/(a^2 + b^2) - b/(a^2 + b^2) * i`. |
| 11 | +## Operations on Complex Numbers |
18 | 12 |
|
19 |
| -Dividing a complex number `a + i * b` by another `c + i * d` gives: |
20 |
| -`(a + i * b) / (c + i * d) = (a * c + b * d)/(c^2 + d^2) + (b * c - a * d)/(c^2 + d^2) * i`. |
| 13 | +### Conjugate |
21 | 14 |
|
22 |
| -Raising e to a complex exponent can be expressed as `e^(a + i * b) = e^a * e^(i * b)`, the last term of which is given by Euler's formula `e^(i * b) = cos(b) + i * sin(b)`. |
| 15 | +The conjugate of the complex number `z = a + b * i` is given by: |
23 | 16 |
|
24 |
| -Implement the following operations: |
| 17 | +```text |
| 18 | +zc = a - b * i |
| 19 | +``` |
25 | 20 |
|
26 |
| -- addition, subtraction, multiplication and division of two complex numbers, |
27 |
| -- conjugate, absolute value, exponent of a given complex number. |
| 21 | +### Absolute Value |
28 | 22 |
|
29 |
| -Assume the programming language you are using does not have an implementation of complex numbers. |
| 23 | +The absolute value (or modulus) of `z` is defined as: |
| 24 | + |
| 25 | +```text |
| 26 | +|z| = sqrt(a^2 + b^2) |
| 27 | +``` |
| 28 | + |
| 29 | +The square of the absolute value is computed as the product of `z` and its conjugate `zc`: |
| 30 | + |
| 31 | +```text |
| 32 | +|z|^2 = z * zc = a^2 + b^2 |
| 33 | +``` |
| 34 | + |
| 35 | +### Addition |
| 36 | + |
| 37 | +The sum of two complex numbers `z1 = a + b * i` and `z2 = c + d * i` is computed by adding their real and imaginary parts separately: |
| 38 | + |
| 39 | +```text |
| 40 | +z1 + z2 = (a + b * i) + (c + d * i) |
| 41 | + = (a + c) + (b + d) * i |
| 42 | +``` |
| 43 | + |
| 44 | +### Subtraction |
| 45 | + |
| 46 | +The difference of two complex numbers is obtained by subtracting their respective parts: |
| 47 | + |
| 48 | +```text |
| 49 | +z1 - z2 = (a + b * i) - (c + d * i) |
| 50 | + = (a - c) + (b - d) * i |
| 51 | +``` |
| 52 | + |
| 53 | +### Multiplication |
| 54 | + |
| 55 | +The product of two complex numbers is defined as: |
| 56 | + |
| 57 | +```text |
| 58 | +z1 * z2 = (a + b * i) * (c + d * i) |
| 59 | + = (a * c - b * d) + (b * c + a * d) * i |
| 60 | +``` |
| 61 | + |
| 62 | +### Reciprocal |
| 63 | + |
| 64 | +The reciprocal of a non-zero complex number is given by: |
| 65 | + |
| 66 | +```text |
| 67 | +1 / z = 1 / (a + b * i) |
| 68 | + = a / (a^2 + b^2) - b / (a^2 + b^2) * i |
| 69 | +``` |
| 70 | + |
| 71 | +### Division |
| 72 | + |
| 73 | +The division of one complex number by another is given by: |
| 74 | + |
| 75 | +```text |
| 76 | +z1 / z2 = z1 * (1 / z2) |
| 77 | + = (a + b * i) / (c + d * i) |
| 78 | + = (a * c + b * d) / (c^2 + d^2) + (b * c - a * d) / (c^2 + d^2) * i |
| 79 | +``` |
| 80 | + |
| 81 | +### Exponentiation |
| 82 | + |
| 83 | +Raising _e_ (the base of the natural logarithm) to a complex exponent can be expressed using Euler's formula: |
| 84 | + |
| 85 | +```text |
| 86 | +e^(a + b * i) = e^a * e^(b * i) |
| 87 | + = e^a * (cos(b) + i * sin(b)) |
| 88 | +``` |
| 89 | + |
| 90 | +## Implementation Requirements |
| 91 | + |
| 92 | +Given that you should not use built-in support for complex numbers, implement the following operations: |
| 93 | + |
| 94 | +- **addition** of two complex numbers |
| 95 | +- **subtraction** of two complex numbers |
| 96 | +- **multiplication** of two complex numbers |
| 97 | +- **division** of two complex numbers |
| 98 | +- **conjugate** of a complex number |
| 99 | +- **absolute value** of a complex number |
| 100 | +- **exponentiation** of _e_ (the base of the natural logarithm) to a complex number |
0 commit comments