gsl 1.12.108
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +29 -0
- data/Rakefile +54 -0
- data/VERSION +2 -0
- data/ext/MANIFEST +119 -0
- data/ext/alf.c +206 -0
- data/ext/array.c +666 -0
- data/ext/array_complex.c +247 -0
- data/ext/blas.c +29 -0
- data/ext/blas1.c +731 -0
- data/ext/blas2.c +1093 -0
- data/ext/blas3.c +881 -0
- data/ext/block.c +44 -0
- data/ext/block_source.c +886 -0
- data/ext/bspline.c +130 -0
- data/ext/bundle.c +3 -0
- data/ext/cdf.c +754 -0
- data/ext/cheb.c +542 -0
- data/ext/combination.c +283 -0
- data/ext/common.c +325 -0
- data/ext/complex.c +1004 -0
- data/ext/const.c +673 -0
- data/ext/const_additional.c +120 -0
- data/ext/cqp.c +283 -0
- data/ext/deriv.c +195 -0
- data/ext/dht.c +361 -0
- data/ext/diff.c +166 -0
- data/ext/dirac.c +395 -0
- data/ext/eigen.c +2373 -0
- data/ext/error.c +194 -0
- data/ext/extconf.rb +281 -0
- data/ext/fcmp.c +66 -0
- data/ext/fft.c +1092 -0
- data/ext/fit.c +205 -0
- data/ext/fresnel.c +312 -0
- data/ext/function.c +524 -0
- data/ext/geometry.c +139 -0
- data/ext/graph.c +1638 -0
- data/ext/gsl.c +271 -0
- data/ext/gsl_narray.c +653 -0
- data/ext/histogram.c +1995 -0
- data/ext/histogram2d.c +1068 -0
- data/ext/histogram3d.c +884 -0
- data/ext/histogram3d_source.c +750 -0
- data/ext/histogram_find.c +101 -0
- data/ext/histogram_oper.c +159 -0
- data/ext/ieee.c +98 -0
- data/ext/integration.c +1138 -0
- data/ext/interp.c +512 -0
- data/ext/jacobi.c +739 -0
- data/ext/linalg.c +4047 -0
- data/ext/linalg_complex.c +741 -0
- data/ext/math.c +725 -0
- data/ext/matrix.c +39 -0
- data/ext/matrix_complex.c +1732 -0
- data/ext/matrix_double.c +560 -0
- data/ext/matrix_int.c +256 -0
- data/ext/matrix_source.c +2733 -0
- data/ext/min.c +250 -0
- data/ext/monte.c +992 -0
- data/ext/multifit.c +1879 -0
- data/ext/multimin.c +808 -0
- data/ext/multimin_fsdf.c +156 -0
- data/ext/multiroots.c +955 -0
- data/ext/ndlinear.c +321 -0
- data/ext/nmf.c +167 -0
- data/ext/nmf_wrap.c +72 -0
- data/ext/ntuple.c +469 -0
- data/ext/odeiv.c +959 -0
- data/ext/ool.c +879 -0
- data/ext/oper_complex_source.c +253 -0
- data/ext/permutation.c +596 -0
- data/ext/poly.c +42 -0
- data/ext/poly2.c +265 -0
- data/ext/poly_source.c +1885 -0
- data/ext/qrng.c +171 -0
- data/ext/randist.c +1873 -0
- data/ext/rational.c +480 -0
- data/ext/rng.c +612 -0
- data/ext/root.c +408 -0
- data/ext/sf.c +1494 -0
- data/ext/sf_airy.c +200 -0
- data/ext/sf_bessel.c +867 -0
- data/ext/sf_clausen.c +28 -0
- data/ext/sf_coulomb.c +206 -0
- data/ext/sf_coupling.c +118 -0
- data/ext/sf_dawson.c +29 -0
- data/ext/sf_debye.c +157 -0
- data/ext/sf_dilog.c +42 -0
- data/ext/sf_elementary.c +44 -0
- data/ext/sf_ellint.c +206 -0
- data/ext/sf_elljac.c +29 -0
- data/ext/sf_erfc.c +93 -0
- data/ext/sf_exp.c +164 -0
- data/ext/sf_expint.c +211 -0
- data/ext/sf_fermi_dirac.c +148 -0
- data/ext/sf_gamma.c +344 -0
- data/ext/sf_gegenbauer.c +96 -0
- data/ext/sf_hyperg.c +197 -0
- data/ext/sf_laguerre.c +112 -0
- data/ext/sf_lambert.c +47 -0
- data/ext/sf_legendre.c +367 -0
- data/ext/sf_log.c +104 -0
- data/ext/sf_mathieu.c +238 -0
- data/ext/sf_power.c +46 -0
- data/ext/sf_psi.c +98 -0
- data/ext/sf_synchrotron.c +48 -0
- data/ext/sf_transport.c +76 -0
- data/ext/sf_trigonometric.c +207 -0
- data/ext/sf_zeta.c +119 -0
- data/ext/signal.c +310 -0
- data/ext/siman.c +718 -0
- data/ext/sort.c +208 -0
- data/ext/spline.c +395 -0
- data/ext/stats.c +799 -0
- data/ext/sum.c +168 -0
- data/ext/tamu_anova.c +56 -0
- data/ext/tensor.c +38 -0
- data/ext/tensor_source.c +1123 -0
- data/ext/vector.c +38 -0
- data/ext/vector_complex.c +2236 -0
- data/ext/vector_double.c +1433 -0
- data/ext/vector_int.c +204 -0
- data/ext/vector_source.c +3329 -0
- data/ext/wavelet.c +937 -0
- data/include/rb_gsl.h +151 -0
- data/include/rb_gsl_array.h +238 -0
- data/include/rb_gsl_cheb.h +21 -0
- data/include/rb_gsl_common.h +343 -0
- data/include/rb_gsl_complex.h +25 -0
- data/include/rb_gsl_const.h +29 -0
- data/include/rb_gsl_dirac.h +13 -0
- data/include/rb_gsl_eigen.h +17 -0
- data/include/rb_gsl_fft.h +62 -0
- data/include/rb_gsl_fit.h +25 -0
- data/include/rb_gsl_function.h +27 -0
- data/include/rb_gsl_graph.h +70 -0
- data/include/rb_gsl_histogram.h +63 -0
- data/include/rb_gsl_histogram3d.h +97 -0
- data/include/rb_gsl_integration.h +17 -0
- data/include/rb_gsl_interp.h +46 -0
- data/include/rb_gsl_linalg.h +25 -0
- data/include/rb_gsl_math.h +26 -0
- data/include/rb_gsl_odeiv.h +21 -0
- data/include/rb_gsl_poly.h +71 -0
- data/include/rb_gsl_rational.h +37 -0
- data/include/rb_gsl_rng.h +21 -0
- data/include/rb_gsl_root.h +22 -0
- data/include/rb_gsl_sf.h +119 -0
- data/include/rb_gsl_statistics.h +17 -0
- data/include/rb_gsl_tensor.h +45 -0
- data/include/rb_gsl_with_narray.h +22 -0
- data/include/templates_off.h +87 -0
- data/include/templates_on.h +241 -0
- data/lib/gsl/gnuplot.rb +41 -0
- data/lib/gsl/oper.rb +68 -0
- data/lib/ool.rb +22 -0
- data/lib/ool/conmin.rb +30 -0
- metadata +224 -0
data/ext/odeiv.c
ADDED
@@ -0,0 +1,959 @@
|
|
1
|
+
/*
|
2
|
+
odeiv.c
|
3
|
+
Ruby/GSL: Ruby extension library for GSL (GNU Scientific Library)
|
4
|
+
(C) Copyright 2004 by Yoshiki Tsunesada
|
5
|
+
|
6
|
+
Ruby/GSL is free software: you can redistribute it and/or modify it
|
7
|
+
under the terms of the GNU General Public License.
|
8
|
+
This library is distributed in the hope that it will be useful, but
|
9
|
+
WITHOUT ANY WARRANTY.
|
10
|
+
*/
|
11
|
+
#include "rb_gsl_config.h"
|
12
|
+
#include "rb_gsl_odeiv.h"
|
13
|
+
#include "rb_gsl_array.h"
|
14
|
+
#include "rb_gsl_function.h"
|
15
|
+
|
16
|
+
#ifndef CHECK_SYSTEM
|
17
|
+
#define CHECK_SYSTEM(x) if(CLASS_OF(x)!=cgsl_odeiv_system)\
|
18
|
+
rb_raise(rb_eTypeError,\
|
19
|
+
"wrong argument type %s (GSL::Odeiv::System expected)",\
|
20
|
+
rb_class2name(CLASS_OF(x)));
|
21
|
+
#endif
|
22
|
+
|
23
|
+
#ifndef CHECK_STEP
|
24
|
+
#define CHECK_STEP(x) if(CLASS_OF(x)!=cgsl_odeiv_step)\
|
25
|
+
rb_raise(rb_eTypeError,\
|
26
|
+
"wrong argument type %s (GSL::Odeiv::Step expected)",\
|
27
|
+
rb_class2name(CLASS_OF(x)));
|
28
|
+
#endif
|
29
|
+
|
30
|
+
#ifndef CHECK_CONTROL
|
31
|
+
#define CHECK_CONTROL(x) if(CLASS_OF(x)!=cgsl_odeiv_control)\
|
32
|
+
rb_raise(rb_eTypeError,\
|
33
|
+
"wrong argument type %s (GSL::Odeiv::Control expected)",\
|
34
|
+
rb_class2name(CLASS_OF(x)));
|
35
|
+
#endif
|
36
|
+
|
37
|
+
#ifndef CHECK_EVOLVE
|
38
|
+
#define CHECK_EVOLVE(x) if(CLASS_OF(x)!=cgsl_odeiv_evolve)\
|
39
|
+
rb_raise(rb_eTypeError,\
|
40
|
+
"wrong argument type %s (GSL::Odeiv::Evolve expected)",\
|
41
|
+
rb_class2name(CLASS_OF(x)));
|
42
|
+
#endif
|
43
|
+
|
44
|
+
#ifndef CHECK_SOLVER
|
45
|
+
#define CHECK_SOLVER(x) if(CLASS_OF(x)!=cgsl_odeiv_solver)\
|
46
|
+
rb_raise(rb_eTypeError,\
|
47
|
+
"wrong argument type %s (GSL::Odeiv::Solver expected)",\
|
48
|
+
rb_class2name(CLASS_OF(x)));
|
49
|
+
#endif
|
50
|
+
|
51
|
+
static VALUE cgsl_odeiv_system;
|
52
|
+
static VALUE cgsl_odeiv_step;
|
53
|
+
static VALUE cgsl_odeiv_control;
|
54
|
+
static VALUE cgsl_odeiv_evolve;
|
55
|
+
static VALUE cgsl_odeiv_solver;
|
56
|
+
|
57
|
+
enum {
|
58
|
+
GSL_ODEIV_STEP_RK2,
|
59
|
+
GSL_ODEIV_STEP_RK4,
|
60
|
+
GSL_ODEIV_STEP_RKF45,
|
61
|
+
GSL_ODEIV_STEP_RKCK,
|
62
|
+
GSL_ODEIV_STEP_RK8PD,
|
63
|
+
GSL_ODEIV_STEP_RK2IMP,
|
64
|
+
GSL_ODEIV_STEP_RK4IMP,
|
65
|
+
GSL_ODEIV_STEP_BSIMP,
|
66
|
+
GSL_ODEIV_STEP_GEAR1,
|
67
|
+
GSL_ODEIV_STEP_GEAR2,
|
68
|
+
GSL_ODEIV_STEP_RK2SIMP,
|
69
|
+
};
|
70
|
+
|
71
|
+
typedef struct __rb_gsl_odeiv_evolve {
|
72
|
+
gsl_odeiv_evolve *e;
|
73
|
+
gsl_odeiv_control *c;
|
74
|
+
gsl_odeiv_step *s;
|
75
|
+
gsl_odeiv_system *sys;
|
76
|
+
} gsl_odeiv_solver;
|
77
|
+
|
78
|
+
static int calc_func(double t, const double y[], double dydt[], void *data);
|
79
|
+
static int calc_jac(double t, const double y[], double *dfdy, double dfdt[], void *data);
|
80
|
+
|
81
|
+
static int calc_func(double t, const double y[], double dydt[], void *data)
|
82
|
+
{
|
83
|
+
VALUE ary, params, proc;
|
84
|
+
VALUE result;
|
85
|
+
VALUE vy, vdydt;
|
86
|
+
gsl_vector_view ytmp, dydttmp;
|
87
|
+
size_t dim;
|
88
|
+
|
89
|
+
ary = (VALUE) data;
|
90
|
+
proc = rb_ary_entry(ary, 0);
|
91
|
+
dim = FIX2INT(rb_ary_entry(ary, 2));
|
92
|
+
params = rb_ary_entry(ary, 3);
|
93
|
+
|
94
|
+
ytmp.vector.data = (double *) y;
|
95
|
+
ytmp.vector.stride = 1;
|
96
|
+
ytmp.vector.size = dim;
|
97
|
+
dydttmp.vector.data = dydt;
|
98
|
+
dydttmp.vector.stride = 1;
|
99
|
+
dydttmp.vector.size = dim;
|
100
|
+
vy = Data_Wrap_Struct(cgsl_vector_view_ro, 0, NULL, &ytmp);
|
101
|
+
vdydt = Data_Wrap_Struct(cgsl_vector_view, 0, NULL, &dydttmp);
|
102
|
+
|
103
|
+
if (NIL_P(params)) result = rb_funcall((VALUE) proc, RBGSL_ID_call, 3, rb_float_new(t),
|
104
|
+
vy, vdydt);
|
105
|
+
else result = rb_funcall((VALUE) proc, RBGSL_ID_call, 4, rb_float_new(t), vy, vdydt, params);
|
106
|
+
|
107
|
+
return GSL_SUCCESS;
|
108
|
+
}
|
109
|
+
|
110
|
+
static int calc_jac(double t, const double y[], double *dfdy, double dfdt[], void *data)
|
111
|
+
{
|
112
|
+
VALUE params, proc, ary;
|
113
|
+
VALUE result, vdfdt;
|
114
|
+
VALUE vy, vmjac;
|
115
|
+
gsl_vector_view ytmp, dfdttmp;
|
116
|
+
gsl_matrix_view mv;
|
117
|
+
size_t dim;
|
118
|
+
|
119
|
+
ary = (VALUE) data;
|
120
|
+
proc = rb_ary_entry(ary, 1);
|
121
|
+
if (NIL_P(proc)) rb_raise(rb_eRuntimeError, "df function not given");
|
122
|
+
|
123
|
+
dim = FIX2INT(rb_ary_entry(ary, 2));
|
124
|
+
params = rb_ary_entry(ary, 3);
|
125
|
+
|
126
|
+
ytmp.vector.data = (double *) y;
|
127
|
+
ytmp.vector.size = dim;
|
128
|
+
ytmp.vector.stride = 1;
|
129
|
+
dfdttmp.vector.data = dfdt;
|
130
|
+
dfdttmp.vector.size = dim;
|
131
|
+
dfdttmp.vector.stride = 1;
|
132
|
+
mv = gsl_matrix_view_array(dfdy, dim, dim);
|
133
|
+
vy = Data_Wrap_Struct(cgsl_vector_view_ro, 0, NULL, &ytmp);
|
134
|
+
vmjac = Data_Wrap_Struct(cgsl_matrix_view, 0, NULL, &mv);
|
135
|
+
vdfdt = Data_Wrap_Struct(cgsl_vector_view, 0, NULL, &dfdttmp);
|
136
|
+
if (NIL_P(params)) result = rb_funcall((VALUE) proc, RBGSL_ID_call, 4, rb_float_new(t),
|
137
|
+
vy, vmjac, vdfdt);
|
138
|
+
else result = rb_funcall((VALUE) proc, RBGSL_ID_call, 5, rb_float_new(t),
|
139
|
+
vy, vmjac, vdfdt, params);
|
140
|
+
return GSL_SUCCESS;
|
141
|
+
}
|
142
|
+
|
143
|
+
static void gsl_odeiv_system_mark(gsl_odeiv_system *sys);
|
144
|
+
static void gsl_odeiv_system_mark(gsl_odeiv_system *sys)
|
145
|
+
{
|
146
|
+
rb_gc_mark((VALUE) sys->params);
|
147
|
+
}
|
148
|
+
|
149
|
+
static gsl_odeiv_system* make_sys(int argc, VALUE *argv);
|
150
|
+
static void set_sys(int argc, VALUE *argv, gsl_odeiv_system *sys);
|
151
|
+
static VALUE rb_gsl_odeiv_system_new(int argc, VALUE *argv, VALUE klass)
|
152
|
+
{
|
153
|
+
VALUE obj;
|
154
|
+
gsl_odeiv_system *sys = NULL;
|
155
|
+
sys = make_sys(argc, argv);
|
156
|
+
obj = Data_Wrap_Struct(klass, gsl_odeiv_system_mark, free, sys);
|
157
|
+
return obj;
|
158
|
+
}
|
159
|
+
|
160
|
+
static VALUE rb_gsl_odeiv_system_set(int argc, VALUE *argv, VALUE obj)
|
161
|
+
{
|
162
|
+
gsl_odeiv_system *sys = NULL;
|
163
|
+
Data_Get_Struct(obj, gsl_odeiv_system, sys);
|
164
|
+
set_sys(argc, argv, sys);
|
165
|
+
return obj;
|
166
|
+
}
|
167
|
+
|
168
|
+
static gsl_odeiv_system* make_sys(int argc, VALUE *argv)
|
169
|
+
{
|
170
|
+
gsl_odeiv_system *sys = NULL;
|
171
|
+
sys = ALLOC(gsl_odeiv_system);
|
172
|
+
sys->function = &calc_func;
|
173
|
+
sys->jacobian = &calc_jac;
|
174
|
+
sys->params = NULL;
|
175
|
+
set_sys(argc, argv, sys);
|
176
|
+
return sys;
|
177
|
+
}
|
178
|
+
|
179
|
+
static void set_sys(int argc, VALUE *argv, gsl_odeiv_system *sys)
|
180
|
+
{
|
181
|
+
size_t dimension;
|
182
|
+
VALUE ary, vjac, dim;
|
183
|
+
VALUE vparams;
|
184
|
+
int itmp;
|
185
|
+
size_t i, j;
|
186
|
+
|
187
|
+
if (argc < 2) rb_raise(rb_eArgError, "too few arguments");
|
188
|
+
CHECK_PROC(argv[0]);
|
189
|
+
|
190
|
+
if (sys == NULL) {
|
191
|
+
sys = ALLOC(gsl_odeiv_system);
|
192
|
+
sys->function = &calc_func;
|
193
|
+
sys->jacobian = &calc_jac;
|
194
|
+
}
|
195
|
+
|
196
|
+
if (sys->params == NULL) {
|
197
|
+
ary = rb_ary_new2(4);
|
198
|
+
/* (VALUE) sys->params = ary;*/
|
199
|
+
sys->params = (void *) ary;
|
200
|
+
} else {
|
201
|
+
ary = (VALUE) sys->params;
|
202
|
+
}
|
203
|
+
rb_ary_store(ary, 1, Qnil); /* function to calc J */
|
204
|
+
rb_ary_store(ary, 3, Qnil); /* parameters */
|
205
|
+
|
206
|
+
itmp = 1;
|
207
|
+
if (rb_obj_is_kind_of(argv[1], rb_cProc)) {
|
208
|
+
vjac = argv[1];
|
209
|
+
itmp = 2;
|
210
|
+
} else {
|
211
|
+
vjac = Qnil;
|
212
|
+
}
|
213
|
+
if ((dim =argv[itmp++]) == Qnil) dim = argv[itmp++];
|
214
|
+
switch (argc - itmp) {
|
215
|
+
case 0:
|
216
|
+
vparams = Qnil;
|
217
|
+
break;
|
218
|
+
case 1:
|
219
|
+
vparams = argv[itmp];
|
220
|
+
break;
|
221
|
+
default:
|
222
|
+
vparams = rb_ary_new2(argc-itmp);
|
223
|
+
for (i = itmp, j = 0; i < argc; i++, j++) rb_ary_store(vparams, j, argv[i]);
|
224
|
+
}
|
225
|
+
dimension = FIX2INT(dim);
|
226
|
+
sys->dimension = dimension;
|
227
|
+
rb_ary_store(ary, 0, argv[0]);
|
228
|
+
rb_ary_store(ary, 1, vjac);
|
229
|
+
rb_ary_store(ary, 2, dim);
|
230
|
+
rb_ary_store(ary, 3, vparams);
|
231
|
+
}
|
232
|
+
|
233
|
+
static VALUE rb_gsl_odeiv_system_set_params(int argc, VALUE *argv, VALUE obj);
|
234
|
+
static VALUE rb_gsl_odeiv_system_set_params(int argc, VALUE *argv, VALUE obj)
|
235
|
+
{
|
236
|
+
VALUE vparams, ary;
|
237
|
+
gsl_odeiv_system *sys = NULL;
|
238
|
+
size_t i;
|
239
|
+
Data_Get_Struct(obj, gsl_odeiv_system, sys);
|
240
|
+
|
241
|
+
ary = (VALUE) sys->params;
|
242
|
+
switch (argc) {
|
243
|
+
case 0:
|
244
|
+
vparams = Qnil;
|
245
|
+
break;
|
246
|
+
case 1:
|
247
|
+
vparams = argv[0];
|
248
|
+
break;
|
249
|
+
default:
|
250
|
+
vparams = rb_ary_new2(argc);
|
251
|
+
for (i = 0; i < argc; i++) rb_ary_store(vparams, i, argv[i]);
|
252
|
+
}
|
253
|
+
// rb_ary_delete_at(ary, 3);
|
254
|
+
rb_ary_store(ary, 3, vparams);
|
255
|
+
return obj;
|
256
|
+
}
|
257
|
+
|
258
|
+
static VALUE rb_gsl_odeiv_system_params(VALUE obj)
|
259
|
+
{
|
260
|
+
VALUE ary;
|
261
|
+
gsl_odeiv_system *sys = NULL;
|
262
|
+
Data_Get_Struct(obj, gsl_odeiv_system, sys);
|
263
|
+
ary = (VALUE) sys->params;
|
264
|
+
return rb_ary_entry(ary, 3);
|
265
|
+
}
|
266
|
+
|
267
|
+
static VALUE rb_gsl_odeiv_system_function(VALUE obj)
|
268
|
+
{
|
269
|
+
VALUE ary;
|
270
|
+
gsl_odeiv_system *sys = NULL;
|
271
|
+
Data_Get_Struct(obj, gsl_odeiv_system, sys);
|
272
|
+
ary = (VALUE) sys->params;
|
273
|
+
return rb_ary_entry(ary, 0);
|
274
|
+
}
|
275
|
+
|
276
|
+
static VALUE rb_gsl_odeiv_system_jacobian(VALUE obj)
|
277
|
+
{
|
278
|
+
VALUE ary;
|
279
|
+
gsl_odeiv_system *sys = NULL;
|
280
|
+
Data_Get_Struct(obj, gsl_odeiv_system, sys);
|
281
|
+
ary = (VALUE) sys->params;
|
282
|
+
return rb_ary_entry(ary, 1);
|
283
|
+
}
|
284
|
+
|
285
|
+
static VALUE rb_gsl_odeiv_system_dimension(VALUE obj)
|
286
|
+
{
|
287
|
+
gsl_odeiv_system *sys = NULL;
|
288
|
+
Data_Get_Struct(obj, gsl_odeiv_system, sys);
|
289
|
+
return INT2FIX(sys->dimension);
|
290
|
+
}
|
291
|
+
|
292
|
+
static const gsl_odeiv_step_type* rb_gsl_odeiv_step_type_get(VALUE tt);
|
293
|
+
|
294
|
+
static gsl_odeiv_step* make_step(VALUE tt, VALUE dim);
|
295
|
+
static VALUE rb_gsl_odeiv_step_new(int argc, VALUE *argv, VALUE klass)
|
296
|
+
{
|
297
|
+
VALUE obj;
|
298
|
+
gsl_odeiv_step *s = NULL;
|
299
|
+
switch (argc) {
|
300
|
+
case 1:
|
301
|
+
CHECK_FIXNUM(argv[0]);
|
302
|
+
s = make_step(INT2FIX(GSL_ODEIV_STEP_RKF45), argv[0]);
|
303
|
+
break;
|
304
|
+
case 2:
|
305
|
+
CHECK_FIXNUM(argv[1]);
|
306
|
+
s = make_step(argv[0], argv[1]);
|
307
|
+
break;
|
308
|
+
default:
|
309
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
|
310
|
+
}
|
311
|
+
obj = Data_Wrap_Struct(klass, 0, gsl_odeiv_step_free, s);
|
312
|
+
return obj;
|
313
|
+
}
|
314
|
+
|
315
|
+
static gsl_odeiv_step* make_step(VALUE tt, VALUE dim)
|
316
|
+
{
|
317
|
+
const gsl_odeiv_step_type *T;
|
318
|
+
T = rb_gsl_odeiv_step_type_get(tt);
|
319
|
+
return gsl_odeiv_step_alloc(T, FIX2INT(dim));
|
320
|
+
}
|
321
|
+
|
322
|
+
static const gsl_odeiv_step_type* rb_gsl_odeiv_step_type_get(VALUE tt)
|
323
|
+
{
|
324
|
+
const gsl_odeiv_step_type *T;
|
325
|
+
int type;
|
326
|
+
char name[64];
|
327
|
+
switch (TYPE(tt)) {
|
328
|
+
case T_FIXNUM:
|
329
|
+
type = FIX2INT(tt);
|
330
|
+
switch (type) {
|
331
|
+
case GSL_ODEIV_STEP_RK2: T = gsl_odeiv_step_rk2; break;
|
332
|
+
case GSL_ODEIV_STEP_RK4: T = gsl_odeiv_step_rk4; break;
|
333
|
+
case GSL_ODEIV_STEP_RKF45: T = gsl_odeiv_step_rkf45; break;
|
334
|
+
case GSL_ODEIV_STEP_RKCK: T = gsl_odeiv_step_rkck; break;
|
335
|
+
case GSL_ODEIV_STEP_RK8PD: T = gsl_odeiv_step_rk8pd; break;
|
336
|
+
case GSL_ODEIV_STEP_RK2IMP: T = gsl_odeiv_step_rk2imp; break;
|
337
|
+
case GSL_ODEIV_STEP_RK4IMP: T = gsl_odeiv_step_rk4imp; break;
|
338
|
+
case GSL_ODEIV_STEP_BSIMP: T = gsl_odeiv_step_bsimp; break;
|
339
|
+
case GSL_ODEIV_STEP_GEAR1: T = gsl_odeiv_step_gear1; break;
|
340
|
+
case GSL_ODEIV_STEP_GEAR2: T = gsl_odeiv_step_gear2; break;
|
341
|
+
#ifdef GSL_1_6_LATER
|
342
|
+
case GSL_ODEIV_STEP_RK2SIMP: T = gsl_odeiv_step_rk2simp; break;
|
343
|
+
#endif
|
344
|
+
default:
|
345
|
+
rb_raise(rb_eArgError, "wrong argument type (Fixnum expected)");
|
346
|
+
break;
|
347
|
+
}
|
348
|
+
break;
|
349
|
+
case T_STRING:
|
350
|
+
strcpy(name, STR2CSTR(tt));
|
351
|
+
if (str_tail_grep(name, "rk2") == 0) T = gsl_odeiv_step_rk2;
|
352
|
+
else if (str_tail_grep(name, "rk4") == 0) T = gsl_odeiv_step_rk4;
|
353
|
+
else if (str_tail_grep(name, "rkf45") == 0) T = gsl_odeiv_step_rkf45;
|
354
|
+
else if (str_tail_grep(name, "rkck") == 0) T = gsl_odeiv_step_rkck;
|
355
|
+
else if (str_tail_grep(name, "rk8pd") == 0) T = gsl_odeiv_step_rk8pd;
|
356
|
+
else if (str_tail_grep(name, "rk2imp") == 0) T = gsl_odeiv_step_rk2imp;
|
357
|
+
else if (str_tail_grep(name, "rk4imp") == 0) T = gsl_odeiv_step_rk4imp;
|
358
|
+
else if (str_tail_grep(name, "bsimp") == 0) T = gsl_odeiv_step_bsimp;
|
359
|
+
else if (str_tail_grep(name, "gear1") == 0) T = gsl_odeiv_step_gear1;
|
360
|
+
else if (str_tail_grep(name, "gear2") == 0) T = gsl_odeiv_step_gear2;
|
361
|
+
#ifdef GSL_1_6_LATER
|
362
|
+
else if (str_tail_grep(name, "rk2simp") == 0) T = gsl_odeiv_step_rk2simp;
|
363
|
+
#endif
|
364
|
+
else {
|
365
|
+
rb_raise(rb_eArgError, "wrong argument type %s", name);
|
366
|
+
}
|
367
|
+
break;
|
368
|
+
default:
|
369
|
+
rb_raise(rb_eArgError, "wrong argument type %s (String or Fixnum expected)",
|
370
|
+
rb_class2name(CLASS_OF(tt)));
|
371
|
+
break;
|
372
|
+
}
|
373
|
+
return T;
|
374
|
+
}
|
375
|
+
|
376
|
+
static VALUE rb_gsl_odeiv_step_reset(VALUE obj)
|
377
|
+
{
|
378
|
+
gsl_odeiv_step *s = NULL;
|
379
|
+
Data_Get_Struct(obj, gsl_odeiv_step, s);
|
380
|
+
return INT2FIX(gsl_odeiv_step_reset(s));
|
381
|
+
}
|
382
|
+
|
383
|
+
static VALUE rb_gsl_odeiv_step_name(VALUE obj)
|
384
|
+
{
|
385
|
+
gsl_odeiv_step *s = NULL;
|
386
|
+
Data_Get_Struct(obj, gsl_odeiv_step, s);
|
387
|
+
return rb_str_new2(gsl_odeiv_step_name(s));
|
388
|
+
}
|
389
|
+
|
390
|
+
static VALUE rb_gsl_odeiv_step_order(VALUE obj)
|
391
|
+
{
|
392
|
+
gsl_odeiv_step *s = NULL;
|
393
|
+
Data_Get_Struct(obj, gsl_odeiv_step, s);
|
394
|
+
return INT2FIX(gsl_odeiv_step_order(s));
|
395
|
+
}
|
396
|
+
|
397
|
+
static VALUE rb_gsl_odeiv_step_dimension(VALUE obj)
|
398
|
+
{
|
399
|
+
gsl_odeiv_step *s = NULL;
|
400
|
+
Data_Get_Struct(obj, gsl_odeiv_step, s);
|
401
|
+
return INT2FIX(s->dimension);
|
402
|
+
}
|
403
|
+
|
404
|
+
static VALUE rb_gsl_odeiv_step_apply(int argc, VALUE *argv, VALUE obj)
|
405
|
+
{
|
406
|
+
gsl_odeiv_step *s = NULL;
|
407
|
+
gsl_odeiv_system *sys = NULL;
|
408
|
+
gsl_vector *y = NULL, *yerr = NULL;
|
409
|
+
gsl_vector *vtmp1 = NULL, *vtmp2 = NULL;
|
410
|
+
double *dydt_in = NULL, *dydt_out = NULL;
|
411
|
+
double t, h;
|
412
|
+
switch (argc) {
|
413
|
+
case 5:
|
414
|
+
break;
|
415
|
+
case 7:
|
416
|
+
if (VECTOR_P(argv[5])) {
|
417
|
+
Data_Get_Struct(argv[5], gsl_vector, vtmp2);
|
418
|
+
if (vtmp2) dydt_out = vtmp2->data;
|
419
|
+
}
|
420
|
+
/* no break */
|
421
|
+
case 6:
|
422
|
+
if (VECTOR_P(argv[4])) {
|
423
|
+
Data_Get_Struct(argv[4], gsl_vector, vtmp1);
|
424
|
+
if (vtmp1) dydt_in = vtmp1->data;
|
425
|
+
}
|
426
|
+
break;
|
427
|
+
default:
|
428
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 5, 6 or 7)", argc);
|
429
|
+
break;
|
430
|
+
}
|
431
|
+
Need_Float(argv[0]); Need_Float(argv[1]);
|
432
|
+
CHECK_VECTOR(argv[2]); CHECK_VECTOR(argv[3]);
|
433
|
+
CHECK_SYSTEM(argv[argc-1]);
|
434
|
+
Data_Get_Struct(obj, gsl_odeiv_step, s);
|
435
|
+
t = NUM2DBL(argv[0]);
|
436
|
+
h = NUM2DBL(argv[1]);
|
437
|
+
Data_Get_Struct(argv[2], gsl_vector, y);
|
438
|
+
Data_Get_Struct(argv[3], gsl_vector, yerr);
|
439
|
+
Data_Get_Struct(argv[argc-1], gsl_odeiv_system, sys);
|
440
|
+
return INT2FIX(gsl_odeiv_step_apply(s, t, h, y->data, yerr->data,
|
441
|
+
dydt_in, dydt_out, sys));
|
442
|
+
}
|
443
|
+
|
444
|
+
static VALUE rb_gsl_odeiv_step_info(VALUE obj)
|
445
|
+
{
|
446
|
+
gsl_odeiv_step *s;
|
447
|
+
char buf[256];
|
448
|
+
Data_Get_Struct(obj, gsl_odeiv_step, s);
|
449
|
+
sprintf(buf, "Class: %s\n", rb_class2name(CLASS_OF(obj)));
|
450
|
+
#ifdef RUBY_1_9_LATER
|
451
|
+
sprintf(buf, "%sSuperClass: %s\n", buf, rb_class2name(RCLASS_SUPER(CLASS_OF(obj))));
|
452
|
+
#else
|
453
|
+
sprintf(buf, "%sSuperClass: %s\n", buf, rb_class2name(RCLASS(CLASS_OF(obj))->super));
|
454
|
+
#endif
|
455
|
+
sprintf(buf, "%sType: %s\n", buf, gsl_odeiv_step_name(s));
|
456
|
+
sprintf(buf, "%sDimension: %d\n", buf, (int) s->dimension);
|
457
|
+
return rb_str_new2(buf);
|
458
|
+
}
|
459
|
+
|
460
|
+
static gsl_odeiv_control* make_control_standard(VALUE epsabs,
|
461
|
+
VALUE epsrel,
|
462
|
+
VALUE ay, VALUE adydt);
|
463
|
+
static gsl_odeiv_control* make_control_y(VALUE epsabs, VALUE epsrel);
|
464
|
+
static VALUE rb_gsl_odeiv_control_standard_new(VALUE klass, VALUE epsabs,
|
465
|
+
VALUE epsrel,
|
466
|
+
VALUE ay, VALUE adydt)
|
467
|
+
{
|
468
|
+
gsl_odeiv_control *c = NULL;
|
469
|
+
c = make_control_standard(epsabs, epsrel, ay, adydt);
|
470
|
+
return Data_Wrap_Struct(klass, 0, gsl_odeiv_control_free, c);
|
471
|
+
}
|
472
|
+
|
473
|
+
static gsl_odeiv_control* make_control_standard(VALUE epsabs,
|
474
|
+
VALUE epsrel,
|
475
|
+
VALUE ay, VALUE adydt)
|
476
|
+
{
|
477
|
+
Need_Float(epsabs); Need_Float(epsrel);
|
478
|
+
Need_Float(ay); Need_Float(adydt);
|
479
|
+
return gsl_odeiv_control_standard_new(NUM2DBL(epsabs), NUM2DBL(epsrel),
|
480
|
+
NUM2DBL(ay), NUM2DBL(adydt));
|
481
|
+
}
|
482
|
+
|
483
|
+
static gsl_odeiv_control* make_control_y(VALUE epsabs, VALUE epsrel)
|
484
|
+
{
|
485
|
+
Need_Float(epsabs); Need_Float(epsrel);
|
486
|
+
return gsl_odeiv_control_y_new(NUM2DBL(epsabs), NUM2DBL(epsrel));
|
487
|
+
}
|
488
|
+
|
489
|
+
static VALUE rb_gsl_odeiv_control_y_new(VALUE klass, VALUE epsabs,
|
490
|
+
VALUE epsrel)
|
491
|
+
{
|
492
|
+
gsl_odeiv_control *c = NULL;
|
493
|
+
c = make_control_y(epsabs, epsrel);
|
494
|
+
return Data_Wrap_Struct(klass, 0, gsl_odeiv_control_free, c);
|
495
|
+
}
|
496
|
+
|
497
|
+
static VALUE rb_gsl_odeiv_control_yp_new(VALUE klass, VALUE epsabs,
|
498
|
+
VALUE epsrel)
|
499
|
+
{
|
500
|
+
gsl_odeiv_control *c = NULL;
|
501
|
+
Need_Float(epsabs); Need_Float(epsrel);
|
502
|
+
c = gsl_odeiv_control_yp_new(NUM2DBL(epsabs), NUM2DBL(epsrel));
|
503
|
+
return Data_Wrap_Struct(klass, 0, gsl_odeiv_control_free, c);
|
504
|
+
}
|
505
|
+
|
506
|
+
#ifdef GSL_1_2_LATER
|
507
|
+
static VALUE rb_gsl_odeiv_control_scaled_new(VALUE klass, VALUE epsabs,
|
508
|
+
VALUE epsrel,
|
509
|
+
VALUE ay, VALUE adydt,
|
510
|
+
VALUE sc, VALUE dd)
|
511
|
+
{
|
512
|
+
gsl_odeiv_control *c = NULL;
|
513
|
+
gsl_vector *v = NULL;
|
514
|
+
Need_Float(epsabs); Need_Float(epsrel);
|
515
|
+
Need_Float(ay); Need_Float(adydt);
|
516
|
+
CHECK_FIXNUM(dd);
|
517
|
+
CHECK_VECTOR(sc);
|
518
|
+
Data_Get_Struct(sc, gsl_vector, v);
|
519
|
+
c = gsl_odeiv_control_scaled_new(NUM2DBL(epsabs), NUM2DBL(epsrel),
|
520
|
+
NUM2DBL(ay), NUM2DBL(adydt), v->data,
|
521
|
+
FIX2INT(dd));
|
522
|
+
return Data_Wrap_Struct(klass, 0, gsl_odeiv_control_free, c);
|
523
|
+
}
|
524
|
+
#endif
|
525
|
+
|
526
|
+
static VALUE rb_gsl_odeiv_control_init(VALUE obj, VALUE epsabs,
|
527
|
+
VALUE epsrel,
|
528
|
+
VALUE ay, VALUE adydt)
|
529
|
+
{
|
530
|
+
gsl_odeiv_control *c = NULL;
|
531
|
+
Need_Float(epsabs); Need_Float(epsrel);
|
532
|
+
Need_Float(ay); Need_Float(adydt);
|
533
|
+
Data_Get_Struct(obj, gsl_odeiv_control, c);
|
534
|
+
gsl_odeiv_control_init(c, NUM2DBL(epsabs), NUM2DBL(epsrel),
|
535
|
+
NUM2DBL(ay), NUM2DBL(adydt));
|
536
|
+
return obj;
|
537
|
+
}
|
538
|
+
|
539
|
+
static VALUE rb_gsl_odeiv_control_name(VALUE obj)
|
540
|
+
{
|
541
|
+
gsl_odeiv_control *c = NULL;
|
542
|
+
Data_Get_Struct(obj, gsl_odeiv_control, c);
|
543
|
+
return rb_str_new2(gsl_odeiv_control_name(c));
|
544
|
+
}
|
545
|
+
|
546
|
+
static VALUE rb_gsl_odeiv_control_hadjust(VALUE obj, VALUE ss, VALUE yy0,
|
547
|
+
VALUE yyerr, VALUE ddydt, VALUE hh)
|
548
|
+
{
|
549
|
+
gsl_odeiv_control *c = NULL;
|
550
|
+
gsl_odeiv_step *s = NULL;
|
551
|
+
gsl_vector *y0 = NULL, *yerr = NULL, *dydt = NULL;
|
552
|
+
double h;
|
553
|
+
int status;
|
554
|
+
CHECK_VECTOR(yy0);
|
555
|
+
CHECK_VECTOR(yyerr);
|
556
|
+
CHECK_VECTOR(ddydt);
|
557
|
+
Data_Get_Struct(obj, gsl_odeiv_control, c);
|
558
|
+
Data_Get_Struct(ss, gsl_odeiv_step, s);
|
559
|
+
Data_Get_Struct(yy0, gsl_vector, y0);
|
560
|
+
Data_Get_Struct(yyerr, gsl_vector, yerr);
|
561
|
+
Data_Get_Struct(ddydt, gsl_vector, dydt);
|
562
|
+
h = NUM2DBL(hh);
|
563
|
+
status = gsl_odeiv_control_hadjust(c, s, y0->data, yerr->data,
|
564
|
+
dydt->data, &h);
|
565
|
+
return rb_ary_new3(2, rb_float_new(h), INT2FIX(status));
|
566
|
+
}
|
567
|
+
|
568
|
+
static gsl_odeiv_evolve* make_evolve(VALUE dim);
|
569
|
+
static VALUE rb_gsl_odeiv_evolve_new(VALUE klass, VALUE dim)
|
570
|
+
{
|
571
|
+
gsl_odeiv_evolve *e = NULL;
|
572
|
+
e = make_evolve(dim);
|
573
|
+
return Data_Wrap_Struct(klass, 0, gsl_odeiv_evolve_free, e);
|
574
|
+
}
|
575
|
+
|
576
|
+
static gsl_odeiv_evolve* make_evolve(VALUE dim)
|
577
|
+
{
|
578
|
+
return gsl_odeiv_evolve_alloc(FIX2INT(dim));
|
579
|
+
}
|
580
|
+
|
581
|
+
static VALUE rb_gsl_odeiv_evolve_reset(VALUE obj)
|
582
|
+
{
|
583
|
+
gsl_odeiv_evolve *e = NULL;
|
584
|
+
Data_Get_Struct(obj, gsl_odeiv_evolve, e);
|
585
|
+
return INT2FIX(gsl_odeiv_evolve_reset(e));
|
586
|
+
}
|
587
|
+
|
588
|
+
static VALUE rb_gsl_odeiv_evolve_count(VALUE obj)
|
589
|
+
{
|
590
|
+
gsl_odeiv_evolve *e = NULL;
|
591
|
+
Data_Get_Struct(obj, gsl_odeiv_evolve, e);
|
592
|
+
return INT2FIX(e->count);
|
593
|
+
}
|
594
|
+
|
595
|
+
static VALUE rb_gsl_odeiv_evolve_dimension(VALUE obj)
|
596
|
+
{
|
597
|
+
gsl_odeiv_evolve *e = NULL;
|
598
|
+
Data_Get_Struct(obj, gsl_odeiv_evolve, e);
|
599
|
+
return INT2FIX(e->dimension);
|
600
|
+
}
|
601
|
+
|
602
|
+
static VALUE rb_gsl_odeiv_evolve_failed_steps(VALUE obj)
|
603
|
+
{
|
604
|
+
gsl_odeiv_evolve *e = NULL;
|
605
|
+
Data_Get_Struct(obj, gsl_odeiv_evolve, e);
|
606
|
+
return INT2FIX(e->failed_steps);
|
607
|
+
}
|
608
|
+
|
609
|
+
static VALUE rb_gsl_odeiv_evolve_last_step(VALUE obj)
|
610
|
+
{
|
611
|
+
gsl_odeiv_evolve *e = NULL;
|
612
|
+
Data_Get_Struct(obj, gsl_odeiv_evolve, e);
|
613
|
+
return rb_float_new(e->last_step);
|
614
|
+
}
|
615
|
+
|
616
|
+
static VALUE rb_gsl_odeiv_evolve_y0(VALUE obj)
|
617
|
+
{
|
618
|
+
gsl_odeiv_evolve *e = NULL;
|
619
|
+
gsl_vector_view *v = NULL;
|
620
|
+
Data_Get_Struct(obj, gsl_odeiv_evolve, e);
|
621
|
+
v = gsl_vector_view_alloc();
|
622
|
+
v->vector.data = e->y0;
|
623
|
+
v->vector.size = e->dimension;
|
624
|
+
v->vector.stride = 1;
|
625
|
+
v->vector.owner = 0;
|
626
|
+
return Data_Wrap_Struct(cgsl_vector_view_ro, 0, gsl_vector_view_free, v);
|
627
|
+
}
|
628
|
+
|
629
|
+
static VALUE rb_gsl_odeiv_evolve_yerr(VALUE obj)
|
630
|
+
{
|
631
|
+
gsl_odeiv_evolve *e = NULL;
|
632
|
+
gsl_vector_view *v = NULL;
|
633
|
+
Data_Get_Struct(obj, gsl_odeiv_evolve, e);
|
634
|
+
v = gsl_vector_view_alloc();
|
635
|
+
v->vector.data = e->yerr;
|
636
|
+
v->vector.size = e->dimension;
|
637
|
+
v->vector.stride = 1;
|
638
|
+
v->vector.owner = 0;
|
639
|
+
return Data_Wrap_Struct(cgsl_vector_view_ro, 0, gsl_vector_view_free, v);
|
640
|
+
}
|
641
|
+
|
642
|
+
static VALUE rb_gsl_odeiv_evolve_apply(VALUE obj, VALUE cc, VALUE ss, VALUE sss,
|
643
|
+
VALUE tt, VALUE tt1, VALUE hh, VALUE yy)
|
644
|
+
{
|
645
|
+
gsl_odeiv_evolve *e = NULL;
|
646
|
+
gsl_odeiv_control *c = NULL;
|
647
|
+
gsl_odeiv_step *s = NULL;
|
648
|
+
gsl_odeiv_system *sys = NULL;
|
649
|
+
gsl_vector *y = NULL;
|
650
|
+
double t, h;
|
651
|
+
int status;
|
652
|
+
CHECK_STEP(ss); CHECK_SYSTEM(sss);
|
653
|
+
CHECK_VECTOR(yy);
|
654
|
+
Data_Get_Struct(obj, gsl_odeiv_evolve, e);
|
655
|
+
if (NIL_P(cc)) {
|
656
|
+
c = NULL;
|
657
|
+
} else {
|
658
|
+
CHECK_CONTROL(cc);
|
659
|
+
Data_Get_Struct(cc, gsl_odeiv_control, c);
|
660
|
+
}
|
661
|
+
Data_Get_Struct(ss, gsl_odeiv_step, s);
|
662
|
+
Data_Get_Struct(sss, gsl_odeiv_system, sys);
|
663
|
+
Data_Get_Struct(yy, gsl_vector, y);
|
664
|
+
/* if (TYPE(tt) != T_FLOAT) rb_raise(rb_eTypeError, "argument 4 Float expected");
|
665
|
+
if (TYPE(hh) != T_FLOAT) rb_raise(rb_eTypeError, "argument 6 Float expected");*/
|
666
|
+
t = NUM2DBL(tt);
|
667
|
+
h = NUM2DBL(hh);
|
668
|
+
status = gsl_odeiv_evolve_apply(e, c, s, sys, &t, NUM2DBL(tt1), &h, y->data);
|
669
|
+
/* RFLOAT(tt)->value = t;
|
670
|
+
RFLOAT(hh)->value = h;*/
|
671
|
+
return rb_ary_new3(3, rb_float_new(t), rb_float_new(h), INT2FIX(status));
|
672
|
+
}
|
673
|
+
|
674
|
+
static void rb_gsl_odeiv_solver_free(gsl_odeiv_solver *gde);
|
675
|
+
static void gsl_odeiv_solver_mark(gsl_odeiv_solver *gos);
|
676
|
+
static VALUE rb_gsl_odeiv_solver_new(int argc, VALUE *argv, VALUE klass)
|
677
|
+
{
|
678
|
+
gsl_odeiv_solver *gos = NULL;
|
679
|
+
VALUE epsabs, epsrel, ay, adydt;
|
680
|
+
VALUE dim;
|
681
|
+
if (argc < 4) rb_raise(rb_eArgError, "too few arguments");
|
682
|
+
Check_Type(argv[1], T_ARRAY);
|
683
|
+
CHECK_PROC(argv[2]);
|
684
|
+
if (rb_obj_is_kind_of(argv[3], rb_cProc) || NIL_P(argv[3])) {
|
685
|
+
dim = argv[4];
|
686
|
+
} else {
|
687
|
+
dim = argv[3];
|
688
|
+
}
|
689
|
+
gos = ALLOC(gsl_odeiv_solver);
|
690
|
+
gos->s = make_step(argv[0], dim);
|
691
|
+
// switch (RARRAY(argv[1])->len) {
|
692
|
+
switch (RARRAY_LEN(argv[1])) {
|
693
|
+
case 2:
|
694
|
+
epsabs = rb_ary_entry(argv[1], 0);
|
695
|
+
epsrel = rb_ary_entry(argv[1], 1);
|
696
|
+
gos->c = make_control_y(epsabs, epsrel);
|
697
|
+
break;
|
698
|
+
case 4:
|
699
|
+
epsabs = rb_ary_entry(argv[1], 0);
|
700
|
+
epsrel = rb_ary_entry(argv[1], 1);
|
701
|
+
ay = rb_ary_entry(argv[1], 2);
|
702
|
+
adydt = rb_ary_entry(argv[1], 3);
|
703
|
+
gos->c = make_control_standard(epsabs, epsrel, ay, adydt);
|
704
|
+
break;
|
705
|
+
default:
|
706
|
+
rb_raise(rb_eArgError, "size of the argument 1 must be 2 or 4");
|
707
|
+
break;
|
708
|
+
}
|
709
|
+
gos->sys = make_sys(argc - 2, argv + 2);
|
710
|
+
gos->e = make_evolve(dim);
|
711
|
+
return Data_Wrap_Struct(klass, gsl_odeiv_solver_mark, rb_gsl_odeiv_solver_free, gos);
|
712
|
+
// return Data_Wrap_Struct(klass, 0, rb_gsl_odeiv_solver_free, gos);
|
713
|
+
}
|
714
|
+
|
715
|
+
static void gsl_odeiv_solver_mark(gsl_odeiv_solver *gos)
|
716
|
+
{
|
717
|
+
rb_gc_mark((VALUE) gos->sys->params);
|
718
|
+
}
|
719
|
+
|
720
|
+
static VALUE rb_gsl_odeiv_solver_evolve(VALUE obj)
|
721
|
+
{
|
722
|
+
gsl_odeiv_solver *gos = NULL;
|
723
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
724
|
+
return Data_Wrap_Struct(cgsl_odeiv_evolve, 0, NULL, gos->e);
|
725
|
+
}
|
726
|
+
|
727
|
+
static VALUE rb_gsl_odeiv_solver_set_evolve(VALUE obj, VALUE ee)
|
728
|
+
{
|
729
|
+
gsl_odeiv_solver *gos = NULL;
|
730
|
+
gsl_odeiv_evolve *e = NULL;
|
731
|
+
CHECK_EVOLVE(ee);
|
732
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
733
|
+
Data_Get_Struct(ee, gsl_odeiv_evolve, e);
|
734
|
+
gos->e = e;
|
735
|
+
return obj;
|
736
|
+
}
|
737
|
+
|
738
|
+
static VALUE rb_gsl_odeiv_solver_step(VALUE obj)
|
739
|
+
{
|
740
|
+
gsl_odeiv_solver *gos = NULL;
|
741
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
742
|
+
return Data_Wrap_Struct(cgsl_odeiv_step, 0, NULL,gos->s);
|
743
|
+
}
|
744
|
+
|
745
|
+
static VALUE rb_gsl_odeiv_solver_set_step(VALUE obj, VALUE ss)
|
746
|
+
{
|
747
|
+
gsl_odeiv_solver *gos = NULL;
|
748
|
+
gsl_odeiv_step *s = NULL;
|
749
|
+
CHECK_STEP(ss);
|
750
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
751
|
+
Data_Get_Struct(ss, gsl_odeiv_step, s);
|
752
|
+
gos->s = s;
|
753
|
+
return obj;
|
754
|
+
}
|
755
|
+
|
756
|
+
static VALUE rb_gsl_odeiv_solver_control(VALUE obj)
|
757
|
+
{
|
758
|
+
gsl_odeiv_solver *gos = NULL;
|
759
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
760
|
+
return Data_Wrap_Struct(cgsl_odeiv_control, 0, NULL, gos->c);
|
761
|
+
}
|
762
|
+
|
763
|
+
static VALUE rb_gsl_odeiv_solver_set_control(VALUE obj, VALUE cc)
|
764
|
+
{
|
765
|
+
gsl_odeiv_solver *gos = NULL;
|
766
|
+
gsl_odeiv_control *c = NULL;
|
767
|
+
CHECK_CONTROL(cc);
|
768
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
769
|
+
Data_Get_Struct(cc, gsl_odeiv_control, c);
|
770
|
+
gos->c = c;
|
771
|
+
return obj;
|
772
|
+
}
|
773
|
+
|
774
|
+
static VALUE rb_gsl_odeiv_solver_sys(VALUE obj)
|
775
|
+
{
|
776
|
+
gsl_odeiv_solver *gos = NULL;
|
777
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
778
|
+
return Data_Wrap_Struct(cgsl_odeiv_system, 0, NULL, gos->sys);
|
779
|
+
}
|
780
|
+
|
781
|
+
static VALUE rb_gsl_odeiv_solver_set_sys(VALUE obj, VALUE ss)
|
782
|
+
{
|
783
|
+
gsl_odeiv_solver *gos = NULL;
|
784
|
+
gsl_odeiv_system *sys = NULL;
|
785
|
+
CHECK_SYSTEM(ss);
|
786
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
787
|
+
Data_Get_Struct(ss, gsl_odeiv_system, sys);
|
788
|
+
gos->sys = sys;
|
789
|
+
return obj;
|
790
|
+
}
|
791
|
+
|
792
|
+
static VALUE rb_gsl_odeiv_solver_apply(VALUE obj, VALUE tt, VALUE tt1, VALUE hh,
|
793
|
+
VALUE yy)
|
794
|
+
{
|
795
|
+
gsl_odeiv_solver *gos = NULL;
|
796
|
+
gsl_vector *y = NULL;
|
797
|
+
double t, h;
|
798
|
+
int status;
|
799
|
+
CHECK_VECTOR(yy);
|
800
|
+
Need_Float(tt1);
|
801
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
802
|
+
Data_Get_Struct(yy, gsl_vector, y);
|
803
|
+
/* if (TYPE(tt) != T_FLOAT) rb_raise(rb_eTypeError, "argument 0 Float expected");
|
804
|
+
if (TYPE(hh) != T_FLOAT) rb_raise(rb_eTypeError, "argument 2 Float expected");*/
|
805
|
+
t = NUM2DBL(tt);
|
806
|
+
h = NUM2DBL(hh);
|
807
|
+
status = gsl_odeiv_evolve_apply(gos->e, gos->c, gos->s,
|
808
|
+
gos->sys, &t, NUM2DBL(tt1), &h, y->data);
|
809
|
+
/* RFLOAT(tt)->value = t;
|
810
|
+
RFLOAT(hh)->value = h;
|
811
|
+
return INT2FIX(status);*/
|
812
|
+
return rb_ary_new3(3, rb_float_new(t), rb_float_new(h), INT2FIX(status));
|
813
|
+
}
|
814
|
+
|
815
|
+
static void rb_gsl_odeiv_solver_free(gsl_odeiv_solver *gos)
|
816
|
+
{
|
817
|
+
free((gsl_odeiv_solver *) gos);
|
818
|
+
}
|
819
|
+
|
820
|
+
static VALUE rb_gsl_odeiv_solver_reset(VALUE obj)
|
821
|
+
{
|
822
|
+
gsl_odeiv_solver *gos = NULL;
|
823
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
824
|
+
gsl_odeiv_step_reset(gos->s);
|
825
|
+
gsl_odeiv_evolve_reset(gos->e);
|
826
|
+
return obj;
|
827
|
+
}
|
828
|
+
|
829
|
+
static VALUE rb_gsl_odeiv_solver_dim(VALUE obj)
|
830
|
+
{
|
831
|
+
gsl_odeiv_solver *gos = NULL;
|
832
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
833
|
+
return INT2FIX(gos->sys->dimension);
|
834
|
+
}
|
835
|
+
|
836
|
+
static VALUE rb_gsl_odeiv_solver_set_params(int argc, VALUE *argv, VALUE obj)
|
837
|
+
{
|
838
|
+
gsl_odeiv_solver *gos = NULL;
|
839
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, gos);
|
840
|
+
rb_gsl_odeiv_system_set_params(argc, argv,
|
841
|
+
Data_Wrap_Struct(cgsl_odeiv_system, 0, NULL, gos->sys));
|
842
|
+
return obj;
|
843
|
+
}
|
844
|
+
|
845
|
+
static VALUE rb_gsl_odeiv_solver_params(VALUE obj)
|
846
|
+
{
|
847
|
+
VALUE ary;
|
848
|
+
gsl_odeiv_solver *solver = NULL;
|
849
|
+
Data_Get_Struct(obj, gsl_odeiv_solver, solver);
|
850
|
+
ary = (VALUE) solver->sys->params;
|
851
|
+
return rb_ary_entry(ary, 3);
|
852
|
+
}
|
853
|
+
|
854
|
+
void Init_gsl_odeiv(VALUE module)
|
855
|
+
{
|
856
|
+
VALUE mgsl_odeiv;
|
857
|
+
mgsl_odeiv = rb_define_module_under(module, "Odeiv");
|
858
|
+
rb_define_const(mgsl_odeiv, "HADJ_DEC", INT2FIX(GSL_ODEIV_HADJ_DEC));
|
859
|
+
rb_define_const(mgsl_odeiv, "HADJ_INC", INT2FIX(GSL_ODEIV_HADJ_INC));
|
860
|
+
rb_define_const(mgsl_odeiv, "HADJ_NIL", INT2FIX(GSL_ODEIV_HADJ_NIL));
|
861
|
+
cgsl_odeiv_step = rb_define_class_under(mgsl_odeiv, "Step",
|
862
|
+
cGSL_Object);
|
863
|
+
rb_define_singleton_method(cgsl_odeiv_step, "alloc", rb_gsl_odeiv_step_new, -1);
|
864
|
+
|
865
|
+
/*****/
|
866
|
+
rb_define_const(cgsl_odeiv_step, "RK2", INT2FIX(GSL_ODEIV_STEP_RK2));
|
867
|
+
rb_define_const(cgsl_odeiv_step, "RK4", INT2FIX(GSL_ODEIV_STEP_RK4));
|
868
|
+
rb_define_const(cgsl_odeiv_step, "RKF45", INT2FIX(GSL_ODEIV_STEP_RKF45));
|
869
|
+
rb_define_const(cgsl_odeiv_step, "RKCK", INT2FIX(GSL_ODEIV_STEP_RKCK));
|
870
|
+
rb_define_const(cgsl_odeiv_step, "RK8PD", INT2FIX(GSL_ODEIV_STEP_RK8PD));
|
871
|
+
rb_define_const(cgsl_odeiv_step, "RK2IMP", INT2FIX(GSL_ODEIV_STEP_RK2IMP));
|
872
|
+
rb_define_const(cgsl_odeiv_step, "RK4IMP", INT2FIX(GSL_ODEIV_STEP_RK4IMP));
|
873
|
+
rb_define_const(cgsl_odeiv_step, "BSIMP", INT2FIX(GSL_ODEIV_STEP_BSIMP));
|
874
|
+
rb_define_const(cgsl_odeiv_step, "GEAR1", INT2FIX(GSL_ODEIV_STEP_GEAR1));
|
875
|
+
rb_define_const(cgsl_odeiv_step, "GEAR2", INT2FIX(GSL_ODEIV_STEP_GEAR2));
|
876
|
+
rb_define_const(cgsl_odeiv_step, "RK2SIMP", INT2FIX(GSL_ODEIV_STEP_RK2SIMP));
|
877
|
+
/*****/
|
878
|
+
|
879
|
+
rb_define_method(cgsl_odeiv_step, "reset", rb_gsl_odeiv_step_reset, 0);
|
880
|
+
rb_define_method(cgsl_odeiv_step, "name", rb_gsl_odeiv_step_name, 0);
|
881
|
+
rb_define_method(cgsl_odeiv_step, "order", rb_gsl_odeiv_step_order, 0);
|
882
|
+
rb_define_method(cgsl_odeiv_step, "dimension", rb_gsl_odeiv_step_dimension, 0);
|
883
|
+
rb_define_alias(cgsl_odeiv_step, "dim", "dimension");
|
884
|
+
rb_define_method(cgsl_odeiv_step, "apply", rb_gsl_odeiv_step_apply, -1);
|
885
|
+
rb_define_method(cgsl_odeiv_step, "info", rb_gsl_odeiv_step_info, 0);
|
886
|
+
|
887
|
+
/****/
|
888
|
+
|
889
|
+
cgsl_odeiv_control = rb_define_class_under(mgsl_odeiv, "Control",
|
890
|
+
cGSL_Object);
|
891
|
+
rb_define_singleton_method(cgsl_odeiv_control, "alloc", rb_gsl_odeiv_control_standard_new, 4);
|
892
|
+
rb_define_singleton_method(cgsl_odeiv_control, "standard_alloc", rb_gsl_odeiv_control_standard_new, 4);
|
893
|
+
rb_define_singleton_method(cgsl_odeiv_control, "y_new", rb_gsl_odeiv_control_y_new, 2);
|
894
|
+
rb_define_singleton_method(cgsl_odeiv_control, "yp_new", rb_gsl_odeiv_control_yp_new, 2);
|
895
|
+
#ifdef GSL_1_2_LATER
|
896
|
+
rb_define_singleton_method(cgsl_odeiv_control, "scaled_alloc", rb_gsl_odeiv_control_scaled_new, 5);
|
897
|
+
#endif
|
898
|
+
|
899
|
+
rb_define_method(cgsl_odeiv_control, "init", rb_gsl_odeiv_control_init, 4);
|
900
|
+
rb_define_method(cgsl_odeiv_control, "name", rb_gsl_odeiv_control_name, 0);
|
901
|
+
rb_define_method(cgsl_odeiv_control, "hadjust", rb_gsl_odeiv_control_hadjust, 5);
|
902
|
+
|
903
|
+
/****/
|
904
|
+
cgsl_odeiv_evolve = rb_define_class_under(mgsl_odeiv, "Evolve",
|
905
|
+
cGSL_Object);
|
906
|
+
rb_define_singleton_method(cgsl_odeiv_evolve, "alloc", rb_gsl_odeiv_evolve_new, 1);
|
907
|
+
|
908
|
+
rb_define_method(cgsl_odeiv_evolve, "reset", rb_gsl_odeiv_evolve_reset, 0);
|
909
|
+
rb_define_method(cgsl_odeiv_evolve, "apply", rb_gsl_odeiv_evolve_apply, 7);
|
910
|
+
rb_define_method(cgsl_odeiv_evolve, "count", rb_gsl_odeiv_evolve_count, 0);
|
911
|
+
rb_define_method(cgsl_odeiv_evolve, "dimension", rb_gsl_odeiv_evolve_dimension, 0);
|
912
|
+
rb_define_method(cgsl_odeiv_evolve, "failed_steps", rb_gsl_odeiv_evolve_failed_steps, 0);
|
913
|
+
rb_define_method(cgsl_odeiv_evolve, "last_step", rb_gsl_odeiv_evolve_last_step, 0);
|
914
|
+
rb_define_method(cgsl_odeiv_evolve, "y0", rb_gsl_odeiv_evolve_y0, 0);
|
915
|
+
rb_define_method(cgsl_odeiv_evolve, "yerr", rb_gsl_odeiv_evolve_yerr, 0);
|
916
|
+
/*****/
|
917
|
+
|
918
|
+
cgsl_odeiv_system = rb_define_class_under(mgsl_odeiv, "System",
|
919
|
+
cGSL_Object);
|
920
|
+
rb_define_singleton_method(cgsl_odeiv_system, "alloc", rb_gsl_odeiv_system_new, -1);
|
921
|
+
rb_define_method(cgsl_odeiv_system, "set", rb_gsl_odeiv_system_set, -1);
|
922
|
+
rb_define_method(cgsl_odeiv_system, "set_params",
|
923
|
+
rb_gsl_odeiv_system_set_params, -1);
|
924
|
+
rb_define_method(cgsl_odeiv_system, "params",
|
925
|
+
rb_gsl_odeiv_system_params, 0);
|
926
|
+
rb_define_method(cgsl_odeiv_system, "function",
|
927
|
+
rb_gsl_odeiv_system_function, 0);
|
928
|
+
rb_define_alias(cgsl_odeiv_system, "func", "function");
|
929
|
+
rb_define_method(cgsl_odeiv_system, "jacobian",
|
930
|
+
rb_gsl_odeiv_system_jacobian, 0);
|
931
|
+
rb_define_alias(cgsl_odeiv_system, "jac", "jacobian");
|
932
|
+
rb_define_method(cgsl_odeiv_system, "dimension", rb_gsl_odeiv_system_dimension, 0);
|
933
|
+
rb_define_alias(cgsl_odeiv_system, "dim", "dimension");
|
934
|
+
|
935
|
+
/*****/
|
936
|
+
cgsl_odeiv_solver = rb_define_class_under(mgsl_odeiv, "Solver", cGSL_Object);
|
937
|
+
rb_define_singleton_method(cgsl_odeiv_solver, "alloc", rb_gsl_odeiv_solver_new, -1);
|
938
|
+
rb_define_method(cgsl_odeiv_solver, "step", rb_gsl_odeiv_solver_step, 0);
|
939
|
+
rb_define_method(cgsl_odeiv_solver, "control", rb_gsl_odeiv_solver_control, 0);
|
940
|
+
rb_define_method(cgsl_odeiv_solver, "evolve", rb_gsl_odeiv_solver_evolve, 0);
|
941
|
+
rb_define_method(cgsl_odeiv_solver, "sys", rb_gsl_odeiv_solver_sys, 0);
|
942
|
+
rb_define_method(cgsl_odeiv_solver, "apply", rb_gsl_odeiv_solver_apply, 4);
|
943
|
+
|
944
|
+
rb_define_method(cgsl_odeiv_solver, "set_evolve", rb_gsl_odeiv_solver_set_evolve, 1);
|
945
|
+
rb_define_method(cgsl_odeiv_solver, "set_step", rb_gsl_odeiv_solver_set_step, 1);
|
946
|
+
rb_define_method(cgsl_odeiv_solver, "set_control", rb_gsl_odeiv_solver_set_control, 1);
|
947
|
+
rb_define_method(cgsl_odeiv_solver, "set_system", rb_gsl_odeiv_solver_set_sys, 1);
|
948
|
+
rb_define_method(cgsl_odeiv_solver, "reset", rb_gsl_odeiv_solver_reset, 0);
|
949
|
+
rb_define_method(cgsl_odeiv_solver, "dim", rb_gsl_odeiv_solver_dim, 0);
|
950
|
+
rb_define_alias(cgsl_odeiv_solver, "dimension", "dim");
|
951
|
+
rb_define_method(cgsl_odeiv_solver, "set_params", rb_gsl_odeiv_solver_set_params, -1);
|
952
|
+
rb_define_method(cgsl_odeiv_solver, "params", rb_gsl_odeiv_solver_params, 0);
|
953
|
+
}
|
954
|
+
|
955
|
+
#undef CHECK_SOLVER
|
956
|
+
#undef CHECK_EVOLVE
|
957
|
+
#undef CHECK_CONTROL
|
958
|
+
#undef CHECK_STEP
|
959
|
+
#undef CHECK_SYSTEM
|