def clear_coefficients_bellow(self, row, col): num_equations = len(self) beta = MyDecimal(self[row].normal_vector[col]) for row_to_be_added_to in range(row + 1, num_equations): n = self[row_to_be_added_to].normal_vector gamma = Decimal(n[col]) alpha = -gamma / beta self.add_multiple_times_row_to_row(alpha, row, row_to_be_added_to)
def did_swap_with_row_below(self, row, col): num_equations = len(self) for k in range(row + 1, num_equations): coefficient = MyDecimal(self[k].normal_vector[col]) if not coefficient.is_near_zero(): self.swap_rows(row, k) return True return False
def raise_execption_if_contradictory_equation(self): for plane in self.planes: try: plane.first_nonzero_index(plane.normal_vector) except Exception as e: if str(e) == Plane.NO_NONZERO_ELMT_FOUND_MSG: tmp_contant = MyDecimal(plane.constant_term) if not tmp_contant.is_near_zero(): raise Exception(self.NO_SOLUTION_MSG) else: raise (e)
def has_unique_solution(self): for x in self.planes: if x.get_nonzero_index() < 0 and not MyDecimal( x.constant_term).is_near_zero(): return self.NO_SOLUTIONS_MSG if not len([ x for x in self.indices_of_first_nonzero_terms_in_each_row() if x >= 0 ]) == self.dimension: return self.INF_SOLUTIONS_MSG return True
def __eq__(self, plane2): if self.normal_vector.is_zero(): if not plane2.normal_vector.is_zero(): return False diff = self.constant_term - plane2.constant_term return MyDecimal(diff).is_near_zero() elif plane2.normal_vector.is_zero(): return False if not self.is_parallel(plane2): return False basepoint_difference = self.basepoint.minus(plane2.basepoint) return basepoint_difference.is_orthogonal(self.normal_vector)
def compute_triangular_form(self): system = deepcopy(self) num_equations = len(system) num_variables = system.dimension col = 0 for row in range(num_equations): while col < num_variables: c = MyDecimal(system[row].normal_vector[col]) if c.is_near_zero(): swap_succeeded = system.did_swap_with_row_below(row, col) if not swap_succeeded: col += 1 continue system.clear_coefficients_bellow(row, col) col += 1 break return system
def to_parametrization(self): plane_index = 0 coefficient_index = 0 basepoint = ['0'] * self.dimension pivots = self.indices_of_first_nonzero_terms_in_each_row() number_of_direction_vectors = self.dimension - len( ['0' for x in pivots if x >= 0]) direction_vectors = [['0' for x in range(self.dimension)] for x in range(number_of_direction_vectors)] for i in range(0, len(self.planes)): pivot = self[i].get_nonzero_index() if pivot < 0: break basepoint[pivot] = self[i].constant_term for i in range(0, len(self.planes)): pivot = self[i].get_nonzero_index() if pivot < 0: break else: index = 0 for j in range(pivot + 1, self.dimension): coefficient = self[i][j] if not MyDecimal(coefficient).is_near_zero(): direction_vectors[index][ pivot] = coefficient * Decimal('-1') if j not in pivots: index = index + 1 index = 0 for i in range(self.dimension): if i not in pivots: direction_vectors[index][i] = '1' index = index + 1 return Parametrization(Vector(basepoint), map(lambda x: Vector(x), direction_vectors))
def first_nonzero_index(iterable): for k, item in enumerate(iterable): if MyDecimal(item).is_near_zero(): return k raise Exception(Plane.NO_NONZERO_EMLT_FOUND_MSG)
def first_nonzero_index(Vector_iterable): iterable = Vector_iterable.coordinates for k, item in enumerate(iterable): if not MyDecimal(item).is_near_zero(): return k raise Exception(Plane.NO_NONZERO_ELTS_FOUND_MSG)