def convert_linear_mapping(cls, linear_mapping, basis_vectors): ''' converts a standard matrix linear mapping to a given coordinate vector linear_mapping: Matrix that inputs size basis_vectors basis_vectors: all basis_vectors ''' lin_width, lin_height = linear_mapping.get_size() # convert basis vectors to a matrix width, height = basis_vectors.get_size() assert linear_mapping.is_square( ) and lin_width == width, "Basis Vectors and Linear Mapping should have the same shape" matrix = Matrix(width, height) for col_idx in range(width): matrix.set_col(col_idx, basis_vectors.basis_vectors[col_idx].values) basis_vec_mat = copy.deepcopy(matrix) # Apply Linear Mapping to basis vectors applied = Matrix.matmul(linear_mapping, matrix) # Solve for Linear Mapping in base_B concat = Matrix.matrix_concat_column(basis_vec_mat, applied) reduced = Matrix.AUG_RREF(concat, width) # Extract Augmented Portion extracted = Matrix(lin_width, lin_height) count = 0 for col_idx in range(width, lin_width + width): extracted.set_col(count, reduced.get_col(col_idx)) count += 1 return extracted
def compute_coordinate(cls, ordered_basis, x): ''' Computes [x]_B given ordered basis and original vector x. ordered_basis: Basis Object, ordered basis x: vector ''' width, height = ordered_basis.get_size() # Construct Matrix using ordered matrix matrix = Matrix(width + 1, height) for col_idx in range(width): matrix.set_col(col_idx, ordered_basis.basis_vectors[col_idx].values) matrix.set_col(width, x.values) # AUG RREF to solve for coefficients reduced = Matrix.AUG_RREF(matrix, width) # Extract Vector Out x_b = reduced.get_col(width) return x_b
def in_span(self, vec_A): ''' Computes if a given vector is already spanned by the collection ''' if isinstance(vec_A, Matrix): # convert to vector vec_A = Matrix.flatten(vec_A) assert isinstance(vec_A, Vector) assert len(vec_A) == self.height # Construct a Matrix matrix = Matrix(self.width + 1, self.height) for vector_idx in range(len(self.vectors)): matrix.set_col(vector_idx, self.vectors[vector_idx].values) matrix.set_col(self.width, vec_A.values) # Row Reduce matrix reduced = Matrix.AUG_RREF(matrix, self.width) # All 0 values row_indices = Matrix.zero_rows(reduced, self.width) for row_idx in row_indices: if reduced.get_value(row_idx, self.width) != 0: return False return True
def change_of_basis(cls, b_coord_basis, c_coord_basis): ''' Computes the change of basis matrix that converts a vector in B_coord to C_coord ''' B_width, B_height = b_coord_basis.get_size() assert b_coord_basis.is_square() and ( B_width, B_height) == c_coord_basis.get_size( ), "Matrices must be square and identical in shape" matrix = Matrix(B_width * 2, B_height) for col_idx in range(B_width): matrix.set_col(col_idx, c_coord_basis.get_col(col_idx)) count = B_width for col_idx in range(B_width): matrix.set_col(count, b_coord_basis.get_col(col_idx)) count += 1 reduced = Matrix.AUG_RREF(matrix, B_width) # Extract Augmented C_B = Matrix(B_width, B_height) count = 0 for col_idx in range(B_width, B_width * 2): C_B.set_col(count, reduced.get_col(col_idx)) count += 1 return C_B