numo-gsl-integration 0.5.0 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa4ddfbcf26eeefdb3642b8358c008ae8e839a0d77dcf934b77e98f2bc3e44f7
4
- data.tar.gz: 07c1c707e6b9d040ac14e996f334f2a207fa0d1139869a93cd032db23d60ac8f
3
+ metadata.gz: 01a7be9ee19ecec129a5afe36fc590bb556a733e826703d01188ed696d8fe60a
4
+ data.tar.gz: 1ad488db58b98142070b70211442a7bc0bde14b3507f21cff9a215c7b8b81d81
5
5
  SHA512:
6
- metadata.gz: 06216b69ff868309cc71941d383db61db1cea770b79b48a606470394229d3fc6ea3107ceb083f4b698d6911f30f31ec6c7599a27c3a7e15c8d3cb6ce11c0975a
7
- data.tar.gz: 13e9ae3991bbd3079a78eb58ee6165edde4c4c3f80a76f8fa4323d4e388c3c73bba95ba6d4115976f6682f8301805938f722a671b4e481627ad21f5981e95aa4
6
+ metadata.gz: a1cd519426b50c54de12f3150738155d8174fa47c3474759599fc886b80fa078cf7aa9621886ff811d4902fd129f75d216ee9d0a987457d7f3341d440e44b6b8
7
+ data.tar.gz: e68326e035e187fc84314bd5f5f5ca9cc0c7339d4b367f558351ac434ac47d2a08b0c1f53a809924dd38d7e0d95199404885f2667c6ba5b10a47d53cee129a6f
data/CHANGELOG.md CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.6.0] - 2026-11-08
6
+
7
+ ### Added in 0.6.0
8
+
9
+ - Added `Numo::GSL::Integration::QAWFTable` for reusing Fourier-cycle QAWO moment tables with a fixed angular frequency, sine or cosine weight, and level count.
10
+
11
+ ### Changed in 0.6.0
12
+
13
+ - Added optional `workspace:` and `cycle_workspace:` keywords to `qawf` so its outer Fourier-cycle workspace and per-cycle QAWO workspace can be reused independently. Calls that omit either keyword continue to allocate and release the missing workspace for that call.
14
+ - Added an optional `table:` keyword to `qawf`. Calls that omit it continue to allocate and release a temporary QAWO table.
15
+
16
+ ### Fixed in 0.6.0
17
+
18
+ - Validated that the `qawf` sine or cosine selector is an integer equal to `SINE` or `COSINE`.
19
+ - Prevented open `QAWOTable` and `QAWFTable` objects from being reinitialized and leaking their native tables.
20
+ - Avoided intermediate overflow when calculating QAWF cycle lengths and rejected angular frequencies that produce a non-finite cycle length.
21
+
5
22
  ## [0.5.0] - 2026-11-07
6
23
 
7
24
  ### Added in 0.5.0
data/README.md CHANGED
@@ -57,9 +57,11 @@ Numo::GSL::Integration::Workspace.open(limit: 100) do |workspace|
57
57
  end
58
58
  ```
59
59
 
60
- Reusable workspaces are supported by `qag`, `qags`, `qagp`, `qagi`, `qagiu`, `qagil`, `qawc`, `qaws`, and `qawo`. See [Integration routines](docs/routines.md) for lifecycle and capacity details.
60
+ Reusable workspaces are supported by `qag`, `qags`, `qagp`, `qagi`, `qagiu`, `qagil`, `qawc`, `qaws`, `qawo`, and `qawf`. See [Integration routines](docs/routines.md) for lifecycle and capacity details.
61
61
 
62
- For repeated weighted integrations, reusable moment tables can also retain precomputed coefficients. `Numo::GSL::Integration::QAWSTable` is used by `qaws` with fixed `alpha`, `beta`, `mu`, and `nu` parameters, while `Numo::GSL::Integration::QAWOTable` is used by `qawo` with fixed angular frequency, interval length, sine or cosine weight, and level count. Pass either object with the `table:` keyword; omitting `table:` retains the allocate-per-call behavior.
62
+ For `qawf`, pass separate `workspace:` and `cycle_workspace:` objects. The first retains Fourier-cycle results and extrapolation state, while the second is used for QAWO integration within each cycle.
63
+
64
+ For repeated weighted integrations, reusable moment tables can also retain precomputed coefficients. `Numo::GSL::Integration::QAWSTable` is used by `qaws` with fixed `alpha`, `beta`, `mu`, and `nu` parameters, while `Numo::GSL::Integration::QAWOTable` is used by `qawo` with fixed angular frequency, interval length, sine or cosine weight, and level count. `Numo::GSL::Integration::QAWFTable` is used by `qawf`; it computes and retains the Fourier-cycle moments for a fixed angular frequency, sine or cosine weight, and level count. Pass the corresponding object with the `table:` keyword; omitting `table:` retains the allocate-per-call behavior.
63
65
 
64
66
  ## Supported routines
65
67
 
data/docs/routines.md CHANGED
@@ -10,7 +10,7 @@ Exceptions raised by an integrand are propagated to the caller. A non-numeric re
10
10
 
11
11
  ## Reusable integration workspaces
12
12
 
13
- `Numo::GSL::Integration::Workspace` retains a GSL adaptive-integration workspace so repeated calls can reuse the same allocation. It is supported by `qag`, `qags`, `qagp`, `qagi`, `qagiu`, `qagil`, `qawc`, `qaws`, and `qawo`. Other routines continue to manage their own routine-specific workspaces.
13
+ `Numo::GSL::Integration::Workspace` retains a GSL adaptive-integration workspace so repeated calls can reuse the same allocation. It is supported by `qag`, `qags`, `qagp`, `qagi`, `qagiu`, `qagil`, `qawc`, `qaws`, `qawo`, and `qawf`. Other routines continue to manage their own routine-specific workspaces.
14
14
 
15
15
  Use `.open` when the workspace should be closed automatically after a block:
16
16
 
@@ -47,6 +47,8 @@ end
47
47
 
48
48
  When `workspace:` is omitted, each integration call allocates and releases a temporary workspace as before. When a reusable workspace is supplied and `limit:` is omitted, the call uses the workspace capacity. An explicit `limit:` must not exceed that capacity.
49
49
 
50
+ QAWF has two independent workspace roles. Its `workspace:` stores Fourier-cycle results and extrapolation state, while `cycle_workspace:` is used by QAWO within each cycle. Either may be omitted independently. When `limit:` is omitted, QAWF uses the supplied workspace capacity, or the smaller capacity when both are supplied. An explicit `limit:` must fit both workspaces, and the two roles must use different `Workspace` objects.
51
+
50
52
  A workspace cannot be used recursively or by overlapping integration calls. It also cannot be closed while an integration is using it. These cases raise `RuntimeError`. Passing a closed workspace raises `RuntimeError`, and passing an object that is not an integration workspace raises `TypeError`.
51
53
 
52
54
  ## QNG integration
@@ -226,7 +228,7 @@ GSL [QAWC](https://www.gnu.org/software/gsl/doc/html/integration.html#qawc-adapt
226
228
  PV \int_a^b \frac{f(x)}{x-c}\,dx = \lim_{\varepsilon\to 0^{+}} \left( \int_a^{c-\varepsilon}\frac{f(x)}{x-c}\,dx + \int_{c+\varepsilon}^b\frac{f(x)}{x-c}\,dx \right)
227
229
  ```
228
230
 
229
- The block passed to `Numo::GSL::Integration.qawc` returns $f(x)$. Do not divide the block result by $x - c$; QAWC applies that weight internally.
231
+ The block passed to `Numo::GSL::Integration.qawc` returns $f(x)$. Do not divide the block result by $x-c$; QAWC applies that weight internally.
230
232
 
231
233
  The following example uses the default integration options and computes the principal value of `x / (x - 1.0)` over the interval from `0.0` to `2.0`, whose exact value is `2.0`:
232
234
 
@@ -262,13 +264,7 @@ GSL [QAWS](https://www.gnu.org/software/gsl/doc/html/integration.html#qaws-adapt
262
264
 
263
265
  Call `Numo::GSL::Integration.qaws` with the endpoints `a` and `b`, followed by `alpha`, `beta`, `mu`, and `nu`. The exponents `alpha` and `beta` must be greater than `-1.0`. The logarithmic powers `mu` and `nu` must each be `0` or `1`.
264
266
 
265
- The block returns the unweighted function $f(x)$. QAWS applies the endpoint weight internally. The following example integrates
266
-
267
- ```math
268
- \frac{1}{\sqrt{x(1-x)}}
269
- ```
270
-
271
- over the interval from `0.0` to `1.0`. Its exact value is `Math::PI`:
267
+ The block returns the unweighted function $f(x)$. QAWS applies the endpoint weight internally. With `alpha: -0.5`, `beta: -0.5`, `mu: 0`, and `nu: 0`, the following example integrates the resulting weighted function over the interval from `0.0` to `1.0`. Its exact value is `Math::PI`:
272
268
 
273
269
  ```ruby
274
270
  result, error = Numo::GSL::Integration.qaws(
@@ -386,7 +382,48 @@ result, error = Numo::GSL::Integration.qawf(
386
382
  end
387
383
  ```
388
384
 
389
- The `epsabs` keyword controls the requested absolute error. The `limit` keyword controls the maximum number of Fourier cycles and the allocated workspace capacity.
385
+ The `epsabs` keyword controls the requested absolute error. The `limit` keyword controls both the maximum number of Fourier cycles and the maximum number of QAWO subintervals used within each cycle.
386
+
387
+ Use two workspaces when repeated QAWF calls should reuse both native allocations:
388
+
389
+ ```ruby
390
+ Numo::GSL::Integration::Workspace.open(limit: 100) do |workspace|
391
+ Numo::GSL::Integration::Workspace.open(limit: 50) do |cycle_workspace|
392
+ result, error = Numo::GSL::Integration.qawf(
393
+ 0.0,
394
+ 1.0,
395
+ Numo::GSL::Integration::COSINE,
396
+ workspace: workspace,
397
+ cycle_workspace: cycle_workspace
398
+ ) do |x|
399
+ Math.exp(-x)
400
+ end
401
+ end
402
+ end
403
+ ```
404
+
405
+ The two workspaces must be different objects. Either keyword can be omitted independently, in which case QAWF allocates and releases only the missing workspace. When `limit:` is omitted, QAWF uses the capacity of the supplied workspace, or the smaller capacity when both are supplied. An explicit `limit:` must not exceed either supplied capacity.
406
+
407
+ QAWF uses a QAWO moment table whose interval length is the Fourier-cycle width defined by GSL. Reuse those moments with `Numo::GSL::Integration::QAWFTable`. The table computes the cycle width automatically, so it does not accept a `length:` keyword:
408
+
409
+ ```ruby
410
+ Numo::GSL::Integration::QAWFTable.open(
411
+ omega: 1.0,
412
+ sine: Numo::GSL::Integration::COSINE,
413
+ levels: 100
414
+ ) do |table|
415
+ result, error = Numo::GSL::Integration.qawf(
416
+ 0.0,
417
+ 1.0,
418
+ Numo::GSL::Integration::COSINE,
419
+ table: table
420
+ ) do |x|
421
+ Math.exp(-x)
422
+ end
423
+ end
424
+ ```
425
+
426
+ `QAWFTable` inherits the lifecycle and parameter accessors of `QAWOTable`. Its `length` is the computed Fourier-cycle width. The angular frequency must be finite and nonzero. A supplied table must match the call's angular frequency and sine or cosine weight. It can be combined with both reusable workspaces, and remains usable by QAWO over its fixed cycle-length interval. When `table:` is omitted, QAWF allocates and releases a temporary table for the call.
390
427
 
391
428
  ## CQUAD integration
392
429
 
@@ -18,9 +18,12 @@ void Init_integration(void) {
18
18
  mGSL = rb_define_module_under(mNumo, "GSL");
19
19
  mIntegration = rb_define_module_under(mGSL, "Integration");
20
20
  numo_eGslError = rb_define_class_under(mGSL, "GslError", rb_eStandardError);
21
+
21
22
  gsl_set_error_handler(&integration_gsl_error_handler);
23
+
22
24
  integration_workspace_init(mIntegration);
23
25
  integration_qawo_table_init(mIntegration);
26
+ integration_qawf_table_init(mIntegration);
24
27
  integration_qaws_table_init(mIntegration);
25
28
  integration_fixed_init();
26
29
 
@@ -32,6 +32,11 @@ typedef struct {
32
32
  } integration_workspace_use;
33
33
 
34
34
  void integration_workspace_init(VALUE integration_module);
35
+ size_t integration_workspace_capacity(VALUE workspace_value, const char *name);
36
+ void integration_workspace_use_begin_named(VALUE workspace_value, size_t *limit,
37
+ int limit_given,
38
+ integration_workspace_use *use,
39
+ const char *name);
35
40
  void integration_workspace_use_begin(VALUE workspace_value, size_t *limit,
36
41
  int limit_given,
37
42
  integration_workspace_use *use);
@@ -58,6 +63,11 @@ void integration_qawo_table_use_begin(VALUE table_value, double omega,
58
63
  size_t levels,
59
64
  integration_qawo_table_use *use);
60
65
  VALUE integration_qawo_table_use_end(VALUE data);
66
+ void integration_qawf_table_init(VALUE integration_module);
67
+ void integration_qawf_table_use_begin(VALUE table_value, double omega,
68
+ enum gsl_integration_qawo_enum sine,
69
+ size_t levels,
70
+ integration_qawo_table_use *use);
61
71
 
62
72
  typedef struct {
63
73
  gsl_integration_qaws_table *table;
@@ -8,9 +8,14 @@ typedef struct {
8
8
  enum gsl_integration_qawo_enum sine;
9
9
  double epsabs;
10
10
  size_t limit;
11
- gsl_integration_workspace *workspace;
12
- gsl_integration_workspace *cycle_workspace;
13
- gsl_integration_qawo_table *table;
11
+ VALUE workspace_value;
12
+ VALUE cycle_workspace_value;
13
+ VALUE table_value;
14
+ integration_workspace_use workspace_use;
15
+ integration_workspace_use cycle_workspace_use;
16
+ integration_qawo_table_use table_use;
17
+ size_t cycle_workspace_native_limit;
18
+ int cycle_workspace_limit_changed;
14
19
  double *result;
15
20
  double *error;
16
21
  } integration_qawf_params;
@@ -18,17 +23,16 @@ typedef struct {
18
23
  static VALUE integration_free_qawf_resources(VALUE data) {
19
24
  integration_qawf_params *params = (integration_qawf_params *)data;
20
25
 
21
- if (params->table != NULL) {
22
- gsl_integration_qawo_table_free(params->table);
23
- }
26
+ integration_qawo_table_use_end((VALUE)&params->table_use);
24
27
 
25
- if (params->cycle_workspace != NULL) {
26
- gsl_integration_workspace_free(params->cycle_workspace);
28
+ if (params->cycle_workspace_limit_changed) {
29
+ params->cycle_workspace_use.workspace->limit =
30
+ params->cycle_workspace_native_limit;
31
+ params->cycle_workspace_limit_changed = 0;
27
32
  }
28
33
 
29
- if (params->workspace != NULL) {
30
- gsl_integration_workspace_free(params->workspace);
31
- }
34
+ integration_workspace_use_end((VALUE)&params->cycle_workspace_use);
35
+ integration_workspace_use_end((VALUE)&params->workspace_use);
32
36
 
33
37
  return Qnil;
34
38
  }
@@ -36,15 +40,25 @@ static VALUE integration_free_qawf_resources(VALUE data) {
36
40
  static VALUE integration_run_qawf(VALUE data) {
37
41
  integration_qawf_params *params = (integration_qawf_params *)data;
38
42
 
39
- params->table = gsl_integration_qawo_table_alloc(params->omega, 1.0,
40
- params->sine, params->limit);
41
- params->workspace = gsl_integration_workspace_alloc(params->limit);
42
- params->cycle_workspace = gsl_integration_workspace_alloc(params->limit);
43
+ integration_workspace_use_begin(params->workspace_value, &params->limit, 1,
44
+ &params->workspace_use);
45
+ integration_workspace_use_begin_named(
46
+ params->cycle_workspace_value, &params->limit, 1,
47
+ &params->cycle_workspace_use, "cycle_workspace");
48
+
49
+ params->cycle_workspace_native_limit =
50
+ params->cycle_workspace_use.workspace->limit;
51
+ params->cycle_workspace_use.workspace->limit = params->limit;
52
+ params->cycle_workspace_limit_changed = 1;
43
53
 
44
- (void)gsl_integration_qawf(params->function, params->a, params->epsabs,
45
- params->limit, params->workspace,
46
- params->cycle_workspace, params->table,
47
- params->result, params->error);
54
+ integration_qawf_table_use_begin(params->table_value, params->omega,
55
+ params->sine, params->limit,
56
+ &params->table_use);
57
+
58
+ (void)gsl_integration_qawf(
59
+ params->function, params->a, params->epsabs, params->limit,
60
+ params->workspace_use.workspace, params->cycle_workspace_use.workspace,
61
+ params->table_use.table, params->result, params->error);
48
62
 
49
63
  return Qnil;
50
64
  }
@@ -61,13 +75,23 @@ VALUE integration_qawf(int argc, VALUE *argv, VALUE self) {
61
75
  VALUE options;
62
76
  VALUE epsabs_value = Qundef;
63
77
  VALUE limit_value = Qundef;
64
- ID keywords[] = {rb_intern("epsabs"), rb_intern("limit")};
65
- VALUE keyword_values[2];
78
+ VALUE workspace_value = Qundef;
79
+ VALUE cycle_workspace_value = Qundef;
80
+ VALUE table_value = Qundef;
81
+ ID keywords[] = {rb_intern("epsabs"), rb_intern("limit"),
82
+ rb_intern("workspace"), rb_intern("cycle_workspace"),
83
+ rb_intern("table")};
84
+ VALUE keyword_values[5];
66
85
  double epsabs;
67
- size_t limit;
86
+ size_t limit = 1000;
87
+ size_t workspace_capacity = 0;
88
+ size_t cycle_workspace_capacity = 0;
89
+ int limit_given;
90
+ int workspace_given;
91
+ int cycle_workspace_given;
68
92
  gsl_function function;
69
93
  integration_callback_params params;
70
- integration_qawf_params qawf_params;
94
+ integration_qawf_params qawf_params = {0};
71
95
  double result;
72
96
  double error;
73
97
  int state = 0;
@@ -77,25 +101,74 @@ VALUE integration_qawf(int argc, VALUE *argv, VALUE self) {
77
101
  rb_scan_args(argc, argv, "3:", &a, &omega, &sine, &options);
78
102
 
79
103
  if (!NIL_P(options)) {
80
- rb_get_kwargs(options, keywords, 0, 2, keyword_values);
104
+ rb_get_kwargs(options, keywords, 0, 5, keyword_values);
81
105
  epsabs_value = keyword_values[0];
82
106
  limit_value = keyword_values[1];
107
+ workspace_value = keyword_values[2];
108
+ cycle_workspace_value = keyword_values[3];
109
+ table_value = keyword_values[4];
83
110
  }
84
111
 
112
+ qawf_params.a = NUM2DBL(a);
113
+ qawf_params.omega = NUM2DBL(omega);
114
+ if (!RB_INTEGER_TYPE_P(sine)) {
115
+ rb_raise(rb_eTypeError, "sine must be an Integer");
116
+ }
117
+ qawf_params.sine = (enum gsl_integration_qawo_enum)NUM2INT(sine);
118
+ if (qawf_params.sine != GSL_INTEG_SINE &&
119
+ qawf_params.sine != GSL_INTEG_COSINE) {
120
+ rb_raise(rb_eArgError, "sine must be SINE or COSINE");
121
+ }
85
122
  epsabs = epsabs_value == Qundef ? 1e-10 : NUM2DBL(epsabs_value);
86
- limit = limit_value == Qundef ? 1000 : NUM2SIZET(limit_value);
123
+ limit_given = limit_value != Qundef;
124
+ if (limit_given) {
125
+ limit = NUM2SIZET(limit_value);
126
+ }
127
+
128
+ workspace_given = workspace_value != Qundef;
129
+ cycle_workspace_given = cycle_workspace_value != Qundef;
130
+
131
+ if (workspace_given) {
132
+ workspace_capacity =
133
+ integration_workspace_capacity(workspace_value, "workspace");
134
+ }
135
+ if (cycle_workspace_given) {
136
+ cycle_workspace_capacity = integration_workspace_capacity(
137
+ cycle_workspace_value, "cycle_workspace");
138
+ }
139
+ if (workspace_given && cycle_workspace_given &&
140
+ workspace_value == cycle_workspace_value) {
141
+ rb_raise(rb_eArgError,
142
+ "workspace and cycle_workspace must be different objects");
143
+ }
144
+
145
+ if (!limit_given) {
146
+ if (workspace_given && cycle_workspace_given) {
147
+ limit = workspace_capacity < cycle_workspace_capacity
148
+ ? workspace_capacity
149
+ : cycle_workspace_capacity;
150
+ } else if (workspace_given) {
151
+ limit = workspace_capacity;
152
+ } else if (cycle_workspace_given) {
153
+ limit = cycle_workspace_capacity;
154
+ }
155
+ }
156
+
157
+ if (workspace_given && limit > workspace_capacity) {
158
+ rb_raise(rb_eArgError, "limit exceeds workspace capacity");
159
+ }
160
+ if (cycle_workspace_given && limit > cycle_workspace_capacity) {
161
+ rb_raise(rb_eArgError, "limit exceeds cycle_workspace capacity");
162
+ }
87
163
 
88
164
  integration_function_init(&function, &params);
89
165
 
90
166
  qawf_params.function = &function;
91
- qawf_params.a = NUM2DBL(a);
92
- qawf_params.omega = NUM2DBL(omega);
93
- qawf_params.sine = (enum gsl_integration_qawo_enum)NUM2INT(sine);
94
167
  qawf_params.epsabs = epsabs;
95
168
  qawf_params.limit = limit;
96
- qawf_params.workspace = NULL;
97
- qawf_params.cycle_workspace = NULL;
98
- qawf_params.table = NULL;
169
+ qawf_params.workspace_value = workspace_value;
170
+ qawf_params.cycle_workspace_value = cycle_workspace_value;
171
+ qawf_params.table_value = table_value;
99
172
  qawf_params.result = &result;
100
173
  qawf_params.error = &error;
101
174
 
@@ -103,6 +176,9 @@ VALUE integration_qawf(int argc, VALUE *argv, VALUE self) {
103
176
 
104
177
  RB_GC_GUARD(params.block);
105
178
  RB_GC_GUARD(params.exception);
179
+ RB_GC_GUARD(workspace_value);
180
+ RB_GC_GUARD(cycle_workspace_value);
181
+ RB_GC_GUARD(table_value);
106
182
 
107
183
  integration_finish_callback(&params, state);
108
184
 
@@ -0,0 +1,120 @@
1
+ #include "integration_internal.h"
2
+
3
+ #include <float.h>
4
+ #include <math.h>
5
+
6
+ static VALUE integration_cQAWFTable;
7
+
8
+ static double integration_qawf_cycle_length(double omega) {
9
+ double absolute_omega = fabs(omega);
10
+ double length =
11
+ (2.0 * (floor(absolute_omega) / absolute_omega) + 1.0 / absolute_omega) *
12
+ M_PI;
13
+
14
+ if (!isfinite(length) || length <= 0.0) {
15
+ rb_raise(rb_eArgError, "omega produces a non-finite cycle length");
16
+ }
17
+
18
+ return length;
19
+ }
20
+
21
+ static int integration_qawf_table_double_equal(double left, double right) {
22
+ double scale = fmax(fabs(left), fabs(right));
23
+
24
+ return fabs(left - right) <= DBL_EPSILON * fmax(1.0, scale) * 4.0;
25
+ }
26
+
27
+ static VALUE integration_qawf_table_initialize(int argc, VALUE *argv,
28
+ VALUE self) {
29
+ VALUE options;
30
+ VALUE omega_value;
31
+ VALUE sine_value;
32
+ VALUE levels_value;
33
+ VALUE qawo_options;
34
+ VALUE super_argv[1];
35
+ ID keywords[] = {rb_intern("omega"), rb_intern("sine"), rb_intern("levels")};
36
+ VALUE keyword_values[3];
37
+ double omega;
38
+ int sine;
39
+
40
+ rb_scan_args(argc, argv, "0:", &options);
41
+ if (NIL_P(options)) {
42
+ rb_raise(rb_eArgError, "missing keywords: :omega, :sine, :levels");
43
+ }
44
+ rb_get_kwargs(options, keywords, 3, 0, keyword_values);
45
+ omega_value = keyword_values[0];
46
+ sine_value = keyword_values[1];
47
+ levels_value = keyword_values[2];
48
+
49
+ if (!rb_obj_is_kind_of(omega_value, rb_cNumeric)) {
50
+ rb_raise(rb_eTypeError, "omega must be Numeric");
51
+ }
52
+ if (!RB_INTEGER_TYPE_P(sine_value)) {
53
+ rb_raise(rb_eTypeError, "sine must be an Integer");
54
+ }
55
+ if (!RB_INTEGER_TYPE_P(levels_value)) {
56
+ rb_raise(rb_eTypeError, "levels must be an Integer");
57
+ }
58
+
59
+ omega = NUM2DBL(omega_value);
60
+ sine = NUM2INT(sine_value);
61
+ if (!isfinite(omega) || omega == 0.0) {
62
+ rb_raise(rb_eArgError, "omega must be finite and nonzero");
63
+ }
64
+ if (sine != GSL_INTEG_SINE && sine != GSL_INTEG_COSINE) {
65
+ rb_raise(rb_eArgError, "sine must be SINE or COSINE");
66
+ }
67
+ if (RTEST(rb_funcall(levels_value, rb_intern("<="), 1, INT2FIX(0)))) {
68
+ rb_raise(rb_eArgError, "levels must be positive");
69
+ }
70
+
71
+ qawo_options = rb_hash_new();
72
+ rb_hash_aset(qawo_options, ID2SYM(rb_intern("omega")), omega_value);
73
+ rb_hash_aset(qawo_options, ID2SYM(rb_intern("length")),
74
+ DBL2NUM(integration_qawf_cycle_length(omega)));
75
+ rb_hash_aset(qawo_options, ID2SYM(rb_intern("sine")), sine_value);
76
+ rb_hash_aset(qawo_options, ID2SYM(rb_intern("levels")), levels_value);
77
+ super_argv[0] = qawo_options;
78
+
79
+ return rb_call_super_kw(1, super_argv, RB_PASS_KEYWORDS);
80
+ }
81
+
82
+ void integration_qawf_table_use_begin(VALUE table_value, double omega,
83
+ enum gsl_integration_qawo_enum sine,
84
+ size_t levels,
85
+ integration_qawo_table_use *use) {
86
+ double length = omega == 0.0 ? 1.0 : integration_qawf_cycle_length(omega);
87
+
88
+ if (table_value != Qundef) {
89
+ double table_omega;
90
+ enum gsl_integration_qawo_enum table_sine;
91
+
92
+ if (!rb_obj_is_kind_of(table_value, integration_cQAWFTable)) {
93
+ rb_raise(rb_eTypeError,
94
+ "table must be a Numo::GSL::Integration::QAWFTable");
95
+ }
96
+
97
+ table_omega = NUM2DBL(rb_funcall(table_value, rb_intern("omega"), 0));
98
+ table_sine = (enum gsl_integration_qawo_enum)NUM2INT(
99
+ rb_funcall(table_value, rb_intern("sine"), 0));
100
+ if (!integration_qawf_table_double_equal(table_omega, omega)) {
101
+ rb_raise(rb_eArgError, "omega does not match QAWF table");
102
+ }
103
+ if (table_sine != sine) {
104
+ rb_raise(rb_eArgError, "sine does not match QAWF table");
105
+ }
106
+ }
107
+
108
+ integration_qawo_table_use_begin(table_value, omega, length, sine, levels,
109
+ use);
110
+ }
111
+
112
+ void integration_qawf_table_init(VALUE integration_module) {
113
+ VALUE qawo_table_class =
114
+ rb_const_get(integration_module, rb_intern("QAWOTable"));
115
+
116
+ integration_cQAWFTable =
117
+ rb_define_class_under(integration_module, "QAWFTable", qawo_table_class);
118
+ rb_define_method(integration_cQAWFTable, "initialize",
119
+ integration_qawf_table_initialize, -1);
120
+ }
@@ -118,6 +118,9 @@ static VALUE integration_qawo_table_initialize(int argc, VALUE *argv,
118
118
  levels = NUM2SIZET(levels_value);
119
119
  TypedData_Get_Struct(self, integration_qawo_table,
120
120
  &integration_qawo_table_type, table);
121
+ if (table->table != NULL) {
122
+ rb_raise(rb_eRuntimeError, "table is already initialized");
123
+ }
121
124
  table->omega = omega;
122
125
  table->length = length;
123
126
  table->sine = (enum gsl_integration_qawo_enum)sine;
@@ -121,9 +121,35 @@ static VALUE integration_workspace_open(int argc, VALUE *argv, VALUE klass) {
121
121
  integration_workspace_close, workspace);
122
122
  }
123
123
 
124
- void integration_workspace_use_begin(VALUE workspace_value, size_t *limit,
125
- int limit_given,
126
- integration_workspace_use *use) {
124
+ static integration_workspace *integration_workspace_get(VALUE workspace_value,
125
+ const char *name) {
126
+ integration_workspace *workspace;
127
+
128
+ if (!rb_obj_is_kind_of(workspace_value, integration_cWorkspace)) {
129
+ rb_raise(rb_eTypeError, "%s must be a Numo::GSL::Integration::Workspace",
130
+ name);
131
+ }
132
+
133
+ TypedData_Get_Struct(workspace_value, integration_workspace,
134
+ &integration_workspace_type, workspace);
135
+ if (workspace->workspace == NULL) {
136
+ rb_raise(rb_eRuntimeError, "%s is closed", name);
137
+ }
138
+ if (workspace->busy) {
139
+ rb_raise(rb_eRuntimeError, "%s is already in use", name);
140
+ }
141
+
142
+ return workspace;
143
+ }
144
+
145
+ size_t integration_workspace_capacity(VALUE workspace_value, const char *name) {
146
+ return integration_workspace_get(workspace_value, name)->limit;
147
+ }
148
+
149
+ void integration_workspace_use_begin_named(VALUE workspace_value, size_t *limit,
150
+ int limit_given,
151
+ integration_workspace_use *use,
152
+ const char *name) {
127
153
  integration_workspace *workspace;
128
154
 
129
155
  use->workspace = NULL;
@@ -137,21 +163,9 @@ void integration_workspace_use_begin(VALUE workspace_value, size_t *limit,
137
163
  return;
138
164
  }
139
165
 
140
- if (!rb_obj_is_kind_of(workspace_value, integration_cWorkspace)) {
141
- rb_raise(rb_eTypeError,
142
- "workspace must be a Numo::GSL::Integration::Workspace");
143
- }
144
-
145
- TypedData_Get_Struct(workspace_value, integration_workspace,
146
- &integration_workspace_type, workspace);
147
- if (workspace->workspace == NULL) {
148
- rb_raise(rb_eRuntimeError, "workspace is closed");
149
- }
150
- if (workspace->busy) {
151
- rb_raise(rb_eRuntimeError, "workspace is already in use");
152
- }
166
+ workspace = integration_workspace_get(workspace_value, name);
153
167
  if (limit_given && *limit > workspace->limit) {
154
- rb_raise(rb_eArgError, "limit exceeds workspace capacity");
168
+ rb_raise(rb_eArgError, "limit exceeds %s capacity", name);
155
169
  }
156
170
  if (!limit_given) {
157
171
  *limit = workspace->limit;
@@ -162,6 +176,13 @@ void integration_workspace_use_begin(VALUE workspace_value, size_t *limit,
162
176
  use->owner = workspace;
163
177
  }
164
178
 
179
+ void integration_workspace_use_begin(VALUE workspace_value, size_t *limit,
180
+ int limit_given,
181
+ integration_workspace_use *use) {
182
+ integration_workspace_use_begin_named(workspace_value, limit, limit_given,
183
+ use, "workspace");
184
+ }
185
+
165
186
  VALUE integration_workspace_use_end(VALUE data) {
166
187
  integration_workspace_use *use = (integration_workspace_use *)data;
167
188
 
@@ -3,7 +3,7 @@
3
3
  module Numo
4
4
  module GSL
5
5
  module Integration
6
- VERSION = "0.5.0"
6
+ VERSION = "0.6.0"
7
7
  end
8
8
  end
9
9
  end
@@ -37,6 +37,18 @@ module Numo
37
37
  def close: () -> nil
38
38
  def closed?: () -> bool
39
39
  end
40
+ class QAWFTable < QAWOTable
41
+ def self.open: [T] (
42
+ omega: Numeric,
43
+ sine: Integer,
44
+ levels: Integer
45
+ ) { (QAWFTable) -> T } -> T
46
+ def initialize: (
47
+ omega: Numeric,
48
+ sine: Integer,
49
+ levels: Integer
50
+ ) -> void
51
+ end
40
52
  class QAWSTable
41
53
  def self.open: [T] (
42
54
  alpha: Numeric,
@@ -145,7 +157,10 @@ module Numo
145
157
  Numeric omega,
146
158
  Integer sine,
147
159
  ?epsabs: Numeric,
148
- ?limit: Integer
160
+ ?limit: Integer,
161
+ ?workspace: Workspace,
162
+ ?cycle_workspace: Workspace,
163
+ ?table: QAWFTable
149
164
  ) { (Float) -> Numeric } -> [Float, Float]
150
165
  def self.cquad: (
151
166
  Numeric a,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numo-gsl-integration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro SATOH
@@ -41,6 +41,7 @@ files:
41
41
  - ext/numo/gsl/integration/qags.c
42
42
  - ext/numo/gsl/integration/qawc.c
43
43
  - ext/numo/gsl/integration/qawf.c
44
+ - ext/numo/gsl/integration/qawf_table.c
44
45
  - ext/numo/gsl/integration/qawo.c
45
46
  - ext/numo/gsl/integration/qawo_table.c
46
47
  - ext/numo/gsl/integration/qaws.c