def exercise_integral_dx(gaussian, x_max=1., n_points=1000): numerical_integral = 0 x_step = x_max / n_points for i in xrange(n_points+1): x = x_max * i / n_points new_value = gaussian.at_x(x) if (i): numerical_integral += (prev_value + new_value) * .5 prev_value = new_value analytical_integral = gaussian.integral_dx_at_x(x, 1.e-3) assert eps_eq(analytical_integral, gaussian.integral_dx_at_x(x)) assert eps_eq(numerical_integral*x_step, analytical_integral, eps=1.e-5)
def exercise_integral_dx(gaussian, x_max=1., n_points=1000): numerical_integral = 0 x_step = x_max / n_points for i in range(n_points + 1): x = x_max * i / n_points new_value = gaussian.at_x(x) if (i): numerical_integral += (prev_value + new_value) * .5 prev_value = new_value analytical_integral = gaussian.integral_dx_at_x(x, 1.e-3) assert eps_eq(analytical_integral, gaussian.integral_dx_at_x(x)) assert eps_eq(numerical_integral * x_step, analytical_integral, eps=1.e-5)
def finite_gradient_dx_at_x(gaussian, x, eps=1.e-5): if (x == 0): return 0 assert x >= eps tm = gaussian.at_x(x-eps) tp = gaussian.at_x(x+eps) return (tp-tm)/(2*eps)
def finite_gradient_dx_at_x(gaussian, x, eps=1.e-5): if (x == 0): return 0 assert x >= eps tm = gaussian.at_x(x - eps) tp = gaussian.at_x(x + eps) return (tp - tm) / (2 * eps)