def __init__(self, data=None, degree=1): """ initialize object @param data: path to data file @param degree: degree of polynomial to solve (e.g., quadratic) """ self.independent_variables = Matrix() self.dependent_variables = Matrix() self.coefficients = list() # make sure degree is at least 1 if degree < 1: degree = 1 # if data is specified, load it and set independent and dependent variables accordingly if data is not None: document = Document().open(filePath=data,splitLines=True,splitTabs=True) append_to_independent_variables = self.independent_variables.append append_to_dependent_variables = self.dependent_variables.append # loop through the rows in the document to get data for row in document: new_row = [float(value) for value in row] dependent_variable_row = [new_row[-1]] independent_variable_row = [new_row[0]**i for i in xrange(degree+1)] #print independent_variable_row, new_row, dependent_variable_row #append_to_independent_variables(new_row[:-1]) append_to_independent_variables(independent_variable_row) #append_to_dependent_variables(new_row[-1:]) append_to_dependent_variables(dependent_variable_row) #print self.independent_variables.matrix self.coefficients = self.getCoefficients([self.independent_variables, self.dependent_variables])
def read(self, content: str) -> Document: return Document().create_from_text(content)