def test_01(): f1 = make_prod(make_const(3.0), make_pwr('x', 1.0)) f2 = make_plus(f1, make_const(100.0)) print f2 z = find_poly_1_zeros(f2) f2f = tof(f2) assert f2f(z.get_val()) == 0.0 print str(z) print 'true zero?', f2f(z.get_val()) == 0.0
def loc_xtrm_1st_drv_test(expr): exprFn = tof(expr) derivativeExpr = deriv(expr) derivfn = tof(derivativeExpr) degree = findDegree(derivativeExpr) critical_points = [] if degree == 2: xvalues = find_poly_2_zeros(derivativeExpr) for x in xvalues: y = exprFn(x.get_val()) critical_points.append(make_point2d(x.get_val(), y)) elif degree == 1: x = find_poly_1_zeros(derivativeExpr) y = exprFn(x.get_val()) critical_points.append(make_point2d(x.get_val(), y)) elif degree == 0: # The derivative is just a constant so all values will be just the constant # f` = 5 x = derivativeExpr.get_val() else: raise Exception("Not a first or second degree polynomial degree=", degree) maxima = None minima = None for p in critical_points: x = p.get_x().get_val() less = derivfn(x - 0.5) more = derivfn(x + 0.5) if less < 0 and more > 0: y = exprFn(x) minima = make_point2d(x, y) elif less > 0 and more < 0: y = exprFn(x) maxima = make_point2d(x, y) if not maxima and not minima: return None else: results = [] if maxima: results.append(("max", maxima)) if minima: results.append(("min", minima)) return results
def find_infl_pnts(expr): drv = deriv(expr) drv_2 = deriv(drv) # print 'f`(x) = ', drv # print 'f``(x) = ', drv_2 zeros = None try: zeros = find_poly_1_zeros(drv_2) # print '1st degree poly' except Exception: # print 'not a 1st degree poly' pass try: zeros = find_poly_2_zeros(drv_2) # print '2nd degree poly' except Exception: # print 'not a 2nd degree poly' pass if isinstance(zeros, const): zeros = [zeros] # print 'zeros:' # for z in zeros: # print ':',z.get_val() f = tof(expr) f_dp = tof(drv_2) delta = 0.1 points = [] #will store array of points and if they are max/min #zeros may be None if no inflection points if not zeros == None: for z in zeros: z_val = f(z.get_val()) z_minus = f_dp(z.get_val() - delta) z_plus = f_dp(z.get_val() + delta) if z_minus < 0 and z_plus > 0: points.append(point2d(z, const(z_val))) if z_minus > 0 and z_plus < 0: points.append(point2d(z, const(z_val))) else: return None if points == []: return None else: # print points return points
def find_infl_pnts(expr): deriv1 = deriv(expr) deriv2 = deriv(deriv1) zero = find_poly_1_zeros(deriv2) func = tof(expr) answers = abs(func(zero.get_val()) - 0.0) # add answers to iflection points inflpoints = [answers] return inflpoints
def find_infl_pnts(expr): #find the second derivative second_drv = deriv(deriv(expr)) degree = findDegree(second_drv) expr_tof = tof(expr) inflection_points = [] if degree == 2: zeros = find_poly_2_zeros(second_drv) for x in zeros: y = expr_tof(x.get_val()) inflection_points.append(make_point2d(x.get_val() , y)) else: x = find_poly_1_zeros(second_drv) y = expr_tof(x.get_val()) inflection_points.append(make_point2d(x.get_val(), y)) return inflection_points
def loc_xtrm_2nd_drv_test(expr): drv = deriv(expr) drv_2 = deriv(drv) # print 'f`(x) = ', drv # print 'f``(x) = ', drv_2 zeros = None try: zeros = find_poly_1_zeros(drv) # print '1st degree poly' except Exception: print('not a 1st degree poly') pass try: zeros = find_poly_2_zeros(drv) # print '2nd degree poly' except Exception: print('not a 2nd degree poly') pass if isinstance(zeros, const): zeros = [zeros] print('zeros:') for z in zeros: print(':', z.get_val()) f = tof(expr) f_dp = tof(drv_2) points = [] #will store array of points and if they are max/min for z in zeros: z_val = f(z.get_val()) z_f_dp = f_dp(z.get_val()) if z_f_dp < 0: points.append(('max', point2d(z, const(z_val)))) if z_f_dp > 0: points.append(('min', point2d(z, const(z_val)))) if points == []: return None else: # print points return points
def loc_xtrm_2nd_drv_test(expr): firstDeriv = deriv(expr) secondDeriv = deriv(firstDeriv) finalyval = secondDeriv.get_elt2() zeros = find_poly_1_zeros(secondDeriv) pf = tof(secondDeriv) extremas = [] for c in zeros: if (pf(c.get_val()) > 0): extremas.append(tuple("min", point2d(c.get_val(), finalyval))) else: extremas.append("max", tuple("max", point2d(c.get_val(), finalyval))) return extremas
def loc_xtrm_1st_drv_test(expr): drv = deriv(expr) # print 'f`(x) = ', drv zeros = None try: zeros = find_poly_1_zeros(drv) # print '1st degree poly' except Exception: # print 'not a 1st degree poly' pass try: zeros = find_poly_2_zeros(drv) # print '2nd degree poly' except Exception: # print 'not a 2nd degree poly' pass if isinstance(zeros, const): zeros = [zeros] # print 'zeros:' # for z in zeros: # print ':',z.get_val() f = tof(expr) delta = 0.1 points = [] #will store array of points and if they are max/min for z in zeros: z_val = f(z.get_val()) z_minus = f(z.get_val() - delta) z_plus = f(z.get_val() + delta) if z_minus < z_val and z_plus < z_val: points.append(('max', point2d(z, const(z_val)))) if z_minus > z_val and z_plus > z_val: points.append(('min', point2d(z, const(z_val)))) if points == []: return None else: # print points return points
from plus import plus from tof import tof # from graphdrv import graph_drv from maker import make_const, make_pwr, make_pwr_expr, make_prod, make_plus, make_point2d import math from poly12 import find_poly_1_zeros, find_poly_2_zeros from point2d import point2d from derivtest import loc_xtrm_1st_drv_test, loc_xtrm_2nd_drv_test fex = plus(prod(mult1=make_const(2.0), mult2=make_pwr('x', 1.0)), make_const(5.0)) print fex # drv = deriv(fex) # print drv z = find_poly_1_zeros(fex) print isinstance(z, const) print z #verify this is a true zero f = tof(fex) print 'true zero?', f(z.get_val()) == 0.0 print '------------------------------------------------------' def test_01(): f1 = make_prod(make_const(3.0), make_pwr('x', 1.0)) f2 = make_plus(f1, make_const(100.0)) print f2 z = find_poly_1_zeros(f2) f2f = tof(f2) assert f2f(z.get_val()) == 0.0