| 1 | from numpy import array, arange |
| 2 | |
| 3 | from miscellaneous import Miscellaneous |
| 4 | |
| 5 | |
| 6 | def evaluate(coefficients, y, x): |
| 7 | a_f, b_f, c_f, d_f, e_f, g_f = coefficients |
| 8 | value = a_f * x ** 2 + b_f * y ** 2 + c_f * x * y + d_f * x + e_f * y + g_f |
| 9 | print ("Function evaluation, y: " + str(y) + ", x: " +str(x) + ", value: " + str(value)) |
| 10 | return value |
| 11 | |
| 12 | def create_function_values(coefficients): |
| 13 | function_values = [] |
| 14 | for y in range(-1,2): |
| 15 | for x in range(-1,2): |
| 16 | function_values.append(evaluate(coefficients, y, x)) |
| 17 | return array(function_values) |
| 18 | |
| 19 | def evaluate_around_optimum(coefficients, y_opt, x_opt): |
| 20 | for y in arange(y_opt - 0.1, y_opt + 0.19, 0.1): |
| 21 | for x in arange(x_opt - 0.1, x_opt + 0.19, 0.1): |
| 22 | evaluate(coefficients, y, x) |
| 23 | |
| 24 | def evaluate_derivatives(coefficients, y, x): |
| 25 | a_f, b_f, c_f, d_f, e_f, g_f = coefficients |
| 26 | df_dy = 2. * b_f * y + c_f * x + e_f |
| 27 | df_dx = 2. * a_f * x + c_f * y + d_f |
| 28 | print ("derivative_y: " + str(df_dy) + ", derivative_x: " + str(df_dx)) |
| 29 | |
| 30 | |
| 31 | coeff = (1.5, 1.2, 2.5, -0.5, 0.7, 3.) |
| 32 | values = create_function_values(coeff) |
| 33 | print ("\nFunction values: " + str(values)) |
| 34 | |
| 35 | y, x = Miscellaneous.sub_pixel_solve(values) |
| 36 | evaluate(coeff, y, x) |
| 37 | |
| 38 | y_old, x_old = Miscellaneous.sub_pixel_solve_old(values) |
| 39 | print ("Old solution, y: " + str(y_old) + ", x: " + str(x_old)) |
| 40 | |
| 41 | evaluate_around_optimum(coeff, y_old, x_old) |
| 42 | |
| 43 | evaluate_derivatives(coeff, y_old, x_old) |