numo-gsl-integration 0.7.0 → 0.8.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 +4 -4
- data/CHANGELOG.md +14 -1
- data/README.md +31 -1
- data/docs/development.md +2 -2
- data/docs/routines.md +47 -2
- data/ext/numo/gsl/integration/fixed.c +374 -80
- data/ext/numo/gsl/integration/integration.c +3 -2
- data/ext/numo/gsl/integration/integration_internal.h +3 -2
- data/ext/numo/gsl/integration/lebedev.c +239 -32
- data/lib/numo/gsl/integration/version.rb +1 -1
- data/sig/numo/gsl/integration.rbs +37 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d5e146e7e06e3bb2da8dd7c62790faa71e1cfce8eff9c95f017f94238590448e
|
|
4
|
+
data.tar.gz: ac9ed81668dad1c05995ac2cd31b78aa6ec2796179900fc1c48b27037c6df082
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0d0f605dcbfd2645e5593b0860a26084f4a60d9e06951d55d4a58d3f58dd124d08431325bc5888d9cda39b399f37f3a4ba53fc42a657f0af0bb37f08fa90d8ea
|
|
7
|
+
data.tar.gz: 0e379c5c20fbf8ad3312e961fcc75fce046d437602d91e7c3de675496783d72e5b8dd5081ae5bd666fc99ec33f98e7aec214e95d2792e4988cf6cb03ce03fa0f
|
data/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## [
|
|
3
|
+
## [0.8.0] - 2026-11-09
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Added `Numo::GSL::Integration::FixedWorkspace` for reusing fixed-point quadrature nodes and weights across calls.
|
|
8
|
+
- Added block-scoped workspace management with `FixedWorkspace.open`, explicit lifecycle methods with `close` and `closed?`, and accessors for the fixed quadrature parameters.
|
|
9
|
+
- Added `Numo::GSL::Integration::LebedevWorkspace` for reusing Lebedev spherical nodes and weights across calls, with block-scoped and explicit lifecycle management.
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- Added a `workspace:` keyword to `fixed`. The method accepts either a quadrature type with its complete rule parameters or a reusable `FixedWorkspace` by itself. Calls that omit `workspace:` retain the previous allocate-per-call behavior.
|
|
14
|
+
- Rejected calls that combine `workspace:` with explicit fixed quadrature parameters or supply rule parameters without a quadrature type.
|
|
15
|
+
- Added a `workspace:` keyword to `lebedev`. The method accepts either a node count or a reusable `LebedevWorkspace` by itself; calls that omit `workspace:` retain the previous allocate-per-call behavior.
|
|
16
|
+
- Rejected `lebedev` calls that combine a node count with `workspace:`.
|
|
4
17
|
|
|
5
18
|
## [0.7.0] - 2026-11-08
|
|
6
19
|
|
data/README.md
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
- [Ruby](https://www.ruby-lang.org/) 3.2 or later
|
|
8
8
|
- GNU Scientific Library (GSL) 2.8 or later
|
|
9
9
|
- `gsl-config` available in `PATH`
|
|
10
|
-
|
|
10
|
+
|
|
11
|
+
GSL itself must be installed separately.
|
|
11
12
|
|
|
12
13
|
## Installation
|
|
13
14
|
|
|
@@ -61,6 +62,35 @@ Reusable `Workspace` objects are supported by `qag`, `qags`, `qagp`, `qagi`, `qa
|
|
|
61
62
|
|
|
62
63
|
For `qawf`, pass separate `workspace:` and `cycle_workspace:` objects. The first retains Fourier-cycle results and extrapolation state, while the second is used for QAWO integration within each cycle.
|
|
63
64
|
|
|
65
|
+
Fixed point quadrature can reuse precomputed nodes and weights through `Numo::GSL::Integration::FixedWorkspace`. Create it with the quadrature type and all rule parameters, then call `fixed` with only `workspace:`:
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
Numo::GSL::Integration::FixedWorkspace.open(
|
|
69
|
+
:legendre,
|
|
70
|
+
order: 10,
|
|
71
|
+
a: 0.0,
|
|
72
|
+
b: Math::PI
|
|
73
|
+
) do |workspace|
|
|
74
|
+
result = Numo::GSL::Integration.fixed(workspace: workspace) do |x|
|
|
75
|
+
Math.sin(x)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`fixed` accepts either a quadrature type with its complete rule parameters or a `workspace:` by itself. These two forms cannot be combined.
|
|
81
|
+
|
|
82
|
+
Lebedev quadrature can reuse its spherical nodes and weights through `Numo::GSL::Integration::LebedevWorkspace`. Create it with a supported node count, then call `lebedev` with only `workspace:`:
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
Numo::GSL::Integration::LebedevWorkspace.open(n: 26) do |workspace|
|
|
86
|
+
result = Numo::GSL::Integration.lebedev(workspace: workspace) do |x, y, z|
|
|
87
|
+
(x * x) + (y * y) + (z * z)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`lebedev` accepts either a node count or a `workspace:` by itself. These two forms cannot be combined.
|
|
93
|
+
|
|
64
94
|
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
95
|
|
|
66
96
|
## Supported routines
|
data/docs/development.md
CHANGED
|
@@ -28,7 +28,7 @@ The default Rake task performs the following checks:
|
|
|
28
28
|
- validates RBS signatures;
|
|
29
29
|
- checks Ruby files with [rubyfmt](https://github.com/fables-tales/rubyfmt);
|
|
30
30
|
- checks C source files with `clang-format` using the LLVM style; and
|
|
31
|
-
- checks
|
|
31
|
+
- checks Markdown files under the project root and `docs` with Prettier and `markdownlint-cli2`.
|
|
32
32
|
|
|
33
33
|
Check source formatting without applying changes:
|
|
34
34
|
|
|
@@ -36,7 +36,7 @@ Check source formatting without applying changes:
|
|
|
36
36
|
bundle exec rake format:check
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
Format the Ruby and
|
|
39
|
+
Format the Ruby, C, and Markdown files:
|
|
40
40
|
|
|
41
41
|
```bash
|
|
42
42
|
bundle exec rake format:apply
|
data/docs/routines.md
CHANGED
|
@@ -451,8 +451,10 @@ Numo::GSL::Integration::CQUADWorkspace.open(limit: 100) do |workspace|
|
|
|
451
451
|
end
|
|
452
452
|
```
|
|
453
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
|
-
|
|
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
|
+
|
|
456
|
+
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.
|
|
457
|
+
|
|
456
458
|
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
459
|
|
|
458
460
|
## Romberg integration
|
|
@@ -547,6 +549,30 @@ end
|
|
|
547
549
|
|
|
548
550
|
The example has an exact value of `1.0 / 5.0`. The block returns the unweighted function. For weighted rules, GSL applies the selected weight internally.
|
|
549
551
|
|
|
552
|
+
To reuse the nodes and weights across calls, create a `Numo::GSL::Integration::FixedWorkspace` with the quadrature type and all rule parameters:
|
|
553
|
+
|
|
554
|
+
```ruby
|
|
555
|
+
Numo::GSL::Integration::FixedWorkspace.open(
|
|
556
|
+
:legendre,
|
|
557
|
+
order: 10,
|
|
558
|
+
a: 0.0,
|
|
559
|
+
b: Math::PI
|
|
560
|
+
) do |workspace|
|
|
561
|
+
first_result = Numo::GSL::Integration.fixed(
|
|
562
|
+
workspace: workspace
|
|
563
|
+
) { |x| Math.sin(x) }
|
|
564
|
+
second_result = Numo::GSL::Integration.fixed(
|
|
565
|
+
workspace: workspace
|
|
566
|
+
) { |x| Math.cos(x) }
|
|
567
|
+
end
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
`FixedWorkspace#type`, `#order`, `#a`, and `#b` return the parameters fixed at allocation. `#alpha` and `#beta` return their fixed values when the selected rule uses them, and `nil` otherwise. 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.
|
|
571
|
+
|
|
572
|
+
A fixed 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`.
|
|
573
|
+
|
|
574
|
+
`fixed` accepts exactly two call forms: either supply the quadrature type with the complete rule parameters, or supply only `workspace:`. Explicit rule parameters and `workspace:` cannot be combined. Supplying `order:`, `a:`, `b:`, `alpha:`, or `beta:` without a quadrature type is also rejected. When `workspace:` is omitted, `fixed` allocates and releases a temporary GSL workspace for the call.
|
|
575
|
+
|
|
550
576
|
The supported types and required keyword arguments are:
|
|
551
577
|
|
|
552
578
|
| Type | Required rule parameters | Description |
|
|
@@ -592,6 +618,25 @@ end
|
|
|
592
618
|
|
|
593
619
|
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`.
|
|
594
620
|
|
|
621
|
+
To reuse the spherical nodes and weights across calls, create a `Numo::GSL::Integration::LebedevWorkspace` with a supported node count:
|
|
622
|
+
|
|
623
|
+
```ruby
|
|
624
|
+
Numo::GSL::Integration::LebedevWorkspace.open(n: 26) do |workspace|
|
|
625
|
+
first_result = Numo::GSL::Integration.lebedev(
|
|
626
|
+
workspace: workspace
|
|
627
|
+
) { |x, _y, _z| x * x }
|
|
628
|
+
second_result = Numo::GSL::Integration.lebedev(
|
|
629
|
+
workspace: workspace
|
|
630
|
+
) { |_x, y, _z| y * y }
|
|
631
|
+
end
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
`LebedevWorkspace#n` returns the node count fixed at allocation. 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.
|
|
635
|
+
|
|
636
|
+
A Lebedev 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`.
|
|
637
|
+
|
|
638
|
+
`lebedev` accepts exactly two call forms: either supply a node count, or supply only `workspace:`. A node count and `workspace:` cannot be combined. When `workspace:` is omitted, `lebedev` allocates and releases a temporary GSL workspace for the call.
|
|
639
|
+
|
|
595
640
|
The argument is the number of Lebedev nodes. GSL 2.8 provides the following node counts:
|
|
596
641
|
|
|
597
642
|
```text
|
|
@@ -13,17 +13,38 @@ typedef struct {
|
|
|
13
13
|
} integration_fixed_type_info;
|
|
14
14
|
|
|
15
15
|
typedef struct {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
const integration_fixed_type_info *type_info;
|
|
17
|
+
size_t order;
|
|
18
|
+
double a;
|
|
19
|
+
double b;
|
|
20
|
+
double alpha;
|
|
21
|
+
double beta;
|
|
22
|
+
} integration_fixed_config;
|
|
23
|
+
|
|
24
|
+
typedef struct {
|
|
25
|
+
gsl_integration_fixed_workspace *workspace;
|
|
26
|
+
const integration_fixed_type_info *type_info;
|
|
18
27
|
size_t order;
|
|
19
28
|
double a;
|
|
20
29
|
double b;
|
|
21
30
|
double alpha;
|
|
22
31
|
double beta;
|
|
32
|
+
int busy;
|
|
33
|
+
} integration_fixed_workspace;
|
|
34
|
+
|
|
35
|
+
typedef struct {
|
|
36
|
+
gsl_integration_fixed_workspace *workspace;
|
|
37
|
+
integration_fixed_workspace *owner;
|
|
38
|
+
} integration_fixed_workspace_use;
|
|
39
|
+
|
|
40
|
+
typedef struct {
|
|
41
|
+
gsl_function *function;
|
|
23
42
|
gsl_integration_fixed_workspace *workspace;
|
|
24
43
|
double *result;
|
|
25
44
|
} integration_fixed_params;
|
|
26
45
|
|
|
46
|
+
static VALUE integration_cFixedWorkspace;
|
|
47
|
+
|
|
27
48
|
static integration_fixed_type_info integration_fixed_types[] = {
|
|
28
49
|
{0, "legendre", &gsl_integration_fixed_legendre, 0},
|
|
29
50
|
{0, "chebyshev", &gsl_integration_fixed_chebyshev, 0},
|
|
@@ -47,25 +68,9 @@ static ID integration_fixed_id_a;
|
|
|
47
68
|
static ID integration_fixed_id_b;
|
|
48
69
|
static ID integration_fixed_id_alpha;
|
|
49
70
|
static ID integration_fixed_id_beta;
|
|
71
|
+
static ID integration_fixed_id_workspace;
|
|
50
72
|
static ID integration_fixed_id_less_than_or_equal;
|
|
51
73
|
|
|
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
74
|
static const integration_fixed_type_info *integration_fixed_type(VALUE type) {
|
|
70
75
|
ID type_id;
|
|
71
76
|
size_t i;
|
|
@@ -87,104 +92,393 @@ static const integration_fixed_type_info *integration_fixed_type(VALUE type) {
|
|
|
87
92
|
rb_id2name(type_id));
|
|
88
93
|
}
|
|
89
94
|
|
|
90
|
-
static
|
|
91
|
-
|
|
95
|
+
static void integration_fixed_config_parse(VALUE type, VALUE order_value,
|
|
96
|
+
VALUE a_value, VALUE b_value,
|
|
97
|
+
VALUE alpha_value, VALUE beta_value,
|
|
98
|
+
integration_fixed_config *config) {
|
|
99
|
+
const integration_fixed_type_info *type_info = integration_fixed_type(type);
|
|
92
100
|
|
|
93
|
-
if (
|
|
94
|
-
|
|
101
|
+
if (order_value == Qundef) {
|
|
102
|
+
rb_raise(rb_eArgError, "missing keyword: :order");
|
|
95
103
|
}
|
|
96
|
-
|
|
104
|
+
if (a_value == Qundef) {
|
|
105
|
+
rb_raise(rb_eArgError, "missing keyword: :a");
|
|
106
|
+
}
|
|
107
|
+
if (b_value == Qundef) {
|
|
108
|
+
rb_raise(rb_eArgError, "missing keyword: :b");
|
|
109
|
+
}
|
|
110
|
+
if (!RB_INTEGER_TYPE_P(order_value)) {
|
|
111
|
+
rb_raise(rb_eTypeError, "order must be an Integer");
|
|
112
|
+
}
|
|
113
|
+
if (RTEST(rb_funcall(order_value, integration_fixed_id_less_than_or_equal, 1,
|
|
114
|
+
INT2FIX(0)))) {
|
|
115
|
+
rb_raise(rb_eArgError, "order must be positive");
|
|
116
|
+
}
|
|
117
|
+
if ((type_info->requirements & INTEGRATION_FIXED_REQUIRES_ALPHA) != 0 &&
|
|
118
|
+
alpha_value == Qundef) {
|
|
119
|
+
rb_raise(rb_eArgError, "alpha is required for %s", type_info->name);
|
|
120
|
+
}
|
|
121
|
+
if ((type_info->requirements & INTEGRATION_FIXED_REQUIRES_BETA) != 0 &&
|
|
122
|
+
beta_value == Qundef) {
|
|
123
|
+
rb_raise(rb_eArgError, "beta is required for %s", type_info->name);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
config->type_info = type_info;
|
|
127
|
+
config->order = NUM2SIZET(order_value);
|
|
128
|
+
config->a = NUM2DBL(a_value);
|
|
129
|
+
config->b = NUM2DBL(b_value);
|
|
130
|
+
config->alpha =
|
|
131
|
+
(type_info->requirements & INTEGRATION_FIXED_REQUIRES_ALPHA) != 0
|
|
132
|
+
? NUM2DBL(alpha_value)
|
|
133
|
+
: 0.0;
|
|
134
|
+
config->beta =
|
|
135
|
+
(type_info->requirements & INTEGRATION_FIXED_REQUIRES_BETA) != 0
|
|
136
|
+
? NUM2DBL(beta_value)
|
|
137
|
+
: 0.0;
|
|
97
138
|
}
|
|
98
139
|
|
|
99
|
-
static
|
|
100
|
-
|
|
140
|
+
static void integration_fixed_workspace_free(void *data) {
|
|
141
|
+
integration_fixed_workspace *workspace = data;
|
|
101
142
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
params->b, params->alpha, params->beta);
|
|
105
|
-
if (params->workspace == NULL) {
|
|
106
|
-
rb_memerror();
|
|
143
|
+
if (workspace->workspace != NULL) {
|
|
144
|
+
gsl_integration_fixed_free(workspace->workspace);
|
|
107
145
|
}
|
|
146
|
+
xfree(workspace);
|
|
147
|
+
}
|
|
108
148
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return Qnil;
|
|
149
|
+
static size_t integration_fixed_workspace_memsize(const void *data) {
|
|
150
|
+
return data == NULL ? 0 : sizeof(integration_fixed_workspace);
|
|
112
151
|
}
|
|
113
152
|
|
|
114
|
-
static
|
|
115
|
-
|
|
116
|
-
|
|
153
|
+
static const rb_data_type_t integration_fixed_workspace_type = {
|
|
154
|
+
.wrap_struct_name = "Numo::GSL::Integration::FixedWorkspace",
|
|
155
|
+
.function =
|
|
156
|
+
{
|
|
157
|
+
.dmark = NULL,
|
|
158
|
+
.dfree = integration_fixed_workspace_free,
|
|
159
|
+
.dsize = integration_fixed_workspace_memsize,
|
|
160
|
+
},
|
|
161
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
static VALUE integration_fixed_workspace_allocate(VALUE klass) {
|
|
165
|
+
integration_fixed_workspace *workspace;
|
|
166
|
+
VALUE object =
|
|
167
|
+
TypedData_Make_Struct(klass, integration_fixed_workspace,
|
|
168
|
+
&integration_fixed_workspace_type, workspace);
|
|
169
|
+
|
|
170
|
+
workspace->workspace = NULL;
|
|
171
|
+
workspace->type_info = NULL;
|
|
172
|
+
workspace->order = 0;
|
|
173
|
+
workspace->a = 0.0;
|
|
174
|
+
workspace->b = 0.0;
|
|
175
|
+
workspace->alpha = 0.0;
|
|
176
|
+
workspace->beta = 0.0;
|
|
177
|
+
workspace->busy = 0;
|
|
178
|
+
return object;
|
|
117
179
|
}
|
|
118
180
|
|
|
119
|
-
VALUE
|
|
181
|
+
static VALUE integration_fixed_workspace_initialize(int argc, VALUE *argv,
|
|
182
|
+
VALUE self) {
|
|
120
183
|
VALUE type;
|
|
121
184
|
VALUE options;
|
|
122
185
|
ID keywords[] = {integration_fixed_id_order, integration_fixed_id_a,
|
|
123
186
|
integration_fixed_id_b, integration_fixed_id_alpha,
|
|
124
187
|
integration_fixed_id_beta};
|
|
125
188
|
VALUE keyword_values[5];
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
189
|
+
integration_fixed_config config;
|
|
190
|
+
gsl_integration_fixed_workspace *native_workspace;
|
|
191
|
+
integration_fixed_workspace *workspace;
|
|
192
|
+
|
|
193
|
+
rb_scan_args(argc, argv, "1:", &type, &options);
|
|
194
|
+
if (NIL_P(options)) {
|
|
195
|
+
options = rb_hash_new();
|
|
196
|
+
}
|
|
197
|
+
rb_get_kwargs(options, keywords, 0, 5, keyword_values);
|
|
198
|
+
integration_fixed_config_parse(type, keyword_values[0], keyword_values[1],
|
|
199
|
+
keyword_values[2], keyword_values[3],
|
|
200
|
+
keyword_values[4], &config);
|
|
201
|
+
|
|
202
|
+
TypedData_Get_Struct(self, integration_fixed_workspace,
|
|
203
|
+
&integration_fixed_workspace_type, workspace);
|
|
204
|
+
if (workspace->workspace != NULL) {
|
|
205
|
+
rb_raise(rb_eRuntimeError, "workspace is already initialized");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
native_workspace = gsl_integration_fixed_alloc(
|
|
209
|
+
*config.type_info->type, config.order, config.a, config.b, config.alpha,
|
|
210
|
+
config.beta);
|
|
211
|
+
if (native_workspace == NULL) {
|
|
212
|
+
rb_memerror();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
workspace->workspace = native_workspace;
|
|
216
|
+
workspace->type_info = config.type_info;
|
|
217
|
+
workspace->order = config.order;
|
|
218
|
+
workspace->a = config.a;
|
|
219
|
+
workspace->b = config.b;
|
|
220
|
+
workspace->alpha = config.alpha;
|
|
221
|
+
workspace->beta = config.beta;
|
|
222
|
+
return self;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
static VALUE integration_fixed_workspace_type_value(VALUE self) {
|
|
226
|
+
integration_fixed_workspace *workspace;
|
|
227
|
+
|
|
228
|
+
TypedData_Get_Struct(self, integration_fixed_workspace,
|
|
229
|
+
&integration_fixed_workspace_type, workspace);
|
|
230
|
+
return ID2SYM(workspace->type_info->id);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
static VALUE integration_fixed_workspace_order(VALUE self) {
|
|
234
|
+
integration_fixed_workspace *workspace;
|
|
235
|
+
|
|
236
|
+
TypedData_Get_Struct(self, integration_fixed_workspace,
|
|
237
|
+
&integration_fixed_workspace_type, workspace);
|
|
238
|
+
return SIZET2NUM(workspace->order);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static VALUE integration_fixed_workspace_a(VALUE self) {
|
|
242
|
+
integration_fixed_workspace *workspace;
|
|
243
|
+
|
|
244
|
+
TypedData_Get_Struct(self, integration_fixed_workspace,
|
|
245
|
+
&integration_fixed_workspace_type, workspace);
|
|
246
|
+
return DBL2NUM(workspace->a);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
static VALUE integration_fixed_workspace_b(VALUE self) {
|
|
250
|
+
integration_fixed_workspace *workspace;
|
|
251
|
+
|
|
252
|
+
TypedData_Get_Struct(self, integration_fixed_workspace,
|
|
253
|
+
&integration_fixed_workspace_type, workspace);
|
|
254
|
+
return DBL2NUM(workspace->b);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
static VALUE integration_fixed_workspace_alpha(VALUE self) {
|
|
258
|
+
integration_fixed_workspace *workspace;
|
|
259
|
+
|
|
260
|
+
TypedData_Get_Struct(self, integration_fixed_workspace,
|
|
261
|
+
&integration_fixed_workspace_type, workspace);
|
|
262
|
+
if ((workspace->type_info->requirements & INTEGRATION_FIXED_REQUIRES_ALPHA) ==
|
|
263
|
+
0) {
|
|
264
|
+
return Qnil;
|
|
265
|
+
}
|
|
266
|
+
return DBL2NUM(workspace->alpha);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
static VALUE integration_fixed_workspace_beta(VALUE self) {
|
|
270
|
+
integration_fixed_workspace *workspace;
|
|
271
|
+
|
|
272
|
+
TypedData_Get_Struct(self, integration_fixed_workspace,
|
|
273
|
+
&integration_fixed_workspace_type, workspace);
|
|
274
|
+
if ((workspace->type_info->requirements & INTEGRATION_FIXED_REQUIRES_BETA) ==
|
|
275
|
+
0) {
|
|
276
|
+
return Qnil;
|
|
277
|
+
}
|
|
278
|
+
return DBL2NUM(workspace->beta);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
static VALUE integration_fixed_workspace_closed_p(VALUE self) {
|
|
282
|
+
integration_fixed_workspace *workspace;
|
|
283
|
+
|
|
284
|
+
TypedData_Get_Struct(self, integration_fixed_workspace,
|
|
285
|
+
&integration_fixed_workspace_type, workspace);
|
|
286
|
+
return workspace->workspace == NULL ? Qtrue : Qfalse;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
static VALUE integration_fixed_workspace_close(VALUE self) {
|
|
290
|
+
integration_fixed_workspace *workspace;
|
|
291
|
+
|
|
292
|
+
TypedData_Get_Struct(self, integration_fixed_workspace,
|
|
293
|
+
&integration_fixed_workspace_type, workspace);
|
|
294
|
+
if (workspace->busy) {
|
|
295
|
+
rb_raise(rb_eRuntimeError, "workspace is already in use");
|
|
296
|
+
}
|
|
297
|
+
if (workspace->workspace != NULL) {
|
|
298
|
+
gsl_integration_fixed_free(workspace->workspace);
|
|
299
|
+
workspace->workspace = NULL;
|
|
300
|
+
}
|
|
301
|
+
return Qnil;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
static VALUE integration_fixed_workspace_yield(VALUE workspace) {
|
|
305
|
+
return rb_yield(workspace);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
static VALUE integration_fixed_workspace_open(int argc, VALUE *argv,
|
|
309
|
+
VALUE klass) {
|
|
310
|
+
VALUE workspace =
|
|
311
|
+
rb_funcallv_kw(klass, rb_intern("new"), argc, argv, RB_PASS_KEYWORDS);
|
|
312
|
+
return rb_ensure(integration_fixed_workspace_yield, workspace,
|
|
313
|
+
integration_fixed_workspace_close, workspace);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
static integration_fixed_workspace *
|
|
317
|
+
integration_fixed_workspace_get(VALUE workspace_value) {
|
|
318
|
+
integration_fixed_workspace *workspace;
|
|
319
|
+
|
|
320
|
+
if (!rb_obj_is_kind_of(workspace_value, integration_cFixedWorkspace)) {
|
|
321
|
+
rb_raise(rb_eTypeError,
|
|
322
|
+
"workspace must be a Numo::GSL::Integration::FixedWorkspace");
|
|
323
|
+
}
|
|
324
|
+
TypedData_Get_Struct(workspace_value, integration_fixed_workspace,
|
|
325
|
+
&integration_fixed_workspace_type, workspace);
|
|
326
|
+
if (workspace->workspace == NULL) {
|
|
327
|
+
rb_raise(rb_eRuntimeError, "workspace is closed");
|
|
328
|
+
}
|
|
329
|
+
if (workspace->busy) {
|
|
330
|
+
rb_raise(rb_eRuntimeError, "workspace is already in use");
|
|
331
|
+
}
|
|
332
|
+
return workspace;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
static void
|
|
336
|
+
integration_fixed_workspace_use_begin(VALUE workspace_value,
|
|
337
|
+
const integration_fixed_config *config,
|
|
338
|
+
integration_fixed_workspace_use *use) {
|
|
339
|
+
integration_fixed_workspace *workspace;
|
|
340
|
+
|
|
341
|
+
use->workspace = NULL;
|
|
342
|
+
use->owner = NULL;
|
|
343
|
+
|
|
344
|
+
if (workspace_value == Qundef) {
|
|
345
|
+
use->workspace = gsl_integration_fixed_alloc(
|
|
346
|
+
*config->type_info->type, config->order, config->a, config->b,
|
|
347
|
+
config->alpha, config->beta);
|
|
348
|
+
if (use->workspace == NULL) {
|
|
349
|
+
rb_memerror();
|
|
350
|
+
}
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
workspace = integration_fixed_workspace_get(workspace_value);
|
|
355
|
+
workspace->busy = 1;
|
|
356
|
+
use->workspace = workspace->workspace;
|
|
357
|
+
use->owner = workspace;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
static VALUE integration_fixed_workspace_use_end(VALUE data) {
|
|
361
|
+
integration_fixed_workspace_use *use =
|
|
362
|
+
(integration_fixed_workspace_use *)data;
|
|
363
|
+
|
|
364
|
+
if (use->owner != NULL) {
|
|
365
|
+
use->owner->busy = 0;
|
|
366
|
+
} else if (use->workspace != NULL) {
|
|
367
|
+
gsl_integration_fixed_free(use->workspace);
|
|
368
|
+
}
|
|
369
|
+
use->workspace = NULL;
|
|
370
|
+
use->owner = NULL;
|
|
371
|
+
return Qnil;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
static VALUE integration_run_fixed(VALUE data) {
|
|
375
|
+
integration_fixed_params *params = (integration_fixed_params *)data;
|
|
376
|
+
|
|
377
|
+
(void)gsl_integration_fixed(params->function, params->result,
|
|
378
|
+
params->workspace);
|
|
379
|
+
return Qnil;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
VALUE integration_fixed(int argc, VALUE *argv, VALUE self) {
|
|
383
|
+
VALUE type = Qundef;
|
|
384
|
+
VALUE options;
|
|
385
|
+
VALUE workspace_value = Qundef;
|
|
386
|
+
ID keywords[] = {integration_fixed_id_order, integration_fixed_id_a,
|
|
387
|
+
integration_fixed_id_b, integration_fixed_id_alpha,
|
|
388
|
+
integration_fixed_id_beta, integration_fixed_id_workspace};
|
|
389
|
+
VALUE keyword_values[6];
|
|
390
|
+
integration_fixed_config config = {0};
|
|
391
|
+
int positional_count;
|
|
392
|
+
int config_given;
|
|
131
393
|
gsl_function function;
|
|
132
394
|
integration_callback_params params;
|
|
133
|
-
integration_fixed_params fixed_params
|
|
395
|
+
integration_fixed_params fixed_params;
|
|
396
|
+
integration_fixed_workspace_use workspace_use;
|
|
134
397
|
double result;
|
|
135
398
|
int state = 0;
|
|
136
399
|
|
|
137
400
|
(void)self;
|
|
138
|
-
rb_scan_args(argc, argv, "
|
|
139
|
-
type_info = integration_fixed_type(type);
|
|
140
|
-
|
|
401
|
+
positional_count = rb_scan_args(argc, argv, "01:", &type, &options);
|
|
141
402
|
if (NIL_P(options)) {
|
|
142
403
|
options = rb_hash_new();
|
|
143
404
|
}
|
|
144
|
-
rb_get_kwargs(options, keywords,
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
alpha_value = keyword_values[3];
|
|
148
|
-
beta_value = keyword_values[4];
|
|
405
|
+
rb_get_kwargs(options, keywords, 0, 6, keyword_values);
|
|
406
|
+
workspace_value = keyword_values[5];
|
|
407
|
+
config_given = positional_count == 1;
|
|
149
408
|
|
|
150
|
-
if (!
|
|
151
|
-
rb_raise(
|
|
409
|
+
if (!config_given && workspace_value == Qundef) {
|
|
410
|
+
rb_raise(rb_eArgError, "wrong number of arguments (given 0, expected 1)");
|
|
152
411
|
}
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
412
|
+
if (config_given && workspace_value != Qundef) {
|
|
413
|
+
rb_raise(rb_eArgError,
|
|
414
|
+
"workspace cannot be specified with fixed quadrature parameters");
|
|
156
415
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
rb_raise(rb_eArgError,
|
|
416
|
+
if (!config_given &&
|
|
417
|
+
(keyword_values[0] != Qundef || keyword_values[1] != Qundef ||
|
|
418
|
+
keyword_values[2] != Qundef || keyword_values[3] != Qundef ||
|
|
419
|
+
keyword_values[4] != Qundef)) {
|
|
420
|
+
rb_raise(rb_eArgError,
|
|
421
|
+
"type is required when fixed quadrature parameters are specified");
|
|
162
422
|
}
|
|
163
|
-
if (
|
|
164
|
-
|
|
165
|
-
|
|
423
|
+
if (config_given) {
|
|
424
|
+
integration_fixed_config_parse(type, keyword_values[0], keyword_values[1],
|
|
425
|
+
keyword_values[2], keyword_values[3],
|
|
426
|
+
keyword_values[4], &config);
|
|
166
427
|
}
|
|
167
428
|
|
|
168
429
|
integration_function_init(&function, ¶ms);
|
|
430
|
+
integration_fixed_workspace_use_begin(workspace_value, &config,
|
|
431
|
+
&workspace_use);
|
|
169
432
|
fixed_params.function = &function;
|
|
170
|
-
fixed_params.
|
|
171
|
-
fixed_params.order = order;
|
|
172
|
-
fixed_params.a = NUM2DBL(keyword_values[1]);
|
|
173
|
-
fixed_params.b = NUM2DBL(keyword_values[2]);
|
|
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;
|
|
433
|
+
fixed_params.workspace = workspace_use.workspace;
|
|
182
434
|
fixed_params.result = &result;
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
435
|
+
rb_protect(integration_run_fixed, (VALUE)&fixed_params, &state);
|
|
436
|
+
integration_fixed_workspace_use_end((VALUE)&workspace_use);
|
|
437
|
+
RB_GC_GUARD(workspace_value);
|
|
186
438
|
RB_GC_GUARD(params.block);
|
|
187
439
|
RB_GC_GUARD(params.exception);
|
|
188
440
|
integration_finish_callback(¶ms, state);
|
|
189
441
|
return DBL2NUM(result);
|
|
190
442
|
}
|
|
443
|
+
|
|
444
|
+
void integration_fixed_init(VALUE integration_module) {
|
|
445
|
+
size_t i;
|
|
446
|
+
|
|
447
|
+
for (i = 0;
|
|
448
|
+
i < sizeof(integration_fixed_types) / sizeof(integration_fixed_types[0]);
|
|
449
|
+
++i) {
|
|
450
|
+
integration_fixed_types[i].id = rb_intern(integration_fixed_types[i].name);
|
|
451
|
+
}
|
|
452
|
+
integration_fixed_id_order = rb_intern("order");
|
|
453
|
+
integration_fixed_id_a = rb_intern("a");
|
|
454
|
+
integration_fixed_id_b = rb_intern("b");
|
|
455
|
+
integration_fixed_id_alpha = rb_intern("alpha");
|
|
456
|
+
integration_fixed_id_beta = rb_intern("beta");
|
|
457
|
+
integration_fixed_id_workspace = rb_intern("workspace");
|
|
458
|
+
integration_fixed_id_less_than_or_equal = rb_intern("<=");
|
|
459
|
+
|
|
460
|
+
integration_cFixedWorkspace =
|
|
461
|
+
rb_define_class_under(integration_module, "FixedWorkspace", rb_cObject);
|
|
462
|
+
rb_define_alloc_func(integration_cFixedWorkspace,
|
|
463
|
+
integration_fixed_workspace_allocate);
|
|
464
|
+
rb_define_method(integration_cFixedWorkspace, "initialize",
|
|
465
|
+
integration_fixed_workspace_initialize, -1);
|
|
466
|
+
rb_define_method(integration_cFixedWorkspace, "type",
|
|
467
|
+
integration_fixed_workspace_type_value, 0);
|
|
468
|
+
rb_define_method(integration_cFixedWorkspace, "order",
|
|
469
|
+
integration_fixed_workspace_order, 0);
|
|
470
|
+
rb_define_method(integration_cFixedWorkspace, "a",
|
|
471
|
+
integration_fixed_workspace_a, 0);
|
|
472
|
+
rb_define_method(integration_cFixedWorkspace, "b",
|
|
473
|
+
integration_fixed_workspace_b, 0);
|
|
474
|
+
rb_define_method(integration_cFixedWorkspace, "alpha",
|
|
475
|
+
integration_fixed_workspace_alpha, 0);
|
|
476
|
+
rb_define_method(integration_cFixedWorkspace, "beta",
|
|
477
|
+
integration_fixed_workspace_beta, 0);
|
|
478
|
+
rb_define_method(integration_cFixedWorkspace, "closed?",
|
|
479
|
+
integration_fixed_workspace_closed_p, 0);
|
|
480
|
+
rb_define_method(integration_cFixedWorkspace, "close",
|
|
481
|
+
integration_fixed_workspace_close, 0);
|
|
482
|
+
rb_define_singleton_method(integration_cFixedWorkspace, "open",
|
|
483
|
+
integration_fixed_workspace_open, -1);
|
|
484
|
+
}
|
|
@@ -31,7 +31,8 @@ void Init_integration(void) {
|
|
|
31
31
|
integration_qawo_table_init(mIntegration);
|
|
32
32
|
integration_qawf_table_init(mIntegration);
|
|
33
33
|
integration_qaws_table_init(mIntegration);
|
|
34
|
-
integration_fixed_init();
|
|
34
|
+
integration_fixed_init(mIntegration);
|
|
35
|
+
integration_lebedev_init(mIntegration);
|
|
35
36
|
|
|
36
37
|
rb_define_const(mIntegration, "GAUSS15", INT2NUM(GSL_INTEG_GAUSS15));
|
|
37
38
|
rb_define_const(mIntegration, "GAUSS21", INT2NUM(GSL_INTEG_GAUSS21));
|
|
@@ -57,5 +58,5 @@ void Init_integration(void) {
|
|
|
57
58
|
rb_define_singleton_method(mIntegration, "romberg", integration_romberg, -1);
|
|
58
59
|
rb_define_singleton_method(mIntegration, "glfixed", integration_glfixed, -1);
|
|
59
60
|
rb_define_singleton_method(mIntegration, "fixed", integration_fixed, -1);
|
|
60
|
-
rb_define_singleton_method(mIntegration, "lebedev", integration_lebedev, 1);
|
|
61
|
+
rb_define_singleton_method(mIntegration, "lebedev", integration_lebedev, -1);
|
|
61
62
|
}
|
|
@@ -151,7 +151,8 @@ VALUE integration_free_cquad_workspace(VALUE data);
|
|
|
151
151
|
VALUE integration_free_romberg_workspace(VALUE data);
|
|
152
152
|
VALUE integration_free_glfixed_table(VALUE data);
|
|
153
153
|
|
|
154
|
-
void integration_fixed_init(
|
|
154
|
+
void integration_fixed_init(VALUE integration_module);
|
|
155
|
+
void integration_lebedev_init(VALUE integration_module);
|
|
155
156
|
|
|
156
157
|
VALUE integration_qag(int argc, VALUE *argv, VALUE self);
|
|
157
158
|
VALUE integration_qng(int argc, VALUE *argv, VALUE self);
|
|
@@ -168,6 +169,6 @@ VALUE integration_cquad(int argc, VALUE *argv, VALUE self);
|
|
|
168
169
|
VALUE integration_romberg(int argc, VALUE *argv, VALUE self);
|
|
169
170
|
VALUE integration_glfixed(int argc, VALUE *argv, VALUE self);
|
|
170
171
|
VALUE integration_fixed(int argc, VALUE *argv, VALUE self);
|
|
171
|
-
VALUE integration_lebedev(VALUE
|
|
172
|
+
VALUE integration_lebedev(int argc, VALUE *argv, VALUE self);
|
|
172
173
|
|
|
173
174
|
#endif
|
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
#include "integration_internal.h"
|
|
2
2
|
|
|
3
|
+
typedef struct {
|
|
4
|
+
gsl_integration_lebedev_workspace *workspace;
|
|
5
|
+
size_t n;
|
|
6
|
+
int busy;
|
|
7
|
+
} integration_lebedev_workspace;
|
|
8
|
+
|
|
9
|
+
typedef struct {
|
|
10
|
+
gsl_integration_lebedev_workspace *workspace;
|
|
11
|
+
integration_lebedev_workspace *owner;
|
|
12
|
+
} integration_lebedev_workspace_use;
|
|
13
|
+
|
|
3
14
|
typedef struct {
|
|
4
15
|
VALUE block;
|
|
5
16
|
VALUE x;
|
|
@@ -11,25 +22,195 @@ typedef struct {
|
|
|
11
22
|
int state;
|
|
12
23
|
} integration_lebedev_params;
|
|
13
24
|
|
|
14
|
-
static VALUE
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
25
|
+
static VALUE integration_cLebedevWorkspace;
|
|
26
|
+
static ID integration_lebedev_id_n;
|
|
27
|
+
static ID integration_lebedev_id_workspace;
|
|
28
|
+
static ID integration_lebedev_id_less_than_or_equal;
|
|
18
29
|
|
|
19
|
-
|
|
20
|
-
|
|
30
|
+
static size_t integration_lebedev_node_count(VALUE n_value) {
|
|
31
|
+
if (!RB_INTEGER_TYPE_P(n_value)) {
|
|
32
|
+
rb_raise(rb_eTypeError, "n must be an Integer");
|
|
33
|
+
}
|
|
34
|
+
if (RTEST(rb_funcall(n_value, integration_lebedev_id_less_than_or_equal, 1,
|
|
35
|
+
INT2FIX(0)))) {
|
|
36
|
+
rb_raise(rb_eArgError, "n must be positive");
|
|
37
|
+
}
|
|
38
|
+
return NUM2SIZET(n_value);
|
|
21
39
|
}
|
|
22
40
|
|
|
23
|
-
static
|
|
24
|
-
|
|
41
|
+
static void integration_lebedev_workspace_free(void *data) {
|
|
42
|
+
integration_lebedev_workspace *workspace = data;
|
|
43
|
+
|
|
44
|
+
if (workspace->workspace != NULL) {
|
|
45
|
+
gsl_integration_lebedev_free(workspace->workspace);
|
|
46
|
+
}
|
|
47
|
+
xfree(workspace);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static size_t integration_lebedev_workspace_memsize(const void *data) {
|
|
51
|
+
return data == NULL ? 0 : sizeof(integration_lebedev_workspace);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static const rb_data_type_t integration_lebedev_workspace_type = {
|
|
55
|
+
.wrap_struct_name = "Numo::GSL::Integration::LebedevWorkspace",
|
|
56
|
+
.function =
|
|
57
|
+
{
|
|
58
|
+
.dmark = NULL,
|
|
59
|
+
.dfree = integration_lebedev_workspace_free,
|
|
60
|
+
.dsize = integration_lebedev_workspace_memsize,
|
|
61
|
+
},
|
|
62
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
static VALUE integration_lebedev_workspace_allocate(VALUE klass) {
|
|
66
|
+
integration_lebedev_workspace *workspace;
|
|
67
|
+
VALUE object =
|
|
68
|
+
TypedData_Make_Struct(klass, integration_lebedev_workspace,
|
|
69
|
+
&integration_lebedev_workspace_type, workspace);
|
|
70
|
+
|
|
71
|
+
workspace->workspace = NULL;
|
|
72
|
+
workspace->n = 0;
|
|
73
|
+
workspace->busy = 0;
|
|
74
|
+
return object;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static VALUE integration_lebedev_workspace_initialize(int argc, VALUE *argv,
|
|
78
|
+
VALUE self) {
|
|
79
|
+
VALUE options;
|
|
80
|
+
ID keywords[] = {integration_lebedev_id_n};
|
|
81
|
+
VALUE keyword_values[1];
|
|
82
|
+
gsl_integration_lebedev_workspace *native_workspace;
|
|
83
|
+
integration_lebedev_workspace *workspace;
|
|
84
|
+
size_t n;
|
|
85
|
+
|
|
86
|
+
rb_scan_args(argc, argv, "0:", &options);
|
|
87
|
+
if (NIL_P(options)) {
|
|
88
|
+
rb_raise(rb_eArgError, "missing keyword: :n");
|
|
89
|
+
}
|
|
90
|
+
rb_get_kwargs(options, keywords, 1, 0, keyword_values);
|
|
91
|
+
n = integration_lebedev_node_count(keyword_values[0]);
|
|
92
|
+
|
|
93
|
+
TypedData_Get_Struct(self, integration_lebedev_workspace,
|
|
94
|
+
&integration_lebedev_workspace_type, workspace);
|
|
95
|
+
if (workspace->workspace != NULL) {
|
|
96
|
+
rb_raise(rb_eRuntimeError, "workspace is already initialized");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
native_workspace = gsl_integration_lebedev_alloc(n);
|
|
100
|
+
if (native_workspace == NULL) {
|
|
101
|
+
rb_memerror();
|
|
102
|
+
}
|
|
103
|
+
workspace->workspace = native_workspace;
|
|
104
|
+
workspace->n = n;
|
|
105
|
+
return self;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static VALUE integration_lebedev_workspace_n(VALUE self) {
|
|
109
|
+
integration_lebedev_workspace *workspace;
|
|
110
|
+
|
|
111
|
+
TypedData_Get_Struct(self, integration_lebedev_workspace,
|
|
112
|
+
&integration_lebedev_workspace_type, workspace);
|
|
113
|
+
return SIZET2NUM(workspace->n);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static VALUE integration_lebedev_workspace_closed_p(VALUE self) {
|
|
117
|
+
integration_lebedev_workspace *workspace;
|
|
118
|
+
|
|
119
|
+
TypedData_Get_Struct(self, integration_lebedev_workspace,
|
|
120
|
+
&integration_lebedev_workspace_type, workspace);
|
|
121
|
+
return workspace->workspace == NULL ? Qtrue : Qfalse;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static VALUE integration_lebedev_workspace_close(VALUE self) {
|
|
125
|
+
integration_lebedev_workspace *workspace;
|
|
126
|
+
|
|
127
|
+
TypedData_Get_Struct(self, integration_lebedev_workspace,
|
|
128
|
+
&integration_lebedev_workspace_type, workspace);
|
|
129
|
+
if (workspace->busy) {
|
|
130
|
+
rb_raise(rb_eRuntimeError, "workspace is already in use");
|
|
131
|
+
}
|
|
132
|
+
if (workspace->workspace != NULL) {
|
|
133
|
+
gsl_integration_lebedev_free(workspace->workspace);
|
|
134
|
+
workspace->workspace = NULL;
|
|
135
|
+
}
|
|
136
|
+
return Qnil;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
static VALUE integration_lebedev_workspace_yield(VALUE workspace) {
|
|
140
|
+
return rb_yield(workspace);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static VALUE integration_lebedev_workspace_open(int argc, VALUE *argv,
|
|
144
|
+
VALUE klass) {
|
|
145
|
+
VALUE workspace =
|
|
146
|
+
rb_funcallv_kw(klass, rb_intern("new"), argc, argv, RB_PASS_KEYWORDS);
|
|
147
|
+
return rb_ensure(integration_lebedev_workspace_yield, workspace,
|
|
148
|
+
integration_lebedev_workspace_close, workspace);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static integration_lebedev_workspace *
|
|
152
|
+
integration_lebedev_workspace_get(VALUE workspace_value) {
|
|
153
|
+
integration_lebedev_workspace *workspace;
|
|
154
|
+
|
|
155
|
+
if (!rb_obj_is_kind_of(workspace_value, integration_cLebedevWorkspace)) {
|
|
156
|
+
rb_raise(rb_eTypeError,
|
|
157
|
+
"workspace must be a Numo::GSL::Integration::LebedevWorkspace");
|
|
158
|
+
}
|
|
159
|
+
TypedData_Get_Struct(workspace_value, integration_lebedev_workspace,
|
|
160
|
+
&integration_lebedev_workspace_type, workspace);
|
|
161
|
+
if (workspace->workspace == NULL) {
|
|
162
|
+
rb_raise(rb_eRuntimeError, "workspace is closed");
|
|
163
|
+
}
|
|
164
|
+
if (workspace->busy) {
|
|
165
|
+
rb_raise(rb_eRuntimeError, "workspace is already in use");
|
|
166
|
+
}
|
|
167
|
+
return workspace;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static void integration_lebedev_workspace_use_begin(
|
|
171
|
+
VALUE workspace_value, size_t n, integration_lebedev_workspace_use *use) {
|
|
172
|
+
integration_lebedev_workspace *workspace;
|
|
173
|
+
|
|
174
|
+
use->workspace = NULL;
|
|
175
|
+
use->owner = NULL;
|
|
25
176
|
|
|
26
|
-
if (
|
|
27
|
-
|
|
177
|
+
if (workspace_value == Qundef) {
|
|
178
|
+
use->workspace = gsl_integration_lebedev_alloc(n);
|
|
179
|
+
if (use->workspace == NULL) {
|
|
180
|
+
rb_memerror();
|
|
181
|
+
}
|
|
182
|
+
return;
|
|
28
183
|
}
|
|
29
184
|
|
|
185
|
+
workspace = integration_lebedev_workspace_get(workspace_value);
|
|
186
|
+
workspace->busy = 1;
|
|
187
|
+
use->workspace = workspace->workspace;
|
|
188
|
+
use->owner = workspace;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static VALUE integration_lebedev_workspace_use_end(VALUE data) {
|
|
192
|
+
integration_lebedev_workspace_use *use =
|
|
193
|
+
(integration_lebedev_workspace_use *)data;
|
|
194
|
+
|
|
195
|
+
if (use->owner != NULL) {
|
|
196
|
+
use->owner->busy = 0;
|
|
197
|
+
} else if (use->workspace != NULL) {
|
|
198
|
+
gsl_integration_lebedev_free(use->workspace);
|
|
199
|
+
}
|
|
200
|
+
use->workspace = NULL;
|
|
201
|
+
use->owner = NULL;
|
|
30
202
|
return Qnil;
|
|
31
203
|
}
|
|
32
204
|
|
|
205
|
+
static VALUE integration_call_lebedev_block(VALUE data) {
|
|
206
|
+
integration_lebedev_params *params = (integration_lebedev_params *)data;
|
|
207
|
+
VALUE arguments[3] = {params->x, params->y, params->z};
|
|
208
|
+
VALUE value;
|
|
209
|
+
|
|
210
|
+
value = rb_funcallv(params->block, rb_intern("call"), 3, arguments);
|
|
211
|
+
return rb_Float(value);
|
|
212
|
+
}
|
|
213
|
+
|
|
33
214
|
static VALUE integration_run_lebedev(VALUE data) {
|
|
34
215
|
integration_lebedev_params *params = (integration_lebedev_params *)data;
|
|
35
216
|
double result = 0.0;
|
|
@@ -56,36 +237,41 @@ static VALUE integration_run_lebedev(VALUE data) {
|
|
|
56
237
|
return Qnil;
|
|
57
238
|
}
|
|
58
239
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
VALUE integration_lebedev(VALUE self, VALUE n_value) {
|
|
240
|
+
VALUE integration_lebedev(int argc, VALUE *argv, VALUE self) {
|
|
241
|
+
VALUE n_value = Qundef;
|
|
242
|
+
VALUE options;
|
|
243
|
+
VALUE workspace_value = Qundef;
|
|
244
|
+
ID keywords[] = {integration_lebedev_id_workspace};
|
|
245
|
+
VALUE keyword_values[1];
|
|
67
246
|
integration_lebedev_params params = {0};
|
|
68
|
-
|
|
247
|
+
integration_lebedev_workspace_use workspace_use;
|
|
248
|
+
size_t n = 0;
|
|
249
|
+
int positional_count;
|
|
69
250
|
int state = 0;
|
|
70
251
|
|
|
71
252
|
(void)self;
|
|
72
|
-
|
|
73
|
-
if (!
|
|
74
|
-
|
|
253
|
+
positional_count = rb_scan_args(argc, argv, "01:", &n_value, &options);
|
|
254
|
+
if (!NIL_P(options)) {
|
|
255
|
+
rb_get_kwargs(options, keywords, 0, 1, keyword_values);
|
|
256
|
+
workspace_value = keyword_values[0];
|
|
75
257
|
}
|
|
76
|
-
if (
|
|
77
|
-
rb_raise(rb_eArgError, "
|
|
258
|
+
if (positional_count == 0 && workspace_value == Qundef) {
|
|
259
|
+
rb_raise(rb_eArgError, "wrong number of arguments (given 0, expected 1)");
|
|
78
260
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
|
|
261
|
+
if (positional_count == 1 && workspace_value != Qundef) {
|
|
262
|
+
rb_raise(rb_eArgError,
|
|
263
|
+
"workspace cannot be specified with a Lebedev node count");
|
|
264
|
+
}
|
|
265
|
+
if (positional_count == 1) {
|
|
266
|
+
n = integration_lebedev_node_count(n_value);
|
|
85
267
|
}
|
|
86
268
|
|
|
87
|
-
|
|
88
|
-
|
|
269
|
+
params.block = rb_block_proc();
|
|
270
|
+
integration_lebedev_workspace_use_begin(workspace_value, n, &workspace_use);
|
|
271
|
+
params.workspace = workspace_use.workspace;
|
|
272
|
+
rb_protect(integration_run_lebedev, (VALUE)¶ms, &state);
|
|
273
|
+
integration_lebedev_workspace_use_end((VALUE)&workspace_use);
|
|
274
|
+
RB_GC_GUARD(workspace_value);
|
|
89
275
|
RB_GC_GUARD(params.block);
|
|
90
276
|
RB_GC_GUARD(params.x);
|
|
91
277
|
RB_GC_GUARD(params.y);
|
|
@@ -101,3 +287,24 @@ VALUE integration_lebedev(VALUE self, VALUE n_value) {
|
|
|
101
287
|
|
|
102
288
|
return DBL2NUM(params.result);
|
|
103
289
|
}
|
|
290
|
+
|
|
291
|
+
void integration_lebedev_init(VALUE integration_module) {
|
|
292
|
+
integration_lebedev_id_n = rb_intern("n");
|
|
293
|
+
integration_lebedev_id_workspace = rb_intern("workspace");
|
|
294
|
+
integration_lebedev_id_less_than_or_equal = rb_intern("<=");
|
|
295
|
+
|
|
296
|
+
integration_cLebedevWorkspace =
|
|
297
|
+
rb_define_class_under(integration_module, "LebedevWorkspace", rb_cObject);
|
|
298
|
+
rb_define_alloc_func(integration_cLebedevWorkspace,
|
|
299
|
+
integration_lebedev_workspace_allocate);
|
|
300
|
+
rb_define_method(integration_cLebedevWorkspace, "initialize",
|
|
301
|
+
integration_lebedev_workspace_initialize, -1);
|
|
302
|
+
rb_define_method(integration_cLebedevWorkspace, "n",
|
|
303
|
+
integration_lebedev_workspace_n, 0);
|
|
304
|
+
rb_define_method(integration_cLebedevWorkspace, "closed?",
|
|
305
|
+
integration_lebedev_workspace_closed_p, 0);
|
|
306
|
+
rb_define_method(integration_cLebedevWorkspace, "close",
|
|
307
|
+
integration_lebedev_workspace_close, 0);
|
|
308
|
+
rb_define_singleton_method(integration_cLebedevWorkspace, "open",
|
|
309
|
+
integration_lebedev_workspace_open, -1);
|
|
310
|
+
}
|
|
@@ -31,6 +31,39 @@ module Numo
|
|
|
31
31
|
def close: () -> nil
|
|
32
32
|
def closed?: () -> bool
|
|
33
33
|
end
|
|
34
|
+
class FixedWorkspace
|
|
35
|
+
def self.open: [T] (
|
|
36
|
+
(:legendre | :chebyshev | :chebyshev2 | :gegenbauer | :jacobi | :laguerre | :hermite | :exponential | :rational) type,
|
|
37
|
+
order: Integer,
|
|
38
|
+
a: Numeric,
|
|
39
|
+
b: Numeric,
|
|
40
|
+
?alpha: Numeric,
|
|
41
|
+
?beta: Numeric
|
|
42
|
+
) { (FixedWorkspace) -> T } -> T
|
|
43
|
+
def initialize: (
|
|
44
|
+
(:legendre | :chebyshev | :chebyshev2 | :gegenbauer | :jacobi | :laguerre | :hermite | :exponential | :rational) type,
|
|
45
|
+
order: Integer,
|
|
46
|
+
a: Numeric,
|
|
47
|
+
b: Numeric,
|
|
48
|
+
?alpha: Numeric,
|
|
49
|
+
?beta: Numeric
|
|
50
|
+
) -> void
|
|
51
|
+
def type: () -> (:legendre | :chebyshev | :chebyshev2 | :gegenbauer | :jacobi | :laguerre | :hermite | :exponential | :rational)
|
|
52
|
+
def order: () -> Integer
|
|
53
|
+
def a: () -> Float
|
|
54
|
+
def b: () -> Float
|
|
55
|
+
def alpha: () -> Float?
|
|
56
|
+
def beta: () -> Float?
|
|
57
|
+
def close: () -> nil
|
|
58
|
+
def closed?: () -> bool
|
|
59
|
+
end
|
|
60
|
+
class LebedevWorkspace
|
|
61
|
+
def self.open: [T] (n: Integer) { (LebedevWorkspace) -> T } -> T
|
|
62
|
+
def initialize: (n: Integer) -> void
|
|
63
|
+
def n: () -> Integer
|
|
64
|
+
def close: () -> nil
|
|
65
|
+
def closed?: () -> bool
|
|
66
|
+
end
|
|
34
67
|
class GLFixedTable
|
|
35
68
|
def self.open: [T] (order: Integer) { (GLFixedTable) -> T } -> T
|
|
36
69
|
def initialize: (order: Integer) -> void
|
|
@@ -213,9 +246,13 @@ module Numo
|
|
|
213
246
|
?alpha: Numeric,
|
|
214
247
|
?beta: Numeric
|
|
215
248
|
) { (Float) -> Numeric } -> Float
|
|
249
|
+
| (workspace: FixedWorkspace) { (Float) -> Numeric } -> Float
|
|
216
250
|
def self.lebedev: (Integer n) {
|
|
217
251
|
(Float, Float, Float) -> Numeric
|
|
218
252
|
} -> Float
|
|
253
|
+
| (workspace: LebedevWorkspace) {
|
|
254
|
+
(Float, Float, Float) -> Numeric
|
|
255
|
+
} -> Float
|
|
219
256
|
end
|
|
220
257
|
end
|
|
221
258
|
end
|