Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please Code In Python, Thank You Very Much ! *TRANSCRIBED 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

Please Code In Python, Thank You Very Much !

image text in transcribed

image text in transcribed

image text in transcribed

*TRANSCRIBED 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

*** TEST CASES TO MAKE SURE CODE WORKS***

### 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)

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, V('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: U 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(i, 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 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. 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 Not ImplementedError() def minus_simplify_op(self): "" "Simplification of Minus expressions." # YOUR CODE HERE raise Not ImplementedError() def multiply_simplify_op(self): """Simplification of Multiply expressions."" # YOUR CODE HERE raise Not ImplementedError() def divide_simplify_op(self): """Simplification of Divide expressions." # YOUR CODE HERE raise Not ImplementedError() 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 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, V('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: U 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(i, 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 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. 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 Not ImplementedError() def minus_simplify_op(self): "" "Simplification of Minus expressions." # YOUR CODE HERE raise Not ImplementedError() def multiply_simplify_op(self): """Simplification of Multiply expressions."" # YOUR CODE HERE raise Not ImplementedError() def divide_simplify_op(self): """Simplification of Divide expressions." # YOUR CODE HERE raise Not ImplementedError() 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

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

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions

Question

What problems have I solved? What skills did that show?

Answered: 1 week ago

Question

What appraisal intervals are often used in appraisal reviews?

Answered: 1 week ago

Question

What are the various alternatives?

Answered: 1 week ago