numo-gsl-integration 0.5.0 → 0.7.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: f9c31af337e5875721df99bb42c6cb33654f7d2306071b727ba6ba4c4660c437
4
+ data.tar.gz: 3838149ad68f08fb298c0f0dc2a6a05137e25654b0fcefecc005d7bc532bbcd3
5
5
  SHA512:
6
- metadata.gz: 06216b69ff868309cc71941d383db61db1cea770b79b48a606470394229d3fc6ea3107ceb083f4b698d6911f30f31ec6c7599a27c3a7e15c8d3cb6ce11c0975a
7
- data.tar.gz: 13e9ae3991bbd3079a78eb58ee6165edde4c4c3f80a76f8fa4323d4e388c3c73bba95ba6d4115976f6682f8301805938f722a671b4e481627ad21f5981e95aa4
6
+ metadata.gz: 032e62f8e113ee88510988b2c408e76bb0017e6a74aa1c67b7fa9b9451a903596fdae856c976c33ea08e119d06d5afb603dbd902d3ea9bc6655e706b24a4a700
7
+ data.tar.gz: 65b044ecbd38db9a7de27b959a7b0790d35189aa6a762a01b42675b97819c33755d0b85a04e3a432ee8a1fb9ca6f0c00ea935c55b6aa6e9aae63dee707b71410
data/CHANGELOG.md CHANGED
@@ -2,6 +2,37 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.7.0] - 2026-11-08
6
+
7
+ ### Added in 0.7.0
8
+
9
+ - Added `Numo::GSL::Integration::CQUADWorkspace` and an optional `workspace:` keyword for reusing CQUAD workspace allocations across calls.
10
+ - Added `Numo::GSL::Integration::RombergWorkspace` and an optional `workspace:` keyword for reusing Romberg workspace allocations across calls.
11
+ - Added `Numo::GSL::Integration::GLFixedTable` and an optional `table:` keyword for reusing fixed-order Gauss-Legendre tables across calls.
12
+
13
+ ### Changed in 0.7.0
14
+
15
+ - Validated that the CQUAD `limit` is at least `3`. When a reusable CQUAD workspace and an explicit `limit:` are supplied together, the limit must equal the workspace capacity.
16
+ - Validated that the Romberg `limit` is between `1` and `30`. When a reusable Romberg workspace and an explicit `limit:` are supplied together, the limit must equal the workspace capacity.
17
+ - When a reusable GLFIXED table is supplied, an omitted `order:` uses the table order and an explicit order must match it.
18
+
19
+ ## [0.6.0] - 2026-11-08
20
+
21
+ ### Added in 0.6.0
22
+
23
+ - Added `Numo::GSL::Integration::QAWFTable` for reusing Fourier-cycle QAWO moment tables with a fixed angular frequency, sine or cosine weight, and level count.
24
+
25
+ ### Changed in 0.6.0
26
+
27
+ - 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.
28
+ - Added an optional `table:` keyword to `qawf`. Calls that omit it continue to allocate and release a temporary QAWO table.
29
+
30
+ ### Fixed in 0.6.0
31
+
32
+ - Validated that the `qawf` sine or cosine selector is an integer equal to `SINE` or `COSINE`.
33
+ - Prevented open `QAWOTable` and `QAWFTable` objects from being reinitialized and leaking their native tables.
34
+ - Avoided intermediate overflow when calculating QAWF cycle lengths and rejected angular frequencies that produce a non-finite cycle length.
35
+
5
36
  ## [0.5.0] - 2026-11-07
6
37
 
7
38
  ### 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 `Workspace` objects are supported by `qag`, `qags`, `qagp`, `qagi`, `qagiu`, `qagil`, `qawc`, `qaws`, `qawo`, and `qawf`. CQUAD and Romberg use their routine-specific `CQUADWorkspace` and `RombergWorkspace` classes. 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 integrations, reusable tables can retain precomputed data. `Numo::GSL::Integration::GLFixedTable` is used by `glfixed` with a fixed order and can be reused across integration intervals. `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
 
@@ -8,6 +8,6 @@ file:line: reason (GSL error code)
8
8
  ```
9
9
 
10
10
  `numo-gsl-integration` does not convert integration return statuses into separate exceptions.
11
- The workspaces used by QAG, QAGS, QAGP, QAGI, QAGIU, QAGIL, QAWC, QAWS, QAWO, QAWF, CQUAD, and Romberg are released when a call completes normally or raises an exception. QAWS, QAWO, and QAWF also release their weight tables, GLFIXED releases its Gauss–Legendre table, and QAGP releases its temporary copy of the point array.
11
+ Temporary workspaces and tables used by QAG, QAGS, QAGP, QAGI, QAGIU, QAGIL, QAWC, QAWS, QAWO, QAWF, CQUAD, Romberg, and GLFIXED are released when a call completes normally or raises an exception. Reusable `Workspace`, `CQUADWorkspace`, `RombergWorkspace`, `GLFixedTable`, `QAWSTable`, `QAWOTable`, and `QAWFTable` objects supplied by the caller remain open and are made available for the next call. QAGP releases its temporary copy of the point array.
12
12
  Invalid argument types raise `TypeError`. Invalid values, counts, or ordering raise `ArgumentError`. Exceptions raised by the integrand are propagated unchanged.
13
13
  The GSL error handler is shared by the entire process. Loading this gem replaces the previously registered handler. If another GSL binding is loaded afterward, that binding may replace this gem's handler in turn.
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`. CQUAD and Romberg use the separate `Numo::GSL::Integration::CQUADWorkspace` and `Numo::GSL::Integration::RombergWorkspace` classes described in their routine sections. 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
 
@@ -400,6 +437,24 @@ end
400
437
 
401
438
  The example integral has an endpoint singularity and an exact value of `-4.0`. The `epsabs` and `epsrel` keyword arguments control the requested error limits. The `limit` keyword controls the number of intervals retained in the CQUAD workspace and defaults to `100`; GSL requires at least `3`.
402
439
 
440
+ To reuse the CQUAD allocation across calls, pass a `Numo::GSL::Integration::CQUADWorkspace`:
441
+
442
+ ```ruby
443
+ Numo::GSL::Integration::CQUADWorkspace.open(limit: 100) do |workspace|
444
+ result, error, evaluations = Numo::GSL::Integration.cquad(
445
+ 0.0,
446
+ 1.0,
447
+ workspace: workspace
448
+ ) do |x|
449
+ Math.log(x) / Math.sqrt(x)
450
+ end
451
+ end
452
+ ```
453
+
454
+ `CQUADWorkspace#limit` returns the allocated capacity, and `CQUADWorkspace#closed?` reports whether the workspace has been closed. Use `.open` for block-scoped cleanup, or call `close` explicitly. Calling `close` more than once is allowed, and Ruby's garbage collector releases an unclosed workspace.
455
+ When `workspace:` is omitted, `cquad` allocates and releases a temporary workspace for the call. When a reusable workspace is supplied and `limit:` is omitted, the call uses the workspace capacity. Because GSL receives the capacity when the CQUAD workspace is allocated rather than when the integration runs, an explicitly supplied `limit:` must equal the reusable workspace capacity.
456
+ A CQUAD workspace cannot be used recursively or by overlapping calls, and it cannot be closed while in use. Passing a closed workspace raises `RuntimeError`; passing another object, including `nil`, raises `TypeError`.
457
+
403
458
  ## Romberg integration
404
459
 
405
460
  GSL [Romberg integration](https://www.gnu.org/software/gsl/doc/html/integration.html#romberg-integration) applies Richardson extrapolation to successive trapezoidal approximations. It is intended for smooth functions over a finite interval. The return value contains the integral and the number of integrand evaluations.
@@ -414,6 +469,26 @@ The example integral has an exact value of `2.0`. The `epsabs` and `epsrel` keyw
414
469
 
415
470
  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.
416
471
 
472
+ To reuse the Romberg allocation across calls, pass a `Numo::GSL::Integration::RombergWorkspace`:
473
+
474
+ ```ruby
475
+ Numo::GSL::Integration::RombergWorkspace.open(limit: 20) do |workspace|
476
+ result, evaluations = Numo::GSL::Integration.romberg(
477
+ 0.0,
478
+ Math::PI,
479
+ workspace: workspace
480
+ ) do |x|
481
+ Math.sin(x)
482
+ end
483
+ end
484
+ ```
485
+
486
+ `RombergWorkspace#limit` returns the allocated capacity, which must be between `1` and `30`. `RombergWorkspace#closed?` reports whether the workspace has been closed. Use `.open` for block-scoped cleanup, or call `close` explicitly. Calling `close` more than once is allowed, and Ruby's garbage collector releases an unclosed workspace.
487
+
488
+ When `workspace:` is omitted, `romberg` allocates and releases a temporary workspace for the call. When a reusable workspace is supplied and `limit:` is omitted, the call uses the workspace capacity. Because GSL receives the capacity when the Romberg workspace is allocated rather than when the integration runs, an explicitly supplied `limit:` must equal the reusable workspace capacity.
489
+
490
+ A Romberg workspace cannot be used recursively or by overlapping calls, and it cannot be closed while in use. Passing a closed workspace raises `RuntimeError`; passing another object, including `nil`, raises `TypeError`.
491
+
417
492
  ## GLFIXED integration
418
493
 
419
494
  GSL [fixed-order Gauss-Legendre integration](https://www.gnu.org/software/gsl/doc/html/integration.html#gauss-legendre-integration) provides fast integration of smooth functions when an appropriate polynomial order is known. An `n`-point rule integrates polynomials of degree `2 * n - 1` or less exactly, subject to floating-point precision.
@@ -432,6 +507,29 @@ The example integral has an exact value of `1.0 / 5.0`. The `order` keyword cont
432
507
 
433
508
  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.
434
509
 
510
+ To reuse the Gauss-Legendre table across calls, create a `Numo::GSL::Integration::GLFixedTable` with a fixed order and pass it with `table:`:
511
+
512
+ ```ruby
513
+ Numo::GSL::Integration::GLFixedTable.open(order: 10) do |table|
514
+ first_result = Numo::GSL::Integration.glfixed(
515
+ 0.0,
516
+ Math::PI,
517
+ table: table
518
+ ) { |x| Math.sin(x) }
519
+ second_result = Numo::GSL::Integration.glfixed(
520
+ 0.0,
521
+ Math::PI / 2.0,
522
+ table: table
523
+ ) { |x| Math.sin(x) }
524
+ end
525
+ ```
526
+
527
+ `GLFixedTable#order` returns the fixed quadrature order. The table can be reused across different integration intervals because the interval is supplied to each `glfixed` call. Use `.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.
528
+
529
+ When `table:` is omitted, `glfixed` allocates and releases a temporary table for the call. If both `order:` and `table:` are omitted, the order defaults to `10`. When a table is supplied and `order:` is omitted, the call uses the table order. An explicitly supplied `order:` must equal the table order.
530
+
531
+ A GLFixed table cannot be used recursively or by overlapping calls, and it cannot be closed while in use. Passing a closed table raises `RuntimeError`; passing another object, including `nil`, raises `TypeError`.
532
+
435
533
  ## Fixed point quadratures
436
534
 
437
535
  GSL [fixed point quadratures](https://www.gnu.org/software/gsl/doc/html/integration.html#fixed-point-quadratures) provide fixed-order Gaussian quadrature rules for several weight functions. `Numo::GSL::Integration.fixed` takes a quadrature type, the number of points, and the GSL rule parameters:
@@ -7,7 +7,9 @@ typedef struct {
7
7
  double epsabs;
8
8
  double epsrel;
9
9
  size_t limit;
10
- gsl_integration_cquad_workspace *workspace;
10
+ int limit_given;
11
+ VALUE workspace_value;
12
+ integration_cquad_workspace_use workspace_use;
11
13
  double *result;
12
14
  double *error;
13
15
  size_t *evaluations;
@@ -16,9 +18,13 @@ typedef struct {
16
18
  static VALUE integration_run_cquad(VALUE data) {
17
19
  integration_cquad_params *params = (integration_cquad_params *)data;
18
20
 
19
- (void)gsl_integration_cquad(
20
- params->function, params->a, params->b, params->epsabs, params->epsrel,
21
- params->workspace, params->result, params->error, params->evaluations);
21
+ integration_cquad_workspace_use_begin(params->workspace_value, &params->limit,
22
+ params->limit_given,
23
+ &params->workspace_use);
24
+ (void)gsl_integration_cquad(params->function, params->a, params->b,
25
+ params->epsabs, params->epsrel,
26
+ params->workspace_use.workspace, params->result,
27
+ params->error, params->evaluations);
22
28
 
23
29
  return Qnil;
24
30
  }
@@ -27,21 +33,28 @@ static VALUE integration_run_cquad_with_workspace(VALUE data) {
27
33
  integration_cquad_params *params = (integration_cquad_params *)data;
28
34
 
29
35
  return rb_ensure(integration_run_cquad, data,
30
- integration_free_cquad_workspace, (VALUE)params->workspace);
36
+ integration_cquad_workspace_use_end,
37
+ (VALUE)&params->workspace_use);
31
38
  }
32
39
 
33
40
  VALUE integration_cquad(int argc, VALUE *argv, VALUE self) {
34
41
  VALUE a;
35
42
  VALUE b;
36
43
  VALUE options;
37
- integration_options parsed_options;
44
+ VALUE epsabs_value = Qundef;
45
+ VALUE epsrel_value = Qundef;
46
+ VALUE limit_value = Qundef;
47
+ VALUE workspace_value = Qundef;
48
+ ID keywords[] = {rb_intern("epsabs"), rb_intern("epsrel"), rb_intern("limit"),
49
+ rb_intern("workspace")};
50
+ VALUE keyword_values[4];
38
51
  double epsabs;
39
52
  double epsrel;
40
- size_t limit;
41
- gsl_integration_cquad_workspace *workspace;
53
+ size_t limit = 100;
54
+ int limit_given;
42
55
  gsl_function function;
43
56
  integration_callback_params params;
44
- integration_cquad_params cquad_params;
57
+ integration_cquad_params cquad_params = {0};
45
58
  double result;
46
59
  double error;
47
60
  size_t evaluations;
@@ -50,24 +63,37 @@ VALUE integration_cquad(int argc, VALUE *argv, VALUE self) {
50
63
  (void)self;
51
64
 
52
65
  rb_scan_args(argc, argv, "2:", &a, &b, &options);
53
-
54
- integration_options_parse(options, &parsed_options, 0.0, 1e-8, 100);
55
-
56
- epsabs = parsed_options.epsabs;
57
- epsrel = parsed_options.epsrel;
58
- limit = parsed_options.limit;
66
+ if (!NIL_P(options)) {
67
+ rb_get_kwargs(options, keywords, 0, 4, keyword_values);
68
+ epsabs_value = keyword_values[0];
69
+ epsrel_value = keyword_values[1];
70
+ limit_value = keyword_values[2];
71
+ workspace_value = keyword_values[3];
72
+ }
73
+
74
+ epsabs = epsabs_value == Qundef ? 0.0 : NUM2DBL(epsabs_value);
75
+ epsrel = epsrel_value == Qundef ? 1e-8 : NUM2DBL(epsrel_value);
76
+ limit_given = limit_value != Qundef;
77
+ if (limit_given) {
78
+ if (!RB_INTEGER_TYPE_P(limit_value)) {
79
+ rb_raise(rb_eTypeError, "limit must be an Integer");
80
+ }
81
+ if (RTEST(rb_funcall(limit_value, rb_intern("<"), 1, INT2FIX(3)))) {
82
+ rb_raise(rb_eArgError, "limit must be at least 3");
83
+ }
84
+ limit = NUM2SIZET(limit_value);
85
+ }
59
86
 
60
87
  integration_function_init(&function, &params);
61
88
 
62
- workspace = gsl_integration_cquad_workspace_alloc(limit);
63
-
64
89
  cquad_params.function = &function;
65
90
  cquad_params.a = NUM2DBL(a);
66
91
  cquad_params.b = NUM2DBL(b);
67
92
  cquad_params.epsabs = epsabs;
68
93
  cquad_params.epsrel = epsrel;
69
94
  cquad_params.limit = limit;
70
- cquad_params.workspace = workspace;
95
+ cquad_params.limit_given = limit_given;
96
+ cquad_params.workspace_value = workspace_value;
71
97
  cquad_params.result = &result;
72
98
  cquad_params.error = &error;
73
99
  cquad_params.evaluations = &evaluations;
@@ -77,6 +103,7 @@ VALUE integration_cquad(int argc, VALUE *argv, VALUE self) {
77
103
 
78
104
  RB_GC_GUARD(params.block);
79
105
  RB_GC_GUARD(params.exception);
106
+ RB_GC_GUARD(workspace_value);
80
107
 
81
108
  integration_finish_callback(&params, state);
82
109
 
@@ -0,0 +1,214 @@
1
+ #include "integration_internal.h"
2
+
3
+ static VALUE integration_cCQUADWorkspace;
4
+
5
+ static void integration_cquad_workspace_free(void *data) {
6
+ integration_cquad_workspace *workspace = data;
7
+
8
+ if (workspace->workspace != NULL) {
9
+ gsl_integration_cquad_workspace_free(workspace->workspace);
10
+ }
11
+
12
+ xfree(workspace);
13
+ }
14
+
15
+ static size_t integration_cquad_workspace_memsize(const void *data) {
16
+ return data == NULL ? 0 : sizeof(integration_cquad_workspace);
17
+ }
18
+
19
+ static const rb_data_type_t integration_cquad_workspace_type = {
20
+ .wrap_struct_name = "Numo::GSL::Integration::CQUADWorkspace",
21
+ .function =
22
+ {
23
+ .dmark = NULL,
24
+ .dfree = integration_cquad_workspace_free,
25
+ .dsize = integration_cquad_workspace_memsize,
26
+ },
27
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
28
+ };
29
+
30
+ static VALUE integration_cquad_workspace_allocate(VALUE klass) {
31
+ integration_cquad_workspace *workspace;
32
+ VALUE object =
33
+ TypedData_Make_Struct(klass, integration_cquad_workspace,
34
+ &integration_cquad_workspace_type, workspace);
35
+
36
+ workspace->workspace = NULL;
37
+ workspace->limit = 0;
38
+ workspace->busy = 0;
39
+
40
+ return object;
41
+ }
42
+
43
+ static VALUE integration_cquad_workspace_initialize(int argc, VALUE *argv,
44
+ VALUE self) {
45
+ VALUE options;
46
+ VALUE limit_value;
47
+ ID keywords[] = {rb_intern("limit")};
48
+ VALUE keyword_values[1];
49
+ size_t limit;
50
+ gsl_integration_cquad_workspace *native_workspace;
51
+ integration_cquad_workspace *workspace;
52
+
53
+ rb_scan_args(argc, argv, "0:", &options);
54
+ if (NIL_P(options)) {
55
+ rb_raise(rb_eArgError, "missing keyword: :limit");
56
+ }
57
+
58
+ rb_get_kwargs(options, keywords, 1, 0, keyword_values);
59
+ limit_value = keyword_values[0];
60
+ if (!RB_INTEGER_TYPE_P(limit_value)) {
61
+ rb_raise(rb_eTypeError, "limit must be an Integer");
62
+ }
63
+ if (RTEST(rb_funcall(limit_value, rb_intern("<"), 1, INT2FIX(3)))) {
64
+ rb_raise(rb_eArgError, "limit must be at least 3");
65
+ }
66
+
67
+ TypedData_Get_Struct(self, integration_cquad_workspace,
68
+ &integration_cquad_workspace_type, workspace);
69
+ if (workspace->workspace != NULL) {
70
+ rb_raise(rb_eRuntimeError, "workspace is already initialized");
71
+ }
72
+
73
+ limit = NUM2SIZET(limit_value);
74
+ native_workspace = gsl_integration_cquad_workspace_alloc(limit);
75
+ if (native_workspace == NULL) {
76
+ rb_memerror();
77
+ }
78
+
79
+ workspace->workspace = native_workspace;
80
+ workspace->limit = limit;
81
+
82
+ return self;
83
+ }
84
+
85
+ static VALUE integration_cquad_workspace_limit(VALUE self) {
86
+ integration_cquad_workspace *workspace;
87
+
88
+ TypedData_Get_Struct(self, integration_cquad_workspace,
89
+ &integration_cquad_workspace_type, workspace);
90
+
91
+ return SIZET2NUM(workspace->limit);
92
+ }
93
+
94
+ static VALUE integration_cquad_workspace_closed_p(VALUE self) {
95
+ integration_cquad_workspace *workspace;
96
+
97
+ TypedData_Get_Struct(self, integration_cquad_workspace,
98
+ &integration_cquad_workspace_type, workspace);
99
+
100
+ return workspace->workspace == NULL ? Qtrue : Qfalse;
101
+ }
102
+
103
+ static VALUE integration_cquad_workspace_close(VALUE self) {
104
+ integration_cquad_workspace *workspace;
105
+
106
+ TypedData_Get_Struct(self, integration_cquad_workspace,
107
+ &integration_cquad_workspace_type, workspace);
108
+ if (workspace->busy) {
109
+ rb_raise(rb_eRuntimeError, "workspace is already in use");
110
+ }
111
+ if (workspace->workspace != NULL) {
112
+ gsl_integration_cquad_workspace_free(workspace->workspace);
113
+ workspace->workspace = NULL;
114
+ }
115
+
116
+ return Qnil;
117
+ }
118
+
119
+ static VALUE integration_cquad_workspace_yield(VALUE workspace) {
120
+ return rb_yield(workspace);
121
+ }
122
+
123
+ static VALUE integration_cquad_workspace_open(int argc, VALUE *argv,
124
+ VALUE klass) {
125
+ VALUE workspace;
126
+
127
+ workspace =
128
+ rb_funcallv_kw(klass, rb_intern("new"), argc, argv, RB_PASS_KEYWORDS);
129
+
130
+ return rb_ensure(integration_cquad_workspace_yield, workspace,
131
+ integration_cquad_workspace_close, workspace);
132
+ }
133
+
134
+ static integration_cquad_workspace *
135
+ integration_cquad_workspace_get(VALUE workspace_value) {
136
+ integration_cquad_workspace *workspace;
137
+
138
+ if (!rb_obj_is_kind_of(workspace_value, integration_cCQUADWorkspace)) {
139
+ rb_raise(rb_eTypeError,
140
+ "workspace must be a Numo::GSL::Integration::CQUADWorkspace");
141
+ }
142
+
143
+ TypedData_Get_Struct(workspace_value, integration_cquad_workspace,
144
+ &integration_cquad_workspace_type, workspace);
145
+ if (workspace->workspace == NULL) {
146
+ rb_raise(rb_eRuntimeError, "workspace is closed");
147
+ }
148
+ if (workspace->busy) {
149
+ rb_raise(rb_eRuntimeError, "workspace is already in use");
150
+ }
151
+
152
+ return workspace;
153
+ }
154
+
155
+ void integration_cquad_workspace_use_begin(
156
+ VALUE workspace_value, size_t *limit, int limit_given,
157
+ integration_cquad_workspace_use *use) {
158
+ integration_cquad_workspace *workspace;
159
+
160
+ use->workspace = NULL;
161
+ use->owner = NULL;
162
+ if (workspace_value == Qundef) {
163
+ use->workspace = gsl_integration_cquad_workspace_alloc(*limit);
164
+ if (use->workspace == NULL) {
165
+ rb_memerror();
166
+ }
167
+ return;
168
+ }
169
+
170
+ workspace = integration_cquad_workspace_get(workspace_value);
171
+ if (limit_given && *limit != workspace->limit) {
172
+ rb_raise(rb_eArgError, "limit must match workspace capacity");
173
+ }
174
+ if (!limit_given) {
175
+ *limit = workspace->limit;
176
+ }
177
+
178
+ workspace->busy = 1;
179
+ use->workspace = workspace->workspace;
180
+ use->owner = workspace;
181
+ }
182
+
183
+ VALUE integration_cquad_workspace_use_end(VALUE data) {
184
+ integration_cquad_workspace_use *use =
185
+ (integration_cquad_workspace_use *)data;
186
+
187
+ if (use->owner != NULL) {
188
+ use->owner->busy = 0;
189
+ } else if (use->workspace != NULL) {
190
+ gsl_integration_cquad_workspace_free(use->workspace);
191
+ }
192
+
193
+ use->workspace = NULL;
194
+ use->owner = NULL;
195
+
196
+ return Qnil;
197
+ }
198
+
199
+ void integration_cquad_workspace_init(VALUE integration_module) {
200
+ integration_cCQUADWorkspace =
201
+ rb_define_class_under(integration_module, "CQUADWorkspace", rb_cObject);
202
+ rb_define_alloc_func(integration_cCQUADWorkspace,
203
+ integration_cquad_workspace_allocate);
204
+ rb_define_method(integration_cCQUADWorkspace, "initialize",
205
+ integration_cquad_workspace_initialize, -1);
206
+ rb_define_method(integration_cCQUADWorkspace, "limit",
207
+ integration_cquad_workspace_limit, 0);
208
+ rb_define_method(integration_cCQUADWorkspace, "closed?",
209
+ integration_cquad_workspace_closed_p, 0);
210
+ rb_define_method(integration_cCQUADWorkspace, "close",
211
+ integration_cquad_workspace_close, 0);
212
+ rb_define_singleton_method(integration_cCQUADWorkspace, "open",
213
+ integration_cquad_workspace_open, -1);
214
+ }