Skip to content

Commit efd211b

Browse files
authored
[#765] New practice exercise complex-numbers (#769)
1 parent 843aa80 commit efd211b

File tree

11 files changed

+657
-0
lines changed

11 files changed

+657
-0
lines changed

config.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,22 @@
24302430
],
24312431
"difficulty": 4
24322432
},
2433+
{
2434+
"slug": "complex-numbers",
2435+
"name": "Complex Numbers",
2436+
"uuid": "0cc86a92-7118-4cb9-8417-db4557baf364",
2437+
"prerequisites": [
2438+
"pattern-matching",
2439+
"erlang-libraries",
2440+
"floating-point-numbers",
2441+
"tuples"
2442+
],
2443+
"practices": [
2444+
"erlang-libraries",
2445+
"floating-point-numbers"
2446+
],
2447+
"difficulty": 4
2448+
},
24332449
{
24342450
"slug": "yacht",
24352451
"name": "Yacht",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
In this exercise, complex numbers are represented as a tuple-pair containing the real and imaginary parts.
2+
3+
For example, the real number `1` is `{1, 0}`, the imaginary number `i` is `{0, 1}` and the complex number `4+3i` is `{4, 3}'.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Description
2+
3+
A complex number is a number in the form `a + b * i` where `a` and `b` are real and `i` satisfies `i^2 = -1`.
4+
5+
`a` is called the real part and `b` is called the imaginary part of `z`.
6+
The conjugate of the number `a + b * i` is the number `a - b * i`.
7+
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.
8+
9+
The sum/difference of two complex numbers involves adding/subtracting their real and imaginary parts separately:
10+
`(a + i * b) + (c + i * d) = (a + c) + (b + d) * i`,
11+
`(a + i * b) - (c + i * d) = (a - c) + (b - d) * i`.
12+
13+
Multiplication result is by definition
14+
`(a + i * b) * (c + i * d) = (a * c - b * d) + (b * c + a * d) * i`.
15+
16+
The reciprocal of a non-zero complex number is
17+
`1 / (a + i * b) = a/(a^2 + b^2) - b/(a^2 + b^2) * i`.
18+
19+
Dividing a complex number `a + i * b` by another `c + i * d` gives:
20+
`(a + i * b) / (c + i * d) = (a * c + b * d)/(c^2 + d^2) + (b * c - a * d)/(c^2 + d^2) * i`.
21+
22+
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)`.
23+
24+
Implement the following operations:
25+
- addition, subtraction, multiplication and division of two complex numbers,
26+
- conjugate, absolute value, exponent of a given complex number.
27+
28+
29+
Assume the programming language you are using does not have an implementation of complex numbers.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"blurb": "Implement complex numbers.",
3+
"authors": [
4+
"jiegillet"
5+
],
6+
"contributors": [
7+
],
8+
"files": {
9+
"solution": [
10+
"lib/complex_numbers.ex"
11+
],
12+
"test": [
13+
"test/complex_numbers_test.exs"
14+
],
15+
"example": [
16+
".meta/example.ex"
17+
]
18+
},
19+
"source": "Wikipedia",
20+
"source_url": "https://en.wikipedia.org/wiki/Complex_number"
21+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
defmodule ComplexNumbers do
2+
@typedoc """
3+
In this module, complex numbers are represented as a tuple-pair containing the real and
4+
imaginary parts.
5+
For example, the real number `1` is `{1, 0}`, the imaginary number `i` is `{0, 1}` and
6+
the complex number `4+3i` is `{4, 3}'.
7+
"""
8+
@type complex :: {float, float}
9+
10+
@doc """
11+
Return the real part of a complex number
12+
"""
13+
@spec real(a :: complex) :: float
14+
def real({real, _im}), do: real
15+
16+
@doc """
17+
Return the imaginary part of a complex number
18+
"""
19+
@spec imaginary(a :: complex) :: float
20+
def imaginary({_real, im}), do: im
21+
22+
@doc """
23+
Multiply two complex numbers
24+
"""
25+
@spec mul(a :: complex, b :: complex) :: complex
26+
def mul({a, b}, {c, d}), do: {a * c - b * d, b * c + a * d}
27+
28+
@doc """
29+
Add two complex numbers
30+
"""
31+
@spec add(a :: complex, b :: complex) :: complex
32+
def add({a, b}, {c, d}), do: {a + c, b + d}
33+
34+
@doc """
35+
Subtract two complex numbers
36+
"""
37+
@spec sub(a :: complex, b :: complex) :: complex
38+
def sub({a, b}, {c, d}), do: {a - c, b - d}
39+
40+
@doc """
41+
Divide two complex numbers
42+
"""
43+
@spec div(a :: complex, b :: complex) :: complex
44+
def div({a, b}, {c, d}),
45+
do: {(a * c + b * d) / (c * c + d * d), (b * c - a * d) / (c * c + d * d)}
46+
47+
@doc """
48+
Absolute value of a complex number
49+
"""
50+
@spec abs(a :: complex) :: float
51+
def abs({a, b}), do: :math.sqrt(a * a + b * b)
52+
53+
@doc """
54+
Conjugate of a complex number
55+
"""
56+
@spec conjugate(a :: complex) :: complex
57+
def conjugate({a, b}), do: {a, -b}
58+
59+
@doc """
60+
Exponential of a complex number
61+
"""
62+
@spec exp(a :: complex) :: complex
63+
def exp({a, b}), do: {:math.exp(a) * :math.cos(b), -:math.exp(a) * :math.sin(b)}
64+
end
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[9f98e133-eb7f-45b0-9676-cce001cd6f7a]
6+
description = "Real part of a purely real number"
7+
8+
[07988e20-f287-4bb7-90cf-b32c4bffe0f3]
9+
description = "Real part of a purely imaginary number"
10+
11+
[4a370e86-939e-43de-a895-a00ca32da60a]
12+
description = "Real part of a number with real and imaginary part"
13+
14+
[9b3fddef-4c12-4a99-b8f8-e3a42c7ccef6]
15+
description = "Imaginary part of a purely real number"
16+
17+
[a8dafedd-535a-4ed3-8a39-fda103a2b01e]
18+
description = "Imaginary part of a purely imaginary number"
19+
20+
[0f998f19-69ee-4c64-80ef-01b086feab80]
21+
description = "Imaginary part of a number with real and imaginary part"
22+
23+
[a39b7fd6-6527-492f-8c34-609d2c913879]
24+
description = "Imaginary unit"
25+
26+
[9a2c8de9-f068-4f6f-b41c-82232cc6c33e]
27+
description = "Add purely real numbers"
28+
29+
[657c55e1-b14b-4ba7-bd5c-19db22b7d659]
30+
description = "Add purely imaginary numbers"
31+
32+
[4e1395f5-572b-4ce8-bfa9-9a63056888da]
33+
description = "Add numbers with real and imaginary part"
34+
35+
[1155dc45-e4f7-44b8-af34-a91aa431475d]
36+
description = "Subtract purely real numbers"
37+
38+
[f95e9da8-acd5-4da4-ac7c-c861b02f774b]
39+
description = "Subtract purely imaginary numbers"
40+
41+
[f876feb1-f9d1-4d34-b067-b599a8746400]
42+
description = "Subtract numbers with real and imaginary part"
43+
44+
[8a0366c0-9e16-431f-9fd7-40ac46ff4ec4]
45+
description = "Multiply purely real numbers"
46+
47+
[e560ed2b-0b80-4b4f-90f2-63cefc911aaf]
48+
description = "Multiply purely imaginary numbers"
49+
50+
[4d1d10f0-f8d4-48a0-b1d0-f284ada567e6]
51+
description = "Multiply numbers with real and imaginary part"
52+
53+
[b0571ddb-9045-412b-9c15-cd1d816d36c1]
54+
description = "Divide purely real numbers"
55+
56+
[5bb4c7e4-9934-4237-93cc-5780764fdbdd]
57+
description = "Divide purely imaginary numbers"
58+
59+
[c4e7fef5-64ac-4537-91c2-c6529707701f]
60+
description = "Divide numbers with real and imaginary part"
61+
62+
[c56a7332-aad2-4437-83a0-b3580ecee843]
63+
description = "Absolute value of a positive purely real number"
64+
65+
[cf88d7d3-ee74-4f4e-8a88-a1b0090ecb0c]
66+
description = "Absolute value of a negative purely real number"
67+
68+
[bbe26568-86c1-4bb4-ba7a-da5697e2b994]
69+
description = "Absolute value of a purely imaginary number with positive imaginary part"
70+
71+
[3b48233d-468e-4276-9f59-70f4ca1f26f3]
72+
description = "Absolute value of a purely imaginary number with negative imaginary part"
73+
74+
[fe400a9f-aa22-4b49-af92-51e0f5a2a6d3]
75+
description = "Absolute value of a number with real and imaginary part"
76+
77+
[fb2d0792-e55a-4484-9443-df1eddfc84a2]
78+
description = "Conjugate a purely real number"
79+
80+
[e37fe7ac-a968-4694-a460-66cb605f8691]
81+
description = "Conjugate a purely imaginary number"
82+
83+
[f7704498-d0be-4192-aaf5-a1f3a7f43e68]
84+
description = "Conjugate a number with real and imaginary part"
85+
86+
[6d96d4c6-2edb-445b-94a2-7de6d4caaf60]
87+
description = "Euler's identity/formula"
88+
89+
[2d2c05a0-4038-4427-a24d-72f6624aa45f]
90+
description = "Exponential of 0"
91+
92+
[ed87f1bd-b187-45d6-8ece-7e331232c809]
93+
description = "Exponential of a purely real number"
94+
95+
[08eedacc-5a95-44fc-8789-1547b27a8702]
96+
description = "Exponential of a number with real and imaginary part"
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
defmodule ComplexNumbers do
2+
@typedoc """
3+
In this module, complex numbers are represented as a tuple-pair containing the real and
4+
imaginary parts.
5+
For example, the real number `1` is `{1, 0}`, the imaginary number `i` is `{0, 1}` and
6+
the complex number `4+3i` is `{4, 3}'.
7+
"""
8+
@type complex :: {float, float}
9+
10+
@doc """
11+
Return the real part of a complex number
12+
"""
13+
@spec real(a :: complex) :: float
14+
def real(a) do
15+
end
16+
17+
@doc """
18+
Return the imaginary part of a complex number
19+
"""
20+
@spec imaginary(a :: complex) :: float
21+
def imaginary(a) do
22+
end
23+
24+
@doc """
25+
Multiply two complex numbers
26+
"""
27+
@spec mul(a :: complex, b :: complex) :: complex
28+
def mul(a, b) do
29+
end
30+
31+
@doc """
32+
Add two complex numbers
33+
"""
34+
@spec add(a :: complex, b :: complex) :: complex
35+
def add(a, b) do
36+
end
37+
38+
@doc """
39+
Subtract two complex numbers
40+
"""
41+
@spec sub(a :: complex, b :: complex) :: complex
42+
def sub(a, b) do
43+
end
44+
45+
@doc """
46+
Divide two complex numbers
47+
"""
48+
@spec div(a :: complex, b :: complex) :: complex
49+
def div(a, b) do
50+
end
51+
52+
@doc """
53+
Absolute value of a complex number
54+
"""
55+
@spec abs(a :: complex) :: float
56+
def abs(a) do
57+
end
58+
59+
@doc """
60+
Conjugate of a complex number
61+
"""
62+
@spec conjugate(a :: complex) :: complex
63+
def conjugate(a) do
64+
end
65+
66+
@doc """
67+
Exponential of a complex number
68+
"""
69+
@spec exp(a :: complex) :: complex
70+
def exp(a) do
71+
end
72+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule ComplexNumbers.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :complex_numbers,
7+
version: "0.1.0",
8+
# elixir: "~> 1.8",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger]
18+
]
19+
end
20+
21+
# Run "mix help deps" to learn about dependencies.
22+
defp deps do
23+
[
24+
# {:dep_from_hexpm, "~> 0.3.0"},
25+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
26+
]
27+
end
28+
end

0 commit comments

Comments
 (0)