Question: Thank You In Advance For Coding In Python! Context: We have not yet implemented a way to take derivatives of Power expressions, that is, expressions
Thank You In Advance For Coding In Python!
Context:

We have not yet implemented a way to take derivatives of Power expressions, that is, expressions involving exponentiation. As we saw during lecture, here's the definition of the derivative of an expression with respect to :
*DEFINITION CAN BE SEEN IN PICTURE ABOVE
To translate this into code, we need Logarithm to be one of our operators. Without it, the set of symbolic expressions would not be closed with respect to symbolic differentiation. We'd better add Logarithm to our collection of operators, and define a way to take the derivative of Logarithm expressions, too.
Using
*CAN ALSO BE SEEN IN PICTURE ABOVE*
we define Logarithm to be a subclass of Expr, with op and op_derivative methods, as seen during lecture:
import math
class Logarithm(Expr):
def op(self, x):
return math.log(x)
def op_derivative(self, var, partials):
return Multiply(
Divide(1, self.children[0]),
partials[0]
)

Now we have everything we need to take derivatives of Power expressions. For this problem, you will implement the op_derivative method for the Power class, making use of Logarithm.
*CODE GIVEN/PROBLEM*
def power_op_derivative(self, var, partials):
"""Implements derivative for Divide expressions.
Should take no more than 5 lines of code to write."""
# YOUR CODE HERE
raise NotImplementedError()
Power.op_derivative = power_op_derivative
*TESTS*
### Tests for `Power.op_derivative`
## We test your code by taking the derivative of expressions involving exponentiation,
## then evaluating the resulting expression for particular values of the variables
# The derivative of x**2 with respect to x is 2x, which is equal to 6 when x = 3
e = V('x') ** 2
assert_almost_equal(e.derivative('x').eval(dict(x=3)), 6)
# The derivative of x**2 with respect to y is 0
e = V('x') ** 2
assert_almost_equal(e.derivative('y').eval(dict(x=3)), 0)
### More tests for `Power.op_derivative`
e = 3 ** V('x')
assert_almost_equal(e.derivative('x').eval(dict(x=4)),
math.log(3) * (3 ** 4), places=2)
e = V('x') ** 2.8
assert_almost_equal(e.derivative('x').eval(dict(x=3)), 2.8 * 3 ** 1.8, places=2)
We have not yet implemented a way to take derivatives of Power expressions, that is, expressions involving exponentiation. As we saw during lecture, here's the definition of the derivative of an expression fs with respect to x: 788 of Og log \f 0x " To translate this into code, we need Logarithm to be one of our operators. Without it, the set of symbolic expressions would not be closed with respect to symbolic differentiation. We'd better add Logarithm to our collection of operators, and define a way to take the derivative of Logarithm expressions, too. Using dowlog sa iyo we define Logarithm to be a subclass of Expr, with op and op derivative methods, as seen during lecture: [] import math class Logarithm (Expr): def op (self,x): return math.log(x) def op derivative (self, var, partials): return Multiply Divide(1, self.children[0]), partials[0] Now we have everything we need to take derivatives of Power expressions. For this problem, you will implement the op_derivative method for the Power class, making use of Logarithm. [] def power_op_derivative (self, var, partials): """Implements derivative for Divide expressions. Should take no more than 5 lines of code to write # YOUR CODE HERE raise NotImplementedError() Power.op_derivative = power_op_derivative ### Feel free to use this cell to write your own tests. ### You will not be graded on what you write here. [ ] ### Tests for "Power.op_derivative ## We test your code by taking the derivative of expressions involving exponentiation, ## then evaluating the resulting expression for particular values of the variables # The derivative of x**2 with respect to x is 2x, which is equal to 6 when x = 3 e = v('x') ** 2 assert_almost_equal(e. derivative('x').eval(dict(x=3)), 6) # The derivative of ***2 with respect to y is 0 e = v('x') ** 2 assert_almost_equal(e. derivative('Y').eval(dict(x=3)), 0) U ### More tests for "Power.op_derivative e = 3 ** v('x') assert_almost_equal(e. derivative('x').eval(dict(x=4)), math.log(3) * (3 ** 4), places=2) e = v('x') ** 2.8 assert_almost_equal(e. derivative('x').eval(dict(x=3)), 2.8 * 3 ** 1.8, places=2) We have not yet implemented a way to take derivatives of Power expressions, that is, expressions involving exponentiation. As we saw during lecture, here's the definition of the derivative of an expression fs with respect to x: 788 of Og log \f 0x " To translate this into code, we need Logarithm to be one of our operators. Without it, the set of symbolic expressions would not be closed with respect to symbolic differentiation. We'd better add Logarithm to our collection of operators, and define a way to take the derivative of Logarithm expressions, too. Using dowlog sa iyo we define Logarithm to be a subclass of Expr, with op and op derivative methods, as seen during lecture: [] import math class Logarithm (Expr): def op (self,x): return math.log(x) def op derivative (self, var, partials): return Multiply Divide(1, self.children[0]), partials[0] Now we have everything we need to take derivatives of Power expressions. For this problem, you will implement the op_derivative method for the Power class, making use of Logarithm. [] def power_op_derivative (self, var, partials): """Implements derivative for Divide expressions. Should take no more than 5 lines of code to write # YOUR CODE HERE raise NotImplementedError() Power.op_derivative = power_op_derivative ### Feel free to use this cell to write your own tests. ### You will not be graded on what you write here. [ ] ### Tests for "Power.op_derivative ## We test your code by taking the derivative of expressions involving exponentiation, ## then evaluating the resulting expression for particular values of the variables # The derivative of x**2 with respect to x is 2x, which is equal to 6 when x = 3 e = v('x') ** 2 assert_almost_equal(e. derivative('x').eval(dict(x=3)), 6) # The derivative of ***2 with respect to y is 0 e = v('x') ** 2 assert_almost_equal(e. derivative('Y').eval(dict(x=3)), 0) U ### More tests for "Power.op_derivative e = 3 ** v('x') assert_almost_equal(e. derivative('x').eval(dict(x=4)), math.log(3) * (3 ** 4), places=2) e = v('x') ** 2.8 assert_almost_equal(e. derivative('x').eval(dict(x=3)), 2.8 * 3 ** 1.8, places=2)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
