def test_add(self): # Test 1 result = complex.add([5, 2], [3, 1]) self.assertEqual(result, [8, 3]) # Test 2 result = complex.add([5, -3], [-4, 2]) self.assertEqual(result, [1, -1]) # Test 3 result = complex.add([-6, 8], [-12, -9]) self.assertEqual(result, [-18, -1])
def add(v1, v2): """ Adds two complex vectors :Array c1: Array of n items, each item is a complex number :Array c2: Array of n items, each item is a complex number :return Array: """ ans_vector = [] for i in range(len(v1)): ans_vector.append(complex.add(v1[i], v2[i])) return ans_vector
def add_mat(m1, m2): """ Adds two complex matrices :Array m1: Matrix of n*m dimentions, each item is a complex number :Array m2: Matrix of n*m dimentions, each item is a complex number :return Array: """ ans = [] for i in range(len(m1)): row = [] for j in range(len(m1[i])): row.append(complex.add(m1[i][j], m2[i][j])) ans.append(row) return ans
def dot_product(v1, v2): """ Function that returns the dot product between two vectors :Array v1: Array of n items, each item is a complex number :Array v2: Array of n items, each item is a complex number :return Array: """ if len(v1) == len(v2): ans = [0, 0] for i in range(len(v1)): ans = complex.add(ans, complex.mult(v1[i], v2[i])) return ans else: return 'Not possible'
def mult_mat(m1, m2): """ Function that multiplies two matrices :Array m1: Array of n*m items, each item is a complex number :Array m2: Array of n*m items, each item is a complex number :return Array: """ if len(m1[0]) == len(m2): ans = [[[0, 0] for j in range(len(m2[0]))] for i in range(len(m1))] for i in range(len(m1)): for j in range(len(m2[0])): for k in range(len(m1[0])): ans[i][j] = complex.add(ans[i][j], complex.mult(m1[i][k], m2[k][j])) return ans else: return 'Not possible'
def main(): # Create two random complex numbers c1 = [random.random(), random.random()] c2 = [random.random(), random.random()] # Print out both numbers print("c1 = ", c1) print("c2 = ", c2) # Test unary functions from complex.py: # conjugation, modulus (squared), scaling print("conj(c2) = ", cplx.conj(c2)) print("|c2|^2 = ", cplx.norm_sq(c2)) print("|c2| = ", cplx.norm(c2)) print("3*c2 = ", cplx.scale(c2, 3.0)) # Test binary functions from complex.py: # addition, subtraction, multiplication, division print("c1+c2 = ", cplx.add(c1, c2)) print("c1-c2 = ", cplx.sub(c1, c2)) print("c1*c2 = ", cplx.mul(c1, c2)) print("c1/c2 = ", cplx.div(c1, c2))