numo-gsl-integration 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24601b3a87462ee4bc6077999599478d3c08776b8da2caa648e56dbfaacec731
4
- data.tar.gz: 97d7fe3772e374a35ab108ea0a4238fbbceb04598fc2637c3f4d76285cc17f66
3
+ metadata.gz: aa4ddfbcf26eeefdb3642b8358c008ae8e839a0d77dcf934b77e98f2bc3e44f7
4
+ data.tar.gz: 07c1c707e6b9d040ac14e996f334f2a207fa0d1139869a93cd032db23d60ac8f
5
5
  SHA512:
6
- metadata.gz: 86447bf3974787c1067ceecacf700b3583fc2365fb0699f7fac10ed33c54b03c83a9ea5467598c0f9f687222d8ad6fed5c14feeca617d61d7ad39f01accb9ea1
7
- data.tar.gz: 682f48569710825aac5fd2905dcfcab621abe47cea6f48f332b367d6da1747afe38512d08a6e84e1911b6ccd331d88fdb329b714bfa5619534ed8cea41227708
6
+ metadata.gz: 06216b69ff868309cc71941d383db61db1cea770b79b48a606470394229d3fc6ea3107ceb083f4b698d6911f30f31ec6c7599a27c3a7e15c8d3cb6ce11c0975a
7
+ data.tar.gz: 13e9ae3991bbd3079a78eb58ee6165edde4c4c3f80a76f8fa4323d4e388c3c73bba95ba6d4115976f6682f8301805938f722a671b4e481627ad21f5981e95aa4
data/CHANGELOG.md CHANGED
@@ -2,6 +2,30 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.5.0] - 2026-11-07
6
+
7
+ ### Added in 0.5.0
8
+
9
+ - Added `Numo::GSL::Integration::Workspace` for reusing adaptive-integration workspace allocations across `qag`, `qags`, `qagp`, `qagi`, `qagiu`, `qagil`, `qawc`, `qaws`, and `qawo` calls.
10
+ - Added block-scoped workspace management with `Workspace.open`, explicit lifecycle methods with `close` and `closed?`, and workspace capacity reporting with `limit`.
11
+ - Added `Numo::GSL::Integration::QAWOTable` for reusing QAWO Chebyshev-moment tables with fixed angular frequency, interval length, weight, and level count.
12
+ - Added `Numo::GSL::Integration::QAWSTable` for reusing QAWS Chebyshev-moment tables with fixed algebraic and logarithmic weight parameters.
13
+
14
+ ### Changed in 0.5.0
15
+
16
+ - Added an optional `table:` keyword to `qaws` and `qawo`. Calls that omit it continue to allocate and release a temporary routine-specific table.
17
+ - 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.
18
+
19
+ ## [0.4.0] - 2026-11-06
20
+
21
+ ### Added in 0.4.0
22
+
23
+ - Added Lebedev quadrature for surface integration over the unit sphere.
24
+
25
+ ### Changed in 0.4.0
26
+
27
+ - Required GNU Scientific Library 2.8 or later.
28
+
5
29
  ## [0.3.1] - 2026-11-06
6
30
 
7
31
  ### Changed in 0.3.1
data/README.md CHANGED
@@ -5,9 +5,8 @@
5
5
  ## Requirements
6
6
 
7
7
  - [Ruby](https://www.ruby-lang.org/) 3.2 or later
8
- - GNU Scientific Library (GSL)
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,26 @@ 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`, and `qawo`. See [Integration routines](docs/routines.md) for lifecycle and capacity details.
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.
63
+
45
64
  ## Supported routines
46
65
 
47
66
  | Method | Interval | Intended use |
@@ -61,6 +80,7 @@ end
61
80
  | `romberg` | Finite | Smooth functions with extrapolated trapezoidal integration |
62
81
  | `glfixed` | Finite | Fixed-order Gauss-Legendre integration of smooth functions |
63
82
  | `fixed` | Rule-dependent | Fixed-order Gaussian quadrature with selectable weight functions |
83
+ | `lebedev` | Unit sphere | Lebedev quadrature over the surface of the unit sphere |
64
84
 
65
85
  See [Integration routines](docs/routines.md) for parameters and examples.
66
86
 
data/docs/development.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Development
2
2
 
3
+ Development and builds require GSL 2.8 or later. Confirm the installed version before building:
4
+
5
+ ```bash
6
+ gsl-config --version
7
+ ```
8
+
9
+ The extension configuration stops with an error if `gsl-config` reports an older version or if the Lebedev integration API is unavailable.
10
+
3
11
  After checking out the repository, install the dependencies:
4
12
 
5
13
  ```bash
data/docs/routines.md CHANGED
@@ -8,6 +8,47 @@ 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`, and `qawo`. 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
+ 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
+
11
52
  ## QNG integration
12
53
 
13
54
  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 +136,7 @@ end
95
136
  ```
96
137
 
97
138
  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.
139
+
98
140
  The `epsabs`, `epsrel`, and `limit` keyword arguments control the requested error limits and maximum number of subintervals:
99
141
 
100
142
  ```ruby
@@ -185,6 +227,7 @@ PV \int_a^b \frac{f(x)}{x-c}\,dx = \lim_{\varepsilon\to 0^{+}} \left( \int_a^{c-
185
227
  ```
186
228
 
187
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.
230
+
188
231
  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
232
 
190
233
  ```ruby
@@ -218,6 +261,7 @@ GSL [QAWS](https://www.gnu.org/software/gsl/doc/html/integration.html#qaws-adapt
218
261
  ```
219
262
 
220
263
  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
+
221
265
  The block returns the unweighted function $f(x)$. QAWS applies the endpoint weight internally. The following example integrates
222
266
 
223
267
  ```math
@@ -259,6 +303,34 @@ end
259
303
 
260
304
  This example computes the integral of `log(x)` from `0.0` to `1.0`, whose exact value is `-1.0`.
261
305
 
306
+ 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:`:
307
+
308
+ ```ruby
309
+ Numo::GSL::Integration::Workspace.open(limit: 100) do |workspace|
310
+ Numo::GSL::Integration::QAWSTable.open(
311
+ alpha: -0.5,
312
+ beta: -0.5,
313
+ mu: 0,
314
+ nu: 0
315
+ ) do |table|
316
+ result, error = Numo::GSL::Integration.qaws(
317
+ 0.0,
318
+ 1.0,
319
+ -0.5,
320
+ -0.5,
321
+ 0,
322
+ 0,
323
+ workspace: workspace,
324
+ table: table
325
+ ) { |_x| 1.0 }
326
+ end
327
+ end
328
+ ```
329
+
330
+ `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.
331
+
332
+ 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:`.
333
+
262
334
  ## QAWO integration
263
335
 
264
336
  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 +346,31 @@ result, error = Numo::GSL::Integration.qawo(
274
346
  end
275
347
  ```
276
348
 
277
- The `epsabs`, `epsrel`, and `limit` keyword arguments control the requested error limits and workspace capacity.
349
+ 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:`:
350
+
351
+ ```ruby
352
+ Numo::GSL::Integration::Workspace.open(limit: 100) do |workspace|
353
+ Numo::GSL::Integration::QAWOTable.open(
354
+ omega: 1.0,
355
+ length: Math::PI,
356
+ sine: Numo::GSL::Integration::SINE,
357
+ levels: 100
358
+ ) do |table|
359
+ result, error = Numo::GSL::Integration.qawo(
360
+ 0.0,
361
+ Math::PI,
362
+ 1.0,
363
+ Numo::GSL::Integration::SINE,
364
+ workspace: workspace,
365
+ table: table
366
+ ) { |_x| 1.0 }
367
+ end
368
+ end
369
+ ```
370
+
371
+ `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.
372
+
373
+ 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
374
 
279
375
  ## QAWF integration
280
376
 
@@ -315,6 +411,7 @@ end
315
411
  ```
316
412
 
317
413
  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.
414
+
318
415
  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
416
 
320
417
  ## GLFIXED integration
@@ -332,6 +429,7 @@ end
332
429
  ```
333
430
 
334
431
  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.
432
+
335
433
  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
434
 
337
435
  ## Fixed point quadratures
@@ -383,3 +481,28 @@ end
383
481
  ```
384
482
 
385
483
  The example result is `1.0 / 6.0`. Fixed point quadratures return only the integral. They do not accept absolute or relative error bounds and do not return an error estimate. Use an adaptive routine when the required order is not known in advance.
484
+
485
+ ## Lebedev integration on the unit sphere
486
+
487
+ GSL [Lebedev quadrature](https://www.gnu.org/software/gsl/doc/html/integration.html#integrating-on-the-unit-sphere) approximates a surface integral over the unit sphere. `Numo::GSL::Integration.lebedev` passes the Cartesian coordinates of each quadrature point to the block:
488
+
489
+ ```ruby
490
+ result = Numo::GSL::Integration.lebedev(26) do |x, y, z|
491
+ (x * x) + (y * y) + (z * z)
492
+ end
493
+ ```
494
+
495
+ Every point satisfies `x * x + y * y + z * z == 1` within floating-point precision, so the example integral has an exact value of `4.0 * Math::PI`.
496
+
497
+ The argument is the number of Lebedev nodes. GSL 2.8 provides the following node counts:
498
+
499
+ ```text
500
+ 6, 14, 26, 38, 50, 74, 86, 110,
501
+ 146, 170, 194, 230, 266, 302, 350, 434,
502
+ 590, 770, 974, 1202, 1454, 1730, 2030, 2354,
503
+ 2702, 3074, 3470, 3890, 4334, 4802, 5294, 5810
504
+ ```
505
+
506
+ The node count must be a positive integer and must identify one of the rules supplied by GSL. Unsupported node counts are reported as `Numo::GSL::GslError`.
507
+
508
+ Lebedev integration returns only the surface integral. It does not accept an error tolerance and does not return an error estimate. Increasing the node count selects a rule that integrates spherical harmonics to a higher degree exactly.
@@ -1,13 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "mkmf"
4
+ require "rubygems/version"
4
5
 
5
6
  gsl_config = find_executable("gsl-config")
6
7
  abort("gsl-config is required") unless gsl_config
7
8
 
9
+ minimum_gsl_version = Gem::Version.new("2.8")
10
+ gsl_version = Gem::Version.new(`#{gsl_config} --version`.strip)
11
+ abort("GSL 2.8 or later is required (found #{gsl_version})") if gsl_version < minimum_gsl_version
12
+
8
13
  $CFLAGS << " #{`#{gsl_config} --cflags`.strip}"
9
14
  $libs = "#{`#{gsl_config} --libs`.strip} #{$libs}"
10
15
 
11
- abort("gsl/gsl_integration.h is required") unless have_header("gsl/gsl_integration.h")
16
+ header = "gsl/gsl_integration.h"
17
+ abort("#{header} is required") unless have_header(header)
18
+ abort("GSL Lebedev integration support is required") unless have_func("gsl_integration_lebedev_alloc", header)
12
19
 
13
20
  create_makefile("numo/gsl/integration/integration")
@@ -19,6 +19,9 @@ void Init_integration(void) {
19
19
  mIntegration = rb_define_module_under(mGSL, "Integration");
20
20
  numo_eGslError = rb_define_class_under(mGSL, "GslError", rb_eStandardError);
21
21
  gsl_set_error_handler(&integration_gsl_error_handler);
22
+ integration_workspace_init(mIntegration);
23
+ integration_qawo_table_init(mIntegration);
24
+ integration_qaws_table_init(mIntegration);
22
25
  integration_fixed_init();
23
26
 
24
27
  rb_define_const(mIntegration, "GAUSS15", INT2NUM(GSL_INTEG_GAUSS15));
@@ -45,4 +48,5 @@ void Init_integration(void) {
45
48
  rb_define_singleton_method(mIntegration, "romberg", integration_romberg, -1);
46
49
  rb_define_singleton_method(mIntegration, "glfixed", integration_glfixed, -1);
47
50
  rb_define_singleton_method(mIntegration, "fixed", integration_fixed, -1);
51
+ rb_define_singleton_method(mIntegration, "lebedev", integration_lebedev, 1);
48
52
  }
@@ -3,6 +3,7 @@
3
3
 
4
4
  #include <gsl/gsl_errno.h>
5
5
  #include <gsl/gsl_integration.h>
6
+ #include <gsl/gsl_math.h>
6
7
  #include <ruby.h>
7
8
 
8
9
  typedef struct {
@@ -14,6 +15,69 @@ typedef struct {
14
15
  void integration_options_parse(VALUE options, integration_options *parsed,
15
16
  double default_epsabs, double default_epsrel,
16
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
+ void integration_workspace_use_begin(VALUE workspace_value, size_t *limit,
36
+ int limit_given,
37
+ integration_workspace_use *use);
38
+ VALUE integration_workspace_use_end(VALUE data);
39
+
40
+ typedef struct {
41
+ gsl_integration_qawo_table *table;
42
+ double omega;
43
+ double length;
44
+ enum gsl_integration_qawo_enum sine;
45
+ size_t levels;
46
+ int busy;
47
+ } integration_qawo_table;
48
+
49
+ typedef struct {
50
+ gsl_integration_qawo_table *table;
51
+ integration_qawo_table *owner;
52
+ } integration_qawo_table_use;
53
+
54
+ void integration_qawo_table_init(VALUE integration_module);
55
+ void integration_qawo_table_use_begin(VALUE table_value, double omega,
56
+ double length,
57
+ enum gsl_integration_qawo_enum sine,
58
+ size_t levels,
59
+ integration_qawo_table_use *use);
60
+ VALUE integration_qawo_table_use_end(VALUE data);
61
+
62
+ typedef struct {
63
+ gsl_integration_qaws_table *table;
64
+ double alpha;
65
+ double beta;
66
+ int mu;
67
+ int nu;
68
+ int busy;
69
+ } integration_qaws_table;
70
+
71
+ typedef struct {
72
+ gsl_integration_qaws_table *table;
73
+ integration_qaws_table *owner;
74
+ } integration_qaws_table_use;
75
+
76
+ void integration_qaws_table_init(VALUE integration_module);
77
+ void integration_qaws_table_use_begin(VALUE table_value, double alpha,
78
+ double beta, int mu, int nu,
79
+ integration_qaws_table_use *use);
80
+ VALUE integration_qaws_table_use_end(VALUE data);
17
81
 
18
82
  typedef struct {
19
83
  VALUE block;
@@ -52,5 +116,6 @@ VALUE integration_cquad(int argc, VALUE *argv, VALUE self);
52
116
  VALUE integration_romberg(int argc, VALUE *argv, VALUE self);
53
117
  VALUE integration_glfixed(int argc, VALUE *argv, VALUE self);
54
118
  VALUE integration_fixed(int argc, VALUE *argv, VALUE self);
119
+ VALUE integration_lebedev(VALUE self, VALUE n_value);
55
120
 
56
121
  #endif
@@ -0,0 +1,103 @@
1
+ #include "integration_internal.h"
2
+
3
+ typedef struct {
4
+ VALUE block;
5
+ VALUE x;
6
+ VALUE y;
7
+ VALUE z;
8
+ VALUE exception;
9
+ gsl_integration_lebedev_workspace *workspace;
10
+ double result;
11
+ int state;
12
+ } integration_lebedev_params;
13
+
14
+ static VALUE integration_call_lebedev_block(VALUE data) {
15
+ integration_lebedev_params *params = (integration_lebedev_params *)data;
16
+ VALUE arguments[3] = {params->x, params->y, params->z};
17
+ VALUE value;
18
+
19
+ value = rb_funcallv(params->block, rb_intern("call"), 3, arguments);
20
+ return rb_Float(value);
21
+ }
22
+
23
+ static VALUE integration_free_lebedev_workspace(VALUE data) {
24
+ integration_lebedev_params *params = (integration_lebedev_params *)data;
25
+
26
+ if (params->workspace != NULL) {
27
+ gsl_integration_lebedev_free(params->workspace);
28
+ }
29
+
30
+ return Qnil;
31
+ }
32
+
33
+ static VALUE integration_run_lebedev(VALUE data) {
34
+ integration_lebedev_params *params = (integration_lebedev_params *)data;
35
+ double result = 0.0;
36
+ size_t i;
37
+
38
+ for (i = 0; i < params->workspace->n; ++i) {
39
+ VALUE value;
40
+
41
+ params->x = DBL2NUM(params->workspace->x[i]);
42
+ params->y = DBL2NUM(params->workspace->y[i]);
43
+ params->z = DBL2NUM(params->workspace->z[i]);
44
+ value = rb_protect(integration_call_lebedev_block, (VALUE)params,
45
+ &params->state);
46
+
47
+ if (params->state != 0) {
48
+ params->exception = rb_errinfo();
49
+ break;
50
+ }
51
+
52
+ result += params->workspace->weights[i] * NUM2DBL(value);
53
+ }
54
+
55
+ params->result = 4.0 * M_PI * result;
56
+ return Qnil;
57
+ }
58
+
59
+ static VALUE integration_run_lebedev_with_workspace(VALUE data) {
60
+ integration_lebedev_params *params = (integration_lebedev_params *)data;
61
+
62
+ return rb_ensure(integration_run_lebedev, data,
63
+ integration_free_lebedev_workspace, data);
64
+ }
65
+
66
+ VALUE integration_lebedev(VALUE self, VALUE n_value) {
67
+ integration_lebedev_params params = {0};
68
+ size_t n;
69
+ int state = 0;
70
+
71
+ (void)self;
72
+
73
+ if (!RB_INTEGER_TYPE_P(n_value)) {
74
+ rb_raise(rb_eTypeError, "n must be an Integer");
75
+ }
76
+ if (RTEST(rb_funcall(n_value, rb_intern("<="), 1, INT2FIX(0)))) {
77
+ rb_raise(rb_eArgError, "n must be positive");
78
+ }
79
+
80
+ n = NUM2SIZET(n_value);
81
+ params.block = rb_block_proc();
82
+ params.workspace = gsl_integration_lebedev_alloc(n);
83
+ if (params.workspace == NULL) {
84
+ rb_memerror();
85
+ }
86
+
87
+ rb_protect(integration_run_lebedev_with_workspace, (VALUE)&params, &state);
88
+
89
+ RB_GC_GUARD(params.block);
90
+ RB_GC_GUARD(params.x);
91
+ RB_GC_GUARD(params.y);
92
+ RB_GC_GUARD(params.z);
93
+ RB_GC_GUARD(params.exception);
94
+
95
+ if (params.state != 0) {
96
+ rb_exc_raise(params.exception);
97
+ }
98
+ if (state != 0) {
99
+ rb_jump_tag(state);
100
+ }
101
+
102
+ return DBL2NUM(params.result);
103
+ }
@@ -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
  }