Example #1
0
 def getAvgMulRuntime(size1a, size1b, size2a, size2b, n):
     """Return average runtime of multiplication of matrices with given sizes and n
     Usage: 'TwistedMatrix.getAvgMulRuntime(2,2,2,2,5)'
     (to multiply a 2x2 matrix with another 2x2 matrix, with n = 5)
     """
     list1 = []  # creates first matrix
     for i in range(size1a):
         row = []
         for j in range(size1b):
             row.append(TwistedInt(randint(0, n - 1), n))
         list1.append(row)
     matrix1 = TwistedMatrix(list1)
     list2 = []  # creates second matrix
     for i in range(size2a):
         row = []
         for j in range(size2b):
             row.append(TwistedInt(randint(0, n - 1), n))
         list2.append(row)
     matrix2 = TwistedMatrix(list2)
     overallTime = 0
     for runNo in range(100):
         start = time.time()  # start of runtime measurement
         TwistedMatrix.__mul__(matrix1, matrix2)
         end = time.time()
         overallTime += (end - start)
     print(str(overallTime / 100) + " seconds")
Example #2
0
 def test_same_length(self):
     """Tests a normal case of creating a TwistedMatrix"""
     x = [TwistedInt(0, 2), TwistedInt(1, 2)]
     y = [TwistedInt(0, 2), TwistedInt(1, 2)]
     m = TwistedMatrix([x, y])
     self.assertEqual(
         "[\"[\'<0:2>\', \'<1:2>\']\", \"[\'<0:2>\', \'<1:2>\']\"]", str(m))
Example #3
0
 def test_add(self):
     """Tests a normal case of adding two identical 2-dimensional matrices"""
     x = [TwistedInt(0, 2), TwistedInt(1, 2)]
     y = [TwistedInt(0, 2), TwistedInt(1, 2)]
     m1 = TwistedMatrix([x, y])
     m2 = TwistedMatrix([x, y])
     self.assertEqual(
         "[\"[\'<0:2>\', \'<0:2>\']\", \"[\'<0:2>\', \'<0:2>\']\"]",
         str(m1 + m2))
Example #4
0
 def test_add_wrong_dimensions(self):
     """Tests a corner case of adding two matrices with different dimensions"""
     x = [TwistedInt(1, 11), TwistedInt(2, 11)]
     y = [TwistedInt(3, 11), TwistedInt(4, 11)]
     z = [TwistedInt(5, 11), TwistedInt(6, 11)]
     x1 = [TwistedInt(7, 11), TwistedInt(8, 11)]
     y1 = [TwistedInt(9, 11), TwistedInt(10, 11)]
     m1 = TwistedMatrix([x, y, z])
     m2 = TwistedMatrix([x1, y1])
     self.assertRaises(ValueError, TwistedMatrix.__add__, m2, m1)
Example #5
0
 def test_mul_wrong_dimensions(self):
     """Tests a corner case of multiplying two matrices with different dimensions
        that cannot be mlutiplied"""
     x = [TwistedInt(1, 11), TwistedInt(2, 11)]
     y = [TwistedInt(3, 11), TwistedInt(4, 11)]
     z = [TwistedInt(5, 11), TwistedInt(6, 11)]
     x1 = [TwistedInt(7, 11), TwistedInt(8, 11)]
     y1 = [TwistedInt(9, 11), TwistedInt(10, 11)]
     m1 = TwistedMatrix([x, y, z])
     m2 = TwistedMatrix([x1, y1])
     self.assertRaises(ValueError, TwistedMatrix.__mul__, m2, m1)
Example #6
0
 def test_mul_diff_dimensions(self):
     """Tests a normal case of multiplying two matrices with different dimensions"""
     x = [TwistedInt(1, 11), TwistedInt(2, 11)]
     y = [TwistedInt(3, 11), TwistedInt(4, 11)]
     z = [TwistedInt(5, 11), TwistedInt(6, 11)]
     x1 = [TwistedInt(7, 11), TwistedInt(8, 11)]
     y1 = [TwistedInt(9, 11), TwistedInt(10, 11)]
     m1 = TwistedMatrix([x, y, z])
     m2 = TwistedMatrix([x1, y1])
     self.assertEqual(
         "[\"[\'<0:11>\', \'<5:11>\']\", \"[\'<3:11>\', \'<1:11>\']\", \"[\'<6:11>\', \'<8:11>\']\"]",
         str(m1 * m2))
Example #7
0
 def getMulRuntime(size1a, size1b, size2a, size2b, n):
     """Return runtime of multiplication of matrices with given sizes and n
     Usage: 'TwistedMatrix.getMulRuntime(2,2,2,2,5)'
     (to multiply a 2x2 matrix with another 2x2 matrix, with n = 5)
     """
     standardTwistedInt = TwistedInt(1,
                                     n)  # standard value to simplify test
     list1 = []  # creates first matrix
     for i in range(size1a):
         row = []
         for j in range(size1b):
             row.append(standardTwistedInt)
         list1.append(row)
     matrix1 = TwistedMatrix(list1)
     list2 = []  # creates second matrix
     for i in range(size2a):
         row = []
         for j in range(size2b):
             row.append(standardTwistedInt)
         list2.append(row)
     matrix2 = TwistedMatrix(list2)
     start = time.time()  # start of runtime measurement
     TwistedMatrix.__mul__(matrix1, matrix2)
     end = time.time()
     print(str(end - start) + " seconds")
Example #8
0
 def test_mul(self):
     """Tests a normal case of multiplying two 2-dimensional matrices"""
     x = [TwistedInt(1, 9), TwistedInt(2, 9)]
     y = [TwistedInt(3, 9), TwistedInt(4, 9)]
     x1 = [TwistedInt(5, 9), TwistedInt(6, 9)]
     y1 = [TwistedInt(7, 9), TwistedInt(8, 9)]
     m1 = TwistedMatrix([x, y])
     m2 = TwistedMatrix([x1, y1])
     self.assertEqual(
         "[\"[\'<7:9>\', \'<3:9>\']\", \"[\'<8:9>\', \'<8:9>\']\"]",
         str(m1 * m2))
Example #9
0
 def test_add2(self):
     """Tests a normal case of adding two non-identical 2-dimensional matrices"""
     x = [TwistedInt(1, 9), TwistedInt(2, 9)]
     y = [TwistedInt(3, 9), TwistedInt(4, 9)]
     x1 = [TwistedInt(5, 9), TwistedInt(6, 9)]
     y1 = [TwistedInt(7, 9), TwistedInt(8, 9)]
     m1 = TwistedMatrix([x, y])
     m2 = TwistedMatrix([x1, y1])
     self.assertEqual(
         "[\"[\'<6:9>\', \'<8:9>\']\", \"[\'<1:9>\', \'<3:9>\']\"]",
         str(m1 + m2))
Example #10
0
    def __init__(self, n):
        """Initialise Zn with the given n length

        >>> x = TwistedIntegers(2)
        >>> print(x)
        ['<0:2>', '<1:2>']
        """
        if n <= 0:
            raise ValueError('Length of the list cannot be less or equal to 0')
        else:
            self.list = []
            i = 0
            while i < n:
                self.list.append(TwistedInt(i,n))
                i += 1
Example #11
0
 def test_mul_sameValue(self):
     """Tests multiplying the same twisted int together with itself"""
     a = TwistedInt(3, 5)
     self.assertEqual("<0:5>", str(a * a))
Example #12
0
 def test_init_nAndValueEqual(self):
     """Tests an initialization where the n and value are equal"""
     a = TwistedInt(2, 2)
     self.assertEqual(2, a.value)
     self.assertEqual(2, a.n)
Example #13
0
 def test_add_differentN(self):
     """Tests that adding twisted ints that have a different n throws an error"""
     a = TwistedInt(2, 5)
     b = TwistedInt(0, 2)
     self.assertRaises(ValueError, TwistedInt.__add__, a, b)
Example #14
0
 def test_mul_validModuloNotCalled(self):
     """Tests multiplying two twisted ints together where the result does not get trunctuated by the mod n function"""
     a = TwistedInt(1, 5)
     b = TwistedInt(1, 5)
     self.assertEqual("<3:5>", str(a * b))
Example #15
0
 def test_add_equalValues(self):
     """Tests adding two twisted ints that have the same value"""
     a = TwistedInt(2, 5)
     b = TwistedInt(2, 5)
     self.assertEqual("<4:5>", str(a + b))
Example #16
0
 def test_add_zeroValue(self):
     """Tests adding a twisted int with a valid twisted int of value 0"""
     a = TwistedInt(2, 5)
     b = TwistedInt(0, 5)
     self.assertEqual("<2:5>", str(a + b))
Example #17
0
 def test_add_sameValue(self):
     """Tests adding the same twisted int together with itself"""
     a = TwistedInt(2, 5)
     self.assertEqual("<4:5>", str(a + a))
Example #18
0
 def test_init_valid(self):
     """Tests a normal case of Twisted Int creation"""
     a = TwistedInt(2, 5)
     self.assertEqual(2, a.value)
     self.assertEqual(5, a.n)
Example #19
0
 def test_add_validModuloCalled(self):
     """Tests adding two twisted ints together where the result gets trunctuated by the mod n function"""
     a = TwistedInt(2, 5)
     b = TwistedInt(4, 5)
     self.assertEqual("<1:5>", str(a + b))
Example #20
0
 def test_add_validModuloNotCalled(self):
     """Tests adding two twisted ints together where the two values\' sum is smaller than n"""
     a = TwistedInt(2, 5)
     b = TwistedInt(1, 5)
     self.assertEqual("<3:5>", str(a + b))
Example #21
0
 def test_str_correctFormat(self):
     """Tests the formatting of the toString fuction of Twisted Ints"""
     a = TwistedInt(2, 5)
     self.assertEqual("<2:5>", str(a))
Example #22
0
 def test_init_zeroValue(self):
     """Tests an initialization where the value is zero"""
     a = TwistedInt(0, 8)
     self.assertEqual(0, a.value)
     self.assertEqual(8, a.n)
Example #23
0
 def test_diff_length(self):
     """Tests the creation of TwistedMatrix with rows of different length"""
     x = [TwistedInt(0, 2), TwistedInt(1, 2)]
     y = [TwistedInt(0, 3), TwistedInt(1, 3), TwistedInt(2, 3)]
     self.assertRaises(ValueError, TwistedMatrix.__init__, self, [x, y])
Example #24
0
 def test_mul_zeroValue(self):
     """Tests multiplying a twisted int with a valid twisted int of value 0"""
     a = TwistedInt(2, 5)
     b = TwistedInt(0, 5)
     self.assertEqual("<2:5>", str(a * b))
Example #25
0
 def test_mul_equalValues(self):
     """Tests multiplying two twisted ints that have the same value"""
     a = TwistedInt(3, 5)
     b = TwistedInt(3, 5)
     self.assertEqual("<0:5>", str(a * b))