コード例 #1
0
    def test_size(self):

        vec1 = vector.Vector(self.lsF4)
        vec2 = vector.Vector(self.lsF5)

        self.assertEqual(vec1.size(), 6)
        self.assertEqual(vec2.size(), 12)
コード例 #2
0
    def test_length(self):

        vec1 = vector.Vector(self.lsF1)
        vec2 = vector.Vector(self.lsF2)
        vec3 = vector.Vector(self.lsF3)

        self.assertAlmostEqual(vec1.length(), 0.000)
        self.assertAlmostEqual(vec2.length(), 5.477225575051)
        self.assertAlmostEqual(vec3.length(), 8.66025403784)
コード例 #3
0
    def setUp(self):

        #setting up some fixtures
        self.lsF1 = [0, 0, 0, 0]
        self.lsF2 = [1, 2, 3, 4]
        self.lsF3 = [3, 1, 7, 4]
        self.lsF4 = [i for i in range(6)]
        self.lsF5 = [i for i in range(12)]

        self.vecF1 = vector.Vector(self.lsF1)
        self.vecF2 = vector.Vector(self.lsF2)
        self.vecF3 = vector.Vector(self.lsF3)
        self.vecF4 = vector.Vector(self.lsF4)
        self.vecF5 = vector.Vector(self.lsF5)
コード例 #4
0
    def test_vector_iterator_splittting_only(self):

        vec = vector.Vector(self.lsF2)
        it = vector.VectorIterator(vec)

        TL, value, BL = it.next()
        self.is_vector_equal_to_list(TL, [])
        self.assertEqual(value, self.lsF2[0])
        self.is_vector_equal_to_list(BL, [2, 3, 4])
        it.merge(TL, value, BL)
        self.is_vector_equal_to_list(vec, self.lsF2)

        TL, value, BL = it.next()
        self.is_vector_equal_to_list(TL, [1])
        self.assertEqual(value, self.lsF2[1])
        self.is_vector_equal_to_list(BL, [3, 4])
        it.merge(TL, value, BL)
        self.is_vector_equal_to_list(vec, self.lsF2)

        TL, value, BL = it.next()
        self.is_vector_equal_to_list(TL, [1, 2])
        self.assertEqual(value, self.lsF2[2])
        self.is_vector_equal_to_list(BL, [4])
        it.merge(TL, value, BL)
        self.is_vector_equal_to_list(vec, self.lsF2)

        TL, value, BL = it.next()
        self.is_vector_equal_to_list(TL, [1, 2, 3])
        self.assertEqual(value, self.lsF2[3])
        self.is_vector_equal_to_list(BL, [])
        it.merge(TL, value, BL)
        self.is_vector_equal_to_list(vec, self.lsF2)
コード例 #5
0
    def test_subsctiption_oprator_slice(self):

        vec1 = vector.Vector(self.lsF2)
        self.is_list_equal_to_list(vec1[:], self.lsF2)
        self.is_list_equal_to_list(vec1[:2], self.lsF2[:2])
        self.is_list_equal_to_list(vec1[1:3], self.lsF2[1:3])
        self.is_list_equal_to_list(vec1[1:1], self.lsF2[1:1])
        self.is_list_equal_to_list(vec1[2:1], self.lsF2[2:1])
コード例 #6
0
    def test_subsctiption_oprator_single(self):

        vec1 = vector.Vector(self.lsF3)

        self.assertAlmostEqual(vec1[0], self.lsF3[0])
        self.assertAlmostEqual(vec1[1], self.lsF3[1])
        self.assertAlmostEqual(vec1[2], self.lsF3[2])
        self.assertAlmostEqual(vec1[3], self.lsF3[3])
        self.assertAlmostEqual(vec1[1], self.lsF3[1])
コード例 #7
0
    def mult_vec_by_axpy(self, vec):
        """
        This function implemetns a matrix vector multiplication but 
        this time based on the AXPY algorithm

        :vec: Vector, the value to multiply
        :returns: Vector
        """

        result = vector.Vector([0]* self.rows())
        for i,c in enumerate(self.data) :
            vector.axpy(c,result, vec[i])

        return result
コード例 #8
0
    def __init__(self, rows, columns,data=None ):
        """
        This is the constructor for the generic matrix
        Args:

        :rows: int, how many rows you want in the matrix
        :columns: int, how many columns you want in the matrix
        :data: Vector[], column vectors representing the data
        """
        if not data:
            self.data = [vector.Vector([0]*rows) for c in xrange(columns)]
        else:
            self.data = data
        self.__columns = columns
        self.__rows = rows
コード例 #9
0
    def mult_vec_by_dot(self, vec):
        """
        matrix vector mult using under dot product under the hood

        :vec: Vector, the vector to multiply
        return Vector
        """
        it = self.row_iterator() 
        values = []
        for row in it:
            val = vector.dot_product(row, vec)

            values.append(val)

        return vector.Vector(values)
コード例 #10
0
    def mult_vec_by_transposed_matrix_axpy(self,vec):
        """
        This function implemetns a matrix vector multiplication using the 
        transposed version of the matrix and is based on a axpy algorithm 

        :vec: Vector, the value to multiply
        :returns: Vector
        """

        result = vector.Vector([0]* self.columns())
        
        it = self.row_iterator()
        
        for i,r in enumerate(it) :
            vector.axpy(r,result, vec[i])
        return result
コード例 #11
0
    def mult_vec_by_transposed_matrix_dot(self,vec):
        """
        This function multiply the given vector by the transpose of the current 
        matrix, the transposition is not performed, instead we loop the columns
        instead of the rows
        
        :vec: Vector, the vector to be multiplied
        :returns: Vector
        """
        
        it = self.column_iterator() 
        values = []
        for c in it:
            val = vector.dot_product(c, vec)

            values.append(val)

        return vector.Vector(values)
コード例 #12
0
    def from_list(cls, rows,columns, data):
        """
        This alternative constructor lets you build the matrix
        from a flat array of values, dimensions needs to be provided
        to partition the data into columns
        
        :rows: int, how many rows you want in the matrix
        :columns: int, how many columns you want in the matrix
        :data: float[], the data to partition 
        """
        

        vectors = []
        for c in range(len(data)/rows):
            vec = vector.Vector([ data[c*rows + r] for r in range(rows)])
            vectors.append(vec)

        inst = cls(rows,columns,vectors)
        return inst
コード例 #13
0
    def mult_vec_by_dot_parted(self, vec):
        """
        This is an alternative method to multiply a matrix by a vector 
        taking advantage of a dot product based operatin but with partitioned
        matrix
        :vec: Vector, the value to multiply
        :returns: Vector
        """

        result = vector.Vector([0]* self.rows())
        tl, bl = vector.part(vec)
        it = self.full_iterator()
        for i,data in enumerate(it):
            tl, value, bl = vector.slice_(tl,bl)
            result[i] = (vector.dot_product(data["at10"],tl)  + 
                      data["a11"] * value +
                      vector.dot_product(data["at12"],bl)) 
            vector.compose(tl,value,bl)

        return result 
コード例 #14
0
    def mult_vec_by_axpy_parted(self, vec):
        """
        This function implements a matrix vector mult based on AXPY but with 
        a parted matrix

        :vec: Vector, the vector to work on
        :returns: Vector
        """

        result = vector.Vector([0]* self.rows())
        TL, BL = vector.part(result)

        it = self.full_iterator()
        for i,data in enumerate(it):
            
            TL, value, BL = vector.slice_(TL,BL)
            vector.axpy(data["a01"],TL,vec[i])
            value = data["a11"] * vec[i] + value
            vector.axpy(data["a21"], BL, vec[i])
    
            vector.compose(TL,value,BL)
        return TL 
コード例 #15
0
    def test_vector_iterator_splitting_modifying_merge(self):

        vec = vector.Vector(self.lsF2)
        it = vector.VectorIterator(vec)

        TL, value, BL = it.next()
        value += 2
        self.is_vector_equal_to_list(TL, [])
        self.assertEqual(value, 3)
        self.is_vector_equal_to_list(BL, [2, 3, 4])
        it.merge(TL, value, BL)
        self.is_vector_equal_to_list(vec, [3, 2, 3, 4])

        TL, value, BL = it.next()
        value += 5
        self.is_vector_equal_to_list(TL, [3])
        self.assertEqual(value, 7)
        self.is_vector_equal_to_list(BL, [3, 4])
        it.merge(TL, value, BL)
        self.is_vector_equal_to_list(vec, [3, 7, 3, 4])

        TL, value, BL = it.next()
        value += 1
        self.is_vector_equal_to_list(TL, [3, 7])

        self.assertEqual(value, 4)
        self.is_vector_equal_to_list(BL, [4])
        it.merge(TL, value, BL)
        self.is_vector_equal_to_list(vec, [3, 7, 4, 4])

        TL, value, BL = it.next()
        value += 6
        self.is_vector_equal_to_list(TL, [3, 7, 4])
        self.assertEqual(value, 10)
        self.is_vector_equal_to_list(BL, [])
        it.merge(TL, value, BL)
        self.is_vector_equal_to_list(vec, [3, 7, 4, 10])
コード例 #16
0
    def test_zero(self):

        vec = vector.Vector(self.lsF5)
        vec.zero()
        self.is_vector_equal_to_list(vec, [0] * 12)
コード例 #17
0
    def test_creation(self):

        vec = vector.Vector(self.lsF1)
        self.is_vector_equal(vec, self.vecF1)
コード例 #18
0
    def test_scalar_mult_operator(self):

        vec = vector.Vector(self.lsF2)
        vec = 2.0 * vec
        self.is_vector_equal_to_list(vec, [2, 4, 6, 8])
コード例 #19
0
    def test_in_place_addition_operator(self):

        vec = vector.Vector(self.lsF3)
        vec += self.vecF2

        self.is_vector_equal_to_list(vec, [4, 3, 10, 8])
コード例 #20
0
    def test_set_to_value(self):

        vec = vector.Vector(self.lsF5)
        vec.set_to_value(5)
        self.is_vector_equal_to_list(vec, [5] * 12)