Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the description for the complex-numbers exercise #2496

Merged
merged 18 commits into from
Nov 20, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 90 additions & 18 deletions exercises/complex-numbers/description.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,101 @@
# Description

A complex number is a number in the form `a + b * i` where `a` and `b` are real and `i` satisfies `i^2 = -1`.
A **complex number** is expressed in the form:

`a` is called the real part and `b` is called the imaginary part of `z`.
The conjugate of the number `a + b * i` is the number `a - b * i`.
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.
```math
z = a + b * i
```

The sum/difference of two complex numbers involves adding/subtracting their real and imaginary parts separately:
`(a + i * b) + (c + i * d) = (a + c) + (b + d) * i`,
`(a + i * b) - (c + i * d) = (a - c) + (b - d) * i`.
where:
tasxatzial marked this conversation as resolved.
Show resolved Hide resolved

Multiplication result is by definition
`(a + i * b) * (c + i * d) = (a * c - b * d) + (b * c + a * d) * i`.
- `a` is the **real part** (a real number),

The reciprocal of a non-zero complex number is
`1 / (a + i * b) = a/(a^2 + b^2) - b/(a^2 + b^2) * i`.
- `b` is the **imaginary part** (also a real number), and

Dividing a complex number `a + i * b` by another `c + i * d` gives:
`(a + i * b) / (c + i * d) = (a * c + b * d)/(c^2 + d^2) + (b * c - a * d)/(c^2 + d^2) * i`.
- `i` is the imaginary unit satisfying `i^2 = -1`.
jagdish-15 marked this conversation as resolved.
Show resolved Hide resolved

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)`.
## Key Properties
jagdish-15 marked this conversation as resolved.
Show resolved Hide resolved

Implement the following operations:
### Conjugate

- addition, subtraction, multiplication and division of two complex numbers,
- conjugate, absolute value, exponent of a given complex number.
The conjugate of the complex number `z = a + b * i` is given by:

Assume the programming language you are using does not have an implementation of complex numbers.
```math
jagdish-15 marked this conversation as resolved.
Show resolved Hide resolved
z̅ = a - b * i
```

### Absolute Value

The absolute value (or modulus) of `z` is defined as:

```math
|z| = sqrt(a^2 + b^2)
jagdish-15 marked this conversation as resolved.
Show resolved Hide resolved
```

The square of the absolute value, `|z|²`, can be computed as the product of `z` and its conjugate:
jagdish-15 marked this conversation as resolved.
Show resolved Hide resolved

```math
|z|² = z * z̅ = a² + b²
```

## Operations on Complex Numbers

### Addition

The sum of two complex numbers `z₁ = a + b * i` and `z₂ = c + d * i` is computed by adding their real and imaginary parts separately:

```math
z₁ + z₂ = (a + c) + (b + d) * i
```

### Subtraction

The difference of two complex numbers is obtained by subtracting their respective parts:

```math
z₁ - z₂ = (a - c) + (b - d) * i
```

### Multiplication

Check failure on line 59 in exercises/complex-numbers/description.md

View workflow job for this annotation

GitHub Actions / Lint markdown files

Trailing spaces

exercises/complex-numbers/description.md:59:19 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md

The product of two complex numbers is defined as:

```math
z₁ * z₂ = (a + b * i) * (c + d * i) = (a * c - b * d) + (b * c + a * d) * i
```

### Division

The division of one complex number by another is given by:

```math
z₁ / z₂ = (a + b * i) / (c + d * i) = (a * c + b * d) / (c² + d²) + (b * c - a * d) / (c² + d²) * i
```

### Reciprocal

The reciprocal of a non-zero complex number is given by:

```math
1 / z = 1 / (a + b * i) = a / (a² + b²) - b / (a² + b²) * i
```

### Exponentiation

Raising _e_ (the base of the natural logarithm) to a complex exponent can be expressed using Euler's formula:

```math
e^(a + b * i) = e^a * e^(b * i) = e^a * (cos(b) + i * sin(b))
```

## Implementation Requirements

Given that you should not use built-in support for complex numbers, implement the following operations:

- **Addition** of two complex numbers.
- **Subtraction** of two complex numbers.
- **Multiplication** of two complex numbers.
- **Division** of two complex numbers.
- Calculation of the **conjugate** of a complex number.
- Calculation of the **absolute value** of a complex number.
- Calculation of the **exponent** of a given complex number.
jagdish-15 marked this conversation as resolved.
Show resolved Hide resolved
Loading