Question: I need help implementing the test _ derivative, to _ fraction, and is _ in _ fraction _ form functions ( in python ) .
I need help implementing the testderivative, tofraction, and isinfractionform functionsin python They should pass the given tests. Code: import math
import random
class IllegalOperatorException:
pass
def calcopleft,right:
if op:
return leftright
elif op:
return leftright
elif op:
return leftright
elif op :
return leftright
else:
raise IllegalOperatorop
def computee:
if isinstanceetuple:
# We have an expression.
oplre
# We compute the subexpressions.
llcomputel
rrcomputer
# And on the basis of those,the whole expression.
return calcopllrr
else:
# base expression; just return the number.
return e
from numbers import Number
def isnumbere:
return isinstanceeNumber
def isvariablee:
return isinstanceestr
def iscompositee:
return isinstanceetuple
def simplifye:
if isinstanceetuple:
op l r e
llsimplifyl
rrsimplifyr
if isnumberll and isnumberrr:
return calcopllrr
else:
return opllrr
else:
return e
varval x: y:
#@title Evaluating an expression with respect to a variable evaluation
def computee varval:
if isinstanceetuple:
opleft,right e
newe opcomputeleftvarvalcomputerightvarval
return simplifynewe
elif e in varval:
return varvale
else:
return e
#@title Exercise: define variables
def variablese:
varset set
operation
if iscompositee:
for i in e:
if iscompositei:
a variablesi
varset a varset
if isvariablei and i not in operation:
varset.addi
return varset
else:
return e
#@title Exercise: implementation of value equality
def valueequalityefnumsamplestolerancee:
Return True if the two expressions self and other are numerically
equivalent. Equivalence is tested by generating
numsamples 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 variablese variablesf
for i in rangenumsamples:
x var: random.gauss for var in vars
if abscomputeexcomputefxtolerance:
return False
return True
#@title Derivation of a leaf expression
def derivateleafex:
This function takes as input an expression e and a variable x
and returns the symbolic derivative of e wrt xas an expression."""
if isvariablex and isvariablee:
if x in e:
return
return
#@title Implement derivate
def derivateex:
Returns the derivative of e wrt x
if not iscompositee:
return derivateleafex
opfg e
df derivatefx if iscompositef else derivateleaffx
dg derivategx if iscompositeg else derivateleafgx
if op in :
return opdfdg
elif op :
return dfgfdg
elif op :
return dfgfdggg
def derivateapproxfxvarval,delta:
Computes the derivative of f with respect to xfor a given delta,
using the fx delta fx delta method.
# This is fx
fx computefvarvalvarval
varvaldelta dictvarval
varvaldeltax delta
# This is fx delta
fxplusdelta computefvarvalvarvaldelta
return fxplusdelta fx delta
def similarxyepsilon:
if x and y :
return similarxyepsilon
if absx y epsilon:
return True
else:
return maxxyminxy epsilon epsilon
#@title Implementation of testderivative
def testderivativefdfxdeltatolerancenumtests:
dffound derivatefx
return valueequalitydffound,df
s True
for i in rangenumtests:
testpoint x delta random.random
y ftestpoint delta ftestpoint delta
if absdftestpoint y tolerance:
s False
break
return s
# Tests points: testderivative
f "cat","cat""dog","cat"
df"cat""dog"
df"cat""dog","cat"
assert testderivativefdf"cat" True
assert testderivativefdf"cat" False
assert testderivativefdf"dog" False
assert testderivativefdf"donkey" False
assert testderivativef"donkey" True
#@title Putting expressions in fraction form.
def tofractione:
Returns the expression e converted to fraction form."""
### SOLUTION HERE
if isinstancee
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
