def test_10(): f1 = lambda x, y: 2*x + y corner_points = [make_point2d(1, 1), make_point2d(1, 5), make_point2d(5, 1)] print(maximize_obj_fun(f1, corner_points)) f2 = lambda x, y: x - 2*y print(minimize_obj_fun(f2, corner_points))
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): #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 minimize_obj_fun(f, corner_points): currentMin = 1000000 for points in corner_points: min = f(points.get_x().get_val(), points.get_y().get_val()) if min < currentMin: currentMin = min min_point = make_point2d(points.get_x().get_val(), points.get_y().get_val()) return min_point
def maximize_obj_fun(f, corner_points): currentMax = 0 for points in corner_points: max = f(points.get_x().get_val(), points.get_y().get_val()) if max > currentMax: currentMax = max max_point = make_point2d(points.get_x().get_val(), points.get_y().get_val()) return max_point
def line_intersection(lneq1, lneq2): # Case 1: 2 const lines if is_const_line(lneq1): if is_const_line(lneq2): if lneq1.get_lhs().get_name() == 'x': x = lneq1.get_rhs().get_val() y = lneq2.get_rhs().get_val() elif lneq1.get_lhs().get_name() == 'y': y = lneq1.get_rhs().get_val() x = lneq2.get_rhs().get_val() else: raise Exception('line_intersection: ' + str(lneq1)) else: y = lneq1.get_rhs().get_val() x = tof(lneq2.get_rhs())(y) elif is_const_line(lneq2): #Case 2: 1 const line y = 1 ;y = x -1 y = lneq2.get_rhs().get_val() x = tof(lneq1.get_rhs())(y) elif isinstance(lneq1.get_rhs(), pwr): #y = 1x; y = -1x +6 eq1_coeff = get_line_coeffs(lneq1) eq2_coeff = get_line_coeffs(lneq2) if isinstance(lneq2.get_rhs(), plus): if isinstance(lneq2.get_rhs().get_elt2(), const): eq2_const = lneq2.get_rhs().get_elt2().get_val() x = eq2_const / (eq1_coeff - eq2_coeff) y = tof(lneq1.get_rhs())(x) elif isinstance(lneq1.get_rhs(), plus): #y = -0.2x+10; y =0.2x+5 eq1_coeff = get_line_coeffs(lneq1) eq2_coeff = get_line_coeffs(lneq2) if isinstance(lneq2.get_rhs(), plus): x = (lneq2.get_rhs().get_elt2().get_val() + lneq1.get_rhs().get_elt2().get_val()) / (eq1_coeff - eq2_coeff) y = tof(lneq1.get_rhs())(x) else: raise Exception("Unknown plus equation") elif isinstance(lneq1.get_rhs(), prod): #y = 0.5x; y = -0.75x +3 eq1_coeff = get_line_coeffs(lneq1) eq2_coeff = get_line_coeffs(lneq2) if isinstance(lneq2.get_rhs(), plus): eq2_const = lneq2.get_rhs().get_elt2().get_val() x = eq2_const / (eq1_coeff - eq2_coeff) y = tof(lneq1.get_rhs())(x) elif isinstance(lneq2.get_rhs(), pwr): #y = -x, y = x x = 0.0 y = 0.0 else: raise Exception("Unknown prod equation") else: raise Exception('line_intersection: ' + 'unknown equations') return make_point2d(x, y)
def loc_xtrm_2nd_drv_test(expr): # Get the extrema from the first derivative first_xtrema = loc_xtrm_1st_drv_test(expr) # Take the second derivative, put those extrema values in expr2 = deriv(deriv(expr)) fn2 = tof(expr2) results = [] for ex in first_xtrema: value = fn2(ex[1].get_x().get_val()) if value < 0: # If the results are negative, then we have a local max point = make_point2d(ex[1].get_x().get_val(), ex[1].get_y().get_val()) results.append(("max", point)) else: # If the results are positive, then we have a local min point = make_point2d(ex[1].get_x().get_val(), ex[1].get_y().get_val()) results.append(("min", point)) return results
def line_intersection(lneq1, lneq2): e1_x, e1_y, e1_c = get_line_coeffs(lneq1) e2_x, e2_y, e2_c = get_line_coeffs(lneq2) A = np.array([[e1_x, e1_y], [e2_x, e2_y]]) b = np.array([e1_c, e2_c]) # print(A) # print(b) try: pt = np.linalg.solve(A,b) except: return None return make_point2d(pt[0], pt[1])