Exemplo n.º 1
0
    def generate_matrix(self, size):
        self.size = size
        self.lines = [[]] * self.size

        # Set the main diagonal with values (diagonal matrix)
        for index in range(0, self.size):
            value = random.randint(1, MAX_POSITIVE_INTEGER)
            element = Element(value, index)
            self.lines[index] = []
            self.lines[index].append(element)

        counter = self.size / 3
        tuples = []
        # Set values above the main diagonal
        while True:
            index = random.randint(0, self.size - 1)
            second_index = random.randint(index, self.size - 1)
            value = random.randint(0, MAX_POSITIVE_INTEGER)

            if not (index, second_index) in tuples:
                tuples.append((index, second_index))
                tuples.append((second_index, index))
                element = Element(value, second_index)
                transposed_element = Element(value, index)
                self.lines[index].append(element)
                self.lines[second_index].append(transposed_element)
                counter = counter - 2

            if counter <= 0:
                break

        self.sort_columns()
Exemplo n.º 2
0
    def make_transpose(self):
        self.transpose = [[]] * self.size
        item = None

        for index in range(0, self.size):
            self.transpose[index] = []

        for index in range(0,self.size):
            for item in self.lines[index]:
                element = Element(item.value, index)
                self.transpose[item.position].append(element)
            self.transpose[item.position].sort(key=lambda x: x.position)
Exemplo n.º 3
0
    def __mul__(self, matrix):
        result = Matrix(self.size, 1)
        for i in range(self.size):
            for element in matrix.lines[i]:
                for j in range(matrix.size):
                    col_element = self.get_el_col(j, i)
                    if col_element:
                        new_element = Element(col_element.value, element.position)
                        new_element.value *= element.value
                        result.add_to_line(j, new_element)

        return result
Exemplo n.º 4
0
    def init_from_file(self, file):
        self.file = open(file, "r")
        self.size = int(self.file.readline())
        self.lines = [[]] * self.size

        while 1:
            element = self.file.readline().split()

            if len(element) == 0:
                break

            new_element = Element(element[0][:-1], element[2])

            position = int(element[1][:-1])

            if not self.lines[position]:
                self.lines[position] = []
            self.lines[position].append(new_element)
Exemplo n.º 5
0
    def init_from_file(self, file):
        self.file = open(file, "r")
        self.size = int(self.file.readline())
        self.lines = [[]] * self.size

        while True:
            element = self.file.readline().split(', ')

            element = [x.strip(' ') for x in element]

            if len(element) < 3:
                break

            new_element = Element(element[0], element[2][:-1])

            position = int(element[1])

            if not self.lines[position]:
                self.lines[position] = []
            self.lines[position].append(new_element)
Exemplo n.º 6
0
    def init_from_file(self, file):
        self.file = open(file, "r")
        self.size = int(self.file.readline())
        self.lines = [[]] * self.size

        while True:
            line = self.file.readline().split(', ')

            values = [x.strip(' ') for x in line]

            if len(values) < 3:
                break

            element = Element(values[0], values[2][:-1])

            position = int(values[1])

            if not self.lines[position]:
                self.lines[position] = []
            self.lines[position].append(element)

        self.sort_columns()
Exemplo n.º 7
0
 def __mul__(self, obj):
     if type(object) == Matrix:
         matrix = obj
         result = Matrix(self.size, 1)
         for i in range(self.size):
             for element in matrix.lines[i]:
                 for j in range(matrix.size):
                     col_element = self.get_el_col(j, i)
                     if col_element:
                         new_element = Element(col_element.value, element.position)
                         new_element.value *= element.value
                         result.add_to_line(j, new_element)
         return result
     elif type(obj) == np.ndarray:
         result = []
         for index in range(self.size):
             compute_sum = 0
             for element in self.lines[index]:
                 compute_sum += element.value * obj[element.position]
             result.insert(index, compute_sum)
         return result
     else:
         raise Exception("Operation is not permitted")