numo-gsl-integration 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,75 @@
1
+ #include "integration_internal.h"
2
+
3
+ typedef struct {
4
+ gsl_function *function;
5
+ double a;
6
+ double b;
7
+ double c;
8
+ double epsabs;
9
+ double epsrel;
10
+ size_t limit;
11
+ gsl_integration_workspace *workspace;
12
+ double *result;
13
+ double *error;
14
+ } integration_qawc_params;
15
+
16
+ static VALUE integration_run_qawc(VALUE data) {
17
+ integration_qawc_params *params = (integration_qawc_params *)data;
18
+ (void)gsl_integration_qawc(params->function, params->a, params->b, params->c,
19
+ params->epsabs, params->epsrel, params->limit,
20
+ params->workspace, params->result, params->error);
21
+ return Qnil;
22
+ }
23
+
24
+ static VALUE integration_run_qawc_with_workspace(VALUE data) {
25
+ integration_qawc_params *params = (integration_qawc_params *)data;
26
+ return rb_ensure(integration_run_qawc, data, integration_free_workspace,
27
+ (VALUE)params->workspace);
28
+ }
29
+
30
+ VALUE integration_qawc(int argc, VALUE *argv, VALUE self) {
31
+ VALUE a;
32
+ VALUE b;
33
+ VALUE c;
34
+ VALUE options;
35
+ integration_options parsed_options;
36
+ double epsabs;
37
+ double epsrel;
38
+ size_t limit;
39
+ gsl_integration_workspace *workspace;
40
+ gsl_function function;
41
+ integration_callback_params params;
42
+ integration_qawc_params qawc_params;
43
+ double result;
44
+ double error;
45
+ int state = 0;
46
+ (void)self;
47
+
48
+ rb_scan_args(argc, argv, "3:", &a, &b, &c, &options);
49
+
50
+ integration_options_parse(options, &parsed_options, 0.0, 1e-8, 1000);
51
+ epsabs = parsed_options.epsabs;
52
+ epsrel = parsed_options.epsrel;
53
+ limit = parsed_options.limit;
54
+
55
+ integration_function_init(&function, &params);
56
+
57
+ workspace = gsl_integration_workspace_alloc(limit);
58
+ qawc_params.function = &function;
59
+ qawc_params.a = NUM2DBL(a);
60
+ qawc_params.b = NUM2DBL(b);
61
+ qawc_params.c = NUM2DBL(c);
62
+ qawc_params.epsabs = epsabs;
63
+ qawc_params.epsrel = epsrel;
64
+ qawc_params.limit = limit;
65
+ qawc_params.workspace = workspace;
66
+ qawc_params.result = &result;
67
+ qawc_params.error = &error;
68
+
69
+ rb_protect(integration_run_qawc_with_workspace, (VALUE)&qawc_params, &state);
70
+ RB_GC_GUARD(params.block);
71
+ RB_GC_GUARD(params.exception);
72
+ integration_finish_callback(&params, state);
73
+
74
+ return rb_ary_new_from_args(2, DBL2NUM(result), DBL2NUM(error));
75
+ }
@@ -0,0 +1,107 @@
1
+ #include "integration_internal.h"
2
+
3
+ typedef struct {
4
+ gsl_function *function;
5
+ double a;
6
+ double omega;
7
+ enum gsl_integration_qawo_enum sine;
8
+ double epsabs;
9
+ size_t limit;
10
+ gsl_integration_workspace *workspace;
11
+ gsl_integration_workspace *cycle_workspace;
12
+ gsl_integration_qawo_table *table;
13
+ double *result;
14
+ double *error;
15
+ } integration_qawf_params;
16
+
17
+ static VALUE integration_free_qawf_resources(VALUE data) {
18
+ integration_qawf_params *params = (integration_qawf_params *)data;
19
+
20
+ if (params->table != NULL) {
21
+ gsl_integration_qawo_table_free(params->table);
22
+ }
23
+ if (params->cycle_workspace != NULL) {
24
+ gsl_integration_workspace_free(params->cycle_workspace);
25
+ }
26
+ if (params->workspace != NULL) {
27
+ gsl_integration_workspace_free(params->workspace);
28
+ }
29
+
30
+ return Qnil;
31
+ }
32
+
33
+ static VALUE integration_run_qawf(VALUE data) {
34
+ integration_qawf_params *params = (integration_qawf_params *)data;
35
+
36
+ params->table = gsl_integration_qawo_table_alloc(params->omega, 1.0,
37
+ params->sine, params->limit);
38
+ params->workspace = gsl_integration_workspace_alloc(params->limit);
39
+ params->cycle_workspace = gsl_integration_workspace_alloc(params->limit);
40
+
41
+ (void)gsl_integration_qawf(params->function, params->a, params->epsabs,
42
+ params->limit, params->workspace,
43
+ params->cycle_workspace, params->table,
44
+ params->result, params->error);
45
+
46
+ return Qnil;
47
+ }
48
+
49
+ static VALUE integration_run_qawf_with_resources(VALUE data) {
50
+ return rb_ensure(integration_run_qawf, data, integration_free_qawf_resources,
51
+ data);
52
+ }
53
+
54
+ VALUE integration_qawf(int argc, VALUE *argv, VALUE self) {
55
+ VALUE a;
56
+ VALUE omega;
57
+ VALUE sine;
58
+ VALUE options;
59
+ VALUE epsabs_value = Qundef;
60
+ VALUE limit_value = Qundef;
61
+ ID keywords[] = {rb_intern("epsabs"), rb_intern("limit")};
62
+ VALUE keyword_values[2];
63
+ double epsabs;
64
+ size_t limit;
65
+ gsl_function function;
66
+ integration_callback_params params;
67
+ integration_qawf_params qawf_params;
68
+ double result;
69
+ double error;
70
+ int state = 0;
71
+
72
+ (void)self;
73
+
74
+ rb_scan_args(argc, argv, "3:", &a, &omega, &sine, &options);
75
+
76
+ if (!NIL_P(options)) {
77
+ rb_get_kwargs(options, keywords, 0, 2, keyword_values);
78
+ epsabs_value = keyword_values[0];
79
+ limit_value = keyword_values[1];
80
+ }
81
+
82
+ epsabs = epsabs_value == Qundef ? 1e-10 : NUM2DBL(epsabs_value);
83
+ limit = limit_value == Qundef ? 1000 : NUM2SIZET(limit_value);
84
+
85
+ integration_function_init(&function, &params);
86
+
87
+ qawf_params.function = &function;
88
+ qawf_params.a = NUM2DBL(a);
89
+ qawf_params.omega = NUM2DBL(omega);
90
+ qawf_params.sine = (enum gsl_integration_qawo_enum)NUM2INT(sine);
91
+ qawf_params.epsabs = epsabs;
92
+ qawf_params.limit = limit;
93
+ qawf_params.workspace = NULL;
94
+ qawf_params.cycle_workspace = NULL;
95
+ qawf_params.table = NULL;
96
+ qawf_params.result = &result;
97
+ qawf_params.error = &error;
98
+
99
+ rb_protect(integration_run_qawf_with_resources, (VALUE)&qawf_params, &state);
100
+
101
+ RB_GC_GUARD(params.block);
102
+ RB_GC_GUARD(params.exception);
103
+
104
+ integration_finish_callback(&params, state);
105
+
106
+ return rb_ary_new_from_args(2, DBL2NUM(result), DBL2NUM(error));
107
+ }
@@ -0,0 +1,99 @@
1
+ #include "integration_internal.h"
2
+
3
+ typedef struct {
4
+ gsl_function *function;
5
+ double a;
6
+ double b;
7
+ double omega;
8
+ enum gsl_integration_qawo_enum sine;
9
+ double epsabs;
10
+ double epsrel;
11
+ size_t limit;
12
+ gsl_integration_workspace *workspace;
13
+ gsl_integration_qawo_table *table;
14
+ double *result;
15
+ double *error;
16
+ } integration_qawo_params;
17
+
18
+ static VALUE integration_free_qawo_resources(VALUE data) {
19
+ integration_qawo_params *params = (integration_qawo_params *)data;
20
+
21
+ if (params->table != NULL) {
22
+ gsl_integration_qawo_table_free(params->table);
23
+ }
24
+ if (params->workspace != NULL) {
25
+ gsl_integration_workspace_free(params->workspace);
26
+ }
27
+
28
+ return Qnil;
29
+ }
30
+
31
+ static VALUE integration_run_qawo(VALUE data) {
32
+ integration_qawo_params *params = (integration_qawo_params *)data;
33
+
34
+ params->table = gsl_integration_qawo_table_alloc(
35
+ params->omega, params->b - params->a, params->sine, params->limit);
36
+ params->workspace = gsl_integration_workspace_alloc(params->limit);
37
+
38
+ (void)gsl_integration_qawo(params->function, params->a, params->epsabs,
39
+ params->epsrel, params->limit, params->workspace,
40
+ params->table, params->result, params->error);
41
+
42
+ return Qnil;
43
+ }
44
+
45
+ static VALUE integration_run_qawo_with_resources(VALUE data) {
46
+ return rb_ensure(integration_run_qawo, data, integration_free_qawo_resources,
47
+ data);
48
+ }
49
+
50
+ VALUE integration_qawo(int argc, VALUE *argv, VALUE self) {
51
+ VALUE a;
52
+ VALUE b;
53
+ VALUE omega;
54
+ VALUE sine;
55
+ VALUE options;
56
+ integration_options parsed_options;
57
+ double epsabs;
58
+ double epsrel;
59
+ size_t limit;
60
+ gsl_function function;
61
+ integration_callback_params params;
62
+ integration_qawo_params qawo_params;
63
+ double result;
64
+ double error;
65
+ int state = 0;
66
+
67
+ (void)self;
68
+
69
+ rb_scan_args(argc, argv, "4:", &a, &b, &omega, &sine, &options);
70
+
71
+ integration_options_parse(options, &parsed_options, 0.0, 1e-8, 1000);
72
+ epsabs = parsed_options.epsabs;
73
+ epsrel = parsed_options.epsrel;
74
+ limit = parsed_options.limit;
75
+
76
+ integration_function_init(&function, &params);
77
+
78
+ qawo_params.function = &function;
79
+ qawo_params.a = NUM2DBL(a);
80
+ qawo_params.b = NUM2DBL(b);
81
+ qawo_params.omega = NUM2DBL(omega);
82
+ qawo_params.sine = (enum gsl_integration_qawo_enum)NUM2INT(sine);
83
+ qawo_params.epsabs = epsabs;
84
+ qawo_params.epsrel = epsrel;
85
+ qawo_params.limit = limit;
86
+ qawo_params.workspace = NULL;
87
+ qawo_params.table = NULL;
88
+ qawo_params.result = &result;
89
+ qawo_params.error = &error;
90
+
91
+ rb_protect(integration_run_qawo_with_resources, (VALUE)&qawo_params, &state);
92
+
93
+ RB_GC_GUARD(params.block);
94
+ RB_GC_GUARD(params.exception);
95
+
96
+ integration_finish_callback(&params, state);
97
+
98
+ return rb_ary_new_from_args(2, DBL2NUM(result), DBL2NUM(error));
99
+ }
@@ -0,0 +1,102 @@
1
+ #include "integration_internal.h"
2
+
3
+ typedef struct {
4
+ gsl_function *function;
5
+ double a;
6
+ double b;
7
+ double alpha;
8
+ double beta;
9
+ int mu;
10
+ int nu;
11
+ double epsabs;
12
+ double epsrel;
13
+ size_t limit;
14
+ gsl_integration_workspace *workspace;
15
+ gsl_integration_qaws_table *table;
16
+ double *result;
17
+ double *error;
18
+ } integration_qaws_params;
19
+
20
+ static VALUE integration_free_qaws_resources(VALUE data) {
21
+ integration_qaws_params *params = (integration_qaws_params *)data;
22
+
23
+ if (params->table != NULL) {
24
+ gsl_integration_qaws_table_free(params->table);
25
+ }
26
+ if (params->workspace != NULL) {
27
+ gsl_integration_workspace_free(params->workspace);
28
+ }
29
+
30
+ return Qnil;
31
+ }
32
+
33
+ static VALUE integration_run_qaws(VALUE data) {
34
+ integration_qaws_params *params = (integration_qaws_params *)data;
35
+
36
+ params->table = gsl_integration_qaws_table_alloc(params->alpha, params->beta,
37
+ params->mu, params->nu);
38
+ params->workspace = gsl_integration_workspace_alloc(params->limit);
39
+ (void)gsl_integration_qaws(params->function, params->a, params->b,
40
+ params->table, params->epsabs, params->epsrel,
41
+ params->limit, params->workspace, params->result,
42
+ params->error);
43
+
44
+ return Qnil;
45
+ }
46
+
47
+ static VALUE integration_run_qaws_with_resources(VALUE data) {
48
+ return rb_ensure(integration_run_qaws, data, integration_free_qaws_resources,
49
+ data);
50
+ }
51
+
52
+ VALUE integration_qaws(int argc, VALUE *argv, VALUE self) {
53
+ VALUE a;
54
+ VALUE b;
55
+ VALUE alpha;
56
+ VALUE beta;
57
+ VALUE mu;
58
+ VALUE nu;
59
+ VALUE options;
60
+ integration_options parsed_options;
61
+ double epsabs;
62
+ double epsrel;
63
+ size_t limit;
64
+ gsl_function function;
65
+ integration_callback_params params;
66
+ integration_qaws_params qaws_params;
67
+ double result;
68
+ double error;
69
+ int state = 0;
70
+
71
+ (void)self;
72
+ rb_scan_args(argc, argv, "6:", &a, &b, &alpha, &beta, &mu, &nu, &options);
73
+
74
+ integration_options_parse(options, &parsed_options, 0.0, 1e-8, 1000);
75
+ epsabs = parsed_options.epsabs;
76
+ epsrel = parsed_options.epsrel;
77
+ limit = parsed_options.limit;
78
+
79
+ integration_function_init(&function, &params);
80
+
81
+ qaws_params.function = &function;
82
+ qaws_params.a = NUM2DBL(a);
83
+ qaws_params.b = NUM2DBL(b);
84
+ qaws_params.alpha = NUM2DBL(alpha);
85
+ qaws_params.beta = NUM2DBL(beta);
86
+ qaws_params.mu = NUM2INT(mu);
87
+ qaws_params.nu = NUM2INT(nu);
88
+ qaws_params.epsabs = epsabs;
89
+ qaws_params.epsrel = epsrel;
90
+ qaws_params.limit = limit;
91
+ qaws_params.workspace = NULL;
92
+ qaws_params.table = NULL;
93
+ qaws_params.result = &result;
94
+ qaws_params.error = &error;
95
+
96
+ rb_protect(integration_run_qaws_with_resources, (VALUE)&qaws_params, &state);
97
+ RB_GC_GUARD(params.block);
98
+ RB_GC_GUARD(params.exception);
99
+ integration_finish_callback(&params, state);
100
+
101
+ return rb_ary_new_from_args(2, DBL2NUM(result), DBL2NUM(error));
102
+ }
@@ -0,0 +1,75 @@
1
+ #include "integration_internal.h"
2
+
3
+ typedef struct {
4
+ gsl_function *function;
5
+ double a;
6
+ double b;
7
+ double epsabs;
8
+ double epsrel;
9
+ double *result;
10
+ double *error;
11
+ size_t *evaluations;
12
+ } integration_qng_params;
13
+
14
+ static VALUE integration_run_qng(VALUE data) {
15
+ integration_qng_params *params = (integration_qng_params *)data;
16
+
17
+ (void)gsl_integration_qng(params->function, params->a, params->b,
18
+ params->epsabs, params->epsrel, params->result,
19
+ params->error, params->evaluations);
20
+
21
+ return Qnil;
22
+ }
23
+
24
+ VALUE integration_qng(int argc, VALUE *argv, VALUE self) {
25
+ VALUE a;
26
+ VALUE b;
27
+ VALUE options;
28
+ VALUE epsabs_value = Qundef;
29
+ VALUE epsrel_value = Qundef;
30
+ ID keywords[] = {rb_intern("epsabs"), rb_intern("epsrel")};
31
+ VALUE keyword_values[2];
32
+ double epsabs;
33
+ double epsrel;
34
+ gsl_function function;
35
+ integration_callback_params params;
36
+ integration_qng_params qng_params;
37
+ double result;
38
+ double error;
39
+ size_t evaluations;
40
+ int state = 0;
41
+
42
+ (void)self;
43
+
44
+ rb_scan_args(argc, argv, "2:", &a, &b, &options);
45
+
46
+ if (!NIL_P(options)) {
47
+ rb_get_kwargs(options, keywords, 0, 2, keyword_values);
48
+ epsabs_value = keyword_values[0];
49
+ epsrel_value = keyword_values[1];
50
+ }
51
+
52
+ epsabs = epsabs_value == Qundef ? 0.0 : NUM2DBL(epsabs_value);
53
+ epsrel = epsrel_value == Qundef ? 1e-8 : NUM2DBL(epsrel_value);
54
+
55
+ integration_function_init(&function, &params);
56
+
57
+ qng_params.function = &function;
58
+ qng_params.a = NUM2DBL(a);
59
+ qng_params.b = NUM2DBL(b);
60
+ qng_params.epsabs = epsabs;
61
+ qng_params.epsrel = epsrel;
62
+ qng_params.result = &result;
63
+ qng_params.error = &error;
64
+ qng_params.evaluations = &evaluations;
65
+
66
+ rb_protect(integration_run_qng, (VALUE)&qng_params, &state);
67
+
68
+ RB_GC_GUARD(params.block);
69
+ RB_GC_GUARD(params.exception);
70
+
71
+ integration_finish_callback(&params, state);
72
+
73
+ return rb_ary_new_from_args(3, DBL2NUM(result), DBL2NUM(error),
74
+ SIZET2NUM(evaluations));
75
+ }
@@ -0,0 +1,35 @@
1
+ #include "integration_internal.h"
2
+
3
+ VALUE integration_free_workspace(VALUE data) {
4
+ gsl_integration_workspace *workspace = (gsl_integration_workspace *)data;
5
+
6
+ gsl_integration_workspace_free(workspace);
7
+
8
+ return Qnil;
9
+ }
10
+
11
+ VALUE integration_free_cquad_workspace(VALUE data) {
12
+ gsl_integration_cquad_workspace *workspace =
13
+ (gsl_integration_cquad_workspace *)data;
14
+
15
+ gsl_integration_cquad_workspace_free(workspace);
16
+
17
+ return Qnil;
18
+ }
19
+
20
+ VALUE integration_free_romberg_workspace(VALUE data) {
21
+ gsl_integration_romberg_workspace *workspace =
22
+ (gsl_integration_romberg_workspace *)data;
23
+
24
+ gsl_integration_romberg_free(workspace);
25
+
26
+ return Qnil;
27
+ }
28
+
29
+ VALUE integration_free_glfixed_table(VALUE data) {
30
+ gsl_integration_glfixed_table *table = (gsl_integration_glfixed_table *)data;
31
+
32
+ gsl_integration_glfixed_table_free(table);
33
+
34
+ return Qnil;
35
+ }
@@ -0,0 +1,79 @@
1
+ #include "integration_internal.h"
2
+
3
+ typedef struct {
4
+ gsl_function *function;
5
+ double a;
6
+ double b;
7
+ double epsabs;
8
+ double epsrel;
9
+ gsl_integration_romberg_workspace *workspace;
10
+ double *result;
11
+ size_t *evaluations;
12
+ } integration_romberg_params;
13
+
14
+ static VALUE integration_run_romberg(VALUE data) {
15
+ integration_romberg_params *params = (integration_romberg_params *)data;
16
+
17
+ (void)gsl_integration_romberg(params->function, params->a, params->b,
18
+ params->epsabs, params->epsrel, params->result,
19
+ params->evaluations, params->workspace);
20
+
21
+ return Qnil;
22
+ }
23
+
24
+ static VALUE integration_run_romberg_with_workspace(VALUE data) {
25
+ integration_romberg_params *params = (integration_romberg_params *)data;
26
+
27
+ return rb_ensure(integration_run_romberg, data,
28
+ integration_free_romberg_workspace,
29
+ (VALUE)params->workspace);
30
+ }
31
+
32
+ VALUE integration_romberg(int argc, VALUE *argv, VALUE self) {
33
+ VALUE a;
34
+ VALUE b;
35
+ VALUE options;
36
+ integration_options parsed_options;
37
+ double epsabs;
38
+ double epsrel;
39
+ size_t limit;
40
+ gsl_integration_romberg_workspace *workspace;
41
+ gsl_function function;
42
+ integration_callback_params params;
43
+ integration_romberg_params romberg_params;
44
+ double result;
45
+ size_t evaluations;
46
+ int state = 0;
47
+
48
+ (void)self;
49
+
50
+ rb_scan_args(argc, argv, "2:", &a, &b, &options);
51
+
52
+ integration_options_parse(options, &parsed_options, 0.0, 1e-8, 20);
53
+ epsabs = parsed_options.epsabs;
54
+ epsrel = parsed_options.epsrel;
55
+ limit = parsed_options.limit;
56
+
57
+ integration_function_init(&function, &params);
58
+
59
+ workspace = gsl_integration_romberg_alloc(limit);
60
+
61
+ romberg_params.function = &function;
62
+ romberg_params.a = NUM2DBL(a);
63
+ romberg_params.b = NUM2DBL(b);
64
+ romberg_params.epsabs = epsabs;
65
+ romberg_params.epsrel = epsrel;
66
+ romberg_params.workspace = workspace;
67
+ romberg_params.result = &result;
68
+ romberg_params.evaluations = &evaluations;
69
+
70
+ rb_protect(integration_run_romberg_with_workspace, (VALUE)&romberg_params,
71
+ &state);
72
+
73
+ RB_GC_GUARD(params.block);
74
+ RB_GC_GUARD(params.exception);
75
+
76
+ integration_finish_callback(&params, state);
77
+
78
+ return rb_ary_new_from_args(2, DBL2NUM(result), SIZET2NUM(evaluations));
79
+ }
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Numo
4
+ module GSL
5
+ module Integration
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "integration/version"
4
+ require "numo/gsl"
5
+
6
+ module Numo
7
+ module GSL
8
+ module Integration
9
+ class Error < Numo::GSL::GslError
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ require "numo/gsl/integration/integration"