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.
@@ -6,7 +6,10 @@ typedef struct {
6
6
  double b;
7
7
  double epsabs;
8
8
  double epsrel;
9
- gsl_integration_romberg_workspace *workspace;
9
+ size_t limit;
10
+ int limit_given;
11
+ VALUE workspace_value;
12
+ integration_romberg_workspace_use workspace_use;
10
13
  double *result;
11
14
  size_t *evaluations;
12
15
  } integration_romberg_params;
@@ -14,9 +17,12 @@ typedef struct {
14
17
  static VALUE integration_run_romberg(VALUE data) {
15
18
  integration_romberg_params *params = (integration_romberg_params *)data;
16
19
 
17
- (void)gsl_integration_romberg(params->function, params->a, params->b,
18
- params->epsabs, params->epsrel, params->result,
19
- params->evaluations, params->workspace);
20
+ integration_romberg_workspace_use_begin(params->workspace_value,
21
+ &params->limit, params->limit_given,
22
+ &params->workspace_use);
23
+ (void)gsl_integration_romberg(
24
+ params->function, params->a, params->b, params->epsabs, params->epsrel,
25
+ params->result, params->evaluations, params->workspace_use.workspace);
20
26
 
21
27
  return Qnil;
22
28
  }
@@ -25,22 +31,28 @@ static VALUE integration_run_romberg_with_workspace(VALUE data) {
25
31
  integration_romberg_params *params = (integration_romberg_params *)data;
26
32
 
27
33
  return rb_ensure(integration_run_romberg, data,
28
- integration_free_romberg_workspace,
29
- (VALUE)params->workspace);
34
+ integration_romberg_workspace_use_end,
35
+ (VALUE)&params->workspace_use);
30
36
  }
31
37
 
32
38
  VALUE integration_romberg(int argc, VALUE *argv, VALUE self) {
33
39
  VALUE a;
34
40
  VALUE b;
35
41
  VALUE options;
36
- integration_options parsed_options;
42
+ VALUE epsabs_value = Qundef;
43
+ VALUE epsrel_value = Qundef;
44
+ VALUE limit_value = Qundef;
45
+ VALUE workspace_value = Qundef;
46
+ ID keywords[] = {rb_intern("epsabs"), rb_intern("epsrel"), rb_intern("limit"),
47
+ rb_intern("workspace")};
48
+ VALUE keyword_values[4];
37
49
  double epsabs;
38
50
  double epsrel;
39
- size_t limit;
40
- gsl_integration_romberg_workspace *workspace;
51
+ size_t limit = 20;
52
+ int limit_given;
41
53
  gsl_function function;
42
54
  integration_callback_params params;
43
- integration_romberg_params romberg_params;
55
+ integration_romberg_params romberg_params = {0};
44
56
  double result;
45
57
  size_t evaluations;
46
58
  int state = 0;
@@ -48,23 +60,38 @@ VALUE integration_romberg(int argc, VALUE *argv, VALUE self) {
48
60
  (void)self;
49
61
 
50
62
  rb_scan_args(argc, argv, "2:", &a, &b, &options);
51
-
52
- integration_options_parse(options, &parsed_options, 0.0, 1e-8, 20);
53
-
54
- epsabs = parsed_options.epsabs;
55
- epsrel = parsed_options.epsrel;
56
- limit = parsed_options.limit;
63
+ if (!NIL_P(options)) {
64
+ rb_get_kwargs(options, keywords, 0, 4, keyword_values);
65
+ epsabs_value = keyword_values[0];
66
+ epsrel_value = keyword_values[1];
67
+ limit_value = keyword_values[2];
68
+ workspace_value = keyword_values[3];
69
+ }
70
+
71
+ epsabs = epsabs_value == Qundef ? 0.0 : NUM2DBL(epsabs_value);
72
+ epsrel = epsrel_value == Qundef ? 1e-8 : NUM2DBL(epsrel_value);
73
+ limit_given = limit_value != Qundef;
74
+ if (limit_given) {
75
+ if (!RB_INTEGER_TYPE_P(limit_value)) {
76
+ rb_raise(rb_eTypeError, "limit must be an Integer");
77
+ }
78
+ if (RTEST(rb_funcall(limit_value, rb_intern("<"), 1, INT2FIX(1))) ||
79
+ RTEST(rb_funcall(limit_value, rb_intern(">"), 1, INT2FIX(30)))) {
80
+ rb_raise(rb_eArgError, "limit must be between 1 and 30");
81
+ }
82
+ limit = NUM2SIZET(limit_value);
83
+ }
57
84
 
58
85
  integration_function_init(&function, &params);
59
86
 
60
- workspace = gsl_integration_romberg_alloc(limit);
61
-
62
87
  romberg_params.function = &function;
63
88
  romberg_params.a = NUM2DBL(a);
64
89
  romberg_params.b = NUM2DBL(b);
65
90
  romberg_params.epsabs = epsabs;
66
91
  romberg_params.epsrel = epsrel;
67
- romberg_params.workspace = workspace;
92
+ romberg_params.limit = limit;
93
+ romberg_params.limit_given = limit_given;
94
+ romberg_params.workspace_value = workspace_value;
68
95
  romberg_params.result = &result;
69
96
  romberg_params.evaluations = &evaluations;
70
97
 
@@ -73,6 +100,7 @@ VALUE integration_romberg(int argc, VALUE *argv, VALUE self) {
73
100
 
74
101
  RB_GC_GUARD(params.block);
75
102
  RB_GC_GUARD(params.exception);
103
+ RB_GC_GUARD(workspace_value);
76
104
 
77
105
  integration_finish_callback(&params, state);
78
106
 
@@ -0,0 +1,215 @@
1
+ #include "integration_internal.h"
2
+
3
+ static VALUE integration_cRombergWorkspace;
4
+
5
+ static void integration_romberg_workspace_free(void *data) {
6
+ integration_romberg_workspace *workspace = data;
7
+
8
+ if (workspace->workspace != NULL) {
9
+ gsl_integration_romberg_free(workspace->workspace);
10
+ }
11
+
12
+ xfree(workspace);
13
+ }
14
+
15
+ static size_t integration_romberg_workspace_memsize(const void *data) {
16
+ return data == NULL ? 0 : sizeof(integration_romberg_workspace);
17
+ }
18
+
19
+ static const rb_data_type_t integration_romberg_workspace_type = {
20
+ .wrap_struct_name = "Numo::GSL::Integration::RombergWorkspace",
21
+ .function =
22
+ {
23
+ .dmark = NULL,
24
+ .dfree = integration_romberg_workspace_free,
25
+ .dsize = integration_romberg_workspace_memsize,
26
+ },
27
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
28
+ };
29
+
30
+ static VALUE integration_romberg_workspace_allocate(VALUE klass) {
31
+ integration_romberg_workspace *workspace;
32
+ VALUE object =
33
+ TypedData_Make_Struct(klass, integration_romberg_workspace,
34
+ &integration_romberg_workspace_type, workspace);
35
+
36
+ workspace->workspace = NULL;
37
+ workspace->limit = 0;
38
+ workspace->busy = 0;
39
+
40
+ return object;
41
+ }
42
+
43
+ static VALUE integration_romberg_workspace_initialize(int argc, VALUE *argv,
44
+ VALUE self) {
45
+ VALUE options;
46
+ VALUE limit_value;
47
+ ID keywords[] = {rb_intern("limit")};
48
+ VALUE keyword_values[1];
49
+ size_t limit;
50
+ gsl_integration_romberg_workspace *native_workspace;
51
+ integration_romberg_workspace *workspace;
52
+
53
+ rb_scan_args(argc, argv, "0:", &options);
54
+ if (NIL_P(options)) {
55
+ rb_raise(rb_eArgError, "missing keyword: :limit");
56
+ }
57
+
58
+ rb_get_kwargs(options, keywords, 1, 0, keyword_values);
59
+ limit_value = keyword_values[0];
60
+ if (!RB_INTEGER_TYPE_P(limit_value)) {
61
+ rb_raise(rb_eTypeError, "limit must be an Integer");
62
+ }
63
+ if (RTEST(rb_funcall(limit_value, rb_intern("<"), 1, INT2FIX(1))) ||
64
+ RTEST(rb_funcall(limit_value, rb_intern(">"), 1, INT2FIX(30)))) {
65
+ rb_raise(rb_eArgError, "limit must be between 1 and 30");
66
+ }
67
+
68
+ TypedData_Get_Struct(self, integration_romberg_workspace,
69
+ &integration_romberg_workspace_type, workspace);
70
+ if (workspace->workspace != NULL) {
71
+ rb_raise(rb_eRuntimeError, "workspace is already initialized");
72
+ }
73
+
74
+ limit = NUM2SIZET(limit_value);
75
+ native_workspace = gsl_integration_romberg_alloc(limit);
76
+ if (native_workspace == NULL) {
77
+ rb_memerror();
78
+ }
79
+
80
+ workspace->workspace = native_workspace;
81
+ workspace->limit = limit;
82
+
83
+ return self;
84
+ }
85
+
86
+ static VALUE integration_romberg_workspace_limit(VALUE self) {
87
+ integration_romberg_workspace *workspace;
88
+
89
+ TypedData_Get_Struct(self, integration_romberg_workspace,
90
+ &integration_romberg_workspace_type, workspace);
91
+
92
+ return SIZET2NUM(workspace->limit);
93
+ }
94
+
95
+ static VALUE integration_romberg_workspace_closed_p(VALUE self) {
96
+ integration_romberg_workspace *workspace;
97
+
98
+ TypedData_Get_Struct(self, integration_romberg_workspace,
99
+ &integration_romberg_workspace_type, workspace);
100
+
101
+ return workspace->workspace == NULL ? Qtrue : Qfalse;
102
+ }
103
+
104
+ static VALUE integration_romberg_workspace_close(VALUE self) {
105
+ integration_romberg_workspace *workspace;
106
+
107
+ TypedData_Get_Struct(self, integration_romberg_workspace,
108
+ &integration_romberg_workspace_type, workspace);
109
+ if (workspace->busy) {
110
+ rb_raise(rb_eRuntimeError, "workspace is already in use");
111
+ }
112
+ if (workspace->workspace != NULL) {
113
+ gsl_integration_romberg_free(workspace->workspace);
114
+ workspace->workspace = NULL;
115
+ }
116
+
117
+ return Qnil;
118
+ }
119
+
120
+ static VALUE integration_romberg_workspace_yield(VALUE workspace) {
121
+ return rb_yield(workspace);
122
+ }
123
+
124
+ static VALUE integration_romberg_workspace_open(int argc, VALUE *argv,
125
+ VALUE klass) {
126
+ VALUE workspace;
127
+
128
+ workspace =
129
+ rb_funcallv_kw(klass, rb_intern("new"), argc, argv, RB_PASS_KEYWORDS);
130
+
131
+ return rb_ensure(integration_romberg_workspace_yield, workspace,
132
+ integration_romberg_workspace_close, workspace);
133
+ }
134
+
135
+ static integration_romberg_workspace *
136
+ integration_romberg_workspace_get(VALUE workspace_value) {
137
+ integration_romberg_workspace *workspace;
138
+
139
+ if (!rb_obj_is_kind_of(workspace_value, integration_cRombergWorkspace)) {
140
+ rb_raise(rb_eTypeError,
141
+ "workspace must be a Numo::GSL::Integration::RombergWorkspace");
142
+ }
143
+
144
+ TypedData_Get_Struct(workspace_value, integration_romberg_workspace,
145
+ &integration_romberg_workspace_type, workspace);
146
+ if (workspace->workspace == NULL) {
147
+ rb_raise(rb_eRuntimeError, "workspace is closed");
148
+ }
149
+ if (workspace->busy) {
150
+ rb_raise(rb_eRuntimeError, "workspace is already in use");
151
+ }
152
+
153
+ return workspace;
154
+ }
155
+
156
+ void integration_romberg_workspace_use_begin(
157
+ VALUE workspace_value, size_t *limit, int limit_given,
158
+ integration_romberg_workspace_use *use) {
159
+ integration_romberg_workspace *workspace;
160
+
161
+ use->workspace = NULL;
162
+ use->owner = NULL;
163
+ if (workspace_value == Qundef) {
164
+ use->workspace = gsl_integration_romberg_alloc(*limit);
165
+ if (use->workspace == NULL) {
166
+ rb_memerror();
167
+ }
168
+ return;
169
+ }
170
+
171
+ workspace = integration_romberg_workspace_get(workspace_value);
172
+ if (limit_given && *limit != workspace->limit) {
173
+ rb_raise(rb_eArgError, "limit must match workspace capacity");
174
+ }
175
+ if (!limit_given) {
176
+ *limit = workspace->limit;
177
+ }
178
+
179
+ workspace->busy = 1;
180
+ use->workspace = workspace->workspace;
181
+ use->owner = workspace;
182
+ }
183
+
184
+ VALUE integration_romberg_workspace_use_end(VALUE data) {
185
+ integration_romberg_workspace_use *use =
186
+ (integration_romberg_workspace_use *)data;
187
+
188
+ if (use->owner != NULL) {
189
+ use->owner->busy = 0;
190
+ } else if (use->workspace != NULL) {
191
+ gsl_integration_romberg_free(use->workspace);
192
+ }
193
+
194
+ use->workspace = NULL;
195
+ use->owner = NULL;
196
+
197
+ return Qnil;
198
+ }
199
+
200
+ void integration_romberg_workspace_init(VALUE integration_module) {
201
+ integration_cRombergWorkspace =
202
+ rb_define_class_under(integration_module, "RombergWorkspace", rb_cObject);
203
+ rb_define_alloc_func(integration_cRombergWorkspace,
204
+ integration_romberg_workspace_allocate);
205
+ rb_define_method(integration_cRombergWorkspace, "initialize",
206
+ integration_romberg_workspace_initialize, -1);
207
+ rb_define_method(integration_cRombergWorkspace, "limit",
208
+ integration_romberg_workspace_limit, 0);
209
+ rb_define_method(integration_cRombergWorkspace, "closed?",
210
+ integration_romberg_workspace_closed_p, 0);
211
+ rb_define_method(integration_cRombergWorkspace, "close",
212
+ integration_romberg_workspace_close, 0);
213
+ rb_define_singleton_method(integration_cRombergWorkspace, "open",
214
+ integration_romberg_workspace_open, -1);
215
+ }
@@ -3,7 +3,7 @@
3
3
  module Numo
4
4
  module GSL
5
5
  module Integration
6
- VERSION = "0.6.0"
6
+ VERSION = "0.8.0"
7
7
  end
8
8
  end
9
9
  end
@@ -17,6 +17,60 @@ module Numo
17
17
  def close: () -> nil
18
18
  def closed?: () -> bool
19
19
  end
20
+ class CQUADWorkspace
21
+ def self.open: [T] (limit: Integer) { (CQUADWorkspace) -> T } -> T
22
+ def initialize: (limit: Integer) -> void
23
+ def limit: () -> Integer
24
+ def close: () -> nil
25
+ def closed?: () -> bool
26
+ end
27
+ class RombergWorkspace
28
+ def self.open: [T] (limit: Integer) { (RombergWorkspace) -> T } -> T
29
+ def initialize: (limit: Integer) -> void
30
+ def limit: () -> Integer
31
+ def close: () -> nil
32
+ def closed?: () -> bool
33
+ end
34
+ class 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
67
+ class GLFixedTable
68
+ def self.open: [T] (order: Integer) { (GLFixedTable) -> T } -> T
69
+ def initialize: (order: Integer) -> void
70
+ def order: () -> Integer
71
+ def close: () -> nil
72
+ def closed?: () -> bool
73
+ end
20
74
  class QAWOTable
21
75
  def self.open: [T] (
22
76
  omega: Numeric,
@@ -167,19 +221,22 @@ module Numo
167
221
  Numeric b,
168
222
  ?epsabs: Numeric,
169
223
  ?epsrel: Numeric,
170
- ?limit: Integer
224
+ ?limit: Integer,
225
+ ?workspace: CQUADWorkspace
171
226
  ) { (Float) -> Numeric } -> [Float, Float, Integer]
172
227
  def self.romberg: (
173
228
  Numeric a,
174
229
  Numeric b,
175
230
  ?epsabs: Numeric,
176
231
  ?epsrel: Numeric,
177
- ?limit: Integer
232
+ ?limit: Integer,
233
+ ?workspace: RombergWorkspace
178
234
  ) { (Float) -> Numeric } -> [Float, Integer]
179
235
  def self.glfixed: (
180
236
  Numeric a,
181
237
  Numeric b,
182
- ?order: Integer
238
+ ?order: Integer,
239
+ ?table: GLFixedTable
183
240
  ) { (Float) -> Numeric } -> Float
184
241
  def self.fixed: (
185
242
  (:legendre | :chebyshev | :chebyshev2 | :gegenbauer | :jacobi | :laguerre | :hermite | :exponential | :rational) type,
@@ -189,9 +246,13 @@ module Numo
189
246
  ?alpha: Numeric,
190
247
  ?beta: Numeric
191
248
  ) { (Float) -> Numeric } -> Float
249
+ | (workspace: FixedWorkspace) { (Float) -> Numeric } -> Float
192
250
  def self.lebedev: (Integer n) {
193
251
  (Float, Float, Float) -> Numeric
194
252
  } -> Float
253
+ | (workspace: LebedevWorkspace) {
254
+ (Float, Float, Float) -> Numeric
255
+ } -> Float
195
256
  end
196
257
  end
197
258
  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.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro SATOH
@@ -26,9 +26,11 @@ files:
26
26
  - docs/routines.md
27
27
  - ext/numo/gsl/integration/callback.c
28
28
  - ext/numo/gsl/integration/cquad.c
29
+ - ext/numo/gsl/integration/cquad_workspace.c
29
30
  - ext/numo/gsl/integration/extconf.rb
30
31
  - ext/numo/gsl/integration/fixed.c
31
32
  - ext/numo/gsl/integration/glfixed.c
33
+ - ext/numo/gsl/integration/glfixed_table.c
32
34
  - ext/numo/gsl/integration/integration.c
33
35
  - ext/numo/gsl/integration/integration_internal.h
34
36
  - ext/numo/gsl/integration/lebedev.c
@@ -49,6 +51,7 @@ files:
49
51
  - ext/numo/gsl/integration/qng.c
50
52
  - ext/numo/gsl/integration/resources.c
51
53
  - ext/numo/gsl/integration/romberg.c
54
+ - ext/numo/gsl/integration/romberg_workspace.c
52
55
  - ext/numo/gsl/integration/workspace.c
53
56
  - lib/numo/gsl/integration.rb
54
57
  - lib/numo/gsl/integration/version.rb