Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PLEASE FILL ###YOUR CODE HERE THANK YOU ITS GREATLY APPRECIATED :) PYTHON QUESTION 5 ### Derivation of variables def v_derivate(self, x): ### YOUR CODE HERE
PLEASE FILL ###YOUR CODE HERE THANK YOU ITS GREATLY APPRECIATED :) PYTHON
QUESTION 5
### Derivation of variables
def v_derivate(self, x):
### YOUR CODE HERE
V.derivate = v_derivate
QUESTION 6
### Derivative of Minus
def minus_derivate(self, x):
### YOUR CODE HERE
Minus.derivate = minus_derivate
- Defining an expression class We define an abstract class Expr, representing a generic expression. This generic class has as subclasses the classes that represent the various operators, such as Plus, Minus, Multiply, as well as the variable class v. Here is our basic implementation. class Expr(object) ""Abstract class representing expressions" name = "expr" # Not used, but just to define it. def __init__(self, *args) : *** An object is created by passing to the constructor the children self.children = args self.value = None The value of the expression self.child_values = None # The values of the children; useful to have det eval(self): Evaluates the expression." # First, we evaluate the children. self.child_values = [c.eval() if isinstance(C, Expr) else for c in self.children] # Then, we evaluate the expression itself. self.value = self.op(*self.child_values) return self.value def op(sel+): ***This operator must be implemented in subclasses; it should compute self.value from self.values, thus implementing the operator at the expression node." raise Not ImplementedError() det __repr_(self): ***Represents the expression in a somewhat readable way."-- if len(self.children) == 1: #Unary operators return "({}})". format(self._class_.name, self.children[@]) elif len(self.children) == 2: return "({}{} })".Format( self.children[@], self._class__.name, self.children(1) # Catch-all. return "{}(0)".Format(self._class_ __name__, ...join(repr(c) for c in self.children)) # Expression constructors def __add__(self, other): return Plus(self, other) def _radd__(self, other): return Plus(self, other) def __sub_(self, other): return Minus(self, other) def __sub_(self, other): return Minus (other, selt) det __tul_(self, other): return Multiply(self, other) det rul_(self, other): return Multiply(other, self) det __truediv_(self, other): return Divide(self, other) def _rtruediv_(self, other): return Divide (other, self) def __neg__(self): return Negative(self) Variables are created specifying a name, and an initial value. If nothing is specified, variables have random initial values. You can assign a value to a variable using the assign method. The eval method of a variable simply returns its value. [24] import randon import string class V(Expr): - "Variable." der _init__(self, value=None): super() . __init_o self.children - self.value = randon.gauss(e, 1) if value is None else value self.name = ".join( randon.choices(string.ascii_letters + string.digits, k=3) det eval(selt): return self.value def assign(self, value): self.value = value def __repr_(self): return "co, value={})".Format(self.name, self.value) Here are the constructors for the other operators; for them, we just need to provide an implementation for op, since all the rest is inherited from Expr. We actually also define a name, as a class attribute, that we use in the representation method. [25] class Plus (Expr): name = " det op(self, x, y): return x + y class Minus (Expr): name = "." det op(self, x, y): return x - y class Multiply(Expr): name = "*" det op(selt, x, y): return xy class Divide(Expr): name = "/" det op(self, x, y): return x/y class Negative(Expr): name = "." def opselt, x): return - Alan innby Why is the result False? Python knows how to compare objects that belong to its own types. So you can do comparisons between strings, numbers, tuples, and more, and it all works as expected. This is why we could check equality of expressions represented as trees: those expression trees are composed entirely of standard Python types, namely, strings, numbers, and tuples. However, Expr, v, etc, are classes we defined, and Python has no idea of what it means for objects of user-defined classes to be equal. In this case, Python defaults to considering equal two objects if they are the same object. The two expressions et and e2 above are not the same object: they are two distinct objects, which just happen to represent the same expression. If we want to have a notion of expression equality that represents our idea that two expression objects are equal if they represent the same expression", we need to define equality ourselves. This can be easily done, by defining an__eq -- method. This method has the form def _c9__(self, other): returnStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started