Skip to content

Commit 5152406

Browse files
authored
add complex-numbers (#59)
1 parent 14ea5e5 commit 5152406

File tree

15 files changed

+813
-0
lines changed

15 files changed

+813
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,14 @@
377377
"prerequisites": [],
378378
"difficulty": 6
379379
},
380+
{
381+
"slug": "complex-numbers",
382+
"name": "Complex Numbers",
383+
"uuid": "5860e836-932b-4eb6-9482-c1ca8bbbce02",
384+
"practices": [],
385+
"prerequisites": [],
386+
"difficulty": 6
387+
},
380388
{
381389
"slug": "knapsack",
382390
"name": "Knapsack",
Lines changed: 100 additions & 0 deletions
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
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace ComplexNumbers
2+
3+
structure ComplexNumber where
4+
real : Float
5+
imag : Float
6+
deriving Repr
7+
8+
/- define equality between two complex numbers -/
9+
instance : BEq ComplexNumber where
10+
beq x y := Float.abs (x.real - y.real) <= 0.001 &&
11+
Float.abs (x.imag - y.imag) <= 0.001
12+
13+
/- define how a complex number should be constructed out of a literal number -/
14+
instance {n : Nat} : OfNat ComplexNumber n where
15+
ofNat := { real := n.toFloat, imag := 0.0 }
16+
17+
def real (z : ComplexNumber) : Float :=
18+
z.real
19+
20+
def imaginary (z : ComplexNumber) : Float :=
21+
z.imag
22+
23+
def mul (z1 : ComplexNumber) (z2 : ComplexNumber) : ComplexNumber :=
24+
{ real := z1.real * z2.real - z1.imag * z2.imag,
25+
imag := z1.imag * z2.real + z1.real * z2.imag }
26+
27+
def div (z1 : ComplexNumber) (z2 : ComplexNumber) : ComplexNumber :=
28+
let square := z2.real ^ 2 + z2.imag ^ 2
29+
{ real := (z1.real * z2.real + z1.imag * z2.imag) / square,
30+
imag := (z1.imag * z2.real - z1.real * z2.imag) / square }
31+
32+
def sub (z1 : ComplexNumber) (z2 : ComplexNumber) : ComplexNumber :=
33+
{ real := z1.real - z2.real, imag := z1.imag - z2.imag }
34+
35+
def add (z1 : ComplexNumber) (z2 : ComplexNumber) : ComplexNumber :=
36+
{ real := z1.real + z2.real, imag := z1.imag + z2.imag }
37+
38+
def abs (z : ComplexNumber) : Float :=
39+
Float.sqrt (z.real ^ 2 + z.imag ^ 2)
40+
41+
def conjugate (z : ComplexNumber) : ComplexNumber :=
42+
{ z with imag := -z.imag }
43+
44+
def exp (z : ComplexNumber) : ComplexNumber :=
45+
let expReal := Float.exp z.real
46+
{ real := expReal * Float.cos z.imag,
47+
imag := expReal * Float.sin z.imag }
48+
49+
end ComplexNumbers
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"oxe-i"
4+
],
5+
"files": {
6+
"solution": [
7+
"ComplexNumbers.lean"
8+
],
9+
"test": [
10+
"ComplexNumbersTest.lean"
11+
],
12+
"example": [
13+
".meta/Example.lean"
14+
]
15+
},
16+
"blurb": "Implement complex numbers.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Complex_number"
19+
}
Lines changed: 130 additions & 0 deletions
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"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace ComplexNumbers
2+
3+
structure ComplexNumber where
4+
real : Float
5+
imag : Float
6+
deriving Repr
7+
8+
/- define equality between two complex numbers -/
9+
instance : BEq ComplexNumber where
10+
beq x y := sorry
11+
12+
/- define how a complex number should be constructed out of a literal number -/
13+
instance {n : Nat} : OfNat ComplexNumber n where
14+
ofNat := sorry
15+
16+
def real (z : ComplexNumber) : Float :=
17+
sorry
18+
19+
def imaginary (z : ComplexNumber) : Float :=
20+
sorry
21+
22+
def mul (z1 : ComplexNumber) (z2 : ComplexNumber) : ComplexNumber :=
23+
sorry
24+
25+
def div (z1 : ComplexNumber) (z2 : ComplexNumber) : ComplexNumber :=
26+
sorry
27+
28+
def sub (z1 : ComplexNumber) (z2 : ComplexNumber) : ComplexNumber :=
29+
sorry
30+
31+
def add (z1 : ComplexNumber) (z2 : ComplexNumber) : ComplexNumber :=
32+
sorry
33+
34+
def abs (z : ComplexNumber) : Float :=
35+
sorry
36+
37+
def conjugate (z : ComplexNumber) : ComplexNumber :=
38+
sorry
39+
40+
def exp (z : ComplexNumber) : ComplexNumber :=
41+
sorry
42+
43+
end ComplexNumbers

0 commit comments

Comments
 (0)