-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplex.py
executable file
·82 lines (59 loc) · 1.44 KB
/
Complex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 25 18:53:47 2021
@author: maherme
"""
#%%
# You can define a complex number in different ways:
a = complex(1, 2)
print(a)
b = 1 + 2j
print(b)
print(a == b)
print(a.real, type(a.real)) # Notice real and imag part are float
print(a.imag, type(a.imag))
#%%
# You can use math operations:
print(a.conjugate())
print(a + b)
print(a * b)
print(a/b)
print(a ** 2)
#%%
# Operator // and % does not work:
a % 2 # All this operations will fail
a // 2
divmod(a, b)
#%%
# Notice we will have the same problems with precison due to the float:
a = 0.1j
print(format(a.imag, '.25f'))
print(a + a + a == 0.3j)
print(format((a+a+a).imag, '.25f'))
print(format((0.3j).imag, '.25f'))
#%%
# Exist a library cmath for complex numbers:
import cmath
import math
print(cmath.pi)
print(type(cmath.pi)) # Some items as pi will kept the same type
#%%
# There are functions than only works with imag number
a = 1 + 2j
print(cmath.sqrt(a))
math.sqrt(a) # This will not work
#%%
# cmath has some specific functions as phase or changing coordenate system
a = 1 + 1j
print(cmath.phase(a))
print(cmath.pi/4)
# rect function pass from polar to rectangular coord.
print(cmath.rect(math.sqrt(2), math.pi/4))
#%%
# Let's see e^(j*pi)+1 = 0
RHS = cmath.exp(cmath.pi * 1j) + 1
print(RHS)
print(cmath.isclose(RHS, 0)) # Notice this is false due to default tolerances
print(cmath.isclose(RHS, 0, abs_tol=0.0001))
#%%