complex.py
Go to the documentation of this file.
1 import sophus
2 import sympy
3 import sys
4 import unittest
5 
6 
7 class Complex:
8  """ Complex class """
9 
10  def __init__(self, real, imag):
11  self.real = real
12  self.imag = imag
13 
14  def __mul__(self, right):
15  """ complex multiplication """
16  return Complex(self.real * right.real - self.imag * right.imag,
17  self.imag * right.real + self.real * right.imag)
18 
19  def __add__(self, right):
20  return Complex(elf.real + right.real, self.imag + right.imag)
21 
22  def __neg__(self):
23  return Complex(-self.real, -self.image)
24 
25  def __truediv__(self, scalar):
26  """ scalar division """
27  return Complex(self.real / scalar, self.imag / scalar)
28 
29  def __repr__(self):
30  return "( " + repr(self.real) + " + " + repr(self.imag) + "i )"
31 
32  def __getitem__(self, key):
33  """ We use the following convention [real, imag] """
34  if key == 0:
35  return self.real
36  else:
37  return self.imag
38 
39  def squared_norm(self):
40  """ squared norm when considering the complex number as tuple """
41  return self.real**2 + self.imag**2
42 
43  def conj(self):
44  """ complex conjugate """
45  return Complex(self.real, -self.imag)
46 
47  def inv(self):
48  """ complex inverse """
49  return self.conj() / self.squared_norm()
50 
51  @staticmethod
52  def identity():
53  return Complex(1, 0)
54 
55  @staticmethod
56  def zero():
57  return Complex(0, 0)
58 
59  def __eq__(self, other):
60  if isinstance(self, other.__class__):
61  return self.real == other.real and self.imag == other.imag
62  return False
63 
64  def subs(self, x, y):
65  return Complex(self.real.subs(x, y), self.imag.subs(x, y))
66 
67  def simplify(self):
68  return Complex(sympy.simplify(self.real),
69  sympy.simplify(self.imag))
70 
71  @staticmethod
72  def Da_a_mul_b(a, b):
73  """ derivatice of complex muliplication wrt left multiplier a """
74  return sympy.Matrix([[b.real, -b.imag],
75  [b.imag, b.real]])
76 
77  @staticmethod
78  def Db_a_mul_b(a, b):
79  """ derivatice of complex muliplication wrt right multiplicand b """
80  return sympy.Matrix([[a.real, -a.imag],
81  [a.imag, a.real]])
82 
83 
84 class TestComplex(unittest.TestCase):
85  def setUp(self):
86  x, y = sympy.symbols('x y', real=True)
87  u, v = sympy.symbols('u v', real=True)
88  self.a = Complex(x, y)
89  self.b = Complex(u, v)
90 
92  product = self.a * self.a.inv()
93  self.assertEqual(product.simplify(),
94  Complex.identity())
95  product = self.a.inv() * self.a
96  self.assertEqual(product.simplify(),
97  Complex.identity())
98 
99  def test_derivatives(self):
100  d = sympy.Matrix(2, 2, lambda r, c: sympy.diff(
101  (self.a * self.b)[r], self.a[c]))
102  self.assertEqual(d,
103  Complex.Da_a_mul_b(self.a, self.b))
104  d = sympy.Matrix(2, 2, lambda r, c: sympy.diff(
105  (self.a * self.b)[r], self.b[c]))
106  self.assertEqual(d,
107  Complex.Db_a_mul_b(self.a, self.b))
108 
109 
110 if __name__ == '__main__':
111  unittest.main()
112  print('hello')
sophus.complex.Complex.subs
def subs(self, x, y)
Definition: complex.py:64
sophus.complex.TestComplex
Definition: complex.py:84
sophus.complex.Complex.__truediv__
def __truediv__(self, scalar)
Definition: complex.py:25
sophus.complex.TestComplex.a
a
Definition: complex.py:88
sophus.complex.Complex.__mul__
def __mul__(self, right)
Definition: complex.py:14
sophus.complex.Complex.squared_norm
def squared_norm(self)
Definition: complex.py:39
sophus.complex.Complex.__repr__
def __repr__(self)
Definition: complex.py:29
sophus.complex.Complex.inv
def inv(self)
Definition: complex.py:47
sophus.complex.Complex.identity
def identity()
Definition: complex.py:52
sophus.complex.Complex.__eq__
def __eq__(self, other)
Definition: complex.py:59
sophus.complex.TestComplex.test_muliplications
def test_muliplications(self)
Definition: complex.py:91
sophus.complex.Complex.__getitem__
def __getitem__(self, key)
Definition: complex.py:32
sophus.complex.Complex.imag
imag
Definition: complex.py:12
sophus.complex.TestComplex.b
b
Definition: complex.py:89
sophus.complex.Complex.__add__
def __add__(self, right)
Definition: complex.py:19
sophus.complex.Complex.zero
def zero()
Definition: complex.py:56
sophus.complex.Complex.__init__
def __init__(self, real, imag)
Definition: complex.py:10
sophus.complex.Complex
Definition: complex.py:7
sophus.complex.Complex.Db_a_mul_b
def Db_a_mul_b(a, b)
Definition: complex.py:78
sophus.complex.Complex.conj
def conj(self)
Definition: complex.py:43
sophus.complex.Complex.real
real
Definition: complex.py:11
sophus.complex.TestComplex.test_derivatives
def test_derivatives(self)
Definition: complex.py:99
sophus.complex.Complex.__neg__
def __neg__(self)
Definition: complex.py:22
sophus.complex.TestComplex.setUp
def setUp(self)
Definition: complex.py:85
sophus.complex.Complex.Da_a_mul_b
def Da_a_mul_b(a, b)
Definition: complex.py:72
sophus.complex.Complex.simplify
def simplify(self)
Definition: complex.py:67


sophus
Author(s): Hauke Strasdat
autogenerated on Wed Mar 2 2022 01:01:47