Skip to content

Commit

Permalink
Update Swift tools version to 6.0 and add new Position struct for sad…
Browse files Browse the repository at this point in the history
…dle points exercise
  • Loading branch information
meatball133 committed Dec 26, 2024
1 parent 30f8e4c commit 43d8ff9
Show file tree
Hide file tree
Showing 21 changed files with 1,360 additions and 1,049 deletions.
11 changes: 11 additions & 0 deletions exercises/practice/complex-numbers/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Append

You will have to implement your own equality operator for the `ComplexNumber` object.
This will pose the challenge of comparing two floating point numbers.
It might be useful to use the method `isApproximatelyEqual(to:absoluteTolerance:)` which can be found in the [Numerics][swift-numberics] library.
With a given tolerance of `0.00001` should be enough to pass the tests.
The library is already imported in the project so it is just to import it in your file.

You are aLso free to implement your own method to compare the two complex numbers.

[swift-numberics]: https://github.com/apple/swift-numerics
107 changes: 89 additions & 18 deletions exercises/practice/complex-numbers/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,100 @@
# Instructions

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 `z = a + b * i`, where:

`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.
- `a` is the **real part** (a real number),

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`.
- `b` is the **imaginary part** (also a real number), and

Multiplication result is by definition
`(a + i * b) * (c + i * d) = (a * c - b * d) + (b * c + a * d) * i`.
- `i` is the **imaginary unit** satisfying `i^2 = -1`.

The reciprocal of a non-zero complex number is
`1 / (a + i * b) = a/(a^2 + b^2) - b/(a^2 + b^2) * i`.
## Operations on Complex Numbers

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`.
### Conjugate

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)`.
The conjugate of the complex number `z = a + b * i` is given by:

Implement the following operations:
```text
zc = a - b * i
```

- addition, subtraction, multiplication and division of two complex numbers,
- conjugate, absolute value, exponent of a given complex number.
### Absolute Value

Assume the programming language you are using does not have an implementation of complex numbers.
The absolute value (or modulus) of `z` is defined as:

```text
|z| = sqrt(a^2 + b^2)
```

The square of the absolute value is computed as the product of `z` and its conjugate `zc`:

```text
|z|^2 = z * zc = a^2 + b^2
```

### Addition

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:

```text
z1 + z2 = (a + b * i) + (c + d * i)
= (a + c) + (b + d) * i
```

### Subtraction

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

```text
z1 - z2 = (a + b * i) - (c + d * i)
= (a - c) + (b - d) * i
```

### Multiplication

The product of two complex numbers is defined as:

```text
z1 * z2 = (a + b * i) * (c + d * i)
= (a * c - b * d) + (b * c + a * d) * i
```

### Reciprocal

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

```text
1 / z = 1 / (a + b * i)
= a / (a^2 + b^2) - b / (a^2 + b^2) * i
```

### Division

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

```text
z1 / z2 = z1 * (1 / z2)
= (a + b * i) / (c + d * i)
= (a * c + b * d) / (c^2 + d^2) + (b * c - a * d) / (c^2 + d^2) * i
```

### Exponentiation

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

```text
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
- **conjugate** of a complex number
- **absolute value** of a complex number
- **exponentiation** of _e_ (the base of the natural logarithm) to a complex number
Original file line number Diff line number Diff line change
@@ -1,66 +1,51 @@
import Foundation
import Numerics

struct ComplexNumber {
struct ComplexNumbers: Equatable {

var realComponent: Double
var real: Double
var imaginary: Double

var imaginaryComponent: Double

func getRealComponent() -> Double {

return self.realComponent
init(realComponent: Double, imaginaryComponent: Double? = 0) {
real = realComponent
imaginary = imaginaryComponent ?? 0
}

func getImaginaryComponent() -> Double {

return self.imaginaryComponent
func add(complexNumber: ComplexNumbers) -> ComplexNumbers {
ComplexNumbers(realComponent: real + complexNumber.real, imaginaryComponent: imaginary + complexNumber.imaginary)
}

func add(complexNumber: ComplexNumber) -> ComplexNumber {

return ComplexNumber(realComponent: self.realComponent + complexNumber.realComponent, imaginaryComponent: self.imaginaryComponent + complexNumber.imaginaryComponent)

func sub(complexNumber: ComplexNumbers) -> ComplexNumbers {
ComplexNumbers(realComponent: real - complexNumber.real, imaginaryComponent: imaginary - complexNumber.imaginary)
}

func subtract(complexNumber: ComplexNumber) -> ComplexNumber {

return ComplexNumber(realComponent: self.realComponent - complexNumber.realComponent, imaginaryComponent: self.imaginaryComponent - complexNumber.imaginaryComponent)
func mul(complexNumber: ComplexNumbers) -> ComplexNumbers {
ComplexNumbers(realComponent: real * complexNumber.real - imaginary * complexNumber.imaginary, imaginaryComponent: real * complexNumber.imaginary + imaginary * complexNumber.real)
}

func multiply(complexNumber: ComplexNumber) -> ComplexNumber {

return ComplexNumber(realComponent: self.realComponent * complexNumber.realComponent - self.imaginaryComponent * complexNumber.imaginaryComponent, imaginaryComponent: self.imaginaryComponent * complexNumber.realComponent + self.realComponent * complexNumber.imaginaryComponent)
func div(complexNumber: ComplexNumbers) -> ComplexNumbers {
let denominator = complexNumber.real * complexNumber.real + complexNumber.imaginary * complexNumber.imaginary
let realComponent = (real * complexNumber.real + imaginary * complexNumber.imaginary) / denominator
let imaginaryComponent = (imaginary * complexNumber.real - real * complexNumber.imaginary) / denominator
return ComplexNumbers(realComponent: realComponent, imaginaryComponent: imaginaryComponent)
}

func divide(complexNumber: ComplexNumber) -> ComplexNumber {

let amplitudeOfComplexNumber = (complexNumber.realComponent * complexNumber.realComponent) + (complexNumber.imaginaryComponent * complexNumber.imaginaryComponent)

let realPartOfQuotient = (self.realComponent * complexNumber.realComponent + self.imaginaryComponent * complexNumber.imaginaryComponent) / amplitudeOfComplexNumber

let imaginaryPartOfQuotient = (self.imaginaryComponent * complexNumber.realComponent - self.realComponent * self.realComponent * complexNumber.imaginaryComponent) / amplitudeOfComplexNumber

return ComplexNumber(realComponent: realPartOfQuotient, imaginaryComponent: imaginaryPartOfQuotient)
func absolute() -> Double {
sqrt(Double(real * real + imaginary * imaginary))
}

func conjugate() -> ComplexNumber {

return ComplexNumber(realComponent: self.realComponent, imaginaryComponent: (-1 * self.imaginaryComponent))
func conjugate() -> ComplexNumbers {
ComplexNumbers(realComponent: real, imaginaryComponent: -imaginary)
}

func absolute() -> Double {

return sqrt(pow(self.realComponent, 2.0) + pow(self.imaginaryComponent, 2.0))
func exponent() -> ComplexNumbers {
let expReal = exp(Double(real)) * cos(Double(imaginary))
let expImaginary = exp(Double(real)) * sin(Double(imaginary))
return ComplexNumbers(realComponent: expReal, imaginaryComponent: expImaginary)
}

func exponent() -> ComplexNumber {

let realPartOfResult = cos(self.imaginaryComponent)
let imaginaryPartOfResult = sin(self.imaginaryComponent)
let factor = exp(self.realComponent)

return ComplexNumber(realComponent: realPartOfResult * factor, imaginaryComponent: imaginaryPartOfResult * factor)

static func == (lhs: ComplexNumbers, rhs: ComplexNumbers) -> Bool {
lhs.real.isApproximatelyEqual(to: rhs.real, absoluteTolerance: 0.0001) && lhs.imaginary.isApproximatelyEqual(to: rhs.imaginary, absoluteTolerance: 0.0001)
}

}
}
3 changes: 2 additions & 1 deletion exercises/practice/complex-numbers/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"authors": [
"AlwynC"
"AlwynC",
"meatball133"
],
"contributors": [
"bhargavg",
Expand Down
72 changes: 72 additions & 0 deletions exercises/practice/complex-numbers/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Testing
import Foundation

@testable import {{exercise|camelCase}}

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct {{exercise|camelCase}}Tests {
{% outer: for case in cases %}
{%- if case.cases %}
{%- for subCases in case.cases %}
{%- if subCases.cases %}
{%- for subSubCases in subCases.cases %}
@Test("{{subSubCases.description}}", .enabled(if: RUNALL))
func test{{subSubCases.description |camelCase }}() {
let complexNumberOne = {{exercise|camelCase}}(realComponent: {{subSubCases.input.z1[0]}}, imaginaryComponent: {{subSubCases.input.z1[1]}})
let complexNumberTwo = {{exercise|camelCase}}(realComponent: {{subSubCases.input.z2[0]}}, imaginaryComponent: {{subSubCases.input.z2[1]}})
let result = complexNumberOne.{{subSubCases.property}}(complexNumber: complexNumberTwo)
let expected = {{exercise|camelCase}}(realComponent: {{subSubCases.expected[0]}}, imaginaryComponent: {{subSubCases.expected[1]}})
#expect(expected == result)
}
{%- endfor %}
{%- else %}
{%- if forloop.outer.first and forloop.first %}
@Test("{{subCases.description}}")
{%- else %}
@Test("{{subCases.description}}", .enabled(if: RUNALL))
{%- endif %}
func test{{subCases.description |camelCase }}() {
{%- if subCases.property == "real" or subCases.property == "imaginary" or subCases.property == "abs" or subCases.property == "conjugate" or subCases.property == "exp" %}
let complexNumber = {{exercise|camelCase}}(realComponent: {{subCases.input.z[0] | complexNumber}}, imaginaryComponent: {{subCases.input.z[1] | complexNumber}})
{%- if subCases.property == "real" %}
#expect(complexNumber.real == {{subCases.expected}})
{%- elif subCases.property == "imaginary" %}
#expect(complexNumber.imaginary == {{subCases.expected}})
{%- elif subCases.property == "abs" %}
#expect(complexNumber.absolute() == {{subCases.expected}})
{%- elif subCases.property == "conjugate" %}
let expected = {{exercise|camelCase}}(realComponent: {{subCases.expected[0]}}, imaginaryComponent: {{subCases.expected[1]}})
#expect(complexNumber.conjugate() == expected)
{%- elif subCases.property == "exp" %}
let expected = {{exercise|camelCase}}(realComponent: {{subCases.expected[0] | complexNumber}}, imaginaryComponent: {{subCases.expected[1]}})
#expect(complexNumber.exponent() == expected)
{%- elif subCases.property == "add" or subCases.property == "sub" or subCases.property == "mul" or subCases.property == "div" %}
{%- endif %}
{%- else %}
{%- if subCases.input.z1[0] %}
let complexNumberOne = {{exercise|camelCase}}(realComponent: {{subCases.input.z1[0]}}, imaginaryComponent: {{subCases.input.z1[1]}})
let complexNumberTwo = {{exercise|camelCase}}(realComponent: {{subCases.input.z2}}, imaginaryComponent: nil)
{%- else %}
let complexNumberOne = {{exercise|camelCase}}(realComponent: {{subCases.input.z1}}, imaginaryComponent: nil)
let complexNumberTwo = {{exercise|camelCase}}(realComponent: {{subCases.input.z2[0]}}, imaginaryComponent: {{subCases.input.z2[1]}})
{%- endif %}
let result = complexNumberOne.{{subCases.property}}(complexNumber: complexNumberTwo)
let expected = {{exercise|camelCase}}(realComponent: {{subCases.expected[0]}}, imaginaryComponent: {{subCases.expected[1]}})
#expect(expected == result)
{%- endif %}
}
{%- endif %}
{% endfor -%}
{%- else %}
@Test("{{case.description}}", .enabled(if: RUNALL))
func test{{case.description |camelCase }}() {
let complexNumberOne = {{exercise|camelCase}}(realComponent: {{case.input.z1[0]}}, imaginaryComponent: {{case.input.z1[1]}})
let complexNumberTwo = {{exercise|camelCase}}(realComponent: {{case.input.z2[0]}}, imaginaryComponent: {{case.input.z2[1]}})
let result = complexNumberOne.{{case.property}}(complexNumber: complexNumberTwo)
let expected = {{exercise|camelCase}}(realComponent: {{case.expected[0]}}, imaginaryComponent: {{case.expected[1]}})
#expect(expected == result)
}
{%- endif %}
{% endfor -%}
}
27 changes: 27 additions & 0 deletions exercises/practice/complex-numbers/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,30 @@ description = "Complex exponential function -> Exponential of a purely real numb

[08eedacc-5a95-44fc-8789-1547b27a8702]
description = "Complex exponential function -> Exponential of a number with real and imaginary part"

[d2de4375-7537-479a-aa0e-d474f4f09859]
description = "Complex exponential function -> Exponential resulting in a number with real and imaginary part"

[06d793bf-73bd-4b02-b015-3030b2c952ec]
description = "Operations between real numbers and complex numbers -> Add real number to complex number"

[d77dbbdf-b8df-43f6-a58d-3acb96765328]
description = "Operations between real numbers and complex numbers -> Add complex number to real number"

[20432c8e-8960-4c40-ba83-c9d910ff0a0f]
description = "Operations between real numbers and complex numbers -> Subtract real number from complex number"

[b4b38c85-e1bf-437d-b04d-49bba6e55000]
description = "Operations between real numbers and complex numbers -> Subtract complex number from real number"

[dabe1c8c-b8f4-44dd-879d-37d77c4d06bd]
description = "Operations between real numbers and complex numbers -> Multiply complex number by real number"

[6c81b8c8-9851-46f0-9de5-d96d314c3a28]
description = "Operations between real numbers and complex numbers -> Multiply real number by complex number"

[8a400f75-710e-4d0c-bcb4-5e5a00c78aa0]
description = "Operations between real numbers and complex numbers -> Divide complex number by real number"

[9a867d1b-d736-4c41-a41e-90bd148e9d5e]
description = "Operations between real numbers and complex numbers -> Divide real number by complex number"
13 changes: 9 additions & 4 deletions exercises/practice/complex-numbers/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

Expand All @@ -9,13 +9,18 @@ let package = Package(
name: "ComplexNumbers",
targets: ["ComplexNumbers"]),
],
dependencies: [],
dependencies: [
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.2"),
],
targets: [
.target(
name: "ComplexNumbers",
dependencies: []),
dependencies: [
.product(name: "Numerics", package: "swift-numerics"),
]),
.testTarget(
name: "ComplexNumbersTests",
dependencies: ["ComplexNumbers"]),
dependencies: ["ComplexNumbers",
.product(name: "Numerics", package: "swift-numerics"),]),
]
)
Loading

0 comments on commit 43d8ff9

Please sign in to comment.