Ejemplo n.º 1
0
class ComplexRepositoryTest(TestCase):
    repo = ComplexRepository()
    assert repo.size() == 0

    c1 = ComplexNumber(1, 2)
    repo.store(c1)
    assert repo.size() == 1
    repo.store(ComplexNumber(130, 23))
    assert repo.size() == 2
    repo.delete_everything()
    assert repo.size() == 0
 def test_multiply(self):
     complex = ComplexNumber(3, 4)
     multiply = complex.multiplyReal(3)
     self.assertEqual(multiply.getRealPart(), 9)
     self.assertEqual(multiply.getImagPart(), 12)
     multiply = complex.multiplyImag(3)
     self.assertEqual(multiply.getRealPart(), -12)
     self.assertEqual(multiply.getImagPart(), 9)
     complex.setRealPart(-3)
     complex.setImagPart(-4)
     multiply = complex.multiplyReal(3)
     self.assertEqual(multiply.getRealPart(), -9)
     self.assertEqual(multiply.getImagPart(), -12)
     multiply = complex.multiplyImag(3)
     self.assertEqual(multiply.getRealPart(), 12)
     self.assertEqual(multiply.getImagPart(), -9)
 def test_exponentialComplex(self):
     c1 = ComplexNumber(3, 4)
     c2 = c1.exponentialComplex()
     self.assertAlmostEqual(c2.getRealPart(), -13, 0)
     self.assertAlmostEqual(c2.getImagPart(), -15, 0)
     c2 = ComplexNumber(1, 0)
     c2 = c2.exponentialComplex()
     self.assertAlmostEqual(c2.getRealPart(), 2.7, 0)
     self.assertAlmostEqual(c2.getImagPart(), 0, 0)
 def test_squarerootComplex(self):
     c1 = ComplexNumber(3, 4)
     c2 = c1.squarerootComplex()
     self.assertEqual(c2.getRealPart(), 2)
     self.assertEqual(c2.getImagPart(), 1)
     c2 = ComplexNumber(1, 0)
     c2 = c2.squarerootComplex()
     self.assertEqual(c2.getRealPart(), 1)
     self.assertEqual(c2.getImagPart(), 0)
Ejemplo n.º 5
0
    def createNumber(self, real=0, imag=0):
        '''
        Creates an object Complex Number
        Input:The real part and the imaginary part of the number
        Output: the number created and stored into the repo
        '''

        c = ComplexNumber(real, imag)
        self.__repo.store(c)
 def test_multiplyNumbers(self):
     c1 = ComplexNumber(3, 4)
     c2 = ComplexNumber(1, 1)
     c3 = c1.multiplyNumbers(c2)
     self.assertEqual(c3.getRealPart(), -1)
     self.assertEqual(c3.getImagPart(), 7)
     c4 = ComplexNumber(-1, 2)
     c5 = ComplexNumber(2, -4)
     c6 = c4.multiplyNumbers(c5)
     self.assertEqual(c6.getRealPart(), 6)
     self.assertEqual(c6.getImagPart(), 8)
 def test_addNumbers(self):
     c1 = ComplexNumber(3, 4)
     c2 = ComplexNumber(1, 1)
     c3 = c1.addNumbers(c2)
     self.assertEqual(c3.getRealPart(), 4)
     self.assertEqual(c3.getImagPart(), 5)
     c4 = ComplexNumber(-1, 2)
     c5 = ComplexNumber(2, -4)
     c6 = c4.addNumbers(c5)
     self.assertEqual(c6.getRealPart(), 1)
     self.assertEqual(c6.getImagPart(), -2)
Ejemplo n.º 8
0
 def use9(self):
     '''
     We multiply two complex numbers
     '''
     c1 = ComplexNumber(user_input(), user_input())
     print('We multiply two complex numbers:')
     l = self.__repo.iterate()
     for i in range(len(l)):
         print('For the number : ', l[i])
         print("The result is: ", l[i].multiplyNumbers(c1))
         l[i] = l[i].multiplyNumbers(c1)
     print(".....................................................")
 def test_powerComplex(self):
     c1 = ComplexNumber(3, 4)
     c3 = c1.powerComplex(3)
     self.assertEqual(c3.getRealPart(), -84)
     self.assertEqual(c3.getImagPart(), 92)
     c2 = ComplexNumber(100, 32)
     c4 = c2.powerComplex(0)
     self.assertEqual(c4.getRealPart(), 1)
     self.assertEqual(c4.getImagPart(), 0)
     try:
         c1.powerComplex(-1)
         assert False
     except:
         assert True
Ejemplo n.º 10
0
 def read_nr(self, filename):
     '''
     We read the numbers from a file
     We split the text and we find the numbers using a regex
     '''
     try:
         filename = open(filename, 'r')
         c1 = filename.read()
         filename.close()
         c1 = [line.split() for line in c1.split('\n')]
         for item in c1:
             nr = re.findall('\d+', str(item))
             if len(nr) > 2:
                 raise IOError
             self.__repo.add_nr(ComplexNumber(nr[0], nr[1]))
     except IOError:
         print('Error while opening the file')
         print('Fix the error then try again')
         exit()
Ejemplo n.º 11
0
'''
Created on 27 Nov 2017

@author: tothi
'''
from Domain.Repo import ComplexRepository
from Controller.ComplexNumberController import ComplexNumberController
from UI.Menu import ComplexNumberMenu
from Domain.ComplexNumber import ComplexNumber


def start():
    repo = ComplexRepository()
    ctrl = ComplexNumberController(repo)
    ui = ComplexNumberMenu(ctrl)
    ui.menu()


#start()
c = ComplexNumber(5, 2)
print(c.exponentialComplex())
Ejemplo n.º 12
0
 def test_matrixRepresentation(self):
     c1 = ComplexNumber(3, 4)
     c2 = ComplexNumber(1, 1)
     self.assertEqual(c1.matrixComplex(), [[3, -4], [4, 3]])
     self.assertEqual(c2.matrixComplex(), [[1, -1], [1, 1]])
     c4 = ComplexNumber(-1, 2)
     c5 = ComplexNumber(2, -4)
     self.assertEqual(c4.matrixComplex(), [[-1, -2], [2, -1]])
     self.assertEqual(c5.matrixComplex(), [[2, 4], [-4, 2]])
Ejemplo n.º 13
0
 def test_conjugate(self):
     complex = ComplexNumber(3, 4)
     self.assertEqual(complex.getConjugate(), '3 -4i')
     complex.setImagPart(-4)
     self.assertEqual(complex.getConjugate(), '3 +4i')
Ejemplo n.º 14
0
 def test_modulus_argument_polar(self):
     complex = ComplexNumber(3, 4)
     self.assertEqual(complex.getModulus(), 5)
     self.assertAlmostEqual(complex.getArgument(), 53.13)
     self.assertEqual(complex.getPolarForm(), '5(cos(53.13) + isin(53.13))')
     complex.setRealPart(-4)
     complex.setImagPart(-3)
     self.assertEqual(complex.getModulus(), 5)
     self.assertAlmostEqual(complex.getArgument(), 36.87)
     self.assertEqual(complex.getPolarForm(), '5(cos(36.87) + isin(36.87))')
Ejemplo n.º 15
0
 def test_gets_sets_str(self):
     complex = ComplexNumber(1, 2)
     complex1 = ComplexNumber(1, -1)
     self.assertEqual(complex.getRealPart(), 1)
     self.assertEqual(complex.getImagPart(), 2)
     self.assertEqual(complex1.getRealPart(), 1)
     self.assertEqual(complex1.getImagPart(), -1)
     self.assertEqual(str(complex), '1 +2i')
     complex.setRealPart(4)
     complex.setImagPart(-100)
     self.assertEqual(complex.getRealPart(), 4)
     self.assertEqual(complex.getImagPart(), -100)
     self.assertEqual(str(complex), '4 -100i')