Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I Am Severely Stuck And Would Appreciate The Help! In Python Please. Although we can take the derivatives of various expressions, the results might not

I Am Severely Stuck And Would Appreciate The Help! In Python Please.

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.

CODE PROVIDED:

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

FOLLOWING CODES ARE JUST FOR TESTING PURPOSES:

### 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 * 0 * 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') ** 0

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_2

Step: 3

blur-text-image_3

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

DB2 11 The Ultimate Database For Cloud Analytics And Mobile

Authors: John Campbell, Chris Crone, Gareth Jones, Surekha Parekh, Jay Yothers

1st Edition

1583474013, 978-1583474013

More Books

Students also viewed these Databases questions

Question

What is the reversal journal entry for mark-to-market?

Answered: 1 week ago

Question

2. Describe how technology can impact intercultural interaction.

Answered: 1 week ago

Question

7. Define cultural space.

Answered: 1 week ago