numo-gsl-integration 0.6.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: 01a7be9ee19ecec129a5afe36fc590bb556a733e826703d01188ed696d8fe60a
4
- data.tar.gz: 1ad488db58b98142070b70211442a7bc0bde14b3507f21cff9a215c7b8b81d81
3
+ metadata.gz: f9c31af337e5875721df99bb42c6cb33654f7d2306071b727ba6ba4c4660c437
4
+ data.tar.gz: 3838149ad68f08fb298c0f0dc2a6a05137e25654b0fcefecc005d7bc532bbcd3
5
5
  SHA512:
6
- metadata.gz: a1cd519426b50c54de12f3150738155d8174fa47c3474759599fc886b80fa078cf7aa9621886ff811d4902fd129f75d216ee9d0a987457d7f3341d440e44b6b8
7
- data.tar.gz: e68326e035e187fc84314bd5f5f5ca9cc0c7339d4b367f558351ac434ac47d2a08b0c1f53a809924dd38d7e0d95199404885f2667c6ba5b10a47d53cee129a6f
6
+ metadata.gz: 032e62f8e113ee88510988b2c408e76bb0017e6a74aa1c67b7fa9b9451a903596fdae856c976c33ea08e119d06d5afb603dbd902d3ea9bc6655e706b24a4a700
7
+ data.tar.gz: 65b044ecbd38db9a7de27b959a7b0790d35189aa6a762a01b42675b97819c33755d0b85a04e3a432ee8a1fb9ca6f0c00ea935c55b6aa6e9aae63dee707b71410
data/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
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
+
5
19
  ## [0.6.0] - 2026-11-08
6
20
 
7
21
  ### Added in 0.6.0
data/README.md CHANGED
@@ -57,11 +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`, `qawo`, and `qawf`. 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
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
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.
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.
65
65
 
66
66
  ## Supported routines
67
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`, `qawo`, and `qawf`. 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
 
@@ -437,6 +437,24 @@ end
437
437
 
438
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`.
439
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
+
440
458
  ## Romberg integration
441
459
 
442
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.
@@ -451,6 +469,26 @@ The example integral has an exact value of `2.0`. The `epsabs` and `epsrel` keyw
451
469
 
452
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.
453
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
+
454
492
  ## GLFIXED integration
455
493
 
456
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.
@@ -469,6 +507,29 @@ The example integral has an exact value of `1.0 / 5.0`. The `order` keyword cont
469
507
 
470
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.
471
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
+
472
533
  ## Fixed point quadratures
473
534
 
474
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
+ }
@@ -5,15 +5,19 @@ typedef struct {
5
5
  double a;
6
6
  double b;
7
7
  size_t order;
8
- gsl_integration_glfixed_table *table;
8
+ int order_given;
9
+ VALUE table_value;
10
+ integration_glfixed_table_use table_use;
9
11
  double *result;
10
12
  } integration_glfixed_params;
11
13
 
12
14
  static VALUE integration_run_glfixed(VALUE data) {
13
15
  integration_glfixed_params *params = (integration_glfixed_params *)data;
14
16
 
15
- *(params->result) = gsl_integration_glfixed(params->function, params->a,
16
- params->b, params->table);
17
+ integration_glfixed_table_use_begin(params->table_value, &params->order,
18
+ params->order_given, &params->table_use);
19
+ *(params->result) = gsl_integration_glfixed(
20
+ params->function, params->a, params->b, params->table_use.table);
17
21
 
18
22
  return Qnil;
19
23
  }
@@ -22,7 +26,8 @@ static VALUE integration_run_glfixed_with_table(VALUE data) {
22
26
  integration_glfixed_params *params = (integration_glfixed_params *)data;
23
27
 
24
28
  return rb_ensure(integration_run_glfixed, data,
25
- integration_free_glfixed_table, (VALUE)params->table);
29
+ integration_glfixed_table_use_end,
30
+ (VALUE)&params->table_use);
26
31
  }
27
32
 
28
33
  VALUE integration_glfixed(int argc, VALUE *argv, VALUE self) {
@@ -30,35 +35,34 @@ VALUE integration_glfixed(int argc, VALUE *argv, VALUE self) {
30
35
  VALUE b;
31
36
  VALUE options;
32
37
  VALUE order_value = Qundef;
33
- ID keywords[] = {rb_intern("order")};
34
- VALUE keyword_values[1];
35
- size_t order;
38
+ VALUE table_value = Qundef;
39
+ ID keywords[] = {rb_intern("order"), rb_intern("table")};
40
+ VALUE keyword_values[2];
41
+ size_t order = 10;
42
+ int order_given;
36
43
  gsl_function function;
37
44
  integration_callback_params params;
38
- integration_glfixed_params glfixed_params;
45
+ integration_glfixed_params glfixed_params = {0};
39
46
  double result;
40
47
  int state = 0;
41
48
 
42
49
  (void)self;
43
50
 
44
51
  rb_scan_args(argc, argv, "2:", &a, &b, &options);
45
-
46
52
  if (!NIL_P(options)) {
47
- rb_get_kwargs(options, keywords, 0, 1, keyword_values);
53
+ rb_get_kwargs(options, keywords, 0, 2, keyword_values);
48
54
  order_value = keyword_values[0];
55
+ table_value = keyword_values[1];
49
56
  }
50
57
 
51
- if (order_value == Qundef) {
52
- order = 10;
53
- } else {
58
+ order_given = order_value != Qundef;
59
+ if (order_given) {
54
60
  if (!RB_INTEGER_TYPE_P(order_value)) {
55
61
  rb_raise(rb_eTypeError, "order must be an Integer");
56
62
  }
57
-
58
63
  if (RTEST(rb_funcall(order_value, rb_intern("<="), 1, INT2FIX(0)))) {
59
64
  rb_raise(rb_eArgError, "order must be positive");
60
65
  }
61
-
62
66
  order = NUM2SIZET(order_value);
63
67
  }
64
68
 
@@ -68,9 +72,8 @@ VALUE integration_glfixed(int argc, VALUE *argv, VALUE self) {
68
72
  glfixed_params.a = NUM2DBL(a);
69
73
  glfixed_params.b = NUM2DBL(b);
70
74
  glfixed_params.order = order;
71
-
72
- glfixed_params.table = gsl_integration_glfixed_table_alloc(order);
73
-
75
+ glfixed_params.order_given = order_given;
76
+ glfixed_params.table_value = table_value;
74
77
  glfixed_params.result = &result;
75
78
 
76
79
  rb_protect(integration_run_glfixed_with_table, (VALUE)&glfixed_params,
@@ -78,6 +81,7 @@ VALUE integration_glfixed(int argc, VALUE *argv, VALUE self) {
78
81
 
79
82
  RB_GC_GUARD(params.block);
80
83
  RB_GC_GUARD(params.exception);
84
+ RB_GC_GUARD(table_value);
81
85
 
82
86
  integration_finish_callback(&params, state);
83
87
 
@@ -0,0 +1,211 @@
1
+ #include "integration_internal.h"
2
+
3
+ static VALUE integration_cGLFixedTable;
4
+
5
+ static void integration_glfixed_table_free(void *data) {
6
+ integration_glfixed_table *table = data;
7
+
8
+ if (table->table != NULL) {
9
+ gsl_integration_glfixed_table_free(table->table);
10
+ }
11
+
12
+ xfree(table);
13
+ }
14
+
15
+ static size_t integration_glfixed_table_memsize(const void *data) {
16
+ return data == NULL ? 0 : sizeof(integration_glfixed_table);
17
+ }
18
+
19
+ static const rb_data_type_t integration_glfixed_table_type = {
20
+ .wrap_struct_name = "Numo::GSL::Integration::GLFixedTable",
21
+ .function =
22
+ {
23
+ .dmark = NULL,
24
+ .dfree = integration_glfixed_table_free,
25
+ .dsize = integration_glfixed_table_memsize,
26
+ },
27
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
28
+ };
29
+
30
+ static VALUE integration_glfixed_table_allocate(VALUE klass) {
31
+ integration_glfixed_table *table;
32
+ VALUE object = TypedData_Make_Struct(klass, integration_glfixed_table,
33
+ &integration_glfixed_table_type, table);
34
+
35
+ table->table = NULL;
36
+ table->order = 0;
37
+ table->busy = 0;
38
+
39
+ return object;
40
+ }
41
+
42
+ static VALUE integration_glfixed_table_initialize(int argc, VALUE *argv,
43
+ VALUE self) {
44
+ VALUE options;
45
+ VALUE order_value;
46
+ ID keywords[] = {rb_intern("order")};
47
+ VALUE keyword_values[1];
48
+ size_t order;
49
+ gsl_integration_glfixed_table *native_table;
50
+ integration_glfixed_table *table;
51
+
52
+ rb_scan_args(argc, argv, "0:", &options);
53
+ if (NIL_P(options)) {
54
+ rb_raise(rb_eArgError, "missing keyword: :order");
55
+ }
56
+
57
+ rb_get_kwargs(options, keywords, 1, 0, keyword_values);
58
+ order_value = keyword_values[0];
59
+ if (!RB_INTEGER_TYPE_P(order_value)) {
60
+ rb_raise(rb_eTypeError, "order must be an Integer");
61
+ }
62
+ if (RTEST(rb_funcall(order_value, rb_intern("<="), 1, INT2FIX(0)))) {
63
+ rb_raise(rb_eArgError, "order must be positive");
64
+ }
65
+
66
+ TypedData_Get_Struct(self, integration_glfixed_table,
67
+ &integration_glfixed_table_type, table);
68
+ if (table->table != NULL) {
69
+ rb_raise(rb_eRuntimeError, "table is already initialized");
70
+ }
71
+
72
+ order = NUM2SIZET(order_value);
73
+ native_table = gsl_integration_glfixed_table_alloc(order);
74
+ if (native_table == NULL) {
75
+ rb_memerror();
76
+ }
77
+
78
+ table->table = native_table;
79
+ table->order = order;
80
+
81
+ return self;
82
+ }
83
+
84
+ static VALUE integration_glfixed_table_order(VALUE self) {
85
+ integration_glfixed_table *table;
86
+
87
+ TypedData_Get_Struct(self, integration_glfixed_table,
88
+ &integration_glfixed_table_type, table);
89
+
90
+ return SIZET2NUM(table->order);
91
+ }
92
+
93
+ static VALUE integration_glfixed_table_closed_p(VALUE self) {
94
+ integration_glfixed_table *table;
95
+
96
+ TypedData_Get_Struct(self, integration_glfixed_table,
97
+ &integration_glfixed_table_type, table);
98
+
99
+ return table->table == NULL ? Qtrue : Qfalse;
100
+ }
101
+
102
+ static VALUE integration_glfixed_table_close(VALUE self) {
103
+ integration_glfixed_table *table;
104
+
105
+ TypedData_Get_Struct(self, integration_glfixed_table,
106
+ &integration_glfixed_table_type, table);
107
+ if (table->busy) {
108
+ rb_raise(rb_eRuntimeError, "table is already in use");
109
+ }
110
+ if (table->table != NULL) {
111
+ gsl_integration_glfixed_table_free(table->table);
112
+ table->table = NULL;
113
+ }
114
+
115
+ return Qnil;
116
+ }
117
+
118
+ static VALUE integration_glfixed_table_yield(VALUE table) {
119
+ return rb_yield(table);
120
+ }
121
+
122
+ static VALUE integration_glfixed_table_open(int argc, VALUE *argv,
123
+ VALUE klass) {
124
+ VALUE table;
125
+
126
+ table = rb_funcallv_kw(klass, rb_intern("new"), argc, argv, RB_PASS_KEYWORDS);
127
+
128
+ return rb_ensure(integration_glfixed_table_yield, table,
129
+ integration_glfixed_table_close, table);
130
+ }
131
+
132
+ static integration_glfixed_table *
133
+ integration_glfixed_table_get(VALUE table_value) {
134
+ integration_glfixed_table *table;
135
+
136
+ if (!rb_obj_is_kind_of(table_value, integration_cGLFixedTable)) {
137
+ rb_raise(rb_eTypeError,
138
+ "table must be a Numo::GSL::Integration::GLFixedTable");
139
+ }
140
+
141
+ TypedData_Get_Struct(table_value, integration_glfixed_table,
142
+ &integration_glfixed_table_type, table);
143
+ if (table->table == NULL) {
144
+ rb_raise(rb_eRuntimeError, "table is closed");
145
+ }
146
+ if (table->busy) {
147
+ rb_raise(rb_eRuntimeError, "table is already in use");
148
+ }
149
+
150
+ return table;
151
+ }
152
+
153
+ void integration_glfixed_table_use_begin(VALUE table_value, size_t *order,
154
+ int order_given,
155
+ integration_glfixed_table_use *use) {
156
+ integration_glfixed_table *table;
157
+
158
+ use->table = NULL;
159
+ use->owner = NULL;
160
+ if (table_value == Qundef) {
161
+ use->table = gsl_integration_glfixed_table_alloc(*order);
162
+ if (use->table == NULL) {
163
+ rb_memerror();
164
+ }
165
+ return;
166
+ }
167
+
168
+ table = integration_glfixed_table_get(table_value);
169
+ if (order_given && *order != table->order) {
170
+ rb_raise(rb_eArgError, "order does not match GLFixed table");
171
+ }
172
+ if (!order_given) {
173
+ *order = table->order;
174
+ }
175
+
176
+ table->busy = 1;
177
+ use->table = table->table;
178
+ use->owner = table;
179
+ }
180
+
181
+ VALUE integration_glfixed_table_use_end(VALUE data) {
182
+ integration_glfixed_table_use *use = (integration_glfixed_table_use *)data;
183
+
184
+ if (use->owner != NULL) {
185
+ use->owner->busy = 0;
186
+ } else if (use->table != NULL) {
187
+ gsl_integration_glfixed_table_free(use->table);
188
+ }
189
+
190
+ use->table = NULL;
191
+ use->owner = NULL;
192
+
193
+ return Qnil;
194
+ }
195
+
196
+ void integration_glfixed_table_init(VALUE integration_module) {
197
+ integration_cGLFixedTable =
198
+ rb_define_class_under(integration_module, "GLFixedTable", rb_cObject);
199
+ rb_define_alloc_func(integration_cGLFixedTable,
200
+ integration_glfixed_table_allocate);
201
+ rb_define_method(integration_cGLFixedTable, "initialize",
202
+ integration_glfixed_table_initialize, -1);
203
+ rb_define_method(integration_cGLFixedTable, "order",
204
+ integration_glfixed_table_order, 0);
205
+ rb_define_method(integration_cGLFixedTable, "closed?",
206
+ integration_glfixed_table_closed_p, 0);
207
+ rb_define_method(integration_cGLFixedTable, "close",
208
+ integration_glfixed_table_close, 0);
209
+ rb_define_singleton_method(integration_cGLFixedTable, "open",
210
+ integration_glfixed_table_open, -1);
211
+ }
@@ -22,6 +22,12 @@ void Init_integration(void) {
22
22
  gsl_set_error_handler(&integration_gsl_error_handler);
23
23
 
24
24
  integration_workspace_init(mIntegration);
25
+
26
+ integration_cquad_workspace_init(mIntegration);
27
+
28
+ integration_romberg_workspace_init(mIntegration);
29
+
30
+ integration_glfixed_table_init(mIntegration);
25
31
  integration_qawo_table_init(mIntegration);
26
32
  integration_qawf_table_init(mIntegration);
27
33
  integration_qaws_table_init(mIntegration);
@@ -42,6 +42,48 @@ void integration_workspace_use_begin(VALUE workspace_value, size_t *limit,
42
42
  integration_workspace_use *use);
43
43
  VALUE integration_workspace_use_end(VALUE data);
44
44
 
45
+ typedef struct {
46
+ gsl_integration_cquad_workspace *workspace;
47
+ size_t limit;
48
+ int busy;
49
+ } integration_cquad_workspace;
50
+ typedef struct {
51
+ gsl_integration_cquad_workspace *workspace;
52
+ integration_cquad_workspace *owner;
53
+ } integration_cquad_workspace_use;
54
+ void integration_cquad_workspace_init(VALUE integration_module);
55
+ void integration_cquad_workspace_use_begin(
56
+ VALUE workspace_value, size_t *limit, int limit_given,
57
+ integration_cquad_workspace_use *use);
58
+ VALUE integration_cquad_workspace_use_end(VALUE data);
59
+ typedef struct {
60
+ gsl_integration_romberg_workspace *workspace;
61
+ size_t limit;
62
+ int busy;
63
+ } integration_romberg_workspace;
64
+ typedef struct {
65
+ gsl_integration_romberg_workspace *workspace;
66
+ integration_romberg_workspace *owner;
67
+ } integration_romberg_workspace_use;
68
+ void integration_romberg_workspace_init(VALUE integration_module);
69
+ void integration_romberg_workspace_use_begin(
70
+ VALUE workspace_value, size_t *limit, int limit_given,
71
+ integration_romberg_workspace_use *use);
72
+ VALUE integration_romberg_workspace_use_end(VALUE data);
73
+ typedef struct {
74
+ gsl_integration_glfixed_table *table;
75
+ size_t order;
76
+ int busy;
77
+ } integration_glfixed_table;
78
+ typedef struct {
79
+ gsl_integration_glfixed_table *table;
80
+ integration_glfixed_table *owner;
81
+ } integration_glfixed_table_use;
82
+ void integration_glfixed_table_init(VALUE integration_module);
83
+ void integration_glfixed_table_use_begin(VALUE table_value, size_t *order,
84
+ int order_given,
85
+ integration_glfixed_table_use *use);
86
+ VALUE integration_glfixed_table_use_end(VALUE data);
45
87
  typedef struct {
46
88
  gsl_integration_qawo_table *table;
47
89
  double omega;
@@ -6,7 +6,10 @@ typedef struct {
6
6
  double b;
7
7
  double epsabs;
8
8
  double epsrel;
9
- gsl_integration_romberg_workspace *workspace;
9
+ size_t limit;
10
+ int limit_given;
11
+ VALUE workspace_value;
12
+ integration_romberg_workspace_use workspace_use;
10
13
  double *result;
11
14
  size_t *evaluations;
12
15
  } integration_romberg_params;
@@ -14,9 +17,12 @@ typedef struct {
14
17
  static VALUE integration_run_romberg(VALUE data) {
15
18
  integration_romberg_params *params = (integration_romberg_params *)data;
16
19
 
17
- (void)gsl_integration_romberg(params->function, params->a, params->b,
18
- params->epsabs, params->epsrel, params->result,
19
- params->evaluations, params->workspace);
20
+ integration_romberg_workspace_use_begin(params->workspace_value,
21
+ &params->limit, params->limit_given,
22
+ &params->workspace_use);
23
+ (void)gsl_integration_romberg(
24
+ params->function, params->a, params->b, params->epsabs, params->epsrel,
25
+ params->result, params->evaluations, params->workspace_use.workspace);
20
26
 
21
27
  return Qnil;
22
28
  }
@@ -25,22 +31,28 @@ static VALUE integration_run_romberg_with_workspace(VALUE data) {
25
31
  integration_romberg_params *params = (integration_romberg_params *)data;
26
32
 
27
33
  return rb_ensure(integration_run_romberg, data,
28
- integration_free_romberg_workspace,
29
- (VALUE)params->workspace);
34
+ integration_romberg_workspace_use_end,
35
+ (VALUE)&params->workspace_use);
30
36
  }
31
37
 
32
38
  VALUE integration_romberg(int argc, VALUE *argv, VALUE self) {
33
39
  VALUE a;
34
40
  VALUE b;
35
41
  VALUE options;
36
- integration_options parsed_options;
42
+ VALUE epsabs_value = Qundef;
43
+ VALUE epsrel_value = Qundef;
44
+ VALUE limit_value = Qundef;
45
+ VALUE workspace_value = Qundef;
46
+ ID keywords[] = {rb_intern("epsabs"), rb_intern("epsrel"), rb_intern("limit"),
47
+ rb_intern("workspace")};
48
+ VALUE keyword_values[4];
37
49
  double epsabs;
38
50
  double epsrel;
39
- size_t limit;
40
- gsl_integration_romberg_workspace *workspace;
51
+ size_t limit = 20;
52
+ int limit_given;
41
53
  gsl_function function;
42
54
  integration_callback_params params;
43
- integration_romberg_params romberg_params;
55
+ integration_romberg_params romberg_params = {0};
44
56
  double result;
45
57
  size_t evaluations;
46
58
  int state = 0;
@@ -48,23 +60,38 @@ VALUE integration_romberg(int argc, VALUE *argv, VALUE self) {
48
60
  (void)self;
49
61
 
50
62
  rb_scan_args(argc, argv, "2:", &a, &b, &options);
51
-
52
- integration_options_parse(options, &parsed_options, 0.0, 1e-8, 20);
53
-
54
- epsabs = parsed_options.epsabs;
55
- epsrel = parsed_options.epsrel;
56
- limit = parsed_options.limit;
63
+ if (!NIL_P(options)) {
64
+ rb_get_kwargs(options, keywords, 0, 4, keyword_values);
65
+ epsabs_value = keyword_values[0];
66
+ epsrel_value = keyword_values[1];
67
+ limit_value = keyword_values[2];
68
+ workspace_value = keyword_values[3];
69
+ }
70
+
71
+ epsabs = epsabs_value == Qundef ? 0.0 : NUM2DBL(epsabs_value);
72
+ epsrel = epsrel_value == Qundef ? 1e-8 : NUM2DBL(epsrel_value);
73
+ limit_given = limit_value != Qundef;
74
+ if (limit_given) {
75
+ if (!RB_INTEGER_TYPE_P(limit_value)) {
76
+ rb_raise(rb_eTypeError, "limit must be an Integer");
77
+ }
78
+ if (RTEST(rb_funcall(limit_value, rb_intern("<"), 1, INT2FIX(1))) ||
79
+ RTEST(rb_funcall(limit_value, rb_intern(">"), 1, INT2FIX(30)))) {
80
+ rb_raise(rb_eArgError, "limit must be between 1 and 30");
81
+ }
82
+ limit = NUM2SIZET(limit_value);
83
+ }
57
84
 
58
85
  integration_function_init(&function, &params);
59
86
 
60
- workspace = gsl_integration_romberg_alloc(limit);
61
-
62
87
  romberg_params.function = &function;
63
88
  romberg_params.a = NUM2DBL(a);
64
89
  romberg_params.b = NUM2DBL(b);
65
90
  romberg_params.epsabs = epsabs;
66
91
  romberg_params.epsrel = epsrel;
67
- romberg_params.workspace = workspace;
92
+ romberg_params.limit = limit;
93
+ romberg_params.limit_given = limit_given;
94
+ romberg_params.workspace_value = workspace_value;
68
95
  romberg_params.result = &result;
69
96
  romberg_params.evaluations = &evaluations;
70
97
 
@@ -73,6 +100,7 @@ VALUE integration_romberg(int argc, VALUE *argv, VALUE self) {
73
100
 
74
101
  RB_GC_GUARD(params.block);
75
102
  RB_GC_GUARD(params.exception);
103
+ RB_GC_GUARD(workspace_value);
76
104
 
77
105
  integration_finish_callback(&params, state);
78
106
 
@@ -0,0 +1,215 @@
1
+ #include "integration_internal.h"
2
+
3
+ static VALUE integration_cRombergWorkspace;
4
+
5
+ static void integration_romberg_workspace_free(void *data) {
6
+ integration_romberg_workspace *workspace = data;
7
+
8
+ if (workspace->workspace != NULL) {
9
+ gsl_integration_romberg_free(workspace->workspace);
10
+ }
11
+
12
+ xfree(workspace);
13
+ }
14
+
15
+ static size_t integration_romberg_workspace_memsize(const void *data) {
16
+ return data == NULL ? 0 : sizeof(integration_romberg_workspace);
17
+ }
18
+
19
+ static const rb_data_type_t integration_romberg_workspace_type = {
20
+ .wrap_struct_name = "Numo::GSL::Integration::RombergWorkspace",
21
+ .function =
22
+ {
23
+ .dmark = NULL,
24
+ .dfree = integration_romberg_workspace_free,
25
+ .dsize = integration_romberg_workspace_memsize,
26
+ },
27
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
28
+ };
29
+
30
+ static VALUE integration_romberg_workspace_allocate(VALUE klass) {
31
+ integration_romberg_workspace *workspace;
32
+ VALUE object =
33
+ TypedData_Make_Struct(klass, integration_romberg_workspace,
34
+ &integration_romberg_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_romberg_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_romberg_workspace *native_workspace;
51
+ integration_romberg_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(1))) ||
64
+ RTEST(rb_funcall(limit_value, rb_intern(">"), 1, INT2FIX(30)))) {
65
+ rb_raise(rb_eArgError, "limit must be between 1 and 30");
66
+ }
67
+
68
+ TypedData_Get_Struct(self, integration_romberg_workspace,
69
+ &integration_romberg_workspace_type, workspace);
70
+ if (workspace->workspace != NULL) {
71
+ rb_raise(rb_eRuntimeError, "workspace is already initialized");
72
+ }
73
+
74
+ limit = NUM2SIZET(limit_value);
75
+ native_workspace = gsl_integration_romberg_alloc(limit);
76
+ if (native_workspace == NULL) {
77
+ rb_memerror();
78
+ }
79
+
80
+ workspace->workspace = native_workspace;
81
+ workspace->limit = limit;
82
+
83
+ return self;
84
+ }
85
+
86
+ static VALUE integration_romberg_workspace_limit(VALUE self) {
87
+ integration_romberg_workspace *workspace;
88
+
89
+ TypedData_Get_Struct(self, integration_romberg_workspace,
90
+ &integration_romberg_workspace_type, workspace);
91
+
92
+ return SIZET2NUM(workspace->limit);
93
+ }
94
+
95
+ static VALUE integration_romberg_workspace_closed_p(VALUE self) {
96
+ integration_romberg_workspace *workspace;
97
+
98
+ TypedData_Get_Struct(self, integration_romberg_workspace,
99
+ &integration_romberg_workspace_type, workspace);
100
+
101
+ return workspace->workspace == NULL ? Qtrue : Qfalse;
102
+ }
103
+
104
+ static VALUE integration_romberg_workspace_close(VALUE self) {
105
+ integration_romberg_workspace *workspace;
106
+
107
+ TypedData_Get_Struct(self, integration_romberg_workspace,
108
+ &integration_romberg_workspace_type, workspace);
109
+ if (workspace->busy) {
110
+ rb_raise(rb_eRuntimeError, "workspace is already in use");
111
+ }
112
+ if (workspace->workspace != NULL) {
113
+ gsl_integration_romberg_free(workspace->workspace);
114
+ workspace->workspace = NULL;
115
+ }
116
+
117
+ return Qnil;
118
+ }
119
+
120
+ static VALUE integration_romberg_workspace_yield(VALUE workspace) {
121
+ return rb_yield(workspace);
122
+ }
123
+
124
+ static VALUE integration_romberg_workspace_open(int argc, VALUE *argv,
125
+ VALUE klass) {
126
+ VALUE workspace;
127
+
128
+ workspace =
129
+ rb_funcallv_kw(klass, rb_intern("new"), argc, argv, RB_PASS_KEYWORDS);
130
+
131
+ return rb_ensure(integration_romberg_workspace_yield, workspace,
132
+ integration_romberg_workspace_close, workspace);
133
+ }
134
+
135
+ static integration_romberg_workspace *
136
+ integration_romberg_workspace_get(VALUE workspace_value) {
137
+ integration_romberg_workspace *workspace;
138
+
139
+ if (!rb_obj_is_kind_of(workspace_value, integration_cRombergWorkspace)) {
140
+ rb_raise(rb_eTypeError,
141
+ "workspace must be a Numo::GSL::Integration::RombergWorkspace");
142
+ }
143
+
144
+ TypedData_Get_Struct(workspace_value, integration_romberg_workspace,
145
+ &integration_romberg_workspace_type, workspace);
146
+ if (workspace->workspace == NULL) {
147
+ rb_raise(rb_eRuntimeError, "workspace is closed");
148
+ }
149
+ if (workspace->busy) {
150
+ rb_raise(rb_eRuntimeError, "workspace is already in use");
151
+ }
152
+
153
+ return workspace;
154
+ }
155
+
156
+ void integration_romberg_workspace_use_begin(
157
+ VALUE workspace_value, size_t *limit, int limit_given,
158
+ integration_romberg_workspace_use *use) {
159
+ integration_romberg_workspace *workspace;
160
+
161
+ use->workspace = NULL;
162
+ use->owner = NULL;
163
+ if (workspace_value == Qundef) {
164
+ use->workspace = gsl_integration_romberg_alloc(*limit);
165
+ if (use->workspace == NULL) {
166
+ rb_memerror();
167
+ }
168
+ return;
169
+ }
170
+
171
+ workspace = integration_romberg_workspace_get(workspace_value);
172
+ if (limit_given && *limit != workspace->limit) {
173
+ rb_raise(rb_eArgError, "limit must match workspace capacity");
174
+ }
175
+ if (!limit_given) {
176
+ *limit = workspace->limit;
177
+ }
178
+
179
+ workspace->busy = 1;
180
+ use->workspace = workspace->workspace;
181
+ use->owner = workspace;
182
+ }
183
+
184
+ VALUE integration_romberg_workspace_use_end(VALUE data) {
185
+ integration_romberg_workspace_use *use =
186
+ (integration_romberg_workspace_use *)data;
187
+
188
+ if (use->owner != NULL) {
189
+ use->owner->busy = 0;
190
+ } else if (use->workspace != NULL) {
191
+ gsl_integration_romberg_free(use->workspace);
192
+ }
193
+
194
+ use->workspace = NULL;
195
+ use->owner = NULL;
196
+
197
+ return Qnil;
198
+ }
199
+
200
+ void integration_romberg_workspace_init(VALUE integration_module) {
201
+ integration_cRombergWorkspace =
202
+ rb_define_class_under(integration_module, "RombergWorkspace", rb_cObject);
203
+ rb_define_alloc_func(integration_cRombergWorkspace,
204
+ integration_romberg_workspace_allocate);
205
+ rb_define_method(integration_cRombergWorkspace, "initialize",
206
+ integration_romberg_workspace_initialize, -1);
207
+ rb_define_method(integration_cRombergWorkspace, "limit",
208
+ integration_romberg_workspace_limit, 0);
209
+ rb_define_method(integration_cRombergWorkspace, "closed?",
210
+ integration_romberg_workspace_closed_p, 0);
211
+ rb_define_method(integration_cRombergWorkspace, "close",
212
+ integration_romberg_workspace_close, 0);
213
+ rb_define_singleton_method(integration_cRombergWorkspace, "open",
214
+ integration_romberg_workspace_open, -1);
215
+ }
@@ -3,7 +3,7 @@
3
3
  module Numo
4
4
  module GSL
5
5
  module Integration
6
- VERSION = "0.6.0"
6
+ VERSION = "0.7.0"
7
7
  end
8
8
  end
9
9
  end
@@ -17,6 +17,27 @@ module Numo
17
17
  def close: () -> nil
18
18
  def closed?: () -> bool
19
19
  end
20
+ class CQUADWorkspace
21
+ def self.open: [T] (limit: Integer) { (CQUADWorkspace) -> T } -> T
22
+ def initialize: (limit: Integer) -> void
23
+ def limit: () -> Integer
24
+ def close: () -> nil
25
+ def closed?: () -> bool
26
+ end
27
+ class RombergWorkspace
28
+ def self.open: [T] (limit: Integer) { (RombergWorkspace) -> T } -> T
29
+ def initialize: (limit: Integer) -> void
30
+ def limit: () -> Integer
31
+ def close: () -> nil
32
+ def closed?: () -> bool
33
+ end
34
+ class GLFixedTable
35
+ def self.open: [T] (order: Integer) { (GLFixedTable) -> T } -> T
36
+ def initialize: (order: Integer) -> void
37
+ def order: () -> Integer
38
+ def close: () -> nil
39
+ def closed?: () -> bool
40
+ end
20
41
  class QAWOTable
21
42
  def self.open: [T] (
22
43
  omega: Numeric,
@@ -167,19 +188,22 @@ module Numo
167
188
  Numeric b,
168
189
  ?epsabs: Numeric,
169
190
  ?epsrel: Numeric,
170
- ?limit: Integer
191
+ ?limit: Integer,
192
+ ?workspace: CQUADWorkspace
171
193
  ) { (Float) -> Numeric } -> [Float, Float, Integer]
172
194
  def self.romberg: (
173
195
  Numeric a,
174
196
  Numeric b,
175
197
  ?epsabs: Numeric,
176
198
  ?epsrel: Numeric,
177
- ?limit: Integer
199
+ ?limit: Integer,
200
+ ?workspace: RombergWorkspace
178
201
  ) { (Float) -> Numeric } -> [Float, Integer]
179
202
  def self.glfixed: (
180
203
  Numeric a,
181
204
  Numeric b,
182
- ?order: Integer
205
+ ?order: Integer,
206
+ ?table: GLFixedTable
183
207
  ) { (Float) -> Numeric } -> Float
184
208
  def self.fixed: (
185
209
  (:legendre | :chebyshev | :chebyshev2 | :gegenbauer | :jacobi | :laguerre | :hermite | :exponential | :rational) type,
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro SATOH
@@ -26,9 +26,11 @@ files:
26
26
  - docs/routines.md
27
27
  - ext/numo/gsl/integration/callback.c
28
28
  - ext/numo/gsl/integration/cquad.c
29
+ - ext/numo/gsl/integration/cquad_workspace.c
29
30
  - ext/numo/gsl/integration/extconf.rb
30
31
  - ext/numo/gsl/integration/fixed.c
31
32
  - ext/numo/gsl/integration/glfixed.c
33
+ - ext/numo/gsl/integration/glfixed_table.c
32
34
  - ext/numo/gsl/integration/integration.c
33
35
  - ext/numo/gsl/integration/integration_internal.h
34
36
  - ext/numo/gsl/integration/lebedev.c
@@ -49,6 +51,7 @@ files:
49
51
  - ext/numo/gsl/integration/qng.c
50
52
  - ext/numo/gsl/integration/resources.c
51
53
  - ext/numo/gsl/integration/romberg.c
54
+ - ext/numo/gsl/integration/romberg_workspace.c
52
55
  - ext/numo/gsl/integration/workspace.c
53
56
  - lib/numo/gsl/integration.rb
54
57
  - lib/numo/gsl/integration/version.rb