Skip to content

Commit a9fc24e

Browse files
authored
Add Complex Numbers exercise (#528)
* Configured spec generator * Example solution * Add append for instructions to introduce closure-based OO and operator overloading
1 parent 8da3340 commit a9fc24e

File tree

10 files changed

+559
-0
lines changed

10 files changed

+559
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,14 @@
12851285
"control_flow_loops"
12861286
]
12871287
},
1288+
{
1289+
"slug": "complex-numbers",
1290+
"name": "Complex Numbers",
1291+
"uuid": "ed6b5073-e3b1-4787-8551-ffffa9e7313d",
1292+
"practices": [],
1293+
"prerequisites": [],
1294+
"difficulty": 6
1295+
},
12881296
{
12891297
"slug": "minesweeper",
12901298
"name": "Minesweeper",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This exercise requires you to use Closure-based objects. You can read more about this approach to object orientation in Lua [in this article][oot].
2+
3+
This exercise also requires you to use operator overloading via metatable metamethods. You can read more about this in [Programming in Lua][am].
4+
5+
[oot]: http://lua-users.org/wiki/ObjectOrientationTutorial
6+
[am]: https://www.lua.org/pil/13.1.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Instructions
2+
3+
A **complex number** is expressed in the form `z = a + b * i`, where:
4+
5+
- `a` is the **real part** (a real number),
6+
7+
- `b` is the **imaginary part** (also a real number), and
8+
9+
- `i` is the **imaginary unit** satisfying `i^2 = -1`.
10+
11+
## Operations on Complex Numbers
12+
13+
### Conjugate
14+
15+
The conjugate of the complex number `z = a + b * i` is given by:
16+
17+
```text
18+
zc = a - b * i
19+
```
20+
21+
### Absolute Value
22+
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"ryanplusplus"
4+
],
5+
"files": {
6+
"solution": [
7+
"complex-numbers.lua"
8+
],
9+
"test": [
10+
"complex-numbers_spec.lua"
11+
],
12+
"example": [
13+
".meta/example.lua"
14+
]
15+
},
16+
"blurb": "Implement complex numbers.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Complex_number"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
local Complex
2+
3+
local function close_to(a, b)
4+
return math.abs(a - b) < 1e-15
5+
end
6+
7+
local function eq(c1, c2)
8+
return close_to(c1.r, c2.r) and close_to(c1.i, c2.i)
9+
end
10+
11+
local function add(c1, c2)
12+
return Complex(c1.r + c2.r, c1.i + c2.i)
13+
end
14+
15+
local function sub(c1, c2)
16+
return Complex(c1.r - c2.r, c1.i - c2.i)
17+
end
18+
19+
local function mul(c1, c2)
20+
return Complex(c1.r * c2.r - c1.i * c2.i, c1.r * c2.i + c1.i * c2.r)
21+
end
22+
23+
local function div(c1, c2)
24+
local d = c2.r * c2.r + c2.i * c2.i
25+
return Complex((c1.r * c2.r + c1.i * c2.i) / d, (c1.i * c2.r - c1.r * c2.i) / d)
26+
end
27+
28+
Complex = function(r, i)
29+
local c = { r = r, i = i or 0 }
30+
31+
function c.abs()
32+
return math.sqrt(c.r * c.r + c.i * c.i)
33+
end
34+
35+
function c.conj()
36+
return Complex(c.r, -c.i)
37+
end
38+
39+
function c.exp()
40+
local e = math.exp(c.r)
41+
return Complex(e * math.cos(c.i), e * math.sin(c.i))
42+
end
43+
44+
return setmetatable(c, { __eq = eq, __add = add, __sub = sub, __mul = mul, __div = div })
45+
end
46+
47+
return Complex
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local unary_operator = { real = 'r', imaginary = 'i', abs = 'abs()', conjugate = 'conj()', exp = 'exp()' }
2+
3+
local binary_operator = { sub = '-', add = '+', mul = '*', div = '/' }
4+
5+
local function complex(input)
6+
if type(input) ~= 'table' then
7+
input = { input }
8+
end
9+
return 'Complex(' .. table.concat(input, ', ') .. ')'
10+
end
11+
12+
local function scalar(input)
13+
return tostring(input)
14+
end
15+
16+
local unary_result_type = { real = scalar, imaginary = scalar, abs = scalar, conjugate = complex, exp = complex }
17+
18+
return {
19+
module_name = 'Complex',
20+
21+
test_helpers = [[
22+
local pi = math.pi
23+
local e = math.exp(1)
24+
local ln = math.log
25+
]],
26+
27+
generate_test = function(case)
28+
if case.input.z then
29+
local template = [[
30+
assert.equal(%s, %s.%s)]]
31+
32+
return template:format(unary_result_type[case.property](case.expected), complex(case.input.z),
33+
unary_operator[case.property])
34+
else
35+
local template = [[
36+
assert.equal(%s, %s %s %s)]]
37+
38+
return template:format(complex(case.expected), complex(case.input.z1), binary_operator[case.property],
39+
complex(case.input.z2))
40+
end
41+
end
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[9f98e133-eb7f-45b0-9676-cce001cd6f7a]
13+
description = "Real part -> Real part of a purely real number"
14+
15+
[07988e20-f287-4bb7-90cf-b32c4bffe0f3]
16+
description = "Real part -> Real part of a purely imaginary number"
17+
18+
[4a370e86-939e-43de-a895-a00ca32da60a]
19+
description = "Real part -> Real part of a number with real and imaginary part"
20+
21+
[9b3fddef-4c12-4a99-b8f8-e3a42c7ccef6]
22+
description = "Imaginary part -> Imaginary part of a purely real number"
23+
24+
[a8dafedd-535a-4ed3-8a39-fda103a2b01e]
25+
description = "Imaginary part -> Imaginary part of a purely imaginary number"
26+
27+
[0f998f19-69ee-4c64-80ef-01b086feab80]
28+
description = "Imaginary part -> Imaginary part of a number with real and imaginary part"
29+
30+
[a39b7fd6-6527-492f-8c34-609d2c913879]
31+
description = "Imaginary unit"
32+
33+
[9a2c8de9-f068-4f6f-b41c-82232cc6c33e]
34+
description = "Arithmetic -> Addition -> Add purely real numbers"
35+
36+
[657c55e1-b14b-4ba7-bd5c-19db22b7d659]
37+
description = "Arithmetic -> Addition -> Add purely imaginary numbers"
38+
39+
[4e1395f5-572b-4ce8-bfa9-9a63056888da]
40+
description = "Arithmetic -> Addition -> Add numbers with real and imaginary part"
41+
42+
[1155dc45-e4f7-44b8-af34-a91aa431475d]
43+
description = "Arithmetic -> Subtraction -> Subtract purely real numbers"
44+
45+
[f95e9da8-acd5-4da4-ac7c-c861b02f774b]
46+
description = "Arithmetic -> Subtraction -> Subtract purely imaginary numbers"
47+
48+
[f876feb1-f9d1-4d34-b067-b599a8746400]
49+
description = "Arithmetic -> Subtraction -> Subtract numbers with real and imaginary part"
50+
51+
[8a0366c0-9e16-431f-9fd7-40ac46ff4ec4]
52+
description = "Arithmetic -> Multiplication -> Multiply purely real numbers"
53+
54+
[e560ed2b-0b80-4b4f-90f2-63cefc911aaf]
55+
description = "Arithmetic -> Multiplication -> Multiply purely imaginary numbers"
56+
57+
[4d1d10f0-f8d4-48a0-b1d0-f284ada567e6]
58+
description = "Arithmetic -> Multiplication -> Multiply numbers with real and imaginary part"
59+
60+
[b0571ddb-9045-412b-9c15-cd1d816d36c1]
61+
description = "Arithmetic -> Division -> Divide purely real numbers"
62+
63+
[5bb4c7e4-9934-4237-93cc-5780764fdbdd]
64+
description = "Arithmetic -> Division -> Divide purely imaginary numbers"
65+
66+
[c4e7fef5-64ac-4537-91c2-c6529707701f]
67+
description = "Arithmetic -> Division -> Divide numbers with real and imaginary part"
68+
69+
[c56a7332-aad2-4437-83a0-b3580ecee843]
70+
description = "Absolute value -> Absolute value of a positive purely real number"
71+
72+
[cf88d7d3-ee74-4f4e-8a88-a1b0090ecb0c]
73+
description = "Absolute value -> Absolute value of a negative purely real number"
74+
75+
[bbe26568-86c1-4bb4-ba7a-da5697e2b994]
76+
description = "Absolute value -> Absolute value of a purely imaginary number with positive imaginary part"
77+
78+
[3b48233d-468e-4276-9f59-70f4ca1f26f3]
79+
description = "Absolute value -> Absolute value of a purely imaginary number with negative imaginary part"
80+
81+
[fe400a9f-aa22-4b49-af92-51e0f5a2a6d3]
82+
description = "Absolute value -> Absolute value of a number with real and imaginary part"
83+
84+
[fb2d0792-e55a-4484-9443-df1eddfc84a2]
85+
description = "Complex conjugate -> Conjugate a purely real number"
86+
87+
[e37fe7ac-a968-4694-a460-66cb605f8691]
88+
description = "Complex conjugate -> Conjugate a purely imaginary number"
89+
90+
[f7704498-d0be-4192-aaf5-a1f3a7f43e68]
91+
description = "Complex conjugate -> Conjugate a number with real and imaginary part"
92+
93+
[6d96d4c6-2edb-445b-94a2-7de6d4caaf60]
94+
description = "Complex exponential function -> Euler's identity/formula"
95+
96+
[2d2c05a0-4038-4427-a24d-72f6624aa45f]
97+
description = "Complex exponential function -> Exponential of 0"
98+
99+
[ed87f1bd-b187-45d6-8ece-7e331232c809]
100+
description = "Complex exponential function -> Exponential of a purely real number"
101+
102+
[08eedacc-5a95-44fc-8789-1547b27a8702]
103+
description = "Complex exponential function -> Exponential of a number with real and imaginary part"
104+
105+
[d2de4375-7537-479a-aa0e-d474f4f09859]
106+
description = "Complex exponential function -> Exponential resulting in a number with real and imaginary part"
107+
108+
[06d793bf-73bd-4b02-b015-3030b2c952ec]
109+
description = "Operations between real numbers and complex numbers -> Add real number to complex number"
110+
111+
[d77dbbdf-b8df-43f6-a58d-3acb96765328]
112+
description = "Operations between real numbers and complex numbers -> Add complex number to real number"
113+
114+
[20432c8e-8960-4c40-ba83-c9d910ff0a0f]
115+
description = "Operations between real numbers and complex numbers -> Subtract real number from complex number"
116+
117+
[b4b38c85-e1bf-437d-b04d-49bba6e55000]
118+
description = "Operations between real numbers and complex numbers -> Subtract complex number from real number"
119+
120+
[dabe1c8c-b8f4-44dd-879d-37d77c4d06bd]
121+
description = "Operations between real numbers and complex numbers -> Multiply complex number by real number"
122+
123+
[6c81b8c8-9851-46f0-9de5-d96d314c3a28]
124+
description = "Operations between real numbers and complex numbers -> Multiply real number by complex number"
125+
126+
[8a400f75-710e-4d0c-bcb4-5e5a00c78aa0]
127+
description = "Operations between real numbers and complex numbers -> Divide complex number by real number"
128+
129+
[9a867d1b-d736-4c41-a41e-90bd148e9d5e]
130+
description = "Operations between real numbers and complex numbers -> Divide real number by complex number"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local Complex
2+
3+
Complex = function(r, i)
4+
local c = {
5+
--
6+
}
7+
8+
return setmetatable(c, {
9+
--
10+
})
11+
end
12+
13+
return Complex

0 commit comments

Comments
 (0)