numo-gsl-integration 0.3.0 → 0.4.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: 0ed1ccbd179314a5925c135faa809ff255bb9acf626c475af39237c8e397f89a
4
- data.tar.gz: 891a0e74db19f6d5c5b40aeff66a2610dbf063c5d2ccd3b9dfdae0a730a880fd
3
+ metadata.gz: 57503e38512a38ad357142981868d2e2aea1a47743f27aee55d80625715eb794
4
+ data.tar.gz: d87dadfb371056e006cb1a914c6cebe81131cfa21c1e5f4daa35f72f377a98bf
5
5
  SHA512:
6
- metadata.gz: 86207b0346bcff38a8892e36c8721d2327010b7367a74b91344db9c799ae2ea0390203572ce275efad201a47bc480f66e26b1daad753cfd63c878c7b78575793
7
- data.tar.gz: 532c33c4e35c701e901cf10d771a9c982533b927353dbbe0d53173684dc2ab66a90231e617906a10cc3d530aa05f364ea83735982e3721b421e06e1cbb2b983a
6
+ metadata.gz: ad109b9e20ce13a6eeb8b34b919c49a353eb5b27b158e74d1f9d149219fc200ca9a567417a12a4ea6dc66f247caf68bfe864d5cbaaf811b58fdacd8c8ef6ffbb
7
+ data.tar.gz: 6cab24c8607e941ab66eaa7f44db5492836c117649956979f148755df73792fd66c0baa794177276c02dd90e1e931d071cd0b19a174dd8d6dbb2c864da08e0cf
data/CHANGELOG.md CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.4.0] - 2026-11-06
6
+
7
+ ### Added in 0.4.0
8
+
9
+ - Added Lebedev quadrature for surface integration over the unit sphere.
10
+
11
+ ### Changed in 0.4.0
12
+
13
+ - Required GNU Scientific Library 2.8 or later.
14
+
15
+ ## [0.3.1] - 2026-11-06
16
+
17
+ ### Changed in 0.3.1
18
+
19
+ - Simplified the internal handling of fixed quadrature rules without changing the public API.
20
+
5
21
  ## [0.3.0] - 2026-11-05
6
22
 
7
23
  ### Added in 0.3.0
data/README.md CHANGED
@@ -5,7 +5,7 @@
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
10
 
11
11
  GSL itself must be installed separately.
@@ -61,6 +61,7 @@ end
61
61
  | `romberg` | Finite | Smooth functions with extrapolated trapezoidal integration |
62
62
  | `glfixed` | Finite | Fixed-order Gauss-Legendre integration of smooth functions |
63
63
  | `fixed` | Rule-dependent | Fixed-order Gaussian quadrature with selectable weight functions |
64
+ | `lebedev` | Unit sphere | Lebedev quadrature over the surface of the unit sphere |
64
65
 
65
66
  See [Integration routines](docs/routines.md) for parameters and examples.
66
67
 
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
@@ -181,14 +181,7 @@ end
181
181
  GSL [QAWC](https://www.gnu.org/software/gsl/doc/html/integration.html#qawc-adaptive-integration-for-cauchy-principal-values) computes the Cauchy principal value (PV) of an integral over a finite interval with a singularity at $c$:
182
182
 
183
183
  ```math
184
- PV \\int_a^b \\frac{f(x)}{x-c}\\,dx
185
- =
186
- \\lim_{\\varepsilon\\to 0^+}
187
- \\left(
188
- \\int_a^{c-\\varepsilon}\\frac{f(x)}{x-c}\\,dx
189
- \+
190
- \\int_{c+\\varepsilon}^b\\frac{f(x)}{x-c}\\,dx
191
- \\right)
184
+ 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)
192
185
  ```
193
186
 
194
187
  The block passed to `Numo::GSL::Integration.qawc` returns $f(x)$. Do not divide the block result by $x - c$; QAWC applies that weight internally.
@@ -220,15 +213,15 @@ end
220
213
  GSL [QAWS](https://www.gnu.org/software/gsl/doc/html/integration.html#qaws-adaptive-integration-for-singular-functions) provides adaptive integration for functions with algebraic or logarithmic singularities at the endpoints of a finite interval. It computes an integral with the weight
221
214
 
222
215
  ```math
223
- (x-a)^\\alpha (b-x)^\\beta
224
- \\log^\\mu(x-a) \\log^\\nu(b-x).
216
+ (x-a)^\alpha (b-x)^\beta
217
+ \log^\mu(x-a) \log^\nu(b-x).
225
218
  ```
226
219
 
227
220
  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`.
228
221
  The block returns the unweighted function $f(x)$. QAWS applies the endpoint weight internally. The following example integrates
229
222
 
230
223
  ```math
231
- \\frac{1}{\\sqrt{x(1-x)}}
224
+ \frac{1}{\sqrt{x(1-x)}}
232
225
  ```
233
226
 
234
227
  over the interval from `0.0` to `1.0`. Its exact value is `Math::PI`:
@@ -390,3 +383,28 @@ end
390
383
  ```
391
384
 
392
385
  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.
386
+
387
+ ## Lebedev integration on the unit sphere
388
+
389
+ 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:
390
+
391
+ ```ruby
392
+ result = Numo::GSL::Integration.lebedev(26) do |x, y, z|
393
+ (x * x) + (y * y) + (z * z)
394
+ end
395
+ ```
396
+
397
+ 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`.
398
+
399
+ The argument is the number of Lebedev nodes. GSL 2.8 provides the following node counts:
400
+
401
+ ```text
402
+ 6, 14, 26, 38, 50, 74, 86, 110,
403
+ 146, 170, 194, 230, 266, 302, 350, 434,
404
+ 590, 770, 974, 1202, 1454, 1730, 2030, 2354,
405
+ 2702, 3074, 3470, 3890, 4334, 4802, 5294, 5810
406
+ ```
407
+
408
+ 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`.
409
+
410
+ 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")
@@ -1,4 +1,17 @@
1
1
  #include "integration_internal.h"
2
+
3
+ enum {
4
+ INTEGRATION_FIXED_REQUIRES_ALPHA = 1 << 0,
5
+ INTEGRATION_FIXED_REQUIRES_BETA = 1 << 1
6
+ };
7
+
8
+ typedef struct {
9
+ ID id;
10
+ const char *name;
11
+ const gsl_integration_fixed_type **type;
12
+ unsigned int requirements;
13
+ } integration_fixed_type_info;
14
+
2
15
  typedef struct {
3
16
  gsl_function *function;
4
17
  const gsl_integration_fixed_type *type;
@@ -10,150 +23,164 @@ typedef struct {
10
23
  gsl_integration_fixed_workspace *workspace;
11
24
  double *result;
12
25
  } integration_fixed_params;
13
- static const gsl_integration_fixed_type *integration_fixed_type(VALUE type) {
26
+
27
+ static integration_fixed_type_info integration_fixed_types[] = {
28
+ {0, "legendre", &gsl_integration_fixed_legendre, 0},
29
+ {0, "chebyshev", &gsl_integration_fixed_chebyshev, 0},
30
+ {0, "chebyshev2", &gsl_integration_fixed_chebyshev2, 0},
31
+ {0, "gegenbauer", &gsl_integration_fixed_gegenbauer,
32
+ INTEGRATION_FIXED_REQUIRES_ALPHA},
33
+ {0, "jacobi", &gsl_integration_fixed_jacobi,
34
+ INTEGRATION_FIXED_REQUIRES_ALPHA | INTEGRATION_FIXED_REQUIRES_BETA},
35
+ {0, "laguerre", &gsl_integration_fixed_laguerre,
36
+ INTEGRATION_FIXED_REQUIRES_ALPHA},
37
+ {0, "hermite", &gsl_integration_fixed_hermite,
38
+ INTEGRATION_FIXED_REQUIRES_ALPHA},
39
+ {0, "exponential", &gsl_integration_fixed_exponential,
40
+ INTEGRATION_FIXED_REQUIRES_ALPHA},
41
+ {0, "rational", &gsl_integration_fixed_rational,
42
+ INTEGRATION_FIXED_REQUIRES_ALPHA | INTEGRATION_FIXED_REQUIRES_BETA},
43
+ };
44
+
45
+ static ID integration_fixed_id_order;
46
+ static ID integration_fixed_id_a;
47
+ static ID integration_fixed_id_b;
48
+ static ID integration_fixed_id_alpha;
49
+ static ID integration_fixed_id_beta;
50
+ static ID integration_fixed_id_less_than_or_equal;
51
+
52
+ void integration_fixed_init(void) {
53
+ size_t i;
54
+
55
+ for (i = 0;
56
+ i < sizeof(integration_fixed_types) / sizeof(integration_fixed_types[0]);
57
+ ++i) {
58
+ integration_fixed_types[i].id = rb_intern(integration_fixed_types[i].name);
59
+ }
60
+
61
+ integration_fixed_id_order = rb_intern("order");
62
+ integration_fixed_id_a = rb_intern("a");
63
+ integration_fixed_id_b = rb_intern("b");
64
+ integration_fixed_id_alpha = rb_intern("alpha");
65
+ integration_fixed_id_beta = rb_intern("beta");
66
+ integration_fixed_id_less_than_or_equal = rb_intern("<=");
67
+ }
68
+
69
+ static const integration_fixed_type_info *integration_fixed_type(VALUE type) {
14
70
  ID type_id;
71
+ size_t i;
72
+
15
73
  if (!RB_TYPE_P(type, T_SYMBOL)) {
16
74
  rb_raise(rb_eTypeError, "type must be a Symbol");
17
75
  }
76
+
18
77
  type_id = SYM2ID(type);
19
- if (type_id == rb_intern("legendre")) {
20
- return gsl_integration_fixed_legendre;
21
- }
22
- if (type_id == rb_intern("chebyshev")) {
23
- return gsl_integration_fixed_chebyshev;
24
- }
25
- if (type_id == rb_intern("chebyshev2")) {
26
- return gsl_integration_fixed_chebyshev2;
27
- }
28
- if (type_id == rb_intern("gegenbauer")) {
29
- return gsl_integration_fixed_gegenbauer;
30
- }
31
- if (type_id == rb_intern("jacobi")) {
32
- return gsl_integration_fixed_jacobi;
33
- }
34
- if (type_id == rb_intern("laguerre")) {
35
- return gsl_integration_fixed_laguerre;
36
- }
37
- if (type_id == rb_intern("hermite")) {
38
- return gsl_integration_fixed_hermite;
39
- }
40
- if (type_id == rb_intern("exponential")) {
41
- return gsl_integration_fixed_exponential;
42
- }
43
- if (type_id == rb_intern("rational")) {
44
- return gsl_integration_fixed_rational;
78
+ for (i = 0;
79
+ i < sizeof(integration_fixed_types) / sizeof(integration_fixed_types[0]);
80
+ ++i) {
81
+ if (integration_fixed_types[i].id == type_id) {
82
+ return &integration_fixed_types[i];
83
+ }
45
84
  }
85
+
46
86
  rb_raise(rb_eArgError, "unsupported fixed quadrature type: %s",
47
87
  rb_id2name(type_id));
48
88
  }
89
+
49
90
  static VALUE integration_free_fixed_workspace(VALUE data) {
50
91
  integration_fixed_params *params = (integration_fixed_params *)data;
92
+
51
93
  if (params->workspace != NULL) {
52
94
  gsl_integration_fixed_free(params->workspace);
53
95
  }
54
96
  return Qnil;
55
97
  }
98
+
56
99
  static VALUE integration_run_fixed(VALUE data) {
57
100
  integration_fixed_params *params = (integration_fixed_params *)data;
101
+
58
102
  params->workspace =
59
103
  gsl_integration_fixed_alloc(params->type, params->order, params->a,
60
104
  params->b, params->alpha, params->beta);
61
105
  if (params->workspace == NULL) {
62
106
  rb_memerror();
63
107
  }
108
+
64
109
  (void)gsl_integration_fixed(params->function, params->result,
65
110
  params->workspace);
66
111
  return Qnil;
67
112
  }
113
+
68
114
  static VALUE integration_run_fixed_with_workspace(VALUE data) {
69
115
  return rb_ensure(integration_run_fixed, data,
70
116
  integration_free_fixed_workspace, data);
71
117
  }
118
+
72
119
  VALUE integration_fixed(int argc, VALUE *argv, VALUE self) {
73
120
  VALUE type;
74
121
  VALUE options;
75
- ID keywords[] = {rb_intern("order"), rb_intern("a"), rb_intern("b"),
76
- rb_intern("alpha"), rb_intern("beta")};
122
+ ID keywords[] = {integration_fixed_id_order, integration_fixed_id_a,
123
+ integration_fixed_id_b, integration_fixed_id_alpha,
124
+ integration_fixed_id_beta};
77
125
  VALUE keyword_values[5];
78
126
  VALUE order_value;
79
127
  VALUE alpha_value;
80
128
  VALUE beta_value;
129
+ const integration_fixed_type_info *type_info;
81
130
  size_t order;
82
131
  gsl_function function;
83
132
  integration_callback_params params;
84
- integration_fixed_params fixed_params;
133
+ integration_fixed_params fixed_params = {0};
85
134
  double result;
86
135
  int state = 0;
136
+
87
137
  (void)self;
88
138
  rb_scan_args(argc, argv, "1:", &type, &options);
89
- fixed_params.type = integration_fixed_type(type);
139
+ type_info = integration_fixed_type(type);
140
+
90
141
  if (NIL_P(options)) {
91
142
  options = rb_hash_new();
92
143
  }
93
144
  rb_get_kwargs(options, keywords, 3, 2, keyword_values);
145
+
94
146
  order_value = keyword_values[0];
95
147
  alpha_value = keyword_values[3];
96
148
  beta_value = keyword_values[4];
149
+
97
150
  if (!RB_INTEGER_TYPE_P(order_value)) {
98
151
  rb_raise(rb_eTypeError, "order must be an Integer");
99
152
  }
100
- if (RTEST(rb_funcall(order_value, rb_intern("<="), 1, INT2FIX(0)))) {
153
+ if (RTEST(rb_funcall(order_value, integration_fixed_id_less_than_or_equal, 1,
154
+ INT2FIX(0)))) {
101
155
  rb_raise(rb_eArgError, "order must be positive");
102
156
  }
103
157
  order = NUM2SIZET(order_value);
158
+
159
+ if ((type_info->requirements & INTEGRATION_FIXED_REQUIRES_ALPHA) != 0 &&
160
+ alpha_value == Qundef) {
161
+ rb_raise(rb_eArgError, "alpha is required for %s", type_info->name);
162
+ }
163
+ if ((type_info->requirements & INTEGRATION_FIXED_REQUIRES_BETA) != 0 &&
164
+ beta_value == Qundef) {
165
+ rb_raise(rb_eArgError, "beta is required for %s", type_info->name);
166
+ }
167
+
104
168
  integration_function_init(&function, &params);
105
169
  fixed_params.function = &function;
170
+ fixed_params.type = *type_info->type;
106
171
  fixed_params.order = order;
107
172
  fixed_params.a = NUM2DBL(keyword_values[1]);
108
173
  fixed_params.b = NUM2DBL(keyword_values[2]);
109
- if (fixed_params.type == gsl_integration_fixed_gegenbauer) {
110
- if (alpha_value == Qundef) {
111
- rb_raise(rb_eArgError, "alpha is required for gegenbauer");
112
- }
113
- fixed_params.alpha = NUM2DBL(alpha_value);
114
- fixed_params.beta = 0.0;
115
- } else if (fixed_params.type == gsl_integration_fixed_jacobi) {
116
- if (alpha_value == Qundef) {
117
- rb_raise(rb_eArgError, "alpha is required for jacobi");
118
- }
119
- if (beta_value == Qundef) {
120
- rb_raise(rb_eArgError, "beta is required for jacobi");
121
- }
122
- fixed_params.alpha = NUM2DBL(alpha_value);
123
- fixed_params.beta = NUM2DBL(beta_value);
124
- } else if (fixed_params.type == gsl_integration_fixed_laguerre) {
125
- if (alpha_value == Qundef) {
126
- rb_raise(rb_eArgError, "alpha is required for laguerre");
127
- }
128
- fixed_params.alpha = NUM2DBL(alpha_value);
129
- fixed_params.beta = 0.0;
130
- } else if (fixed_params.type == gsl_integration_fixed_hermite) {
131
- if (alpha_value == Qundef) {
132
- rb_raise(rb_eArgError, "alpha is required for hermite");
133
- }
134
- fixed_params.alpha = NUM2DBL(alpha_value);
135
- fixed_params.beta = 0.0;
136
- } else if (fixed_params.type == gsl_integration_fixed_exponential) {
137
- if (alpha_value == Qundef) {
138
- rb_raise(rb_eArgError, "alpha is required for exponential");
139
- }
140
- fixed_params.alpha = NUM2DBL(alpha_value);
141
- fixed_params.beta = 0.0;
142
- } else if (fixed_params.type == gsl_integration_fixed_rational) {
143
- if (alpha_value == Qundef) {
144
- rb_raise(rb_eArgError, "alpha is required for rational");
145
- }
146
- if (beta_value == Qundef) {
147
- rb_raise(rb_eArgError, "beta is required for rational");
148
- }
149
- fixed_params.alpha = NUM2DBL(alpha_value);
150
- fixed_params.beta = NUM2DBL(beta_value);
151
- } else {
152
- fixed_params.alpha = 0.0;
153
- fixed_params.beta = 0.0;
154
- }
155
- fixed_params.workspace = NULL;
174
+ fixed_params.alpha =
175
+ (type_info->requirements & INTEGRATION_FIXED_REQUIRES_ALPHA) != 0
176
+ ? NUM2DBL(alpha_value)
177
+ : 0.0;
178
+ fixed_params.beta =
179
+ (type_info->requirements & INTEGRATION_FIXED_REQUIRES_BETA) != 0
180
+ ? NUM2DBL(beta_value)
181
+ : 0.0;
156
182
  fixed_params.result = &result;
183
+
157
184
  rb_protect(integration_run_fixed_with_workspace, (VALUE)&fixed_params,
158
185
  &state);
159
186
  RB_GC_GUARD(params.block);
@@ -19,6 +19,7 @@ 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_fixed_init();
22
23
 
23
24
  rb_define_const(mIntegration, "GAUSS15", INT2NUM(GSL_INTEG_GAUSS15));
24
25
  rb_define_const(mIntegration, "GAUSS21", INT2NUM(GSL_INTEG_GAUSS21));
@@ -44,4 +45,5 @@ void Init_integration(void) {
44
45
  rb_define_singleton_method(mIntegration, "romberg", integration_romberg, -1);
45
46
  rb_define_singleton_method(mIntegration, "glfixed", integration_glfixed, -1);
46
47
  rb_define_singleton_method(mIntegration, "fixed", integration_fixed, -1);
48
+ rb_define_singleton_method(mIntegration, "lebedev", integration_lebedev, 1);
47
49
  }
@@ -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 {
@@ -35,6 +36,8 @@ VALUE integration_free_cquad_workspace(VALUE data);
35
36
  VALUE integration_free_romberg_workspace(VALUE data);
36
37
  VALUE integration_free_glfixed_table(VALUE data);
37
38
 
39
+ void integration_fixed_init(void);
40
+
38
41
  VALUE integration_qag(int argc, VALUE *argv, VALUE self);
39
42
  VALUE integration_qng(int argc, VALUE *argv, VALUE self);
40
43
  VALUE integration_qags(int argc, VALUE *argv, VALUE self);
@@ -50,5 +53,6 @@ VALUE integration_cquad(int argc, VALUE *argv, VALUE self);
50
53
  VALUE integration_romberg(int argc, VALUE *argv, VALUE self);
51
54
  VALUE integration_glfixed(int argc, VALUE *argv, VALUE self);
52
55
  VALUE integration_fixed(int argc, VALUE *argv, VALUE self);
56
+ VALUE integration_lebedev(VALUE self, VALUE n_value);
53
57
 
54
58
  #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
+ }
@@ -3,7 +3,7 @@
3
3
  module Numo
4
4
  module GSL
5
5
  module Integration
6
- VERSION = "0.3.0"
6
+ VERSION = "0.4.0"
7
7
  end
8
8
  end
9
9
  end
@@ -116,6 +116,9 @@ module Numo
116
116
  ?alpha: Numeric,
117
117
  ?beta: Numeric
118
118
  ) { (Float) -> Numeric } -> Float
119
+ def self.lebedev: (Integer n) {
120
+ (Float, Float, Float) -> Numeric
121
+ } -> Float
119
122
  end
120
123
  end
121
124
  end
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro SATOH
@@ -31,6 +31,7 @@ files:
31
31
  - ext/numo/gsl/integration/glfixed.c
32
32
  - ext/numo/gsl/integration/integration.c
33
33
  - ext/numo/gsl/integration/integration_internal.h
34
+ - ext/numo/gsl/integration/lebedev.c
34
35
  - ext/numo/gsl/integration/options.c
35
36
  - ext/numo/gsl/integration/qag.c
36
37
  - ext/numo/gsl/integration/qagi.c