numo-gsl-integration 0.3.1 → 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.
@@ -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
+ }
@@ -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 alpha;
@@ -11,8 +13,11 @@ typedef struct {
11
13
  double epsabs;
12
14
  double epsrel;
13
15
  size_t limit;
16
+ int limit_given;
14
17
  gsl_integration_workspace *workspace;
18
+ integration_workspace_use workspace_use;
15
19
  gsl_integration_qaws_table *table;
20
+ integration_qaws_table_use table_use;
16
21
  double *result;
17
22
  double *error;
18
23
  } integration_qaws_params;
@@ -20,13 +25,8 @@ typedef struct {
20
25
  static VALUE integration_free_qaws_resources(VALUE data) {
21
26
  integration_qaws_params *params = (integration_qaws_params *)data;
22
27
 
23
- if (params->table != NULL) {
24
- gsl_integration_qaws_table_free(params->table);
25
- }
26
-
27
- if (params->workspace != NULL) {
28
- gsl_integration_workspace_free(params->workspace);
29
- }
28
+ integration_qaws_table_use_end((VALUE)&params->table_use);
29
+ integration_workspace_use_end((VALUE)&params->workspace_use);
30
30
 
31
31
  return Qnil;
32
32
  }
@@ -34,9 +34,13 @@ static VALUE integration_free_qaws_resources(VALUE data) {
34
34
  static VALUE integration_run_qaws(VALUE data) {
35
35
  integration_qaws_params *params = (integration_qaws_params *)data;
36
36
 
37
- params->table = gsl_integration_qaws_table_alloc(params->alpha, params->beta,
38
- params->mu, params->nu);
39
- params->workspace = gsl_integration_workspace_alloc(params->limit);
37
+ integration_workspace_use_begin(params->workspace_value, &params->limit,
38
+ params->limit_given, &params->workspace_use);
39
+ params->workspace = params->workspace_use.workspace;
40
+ integration_qaws_table_use_begin(params->table_value, params->alpha,
41
+ params->beta, params->mu, params->nu,
42
+ &params->table_use);
43
+ params->table = params->table_use.table;
40
44
 
41
45
  (void)gsl_integration_qaws(params->function, params->a, params->b,
42
46
  params->table, params->epsabs, params->epsrel,
@@ -59,13 +63,27 @@ VALUE integration_qaws(int argc, VALUE *argv, VALUE self) {
59
63
  VALUE mu;
60
64
  VALUE nu;
61
65
  VALUE options;
62
- integration_options parsed_options;
66
+ VALUE epsabs_value = Qundef;
67
+ VALUE epsrel_value = Qundef;
68
+ VALUE limit_value = Qundef;
69
+ VALUE workspace_value = Qundef;
70
+ VALUE table_value = Qundef;
71
+ ID keywords[] = {rb_intern("epsabs"), rb_intern("epsrel"), rb_intern("limit"),
72
+ rb_intern("workspace"), rb_intern("table")};
73
+ VALUE keyword_values[5];
74
+ double a_double;
75
+ double b_double;
76
+ double alpha_double;
77
+ double beta_double;
78
+ int mu_integer;
79
+ int nu_integer;
63
80
  double epsabs;
64
81
  double epsrel;
65
82
  size_t limit;
83
+ int limit_given;
66
84
  gsl_function function;
67
85
  integration_callback_params params;
68
- integration_qaws_params qaws_params;
86
+ integration_qaws_params qaws_params = {0};
69
87
  double result;
70
88
  double error;
71
89
  int state = 0;
@@ -74,31 +92,48 @@ VALUE integration_qaws(int argc, VALUE *argv, VALUE self) {
74
92
 
75
93
  rb_scan_args(argc, argv, "6:", &a, &b, &alpha, &beta, &mu, &nu, &options);
76
94
 
77
- integration_options_parse(options, &parsed_options, 0.0, 1e-8, 1000);
95
+ if (!NIL_P(options)) {
96
+ rb_get_kwargs(options, keywords, 0, 5, keyword_values);
97
+ epsabs_value = keyword_values[0];
98
+ epsrel_value = keyword_values[1];
99
+ limit_value = keyword_values[2];
100
+ workspace_value = keyword_values[3];
101
+ table_value = keyword_values[4];
102
+ }
78
103
 
79
- epsabs = parsed_options.epsabs;
80
- epsrel = parsed_options.epsrel;
81
- limit = parsed_options.limit;
104
+ a_double = NUM2DBL(a);
105
+ b_double = NUM2DBL(b);
106
+ alpha_double = NUM2DBL(alpha);
107
+ beta_double = NUM2DBL(beta);
108
+ mu_integer = NUM2INT(mu);
109
+ nu_integer = NUM2INT(nu);
110
+ epsabs = epsabs_value == Qundef ? 0.0 : NUM2DBL(epsabs_value);
111
+ epsrel = epsrel_value == Qundef ? 1e-8 : NUM2DBL(epsrel_value);
112
+ limit = limit_value == Qundef ? 1000 : NUM2SIZET(limit_value);
113
+ limit_given = limit_value != Qundef;
82
114
 
83
115
  integration_function_init(&function, &params);
84
116
 
85
117
  qaws_params.function = &function;
86
- qaws_params.a = NUM2DBL(a);
87
- qaws_params.b = NUM2DBL(b);
88
- qaws_params.alpha = NUM2DBL(alpha);
89
- qaws_params.beta = NUM2DBL(beta);
90
- qaws_params.mu = NUM2INT(mu);
91
- qaws_params.nu = NUM2INT(nu);
118
+ qaws_params.workspace_value = workspace_value;
119
+ qaws_params.table_value = table_value;
120
+ qaws_params.a = a_double;
121
+ qaws_params.b = b_double;
122
+ qaws_params.alpha = alpha_double;
123
+ qaws_params.beta = beta_double;
124
+ qaws_params.mu = mu_integer;
125
+ qaws_params.nu = nu_integer;
92
126
  qaws_params.epsabs = epsabs;
93
127
  qaws_params.epsrel = epsrel;
94
128
  qaws_params.limit = limit;
95
- qaws_params.workspace = NULL;
96
- qaws_params.table = NULL;
129
+ qaws_params.limit_given = limit_given;
97
130
  qaws_params.result = &result;
98
131
  qaws_params.error = &error;
99
132
 
100
133
  rb_protect(integration_run_qaws_with_resources, (VALUE)&qaws_params, &state);
101
134
 
135
+ RB_GC_GUARD(workspace_value);
136
+ RB_GC_GUARD(table_value);
102
137
  RB_GC_GUARD(params.block);
103
138
  RB_GC_GUARD(params.exception);
104
139