-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Swift tools version to 6.0 and add new Position struct for sad…
…dle points exercise
- Loading branch information
1 parent
30f8e4c
commit 43d8ff9
Showing
21 changed files
with
1,360 additions
and
1,049 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
exercises/practice/complex-numbers/.docs/instructions.append.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
107
exercises/practice/complex-numbers/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
73 changes: 29 additions & 44 deletions
73
exercises/practice/complex-numbers/.meta/Sources/ComplexNumbers/ComplexNumbersExample.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"authors": [ | ||
"AlwynC" | ||
"AlwynC", | ||
"meatball133" | ||
], | ||
"contributors": [ | ||
"bhargavg", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -%} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.