numo-gsl-integration 0.6.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 +28 -1
- data/README.md +33 -3
- data/docs/development.md +2 -2
- data/docs/error-handling.md +1 -1
- data/docs/routines.md +107 -1
- data/ext/numo/gsl/integration/cquad.c +45 -18
- data/ext/numo/gsl/integration/cquad_workspace.c +214 -0
- data/ext/numo/gsl/integration/fixed.c +374 -80
- data/ext/numo/gsl/integration/glfixed.c +22 -18
- data/ext/numo/gsl/integration/glfixed_table.c +211 -0
- data/ext/numo/gsl/integration/integration.c +9 -2
- data/ext/numo/gsl/integration/integration_internal.h +45 -2
- data/ext/numo/gsl/integration/lebedev.c +239 -32
- data/ext/numo/gsl/integration/romberg.c +47 -19
- data/ext/numo/gsl/integration/romberg_workspace.c +215 -0
- data/lib/numo/gsl/integration/version.rb +1 -1
- data/sig/numo/gsl/integration.rbs +64 -3
- metadata +4 -1
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
#include "integration_internal.h"
|
|
2
|
+
|
|
3
|
+
static VALUE integration_cGLFixedTable;
|
|
4
|
+
|
|
5
|
+
static void integration_glfixed_table_free(void *data) {
|
|
6
|
+
integration_glfixed_table *table = data;
|
|
7
|
+
|
|
8
|
+
if (table->table != NULL) {
|
|
9
|
+
gsl_integration_glfixed_table_free(table->table);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
xfree(table);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static size_t integration_glfixed_table_memsize(const void *data) {
|
|
16
|
+
return data == NULL ? 0 : sizeof(integration_glfixed_table);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static const rb_data_type_t integration_glfixed_table_type = {
|
|
20
|
+
.wrap_struct_name = "Numo::GSL::Integration::GLFixedTable",
|
|
21
|
+
.function =
|
|
22
|
+
{
|
|
23
|
+
.dmark = NULL,
|
|
24
|
+
.dfree = integration_glfixed_table_free,
|
|
25
|
+
.dsize = integration_glfixed_table_memsize,
|
|
26
|
+
},
|
|
27
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
static VALUE integration_glfixed_table_allocate(VALUE klass) {
|
|
31
|
+
integration_glfixed_table *table;
|
|
32
|
+
VALUE object = TypedData_Make_Struct(klass, integration_glfixed_table,
|
|
33
|
+
&integration_glfixed_table_type, table);
|
|
34
|
+
|
|
35
|
+
table->table = NULL;
|
|
36
|
+
table->order = 0;
|
|
37
|
+
table->busy = 0;
|
|
38
|
+
|
|
39
|
+
return object;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static VALUE integration_glfixed_table_initialize(int argc, VALUE *argv,
|
|
43
|
+
VALUE self) {
|
|
44
|
+
VALUE options;
|
|
45
|
+
VALUE order_value;
|
|
46
|
+
ID keywords[] = {rb_intern("order")};
|
|
47
|
+
VALUE keyword_values[1];
|
|
48
|
+
size_t order;
|
|
49
|
+
gsl_integration_glfixed_table *native_table;
|
|
50
|
+
integration_glfixed_table *table;
|
|
51
|
+
|
|
52
|
+
rb_scan_args(argc, argv, "0:", &options);
|
|
53
|
+
if (NIL_P(options)) {
|
|
54
|
+
rb_raise(rb_eArgError, "missing keyword: :order");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
rb_get_kwargs(options, keywords, 1, 0, keyword_values);
|
|
58
|
+
order_value = keyword_values[0];
|
|
59
|
+
if (!RB_INTEGER_TYPE_P(order_value)) {
|
|
60
|
+
rb_raise(rb_eTypeError, "order must be an Integer");
|
|
61
|
+
}
|
|
62
|
+
if (RTEST(rb_funcall(order_value, rb_intern("<="), 1, INT2FIX(0)))) {
|
|
63
|
+
rb_raise(rb_eArgError, "order must be positive");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
TypedData_Get_Struct(self, integration_glfixed_table,
|
|
67
|
+
&integration_glfixed_table_type, table);
|
|
68
|
+
if (table->table != NULL) {
|
|
69
|
+
rb_raise(rb_eRuntimeError, "table is already initialized");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
order = NUM2SIZET(order_value);
|
|
73
|
+
native_table = gsl_integration_glfixed_table_alloc(order);
|
|
74
|
+
if (native_table == NULL) {
|
|
75
|
+
rb_memerror();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
table->table = native_table;
|
|
79
|
+
table->order = order;
|
|
80
|
+
|
|
81
|
+
return self;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static VALUE integration_glfixed_table_order(VALUE self) {
|
|
85
|
+
integration_glfixed_table *table;
|
|
86
|
+
|
|
87
|
+
TypedData_Get_Struct(self, integration_glfixed_table,
|
|
88
|
+
&integration_glfixed_table_type, table);
|
|
89
|
+
|
|
90
|
+
return SIZET2NUM(table->order);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static VALUE integration_glfixed_table_closed_p(VALUE self) {
|
|
94
|
+
integration_glfixed_table *table;
|
|
95
|
+
|
|
96
|
+
TypedData_Get_Struct(self, integration_glfixed_table,
|
|
97
|
+
&integration_glfixed_table_type, table);
|
|
98
|
+
|
|
99
|
+
return table->table == NULL ? Qtrue : Qfalse;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static VALUE integration_glfixed_table_close(VALUE self) {
|
|
103
|
+
integration_glfixed_table *table;
|
|
104
|
+
|
|
105
|
+
TypedData_Get_Struct(self, integration_glfixed_table,
|
|
106
|
+
&integration_glfixed_table_type, table);
|
|
107
|
+
if (table->busy) {
|
|
108
|
+
rb_raise(rb_eRuntimeError, "table is already in use");
|
|
109
|
+
}
|
|
110
|
+
if (table->table != NULL) {
|
|
111
|
+
gsl_integration_glfixed_table_free(table->table);
|
|
112
|
+
table->table = NULL;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return Qnil;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static VALUE integration_glfixed_table_yield(VALUE table) {
|
|
119
|
+
return rb_yield(table);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static VALUE integration_glfixed_table_open(int argc, VALUE *argv,
|
|
123
|
+
VALUE klass) {
|
|
124
|
+
VALUE table;
|
|
125
|
+
|
|
126
|
+
table = rb_funcallv_kw(klass, rb_intern("new"), argc, argv, RB_PASS_KEYWORDS);
|
|
127
|
+
|
|
128
|
+
return rb_ensure(integration_glfixed_table_yield, table,
|
|
129
|
+
integration_glfixed_table_close, table);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
static integration_glfixed_table *
|
|
133
|
+
integration_glfixed_table_get(VALUE table_value) {
|
|
134
|
+
integration_glfixed_table *table;
|
|
135
|
+
|
|
136
|
+
if (!rb_obj_is_kind_of(table_value, integration_cGLFixedTable)) {
|
|
137
|
+
rb_raise(rb_eTypeError,
|
|
138
|
+
"table must be a Numo::GSL::Integration::GLFixedTable");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
TypedData_Get_Struct(table_value, integration_glfixed_table,
|
|
142
|
+
&integration_glfixed_table_type, table);
|
|
143
|
+
if (table->table == NULL) {
|
|
144
|
+
rb_raise(rb_eRuntimeError, "table is closed");
|
|
145
|
+
}
|
|
146
|
+
if (table->busy) {
|
|
147
|
+
rb_raise(rb_eRuntimeError, "table is already in use");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return table;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
void integration_glfixed_table_use_begin(VALUE table_value, size_t *order,
|
|
154
|
+
int order_given,
|
|
155
|
+
integration_glfixed_table_use *use) {
|
|
156
|
+
integration_glfixed_table *table;
|
|
157
|
+
|
|
158
|
+
use->table = NULL;
|
|
159
|
+
use->owner = NULL;
|
|
160
|
+
if (table_value == Qundef) {
|
|
161
|
+
use->table = gsl_integration_glfixed_table_alloc(*order);
|
|
162
|
+
if (use->table == NULL) {
|
|
163
|
+
rb_memerror();
|
|
164
|
+
}
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
table = integration_glfixed_table_get(table_value);
|
|
169
|
+
if (order_given && *order != table->order) {
|
|
170
|
+
rb_raise(rb_eArgError, "order does not match GLFixed table");
|
|
171
|
+
}
|
|
172
|
+
if (!order_given) {
|
|
173
|
+
*order = table->order;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
table->busy = 1;
|
|
177
|
+
use->table = table->table;
|
|
178
|
+
use->owner = table;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
VALUE integration_glfixed_table_use_end(VALUE data) {
|
|
182
|
+
integration_glfixed_table_use *use = (integration_glfixed_table_use *)data;
|
|
183
|
+
|
|
184
|
+
if (use->owner != NULL) {
|
|
185
|
+
use->owner->busy = 0;
|
|
186
|
+
} else if (use->table != NULL) {
|
|
187
|
+
gsl_integration_glfixed_table_free(use->table);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
use->table = NULL;
|
|
191
|
+
use->owner = NULL;
|
|
192
|
+
|
|
193
|
+
return Qnil;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
void integration_glfixed_table_init(VALUE integration_module) {
|
|
197
|
+
integration_cGLFixedTable =
|
|
198
|
+
rb_define_class_under(integration_module, "GLFixedTable", rb_cObject);
|
|
199
|
+
rb_define_alloc_func(integration_cGLFixedTable,
|
|
200
|
+
integration_glfixed_table_allocate);
|
|
201
|
+
rb_define_method(integration_cGLFixedTable, "initialize",
|
|
202
|
+
integration_glfixed_table_initialize, -1);
|
|
203
|
+
rb_define_method(integration_cGLFixedTable, "order",
|
|
204
|
+
integration_glfixed_table_order, 0);
|
|
205
|
+
rb_define_method(integration_cGLFixedTable, "closed?",
|
|
206
|
+
integration_glfixed_table_closed_p, 0);
|
|
207
|
+
rb_define_method(integration_cGLFixedTable, "close",
|
|
208
|
+
integration_glfixed_table_close, 0);
|
|
209
|
+
rb_define_singleton_method(integration_cGLFixedTable, "open",
|
|
210
|
+
integration_glfixed_table_open, -1);
|
|
211
|
+
}
|
|
@@ -22,10 +22,17 @@ void Init_integration(void) {
|
|
|
22
22
|
gsl_set_error_handler(&integration_gsl_error_handler);
|
|
23
23
|
|
|
24
24
|
integration_workspace_init(mIntegration);
|
|
25
|
+
|
|
26
|
+
integration_cquad_workspace_init(mIntegration);
|
|
27
|
+
|
|
28
|
+
integration_romberg_workspace_init(mIntegration);
|
|
29
|
+
|
|
30
|
+
integration_glfixed_table_init(mIntegration);
|
|
25
31
|
integration_qawo_table_init(mIntegration);
|
|
26
32
|
integration_qawf_table_init(mIntegration);
|
|
27
33
|
integration_qaws_table_init(mIntegration);
|
|
28
|
-
integration_fixed_init();
|
|
34
|
+
integration_fixed_init(mIntegration);
|
|
35
|
+
integration_lebedev_init(mIntegration);
|
|
29
36
|
|
|
30
37
|
rb_define_const(mIntegration, "GAUSS15", INT2NUM(GSL_INTEG_GAUSS15));
|
|
31
38
|
rb_define_const(mIntegration, "GAUSS21", INT2NUM(GSL_INTEG_GAUSS21));
|
|
@@ -51,5 +58,5 @@ void Init_integration(void) {
|
|
|
51
58
|
rb_define_singleton_method(mIntegration, "romberg", integration_romberg, -1);
|
|
52
59
|
rb_define_singleton_method(mIntegration, "glfixed", integration_glfixed, -1);
|
|
53
60
|
rb_define_singleton_method(mIntegration, "fixed", integration_fixed, -1);
|
|
54
|
-
rb_define_singleton_method(mIntegration, "lebedev", integration_lebedev, 1);
|
|
61
|
+
rb_define_singleton_method(mIntegration, "lebedev", integration_lebedev, -1);
|
|
55
62
|
}
|
|
@@ -42,6 +42,48 @@ void integration_workspace_use_begin(VALUE workspace_value, size_t *limit,
|
|
|
42
42
|
integration_workspace_use *use);
|
|
43
43
|
VALUE integration_workspace_use_end(VALUE data);
|
|
44
44
|
|
|
45
|
+
typedef struct {
|
|
46
|
+
gsl_integration_cquad_workspace *workspace;
|
|
47
|
+
size_t limit;
|
|
48
|
+
int busy;
|
|
49
|
+
} integration_cquad_workspace;
|
|
50
|
+
typedef struct {
|
|
51
|
+
gsl_integration_cquad_workspace *workspace;
|
|
52
|
+
integration_cquad_workspace *owner;
|
|
53
|
+
} integration_cquad_workspace_use;
|
|
54
|
+
void integration_cquad_workspace_init(VALUE integration_module);
|
|
55
|
+
void integration_cquad_workspace_use_begin(
|
|
56
|
+
VALUE workspace_value, size_t *limit, int limit_given,
|
|
57
|
+
integration_cquad_workspace_use *use);
|
|
58
|
+
VALUE integration_cquad_workspace_use_end(VALUE data);
|
|
59
|
+
typedef struct {
|
|
60
|
+
gsl_integration_romberg_workspace *workspace;
|
|
61
|
+
size_t limit;
|
|
62
|
+
int busy;
|
|
63
|
+
} integration_romberg_workspace;
|
|
64
|
+
typedef struct {
|
|
65
|
+
gsl_integration_romberg_workspace *workspace;
|
|
66
|
+
integration_romberg_workspace *owner;
|
|
67
|
+
} integration_romberg_workspace_use;
|
|
68
|
+
void integration_romberg_workspace_init(VALUE integration_module);
|
|
69
|
+
void integration_romberg_workspace_use_begin(
|
|
70
|
+
VALUE workspace_value, size_t *limit, int limit_given,
|
|
71
|
+
integration_romberg_workspace_use *use);
|
|
72
|
+
VALUE integration_romberg_workspace_use_end(VALUE data);
|
|
73
|
+
typedef struct {
|
|
74
|
+
gsl_integration_glfixed_table *table;
|
|
75
|
+
size_t order;
|
|
76
|
+
int busy;
|
|
77
|
+
} integration_glfixed_table;
|
|
78
|
+
typedef struct {
|
|
79
|
+
gsl_integration_glfixed_table *table;
|
|
80
|
+
integration_glfixed_table *owner;
|
|
81
|
+
} integration_glfixed_table_use;
|
|
82
|
+
void integration_glfixed_table_init(VALUE integration_module);
|
|
83
|
+
void integration_glfixed_table_use_begin(VALUE table_value, size_t *order,
|
|
84
|
+
int order_given,
|
|
85
|
+
integration_glfixed_table_use *use);
|
|
86
|
+
VALUE integration_glfixed_table_use_end(VALUE data);
|
|
45
87
|
typedef struct {
|
|
46
88
|
gsl_integration_qawo_table *table;
|
|
47
89
|
double omega;
|
|
@@ -109,7 +151,8 @@ VALUE integration_free_cquad_workspace(VALUE data);
|
|
|
109
151
|
VALUE integration_free_romberg_workspace(VALUE data);
|
|
110
152
|
VALUE integration_free_glfixed_table(VALUE data);
|
|
111
153
|
|
|
112
|
-
void integration_fixed_init(
|
|
154
|
+
void integration_fixed_init(VALUE integration_module);
|
|
155
|
+
void integration_lebedev_init(VALUE integration_module);
|
|
113
156
|
|
|
114
157
|
VALUE integration_qag(int argc, VALUE *argv, VALUE self);
|
|
115
158
|
VALUE integration_qng(int argc, VALUE *argv, VALUE self);
|
|
@@ -126,6 +169,6 @@ VALUE integration_cquad(int argc, VALUE *argv, VALUE self);
|
|
|
126
169
|
VALUE integration_romberg(int argc, VALUE *argv, VALUE self);
|
|
127
170
|
VALUE integration_glfixed(int argc, VALUE *argv, VALUE self);
|
|
128
171
|
VALUE integration_fixed(int argc, VALUE *argv, VALUE self);
|
|
129
|
-
VALUE integration_lebedev(VALUE
|
|
172
|
+
VALUE integration_lebedev(int argc, VALUE *argv, VALUE self);
|
|
130
173
|
|
|
131
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
|
+
}
|