![]() |
Linear Equations Homework - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Linear Equations Homework (/thread-5877.html) |
Linear Equations Homework - ngr33n - Oct-26-2017 I'm a little bit confused with what this method is supposed to do. Here's the full description: "Write a class for linear equations. A generic linear equation is of the form y = mx + b where m and b are constants. Include the following methods: (a) __init__, __str__, __repr__. (b) value(x), which returns the value of the equation given x. © compose(linear_equation) that composes two linear equations. That is, if y = x + 1 and z = 2a + 5, then y(z) = 2x + 6 and will be called as y.compose(z). Note that the compose operation is not communitive. (d) __add__ returns the sum of two linear equations. That is, if y = ax + b and z = cx + d, then y + z = (a + c)x + (b + d). Include sample code that uses your class and demonstrates the use of all methods as well as error handling." On part c), I don't know what's actually going on when they say compose two linear equations. I tried looking it up but am not finding what I need. So if y = x + 1 and z = 2a + 5, how does y(z) = 2x + 6? I notice the constants are added together, but what about the x and 2a? I'm a little confused. What happens to the "a", or what even is the "a"? Here's my full program: class LinearEquations(object): def __init__(self, m, b): # print("in constructor") self.m = float(m) self.b = float(b) def __str__(self): # print("in str") return "%2fx + %.2f" % (self.m, self.b) def __repr__(self): # print("in repr") return self.__str__() def value(self, x): return self.m * x + self.b def compose(self, linear_equation): return LinearEquations(self.m * linear_equation.m, \ self.m * linear_equation.b + self.b) def __add__(self, z): """ Combine like terms by adding two linear equations together. """ try: return LinearEquations(self.m + z.m, self.b + z.b) except TypeError as e: print("Error!", e) return None # ---------------------------------main--------------------------------------- # description example executed equation_1 = LinearEquations(1, 1) equation_2 = LinearEquations(2, 5) print("\nLinear Equation 1: y =", equation_1) print("Linear Equation 2: z =", equation_2) print("\nThe 'composition' of the two linear equations is y(z) =", \ equation_1.compose(equation_2)) print("The 'sum' of the two linear equations is y + z =", \ equation_1.__add__(equation_2))I know the compose() method works, but I'm honestly not sure what's going on. Also, I have one more question. I'm supposed to implement error handling? Did I do it right with the try-except? That's honestly my biggest question. Thanks! One more thing, where else do I need to implement error handling besides the __add__() method? RE: Linear Equations Homework - buran - Oct-26-2017 in y = x + 1 y is dependent variable and x is independent variable. In z = 2a + 5 z is dependent and a - independent variable. They use different notation to avoid confusion. It is same to write the second one as z = 2x + 5 so, it is simple math substitution: y(x) = x +1 (1) y(z) = z+1 (2) z = 2x + 5 (3) substitute (3) in (2) y = (2x + 5) + 1 (4) y = 2x + 6 (5) In more general form y = ax+b z = cx+d y(z) = a(cx+d) + b y(z) = acx + ad + b in your particular example a=b=1, c=2 and d=5 that is why y(z) = 1*2*x + 1*5 + 1 y(z) = 2x + 6
RE: Linear Equations Homework - ngr33n - Oct-26-2017 (Oct-26-2017, 08:19 AM)buran Wrote: in I don't know why that confused me. I guess the "a" is what threw my off. Thank you. This is my updated coded by the way. Is there anything I can improve? class LinearEquations(object): def __init__(self, m, b): # print("in constructor") self.m = float(m) self.b = float(b) def __str__(self): # print("in str") # return "%2fx + %.2f" % (self.m, self.b) # optional method return "{}x + {}".format(self.m, self.b) def __repr__(self): # print("in repr") return self.__str__() def value(self, x): return self.m * x + self.b def compose(self, linear_equation): return LinearEquations(self.m * linear_equation.m, \ self.m * linear_equation.b + self.b) def __add__(self, z): """ Combine like terms by adding two linear equations together. """ return LinearEquations(self.m + z.m, self.b + z.b) # ---------------------------------------------------------------------------- # description example executed try: # create equations equation_y = LinearEquations(1, 1) equation_z = LinearEquations(2, 5) # display equations print("\nLinear Equation 1: y =", equation_y) print("Linear Equation 2: z =", equation_z) # user input for x values y_x = float(input("Enter an x value for Equation 1: ")) z_x = float(input("Enter an x value for Equation 2: ")) # value(x) method print("\ny =", equation_y.value(y_x), "if x =", y_x) # y = 1(x) + 1 print("z =", equation_z.value(z_x), "if x =", z_x) # z = 2(x) + 5 # compose(linear_equation) method print("\ny(z) =", equation_y.compose(equation_z)) print("z(y) =", equation_z.compose(equation_y)) # __add__(z) method print("\ny + z =", equation_y + equation_z) except NameError: print("Sorry, there was a name error.")I wanna say my teacher said something about an equality() method, but maybe I misheard him. There's no need for an equality method is there? RE: Linear Equations Homework - buran - Oct-27-2017 it depends - it's not in the assignment description posted in your original post. But it is definitely possible to develop equality(y,z) method. If I understand it right it will return True if two equations y and z are the same (i.e. y(x) == z(x) for any given x) and False otherwise. What bothers me is that there are the so-called “rich comparison” methods, which definitely can be developed (or most of them). However this maybe out of the scope of your curriculum... |