Exemplo n.º 1
0
 def __init__(self, size, density, pos_def, distribution, cond,
              diagonal_option, symmetric):
     self.size = size
     self.density = density
     self.pos_def = pos_def
     self.distribution = distribution
     self.diagonal_option = diagonal_option
     self.symmetric = symmetric
     self.diagonal_generator = DiagonalGenerator(self.size, self.pos_def,
                                                 self.diagonal_option, cond)
     self.matrix = np.zeros((self.size, self.size))
Exemplo n.º 2
0
 def __init__(self, size, density, pos_def, distribution, cond,
              diagonal_option, symmetric):
     self.size = size
     self.density = density
     self.pos_def = pos_def
     self.distribution = distribution
     self.symmetric = symmetric
     self.matrix = np.random.random((self.size, self.size))
     self.density_setter = DensitySetter(self.size, self.density)
     self.bandwidth_reducer = BandwidthReducer(self.size)
     cond = random.randint(1, 100)
     self.diagonal_generator = DiagonalGenerator(self.size, self.pos_def,
                                                 diagonal_option, cond)
Exemplo n.º 3
0
 def __init__(self, size, density, pos_def, distribution, cond,
              diagonal_option, symmetric):
     self.size = size
     self.density = density
     self.pos_def = pos_def
     self.distribution = distribution
     self.cond = cond
     self.diagonal_option = diagonal_option
     self.symmetric = symmetric
     self.diagonal_generator = DiagonalGenerator(self.size, self.pos_def,
                                                 diagonal_option, cond)
     self.matrix = self.diagonal_generator.set_diagonal_entries(
         np.zeros((self.size, self.size)))
     self.orthogonal_matrix = None
     self.householder = HouseholderTransformation(self.size)
     self.bandwidth_reducer = BandwidthReducer(self.size)
Exemplo n.º 4
0
 def __init__(self, size, density, pos_def, distribution, cond,
              diagonal_option, symmetric):
     self.size = size
     self.density = density
     self.pos_def = pos_def
     self.distribution = distribution
     self.density_setter = DensitySetter(self.size, self.density)
     self.bandwidth_reducer = BandwidthReducer(self.size)
     self.cond = cond
     self.symmetric = symmetric
     self.diagonal_generator = DiagonalGenerator(self.size, True,
                                                 diagonal_option, 20)
     self.matrix = np.zeros((self.size, self.size))
     self.s_matrix = np.random.random(
         (self.size, self.size))  #np.zeros((self.size, self.size))
     self.generate_s()
     self.random_generator = RandomGenerator()
Exemplo n.º 5
0
class Generator1:
    def __init__(self, size, density, pos_def, distribution, cond,
                 diagonal_option, symmetric):
        self.size = size
        self.density = density
        self.pos_def = pos_def
        self.distribution = distribution
        self.cond = cond
        self.diagonal_option = diagonal_option
        self.symmetric = symmetric
        self.diagonal_generator = DiagonalGenerator(self.size, self.pos_def,
                                                    diagonal_option, cond)
        self.matrix = self.diagonal_generator.set_diagonal_entries(
            np.zeros((self.size, self.size)))
        self.orthogonal_matrix = None
        self.householder = HouseholderTransformation(self.size)
        self.bandwidth_reducer = BandwidthReducer(self.size)

    def generate(self) -> np.ndarray:
        self.premultiply()
        self.post_multiply()
        #self.bandwidth_transform()
        self.matrix = self.bandwidth_reducer.set_lower_bandwidth(self.matrix)
        self.matrix = self.bandwidth_reducer.set_upper_bandwidth(self.matrix)

        return self.matrix

    def premultiply(self):
        generator_5 = Generator5(self.size, self.density, self.pos_def,
                                 self.distribution, self.cond,
                                 self.diagonal_option, self.symmetric)
        self.orthogonal_matrix = generator_5.generate()
        self.matrix = np.matmul(self.orthogonal_matrix, self.matrix)

    def post_multiply(self):
        if self.symmetric:
            self.matrix = np.matmul(np.transpose(self.orthogonal_matrix),
                                    self.matrix)
        else:
            generator_5 = Generator5(self.size, self.density, self.pos_def,
                                     self.distribution, self.cond,
                                     self.diagonal_option, self.symmetric)
            orthogonal_matrix = generator_5.generate()
            self.matrix = np.matmul(self.matrix, orthogonal_matrix)

    def bandwidth_transform(self):
        bandwidth = self.size
        elimination_number = 5
        block_size = 1
        self.matrix = self.householder.transform(self.matrix, bandwidth,
                                                 elimination_number,
                                                 block_size)
 def __init__(self, size, amount, density, pos_def, distribution, cond,
              storage_location, generation_option, diagonal_option,
              symmetric):
     print("Your matrix will be generated, please wait...")
     self.size = size
     # value between 0 and 1
     self.density = density
     self.storage_location = storage_location
     self.amount = amount
     self.pos_def = pos_def
     # Option 1: (0,1) ; Option 2: (-1, 1); Option 3: normal
     self.distribution = distribution
     # Option 1 to 6
     self.generation_option = generation_option
     # boolean
     self.symmetric = symmetric
     self.cond = cond
     self.generator = None
     self.diagonal_option = diagonal_option
     self.matrix = None
     self.density_setter = DensitySetter(self.size, self.density)
     self.diagonal_generator = DiagonalGenerator(self.size, self.pos_def,
                                                 self.diagonal_option,
                                                 self.cond)
Exemplo n.º 7
0
class Generator6:
    def __init__(self, size, density, pos_def, distribution, cond,
                 diagonal_option, symmetric):
        self.size = size
        self.density = density
        self.pos_def = pos_def
        self.distribution = distribution
        self.diagonal_option = diagonal_option
        self.symmetric = symmetric
        self.diagonal_generator = DiagonalGenerator(self.size, self.pos_def,
                                                    self.diagonal_option, cond)
        self.matrix = np.zeros((self.size, self.size))

    def generate(self):
        self.matrix = self.diagonal_generator.set_diagonal_entries(self.matrix)
        for i in range(random.randint(1, 10)):
            help_matrix = np.random.random((self.size, self.size))
            self.matrix = np.matmul(help_matrix, self.matrix)
            help_matrix = np.linalg.inv(help_matrix)
            self.matrix = np.matmul(self.matrix, help_matrix)
        return self.matrix
Exemplo n.º 8
0
 def generate_diagonal_matrix(self):
     cond = random.randint(0, 100)
     diagonal_generator = DiagonalGenerator(self.size, True, 0, cond)
     matrix = np
     matrix = diagonal_generator.set_diagonal_entries(matrix)
     return matrix
Exemplo n.º 9
0
class Generator4:
    def __init__(self, size, density, pos_def, distribution, cond,
                 diagonal_option, symmetric):
        self.size = size
        self.density = density
        self.pos_def = pos_def
        self.distribution = distribution
        self.symmetric = symmetric
        self.matrix = np.random.random((self.size, self.size))
        self.density_setter = DensitySetter(self.size, self.density)
        self.bandwidth_reducer = BandwidthReducer(self.size)
        cond = random.randint(1, 100)
        self.diagonal_generator = DiagonalGenerator(self.size, self.pos_def,
                                                    diagonal_option, cond)

    def generate(self):
        self.matrix = self.matrix = self.density_setter.set_density(
            self.matrix)
        # (optional) pre-/postmultiply by diagonalmatrices
        rand = random.randint(0, 1)
        if rand == 0:
            self.premultiply()
        elif rand == 1:
            self.post_multiply()
        # (optional) Permute rows/columns
        rand = 0  #random.randint(0, 1)
        if rand == 0:
            self.permute_columns()
        elif rand == 1:
            self.permute_rows()
        # (optional) set density
        rand = 0  # random.randint(0, 1)
        if rand == 0:
            self.matrix = self.density_setter.set_density(self.matrix)
        # (optional) Reducing upper and(/or) lower bandwidth -> and: band matrix?
        rand = random.randint(0, 2)
        if rand == 0:
            self.matrix = self.bandwidth_reducer.set_lower_bandwidth(
                self.matrix)
        elif rand == 1:
            self.matrix = self.bandwidth_reducer.set_upper_bandwidth(
                self.matrix)
        elif rand == 2:
            self.matrix = self.bandwidth_reducer.set_lower_bandwidth(
                self.matrix)
            self.matrix = self.bandwidth_reducer.set_upper_bandwidth(
                self.matrix)

        self.matrix = self.diagonal_generator.set_diagonal_entries(self.matrix)
        return self.matrix

    def premultiply(self):
        mult_matrix = np.zeros((self.size, self.size))
        self.diagonal_generator.set_diagonal_entries(mult_matrix)
        self.matrix = np.matmul(mult_matrix, self.matrix)

    def post_multiply(self):
        mult_matrix = np.zeros((self.size, self.size))
        self.diagonal_generator.set_diagonal_entries(mult_matrix)
        self.matrix = np.matmul(self.matrix, mult_matrix)

    def permute_rows(self):
        np.random.shuffle(self.matrix)

    def permute_columns(self):
        np.transpose(np.random.shuffle(np.transpose(self.matrix)))

    def generate_diagonal_matrix(self):
        cond = random.randint(0, 100)
        diagonal_generator = DiagonalGenerator(self.size, True, 0, cond)
        matrix = np
        matrix = diagonal_generator.set_diagonal_entries(matrix)
        return matrix
class GenerationController:
    def __init__(self, size, amount, density, pos_def, distribution, cond,
                 storage_location, generation_option, diagonal_option,
                 symmetric):
        print("Your matrix will be generated, please wait...")
        self.size = size
        # value between 0 and 1
        self.density = density
        self.storage_location = storage_location
        self.amount = amount
        self.pos_def = pos_def
        # Option 1: (0,1) ; Option 2: (-1, 1); Option 3: normal
        self.distribution = distribution
        # Option 1 to 6
        self.generation_option = generation_option
        # boolean
        self.symmetric = symmetric
        self.cond = cond
        self.generator = None
        self.diagonal_option = diagonal_option
        self.matrix = None
        self.density_setter = DensitySetter(self.size, self.density)
        self.diagonal_generator = DiagonalGenerator(self.size, self.pos_def,
                                                    self.diagonal_option,
                                                    self.cond)

    def start_generation(self):
        amount = self.amount
        i = 0
        while i < amount:
            self.choose_generator()
            self.matrix = self.generator.generate()
            self.matrix = self.density_setter.set_density(self.matrix)
            self.matrix = self.density_setter.set_density(self.matrix)
            self.matrix = self.diagonal_generator.set_diagonal_entries(
                self.matrix)
            if self.check_components(i):
                self.save()
                i = i + 1
            else:
                print("not regular matrix was generated")
        print("Finishing matrix generation")

    def choose_generator(self):
        generator = self.generation_option
        if self.generation_option == 0:
            generator = random.randint(1, 6)

        if generator == 1:
            self.generator = Generator1(self.size, self.density, self.pos_def,
                                        self.distribution, self.cond,
                                        self.diagonal_option, self.symmetric)
        elif generator == 2:
            self.generator = Generator2(self.size, self.density, self.pos_def,
                                        self.distribution, self.cond,
                                        self.diagonal_option, self.symmetric)
        elif generator == 3:
            self.generator = Generator3(self.size, self.density, self.pos_def,
                                        self.distribution, self.cond,
                                        self.diagonal_option, self.symmetric)
        elif generator == 4:
            self.generator = Generator4(self.size, self.density, self.pos_def,
                                        self.distribution, self.cond,
                                        self.diagonal_option, self.symmetric)
        elif generator == 5:
            self.generator = Generator5(self.size, self.density, self.pos_def,
                                        self.distribution, self.cond,
                                        self.diagonal_option, self.symmetric)
        elif generator == 6:
            self.generator = Generator6(self.size, self.density, self.pos_def,
                                        self.distribution, self.cond,
                                        self.diagonal_option, self.symmetric)
        else:
            raise Exception("NotImplementedException")

    def save(self):
        # save in hdf5 file
        print(self.matrix)
        self.show_matrix()

    def save2(self):
        # save in hdf5 file
        pass

    def check_components(self, i):
        if self.pos_def:
            if self.check_pos_def():
                return self.check_regularity()
            else:
                print("not positive definite matrix was generated")
                return False
        else:
            return self.check_regularity()

    def check_regularity(self):
        np.seterr(all='ignore')
        if det(self.matrix) != 0:
            return True
        else:
            print("not regular matrix was generated")
            return False

    def check_pos_def(self):
        return np.all(np.linalg.eigvals(self.matrix) > 0)

    def show_matrix(self):
        matrix = self.matrix
        plt.spy(matrix)
        plt.show()
Exemplo n.º 11
0
 def generate_s(self):
     matrix = np.random.random((self.size, self.size))
     self.diagonal_generator = DiagonalGenerator(self.size, True,
                                                 random.randint(1, 6), 20)
     self.s_matrix = self.diagonal_generator.set_diagonal_entries(matrix)
Exemplo n.º 12
0
class Generator2:
    def __init__(self, size, density, pos_def, distribution, cond,
                 diagonal_option, symmetric):
        self.size = size
        self.density = density
        self.pos_def = pos_def
        self.distribution = distribution
        self.density_setter = DensitySetter(self.size, self.density)
        self.bandwidth_reducer = BandwidthReducer(self.size)
        self.cond = cond
        self.symmetric = symmetric
        self.diagonal_generator = DiagonalGenerator(self.size, True,
                                                    diagonal_option, 20)
        self.matrix = np.zeros((self.size, self.size))
        self.s_matrix = np.random.random(
            (self.size, self.size))  #np.zeros((self.size, self.size))
        self.generate_s()
        self.random_generator = RandomGenerator()

    def generate(self) -> np.ndarray:
        self.matrix = self.diagonal_generator.set_diagonal_entries(self.matrix)
        # Fill upper/lower Triangle
        rand = random.randint(0, 2)
        if rand == 0:
            self.fill_lower_triangle()
        elif rand == 1:
            self.fill_upper_triangle()

        # Pre-/postmultiply with S and S -1
        rand = random.randint(0, 1)
        if rand == 0:
            self.premultiply()
            self.post_multiply()

        # Reducing upper OR lower bandwidth
        rand = random.randint(0, 2)
        if rand == 0:
            self.matrix = self.bandwidth_reducer.set_lower_bandwidth(
                self.matrix)
        elif rand == 1:
            self.matrix = self.bandwidth_reducer.set_upper_bandwidth(
                self.matrix)

        # set density
        rand = 0  #random.randint(0, 1)
        if rand == 0:
            self.matrix = self.density_setter.set_density(self.matrix)
        return self.matrix

    def fill_upper_triangle(self):
        distance = 1
        for i in range(self.size - 1):
            array = self.random_generator.set_random(self.size - distance,
                                                     self.distribution, False)
            for j in range(self.size - distance):
                self.matrix[j, j + distance] = array[j]
                # random.randint(0, 1000)/1000
            distance = distance + 1

    def fill_lower_triangle(self):
        distance = 1
        for i in range(self.size - 1):
            array = self.random_generator.set_random(self.size - distance,
                                                     self.distribution, False)
            for j in range(self.size - distance):
                self.matrix[j + distance, j] = array[j]
                # random.randint(0, 1000) / 1000
            distance = distance + 1

    def premultiply(self):
        self.generate_s()
        self.diagonal_generator.set_diagonal_entries(self.s_matrix)
        self.matrix = np.matmul(self.s_matrix, self.matrix)

    def post_multiply(self):
        s_inv_matrix = np.linalg.inv(self.s_matrix)
        self.diagonal_generator.set_diagonal_entries(s_inv_matrix)
        self.matrix = np.matmul(self.matrix, s_inv_matrix)

    def generate_s(self):
        matrix = np.random.random((self.size, self.size))
        self.diagonal_generator = DiagonalGenerator(self.size, True,
                                                    random.randint(1, 6), 20)
        self.s_matrix = self.diagonal_generator.set_diagonal_entries(matrix)