numo-gsl-integration 0.4.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: 57503e38512a38ad357142981868d2e2aea1a47743f27aee55d80625715eb794
4
- data.tar.gz: d87dadfb371056e006cb1a914c6cebe81131cfa21c1e5f4daa35f72f377a98bf
3
+ metadata.gz: 01a7be9ee19ecec129a5afe36fc590bb556a733e826703d01188ed696d8fe60a
4
+ data.tar.gz: 1ad488db58b98142070b70211442a7bc0bde14b3507f21cff9a215c7b8b81d81
5
5
  SHA512:
6
- metadata.gz: ad109b9e20ce13a6eeb8b34b919c49a353eb5b27b158e74d1f9d149219fc200ca9a567417a12a4ea6dc66f247caf68bfe864d5cbaaf811b58fdacd8c8ef6ffbb
7
- data.tar.gz: 6cab24c8607e941ab66eaa7f44db5492836c117649956979f148755df73792fd66c0baa794177276c02dd90e1e931d071cd0b19a174dd8d6dbb2c864da08e0cf
6
+ metadata.gz: a1cd519426b50c54de12f3150738155d8174fa47c3474759599fc886b80fa078cf7aa9621886ff811d4902fd129f75d216ee9d0a987457d7f3341d440e44b6b8
7
+ data.tar.gz: e68326e035e187fc84314bd5f5f5ca9cc0c7339d4b367f558351ac434ac47d2a08b0c1f53a809924dd38d7e0d95199404885f2667c6ba5b10a47d53cee129a6f
data/CHANGELOG.md CHANGED
@@ -2,6 +2,37 @@
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
+
22
+ ## [0.5.0] - 2026-11-07
23
+
24
+ ### Added in 0.5.0
25
+
26
+ - Added `Numo::GSL::Integration::Workspace` for reusing adaptive-integration workspace allocations across `qag`, `qags`, `qagp`, `qagi`, `qagiu`, `qagil`, `qawc`, `qaws`, and `qawo` calls.
27
+ - Added block-scoped workspace management with `Workspace.open`, explicit lifecycle methods with `close` and `closed?`, and workspace capacity reporting with `limit`.
28
+ - Added `Numo::GSL::Integration::QAWOTable` for reusing QAWO Chebyshev-moment tables with fixed angular frequency, interval length, weight, and level count.
29
+ - Added `Numo::GSL::Integration::QAWSTable` for reusing QAWS Chebyshev-moment tables with fixed algebraic and logarithmic weight parameters.
30
+
31
+ ### Changed in 0.5.0
32
+
33
+ - Added an optional `table:` keyword to `qaws` and `qawo`. Calls that omit it continue to allocate and release a temporary routine-specific table.
34
+ - Added an optional `workspace:` keyword to `qag`, `qags`, `qagp`, `qagi`, `qagiu`, `qagil`, `qawc`, `qaws`, and `qawo`. Calls that omit it retain the previous allocate-per-call behavior.
35
+
5
36
  ## [0.4.0] - 2026-11-06
6
37
 
7
38
  ### Added in 0.4.0
data/README.md CHANGED
@@ -7,7 +7,6 @@
7
7
  - [Ruby](https://www.ruby-lang.org/) 3.2 or later
8
8
  - GNU Scientific Library (GSL) 2.8 or later
9
9
  - `gsl-config` available in `PATH`
10
-
11
10
  GSL itself must be installed separately.
12
11
 
13
12
  ## Installation
@@ -42,6 +41,28 @@ result, error = Numo::GSL::Integration.qag(0.0, 1.0) do |x|
42
41
  end
43
42
  ```
44
43
 
44
+ To reuse the allocation across repeated adaptive integrations, create an integration workspace and pass it with the `workspace:` keyword:
45
+
46
+ ```ruby
47
+ Numo::GSL::Integration::Workspace.open(limit: 100) do |workspace|
48
+ 2.times do
49
+ result, error = Numo::GSL::Integration.qag(
50
+ 0.0,
51
+ 1.0,
52
+ workspace: workspace
53
+ ) do |x|
54
+ x * x
55
+ end
56
+ end
57
+ end
58
+ ```
59
+
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
+
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.
65
+
45
66
  ## Supported routines
46
67
 
47
68
  | Method | Interval | Intended use |
data/docs/routines.md CHANGED
@@ -8,6 +8,49 @@ require "numo/gsl/integration"
8
8
 
9
9
  Exceptions raised by an integrand are propagated to the caller. A non-numeric return value from an integrand raises `TypeError`.
10
10
 
11
+ ## Reusable integration workspaces
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`, `qawo`, and `qawf`. Other routines continue to manage their own routine-specific workspaces.
14
+
15
+ Use `.open` when the workspace should be closed automatically after a block:
16
+
17
+ ```ruby
18
+ Numo::GSL::Integration::Workspace.open(limit: 100) do |workspace|
19
+ result, error = Numo::GSL::Integration.qag(
20
+ 0.0,
21
+ 1.0,
22
+ workspace: workspace
23
+ ) do |x|
24
+ x * x
25
+ end
26
+ end
27
+ ```
28
+
29
+ Alternatively, create and close a workspace explicitly:
30
+
31
+ ```ruby
32
+ workspace = Numo::GSL::Integration::Workspace.new(limit: 100)
33
+ begin
34
+ result, error = Numo::GSL::Integration.qags(
35
+ 0.0,
36
+ 1.0,
37
+ workspace: workspace
38
+ ) do |x|
39
+ Math.log(x) / Math.sqrt(x)
40
+ end
41
+ ensure
42
+ workspace.close
43
+ end
44
+ ```
45
+
46
+ `Workspace#limit` returns the allocated capacity, and `Workspace#closed?` reports whether the workspace has been closed. Calling `close` more than once is allowed. If a workspace is not closed explicitly, Ruby's garbage collector releases it when the object is collected.
47
+
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
+
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
+
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`.
53
+
11
54
  ## QNG integration
12
55
 
13
56
  GSL [QNG](https://www.gnu.org/software/gsl/doc/html/integration.html#qng-non-adaptive-gauss-kronrod-integration) provides non-adaptive integration for smooth functions over a finite interval. It applies increasingly accurate Gauss-Kronrod-Patterson rules and evaluates the integrand at up to 87 points.
@@ -95,6 +138,7 @@ end
95
138
  ```
96
139
 
97
140
  The points must be finite numeric values in strictly increasing order. The array must contain at least two values. A non-array argument or a non-numeric element raises `TypeError`. Too few points, non-finite values, or values that are not strictly increasing raise `ArgumentError` before GSL is called.
141
+
98
142
  The `epsabs`, `epsrel`, and `limit` keyword arguments control the requested error limits and maximum number of subintervals:
99
143
 
100
144
  ```ruby
@@ -184,7 +228,8 @@ GSL [QAWC](https://www.gnu.org/software/gsl/doc/html/integration.html#qawc-adapt
184
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)
185
229
  ```
186
230
 
187
- 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.
232
+
188
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`:
189
234
 
190
235
  ```ruby
@@ -218,13 +263,8 @@ GSL [QAWS](https://www.gnu.org/software/gsl/doc/html/integration.html#qaws-adapt
218
263
  ```
219
264
 
220
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`.
221
- The block returns the unweighted function $f(x)$. QAWS applies the endpoint weight internally. The following example integrates
222
266
 
223
- ```math
224
- \frac{1}{\sqrt{x(1-x)}}
225
- ```
226
-
227
- 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`:
228
268
 
229
269
  ```ruby
230
270
  result, error = Numo::GSL::Integration.qaws(
@@ -259,6 +299,34 @@ end
259
299
 
260
300
  This example computes the integral of `log(x)` from `0.0` to `1.0`, whose exact value is `-1.0`.
261
301
 
302
+ QAWS precomputes Chebyshev moments for the algebraic and logarithmic weight parameters. Reuse those moments by creating a `Numo::GSL::Integration::QAWSTable` with fixed `alpha`, `beta`, `mu`, and `nu` parameters and passing it with `table:`:
303
+
304
+ ```ruby
305
+ Numo::GSL::Integration::Workspace.open(limit: 100) do |workspace|
306
+ Numo::GSL::Integration::QAWSTable.open(
307
+ alpha: -0.5,
308
+ beta: -0.5,
309
+ mu: 0,
310
+ nu: 0
311
+ ) do |table|
312
+ result, error = Numo::GSL::Integration.qaws(
313
+ 0.0,
314
+ 1.0,
315
+ -0.5,
316
+ -0.5,
317
+ 0,
318
+ 0,
319
+ workspace: workspace,
320
+ table: table
321
+ ) { |_x| 1.0 }
322
+ end
323
+ end
324
+ ```
325
+
326
+ `QAWSTable#alpha`, `#beta`, `#mu`, and `#nu` return the fixed weight parameters. A supplied table must match the corresponding `qaws` arguments, but it can be reused for different integration intervals because the endpoints are not part of the table. It cannot be used recursively or by overlapping calls, and it cannot be closed while in use. Use `QAWSTable.open` for block-scoped cleanup, or call `close` explicitly. Calling `close` more than once is allowed, and Ruby's garbage collector releases an unclosed table.
327
+
328
+ When `table:` is omitted, `qaws` allocates and releases a temporary QAWS table for the call. A reusable `table:` may be used with or without a reusable `workspace:`.
329
+
262
330
  ## QAWO integration
263
331
 
264
332
  GSL [QAWO](https://www.gnu.org/software/gsl/doc/html/integration.html#qawo-adaptive-integration-for-oscillatory-functions) provides adaptive integration for finite-interval integrals with a sine or cosine weight. The block returns the unweighted function; QAWO applies the oscillatory weight internally.
@@ -274,7 +342,31 @@ result, error = Numo::GSL::Integration.qawo(
274
342
  end
275
343
  ```
276
344
 
277
- The `epsabs`, `epsrel`, and `limit` keyword arguments control the requested error limits and workspace capacity.
345
+ The `epsabs`, `epsrel`, and `limit` keyword arguments control the requested error limits and workspace capacity. QAWO also precomputes Chebyshev moments for the angular frequency, interval length, and sine or cosine weight. Reuse those moments by creating a `Numo::GSL::Integration::QAWOTable` with the same fixed parameters and passing it with `table:`:
346
+
347
+ ```ruby
348
+ Numo::GSL::Integration::Workspace.open(limit: 100) do |workspace|
349
+ Numo::GSL::Integration::QAWOTable.open(
350
+ omega: 1.0,
351
+ length: Math::PI,
352
+ sine: Numo::GSL::Integration::SINE,
353
+ levels: 100
354
+ ) do |table|
355
+ result, error = Numo::GSL::Integration.qawo(
356
+ 0.0,
357
+ Math::PI,
358
+ 1.0,
359
+ Numo::GSL::Integration::SINE,
360
+ workspace: workspace,
361
+ table: table
362
+ ) { |_x| 1.0 }
363
+ end
364
+ end
365
+ ```
366
+
367
+ `QAWOTable#omega`, `#length`, `#sine`, and `#levels` return the fixed table parameters. `levels` is the number of precomputed bisection levels, not the integration workspace capacity. A supplied table must match the call's angular frequency, interval length, and weight. It cannot be used recursively or by overlapping calls, and it cannot be closed while in use. Use `QAWOTable.open` for block-scoped cleanup, or call `close` explicitly. Calling `close` more than once is allowed, and Ruby's garbage collector releases an unclosed table.
368
+
369
+ When `table:` is omitted, `qawo` allocates and releases a temporary QAWO table for the call. A reusable `table:` may be used with or without a reusable `workspace:`.
278
370
 
279
371
  ## QAWF integration
280
372
 
@@ -290,7 +382,48 @@ result, error = Numo::GSL::Integration.qawf(
290
382
  end
291
383
  ```
292
384
 
293
- 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.
294
427
 
295
428
  ## CQUAD integration
296
429
 
@@ -315,6 +448,7 @@ end
315
448
  ```
316
449
 
317
450
  The example integral has an exact value of `2.0`. The `epsabs` and `epsrel` keyword arguments control the requested error limits. The `limit` keyword controls the maximum number of Romberg levels and defaults to `20`. GSL requires at least one level and limits the effective workspace to 30 levels.
451
+
318
452
  Romberg integration does not return an estimated absolute error. Use CQUAD, QAG, QAGS, or another routine that returns an error estimate when the caller needs one.
319
453
 
320
454
  ## GLFIXED integration
@@ -332,6 +466,7 @@ end
332
466
  ```
333
467
 
334
468
  The example integral has an exact value of `1.0 / 5.0`. The `order` keyword controls the number of Gauss-Legendre points and defaults to `10`. It must be a positive integer.
469
+
335
470
  GLFIXED returns only the integral. It does not accept absolute or relative error bounds and does not return an error estimate. Use an adaptive routine when the required fixed order is not known in advance.
336
471
 
337
472
  ## Fixed point quadratures
@@ -18,7 +18,13 @@ 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
+
24
+ integration_workspace_init(mIntegration);
25
+ integration_qawo_table_init(mIntegration);
26
+ integration_qawf_table_init(mIntegration);
27
+ integration_qaws_table_init(mIntegration);
22
28
  integration_fixed_init();
23
29
 
24
30
  rb_define_const(mIntegration, "GAUSS15", INT2NUM(GSL_INTEG_GAUSS15));
@@ -15,6 +15,79 @@ typedef struct {
15
15
  void integration_options_parse(VALUE options, integration_options *parsed,
16
16
  double default_epsabs, double default_epsrel,
17
17
  size_t default_limit);
18
+ void integration_options_parse_with_workspace(
19
+ VALUE options, integration_options *parsed, double default_epsabs,
20
+ double default_epsrel, size_t default_limit, VALUE *workspace_value,
21
+ int *limit_given);
22
+
23
+ typedef struct {
24
+ gsl_integration_workspace *workspace;
25
+ size_t limit;
26
+ int busy;
27
+ } integration_workspace;
28
+
29
+ typedef struct {
30
+ gsl_integration_workspace *workspace;
31
+ integration_workspace *owner;
32
+ } integration_workspace_use;
33
+
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);
40
+ void integration_workspace_use_begin(VALUE workspace_value, size_t *limit,
41
+ int limit_given,
42
+ integration_workspace_use *use);
43
+ VALUE integration_workspace_use_end(VALUE data);
44
+
45
+ typedef struct {
46
+ gsl_integration_qawo_table *table;
47
+ double omega;
48
+ double length;
49
+ enum gsl_integration_qawo_enum sine;
50
+ size_t levels;
51
+ int busy;
52
+ } integration_qawo_table;
53
+
54
+ typedef struct {
55
+ gsl_integration_qawo_table *table;
56
+ integration_qawo_table *owner;
57
+ } integration_qawo_table_use;
58
+
59
+ void integration_qawo_table_init(VALUE integration_module);
60
+ void integration_qawo_table_use_begin(VALUE table_value, double omega,
61
+ double length,
62
+ enum gsl_integration_qawo_enum sine,
63
+ size_t levels,
64
+ integration_qawo_table_use *use);
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);
71
+
72
+ typedef struct {
73
+ gsl_integration_qaws_table *table;
74
+ double alpha;
75
+ double beta;
76
+ int mu;
77
+ int nu;
78
+ int busy;
79
+ } integration_qaws_table;
80
+
81
+ typedef struct {
82
+ gsl_integration_qaws_table *table;
83
+ integration_qaws_table *owner;
84
+ } integration_qaws_table_use;
85
+
86
+ void integration_qaws_table_init(VALUE integration_module);
87
+ void integration_qaws_table_use_begin(VALUE table_value, double alpha,
88
+ double beta, int mu, int nu,
89
+ integration_qaws_table_use *use);
90
+ VALUE integration_qaws_table_use_end(VALUE data);
18
91
 
19
92
  typedef struct {
20
93
  VALUE block;
@@ -24,3 +24,33 @@ void integration_options_parse(VALUE options, integration_options *parsed,
24
24
  parsed->limit =
25
25
  limit_value == Qundef ? default_limit : NUM2SIZET(limit_value);
26
26
  }
27
+
28
+ void integration_options_parse_with_workspace(
29
+ VALUE options, integration_options *parsed, double default_epsabs,
30
+ double default_epsrel, size_t default_limit, VALUE *workspace_value,
31
+ int *limit_given) {
32
+ VALUE epsabs_value = Qundef;
33
+ VALUE epsrel_value = Qundef;
34
+ VALUE limit_value = Qundef;
35
+ ID keywords[] = {rb_intern("epsabs"), rb_intern("epsrel"), rb_intern("limit"),
36
+ rb_intern("workspace")};
37
+ VALUE keyword_values[4];
38
+
39
+ if (!NIL_P(options)) {
40
+ rb_get_kwargs(options, keywords, 0, 4, keyword_values);
41
+ epsabs_value = keyword_values[0];
42
+ epsrel_value = keyword_values[1];
43
+ limit_value = keyword_values[2];
44
+ *workspace_value = keyword_values[3];
45
+ } else {
46
+ *workspace_value = Qundef;
47
+ }
48
+
49
+ parsed->epsabs =
50
+ epsabs_value == Qundef ? default_epsabs : NUM2DBL(epsabs_value);
51
+ parsed->epsrel =
52
+ epsrel_value == Qundef ? default_epsrel : NUM2DBL(epsrel_value);
53
+ parsed->limit =
54
+ limit_value == Qundef ? default_limit : NUM2SIZET(limit_value);
55
+ *limit_given = limit_value != Qundef;
56
+ }
@@ -32,59 +32,70 @@ VALUE integration_qag(int argc, VALUE *argv, VALUE self) {
32
32
  VALUE epsrel_value = Qundef;
33
33
  VALUE limit_value = Qundef;
34
34
  VALUE key_value = Qundef;
35
+ VALUE workspace_value = Qundef;
35
36
  ID keywords[] = {rb_intern("epsabs"), rb_intern("epsrel"), rb_intern("limit"),
36
- rb_intern("key")};
37
- VALUE keyword_values[4];
37
+ rb_intern("key"), rb_intern("workspace")};
38
+ VALUE keyword_values[5];
39
+ double a_double;
40
+ double b_double;
38
41
  double epsabs;
39
42
  double epsrel;
40
43
  size_t limit;
44
+ int limit_given;
41
45
  int key;
42
- gsl_integration_workspace *workspace;
43
46
  gsl_function function;
44
47
  integration_callback_params params;
45
48
  integration_qag_params qag_params;
49
+ integration_workspace_use workspace_use;
46
50
  double result;
47
51
  double error;
52
+ int state = 0;
48
53
 
49
54
  (void)self;
50
55
 
51
56
  rb_scan_args(argc, argv, "2:", &a, &b, &options);
52
57
 
53
58
  if (!NIL_P(options)) {
54
- rb_get_kwargs(options, keywords, 0, 4, keyword_values);
59
+ rb_get_kwargs(options, keywords, 0, 5, keyword_values);
55
60
  epsabs_value = keyword_values[0];
56
61
  epsrel_value = keyword_values[1];
57
62
  limit_value = keyword_values[2];
58
63
  key_value = keyword_values[3];
64
+ workspace_value = keyword_values[4];
59
65
  }
60
66
 
67
+ a_double = NUM2DBL(a);
68
+ b_double = NUM2DBL(b);
61
69
  epsabs = epsabs_value == Qundef ? 0.0 : NUM2DBL(epsabs_value);
62
70
  epsrel = epsrel_value == Qundef ? 1e-8 : NUM2DBL(epsrel_value);
63
71
  limit = limit_value == Qundef ? 1000 : NUM2SIZET(limit_value);
72
+ limit_given = limit_value != Qundef;
64
73
  key = key_value == Qundef ? GSL_INTEG_GAUSS61 : NUM2INT(key_value);
65
74
 
66
75
  integration_function_init(&function, &params);
67
76
 
68
- workspace = gsl_integration_workspace_alloc(limit);
77
+ integration_workspace_use_begin(workspace_value, &limit, limit_given,
78
+ &workspace_use);
69
79
 
70
80
  qag_params.function = &function;
71
- qag_params.a = NUM2DBL(a);
72
- qag_params.b = NUM2DBL(b);
81
+ qag_params.a = a_double;
82
+ qag_params.b = b_double;
73
83
  qag_params.epsabs = epsabs;
74
84
  qag_params.epsrel = epsrel;
75
85
  qag_params.limit = limit;
76
86
  qag_params.key = key;
77
- qag_params.workspace = workspace;
87
+ qag_params.workspace = workspace_use.workspace;
78
88
  qag_params.result = &result;
79
89
  qag_params.error = &error;
80
90
 
81
- rb_ensure(integration_run_qag, (VALUE)&qag_params, integration_free_workspace,
82
- (VALUE)workspace);
91
+ rb_protect(integration_run_qag, (VALUE)&qag_params, &state);
92
+ integration_workspace_use_end((VALUE)&workspace_use);
83
93
 
94
+ RB_GC_GUARD(workspace_value);
84
95
  RB_GC_GUARD(params.block);
85
96
  RB_GC_GUARD(params.exception);
86
97
 
87
- integration_finish_callback(&params, 0);
98
+ integration_finish_callback(&params, state);
88
99
 
89
100
  return rb_ary_new_from_args(2, DBL2NUM(result), DBL2NUM(error));
90
101
  }
@@ -20,23 +20,18 @@ static VALUE integration_run_qagi(VALUE data) {
20
20
  return Qnil;
21
21
  }
22
22
 
23
- static VALUE integration_run_qagi_with_workspace(VALUE data) {
24
- integration_qagi_params *params = (integration_qagi_params *)data;
25
-
26
- return rb_ensure(integration_run_qagi, data, integration_free_workspace,
27
- (VALUE)params->workspace);
28
- }
29
-
30
23
  VALUE integration_qagi(int argc, VALUE *argv, VALUE self) {
31
24
  VALUE options;
25
+ VALUE workspace_value;
32
26
  integration_options parsed_options;
33
27
  double epsabs;
34
28
  double epsrel;
35
29
  size_t limit;
36
- gsl_integration_workspace *workspace;
30
+ int limit_given;
37
31
  gsl_function function;
38
32
  integration_callback_params params;
39
33
  integration_qagi_params qagi_params;
34
+ integration_workspace_use workspace_use;
40
35
  double result;
41
36
  double error;
42
37
  int state = 0;
@@ -44,8 +39,9 @@ VALUE integration_qagi(int argc, VALUE *argv, VALUE self) {
44
39
  (void)self;
45
40
 
46
41
  rb_scan_args(argc, argv, "0:", &options);
47
-
48
- integration_options_parse(options, &parsed_options, 0.0, 1e-8, 1000);
42
+ integration_options_parse_with_workspace(options, &parsed_options, 0.0, 1e-8,
43
+ 1000, &workspace_value,
44
+ &limit_given);
49
45
 
50
46
  epsabs = parsed_options.epsabs;
51
47
  epsrel = parsed_options.epsrel;
@@ -53,18 +49,21 @@ VALUE integration_qagi(int argc, VALUE *argv, VALUE self) {
53
49
 
54
50
  integration_function_init(&function, &params);
55
51
 
56
- workspace = gsl_integration_workspace_alloc(limit);
52
+ integration_workspace_use_begin(workspace_value, &limit, limit_given,
53
+ &workspace_use);
57
54
 
58
55
  qagi_params.function = &function;
59
56
  qagi_params.epsabs = epsabs;
60
57
  qagi_params.epsrel = epsrel;
61
58
  qagi_params.limit = limit;
62
- qagi_params.workspace = workspace;
59
+ qagi_params.workspace = workspace_use.workspace;
63
60
  qagi_params.result = &result;
64
61
  qagi_params.error = &error;
65
62
 
66
- rb_protect(integration_run_qagi_with_workspace, (VALUE)&qagi_params, &state);
63
+ rb_protect(integration_run_qagi, (VALUE)&qagi_params, &state);
64
+ integration_workspace_use_end((VALUE)&workspace_use);
67
65
 
66
+ RB_GC_GUARD(workspace_value);
68
67
  RB_GC_GUARD(params.block);
69
68
  RB_GC_GUARD(params.exception);
70
69
 
@@ -21,24 +21,20 @@ static VALUE integration_run_qagil(VALUE data) {
21
21
  return Qnil;
22
22
  }
23
23
 
24
- static VALUE integration_run_qagil_with_workspace(VALUE data) {
25
- integration_qagil_params *params = (integration_qagil_params *)data;
26
-
27
- return rb_ensure(integration_run_qagil, data, integration_free_workspace,
28
- (VALUE)params->workspace);
29
- }
30
-
31
24
  VALUE integration_qagil(int argc, VALUE *argv, VALUE self) {
32
25
  VALUE b;
33
26
  VALUE options;
27
+ VALUE workspace_value;
34
28
  integration_options parsed_options;
29
+ double b_double;
35
30
  double epsabs;
36
31
  double epsrel;
37
32
  size_t limit;
38
- gsl_integration_workspace *workspace;
33
+ int limit_given;
39
34
  gsl_function function;
40
35
  integration_callback_params params;
41
36
  integration_qagil_params qagil_params;
37
+ integration_workspace_use workspace_use;
42
38
  double result;
43
39
  double error;
44
40
  int state = 0;
@@ -46,29 +42,33 @@ VALUE integration_qagil(int argc, VALUE *argv, VALUE self) {
46
42
  (void)self;
47
43
 
48
44
  rb_scan_args(argc, argv, "1:", &b, &options);
45
+ integration_options_parse_with_workspace(options, &parsed_options, 0.0, 1e-8,
46
+ 1000, &workspace_value,
47
+ &limit_given);
49
48
 
50
- integration_options_parse(options, &parsed_options, 0.0, 1e-8, 1000);
51
-
49
+ b_double = NUM2DBL(b);
52
50
  epsabs = parsed_options.epsabs;
53
51
  epsrel = parsed_options.epsrel;
54
52
  limit = parsed_options.limit;
55
53
 
56
54
  integration_function_init(&function, &params);
57
55
 
58
- workspace = gsl_integration_workspace_alloc(limit);
56
+ integration_workspace_use_begin(workspace_value, &limit, limit_given,
57
+ &workspace_use);
59
58
 
60
59
  qagil_params.function = &function;
61
- qagil_params.b = NUM2DBL(b);
60
+ qagil_params.b = b_double;
62
61
  qagil_params.epsabs = epsabs;
63
62
  qagil_params.epsrel = epsrel;
64
63
  qagil_params.limit = limit;
65
- qagil_params.workspace = workspace;
64
+ qagil_params.workspace = workspace_use.workspace;
66
65
  qagil_params.result = &result;
67
66
  qagil_params.error = &error;
68
67
 
69
- rb_protect(integration_run_qagil_with_workspace, (VALUE)&qagil_params,
70
- &state);
68
+ rb_protect(integration_run_qagil, (VALUE)&qagil_params, &state);
69
+ integration_workspace_use_end((VALUE)&workspace_use);
71
70
 
71
+ RB_GC_GUARD(workspace_value);
72
72
  RB_GC_GUARD(params.block);
73
73
  RB_GC_GUARD(params.exception);
74
74