def loc_xtrm_1st_drv_test(expr): #step 1 take derivative myderiv = deriv(expr) #step2 solve for x zeros = find_poly_2_zeros(myderiv) # for c in zeros: # print c pf = tof(expr) extremas = [] for c in zeros: yval = abs(pf(c.get_val()) - 0.0) if (pf(c.get_val()) - 1 > 0 & pf(c.get_val()) + 1 < 0): extremas.append("min", point2d(c.get_val(), yval)) else: extremas.append("max", point2d(c.get_val(), yval)) return extremas
def max_profit(cost_fun, rev_fun): profit = plus(cost_fun, prod(rev_fun,-1)) profit_deriv = deriv(profit) zeroes = find_poly_2_zeros(profit_deriv) return max(zeroes)
def test_02(): f0 = make_prod(make_const(0.5), make_pwr('x', 2.0)) f1 = make_prod(make_const(6.0), make_pwr('x', 1.0)) f2 = make_plus(f0, f1) poly = make_plus(f2, make_const(0.0)) print '+++', poly zeros = find_poly_2_zeros(poly) for c in zeros: print c pf = tof(poly) for c in zeros: assert abs(pf(c.get_val()) - 0.0) <= 0.0001 print 'True zeros!'
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 max_norman_window_area(p): assert isinstance(p, const) hexpr = quot(plus(plus(p,prod(const(-2.0),'r')), prod(prod(np.pi,'r'), const(-1.0)))/const(2.0)) area = plus(prod(const(0.5), prod(np.pi, pwr('r', const(2.0)))), prod(prod(const(2.0),'r'), hexpr)) area_deriv = deriv(area) zeroes = find_poly_2_zeros(area_deriv) ans = area(zeroes) return max(ans)
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_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