gsl 1.12.108

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/README.rdoc +29 -0
  2. data/Rakefile +54 -0
  3. data/VERSION +2 -0
  4. data/ext/MANIFEST +119 -0
  5. data/ext/alf.c +206 -0
  6. data/ext/array.c +666 -0
  7. data/ext/array_complex.c +247 -0
  8. data/ext/blas.c +29 -0
  9. data/ext/blas1.c +731 -0
  10. data/ext/blas2.c +1093 -0
  11. data/ext/blas3.c +881 -0
  12. data/ext/block.c +44 -0
  13. data/ext/block_source.c +886 -0
  14. data/ext/bspline.c +130 -0
  15. data/ext/bundle.c +3 -0
  16. data/ext/cdf.c +754 -0
  17. data/ext/cheb.c +542 -0
  18. data/ext/combination.c +283 -0
  19. data/ext/common.c +325 -0
  20. data/ext/complex.c +1004 -0
  21. data/ext/const.c +673 -0
  22. data/ext/const_additional.c +120 -0
  23. data/ext/cqp.c +283 -0
  24. data/ext/deriv.c +195 -0
  25. data/ext/dht.c +361 -0
  26. data/ext/diff.c +166 -0
  27. data/ext/dirac.c +395 -0
  28. data/ext/eigen.c +2373 -0
  29. data/ext/error.c +194 -0
  30. data/ext/extconf.rb +281 -0
  31. data/ext/fcmp.c +66 -0
  32. data/ext/fft.c +1092 -0
  33. data/ext/fit.c +205 -0
  34. data/ext/fresnel.c +312 -0
  35. data/ext/function.c +524 -0
  36. data/ext/geometry.c +139 -0
  37. data/ext/graph.c +1638 -0
  38. data/ext/gsl.c +271 -0
  39. data/ext/gsl_narray.c +653 -0
  40. data/ext/histogram.c +1995 -0
  41. data/ext/histogram2d.c +1068 -0
  42. data/ext/histogram3d.c +884 -0
  43. data/ext/histogram3d_source.c +750 -0
  44. data/ext/histogram_find.c +101 -0
  45. data/ext/histogram_oper.c +159 -0
  46. data/ext/ieee.c +98 -0
  47. data/ext/integration.c +1138 -0
  48. data/ext/interp.c +512 -0
  49. data/ext/jacobi.c +739 -0
  50. data/ext/linalg.c +4047 -0
  51. data/ext/linalg_complex.c +741 -0
  52. data/ext/math.c +725 -0
  53. data/ext/matrix.c +39 -0
  54. data/ext/matrix_complex.c +1732 -0
  55. data/ext/matrix_double.c +560 -0
  56. data/ext/matrix_int.c +256 -0
  57. data/ext/matrix_source.c +2733 -0
  58. data/ext/min.c +250 -0
  59. data/ext/monte.c +992 -0
  60. data/ext/multifit.c +1879 -0
  61. data/ext/multimin.c +808 -0
  62. data/ext/multimin_fsdf.c +156 -0
  63. data/ext/multiroots.c +955 -0
  64. data/ext/ndlinear.c +321 -0
  65. data/ext/nmf.c +167 -0
  66. data/ext/nmf_wrap.c +72 -0
  67. data/ext/ntuple.c +469 -0
  68. data/ext/odeiv.c +959 -0
  69. data/ext/ool.c +879 -0
  70. data/ext/oper_complex_source.c +253 -0
  71. data/ext/permutation.c +596 -0
  72. data/ext/poly.c +42 -0
  73. data/ext/poly2.c +265 -0
  74. data/ext/poly_source.c +1885 -0
  75. data/ext/qrng.c +171 -0
  76. data/ext/randist.c +1873 -0
  77. data/ext/rational.c +480 -0
  78. data/ext/rng.c +612 -0
  79. data/ext/root.c +408 -0
  80. data/ext/sf.c +1494 -0
  81. data/ext/sf_airy.c +200 -0
  82. data/ext/sf_bessel.c +867 -0
  83. data/ext/sf_clausen.c +28 -0
  84. data/ext/sf_coulomb.c +206 -0
  85. data/ext/sf_coupling.c +118 -0
  86. data/ext/sf_dawson.c +29 -0
  87. data/ext/sf_debye.c +157 -0
  88. data/ext/sf_dilog.c +42 -0
  89. data/ext/sf_elementary.c +44 -0
  90. data/ext/sf_ellint.c +206 -0
  91. data/ext/sf_elljac.c +29 -0
  92. data/ext/sf_erfc.c +93 -0
  93. data/ext/sf_exp.c +164 -0
  94. data/ext/sf_expint.c +211 -0
  95. data/ext/sf_fermi_dirac.c +148 -0
  96. data/ext/sf_gamma.c +344 -0
  97. data/ext/sf_gegenbauer.c +96 -0
  98. data/ext/sf_hyperg.c +197 -0
  99. data/ext/sf_laguerre.c +112 -0
  100. data/ext/sf_lambert.c +47 -0
  101. data/ext/sf_legendre.c +367 -0
  102. data/ext/sf_log.c +104 -0
  103. data/ext/sf_mathieu.c +238 -0
  104. data/ext/sf_power.c +46 -0
  105. data/ext/sf_psi.c +98 -0
  106. data/ext/sf_synchrotron.c +48 -0
  107. data/ext/sf_transport.c +76 -0
  108. data/ext/sf_trigonometric.c +207 -0
  109. data/ext/sf_zeta.c +119 -0
  110. data/ext/signal.c +310 -0
  111. data/ext/siman.c +718 -0
  112. data/ext/sort.c +208 -0
  113. data/ext/spline.c +395 -0
  114. data/ext/stats.c +799 -0
  115. data/ext/sum.c +168 -0
  116. data/ext/tamu_anova.c +56 -0
  117. data/ext/tensor.c +38 -0
  118. data/ext/tensor_source.c +1123 -0
  119. data/ext/vector.c +38 -0
  120. data/ext/vector_complex.c +2236 -0
  121. data/ext/vector_double.c +1433 -0
  122. data/ext/vector_int.c +204 -0
  123. data/ext/vector_source.c +3329 -0
  124. data/ext/wavelet.c +937 -0
  125. data/include/rb_gsl.h +151 -0
  126. data/include/rb_gsl_array.h +238 -0
  127. data/include/rb_gsl_cheb.h +21 -0
  128. data/include/rb_gsl_common.h +343 -0
  129. data/include/rb_gsl_complex.h +25 -0
  130. data/include/rb_gsl_const.h +29 -0
  131. data/include/rb_gsl_dirac.h +13 -0
  132. data/include/rb_gsl_eigen.h +17 -0
  133. data/include/rb_gsl_fft.h +62 -0
  134. data/include/rb_gsl_fit.h +25 -0
  135. data/include/rb_gsl_function.h +27 -0
  136. data/include/rb_gsl_graph.h +70 -0
  137. data/include/rb_gsl_histogram.h +63 -0
  138. data/include/rb_gsl_histogram3d.h +97 -0
  139. data/include/rb_gsl_integration.h +17 -0
  140. data/include/rb_gsl_interp.h +46 -0
  141. data/include/rb_gsl_linalg.h +25 -0
  142. data/include/rb_gsl_math.h +26 -0
  143. data/include/rb_gsl_odeiv.h +21 -0
  144. data/include/rb_gsl_poly.h +71 -0
  145. data/include/rb_gsl_rational.h +37 -0
  146. data/include/rb_gsl_rng.h +21 -0
  147. data/include/rb_gsl_root.h +22 -0
  148. data/include/rb_gsl_sf.h +119 -0
  149. data/include/rb_gsl_statistics.h +17 -0
  150. data/include/rb_gsl_tensor.h +45 -0
  151. data/include/rb_gsl_with_narray.h +22 -0
  152. data/include/templates_off.h +87 -0
  153. data/include/templates_on.h +241 -0
  154. data/lib/gsl/gnuplot.rb +41 -0
  155. data/lib/gsl/oper.rb +68 -0
  156. data/lib/ool.rb +22 -0
  157. data/lib/ool/conmin.rb +30 -0
  158. metadata +224 -0
@@ -0,0 +1,725 @@
1
+ /*
2
+ math.c
3
+ Ruby/GSL: Ruby extension library for GSL (GNU Scientific Library)
4
+ (C) Copyright 2001-2006 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; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
+ */
12
+
13
+ #include "rb_gsl_config.h"
14
+ #include "rb_gsl_math.h"
15
+ #include "rb_gsl_complex.h"
16
+ #ifdef HAVE_NARRAY_H
17
+ #include "narray.h"
18
+ #endif
19
+
20
+ static void rb_gsl_define_const(VALUE module);
21
+
22
+ static void rb_gsl_define_const(VALUE module)
23
+ {
24
+ rb_define_const(module, "M_E", rb_float_new(M_E));
25
+ rb_define_const(module, "M_LOG2E", rb_float_new(M_LOG2E));
26
+ rb_define_const(module, "M_LOG10E", rb_float_new(M_LOG10E));
27
+ rb_define_const(module, "M_SQRT2", rb_float_new(M_SQRT2));
28
+ rb_define_const(module, "M_SQRT1_2", rb_float_new(M_SQRT1_2));
29
+ rb_define_const(module, "M_SQRT3", rb_float_new(M_SQRT3));
30
+ rb_define_const(module, "M_PI", rb_float_new(M_PI));
31
+ rb_define_const(module, "M_PI_2", rb_float_new(M_PI_2));
32
+ rb_define_const(module, "M_PI_4", rb_float_new(M_PI_4));
33
+ rb_define_const(module, "M_SQRTPI", rb_float_new(M_SQRTPI));
34
+ rb_define_const(module, "M_2_SQRTPI", rb_float_new(M_2_SQRTPI));
35
+ rb_define_const(module, "M_1_PI", rb_float_new(M_1_PI));
36
+ rb_define_const(module, "M_2_PI", rb_float_new(M_2_PI));
37
+ rb_define_const(module, "M_LN10", rb_float_new(M_LN10));
38
+ rb_define_const(module, "M_LN2", rb_float_new(M_LN2));
39
+ rb_define_const(module, "M_LNPI", rb_float_new(M_LNPI));
40
+ rb_define_const(module, "M_EULER", rb_float_new(M_EULER));
41
+ rb_define_const(module, "POSINF", rb_float_new(GSL_POSINF));
42
+ rb_define_const(module, "NEGINF", rb_float_new(GSL_NEGINF));
43
+ rb_define_const(module, "NAN", rb_float_new(GSL_NAN));
44
+ }
45
+
46
+ static VALUE rb_GSL_POSINF(VALUE obj)
47
+ {
48
+ return rb_float_new(GSL_POSINF);
49
+ }
50
+
51
+ static VALUE rb_GSL_NEGINF(VALUE obj)
52
+ {
53
+ return rb_float_new(GSL_NEGINF);
54
+ }
55
+
56
+ static VALUE rb_GSL_NAN(VALUE obj)
57
+ {
58
+ return rb_float_new(GSL_NAN);
59
+ }
60
+
61
+ static VALUE rb_gsl_isnan(VALUE obj, VALUE x)
62
+ {
63
+ Need_Float(x);
64
+ return INT2FIX(gsl_isnan(NUM2DBL(x)));
65
+ }
66
+
67
+ static VALUE rb_gsl_isnan2(VALUE obj, VALUE x)
68
+ {
69
+ Need_Float(x);
70
+ if (gsl_isnan(NUM2DBL(x))) return Qtrue;
71
+ else return Qfalse;
72
+ }
73
+
74
+ static VALUE rb_gsl_isinf(VALUE obj, VALUE x)
75
+ {
76
+ Need_Float(x);
77
+ return INT2FIX(gsl_isinf(NUM2DBL(x)));
78
+ }
79
+
80
+ static VALUE rb_gsl_isinf2(VALUE obj, VALUE x)
81
+ {
82
+ // Need_Float(x);
83
+ if (gsl_isinf(NUM2DBL(x))) return Qtrue;
84
+ else return Qfalse;
85
+ }
86
+
87
+ static VALUE rb_gsl_finite(VALUE obj, VALUE x)
88
+ {
89
+ Need_Float(x);
90
+ return INT2FIX(gsl_finite(NUM2DBL(x)));
91
+ }
92
+
93
+ static VALUE rb_gsl_finite2(VALUE obj, VALUE x)
94
+ {
95
+ Need_Float(x);
96
+ if (gsl_finite(NUM2DBL(x))) return Qtrue;
97
+ else return Qfalse;
98
+ }
99
+
100
+ /*****/
101
+
102
+ static VALUE rb_gsl_math_eval(double (*func)(const double), VALUE xx);
103
+
104
+ VALUE rb_gsl_math_complex_eval(gsl_complex (*func)(gsl_complex), VALUE obj)
105
+ {
106
+ gsl_complex *z, *znew;
107
+ gsl_vector_complex *v, *vnew;
108
+ gsl_matrix_complex *m, *mnew;
109
+ size_t i, j;
110
+ if (COMPLEX_P(obj)) {
111
+ Data_Get_Struct(obj, gsl_complex, z);
112
+ znew = xmalloc(sizeof(gsl_complex));
113
+ *znew = (*func)(*z);
114
+ return Data_Wrap_Struct(cgsl_complex, 0, free, znew);
115
+ } else if (VECTOR_COMPLEX_P(obj)) {
116
+ Data_Get_Struct(obj, gsl_vector_complex, v);
117
+ vnew = gsl_vector_complex_alloc(v->size);
118
+ for (i = 0; i < v->size; i++) {
119
+ z = GSL_COMPLEX_AT(v, i);
120
+ gsl_vector_complex_set(vnew, i, (*func)(*z));
121
+ }
122
+ return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
123
+ } else if (MATRIX_COMPLEX_P(obj)) {
124
+ Data_Get_Struct(obj, gsl_matrix_complex, m);
125
+ mnew = gsl_matrix_complex_alloc(m->size1, m->size2);
126
+ for (i = 0; i < m->size1; i++) {
127
+ for (j = 0; j < m->size2; j++) {
128
+ gsl_matrix_complex_set(mnew, i, j, (*func)(gsl_matrix_complex_get(m, i, j)));
129
+ }
130
+ }
131
+ return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, mnew);
132
+ } else {
133
+ rb_raise(rb_eTypeError,
134
+ "wrong argument type %s "
135
+ " (GSL::Complex or GSL::Vector::Complex expected)",
136
+ rb_class2name(CLASS_OF(obj)));
137
+ }
138
+ }
139
+
140
+ static VALUE rb_gsl_math_eval(double (*func)(const double), VALUE xx)
141
+ {
142
+ VALUE x, ary;
143
+ size_t i, size;
144
+ #ifdef HAVE_NARRAY_H
145
+ struct NARRAY *na;
146
+ double *ptr1, *ptr2;
147
+ #endif
148
+ if (CLASS_OF(xx) == rb_cRange) xx = rb_gsl_range2ary(xx);
149
+ switch (TYPE(xx)) {
150
+ case T_FIXNUM:
151
+ case T_BIGNUM:
152
+ case T_FLOAT:
153
+ return rb_float_new((*func)(NUM2DBL(xx)));
154
+ break;
155
+ case T_ARRAY:
156
+ size = RARRAY_LEN(xx);
157
+ ary = rb_ary_new2(size);
158
+ for (i = 0; i < size; i++) {
159
+ x = rb_ary_entry(xx, i);
160
+ Need_Float(x);
161
+ // rb_ary_store(ary, i, rb_float_new((*func)(RFLOAT(x)->value)));
162
+ rb_ary_store(ary, i, rb_float_new((*func)(NUM2DBL(x))));
163
+ }
164
+ return ary;
165
+ break;
166
+ default:
167
+ #ifdef HAVE_NARRAY_H
168
+ if (NA_IsNArray(xx)) {
169
+ GetNArray(xx, na);
170
+ ptr1 = (double*) na->ptr;
171
+ size = na->total;
172
+ ary = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(xx));
173
+ ptr2 = NA_PTR_TYPE(ary, double*);
174
+ for (i = 0; i < size; i++) ptr2[i] = (*func)(ptr1[i]);
175
+ return ary;
176
+ }
177
+ #endif
178
+ if (VECTOR_P(xx)) {
179
+ return vector_eval_create(xx, func);
180
+ } else if (MATRIX_P(xx)) {
181
+ return matrix_eval_create(xx, func);
182
+ } else {
183
+ rb_raise(rb_eTypeError, "wrong argument type %s (Array or Vector or Matrix expected)", rb_class2name(CLASS_OF(xx)));
184
+ }
185
+ break;
186
+ }
187
+ /* never reach here */
188
+ return Qnil;
189
+ }
190
+
191
+ static VALUE rb_gsl_math_eval2(double (*func)(const double, const double), VALUE xx,
192
+ VALUE yy);
193
+ static VALUE rb_gsl_math_eval2(double (*func)(const double, const double), VALUE xx,
194
+ VALUE yy)
195
+ {
196
+ VALUE x, y, ary;
197
+ size_t i, j, size;
198
+ gsl_vector *v = NULL, *v2 = NULL, *vnew = NULL;
199
+ gsl_matrix *m = NULL, *m2 = NULL, *mnew = NULL;
200
+ #ifdef HAVE_NARRAY_H
201
+ struct NARRAY *nax, *nay;
202
+ double *ptr1, *ptr2, *ptr3;
203
+ #endif
204
+ if (CLASS_OF(xx) == rb_cRange) xx = rb_gsl_range2ary(xx);
205
+ switch (TYPE(xx)) {
206
+ case T_FIXNUM:
207
+ case T_BIGNUM:
208
+ case T_FLOAT:
209
+ Need_Float(yy);
210
+ return rb_float_new((*func)(NUM2DBL(xx), NUM2DBL(yy)));
211
+ break;
212
+ case T_ARRAY:
213
+ Check_Type(yy, T_ARRAY);
214
+ size = RARRAY_LEN(xx);
215
+ // if (size != RARRAY(yy)->len) rb_raise(rb_eRuntimeError, "array sizes are different.");
216
+ if (size != RARRAY_LEN(yy)) rb_raise(rb_eRuntimeError, "array sizes are different.");
217
+ ary = rb_ary_new2(size);
218
+ for (i = 0; i < size; i++) {
219
+ x = rb_ary_entry(xx, i);
220
+ y = rb_ary_entry(yy, i);
221
+ Need_Float(x); Need_Float(y);
222
+ // rb_ary_store(ary, i, rb_float_new((*func)(RFLOAT(x)->value, RFLOAT(y)->value)));
223
+ rb_ary_store(ary, i, rb_float_new((*func)(NUM2DBL(x), NUM2DBL(y))));
224
+ }
225
+ return ary;
226
+ break;
227
+ default:
228
+ #ifdef HAVE_NARRAY_H
229
+ if (NA_IsNArray(xx)) {
230
+ GetNArray(xx, nax);
231
+ GetNArray(yy, nay);
232
+ ptr1 = (double*) nax->ptr;
233
+ ptr2 = (double*) nay->ptr;
234
+ size = nax->total;
235
+ ary = na_make_object(NA_DFLOAT, nax->rank, nax->shape, CLASS_OF(xx));
236
+ ptr3 = NA_PTR_TYPE(ary, double*);
237
+ for (i = 0; i < size; i++) ptr3[i] = (*func)(ptr1[i], ptr2[i]);
238
+ return ary;
239
+ }
240
+ #endif
241
+ if (VECTOR_P(xx)) {
242
+ CHECK_VECTOR(yy);
243
+ Data_Get_Struct(xx, gsl_vector, v);
244
+ Data_Get_Struct(yy, gsl_vector, v2);
245
+ vnew = gsl_vector_alloc(v->size);
246
+ for (i = 0; i < v->size; i++) {
247
+ gsl_vector_set(vnew, i, (*func)(gsl_vector_get(v, i), gsl_vector_get(v2, i)));
248
+ }
249
+ return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew);
250
+ } else if (MATRIX_P(xx)) {
251
+ CHECK_MATRIX(yy);
252
+ Data_Get_Struct(xx, gsl_matrix, m);
253
+ Data_Get_Struct(yy, gsl_matrix, m2);
254
+ mnew = gsl_matrix_alloc(m->size1, m->size2);
255
+ for (i = 0; i < m->size1; i++) {
256
+ for (j = 0; j < m->size2; j++) {
257
+ gsl_matrix_set(mnew, i, j, (*func)(gsl_matrix_get(m, i, j), gsl_matrix_get(m2, i, j)));
258
+ }
259
+ }
260
+ return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
261
+ } else {
262
+ rb_raise(rb_eTypeError,
263
+ "wrong argument type %s "
264
+ "(Array or Vector or Matrix expected)", rb_class2name(CLASS_OF(xx)));
265
+ }
266
+ break;
267
+ }
268
+ /* never reach here */
269
+ return Qnil;
270
+ }
271
+
272
+ static VALUE rb_gsl_sqrt(VALUE obj, VALUE x)
273
+ {
274
+ return rb_gsl_math_eval(sqrt, x);
275
+ }
276
+
277
+ static VALUE rb_gsl_log1p(VALUE obj, VALUE x)
278
+ {
279
+ return rb_gsl_math_eval(gsl_log1p, x);
280
+ }
281
+
282
+ static VALUE rb_gsl_expm1(VALUE obj, VALUE x)
283
+ {
284
+ return rb_gsl_math_eval(gsl_expm1, x);
285
+ }
286
+
287
+ static VALUE rb_gsl_hypot(VALUE obj, VALUE x, VALUE y)
288
+ {
289
+ return rb_gsl_math_eval2(gsl_hypot, x, y);
290
+ }
291
+ #ifdef GSL_1_10_LATER
292
+ static VALUE rb_gsl_hypot3(VALUE obj, VALUE x, VALUE y, VALUE z)
293
+ {
294
+ Need_Float(x);
295
+ Need_Float(y);
296
+ Need_Float(z);
297
+ return rb_float_new(gsl_hypot3(NUM2DBL(x), NUM2DBL(y), NUM2DBL(z)));
298
+ }
299
+ #endif
300
+ static VALUE rb_gsl_acosh(VALUE obj, VALUE x)
301
+ {
302
+ if (COMPLEX_P(x) || VECTOR_COMPLEX_P(x) || MATRIX_COMPLEX_P(x))
303
+ return rb_gsl_math_complex_eval(gsl_complex_arccosh, x);
304
+ return rb_gsl_math_eval(gsl_acosh, x);
305
+ }
306
+
307
+ static VALUE rb_gsl_asinh(VALUE obj, VALUE x)
308
+ {
309
+ if (COMPLEX_P(x) || VECTOR_COMPLEX_P(x) || MATRIX_COMPLEX_P(x))
310
+ return rb_gsl_math_complex_eval(gsl_complex_arcsinh, x);
311
+ return rb_gsl_math_eval(gsl_asinh, x);
312
+ }
313
+
314
+ static VALUE rb_gsl_atanh(VALUE obj, VALUE x)
315
+ {
316
+ if (COMPLEX_P(x) || VECTOR_COMPLEX_P(x) || MATRIX_COMPLEX_P(x))
317
+ return rb_gsl_math_complex_eval(gsl_complex_arctanh, x);
318
+ return rb_gsl_math_eval(gsl_atanh, x);
319
+ }
320
+
321
+ #include <math.h>
322
+ /* xx: Numeric, Complex, Vector, Matrix
323
+ nn: Numeric, Complex
324
+ */
325
+ VALUE rb_gsl_pow(VALUE obj, VALUE xx, VALUE nn)
326
+ {
327
+ VALUE x, ary, argv[2];
328
+ size_t i, j, size;
329
+ double n;
330
+ gsl_vector *v = NULL, *vnew = NULL;
331
+ gsl_matrix *m = NULL, *mnew = NULL;
332
+ #ifdef HAVE_NARRAY_H
333
+ struct NARRAY *na;
334
+ double *ptr1, *ptr2;
335
+ #endif
336
+ if (CLASS_OF(xx) == rb_cRange) xx = rb_gsl_range2ary(xx);
337
+ switch (TYPE(xx)) {
338
+ case T_FIXNUM:
339
+ case T_BIGNUM:
340
+ case T_FLOAT:
341
+ return rb_float_new(pow(NUM2DBL(xx), NUM2DBL(nn)));
342
+ break;
343
+ case T_ARRAY:
344
+ n = NUM2DBL(nn);
345
+ size = RARRAY_LEN(xx);
346
+ ary = rb_ary_new2(size);
347
+ for (i = 0; i < size; i++) {
348
+ x = rb_ary_entry(xx, i);
349
+ Need_Float(x);
350
+ rb_ary_store(ary, i, rb_float_new(pow(NUM2DBL(x), n)));
351
+ }
352
+ return ary;
353
+ break;
354
+ default:
355
+ #ifdef HAVE_NARRAY_H
356
+ if (NA_IsNArray(xx)) {
357
+ n = NUM2DBL(nn);
358
+ GetNArray(xx, na);
359
+ ptr1 = (double*) na->ptr;
360
+ size = na->total;
361
+ ary = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(xx));
362
+ ptr2 = NA_PTR_TYPE(ary, double*);
363
+ for (i = 0; i < size; i++) ptr2[i] = pow(ptr1[i], n);
364
+ return ary;
365
+ }
366
+ #endif
367
+ if (VECTOR_P(xx)) {
368
+ n = NUM2DBL(nn);
369
+ Data_Get_Struct(xx, gsl_vector, v);
370
+ vnew = gsl_vector_alloc(v->size);
371
+ for (i = 0; i < v->size; i++) {
372
+ gsl_vector_set(vnew, i, pow(gsl_vector_get(v, i), n));
373
+ }
374
+ return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew);
375
+ }
376
+ if (MATRIX_P(xx)) {
377
+ n = NUM2DBL(nn);
378
+ Data_Get_Struct(xx, gsl_matrix, m);
379
+ mnew = gsl_matrix_alloc(m->size1, m->size2);
380
+ for (i = 0; i < m->size1; i++) {
381
+ for (j = 0; j < m->size2; j++) {
382
+ gsl_matrix_set(mnew, i, j, pow(gsl_matrix_get(m, i, j), n));
383
+ }
384
+ }
385
+ return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
386
+ }
387
+ if (COMPLEX_P(xx) || VECTOR_COMPLEX_P(xx) || MATRIX_COMPLEX_P(xx)) {
388
+ argv[0] = xx;
389
+ argv[1] = nn;
390
+ return rb_gsl_complex_pow(2, argv, obj);
391
+ }
392
+ rb_raise(rb_eTypeError, "wrong argument type %s (Array or Vector or Matrix expected)", rb_class2name(CLASS_OF(xx)));
393
+ break;
394
+ }
395
+ /* never reach here */
396
+ return Qnil;
397
+ }
398
+
399
+ static VALUE rb_gsl_pow_int(VALUE obj, VALUE xx, VALUE nn)
400
+ {
401
+ VALUE x, ary, argv[2];
402
+ size_t i, j, size;
403
+ int n;
404
+ gsl_vector *v = NULL, *vnew = NULL;
405
+ gsl_matrix *m = NULL, *mnew = NULL;
406
+ #ifdef HAVE_NARRAY_H
407
+ struct NARRAY *na;
408
+ double *ptr1, *ptr2;
409
+ #endif
410
+
411
+ if (CLASS_OF(xx) == rb_cRange) xx = rb_gsl_range2ary(xx);
412
+ switch (TYPE(xx)) {
413
+ case T_FIXNUM:
414
+ case T_BIGNUM:
415
+ case T_FLOAT:
416
+ return rb_float_new(gsl_pow_int(NUM2DBL(xx), FIX2INT(nn)));
417
+ break;
418
+ case T_ARRAY:
419
+ CHECK_FIXNUM(nn);
420
+ n = FIX2INT(nn);
421
+ size = RARRAY_LEN(xx);
422
+ ary = rb_ary_new2(size);
423
+ for (i = 0; i < size; i++) {
424
+ x = rb_ary_entry(xx, i);
425
+ Need_Float(x);
426
+ // rb_ary_store(ary, i, rb_float_new(gsl_pow_int(RFLOAT(x)->value, n)));
427
+ rb_ary_store(ary, i, rb_float_new(gsl_pow_int(NUM2DBL(x), n)));
428
+ }
429
+ return ary;
430
+ break;
431
+ default:
432
+ #ifdef HAVE_NARRAY_H
433
+ if (NA_IsNArray(xx)) {
434
+ CHECK_FIXNUM(nn);
435
+ n = FIX2INT(nn);
436
+ GetNArray(xx, na);
437
+ ptr1 = (double*) na->ptr;
438
+ size = na->total;
439
+ ary = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(xx));
440
+ ptr2 = NA_PTR_TYPE(ary, double*);
441
+ for (i = 0; i < size; i++) ptr2[i] = gsl_pow_int(ptr1[i], n);
442
+ return ary;
443
+ }
444
+ #endif
445
+ if (VECTOR_P(xx)) {
446
+ CHECK_FIXNUM(nn);
447
+ n = FIX2INT(nn);
448
+ Data_Get_Struct(xx, gsl_vector, v);
449
+ vnew = gsl_vector_alloc(v->size);
450
+ for (i = 0; i < v->size; i++) {
451
+ gsl_vector_set(vnew, i, gsl_pow_int(gsl_vector_get(v, i), n));
452
+ }
453
+ return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew);
454
+ } else if (MATRIX_P(xx)) {
455
+ CHECK_FIXNUM(nn);
456
+ n = FIX2INT(nn);
457
+ Data_Get_Struct(xx, gsl_matrix, m);
458
+ mnew = gsl_matrix_alloc(m->size1, m->size2);
459
+ for (i = 0; i < m->size1; i++) {
460
+ for (j = 0; j < m->size2; j++) {
461
+ gsl_matrix_set(mnew, i, j, gsl_pow_int(gsl_matrix_get(m, i, j), n));
462
+ }
463
+ }
464
+ return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
465
+ } else if (COMPLEX_P(xx) || VECTOR_COMPLEX_P(xx) || MATRIX_COMPLEX_P(xx)) {
466
+ argv[0] = xx;
467
+ argv[1] = nn;
468
+ return rb_gsl_complex_pow_real(2, argv, obj);
469
+ } else {
470
+ rb_raise(rb_eTypeError, "wrong argument type %s (Array or Vector or Matrix expected)", rb_class2name(CLASS_OF(xx)));
471
+ }
472
+ break;
473
+ }
474
+ /* never reach here */
475
+ return Qnil;
476
+ }
477
+
478
+ static VALUE rb_gsl_pow_2(VALUE obj, VALUE x)
479
+ {
480
+ return rb_gsl_math_eval(gsl_pow_2, x);
481
+ }
482
+
483
+ static VALUE rb_gsl_pow_3(VALUE obj, VALUE x)
484
+ {
485
+ return rb_gsl_math_eval(gsl_pow_3, x);
486
+ }
487
+
488
+ static VALUE rb_gsl_pow_4(VALUE obj, VALUE x)
489
+ {
490
+ return rb_gsl_math_eval(gsl_pow_4, x);
491
+ }
492
+
493
+ static VALUE rb_gsl_pow_5(VALUE obj, VALUE x)
494
+ {
495
+ return rb_gsl_math_eval(gsl_pow_5, x);
496
+ }
497
+
498
+ static VALUE rb_gsl_pow_6(VALUE obj, VALUE x)
499
+ {
500
+ return rb_gsl_math_eval(gsl_pow_6, x);
501
+ }
502
+
503
+ static VALUE rb_gsl_pow_7(VALUE obj, VALUE x)
504
+ {
505
+ return rb_gsl_math_eval(gsl_pow_7, x);
506
+ }
507
+
508
+ static VALUE rb_gsl_pow_8(VALUE obj, VALUE x)
509
+ {
510
+ return rb_gsl_math_eval(gsl_pow_8, x);
511
+ }
512
+
513
+ static VALUE rb_gsl_pow_9(VALUE obj, VALUE x)
514
+ {
515
+ return rb_gsl_math_eval(gsl_pow_9, x);
516
+ }
517
+
518
+ /*****/
519
+ static VALUE rb_GSL_SIGN(VALUE obj, VALUE x)
520
+ {
521
+ return INT2FIX(GSL_SIGN(NUM2DBL(x)));
522
+ }
523
+
524
+ static VALUE rb_GSL_IS_ODD(VALUE obj, VALUE n)
525
+ {
526
+ CHECK_FIXNUM(n);
527
+ return INT2FIX(GSL_IS_ODD(FIX2INT(n)));
528
+ }
529
+
530
+ static VALUE rb_GSL_IS_ODD2(VALUE obj, VALUE n)
531
+ {
532
+ CHECK_FIXNUM(n);
533
+ if (GSL_IS_ODD(FIX2INT(n))) return Qtrue;
534
+ else return Qfalse;
535
+ }
536
+
537
+ static VALUE rb_GSL_IS_EVEN(VALUE obj, VALUE n)
538
+ {
539
+ CHECK_FIXNUM(n);
540
+ return INT2FIX(GSL_IS_EVEN(FIX2INT(n)));
541
+ }
542
+
543
+ static VALUE rb_GSL_IS_EVEN2(VALUE obj, VALUE n)
544
+ {
545
+ CHECK_FIXNUM(n);
546
+ if (GSL_IS_EVEN(FIX2INT(n))) return Qtrue;
547
+ else return Qfalse;
548
+ }
549
+
550
+ static VALUE rb_GSL_MAX(VALUE obj, VALUE aa, VALUE bb)
551
+ {
552
+ double a, b;
553
+ double max;
554
+ /* Need_Float(aa); Need_Float(bb);*/
555
+ a = NUM2DBL(aa);
556
+ b = NUM2DBL(bb);
557
+ max = GSL_MAX_DBL(a, b);
558
+ if (gsl_fcmp(max, a, 1.0e-10) == 0) return aa;
559
+ else return bb;
560
+
561
+ }
562
+
563
+ static VALUE rb_GSL_MIN(VALUE obj, VALUE aa, VALUE bb)
564
+ {
565
+ double a, b;
566
+ double min;
567
+ /* Need_Float(aa); Need_Float(bb);*/
568
+ a = NUM2DBL(aa);
569
+ b = NUM2DBL(bb);
570
+ min = GSL_MIN_DBL(a, b);
571
+ if (gsl_fcmp(min, a, 1.0e-10) == 0) return aa;
572
+ else return bb;
573
+ }
574
+
575
+ static VALUE rb_GSL_MAX_DBL(VALUE obj, VALUE aa, VALUE bb)
576
+ {
577
+ Need_Float(aa); Need_Float(bb);
578
+ return rb_float_new(GSL_MAX_DBL(NUM2DBL(aa), NUM2DBL(bb)));
579
+ }
580
+
581
+ static VALUE rb_GSL_MIN_DBL(VALUE obj, VALUE aa, VALUE bb)
582
+ {
583
+ Need_Float(aa); Need_Float(bb);
584
+ return rb_float_new(GSL_MAX_DBL(NUM2DBL(aa), NUM2DBL(bb)));
585
+ }
586
+
587
+ static VALUE rb_GSL_MAX_INT(VALUE obj, VALUE aa, VALUE bb)
588
+ {
589
+ if (TYPE(aa) != T_FIXNUM || TYPE(bb) != T_FIXNUM)
590
+ return rb_GSL_MAX(obj, aa, bb);
591
+ else
592
+ return INT2FIX(GSL_MAX_INT(FIX2INT(aa), FIX2INT(bb)));
593
+ }
594
+
595
+ static VALUE rb_GSL_MIN_INT(VALUE obj, VALUE aa, VALUE bb)
596
+ {
597
+ if (TYPE(aa) != T_FIXNUM || TYPE(bb) != T_FIXNUM)
598
+ return rb_GSL_MIN(obj, aa, bb);
599
+ return
600
+ INT2FIX(GSL_MIN_INT(FIX2INT(aa), FIX2INT(bb)));
601
+ }
602
+
603
+ #ifdef GSL_1_3_LATER
604
+ static VALUE rb_gsl_ldexp(VALUE obj, VALUE x, VALUE e)
605
+ {
606
+ return rb_float_new(gsl_ldexp(NUM2DBL(x), FIX2INT(e)));
607
+ }
608
+
609
+ static VALUE rb_gsl_frexp(VALUE obj, VALUE x)
610
+ {
611
+ int e;
612
+ double val;
613
+ Need_Float(x);
614
+ val = gsl_frexp(NUM2DBL(x), &e);
615
+ return rb_ary_new3(2, rb_float_new(val), INT2FIX(e));
616
+ }
617
+ #endif
618
+
619
+ static VALUE rb_gsl_fcmp(int argc, VALUE *argv, VALUE obj)
620
+ {
621
+ double a, b, epsilon = 1e-10;
622
+ switch (argc) {
623
+ case 3:
624
+ epsilon = NUM2DBL(argv[2]);
625
+ /* no break, do next */
626
+ case 2:
627
+ a = NUM2DBL(argv[0]);
628
+ b = NUM2DBL(argv[1]);
629
+ break;
630
+ default:
631
+ rb_raise(rb_eArgError, "wrong number of arguments");
632
+ break;
633
+ }
634
+ return INT2FIX(gsl_fcmp(a, b, epsilon));
635
+ }
636
+
637
+ static VALUE rb_gsl_equal(int argc, VALUE *argv, VALUE obj)
638
+ {
639
+ double a, b, epsilon = 1e-10;
640
+ int retval;
641
+
642
+ switch (argc) {
643
+ case 3:
644
+ epsilon = NUM2DBL(argv[2]);
645
+ /* no break, do next */
646
+ case 2:
647
+ a = NUM2DBL(argv[0]);
648
+ b = NUM2DBL(argv[1]);
649
+ break;
650
+ default:
651
+ rb_raise(rb_eArgError, "wrong number of arguments");
652
+ break;
653
+ }
654
+ retval = gsl_fcmp(a, b, epsilon);
655
+ if (retval == 0) return Qtrue;
656
+ else return Qfalse;
657
+ }
658
+
659
+ void Init_gsl_math(VALUE module)
660
+ {
661
+ rb_gsl_define_const(module);
662
+
663
+ rb_define_module_function(module, "posinf", rb_GSL_POSINF, 0);
664
+ rb_define_module_function(module, "neginf", rb_GSL_NEGINF, 0);
665
+ rb_define_module_function(module, "nan", rb_GSL_NAN, 0);
666
+ rb_define_module_function(module, "isnan", rb_gsl_isnan, 1);
667
+ rb_define_module_function(module, "isnan?", rb_gsl_isnan2, 1);
668
+ rb_define_module_function(module, "isinf", rb_gsl_isinf, 1);
669
+ rb_define_module_function(module, "isinf?", rb_gsl_isinf2, 1);
670
+ rb_define_module_function(module, "finite", rb_gsl_finite, 1);
671
+ rb_define_module_function(module, "finite?", rb_gsl_finite2, 1);
672
+
673
+ rb_define_module_function(module, "sqrt", rb_gsl_sqrt, 1);
674
+ rb_define_module_function(module, "log1p", rb_gsl_log1p, 1);
675
+ rb_define_module_function(module, "expm1", rb_gsl_expm1, 1);
676
+ rb_define_module_function(module, "hypot", rb_gsl_hypot, 2);
677
+ #ifdef GSL_1_10_LATER
678
+ rb_define_module_function(module, "hypot3", rb_gsl_hypot3, 3);
679
+ #endif
680
+ rb_define_module_function(module, "acosh", rb_gsl_acosh, 1);
681
+ rb_define_module_function(module, "asinh", rb_gsl_asinh, 1);
682
+ rb_define_module_function(module, "atanh", rb_gsl_atanh, 1);
683
+ rb_define_module_function(module, "pow", rb_gsl_pow, 2);
684
+ rb_define_module_function(module, "pow_int", rb_gsl_pow_int, 2);
685
+ rb_define_module_function(module, "pow_2", rb_gsl_pow_2, 1);
686
+ rb_define_module_function(module, "pow_3", rb_gsl_pow_3, 1);
687
+ rb_define_module_function(module, "pow_4", rb_gsl_pow_4, 1);
688
+ rb_define_module_function(module, "pow_5", rb_gsl_pow_5, 1);
689
+ rb_define_module_function(module, "pow_6", rb_gsl_pow_6, 1);
690
+ rb_define_module_function(module, "pow_7", rb_gsl_pow_7, 1);
691
+ rb_define_module_function(module, "pow_8", rb_gsl_pow_8, 1);
692
+ rb_define_module_function(module, "pow_9", rb_gsl_pow_9, 1);
693
+ rb_define_module_function(module, "sign", rb_GSL_SIGN, 1);
694
+ rb_define_module_function(module, "SIGN", rb_GSL_SIGN, 1);
695
+
696
+ rb_define_module_function(module, "is_odd", rb_GSL_IS_ODD, 1);
697
+ rb_define_module_function(module, "IS_ODD", rb_GSL_IS_ODD, 1);
698
+ rb_define_module_function(module, "is_odd?", rb_GSL_IS_ODD2, 1);
699
+ rb_define_module_function(module, "IS_ODD?", rb_GSL_IS_ODD2, 1);
700
+ rb_define_module_function(module, "is_even", rb_GSL_IS_EVEN, 1);
701
+ rb_define_module_function(module, "IS_EVEN", rb_GSL_IS_EVEN, 1);
702
+ rb_define_module_function(module, "is_even?", rb_GSL_IS_EVEN2, 1);
703
+ rb_define_module_function(module, "IS_EVEN?", rb_GSL_IS_EVEN2, 1);
704
+ rb_define_module_function(module, "max", rb_GSL_MAX, 2);
705
+ rb_define_module_function(module, "MAX", rb_GSL_MAX, 2);
706
+ rb_define_module_function(module, "min", rb_GSL_MIN, 2);
707
+ rb_define_module_function(module, "MIN", rb_GSL_MIN, 2);
708
+
709
+ rb_define_module_function(module, "max_dbl", rb_GSL_MAX_DBL, 2);
710
+ rb_define_module_function(module, "MAX_DBL", rb_GSL_MAX_DBL, 2);
711
+ rb_define_module_function(module, "min_dbl", rb_GSL_MIN_DBL, 2);
712
+ rb_define_module_function(module, "MIN_DBL", rb_GSL_MIN_DBL, 2);
713
+
714
+ rb_define_module_function(module, "max_int", rb_GSL_MAX_INT, 2);
715
+ rb_define_module_function(module, "MAX_INT", rb_GSL_MAX_INT, 2);
716
+ rb_define_module_function(module, "min_int", rb_GSL_MIN_INT, 2);
717
+ rb_define_module_function(module, "MIN_INT", rb_GSL_MIN_INT, 2);
718
+
719
+ rb_define_module_function(module, "fcmp", rb_gsl_fcmp, -1);
720
+ rb_define_singleton_method(module, "equal?", rb_gsl_equal, -1);
721
+ #ifdef GSL_1_3_LATER
722
+ rb_define_module_function(module, "ldexp", rb_gsl_ldexp, 2);
723
+ rb_define_module_function(module, "frexp", rb_gsl_frexp, 1);
724
+ #endif
725
+ }