numo-gsl-integration 0.4.0 → 0.5.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.
@@ -3,13 +3,16 @@
3
3
 
4
4
  typedef struct {
5
5
  VALUE points_array;
6
+ VALUE workspace_value;
6
7
  gsl_function *function;
7
8
  double *points;
8
9
  size_t point_count;
9
10
  double epsabs;
10
11
  double epsrel;
11
12
  size_t limit;
13
+ int limit_given;
12
14
  gsl_integration_workspace *workspace;
15
+ integration_workspace_use workspace_use;
13
16
  double *result;
14
17
  double *error;
15
18
  } integration_qagp_params;
@@ -62,7 +65,9 @@ static VALUE integration_run_qagp(VALUE data) {
62
65
  NUM2DBL(rb_ary_entry(params->points_array, (long)index));
63
66
  }
64
67
 
65
- params->workspace = gsl_integration_workspace_alloc(params->limit);
68
+ integration_workspace_use_begin(params->workspace_value, &params->limit,
69
+ params->limit_given, &params->workspace_use);
70
+ params->workspace = params->workspace_use.workspace;
66
71
 
67
72
  (void)gsl_integration_qagp(params->function, params->points,
68
73
  params->point_count, params->epsabs,
@@ -75,12 +80,11 @@ static VALUE integration_run_qagp(VALUE data) {
75
80
  static VALUE integration_free_qagp_resources(VALUE data) {
76
81
  integration_qagp_params *params = (integration_qagp_params *)data;
77
82
 
78
- if (params->workspace != NULL) {
79
- gsl_integration_workspace_free(params->workspace);
80
- }
83
+ integration_workspace_use_end((VALUE)&params->workspace_use);
81
84
 
82
85
  if (params->points != NULL) {
83
86
  xfree(params->points);
87
+ params->points = NULL;
84
88
  }
85
89
 
86
90
  return Qnil;
@@ -94,13 +98,15 @@ static VALUE integration_run_qagp_with_resources(VALUE data) {
94
98
  VALUE integration_qagp(int argc, VALUE *argv, VALUE self) {
95
99
  VALUE points;
96
100
  VALUE options;
101
+ VALUE workspace_value;
97
102
  integration_options parsed_options;
98
103
  double epsabs;
99
104
  double epsrel;
100
105
  size_t limit;
106
+ int limit_given;
101
107
  gsl_function function;
102
108
  integration_callback_params params;
103
- integration_qagp_params qagp_params;
109
+ integration_qagp_params qagp_params = {0};
104
110
  double result;
105
111
  double error;
106
112
  int state = 0;
@@ -111,7 +117,9 @@ VALUE integration_qagp(int argc, VALUE *argv, VALUE self) {
111
117
 
112
118
  integration_validate_qagp_points(points);
113
119
 
114
- integration_options_parse(options, &parsed_options, 0.0, 1e-8, 1000);
120
+ integration_options_parse_with_workspace(options, &parsed_options, 0.0, 1e-8,
121
+ 1000, &workspace_value,
122
+ &limit_given);
115
123
 
116
124
  epsabs = parsed_options.epsabs;
117
125
  epsrel = parsed_options.epsrel;
@@ -120,19 +128,20 @@ VALUE integration_qagp(int argc, VALUE *argv, VALUE self) {
120
128
  integration_function_init(&function, &params);
121
129
 
122
130
  qagp_params.points_array = points;
131
+ qagp_params.workspace_value = workspace_value;
123
132
  qagp_params.function = &function;
124
- qagp_params.points = NULL;
125
133
  qagp_params.point_count = (size_t)RARRAY_LEN(points);
126
134
  qagp_params.epsabs = epsabs;
127
135
  qagp_params.epsrel = epsrel;
128
136
  qagp_params.limit = limit;
129
- qagp_params.workspace = NULL;
137
+ qagp_params.limit_given = limit_given;
130
138
  qagp_params.result = &result;
131
139
  qagp_params.error = &error;
132
140
 
133
141
  rb_protect(integration_run_qagp_with_resources, (VALUE)&qagp_params, &state);
134
142
 
135
143
  RB_GC_GUARD(points);
144
+ RB_GC_GUARD(workspace_value);
136
145
  RB_GC_GUARD(params.block);
137
146
  RB_GC_GUARD(params.exception);
138
147
 
@@ -22,25 +22,22 @@ static VALUE integration_run_qags(VALUE data) {
22
22
  return Qnil;
23
23
  }
24
24
 
25
- static VALUE integration_run_qags_with_workspace(VALUE data) {
26
- integration_qags_params *params = (integration_qags_params *)data;
27
-
28
- return rb_ensure(integration_run_qags, data, integration_free_workspace,
29
- (VALUE)params->workspace);
30
- }
31
-
32
25
  VALUE integration_qags(int argc, VALUE *argv, VALUE self) {
33
26
  VALUE a;
34
27
  VALUE b;
35
28
  VALUE options;
29
+ VALUE workspace_value;
36
30
  integration_options parsed_options;
31
+ double a_double;
32
+ double b_double;
37
33
  double epsabs;
38
34
  double epsrel;
39
35
  size_t limit;
40
- gsl_integration_workspace *workspace;
36
+ int limit_given;
41
37
  gsl_function function;
42
38
  integration_callback_params params;
43
39
  integration_qags_params qags_params;
40
+ integration_workspace_use workspace_use;
44
41
  double result;
45
42
  double error;
46
43
  int state = 0;
@@ -48,29 +45,35 @@ VALUE integration_qags(int argc, VALUE *argv, VALUE self) {
48
45
  (void)self;
49
46
 
50
47
  rb_scan_args(argc, argv, "2:", &a, &b, &options);
48
+ integration_options_parse_with_workspace(options, &parsed_options, 0.0, 1e-8,
49
+ 1000, &workspace_value,
50
+ &limit_given);
51
51
 
52
- integration_options_parse(options, &parsed_options, 0.0, 1e-8, 1000);
53
-
52
+ a_double = NUM2DBL(a);
53
+ b_double = NUM2DBL(b);
54
54
  epsabs = parsed_options.epsabs;
55
55
  epsrel = parsed_options.epsrel;
56
56
  limit = parsed_options.limit;
57
57
 
58
58
  integration_function_init(&function, &params);
59
59
 
60
- workspace = gsl_integration_workspace_alloc(limit);
60
+ integration_workspace_use_begin(workspace_value, &limit, limit_given,
61
+ &workspace_use);
61
62
 
62
63
  qags_params.function = &function;
63
- qags_params.a = NUM2DBL(a);
64
- qags_params.b = NUM2DBL(b);
64
+ qags_params.a = a_double;
65
+ qags_params.b = b_double;
65
66
  qags_params.epsabs = epsabs;
66
67
  qags_params.epsrel = epsrel;
67
68
  qags_params.limit = limit;
68
- qags_params.workspace = workspace;
69
+ qags_params.workspace = workspace_use.workspace;
69
70
  qags_params.result = &result;
70
71
  qags_params.error = &error;
71
72
 
72
- rb_protect(integration_run_qags_with_workspace, (VALUE)&qags_params, &state);
73
+ rb_protect(integration_run_qags, (VALUE)&qags_params, &state);
74
+ integration_workspace_use_end((VALUE)&workspace_use);
73
75
 
76
+ RB_GC_GUARD(workspace_value);
74
77
  RB_GC_GUARD(params.block);
75
78
  RB_GC_GUARD(params.exception);
76
79
 
@@ -23,26 +23,24 @@ static VALUE integration_run_qawc(VALUE data) {
23
23
  return Qnil;
24
24
  }
25
25
 
26
- static VALUE integration_run_qawc_with_workspace(VALUE data) {
27
- integration_qawc_params *params = (integration_qawc_params *)data;
28
-
29
- return rb_ensure(integration_run_qawc, data, integration_free_workspace,
30
- (VALUE)params->workspace);
31
- }
32
-
33
26
  VALUE integration_qawc(int argc, VALUE *argv, VALUE self) {
34
27
  VALUE a;
35
28
  VALUE b;
36
29
  VALUE c;
37
30
  VALUE options;
31
+ VALUE workspace_value;
38
32
  integration_options parsed_options;
33
+ double a_double;
34
+ double b_double;
35
+ double c_double;
39
36
  double epsabs;
40
37
  double epsrel;
41
38
  size_t limit;
42
- gsl_integration_workspace *workspace;
39
+ int limit_given;
43
40
  gsl_function function;
44
41
  integration_callback_params params;
45
42
  integration_qawc_params qawc_params;
43
+ integration_workspace_use workspace_use;
46
44
  double result;
47
45
  double error;
48
46
  int state = 0;
@@ -50,30 +48,37 @@ VALUE integration_qawc(int argc, VALUE *argv, VALUE self) {
50
48
  (void)self;
51
49
 
52
50
  rb_scan_args(argc, argv, "3:", &a, &b, &c, &options);
51
+ integration_options_parse_with_workspace(options, &parsed_options, 0.0, 1e-8,
52
+ 1000, &workspace_value,
53
+ &limit_given);
53
54
 
54
- integration_options_parse(options, &parsed_options, 0.0, 1e-8, 1000);
55
-
55
+ a_double = NUM2DBL(a);
56
+ b_double = NUM2DBL(b);
57
+ c_double = NUM2DBL(c);
56
58
  epsabs = parsed_options.epsabs;
57
59
  epsrel = parsed_options.epsrel;
58
60
  limit = parsed_options.limit;
59
61
 
60
62
  integration_function_init(&function, &params);
61
63
 
62
- workspace = gsl_integration_workspace_alloc(limit);
64
+ integration_workspace_use_begin(workspace_value, &limit, limit_given,
65
+ &workspace_use);
63
66
 
64
67
  qawc_params.function = &function;
65
- qawc_params.a = NUM2DBL(a);
66
- qawc_params.b = NUM2DBL(b);
67
- qawc_params.c = NUM2DBL(c);
68
+ qawc_params.a = a_double;
69
+ qawc_params.b = b_double;
70
+ qawc_params.c = c_double;
68
71
  qawc_params.epsabs = epsabs;
69
72
  qawc_params.epsrel = epsrel;
70
73
  qawc_params.limit = limit;
71
- qawc_params.workspace = workspace;
74
+ qawc_params.workspace = workspace_use.workspace;
72
75
  qawc_params.result = &result;
73
76
  qawc_params.error = &error;
74
77
 
75
- rb_protect(integration_run_qawc_with_workspace, (VALUE)&qawc_params, &state);
78
+ rb_protect(integration_run_qawc, (VALUE)&qawc_params, &state);
79
+ integration_workspace_use_end((VALUE)&workspace_use);
76
80
 
81
+ RB_GC_GUARD(workspace_value);
77
82
  RB_GC_GUARD(params.block);
78
83
  RB_GC_GUARD(params.exception);
79
84
 
@@ -2,6 +2,8 @@
2
2
 
3
3
  typedef struct {
4
4
  gsl_function *function;
5
+ VALUE workspace_value;
6
+ VALUE table_value;
5
7
  double a;
6
8
  double b;
7
9
  double omega;
@@ -10,8 +12,11 @@ typedef struct {
10
12
  double epsabs;
11
13
  double epsrel;
12
14
  size_t limit;
15
+ int limit_given;
13
16
  gsl_integration_workspace *workspace;
17
+ integration_workspace_use workspace_use;
14
18
  gsl_integration_qawo_table *table;
19
+ integration_qawo_table_use table_use;
15
20
  double *result;
16
21
  double *error;
17
22
  } integration_qawo_params;
@@ -19,13 +24,8 @@ typedef struct {
19
24
  static VALUE integration_free_qawo_resources(VALUE data) {
20
25
  integration_qawo_params *params = (integration_qawo_params *)data;
21
26
 
22
- if (params->table != NULL) {
23
- gsl_integration_qawo_table_free(params->table);
24
- }
25
-
26
- if (params->workspace != NULL) {
27
- gsl_integration_workspace_free(params->workspace);
28
- }
27
+ integration_qawo_table_use_end((VALUE)&params->table_use);
28
+ integration_workspace_use_end((VALUE)&params->workspace_use);
29
29
 
30
30
  return Qnil;
31
31
  }
@@ -33,9 +33,13 @@ static VALUE integration_free_qawo_resources(VALUE data) {
33
33
  static VALUE integration_run_qawo(VALUE data) {
34
34
  integration_qawo_params *params = (integration_qawo_params *)data;
35
35
 
36
- params->table = gsl_integration_qawo_table_alloc(
37
- params->omega, params->b - params->a, params->sine, params->limit);
38
- params->workspace = gsl_integration_workspace_alloc(params->limit);
36
+ integration_workspace_use_begin(params->workspace_value, &params->limit,
37
+ params->limit_given, &params->workspace_use);
38
+ params->workspace = params->workspace_use.workspace;
39
+ integration_qawo_table_use_begin(params->table_value, params->omega,
40
+ params->b - params->a, params->sine,
41
+ params->limit, &params->table_use);
42
+ params->table = params->table_use.table;
39
43
 
40
44
  (void)gsl_integration_qawo(params->function, params->a, params->epsabs,
41
45
  params->epsrel, params->limit, params->workspace,
@@ -55,13 +59,25 @@ VALUE integration_qawo(int argc, VALUE *argv, VALUE self) {
55
59
  VALUE omega;
56
60
  VALUE sine;
57
61
  VALUE options;
58
- integration_options parsed_options;
62
+ VALUE epsabs_value = Qundef;
63
+ VALUE epsrel_value = Qundef;
64
+ VALUE limit_value = Qundef;
65
+ VALUE workspace_value = Qundef;
66
+ VALUE table_value = Qundef;
67
+ ID keywords[] = {rb_intern("epsabs"), rb_intern("epsrel"), rb_intern("limit"),
68
+ rb_intern("workspace"), rb_intern("table")};
69
+ VALUE keyword_values[5];
70
+ double a_double;
71
+ double b_double;
72
+ double omega_double;
73
+ int sine_integer;
59
74
  double epsabs;
60
75
  double epsrel;
61
76
  size_t limit;
77
+ int limit_given;
62
78
  gsl_function function;
63
79
  integration_callback_params params;
64
- integration_qawo_params qawo_params;
80
+ integration_qawo_params qawo_params = {0};
65
81
  double result;
66
82
  double error;
67
83
  int state = 0;
@@ -70,29 +86,44 @@ VALUE integration_qawo(int argc, VALUE *argv, VALUE self) {
70
86
 
71
87
  rb_scan_args(argc, argv, "4:", &a, &b, &omega, &sine, &options);
72
88
 
73
- integration_options_parse(options, &parsed_options, 0.0, 1e-8, 1000);
89
+ if (!NIL_P(options)) {
90
+ rb_get_kwargs(options, keywords, 0, 5, keyword_values);
91
+ epsabs_value = keyword_values[0];
92
+ epsrel_value = keyword_values[1];
93
+ limit_value = keyword_values[2];
94
+ workspace_value = keyword_values[3];
95
+ table_value = keyword_values[4];
96
+ }
74
97
 
75
- epsabs = parsed_options.epsabs;
76
- epsrel = parsed_options.epsrel;
77
- limit = parsed_options.limit;
98
+ a_double = NUM2DBL(a);
99
+ b_double = NUM2DBL(b);
100
+ omega_double = NUM2DBL(omega);
101
+ sine_integer = NUM2INT(sine);
102
+ epsabs = epsabs_value == Qundef ? 0.0 : NUM2DBL(epsabs_value);
103
+ epsrel = epsrel_value == Qundef ? 1e-8 : NUM2DBL(epsrel_value);
104
+ limit = limit_value == Qundef ? 1000 : NUM2SIZET(limit_value);
105
+ limit_given = limit_value != Qundef;
78
106
 
79
107
  integration_function_init(&function, &params);
80
108
 
81
109
  qawo_params.function = &function;
82
- qawo_params.a = NUM2DBL(a);
83
- qawo_params.b = NUM2DBL(b);
84
- qawo_params.omega = NUM2DBL(omega);
85
- qawo_params.sine = (enum gsl_integration_qawo_enum)NUM2INT(sine);
110
+ qawo_params.workspace_value = workspace_value;
111
+ qawo_params.table_value = table_value;
112
+ qawo_params.a = a_double;
113
+ qawo_params.b = b_double;
114
+ qawo_params.omega = omega_double;
115
+ qawo_params.sine = (enum gsl_integration_qawo_enum)sine_integer;
86
116
  qawo_params.epsabs = epsabs;
87
117
  qawo_params.epsrel = epsrel;
88
118
  qawo_params.limit = limit;
89
- qawo_params.workspace = NULL;
90
- qawo_params.table = NULL;
119
+ qawo_params.limit_given = limit_given;
91
120
  qawo_params.result = &result;
92
121
  qawo_params.error = &error;
93
122
 
94
123
  rb_protect(integration_run_qawo_with_resources, (VALUE)&qawo_params, &state);
95
124
 
125
+ RB_GC_GUARD(workspace_value);
126
+ RB_GC_GUARD(table_value);
96
127
  RB_GC_GUARD(params.block);
97
128
  RB_GC_GUARD(params.exception);
98
129
 
@@ -0,0 +1,292 @@
1
+ #include "integration_internal.h"
2
+
3
+ #include <float.h>
4
+ #include <math.h>
5
+
6
+ static VALUE integration_cQAWOTable;
7
+
8
+ static int integration_qawo_table_double_equal(double left, double right) {
9
+ double scale = fmax(fabs(left), fabs(right));
10
+
11
+ return fabs(left - right) <= DBL_EPSILON * fmax(1.0, scale) * 4.0;
12
+ }
13
+
14
+ static void integration_qawo_table_free(void *data) {
15
+ integration_qawo_table *table = data;
16
+
17
+ if (table->table != NULL) {
18
+ gsl_integration_qawo_table_free(table->table);
19
+ }
20
+
21
+ xfree(table);
22
+ }
23
+
24
+ static size_t integration_qawo_table_memsize(const void *data) {
25
+ const integration_qawo_table *table = data;
26
+
27
+ if (table == NULL) {
28
+ return 0;
29
+ }
30
+
31
+ return sizeof(integration_qawo_table) +
32
+ (table->table == NULL ? 0
33
+ : sizeof(gsl_integration_qawo_table) +
34
+ 25 * table->levels * sizeof(double));
35
+ }
36
+
37
+ static const rb_data_type_t integration_qawo_table_type = {
38
+ .wrap_struct_name = "Numo::GSL::Integration::QAWOTable",
39
+ .function =
40
+ {
41
+ .dmark = NULL,
42
+ .dfree = integration_qawo_table_free,
43
+ .dsize = integration_qawo_table_memsize,
44
+ },
45
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
46
+ };
47
+
48
+ static VALUE integration_qawo_table_allocate(VALUE klass) {
49
+ integration_qawo_table *table;
50
+ VALUE object = TypedData_Make_Struct(klass, integration_qawo_table,
51
+ &integration_qawo_table_type, table);
52
+
53
+ table->table = NULL;
54
+ table->omega = 0.0;
55
+ table->length = 0.0;
56
+ table->sine = GSL_INTEG_COSINE;
57
+ table->levels = 0;
58
+ table->busy = 0;
59
+
60
+ return object;
61
+ }
62
+
63
+ static VALUE integration_qawo_table_initialize(int argc, VALUE *argv,
64
+ VALUE self) {
65
+ VALUE options;
66
+ VALUE omega_value;
67
+ VALUE length_value;
68
+ VALUE sine_value;
69
+ VALUE levels_value;
70
+ ID keywords[] = {rb_intern("omega"), rb_intern("length"), rb_intern("sine"),
71
+ rb_intern("levels")};
72
+ VALUE keyword_values[4];
73
+ double omega;
74
+ double length;
75
+ int sine;
76
+ size_t levels;
77
+ integration_qawo_table *table;
78
+
79
+ rb_scan_args(argc, argv, "0:", &options);
80
+
81
+ if (NIL_P(options)) {
82
+ rb_raise(rb_eArgError, "missing keywords: :omega, :length, :sine, :levels");
83
+ }
84
+
85
+ rb_get_kwargs(options, keywords, 4, 0, keyword_values);
86
+ omega_value = keyword_values[0];
87
+ length_value = keyword_values[1];
88
+ sine_value = keyword_values[2];
89
+ levels_value = keyword_values[3];
90
+
91
+ if (!rb_obj_is_kind_of(omega_value, rb_cNumeric)) {
92
+ rb_raise(rb_eTypeError, "omega must be Numeric");
93
+ }
94
+ if (!rb_obj_is_kind_of(length_value, rb_cNumeric)) {
95
+ rb_raise(rb_eTypeError, "length must be Numeric");
96
+ }
97
+ if (!RB_INTEGER_TYPE_P(sine_value)) {
98
+ rb_raise(rb_eTypeError, "sine must be an Integer");
99
+ }
100
+ if (!RB_INTEGER_TYPE_P(levels_value)) {
101
+ rb_raise(rb_eTypeError, "levels must be an Integer");
102
+ }
103
+
104
+ omega = NUM2DBL(omega_value);
105
+ length = NUM2DBL(length_value);
106
+ sine = NUM2INT(sine_value);
107
+
108
+ if (length <= 0.0) {
109
+ rb_raise(rb_eArgError, "length must be positive");
110
+ }
111
+ if (sine != GSL_INTEG_SINE && sine != GSL_INTEG_COSINE) {
112
+ rb_raise(rb_eArgError, "sine must be SINE or COSINE");
113
+ }
114
+ if (RTEST(rb_funcall(levels_value, rb_intern("<="), 1, INT2FIX(0)))) {
115
+ rb_raise(rb_eArgError, "levels must be positive");
116
+ }
117
+
118
+ levels = NUM2SIZET(levels_value);
119
+ TypedData_Get_Struct(self, integration_qawo_table,
120
+ &integration_qawo_table_type, table);
121
+ table->omega = omega;
122
+ table->length = length;
123
+ table->sine = (enum gsl_integration_qawo_enum)sine;
124
+ table->levels = levels;
125
+ table->table = gsl_integration_qawo_table_alloc(table->omega, table->length,
126
+ table->sine, table->levels);
127
+
128
+ if (table->table == NULL) {
129
+ rb_memerror();
130
+ }
131
+
132
+ return self;
133
+ }
134
+
135
+ static VALUE integration_qawo_table_omega(VALUE self) {
136
+ integration_qawo_table *table;
137
+
138
+ TypedData_Get_Struct(self, integration_qawo_table,
139
+ &integration_qawo_table_type, table);
140
+
141
+ return DBL2NUM(table->omega);
142
+ }
143
+
144
+ static VALUE integration_qawo_table_length(VALUE self) {
145
+ integration_qawo_table *table;
146
+
147
+ TypedData_Get_Struct(self, integration_qawo_table,
148
+ &integration_qawo_table_type, table);
149
+
150
+ return DBL2NUM(table->length);
151
+ }
152
+
153
+ static VALUE integration_qawo_table_sine(VALUE self) {
154
+ integration_qawo_table *table;
155
+
156
+ TypedData_Get_Struct(self, integration_qawo_table,
157
+ &integration_qawo_table_type, table);
158
+
159
+ return INT2NUM(table->sine);
160
+ }
161
+
162
+ static VALUE integration_qawo_table_levels(VALUE self) {
163
+ integration_qawo_table *table;
164
+
165
+ TypedData_Get_Struct(self, integration_qawo_table,
166
+ &integration_qawo_table_type, table);
167
+
168
+ return SIZET2NUM(table->levels);
169
+ }
170
+
171
+ static VALUE integration_qawo_table_closed_p(VALUE self) {
172
+ integration_qawo_table *table;
173
+
174
+ TypedData_Get_Struct(self, integration_qawo_table,
175
+ &integration_qawo_table_type, table);
176
+
177
+ return table->table == NULL ? Qtrue : Qfalse;
178
+ }
179
+
180
+ static VALUE integration_qawo_table_close(VALUE self) {
181
+ integration_qawo_table *table;
182
+
183
+ TypedData_Get_Struct(self, integration_qawo_table,
184
+ &integration_qawo_table_type, table);
185
+
186
+ if (table->busy) {
187
+ rb_raise(rb_eRuntimeError, "table is already in use");
188
+ }
189
+ if (table->table != NULL) {
190
+ gsl_integration_qawo_table_free(table->table);
191
+ table->table = NULL;
192
+ }
193
+
194
+ return Qnil;
195
+ }
196
+
197
+ static VALUE integration_qawo_table_yield(VALUE table) {
198
+ return rb_yield(table);
199
+ }
200
+
201
+ static VALUE integration_qawo_table_open(int argc, VALUE *argv, VALUE klass) {
202
+ VALUE table;
203
+
204
+ table = rb_funcallv_kw(klass, rb_intern("new"), argc, argv, RB_PASS_KEYWORDS);
205
+
206
+ return rb_ensure(integration_qawo_table_yield, table,
207
+ integration_qawo_table_close, table);
208
+ }
209
+
210
+ void integration_qawo_table_use_begin(VALUE table_value, double omega,
211
+ double length,
212
+ enum gsl_integration_qawo_enum sine,
213
+ size_t levels,
214
+ integration_qawo_table_use *use) {
215
+ integration_qawo_table *table;
216
+
217
+ use->table = NULL;
218
+ use->owner = NULL;
219
+
220
+ if (table_value == Qundef) {
221
+ use->table = gsl_integration_qawo_table_alloc(omega, length, sine, levels);
222
+ if (use->table == NULL) {
223
+ rb_memerror();
224
+ }
225
+ return;
226
+ }
227
+
228
+ if (!rb_obj_is_kind_of(table_value, integration_cQAWOTable)) {
229
+ rb_raise(rb_eTypeError,
230
+ "table must be a Numo::GSL::Integration::QAWOTable");
231
+ }
232
+
233
+ TypedData_Get_Struct(table_value, integration_qawo_table,
234
+ &integration_qawo_table_type, table);
235
+
236
+ if (table->table == NULL) {
237
+ rb_raise(rb_eRuntimeError, "table is closed");
238
+ }
239
+ if (table->busy) {
240
+ rb_raise(rb_eRuntimeError, "table is already in use");
241
+ }
242
+ if (!integration_qawo_table_double_equal(table->omega, omega)) {
243
+ rb_raise(rb_eArgError, "omega does not match QAWO table");
244
+ }
245
+ if (!integration_qawo_table_double_equal(table->length, length)) {
246
+ rb_raise(rb_eArgError, "interval length does not match QAWO table");
247
+ }
248
+ if (table->sine != sine) {
249
+ rb_raise(rb_eArgError, "sine does not match QAWO table");
250
+ }
251
+
252
+ table->busy = 1;
253
+ use->table = table->table;
254
+ use->owner = table;
255
+ }
256
+
257
+ VALUE integration_qawo_table_use_end(VALUE data) {
258
+ integration_qawo_table_use *use = (integration_qawo_table_use *)data;
259
+
260
+ if (use->owner != NULL) {
261
+ use->owner->busy = 0;
262
+ } else if (use->table != NULL) {
263
+ gsl_integration_qawo_table_free(use->table);
264
+ }
265
+
266
+ use->table = NULL;
267
+ use->owner = NULL;
268
+
269
+ return Qnil;
270
+ }
271
+
272
+ void integration_qawo_table_init(VALUE integration_module) {
273
+ integration_cQAWOTable =
274
+ rb_define_class_under(integration_module, "QAWOTable", rb_cObject);
275
+ rb_define_alloc_func(integration_cQAWOTable, integration_qawo_table_allocate);
276
+ rb_define_method(integration_cQAWOTable, "initialize",
277
+ integration_qawo_table_initialize, -1);
278
+ rb_define_method(integration_cQAWOTable, "omega",
279
+ integration_qawo_table_omega, 0);
280
+ rb_define_method(integration_cQAWOTable, "length",
281
+ integration_qawo_table_length, 0);
282
+ rb_define_method(integration_cQAWOTable, "sine", integration_qawo_table_sine,
283
+ 0);
284
+ rb_define_method(integration_cQAWOTable, "levels",
285
+ integration_qawo_table_levels, 0);
286
+ rb_define_method(integration_cQAWOTable, "closed?",
287
+ integration_qawo_table_closed_p, 0);
288
+ rb_define_method(integration_cQAWOTable, "close",
289
+ integration_qawo_table_close, 0);
290
+ rb_define_singleton_method(integration_cQAWOTable, "open",
291
+ integration_qawo_table_open, -1);
292
+ }