Question
Python Problem: Problem 1: Computing derivatives of expressions During lecture, we wrote a method derivative such that, for an expression e, the method call e.derivative('x')
Python Problem:
Problem 1: Computing derivatives of expressions
During lecture, we wrote a method derivative such that, for an expression e, the method call e.derivative('x') returns the derivative of the expression with respect to V('x').
To get the derivative of a composite expression with respect to a variable, we need to get the derivative of the subexpressions with respect to that same variable. Thus, we divide the work as follows:
- The method derivative first calls the method derivative for all the subexpressions, that is, the children of the expression, accumulating the results into a list partials (so called because these are partial derivatives).
- Then, the method derivative calls op_derivative. This method is an abstract method of Expr, and is implemented in each subclass (V, Plus, Minus, and so on). The method op_derivative will compute the derivative of the expression, according to the operator type.
We defined the derivative method for the Expr class, and we began implementing op_derivative for each operator, but during lecture we only wrote op_derivative for some of the subclasses of Expr. In particular, we implemented it for the V, Plus, and Multiply subclasses of Expr.
Below is the code that defines the derivative methods for the Expr class and the V class, and defines the op_derivative methods for the Plus, Negative, and Multiply subclasses of Expr. How does it work? partials is a list that contains the partial derivatives of every subexpression of an expression, computed in the Expr class. The op_derivative methods for each operator all combine the elements of partials to construct the partial derivative in whichever way is appropriate for that operator.
def expr_derivative(self, var):
"""Computes the derivative of the expression with respect to var."""
partials = [(c.derivative(var) if isinstance(c, Expr) else 0)
for c in self.children]
return self.op_derivative(var, partials).eval()
def expr_op_derivative(self, var, partials):
raise NotImplementedError()
# Extends the existing Expr class with the newly defined methods.
Expr.derivative = expr_derivative
Expr.op_derivative = expr_op_derivative
def variable_derivative(self, var):
return 1 if self.children[0] == var else 0
# Extends the existing V class with the newly defined method.
V.derivative = variable_derivative
def plus_op_derivative(self, var, partials):
return Plus(partials[0], partials[1])
# Extends the existing Plus class...you get the idea.
Plus.op_derivative = plus_op_derivative
def negative_op_derivative(self, var, partials):
return Negative(partials[0])
Negative.op_derivative = negative_op_derivative
def multiply_op_derivative(self, var, partials):
return Plus(
Multiply(partials[0], self.children[1]),
Multiply(partials[1], self.children[0])
)
Multiply.op_derivative = multiply_op_derivative
class Plus (Expr): def op (self, x, y): return x + y class Minus (Expr): def op(self, x, y): return x - y class Multiply(Expr): def op (self, x, y): return x * y class Divide(Expr): def op(self, x, y): return x/y class Power(Expr): def op(self, x, y): return x ** y class Negative(Expr): def op (self, x): return -X However, we are still missing op derivative methods for several classes. For this problem, you will implement the missing op derivative methods for the Minus and Divide classes, making it possible for us to take the derivative of expressions that involve subtraction and division. You should make use of the following definitions (which we saw in lecture, as well as in the previous homework): en (5 9) = Hints: Minus.op_derivative is very similar to Plus.op_derivative and can be implemented in one line of code. For Divide.op_derivative, keep in mind that self is the Divide expression you are dealing with, and that its subexpressions can be found in the list self.children. For instance, for the expression Divide(5, V('x')), children[0] is 5 and children[1] is v('x'). You can find the partial derivatives of each of the children in the partials list. def minus_op_derivative(self, var, partials): ""'"Implements derivative for Minus expressions."" # YOUR CODE HERE raise NotImplementedError() def divide_op_derivative(self, var, partials): "Implements derivative for Divide expressions."" # YOUR CODE HERE raise NotImplementedError() Minus.op_derivative = minus op derivative Divide.op_derivative = divide op derivative class Plus (Expr): def op (self, x, y): return x + y class Minus (Expr): def op(self, x, y): return x - y class Multiply(Expr): def op (self, x, y): return x * y class Divide(Expr): def op(self, x, y): return x/y class Power(Expr): def op(self, x, y): return x ** y class Negative(Expr): def op (self, x): return -X However, we are still missing op derivative methods for several classes. For this problem, you will implement the missing op derivative methods for the Minus and Divide classes, making it possible for us to take the derivative of expressions that involve subtraction and division. You should make use of the following definitions (which we saw in lecture, as well as in the previous homework): en (5 9) = Hints: Minus.op_derivative is very similar to Plus.op_derivative and can be implemented in one line of code. For Divide.op_derivative, keep in mind that self is the Divide expression you are dealing with, and that its subexpressions can be found in the list self.children. For instance, for the expression Divide(5, V('x')), children[0] is 5 and children[1] is v('x'). You can find the partial derivatives of each of the children in the partials list. def minus_op_derivative(self, var, partials): ""'"Implements derivative for Minus expressions."" # YOUR CODE HERE raise NotImplementedError() def divide_op_derivative(self, var, partials): "Implements derivative for Divide expressions."" # YOUR CODE HERE raise NotImplementedError() Minus.op_derivative = minus op derivative Divide.op_derivative = divide op derivativeStep 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