Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*COMPLETE WHERE IT STATES # YOUR CODE HERE * THANK YOU ! *TRANSCRIBED PICTURE TEXT* Although we can take the derivatives of various expressions, the

*COMPLETE WHERE IT STATES # YOUR CODE HERE * THANK YOU !

image text in transcribed

*TRANSCRIBED PICTURE TEXT*

Although we can take the derivatives of various expressions, the results might not be exactly what we were hoping for. For example, the derivative of 5+1 with respect to should be 5, but it comes out as Plus(Plus(Multiply(0, V('y')), 5), 0). This is equivalent to 5 (because 0=0, 0+5=5, and 5+0=5), but it is rather ugly to read:

[ ]

e = 5 * V('y') + 1

e.derivative('y')

Although calling eval() would help in many cases, in this situation calling eval() on e.derivative('y') would not help, because we do not have a value for the variable V('y'). But what we can do instead is implement a method to simplify expressions like this one.

For this problem, you will write methods implementing simplification of expressions for each subclass of Expr. You should implement the following simplifications:

  • Plus(0, e) and Plus(e, 0) should simplify to e, for any e.
  • Minus(e, 0) should simplify to e, for any e.
  • Minus(0, e) should simplify to Negative(e).
  • Multiply(0, e) and Multiply(e, 0) should simplify to 0, for any e.
  • Multiply(1, e) and Multiply(e, 1) should simplify to e, for any e.
  • Divide(0, e) should simplify to 0, for any e.
  • Divide(e, 1) should simplify to e, for any e.
  • Power(1, e) should simplify to 1, for any e.
  • Power(e, 0) should simplify to 1, for any e.
  • Power(e, 1) should simplify to e, for any e.
  • Logarithm(1) should simplify to 0.
  • Negative(0) should simplify to 0.

The simplification rules should be applied recursively over the expression, so that V('x') * (1 + V('y') * 0) should be simplifed to V('x').

To get you started, we have implemented a simplify method for the Expr class, which relies on each subclass having implemented a simplify_op method. Your task is to implement these simplify_op methods for the Plus, Minus, Multiply, Divide, Logarithm, Power, and Negative classes. The methods you write should implement the simplifications described above for each expression type, and return a simplified expression. If none of the above simplifications are possible, each simplify_op method should call eval() on the expression.

Finally, in order to test your simplify_op implementations, we need to be able to check for expression equality, with a more lenient notion of equality than Python's "same object" notion. The below cell should do the job; be sure to run it before you test your code.

*COMPLETE CODE WHERE IT STATES "# YOUR CODE HERE" IN THE PROVIDED CODE*

def equality_as_same_types_and_attributes(self, other):

return type(self) == type(other) and self.__dict__ == other.__dict__

Expr.__eq__ = equality_as_same_types_and_attributes

def expr_simplify(self):

children = [c.simplify() if isinstance(c, Expr) else c

for c in self.children]

e = self.__class__(*children)

return e.simplify_op()

Expr.simplify = expr_simplify

def expr_simplify_op(self):

return self.eval()

Expr.simplify_op = expr_simplify_op

def plus_simplify_op(self):

"""Simplification of Plus expressions."""

# YOUR CODE HERE

raise NotImplementedError()

def minus_simplify_op(self):

"""Simplification of Minus expressions."""

# YOUR CODE HERE

raise NotImplementedError()

def multiply_simplify_op(self):

"""Simplification of Multiply expressions."""

# YOUR CODE HERE

raise NotImplementedError()

def divide_simplify_op(self):

"""Simplification of Divide expressions."""

# YOUR CODE HERE

raise NotImplementedError()

def power_simplify_op(self):

"""Simplification of Power expressions."""

# YOUR CODE HERE

raise NotImplementedError()

def logarithm_simplify_op(self):

"""Simplification of Logarithm expressions."""

# YOUR CODE HERE

raise NotImplementedError()

def negative_simplify_op(self):

"""Simplification of Negative expressions."""

# YOUR CODE HERE

raise NotImplementedError()

Plus.simplify_op = plus_simplify_op

Minus.simplify_op = minus_simplify_op

Multiply.simplify_op = multiply_simplify_op

Divide.simplify_op = divide_simplify_op

Logarithm.simplify_op = logarithm_simplify_op

Power.simplify_op = power_simplify_op

Negative.simplify_op = negative_simplify_op

image text in transcribed

image text in transcribed

Although we can take the derivatives of various expressions, the results might not be exactly what we were hoping for. For example, the derivative of 5y + 1 with respect to y should be 5, but it comes out as Plus (Plus (Multiply(0, vi'y')), 5), 0). This is equivalent to 5 (because 0 x y = 0,0 + 5 = 5, and 5 + 0 = 5), but it is rather ugly to read: [] e = 5 * v'y') + 1 e derivative ('y') Although calling eval() would help in many cases, in this situation calling eval() on e.derivative('y') would not help, because we do not have a value for the variable vi'y'). But what we can do instead is implement a method to simplify expressions like this one. For this problem, you will write methods implementing simplification of expressions for each subclass of Expr. You should implement the following simplifications: Plus (0, e) and Plus(e, 0) should simplify to e, for any e. Minus(e, 0) should simplify to e, for any e. Minus (0, e) should simplify to Negative(e). Multiply(0, e) and Multiply(e, 0) should simplify to o, for any e. Multiply(1, e) and Multiply(e, 1) should simplify to e, for any e. Divide (0, e) should simplify to o, for any e. Divide (e, 1) should simplify to e, for any e. Power(1, e) should simplify to 1, for any e. Power(e, 0) should simplify to 1, for any e. Power(e, 1) should simplify to e, for any e. Logarithm(1) should simplify to o. Negative(0) should simplify to o. The simplification rules should be applied recursively over the expression, so that I'x') * (1 + V('Y') * 0) should be simplifed to v('x'). To get you started, we have implemented a simplify method for the Expr class, which relies on each subclass having implemented a simplify_op method. Your task is to implement these simplify_op methods for the Plus, Minus, Multiply, Divide, Logarithm, Power, and Negative classes. The methods you write should implement the simplifications described above for each expression type, and return a simplified expression. If none of the above simplifications are possible, each simplify_op method should call eval() on the expression. Finally, in order to test your simplify_op implementations, we need to be able to check for expression equality, with a more lenient notion of equality than Python's "same object" notion. The below cell should do the job; be sure to run it before you test your code. ### Tests for simplification of Plus expressions e = v('x') + 0 assert_equal(e. simplify(), V('x')). e = 0 + V('x'). assert_equal(e.simplify(), V('x')) e = 0 + (0 + V('x')) + v('y'). assert_equal(e.simplify(), V('x') + v('y')). e = 2 + 3 + v('x') + 0 assert_equal(e.simplify ( ), 5 + V('x')). ### Tests for simplification of Minus expressions e = v('x') - 0 assert_equal(e.simplify(), V('x')). e = 0 - V('x') # Must evaluate to Negative (v('x')). assert_equal(e.simplify(), -v('x')). ### Tests for simplification of Multiply expressions e = v('x') * 1 assert_equal(e.simplify(), V('x')). e = 1 * V('x') * 1 * 1 * 1 assert_equal(e.simplify ( ), V('x')). e = 1 * v('x') * 1 * O * v('y') assert_equal(e.simplify ( ), 0) ### Tests for simplification of Divide expressions e = 0 * v('x') / 3 assert_equal(e.simplify ( ), 0) e = v('x') / 1 assert_equal(e. simplify(, V('x')). ### Tests for simplification of Power expressions e = v('x') ** O assert_equal(e.simplify(), 1) e = v('z') ** 1 assert_equal(e.simplify(), V('z')). e = 1 ** (v('x') + 2) assert_equal(e.simplify ( ), 1) ### Tests for simplification of Logarithm expressions e = Logarithm (1) assert_equal(e.simplify (), 0) e = Logarithm(1) + 0 * 3 assert_equal(e.simplify ( ), 0) e = Logarithm (1) + Logarithm(0.5) assert_almost_equal(e. simplify (), math.log(0.5)) ### Overall tests e = ((3 - 3) * ((V('x') - 2) / (v('y') - 2))) + (V('x') - 1) / (3 - 2) assert_equal(e.simplify ( ), V('x') - 1) Although we can take the derivatives of various expressions, the results might not be exactly what we were hoping for. For example, the derivative of 5y + 1 with respect to y should be 5, but it comes out as Plus (Plus (Multiply(0, vi'y')), 5), 0). This is equivalent to 5 (because 0 x y = 0,0 + 5 = 5, and 5 + 0 = 5), but it is rather ugly to read: [] e = 5 * v'y') + 1 e derivative ('y') Although calling eval() would help in many cases, in this situation calling eval() on e.derivative('y') would not help, because we do not have a value for the variable vi'y'). But what we can do instead is implement a method to simplify expressions like this one. For this problem, you will write methods implementing simplification of expressions for each subclass of Expr. You should implement the following simplifications: Plus (0, e) and Plus(e, 0) should simplify to e, for any e. Minus(e, 0) should simplify to e, for any e. Minus (0, e) should simplify to Negative(e). Multiply(0, e) and Multiply(e, 0) should simplify to o, for any e. Multiply(1, e) and Multiply(e, 1) should simplify to e, for any e. Divide (0, e) should simplify to o, for any e. Divide (e, 1) should simplify to e, for any e. Power(1, e) should simplify to 1, for any e. Power(e, 0) should simplify to 1, for any e. Power(e, 1) should simplify to e, for any e. Logarithm(1) should simplify to o. Negative(0) should simplify to o. The simplification rules should be applied recursively over the expression, so that I'x') * (1 + V('Y') * 0) should be simplifed to v('x'). To get you started, we have implemented a simplify method for the Expr class, which relies on each subclass having implemented a simplify_op method. Your task is to implement these simplify_op methods for the Plus, Minus, Multiply, Divide, Logarithm, Power, and Negative classes. The methods you write should implement the simplifications described above for each expression type, and return a simplified expression. If none of the above simplifications are possible, each simplify_op method should call eval() on the expression. Finally, in order to test your simplify_op implementations, we need to be able to check for expression equality, with a more lenient notion of equality than Python's "same object" notion. The below cell should do the job; be sure to run it before you test your code. ### Tests for simplification of Plus expressions e = v('x') + 0 assert_equal(e. simplify(), V('x')). e = 0 + V('x'). assert_equal(e.simplify(), V('x')) e = 0 + (0 + V('x')) + v('y'). assert_equal(e.simplify(), V('x') + v('y')). e = 2 + 3 + v('x') + 0 assert_equal(e.simplify ( ), 5 + V('x')). ### Tests for simplification of Minus expressions e = v('x') - 0 assert_equal(e.simplify(), V('x')). e = 0 - V('x') # Must evaluate to Negative (v('x')). assert_equal(e.simplify(), -v('x')). ### Tests for simplification of Multiply expressions e = v('x') * 1 assert_equal(e.simplify(), V('x')). e = 1 * V('x') * 1 * 1 * 1 assert_equal(e.simplify ( ), V('x')). e = 1 * v('x') * 1 * O * v('y') assert_equal(e.simplify ( ), 0) ### Tests for simplification of Divide expressions e = 0 * v('x') / 3 assert_equal(e.simplify ( ), 0) e = v('x') / 1 assert_equal(e. simplify(, V('x')). ### Tests for simplification of Power expressions e = v('x') ** O assert_equal(e.simplify(), 1) e = v('z') ** 1 assert_equal(e.simplify(), V('z')). e = 1 ** (v('x') + 2) assert_equal(e.simplify ( ), 1) ### Tests for simplification of Logarithm expressions e = Logarithm (1) assert_equal(e.simplify (), 0) e = Logarithm(1) + 0 * 3 assert_equal(e.simplify ( ), 0) e = Logarithm (1) + Logarithm(0.5) assert_almost_equal(e. simplify (), math.log(0.5)) ### Overall tests e = ((3 - 3) * ((V('x') - 2) / (v('y') - 2))) + (V('x') - 1) / (3 - 2) assert_equal(e.simplify ( ), V('x') - 1)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions