Quantum 3xplorer (Q3)
An AI-native quantum simulation engine
Loading...
Searching...
No Matches
simpson.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * Simpson's rule numerical integration on a non-uniform grid.
5 */
6
7#include <algorithm>
8#include <vector>
9
11
12namespace q3 {
13
14/**
15 * Simpson's rule integration on a grid: ∫ f(r) dr
16 * where dr is given by integration_factors[i] = dr/di.
17 *
18 * @param function_values Function values at grid points
19 * @param integration_factors Integration weight factors (dr/di)
20 * @param number_of_grid_points Number of grid points to integrate over
21 * @return Integrated value
22 */
23inline Real simpson_rule(const std::vector<Real>& function_values,
24 const std::vector<Real>& integration_factors,
25 int number_of_grid_points) {
26 Real integrated_sum = 0.0;
27 int mesh_size = std::min(number_of_grid_points, static_cast<int>(function_values.size()));
28 if (mesh_size < 3) {
29 return 0.0;
30 }
31
32 for (int grid_index = 1; grid_index < mesh_size - 1; grid_index++) {
33 Real integration_weight = (grid_index % 2 == 1) ? 4.0 : 2.0;
34 integrated_sum +=
35 integration_weight * function_values[grid_index] * integration_factors[grid_index];
36 }
37
38 if (mesh_size % 2 == 1) {
39 integrated_sum += function_values[0] * integration_factors[0] +
40 function_values[mesh_size - 1] * integration_factors[mesh_size - 1];
41 } else {
42 integrated_sum +=
43 function_values[0] * integration_factors[0] -
44 function_values[mesh_size - 3] * integration_factors[mesh_size - 3] * 0.25 +
45 function_values[mesh_size - 2] * integration_factors[mesh_size - 2] +
46 function_values[mesh_size - 1] * integration_factors[mesh_size - 1] * 1.25;
47 }
48
49 return integrated_sum / 3.0;
50}
51
52} // namespace q3
Real simpson_rule(const std::vector< Real > &function_values, const std::vector< Real > &integration_factors, int number_of_grid_points)
Definition simpson.hpp:23
double Real