class LiniarSystem(object): """Generic container for liniar ecuation""" def __init__(self): """Setup new instace""" self._database = Matrix(0, 0) self._results = {} self._variable = [] def __repr__(self): """Machine-readable representation""" return "<LiniarSystem with {}>".format(self._variable) def __str__(self): """Human-readable representation""" representation, index = [], 0 for var in self._variable: representation.append(var) representation.append("\t") representation.append("\n\n") for row in self._database.row_iter(): for element in row.items: representation.append(str(element)) representation.append("\t") representation.append("\t{}\n".format(self._results[index])) index += 1 return "".join(representation) @property def results(self): """Getter for results""" return self._results def _update_variable(self, variable): """Check if system contains all required variable""" for var in variable: if not var in self._variable: self._database.columns += 1 self._variable.append(var) def add(self, ecuation): """Append new ecuation into current system""" self._update_variable(ecuation.vars()) row = [] for var in self._variable: row.append(ecuation.get(var)) self._database.rows += 1 self._database.set_row(self._database.rows - 1, row) self._results[self._database.rows - 1] = ecuation.result def solve(self): """Solve system using Gaus elimination with partial pivoting""" matrix = self._database.to_list() resolver = Gauss(matrix, self.results) try: solution = resolver.solve() except ValueError: solution = None response = {} for index, value in enumerate(solution): response[self._variable[index]] = np.around(value, decimals=2) return response
def __init__(self): """Setup new instace""" self._database = Matrix(0, 0) self._results = {} self._variable = []