def is_parallel(self, v): self_magnitude = self.magnitude() v_magnitude = v.magnitude() if float_is_zero(self_magnitude) or float_is_zero(v_magnitude): return True else: return float_is_zero( abs(abs(self.dot(v)) - abs(self_magnitude * v_magnitude)))
def clear_all_terms_above_with_non_zero_coefficient( self, from_row, variable_no, coefficient=1): for row in reversed(range(from_row)): coefficient_to_clear = self.planes[row].normal_vector.coordinates[ variable_no] if not float_is_zero(coefficient_to_clear): self.add_multiple_times_row_to_row( -coefficient_to_clear / coefficient, from_row, row)
def compute_rref(self): tf = self.compute_triangular_form() for i in reversed(range(len(tf.planes))): plane = tf.planes[i] for j in range(tf.dimension): coefficient = plane.normal_vector.coordinates[j] if float_is_zero(coefficient): continue if coefficient != 1: new_normal_vector = plane.normal_vector.times_scalar( 1 / coefficient) new_constant_term = plane.constant_term / coefficient tf.planes[i] = Plane(new_normal_vector, new_constant_term) tf.clear_all_terms_above_with_non_zero_coefficient(i, j) break return tf
def compute_triangular_form(self): system = deepcopy(self) num_planes = len(system.planes) j = 0 for i in range(num_planes): while j < system.dimension: plane = system.planes[i] c = plane.normal_vector.coordinates[j] if float_is_zero(c): r = system.get_first_row_below_with_non_zero_coefficient( i, j) if r is None: j += 1 continue else: system.swap_rows(i, r) plane = system.planes[i] c = plane.normal_vector.coordinates[j] system.clear_all_terms_below_with_non_zero_coefficient(i, j, c) j += 1 break return system
def is_orthogonal(self, v): return float_is_zero(abs(self.dot(v)))
def is_zero(self): return float_is_zero(self.magnitude())
def get_first_row_below_with_non_zero_coefficient(self, i, j): for row in range(i + 1, len(self.planes)): if not float_is_zero( self.planes[row].normal_vector.coordinates[j]): return row return None
def first_nonzero_index(iterable): for k, item in enumerate(iterable): if not float_is_zero(item): return k raise Exception(Line.NO_NONZERO_ELTS_FOUND_MSG)