-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
[Swift 6]: Update Exercises batch 22 #816
base: main
Are you sure you want to change the base?
Changes from all commits
43d8ff9
93a1df0
74f1025
e2306ac
31c31b7
9924cb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is confusing. Does it mean the library is imported but the student needs to reference it in their code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The library is included in the project config file. But you also have to import it the file you are working in. |
||||||
|
||||||
You are aLso free to implement your own method to compare the two complex numbers. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
[swift-numberics]: https://github.com/apple/swift-numerics |
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,60 @@ | ||
import Foundation | ||
|
||
struct ComplexNumber { | ||
|
||
var realComponent: Double | ||
|
||
var imaginaryComponent: Double | ||
|
||
func getRealComponent() -> Double { | ||
|
||
return self.realComponent | ||
} | ||
|
||
func getImaginaryComponent() -> Double { | ||
|
||
return self.imaginaryComponent | ||
} | ||
|
||
func add(complexNumber: ComplexNumber) -> ComplexNumber { | ||
|
||
return ComplexNumber(realComponent: self.realComponent + complexNumber.realComponent, imaginaryComponent: self.imaginaryComponent + complexNumber.imaginaryComponent) | ||
|
||
} | ||
|
||
func subtract(complexNumber: ComplexNumber) -> ComplexNumber { | ||
|
||
return ComplexNumber(realComponent: self.realComponent - complexNumber.realComponent, imaginaryComponent: self.imaginaryComponent - complexNumber.imaginaryComponent) | ||
} | ||
|
||
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 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 conjugate() -> ComplexNumber { | ||
|
||
return ComplexNumber(realComponent: self.realComponent, imaginaryComponent: (-1 * self.imaginaryComponent)) | ||
} | ||
|
||
func absolute() -> Double { | ||
|
||
return sqrt(pow(self.realComponent, 2.0) + pow(self.imaginaryComponent, 2.0)) | ||
} | ||
|
||
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) | ||
|
||
} | ||
|
||
import Numerics | ||
|
||
struct ComplexNumbers: Equatable { | ||
|
||
var real: Double | ||
var imaginary: Double | ||
|
||
init(realComponent: Double, imaginaryComponent: Double? = 0) { | ||
real = realComponent | ||
imaginary = imaginaryComponent ?? 0 | ||
} | ||
|
||
func add(complexNumber: ComplexNumbers) -> ComplexNumbers { | ||
ComplexNumbers( | ||
realComponent: real + complexNumber.real, | ||
imaginaryComponent: imaginary + complexNumber.imaginary) | ||
} | ||
|
||
func sub(complexNumber: ComplexNumbers) -> ComplexNumbers { | ||
ComplexNumbers( | ||
realComponent: real - complexNumber.real, | ||
imaginaryComponent: imaginary - complexNumber.imaginary) | ||
} | ||
|
||
func mul(complexNumber: ComplexNumbers) -> ComplexNumbers { | ||
ComplexNumbers( | ||
realComponent: real * complexNumber.real - imaginary * complexNumber.imaginary, | ||
imaginaryComponent: real * complexNumber.imaginary + imaginary * complexNumber.real) | ||
} | ||
|
||
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 absolute() -> Double { | ||
sqrt(Double(real * real + imaginary * imaginary)) | ||
} | ||
|
||
func conjugate() -> ComplexNumbers { | ||
ComplexNumbers(realComponent: real, imaginaryComponent: -imaginary) | ||
} | ||
|
||
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) | ||
} | ||
|
||
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) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"authors": [ | ||
"AlwynC" | ||
"AlwynC", | ||
"meatball133" | ||
], | ||
"contributors": [ | ||
"bhargavg", | ||
|
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 -%} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.