LMMC
Lamina Mathematic core
载入中...
搜索中...
未找到
quadrature_fixed.c
浏览该文件的文档.
2
3static int lmmc_quad_fixed_interior(
4 lmmc_quad_func_t func, void* user_data,
5 lmmc_real_t center, lmmc_real_t half_length, lmmc_real_t normalized_h,
6 size_t n, int simpson_rule, lmmc_real_t* sum)
7{
8 for (size_t i = 1; i < n; ++i) {
9 const lmmc_real_t normalized_x = -1.0 + (lmmc_real_t)i * normalized_h;
10 const lmmc_real_t x = fma(half_length, normalized_x, center);
11 const lmmc_real_t value = func(x, user_data);
12 const lmmc_real_t weight = simpson_rule && i % 2 == 1 ? 4.0 : 2.0;
13 if (!lmmc_is_finite(&x) || !lmmc_is_finite(&value)) {
14 return 0;
15 }
16 *sum += weight * value;
17 if (!lmmc_is_finite(sum)) {
18 return 0;
19 }
20 }
21 return 1;
22}
23
26 void* user_data,
29 size_t n,
30 lmmc_real_t* out_result)
31{
32 lmmc_real_t center, half_length, normalized_h, left, right, sum;
33 if (func == NULL || out_result == NULL || n == 0 || a >= b) {
35 }
36 if (!lmmc_is_finite(&a) || !lmmc_is_finite(&b)) {
38 }
39 lmmc_quad_center_half_length(a, b, &center, &half_length);
40 normalized_h = 2.0 / (lmmc_real_t)n;
41 left = func(a, user_data);
42 right = func(b, user_data);
43 if (!lmmc_is_finite(&center) || !lmmc_is_finite(&half_length) ||
44 !lmmc_is_finite(&left) || !lmmc_is_finite(&right)) {
46 }
47 sum = left + right;
48 if (!lmmc_quad_fixed_interior(
49 func, user_data, center, half_length, normalized_h, n, 0, &sum)) {
51 }
52 *out_result = half_length * (normalized_h * 0.5 * sum);
53 return lmmc_is_finite(out_result) ? LMMC_STATUS_OK
55}
56
57
60 void* user_data,
63 size_t n,
64 lmmc_real_t* out_result)
65{
66 lmmc_real_t center, half_length, normalized_h, left, right, sum;
67 if (func == NULL || out_result == NULL || n == 0 || n % 2 != 0 ||
68 a >= b) {
70 }
71 lmmc_quad_center_half_length(a, b, &center, &half_length);
72 normalized_h = 2.0 / (lmmc_real_t)n;
73 left = func(a, user_data);
74 right = func(b, user_data);
75 if (!lmmc_is_finite(&center) || !lmmc_is_finite(&half_length) ||
76 !lmmc_is_finite(&left) || !lmmc_is_finite(&right)) {
78 }
79 sum = left + right;
80 if (!lmmc_quad_fixed_interior(
81 func, user_data, center, half_length, normalized_h, n, 1, &sum)) {
83 }
84 *out_result = half_length * (normalized_h / 3.0 * sum);
85 return lmmc_is_finite(out_result) ? LMMC_STATUS_OK
87}
double lmmc_real_t
Public and internal LMMC real type.
lmmc_real_t(*) lmmc_quad_func_t(lmmc_real_t x, void *user_data)
被积函数签名 。
lmmc_status_t lmmc_quad_trapezoid(lmmc_quad_func_t func, void *user_data, lmmc_real_t a, lmmc_real_t b, size_t n, lmmc_real_t *out_result)
复合梯形公式:将 等分为 n 段。
lmmc_status_t lmmc_quad_simpson(lmmc_quad_func_t func, void *user_data, lmmc_real_t a, lmmc_real_t b, size_t n, lmmc_real_t *out_result)
复合 Simpson 公式:将 等分为 n 段(要求偶数)。
void lmmc_quad_center_half_length(lmmc_real_t a, lmmc_real_t b, lmmc_real_t *center, lmmc_real_t *half_length)
lmmc_status_t
LMMC 库统一返回的状态码.
@ LMMC_STATUS_OK
@ LMMC_STATUS_NUMERICAL_FAILURE
@ LMMC_STATUS_INVALID_ARGUMENT