Question: I need help implementing the test _ derivative, to _ fraction, and is _ in _ fraction _ form functions ( in python ) .

I need help implementing the test_derivative, to_fraction, and is_in_fraction_form functions(in python). They should pass the given tests. Code: import math
import random
class IllegalOperator(Exception):
pass
def calc(op,left,right):
if op=="+":
return left+right
elif op=="-":
return left-right
elif op=="*":
return left*right
elif op =="/":
return left/right
else:
raise IllegalOperator(op)
def compute(e):
if isinstance(e,tuple):
# We have an expression.
op,l,r=e
# We compute the subexpressions.
ll=compute(l)
rr=compute(r)
# And on the basis of those,the whole expression.
return calc(op,ll,rr)
else:
# base expression; just return the number.
return e
from numbers import Number
def isnumber(e):
return isinstance(e,Number)
def isvariable(e):
return isinstance(e,str)
def iscomposite(e):
return isinstance(e,tuple)
def simplify(e):
if isinstance(e,tuple):
op, l, r = e
ll=simplify(l)
rr=simplify(r)
if isnumber(ll) and isnumber(rr):
return calc(op,ll,rr)
else:
return (op,ll,rr)
else:
return e
varval ={'x': 3,'y': 8}
#@title Evaluating an expression with respect to a variable evaluation
def compute(e, varval={}):
if isinstance(e,tuple):
op,left,right = e
new_e = op,compute(left,varval),compute(right,varval)
return simplify(new_e)
elif e in varval:
return varval[e]
else:
return e
#@title Exercise: define `variables`
def variables(e):
var_set = set()
operation =['+','-','*','/']
if iscomposite(e):
for i in e:
if iscomposite(i):
a = variables(i)
var_set = a | var_set
if isvariable(i) and i not in operation:
var_set.add(i)
return var_set
else:
return {e}
#@title Exercise: implementation of value equality
def value_equality(e,f,num_samples=1000,tolerance=1e-6):
"""Return True if the two expressions self and other are numerically
equivalent. Equivalence is tested by generating
num_samples assignments,and checking that equality holds
for all of them. Equality is checked up to tolerance,that is,
the values of the two expressions have to be closer than tolerance."""
vars = variables(e)| variables(f)
for i in range(num_samples):
x ={var: random.gauss(0,10) for var in vars}
if abs(compute(e,x)-compute(f,x))>tolerance:
return False
return True
#@title Derivation of a leaf expression
def derivate_leaf(e,x):
"""This function takes as input an expression e and a variable x,
and returns the symbolic derivative of e wrt. x,as an expression."""
if isvariable(x) and isvariable(e):
if x in e:
return 1
return 0
#@title Implement `derivate`
def derivate(e,x):
"""Returns the derivative of e wrt x."""
if not iscomposite(e):
return derivate_leaf(e,x)
op,f,g = e
df = derivate(f,x) if iscomposite(f) else derivate_leaf(f,x)
dg = derivate(g,x) if iscomposite(g) else derivate_leaf(g,x)
if op in '+-':
return (op,df,dg)
elif op =='*':
return ('+',('*',df,g),('*',f,dg))
elif op =='/':
return ('/',('-',('*',df,g),('*',f,dg)),('*',g,g))
def derivate_approx(f,x,varval,delta=0.0001):
"""Computes the derivative of f with respect to x,for a given delta,
using the (f(x + delta)- f(x))/ delta method. """
# This is f(x)
f_x = compute(f,varval=varval)
varval_delta = dict(varval)
varval_delta[x]+= delta
# This is f(x + delta)
f_x_plus_delta = compute(f,varval=varval_delta)
return (f_x_plus_delta - f_x)/ delta
def similar(x,y,epsilon):
if x 0 and y 0:
return similar(-x,-y,epsilon)
if abs(x - y) epsilon:
return True
else:
return max(x,y)/(min(x,y)+ epsilon)1+ epsilon
#@title Implementation of `test_derivative`
def test_derivative(f,df,x,delta=0.0001,tolerance=0.01,num_tests=1000):
'''df_found = derivate(f,x)
return value_equality(df_found,df)'''
s = True
for i in range(num_tests):
test_point = x + delta *(2* random.random()-1)
y =(f(test_point + delta)- f(test_point))/ delta
if abs(df(test_point)- y)> tolerance:
s = False
break
return s
# Tests 10 points: `test_derivative`
f =("+",("*","cat","cat"),("*","dog","cat"))
df1=("+",("*",2,"cat"),"dog")
df2=("+",("*",2,"cat"),("*","dog","cat"))
assert test_derivative(f,df1,"cat")== True
assert test_derivative(f,df2,"cat")== False
assert test_derivative(f,df1,"dog")== False
assert test_derivative(f,df1,"donkey")== False
assert test_derivative(f,0,"donkey")== True
#@title Putting expressions in fraction form.
def to_fraction(e):
"""Returns the expression e converted to fraction form."""
### SOLUTION HERE
'''if isinstance(e,
 I need help implementing the test_derivative, to_fraction, and is_in_fraction_form functions(in python).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!