romanbsd-gsl 1.11.2.2

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.
Files changed (156) hide show
  1. data/README.rdoc +29 -0
  2. data/Rakefile +54 -0
  3. data/VERSION +1 -0
  4. data/ext/MANIFEST +116 -0
  5. data/ext/array.c +665 -0
  6. data/ext/array_complex.c +247 -0
  7. data/ext/blas.c +29 -0
  8. data/ext/blas1.c +729 -0
  9. data/ext/blas2.c +1093 -0
  10. data/ext/blas3.c +881 -0
  11. data/ext/block.c +44 -0
  12. data/ext/block_source.c +885 -0
  13. data/ext/bspline.c +116 -0
  14. data/ext/bundle.c +3 -0
  15. data/ext/cdf.c +754 -0
  16. data/ext/cheb.c +538 -0
  17. data/ext/combination.c +283 -0
  18. data/ext/common.c +310 -0
  19. data/ext/complex.c +1005 -0
  20. data/ext/const.c +668 -0
  21. data/ext/const_additional.c +120 -0
  22. data/ext/cqp.c +283 -0
  23. data/ext/deriv.c +194 -0
  24. data/ext/dht.c +360 -0
  25. data/ext/diff.c +165 -0
  26. data/ext/dirac.c +395 -0
  27. data/ext/eigen.c +2373 -0
  28. data/ext/error.c +194 -0
  29. data/ext/extconf.rb +272 -0
  30. data/ext/fcmp.c +66 -0
  31. data/ext/fft.c +1092 -0
  32. data/ext/fit.c +205 -0
  33. data/ext/fresnel.c +312 -0
  34. data/ext/function.c +522 -0
  35. data/ext/graph.c +1634 -0
  36. data/ext/gsl.c +265 -0
  37. data/ext/gsl_narray.c +652 -0
  38. data/ext/histogram.c +1717 -0
  39. data/ext/histogram2d.c +1067 -0
  40. data/ext/histogram3d.c +883 -0
  41. data/ext/histogram3d_source.c +750 -0
  42. data/ext/histogram_find.c +101 -0
  43. data/ext/histogram_oper.c +159 -0
  44. data/ext/ieee.c +98 -0
  45. data/ext/integration.c +1138 -0
  46. data/ext/interp.c +511 -0
  47. data/ext/jacobi.c +737 -0
  48. data/ext/linalg.c +4045 -0
  49. data/ext/linalg_complex.c +743 -0
  50. data/ext/math.c +724 -0
  51. data/ext/matrix.c +39 -0
  52. data/ext/matrix_complex.c +1731 -0
  53. data/ext/matrix_double.c +560 -0
  54. data/ext/matrix_int.c +256 -0
  55. data/ext/matrix_source.c +2678 -0
  56. data/ext/min.c +234 -0
  57. data/ext/monte.c +759 -0
  58. data/ext/multifit.c +1810 -0
  59. data/ext/multimin.c +793 -0
  60. data/ext/multimin_fsdf.c +156 -0
  61. data/ext/multiroots.c +952 -0
  62. data/ext/ndlinear.c +320 -0
  63. data/ext/nmf.c +159 -0
  64. data/ext/nmf_wrap.c +63 -0
  65. data/ext/ntuple.c +469 -0
  66. data/ext/odeiv.c +958 -0
  67. data/ext/ool.c +879 -0
  68. data/ext/oper_complex_source.c +253 -0
  69. data/ext/permutation.c +596 -0
  70. data/ext/poly.c +42 -0
  71. data/ext/poly2.c +265 -0
  72. data/ext/poly_source.c +1799 -0
  73. data/ext/qrng.c +171 -0
  74. data/ext/randist.c +1869 -0
  75. data/ext/rational.c +480 -0
  76. data/ext/rng.c +612 -0
  77. data/ext/root.c +408 -0
  78. data/ext/sf.c +1494 -0
  79. data/ext/sf_airy.c +200 -0
  80. data/ext/sf_bessel.c +867 -0
  81. data/ext/sf_clausen.c +28 -0
  82. data/ext/sf_coulomb.c +206 -0
  83. data/ext/sf_coupling.c +118 -0
  84. data/ext/sf_dawson.c +29 -0
  85. data/ext/sf_debye.c +157 -0
  86. data/ext/sf_dilog.c +42 -0
  87. data/ext/sf_elementary.c +44 -0
  88. data/ext/sf_ellint.c +206 -0
  89. data/ext/sf_elljac.c +29 -0
  90. data/ext/sf_erfc.c +93 -0
  91. data/ext/sf_exp.c +164 -0
  92. data/ext/sf_expint.c +211 -0
  93. data/ext/sf_fermi_dirac.c +148 -0
  94. data/ext/sf_gamma.c +344 -0
  95. data/ext/sf_gegenbauer.c +96 -0
  96. data/ext/sf_hyperg.c +197 -0
  97. data/ext/sf_laguerre.c +112 -0
  98. data/ext/sf_lambert.c +47 -0
  99. data/ext/sf_legendre.c +367 -0
  100. data/ext/sf_log.c +104 -0
  101. data/ext/sf_mathieu.c +238 -0
  102. data/ext/sf_power.c +46 -0
  103. data/ext/sf_psi.c +98 -0
  104. data/ext/sf_synchrotron.c +48 -0
  105. data/ext/sf_transport.c +76 -0
  106. data/ext/sf_trigonometric.c +207 -0
  107. data/ext/sf_zeta.c +119 -0
  108. data/ext/signal.c +310 -0
  109. data/ext/siman.c +718 -0
  110. data/ext/sort.c +208 -0
  111. data/ext/spline.c +393 -0
  112. data/ext/stats.c +799 -0
  113. data/ext/sum.c +168 -0
  114. data/ext/tamu_anova.c +56 -0
  115. data/ext/tensor.c +38 -0
  116. data/ext/tensor_source.c +1121 -0
  117. data/ext/vector.c +38 -0
  118. data/ext/vector_complex.c +2139 -0
  119. data/ext/vector_double.c +1445 -0
  120. data/ext/vector_int.c +204 -0
  121. data/ext/vector_source.c +3325 -0
  122. data/ext/wavelet.c +937 -0
  123. data/include/rb_gsl.h +140 -0
  124. data/include/rb_gsl_array.h +230 -0
  125. data/include/rb_gsl_cheb.h +21 -0
  126. data/include/rb_gsl_common.h +343 -0
  127. data/include/rb_gsl_complex.h +25 -0
  128. data/include/rb_gsl_const.h +29 -0
  129. data/include/rb_gsl_dirac.h +15 -0
  130. data/include/rb_gsl_eigen.h +17 -0
  131. data/include/rb_gsl_fft.h +62 -0
  132. data/include/rb_gsl_fit.h +25 -0
  133. data/include/rb_gsl_function.h +27 -0
  134. data/include/rb_gsl_graph.h +70 -0
  135. data/include/rb_gsl_histogram.h +65 -0
  136. data/include/rb_gsl_histogram3d.h +97 -0
  137. data/include/rb_gsl_integration.h +17 -0
  138. data/include/rb_gsl_interp.h +46 -0
  139. data/include/rb_gsl_linalg.h +25 -0
  140. data/include/rb_gsl_math.h +26 -0
  141. data/include/rb_gsl_odeiv.h +21 -0
  142. data/include/rb_gsl_poly.h +71 -0
  143. data/include/rb_gsl_rational.h +37 -0
  144. data/include/rb_gsl_rng.h +21 -0
  145. data/include/rb_gsl_root.h +22 -0
  146. data/include/rb_gsl_sf.h +119 -0
  147. data/include/rb_gsl_statistics.h +17 -0
  148. data/include/rb_gsl_tensor.h +45 -0
  149. data/include/rb_gsl_with_narray.h +22 -0
  150. data/include/templates_off.h +87 -0
  151. data/include/templates_on.h +241 -0
  152. data/lib/gsl/gnuplot.rb +41 -0
  153. data/lib/gsl/oper.rb +68 -0
  154. data/lib/ool.rb +22 -0
  155. data/lib/ool/conmin.rb +30 -0
  156. metadata +221 -0
data/ext/sum.c ADDED
@@ -0,0 +1,168 @@
1
+ /*
2
+ sum.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.
10
+ */
11
+ #include "rb_gsl_config.h"
12
+ #include "rb_gsl_array.h"
13
+ #include <gsl/gsl_sum.h>
14
+
15
+ static VALUE rb_gsl_sum_accel(VALUE obj)
16
+ {
17
+ gsl_sum_levin_u_workspace *w = NULL;
18
+ double sum, err, sum_plain, *ptr;
19
+ size_t terms_used, n, stride;
20
+ ptr = get_vector_ptr(obj, &stride, &n);
21
+ w = gsl_sum_levin_u_alloc(n);
22
+ gsl_sum_levin_u_accel(ptr, n, w, &sum, &err);
23
+ sum_plain = w->sum_plain;
24
+ terms_used = w->terms_used;
25
+ gsl_sum_levin_u_free(w);
26
+ return rb_ary_new3(4, rb_float_new(sum), rb_float_new(err),
27
+ rb_float_new(sum_plain), INT2FIX(terms_used));
28
+ }
29
+
30
+ static VALUE rb_gsl_utrunc_accel(VALUE obj)
31
+ {
32
+ gsl_sum_levin_utrunc_workspace *w = NULL;
33
+ double sum, err, sum_plain, *ptr;
34
+ size_t terms_used, n, stride;
35
+ ptr = get_vector_ptr(obj, &stride, &n);
36
+ w = gsl_sum_levin_utrunc_alloc(n);
37
+ gsl_sum_levin_utrunc_accel(ptr, n, w, &sum, &err);
38
+ sum_plain = w->sum_plain;
39
+ terms_used = w->terms_used;
40
+ gsl_sum_levin_utrunc_free(w);
41
+ return rb_ary_new3(4, rb_float_new(sum), rb_float_new(err),
42
+ rb_float_new(sum_plain), INT2FIX(terms_used));
43
+ }
44
+
45
+ static VALUE rb_gsl_sum_levin_u_new(VALUE klass, VALUE nn)
46
+ {
47
+ gsl_sum_levin_u_workspace *w = NULL;
48
+ CHECK_FIXNUM(nn);
49
+ w = gsl_sum_levin_u_alloc(FIX2INT(nn));
50
+ return Data_Wrap_Struct(klass, 0, gsl_sum_levin_u_free, w);
51
+ }
52
+
53
+ static VALUE rb_gsl_sum_levin_utrunc_new(VALUE klass, VALUE nn)
54
+ {
55
+ gsl_sum_levin_utrunc_workspace *w = NULL;
56
+ CHECK_FIXNUM(nn);
57
+ w = gsl_sum_levin_utrunc_alloc(FIX2INT(nn));
58
+ return Data_Wrap_Struct(klass, 0, gsl_sum_levin_utrunc_free, w);
59
+ }
60
+
61
+ /* singleton */
62
+ static VALUE rb_gsl_sum_levin_u_accel2(VALUE obj, VALUE vv)
63
+ {
64
+ gsl_sum_levin_u_workspace *w = NULL;
65
+ double sum, err, sum_plain;
66
+ size_t terms_used, n, stride;
67
+ double *ptr;
68
+ ptr = get_vector_ptr(vv, &stride, &n);
69
+ w = gsl_sum_levin_u_alloc(n);
70
+ gsl_sum_levin_u_accel(ptr, n, w, &sum, &err);
71
+ sum_plain = w->sum_plain;
72
+ terms_used = w->terms_used;
73
+ gsl_sum_levin_u_free(w);
74
+ return rb_ary_new3(4, rb_float_new(sum), rb_float_new(err),
75
+ rb_float_new(sum_plain), INT2FIX(terms_used));
76
+ }
77
+
78
+ static VALUE rb_gsl_sum_levin_utrunc_accel2(VALUE obj, VALUE vv)
79
+ {
80
+ gsl_sum_levin_utrunc_workspace *w = NULL;
81
+ double sum, err, sum_plain;
82
+ size_t terms_used, n, stride;
83
+ double *ptr;
84
+ ptr = get_vector_ptr(vv, &stride, &n);
85
+ w = gsl_sum_levin_utrunc_alloc(n);
86
+ gsl_sum_levin_utrunc_accel(ptr, n, w, &sum, &err);
87
+ sum_plain = w->sum_plain;
88
+ terms_used = w->terms_used;
89
+ gsl_sum_levin_utrunc_free(w);
90
+ return rb_ary_new3(4, rb_float_new(sum), rb_float_new(err),
91
+ rb_float_new(sum_plain), INT2FIX(terms_used));
92
+ }
93
+
94
+ static VALUE rb_gsl_sum_levin_u_sum_plain(VALUE obj)
95
+ {
96
+ gsl_sum_levin_u_workspace *w = NULL;
97
+ Data_Get_Struct(obj, gsl_sum_levin_u_workspace, w);
98
+ return rb_float_new(w->sum_plain);
99
+ }
100
+
101
+ static VALUE rb_gsl_sum_levin_u_terms_used(VALUE obj)
102
+ {
103
+ gsl_sum_levin_u_workspace *w = NULL;
104
+ Data_Get_Struct(obj, gsl_sum_levin_u_workspace, w);
105
+ return INT2FIX(w->terms_used);
106
+ }
107
+
108
+ static VALUE rb_gsl_sum_levin_utrunc_sum_plain(VALUE obj)
109
+ {
110
+ gsl_sum_levin_utrunc_workspace *w = NULL;
111
+ Data_Get_Struct(obj, gsl_sum_levin_utrunc_workspace, w);
112
+ return rb_float_new(w->sum_plain);
113
+ }
114
+
115
+ static VALUE rb_gsl_sum_levin_utrunc_terms_used(VALUE obj)
116
+ {
117
+ gsl_sum_levin_utrunc_workspace *w = NULL;
118
+ Data_Get_Struct(obj, gsl_sum_levin_utrunc_workspace, w);
119
+ return INT2FIX(w->terms_used);
120
+ }
121
+
122
+ void Init_gsl_sum(VALUE module)
123
+ {
124
+ VALUE mgsl_sum;
125
+ VALUE cgsl_sum_levin_u, cgsl_sum_levin_utrunc;
126
+
127
+ mgsl_sum = rb_define_module_under(module, "Sum");
128
+ cgsl_sum_levin_u = rb_define_class_under(mgsl_sum,
129
+ "Levin_u", cGSL_Object);
130
+ cgsl_sum_levin_utrunc = rb_define_class_under(mgsl_sum,
131
+ "Levin_utrunc", cGSL_Object);
132
+
133
+ rb_define_singleton_method(cgsl_sum_levin_u, "new", rb_gsl_sum_levin_u_new, 1);
134
+ rb_define_singleton_method(cgsl_sum_levin_u, "alloc", rb_gsl_sum_levin_u_new, 1);
135
+ rb_define_singleton_method(cgsl_sum_levin_utrunc, "new",
136
+ rb_gsl_sum_levin_utrunc_new, 1);
137
+ rb_define_singleton_method(cgsl_sum_levin_utrunc, "alloc",
138
+ rb_gsl_sum_levin_utrunc_new, 1);
139
+ rb_define_singleton_method(cgsl_sum_levin_u, "accel",
140
+ rb_gsl_sum_levin_u_accel2, 1);
141
+
142
+ rb_define_singleton_method(cgsl_sum_levin_utrunc, "accel",
143
+ rb_gsl_sum_levin_utrunc_accel2, 1);
144
+ rb_define_method(cgsl_sum_levin_u, "accel", rb_gsl_sum_levin_u_accel2, 1);
145
+ rb_define_method(cgsl_sum_levin_utrunc, "accel",
146
+ rb_gsl_sum_levin_utrunc_accel2, 1);
147
+
148
+ rb_define_method(cgsl_sum_levin_u, "sum_plain", rb_gsl_sum_levin_u_sum_plain, 0);
149
+ rb_define_method(cgsl_sum_levin_u, "terms_used",
150
+ rb_gsl_sum_levin_u_terms_used, 0);
151
+ rb_define_method(cgsl_sum_levin_utrunc, "sum_plain",
152
+ rb_gsl_sum_levin_utrunc_sum_plain, 0);
153
+ rb_define_method(cgsl_sum_levin_utrunc, "terms_used",
154
+ rb_gsl_sum_levin_utrunc_terms_used, 0);
155
+ /***/
156
+
157
+ rb_define_method(cgsl_vector, "accel_sum", rb_gsl_sum_accel, 0);
158
+ rb_define_alias(cgsl_vector, "accel", "accel_sum");
159
+ rb_define_alias(cgsl_vector, "sum_accel", "accel_sum");
160
+ rb_define_method(cgsl_vector, "utrunc_accel", rb_gsl_utrunc_accel, 0);
161
+
162
+ #ifdef HAVE_NARRAY_H
163
+ rb_define_method(cNArray, "accel_sum", rb_gsl_sum_accel, 0);
164
+ rb_define_alias(cNArray, "accel", "accel_sum");
165
+ rb_define_alias(cNArray, "sum_accel", "accel_sum");
166
+ rb_define_method(cNArray, "utrunc_accel", rb_gsl_utrunc_accel, 0);
167
+ #endif
168
+ }
data/ext/tamu_anova.c ADDED
@@ -0,0 +1,56 @@
1
+ #include "rb_gsl.h"
2
+
3
+ #ifdef HAVE_TAMU_ANOVA_TAMU_ANOVA_H
4
+ VALUE rb_tamu_anova_alloc(int argc, VALUE *argv, VALUE klass)
5
+ {
6
+ gsl_vector *data;
7
+ gsl_vector_long *factor;
8
+ long I, J;
9
+ struct tamu_anova_table *table;
10
+ switch (argc) {
11
+ case 3:
12
+ case 4:
13
+ Data_Get_Struct(argv[0], gsl_vector, data);
14
+ Data_Get_Struct(argv[1], gsl_vector_long, factor);
15
+ if (argc == 3) {
16
+ I = data->size;
17
+ J = NUM2INT(argv[2]);
18
+ } else {
19
+ I = NUM2INT(argv[2]);
20
+ J = NUM2INT(argv[3]);
21
+ }
22
+ table = (struct tamu_anova_table *) malloc(sizeof(struct tamu_anova_table));
23
+ *table = tamu_anova(data->data, factor->data, I, J);
24
+ break;
25
+ default:
26
+ rb_raise(rb_eArgError, "Wrong number of arguments (%d for 3 or 4)", argc);
27
+ break;
28
+ }
29
+ return Data_Wrap_Struct(klass, 0, free, table);
30
+ }
31
+
32
+ VALUE rb_tamu_anova_printtable(VALUE *vTable)
33
+ {
34
+ struct tamu_anova_table *table;
35
+ Data_Get_Struct(vTable, struct tamu_anova_table, table);
36
+ tamu_anova_printtable(*table);
37
+ return Qtrue;
38
+ }
39
+
40
+ #endif
41
+
42
+ void Init_tamu_anova(VALUE module)
43
+ {
44
+ #ifdef HAVE_TAMU_ANOVA_TAMU_ANOVA_H
45
+ VALUE mTAMU_ANOVA;
46
+ VALUE cTable;
47
+
48
+ mTAMU_ANOVA = rb_define_module_under(module, "TAMU_ANOVA");
49
+ cTable = rb_define_class_under(mTAMU_ANOVA, "Table", cGSL_Object);
50
+
51
+ rb_define_singleton_method(cTable, "alloc", rb_tamu_anova_alloc, -1);
52
+ rb_define_singleton_method(cTable, "oneway", rb_tamu_anova_alloc, -1);
53
+
54
+ rb_define_method(cTable, "print", rb_tamu_anova_printtable, 0);
55
+ #endif
56
+ }
data/ext/tensor.c ADDED
@@ -0,0 +1,38 @@
1
+ /*
2
+ tensor.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
+
12
+ /*
13
+ The gsl_tensor package is developed by J.Burguet, and
14
+ distributed separately as an add-on package.
15
+ */
16
+
17
+ #ifdef HAVE_TENSOR_TENSOR_H
18
+
19
+ #include "rb_gsl_config.h"
20
+ #include "rb_gsl_tensor.h"
21
+
22
+ #ifdef HAVE_NARRAY_H
23
+ #include "rb_gsl_with_narray.h"
24
+ #endif
25
+
26
+ #define BASE_DOUBLE
27
+ #include "templates_on.h"
28
+ #include "tensor_source.c"
29
+ #include "templates_off.h"
30
+ #undef BASE_DOUBLE
31
+
32
+ #define BASE_INT
33
+ #include "templates_on.h"
34
+ #include "tensor_source.c"
35
+ #include "templates_off.h"
36
+ #undef BASE_INT
37
+
38
+ #endif
@@ -0,0 +1,1121 @@
1
+ /*
2
+ tensor_source.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
+
12
+ /*
13
+ The tensor package is developed by Jordi Burguet-Caltell, and
14
+ distributed separately as an add-on package.
15
+ http://savannah.nongnu.org/projects/tensor/
16
+ */
17
+
18
+ #ifdef HAVE_TENSOR_TENSOR_H
19
+
20
+ #include "rb_gsl_config.h"
21
+ #include "rb_gsl_tensor.h"
22
+ #include "rb_gsl_common.h"
23
+
24
+ #ifdef BASE_DOUBLE
25
+ VALUE cgsl_tensor, cgsl_tensor_int;
26
+ VALUE cgsl_tensor_view, cgsl_tensor_int_view;
27
+ #define NUMCONV(x) NUM2DBL(x)
28
+ #define C_TO_VALUE(x) rb_float_new(x)
29
+ #define CHECK_TEN(x) CHECK_TENSOR(x)
30
+ #define TEN_P(x) TENSOR_P(x)
31
+ #define VEC_P(x) VECTOR_P(x)
32
+ #define MAT_P(x) MATRIX_P(x)
33
+ #else defined(BASE_INT)
34
+ #define NUMCONV(x) FIX2INT(x)
35
+ #define C_TO_VALUE(x) INT2FIX(x)
36
+ #define CHECK_TEN(x) CHECK_TENSOR_INT(x)
37
+ #define TEN_P(x) TENSOR_INT_P(x)
38
+ #define VEC_P(x) VECTOR_INT_P(x)
39
+ #define MAT_P(x) MATRIX_INT_P(x)
40
+ #endif
41
+
42
+ GSL_TYPE(rbgsl_tensor)* FUNCTION(rbgsl_tensor,alloc)(const unsigned int rank,
43
+ const size_t dimension)
44
+ {
45
+ GSL_TYPE(rbgsl_tensor) *t;
46
+ t = ALLOC(GSL_TYPE(rbgsl_tensor));
47
+ t->tensor = FUNCTION(tensor,alloc)(rank, dimension);
48
+ if (rank == 0)
49
+ t->indices = gsl_permutation_alloc(1);
50
+ else
51
+ t->indices = gsl_permutation_alloc(rank);
52
+ return t;
53
+ }
54
+
55
+ GSL_TYPE(rbgsl_tensor)* FUNCTION(rbgsl_tensor,calloc)(const unsigned int rank,
56
+ const size_t dimension)
57
+ {
58
+ GSL_TYPE(rbgsl_tensor) *t;
59
+ t = ALLOC(GSL_TYPE(rbgsl_tensor));
60
+ t->tensor = FUNCTION(tensor,calloc)(rank, dimension);
61
+ if (rank == 0)
62
+ t->indices = gsl_permutation_alloc(1);
63
+ else
64
+ t->indices = gsl_permutation_alloc(rank);
65
+ return t;
66
+ }
67
+
68
+ GSL_TYPE(rbgsl_tensor)* FUNCTION(rbgsl_tensor,copy)(const GSL_TYPE(rbgsl_tensor) *t)
69
+ {
70
+ GSL_TYPE(rbgsl_tensor) *tnew;
71
+ tnew = ALLOC(GSL_TYPE(rbgsl_tensor));
72
+ if (t->tensor->rank == 0)
73
+ tnew->indices = gsl_permutation_alloc(1);
74
+ else
75
+ tnew->indices = gsl_permutation_alloc(t->tensor->rank);
76
+ tnew->tensor = FUNCTION(tensor,copy)(t->tensor);
77
+ return tnew;
78
+ }
79
+
80
+ void FUNCTION(rbgsl_tensor,free)(GSL_TYPE(rbgsl_tensor) *t)
81
+ {
82
+ gsl_permutation_free(t->indices);
83
+ FUNCTION(tensor,free)(t->tensor);
84
+ free((GSL_TYPE(rbgsl_tensor) *) t);
85
+ }
86
+
87
+ void FUNCTION(rbgsl_tensor,free2)(GSL_TYPE(rbgsl_tensor) *t)
88
+ {
89
+ gsl_permutation_free(t->indices);
90
+ free((GSL_TYPE(tensor)*) t->tensor);
91
+ free((GSL_TYPE(rbgsl_tensor) *) t);
92
+ }
93
+
94
+ /* singleton methods */
95
+ static VALUE FUNCTION(rb_tensor,new)(int argc, VALUE *argv, VALUE klass)
96
+ {
97
+ unsigned int rank;
98
+ size_t dim;
99
+ GSL_TYPE(rbgsl_tensor) *t = NULL;
100
+ switch (argc) {
101
+ case 2:
102
+ rank = FIX2UINT(argv[0]);
103
+ dim = (size_t) FIX2UINT(argv[1]);
104
+ t = FUNCTION(rbgsl_tensor,alloc)(rank, dim);
105
+ break;
106
+ default:
107
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2, rank and dimension)",
108
+ argc);
109
+ break;
110
+ }
111
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), t);
112
+ }
113
+
114
+ static VALUE FUNCTION(rb_tensor,calloc)(VALUE klass, VALUE r, VALUE s)
115
+ {
116
+ unsigned int rank;
117
+ size_t dim;
118
+ GSL_TYPE(rbgsl_tensor) *t = NULL;
119
+ rank = FIX2UINT(r);
120
+ dim = (size_t) FIX2UINT(s);
121
+ t = FUNCTION(rbgsl_tensor,calloc)(rank, dim);
122
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), t);
123
+ }
124
+
125
+ static VALUE FUNCTION(rb_tensor,copy_singleton)(VALUE klass, VALUE obj)
126
+ {
127
+ GSL_TYPE(rbgsl_tensor) *t = NULL, *tnew = NULL;
128
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
129
+ tnew = FUNCTION(rbgsl_tensor,copy(t));
130
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), tnew);
131
+ }
132
+
133
+ static VALUE FUNCTION(rb_tensor,memcpy_singleton)(VALUE klass, VALUE a, VALUE b)
134
+ {
135
+ GSL_TYPE(rbgsl_tensor) *dst, *src;
136
+ CHECK_TEN(b);
137
+ Data_Get_Struct(a, GSL_TYPE(rbgsl_tensor), dst);
138
+ Data_Get_Struct(b, GSL_TYPE(rbgsl_tensor), src);
139
+ return INT2FIX(FUNCTION(tensor,memcpy)(dst->tensor, src->tensor));
140
+ }
141
+
142
+ static VALUE FUNCTION(rb_tensor,swap_singleton)(VALUE klass, VALUE a, VALUE b)
143
+ {
144
+ GSL_TYPE(rbgsl_tensor) *t1, *t2;
145
+ CHECK_TEN(b);
146
+ Data_Get_Struct(a, GSL_TYPE(rbgsl_tensor), t1);
147
+ Data_Get_Struct(b, GSL_TYPE(rbgsl_tensor), t2);
148
+ return INT2FIX(FUNCTION(tensor,swap)(t1->tensor, t2->tensor));
149
+ }
150
+
151
+ /*****/
152
+ static VALUE FUNCTION(rb_tensor,copy)(VALUE obj)
153
+ {
154
+ GSL_TYPE(rbgsl_tensor) *t = NULL, *tnew = NULL;
155
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
156
+ tnew = FUNCTION(rbgsl_tensor,copy)(t);
157
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), tnew);
158
+ }
159
+
160
+ static VALUE FUNCTION(rb_tensor,set_zero)(VALUE obj)
161
+ {
162
+ GSL_TYPE(rbgsl_tensor) *t = NULL;
163
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
164
+ FUNCTION(tensor,set_zero)(t->tensor);
165
+ return obj;
166
+ }
167
+
168
+ static VALUE FUNCTION(rb_tensor,set_all)(VALUE obj, VALUE xx)
169
+ {
170
+ GSL_TYPE(rbgsl_tensor) *t = NULL;
171
+ BASE x;
172
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
173
+ x = NUMCONV(xx);
174
+ FUNCTION(tensor,set_all)(t->tensor, x);
175
+ return obj;
176
+ }
177
+
178
+ static void rb_tensor_get_indices_array(tensor_indices *v, VALUE ary);
179
+ static void rbgsl_tensor_get_indices(int argc, VALUE *argv, tensor_indices *indices,
180
+ size_t *n);
181
+ #ifdef BASE_DOUBLE
182
+ static void rb_tensor_get_indices_array(tensor_indices *v, VALUE ary)
183
+ {
184
+ size_t i, nn;
185
+ nn = (size_t) GSL_MIN_INT((int) v->size, (int) RARRAY(ary)->len);
186
+ for (i = 0; i < nn; i++)
187
+ v->data[i] = FIX2UINT(rb_ary_entry(ary, i));
188
+ }
189
+
190
+ static void rbgsl_tensor_get_indices(int argc, VALUE *argv,
191
+ tensor_indices *indices, size_t *n)
192
+ {
193
+ size_t i;
194
+ for (i = 0; i < indices->size; i++) indices->data[i] = 0;
195
+ switch (argc) {
196
+ case 1:
197
+ switch (TYPE(argv[0])) {
198
+ case T_ARRAY:
199
+ *n = (size_t) GSL_MIN_INT((int) indices->size, (int) RARRAY(argv[0])->len);
200
+ rb_tensor_get_indices_array(indices, argv[0]);
201
+ break;
202
+ case T_FIXNUM:
203
+ *n = 1;
204
+ indices->data[0] = FIX2INT(argv[0]);
205
+ break;
206
+ default:
207
+ rb_raise(rb_eTypeError, "wrong argument type %s (Array expected)",
208
+ rb_class2name(CLASS_OF(argv[0])));
209
+ break;
210
+ }
211
+ break;
212
+ default:
213
+ *n = (size_t) GSL_MIN_INT(argc, (int) indices->size);
214
+ for (i = 0; i < *n; i++) {
215
+ CHECK_FIXNUM(argv[i]);
216
+ indices->data[i] = FIX2INT(argv[i]);
217
+ }
218
+ break;
219
+ }
220
+ }
221
+ #endif
222
+
223
+ size_t FUNCTION(tensor,position)(const size_t * indices,
224
+ const GSL_TYPE(tensor) * t);
225
+ static VALUE FUNCTION(rb_tensor,position)(int argc, VALUE *argv, VALUE obj)
226
+ {
227
+ GSL_TYPE(rbgsl_tensor) *t = NULL;
228
+ size_t n, position;
229
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
230
+ rbgsl_tensor_get_indices(argc, argv, t->indices, &n);
231
+ position = (size_t) FUNCTION(tensor,position)(t->indices->data,t->tensor);
232
+ return INT2FIX(position);
233
+ }
234
+ static VALUE FUNCTION(rb_tensor,subtensor)(int argc, VALUE *argv, VALUE obj);
235
+ static VALUE FUNCTION(rb_tensor,get)(int argc, VALUE *argv, VALUE obj)
236
+ {
237
+ GSL_TYPE(rbgsl_tensor) *t = NULL;
238
+ BASE x;
239
+ size_t n;
240
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
241
+ rbgsl_tensor_get_indices(argc, argv, t->indices, &n);
242
+ if (n < t->tensor->rank) {
243
+ return FUNCTION(rb_tensor,subtensor)(argc, argv, obj);
244
+ } else {
245
+ x = FUNCTION(tensor,get)(t->tensor, t->indices->data);
246
+ return C_TO_VALUE(x);
247
+ }
248
+ return Qnil;
249
+ }
250
+
251
+ static VALUE FUNCTION(rb_tensor,set)(int argc, VALUE *argv, VALUE obj)
252
+ {
253
+ GSL_TYPE(rbgsl_tensor) *t = NULL;
254
+ size_t n;
255
+ BASE x;
256
+ if (argc < 2)
257
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for >= 2)", argc);
258
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
259
+ rbgsl_tensor_get_indices(argc-1, argv, t->indices, &n);
260
+ x = NUMCONV(argv[argc-1]);
261
+ FUNCTION(tensor,set)(t->tensor, t->indices->data, x);
262
+ return obj;
263
+ }
264
+
265
+ static VALUE FUNCTION(rb_tensor,fread)(VALUE obj, VALUE io)
266
+ {
267
+ GSL_TYPE(rbgsl_tensor) *t = NULL;
268
+ FILE *f = NULL;
269
+ int status, flag = 0;
270
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
271
+ f = rb_gsl_open_readfile(io, &flag);
272
+ status = FUNCTION(tensor,fread)(f, t->tensor);
273
+ if (flag == 1) fclose(f);
274
+ return INT2FIX(status);
275
+ }
276
+
277
+ static VALUE FUNCTION(rb_tensor,fwrite)(VALUE obj, VALUE io)
278
+ {
279
+ GSL_TYPE(rbgsl_tensor) *t = NULL;
280
+ FILE *f = NULL;
281
+ int status, flag = 0;
282
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
283
+ f = rb_gsl_open_writefile(io, &flag);
284
+ status = FUNCTION(tensor,fwrite)(f, t->tensor);
285
+ if (flag == 1) fclose(f);
286
+ return INT2FIX(status);
287
+ }
288
+
289
+ static VALUE FUNCTION(rb_tensor,fprintf)(int argc, VALUE *argv, VALUE obj)
290
+ {
291
+ GSL_TYPE(rbgsl_tensor) *h = NULL;
292
+ FILE *fp = NULL;
293
+ int status, flag = 0;
294
+ if (argc != 1 && argc != 2)
295
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
296
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), h);
297
+ fp = rb_gsl_open_writefile(argv[0], &flag);
298
+ switch (argc) {
299
+ case 2:
300
+ if (TYPE(argv[1]) == T_STRING)
301
+ status = FUNCTION(tensor,fprintf)(fp, h->tensor, STR2CSTR(argv[1]));
302
+ else
303
+ rb_raise(rb_eTypeError, "argv 2 String expected");
304
+ break;
305
+ default:
306
+ status = FUNCTION(tensor,fprintf)(fp, h->tensor, OUT_FORMAT);
307
+ break;
308
+ }
309
+ if (flag == 1) fclose(fp);
310
+ return INT2FIX(status);
311
+ }
312
+
313
+ static VALUE FUNCTION(rb_tensor,printf)(int argc, VALUE *argv, VALUE obj)
314
+ {
315
+ GSL_TYPE(rbgsl_tensor) *h = NULL;
316
+ int status;
317
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), h);
318
+ if (argc == 1) {
319
+ if (TYPE(argv[0]) != T_STRING)
320
+ rb_raise(rb_eTypeError, "String expected");
321
+ else
322
+ status = FUNCTION(tensor,fprintf)(stdout, h->tensor, STR2CSTR(argv[0]));
323
+ } else {
324
+ status = FUNCTION(tensor,fprintf)(stdout, h->tensor, OUT_FORMAT);
325
+ }
326
+ return INT2FIX(status);
327
+ }
328
+
329
+ static VALUE FUNCTION(rb_tensor,fscanf)(VALUE obj, VALUE io)
330
+ {
331
+ GSL_TYPE(rbgsl_tensor) *h = NULL;
332
+ FILE *fp = NULL;
333
+ int status, flag = 0;
334
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), h);
335
+ fp = rb_gsl_open_readfile(io, &flag);
336
+ status = FUNCTION(tensor,fscanf)(fp, h->tensor);
337
+ if (flag == 1) fclose(fp);
338
+ return INT2FIX(status);
339
+ }
340
+
341
+ static VALUE FUNCTION(rb_tensor,swap_indices)(VALUE obj, VALUE ii, VALUE jj)
342
+ {
343
+ GSL_TYPE(rbgsl_tensor) *t, *tnew;
344
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
345
+ tnew = ALLOC(GSL_TYPE(rbgsl_tensor));
346
+ if (t->tensor->rank == 0)
347
+ tnew->indices = gsl_permutation_alloc(1);
348
+ else
349
+ tnew->indices = gsl_permutation_alloc(t->tensor->rank);
350
+ tnew->tensor = FUNCTION(tensor,swap_indices)(t->tensor, FIX2INT(ii), FIX2INT(jj));
351
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), tnew);
352
+ }
353
+
354
+ static VALUE FUNCTION(rb_tensor,max)(VALUE obj)
355
+ {
356
+ GSL_TYPE(rbgsl_tensor) *t;
357
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
358
+ return C_TO_VALUE(FUNCTION(tensor,max)(t->tensor));
359
+ }
360
+
361
+ static VALUE FUNCTION(rb_tensor,min)(VALUE obj)
362
+ {
363
+ GSL_TYPE(rbgsl_tensor) *t;
364
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
365
+ return C_TO_VALUE(FUNCTION(tensor,min)(t->tensor));
366
+ }
367
+
368
+ static VALUE FUNCTION(rb_tensor,minmax)(VALUE obj)
369
+ {
370
+ GSL_TYPE(rbgsl_tensor) *t;
371
+ BASE min, max;
372
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
373
+ FUNCTION(tensor,minmax)(t->tensor, &min, &max);
374
+ return rb_ary_new3(2, C_TO_VALUE(min), C_TO_VALUE(max));
375
+ }
376
+
377
+ static VALUE FUNCTION(rb_tensor,max_index)(VALUE obj)
378
+ {
379
+ GSL_TYPE(rbgsl_tensor) *t;
380
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
381
+ FUNCTION(tensor,max_index)(t->tensor, t->indices->data);
382
+ return Data_Wrap_Struct(cgsl_index, 0, NULL, t->indices);
383
+ }
384
+
385
+ static VALUE FUNCTION(rb_tensor,min_index)(VALUE obj)
386
+ {
387
+ GSL_TYPE(rbgsl_tensor) *t;
388
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
389
+ FUNCTION(tensor,min_index)(t->tensor, t->indices->data);
390
+ return Data_Wrap_Struct(cgsl_index, 0, NULL, t->indices);
391
+ }
392
+
393
+ static VALUE FUNCTION(rb_tensor,minmax_index)(VALUE obj)
394
+ {
395
+ GSL_TYPE(rbgsl_tensor) *t;
396
+ gsl_permutation *min, *max;
397
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
398
+ if (t->tensor->rank == 0) {
399
+ min = gsl_permutation_alloc(1);
400
+ max = gsl_permutation_alloc(1);
401
+ } else {
402
+ min = gsl_permutation_alloc(t->tensor->rank);
403
+ max = gsl_permutation_alloc(t->tensor->rank);
404
+ }
405
+ FUNCTION(tensor,minmax_index)(t->tensor, min->data, max->data);
406
+ return rb_ary_new3(2,
407
+ Data_Wrap_Struct(cgsl_index, 0, gsl_permutation_free, min),
408
+ Data_Wrap_Struct(cgsl_index, 0, gsl_permutation_free, max));
409
+ }
410
+
411
+ static VALUE FUNCTION(rb_tensor,isnull)(VALUE obj)
412
+ {
413
+ GSL_TYPE(rbgsl_tensor) *t;
414
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
415
+ return INT2FIX(FUNCTION(tensor,isnull)(t->tensor));
416
+ }
417
+
418
+ static VALUE FUNCTION(rb_tensor,isnull2)(VALUE obj)
419
+ {
420
+ GSL_TYPE(rbgsl_tensor) *t;
421
+ int status;
422
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
423
+ status = FUNCTION(tensor,isnull)(t->tensor);
424
+ if (status) return Qtrue;
425
+ else return Qfalse;
426
+ }
427
+
428
+ static VALUE FUNCTION(rb_tensor,oper)(VALUE obj, VALUE bb,
429
+ int flag)
430
+ {
431
+ GSL_TYPE(rbgsl_tensor) *a, *b, *anew;
432
+ BASE x;
433
+ int (*f)(GSL_TYPE(tensor)*, const GSL_TYPE(tensor)*);
434
+ int (*f2)(GSL_TYPE(tensor)*, const double);
435
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), a);
436
+ anew = FUNCTION(rbgsl_tensor,copy)(a);
437
+ if (TEN_P(bb)) {
438
+ Data_Get_Struct(bb, GSL_TYPE(rbgsl_tensor), b);
439
+ switch (flag) {
440
+ case TENSOR_ADD: f = FUNCTION(&tensor,add); break;
441
+ case TENSOR_SUB: f = FUNCTION(&tensor,sub); break;
442
+ case TENSOR_MUL_ELEMENTS: f = FUNCTION(&tensor,mul_elements); break;
443
+ case TENSOR_DIV_ELEMENTS: f = FUNCTION(&tensor,div_elements); break;
444
+ default: rb_raise(rb_eRuntimeError, "unknown operation"); break;
445
+ }
446
+ (*f)(anew->tensor, b->tensor);
447
+ } else {
448
+ switch (flag) {
449
+ case TENSOR_ADD:
450
+ case TENSOR_ADD_CONSTANT:
451
+ x = NUMCONV(bb);
452
+ f2 = FUNCTION(&tensor,add_constant);
453
+ break;
454
+ case TENSOR_SUB:
455
+ x = -NUMCONV(bb);
456
+ f2 = FUNCTION(&tensor,add_constant);
457
+ break;
458
+ case TENSOR_ADD_DIAGONAL: f2 = FUNCTION(&tensor,add_diagonal); break;
459
+ case TENSOR_MUL_ELEMENTS:
460
+ case TENSOR_SCALE:
461
+ x = NUMCONV(bb);
462
+ f2 = FUNCTION(&tensor,scale);
463
+ break;
464
+ case TENSOR_DIV_ELEMENTS:
465
+ x = 1.0/NUMCONV(bb);
466
+ f2 = FUNCTION(&tensor,scale);
467
+ break;
468
+ default: rb_raise(rb_eRuntimeError, "unknown operation"); break;
469
+ }
470
+ (*f2)(anew->tensor, x);
471
+ }
472
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), anew);
473
+ }
474
+
475
+
476
+ static VALUE FUNCTION(rb_tensor,oper_bang)(VALUE obj, VALUE bb, int flag)
477
+ {
478
+ GSL_TYPE(rbgsl_tensor) *a, *b;
479
+ BASE x;
480
+ int (*f)(GSL_TYPE(tensor)*, const GSL_TYPE(tensor)*);
481
+ int (*f2)(GSL_TYPE(tensor)*, const double);
482
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), a);
483
+ if (TEN_P(bb)) {
484
+ Data_Get_Struct(bb, GSL_TYPE(rbgsl_tensor), b);
485
+ switch (flag) {
486
+ case TENSOR_ADD: f = FUNCTION(&tensor,add); break;
487
+ case TENSOR_SUB: f = FUNCTION(&tensor,sub); break;
488
+ case TENSOR_MUL_ELEMENTS: f = FUNCTION(&tensor,mul_elements); break;
489
+ case TENSOR_DIV_ELEMENTS: f = FUNCTION(&tensor,div_elements); break;
490
+ default: rb_raise(rb_eRuntimeError, "unknown operation"); break;
491
+ }
492
+ (*f)(a->tensor, b->tensor);
493
+ } else {
494
+ switch (flag) {
495
+ case TENSOR_ADD:
496
+ case TENSOR_ADD_CONSTANT:
497
+ x = NUMCONV(bb);
498
+ f2 = FUNCTION(&tensor,add_constant);
499
+ break;
500
+ case TENSOR_SUB:
501
+ x = -NUMCONV(bb);
502
+ f2 = FUNCTION(&tensor,add_constant);
503
+ break;
504
+ case TENSOR_ADD_DIAGONAL: f2 = FUNCTION(&tensor,add_diagonal); break;
505
+ case TENSOR_MUL_ELEMENTS:
506
+ case TENSOR_SCALE:
507
+ x = NUMCONV(bb);
508
+ f2 = FUNCTION(&tensor,scale);
509
+ break;
510
+ case TENSOR_DIV_ELEMENTS:
511
+ x = 1.0/NUMCONV(bb);
512
+ f2 = FUNCTION(&tensor,scale);
513
+ break;
514
+ default: rb_raise(rb_eRuntimeError, "unknown operation"); break;
515
+ }
516
+ (*f2)(a->tensor, x);
517
+ }
518
+ return obj;
519
+ }
520
+
521
+ static VALUE FUNCTION(rb_tensor,add)(VALUE obj, VALUE bb)
522
+ {
523
+ return FUNCTION(rb_tensor,oper)(obj, bb, TENSOR_ADD);
524
+ }
525
+
526
+ static VALUE FUNCTION(rb_tensor,sub)(VALUE obj, VALUE bb)
527
+ {
528
+ return FUNCTION(rb_tensor,oper)(obj, bb, TENSOR_SUB);
529
+ }
530
+
531
+ static VALUE FUNCTION(rb_tensor,mul_elements)(VALUE obj, VALUE bb)
532
+ {
533
+ return FUNCTION(rb_tensor,oper)(obj, bb, TENSOR_MUL_ELEMENTS);
534
+ }
535
+
536
+ static VALUE FUNCTION(rb_tensor,div_elements)(VALUE obj, VALUE bb)
537
+ {
538
+ return FUNCTION(rb_tensor,oper)(obj, bb, TENSOR_DIV_ELEMENTS);
539
+ }
540
+
541
+ static VALUE FUNCTION(rb_tensor,add_constant)(VALUE obj, VALUE bb)
542
+ {
543
+ return FUNCTION(rb_tensor,oper)(obj, bb, TENSOR_ADD_CONSTANT);
544
+ }
545
+
546
+ static VALUE FUNCTION(rb_tensor,add_diagonal)(VALUE obj, VALUE bb)
547
+ {
548
+ return FUNCTION(rb_tensor,oper)(obj, bb, TENSOR_ADD_DIAGONAL);
549
+ }
550
+
551
+ static VALUE FUNCTION(rb_tensor,scale)(VALUE obj, VALUE bb)
552
+ {
553
+ return FUNCTION(rb_tensor,oper)(obj, bb, TENSOR_SCALE);
554
+ }
555
+
556
+ /***/
557
+ static VALUE FUNCTION(rb_tensor,add_bang)(VALUE obj, VALUE bb)
558
+ {
559
+ return FUNCTION(rb_tensor,oper_bang)(obj, bb, TENSOR_ADD);
560
+ }
561
+
562
+ static VALUE FUNCTION(rb_tensor,sub_bang)(VALUE obj, VALUE bb)
563
+ {
564
+ return FUNCTION(rb_tensor,oper_bang)(obj, bb, TENSOR_SUB);
565
+ }
566
+
567
+ static VALUE FUNCTION(rb_tensor,mul_elements_bang)(VALUE obj, VALUE bb)
568
+ {
569
+ return FUNCTION(rb_tensor,oper_bang)(obj, bb, TENSOR_MUL_ELEMENTS);
570
+ }
571
+
572
+ static VALUE FUNCTION(rb_tensor,div_elements_bang)(VALUE obj, VALUE bb)
573
+ {
574
+ return FUNCTION(rb_tensor,oper_bang)(obj, bb, TENSOR_DIV_ELEMENTS);
575
+ }
576
+
577
+ static VALUE FUNCTION(rb_tensor,add_constant_bang)(VALUE obj, VALUE bb)
578
+ {
579
+ return FUNCTION(rb_tensor,oper_bang)(obj, bb, TENSOR_ADD_CONSTANT);
580
+ }
581
+
582
+ static VALUE FUNCTION(rb_tensor,add_diagonal_bang)(VALUE obj, VALUE bb)
583
+ {
584
+ return FUNCTION(rb_tensor,oper_bang)(obj, bb, TENSOR_ADD_DIAGONAL);
585
+ }
586
+
587
+ static VALUE FUNCTION(rb_tensor,scale_bang)(VALUE obj, VALUE bb)
588
+ {
589
+ return FUNCTION(rb_tensor,oper_bang)(obj, bb, TENSOR_SCALE);
590
+ }
591
+
592
+ /*****/
593
+ static VALUE FUNCTION(rb_tensor,product_singleton)(VALUE obj, VALUE aa, VALUE bb)
594
+ {
595
+ GSL_TYPE(rbgsl_tensor) *a, *b, *c;
596
+ CHECK_TEN(aa);
597
+ CHECK_TEN(bb);
598
+ Data_Get_Struct(aa, GSL_TYPE(rbgsl_tensor), a);
599
+ switch (TYPE(bb)) {
600
+ case T_FIXNUM:
601
+ case T_BIGNUM:
602
+ case T_FLOAT:
603
+ return FUNCTION(rb_tensor,mul_elements)(aa, bb);
604
+ break;
605
+ default:
606
+ Data_Get_Struct(bb, GSL_TYPE(rbgsl_tensor), b);
607
+ c = ALLOC(GSL_TYPE(rbgsl_tensor));
608
+ c->tensor = FUNCTION(tensor,product(a->tensor, b->tensor));
609
+ if (c->tensor->rank == 0)
610
+ c->indices = gsl_permutation_alloc(1);
611
+ else
612
+ c->indices = gsl_permutation_alloc(c->tensor->rank);
613
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), c);
614
+ break;
615
+ }
616
+ }
617
+
618
+ static VALUE FUNCTION(rb_tensor,product)(VALUE obj, VALUE bb)
619
+ {
620
+ GSL_TYPE(rbgsl_tensor) *a, *b, *c;
621
+ switch (TYPE(bb)) {
622
+ case T_FIXNUM:
623
+ case T_BIGNUM:
624
+ case T_FLOAT:
625
+ return FUNCTION(rb_tensor,mul_elements)(obj, bb);
626
+ break;
627
+ default:
628
+ CHECK_TEN(bb);
629
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), a);
630
+ Data_Get_Struct(bb, GSL_TYPE(rbgsl_tensor), b);
631
+ c = ALLOC(GSL_TYPE(rbgsl_tensor));
632
+ c->tensor = FUNCTION(tensor,product(a->tensor, b->tensor));
633
+ if (c->tensor->rank == 0)
634
+ c->indices = gsl_permutation_alloc(1);
635
+ else
636
+ c->indices = gsl_permutation_alloc(c->tensor->rank);
637
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), c);
638
+ break;
639
+ }
640
+ }
641
+
642
+ static VALUE FUNCTION(rb_tensor,contract)(VALUE obj, VALUE ii, VALUE jj)
643
+ {
644
+ GSL_TYPE(rbgsl_tensor) *t, *tnew;
645
+ size_t rank;
646
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
647
+ tnew = ALLOC(GSL_TYPE(rbgsl_tensor));
648
+ tnew->tensor = FUNCTION(tensor,contract)(t->tensor, FIX2INT(ii), FIX2INT(jj));
649
+ if (tnew->tensor->rank == 0) rank = 1;
650
+ else rank = tnew->tensor->rank;
651
+ tnew->indices = gsl_permutation_alloc(rank);
652
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), tnew);
653
+ }
654
+
655
+ static VALUE FUNCTION(rb_tensor,size)(VALUE obj)
656
+ {
657
+ GSL_TYPE(rbgsl_tensor) *t;
658
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
659
+ return INT2FIX(t->tensor->size);
660
+ }
661
+
662
+ static VALUE FUNCTION(rb_tensor,rank)(VALUE obj)
663
+ {
664
+ GSL_TYPE(rbgsl_tensor) *t;
665
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
666
+ return INT2FIX(t->tensor->rank);
667
+ }
668
+
669
+ static VALUE FUNCTION(rb_tensor,dimension)(VALUE obj)
670
+ {
671
+ GSL_TYPE(rbgsl_tensor) *t;
672
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
673
+ return INT2FIX(t->tensor->dimension);
674
+ }
675
+
676
+ static VALUE FUNCTION(rb_tensor,data)(VALUE obj)
677
+ {
678
+ GSL_TYPE(rbgsl_tensor) *t;
679
+ QUALIFIED_VIEW(gsl_vector,view) *v = NULL;
680
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
681
+ v = FUNCTION(rb_gsl_make_vector,view)(t->tensor->data, t->tensor->size, 1);
682
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,view), 0, free, v);
683
+ }
684
+
685
+ static VALUE FUNCTION(rb_tensor,2matrix)(VALUE obj)
686
+ {
687
+ GSL_TYPE(rbgsl_tensor) *t;
688
+ GSL_TYPE(gsl_matrix) *m = NULL;
689
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
690
+ m = (GSL_TYPE(gsl_matrix)*) FUNCTION(tensor,2matrix)(t->tensor);
691
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_matrix,view), 0, FUNCTION(gsl_matrix,free), m);
692
+ }
693
+
694
+ static VALUE FUNCTION(rb_tensor,2vector)(VALUE obj)
695
+ {
696
+ GSL_TYPE(rbgsl_tensor) *t;
697
+ GSL_TYPE(gsl_vector) *v = NULL;
698
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
699
+ v = (GSL_TYPE(gsl_vector) *) FUNCTION(tensor,2vector)(t->tensor);
700
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,view), 0, FUNCTION(gsl_vector,free), v);
701
+ }
702
+
703
+ static VALUE FUNCTION(rb_tensor,to_v)(VALUE obj)
704
+ {
705
+ GSL_TYPE(rbgsl_tensor) *t;
706
+ GSL_TYPE(gsl_vector) *v;
707
+
708
+ v = FUNCTION(gsl_vector,alloc)(t->tensor->size);
709
+ memcpy(v->data, t->tensor->data, sizeof(BASE)*v->size);
710
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_vector), 0, FUNCTION(gsl_vector,free), v);
711
+ }
712
+
713
+ /*
714
+ Creates a subtensor slicing the existing tensor.
715
+ NOTE: no new data region is malloced.
716
+ t: Tensor
717
+ rank: rank of the tensor created
718
+ */
719
+ GSL_TYPE(tensor) FUNCTION(tensor,subtensor)(const GSL_TYPE(tensor) *t,
720
+ const unsigned int rank,
721
+ size_t *indices)
722
+ {
723
+ GSL_TYPE(tensor) tnew;
724
+ size_t position;
725
+ tnew.rank = rank;
726
+ tnew.dimension = t->dimension;
727
+ tnew.size = quick_pow(t->dimension, rank);
728
+ position = FUNCTION(tensor,position)(indices, t);
729
+ if (position >= t->size)
730
+ rb_raise(rb_eRangeError, "wrong indices given");
731
+ tnew.data = t->data + position;
732
+ return tnew;
733
+ }
734
+
735
+ static VALUE FUNCTION(rb_tensor,subtensor)(int argc, VALUE *argv, VALUE obj)
736
+ {
737
+ GSL_TYPE(rbgsl_tensor) *t, *tnew;
738
+ unsigned int rank;
739
+ size_t n;
740
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
741
+ /* n: number of indices given */
742
+ rbgsl_tensor_get_indices(argc, argv, t->indices, &n);
743
+ rank = t->tensor->rank - n;
744
+ tnew = ALLOC(GSL_TYPE(rbgsl_tensor));
745
+ tnew->tensor = (GSL_TYPE(tensor)*) malloc(sizeof(GSL_TYPE(tensor)));
746
+ *(tnew->tensor) = FUNCTION(tensor,subtensor)(t->tensor, rank, t->indices->data);
747
+ if (rank == 0)
748
+ tnew->indices = gsl_permutation_alloc(1);
749
+ else
750
+ tnew->indices = gsl_permutation_alloc(rank);
751
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_tensor,view), 0, FUNCTION(rbgsl_tensor,free2), tnew);
752
+ }
753
+
754
+ #ifdef BASE_DOUBLE
755
+ #define SHOW_ELM 6
756
+ #define PRINTF_FORMAT "%4.3e "
757
+ #else
758
+ #define SHOW_ELM 15
759
+ #define PRINTF_FORMAT "%d "
760
+ #endif
761
+ static VALUE FUNCTION(rb_tensor,to_s)(VALUE obj)
762
+ {
763
+ GSL_TYPE(rbgsl_tensor) *t;
764
+ QUALIFIED_VIEW(gsl_matrix,view) matrix;
765
+ QUALIFIED_VIEW(gsl_vector,view) vector;
766
+ GSL_TYPE(gsl_matrix) *m;
767
+ GSL_TYPE(gsl_vector) *v;
768
+ char buf[16];
769
+ size_t i, j;
770
+ VALUE str;
771
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
772
+ str = rb_str_new2("[ ");
773
+ switch (t->tensor->rank) {
774
+ case 2:
775
+ matrix.matrix.data = t->tensor->data;
776
+ matrix.matrix.size1 = t->tensor->dimension;
777
+ matrix.matrix.size2 = t->tensor->dimension;
778
+ matrix.matrix.tda = t->tensor->dimension;
779
+ matrix.matrix.block = 0;
780
+ matrix.matrix.owner = 0;
781
+ m = &(matrix.matrix);
782
+ for (i = 0; i < m->size1; i++) {
783
+ if (i != 0) {
784
+ strcpy(buf, " ");
785
+ rb_str_cat(str, buf, strlen(buf));
786
+ }
787
+ for (j = 0; j < m->size2; j++) {
788
+ sprintf(buf, PRINTF_FORMAT, FUNCTION(gsl_matrix,get)(m, i, j));
789
+ rb_str_cat(str, buf, strlen(buf));
790
+ if (j == SHOW_ELM) {
791
+ strcpy(buf, "... ");
792
+ rb_str_cat(str, buf, strlen(buf));
793
+ break;
794
+ }
795
+ }
796
+ if (i == 6) {
797
+ strcpy(buf, "\n ... ]");
798
+ rb_str_cat(str, buf, strlen(buf));
799
+ break;
800
+ }
801
+ if (i == m->size1 - 1) {
802
+ strcpy(buf, "]");
803
+ rb_str_cat(str, buf, strlen(buf));
804
+ } else {
805
+ strcpy(buf, "\n");
806
+ rb_str_cat(str, buf, strlen(buf));
807
+ }
808
+ }
809
+ return str;
810
+ break;
811
+ default:
812
+ vector.vector.data = t->tensor->data;
813
+ vector.vector.stride = 1;
814
+ vector.vector.size = t->tensor->size;
815
+ vector.vector.owner = 0;
816
+ vector.vector.block = 0;
817
+ v = &(vector.vector);
818
+ sprintf(buf, PRINTF_FORMAT, FUNCTION(gsl_vector,get)(v, 0));
819
+ rb_str_cat(str, buf, strlen(buf));
820
+ for (i = 1; i < v->size; i++) {
821
+ sprintf(buf, PRINTF_FORMAT, FUNCTION(gsl_vector,get)(v, i));
822
+ rb_str_cat(str, buf, strlen(buf));
823
+ if (i == SHOW_ELM && i != v->size-1) {
824
+ strcpy(buf, "... ");
825
+ rb_str_cat(str, buf, strlen(buf));
826
+ break;
827
+ }
828
+ }
829
+ sprintf(buf, "]");
830
+ rb_str_cat(str, buf, strlen(buf));
831
+ return str;
832
+ break;
833
+ }
834
+ }
835
+ #undef SHOW_ELM
836
+ #undef PRINTF_FORMAT
837
+
838
+ static VALUE FUNCTION(rb_tensor,inspect)(VALUE obj)
839
+ {
840
+ VALUE str;
841
+ char buf[64];
842
+ sprintf(buf, "%s\n", rb_class2name(CLASS_OF(obj)));
843
+ str = rb_str_new2(buf);
844
+ return rb_str_concat(str, FUNCTION(rb_tensor,to_s)(obj));
845
+ }
846
+
847
+ VALUE FUNCTION(rb_tensor,equal)(int argc, VALUE *argv, VALUE obj)
848
+ {
849
+ GSL_TYPE(rbgsl_tensor) *a, *b;
850
+ GSL_TYPE(gsl_matrix) *m;
851
+ GSL_TYPE(gsl_vector) *v;
852
+ VALUE other;
853
+ double eps = 1e-10;
854
+ size_t i;
855
+ switch (argc) {
856
+ case 2:
857
+ other = argv[0];
858
+ eps = NUM2DBL(argv[1]);
859
+ break;
860
+ case 1:
861
+ other = argv[0];
862
+ break;
863
+ default:
864
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
865
+ }
866
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), a);
867
+ if (TEN_P(other)) {
868
+ Data_Get_Struct(other, GSL_TYPE(rbgsl_tensor), b);
869
+ if (a->tensor->rank != b->tensor->rank) return Qfalse;
870
+ if (a->tensor->dimension != b->tensor->dimension) return Qfalse;
871
+ if (a->tensor->size != b->tensor->size) return Qfalse;
872
+ for (i = 0; i < a->tensor->size; i++)
873
+ if (fabs(a->tensor->data[i]-b->tensor->data[i]) > eps)
874
+ return Qfalse;
875
+ return Qtrue;
876
+ } else if (MAT_P(other)) {
877
+ if (a->tensor->rank != 2) return Qfalse;
878
+ Data_Get_Struct(other, GSL_TYPE(gsl_matrix), m);
879
+ if (a->tensor->dimension != m->size1 || a->tensor->dimension != m->size2)
880
+ return Qfalse;
881
+ for (i = 0; i < a->tensor->size; i++)
882
+ if (fabs(a->tensor->data[i]-m->data[i]) > eps)
883
+ return Qfalse;
884
+ return Qtrue;
885
+ } else if (VEC_P(other)) {
886
+ Data_Get_Struct(other, GSL_TYPE(gsl_vector), v);
887
+ if (a->tensor->size != v->size) return Qfalse;
888
+ for (i = 0; i < a->tensor->size; i++)
889
+ if (fabs(a->tensor->data[i]-v->data[i]) > eps)
890
+ return Qfalse;
891
+ return Qtrue;
892
+ } else {
893
+ rb_raise(rb_eTypeError, "wrong argument type %s (Tensor, Matrix or Vector expected)",
894
+ rb_class2name(CLASS_OF(other)));
895
+ }
896
+ }
897
+
898
+ static VALUE FUNCTION(rb_tensor,uplus)(VALUE obj)
899
+ {
900
+ return obj;
901
+ }
902
+
903
+ static VALUE FUNCTION(rb_tensor,uminus)(VALUE obj)
904
+ {
905
+ GSL_TYPE(rbgsl_tensor) *t, *tnew;
906
+ size_t i;
907
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
908
+ tnew = FUNCTION(rbgsl_tensor,copy)(t);
909
+ for (i = 0; i < tnew->tensor->size; i++)
910
+ tnew->tensor->data[i] *= -1;
911
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), tnew);
912
+ }
913
+
914
+ static VALUE FUNCTION(rb_tensor,coerce)(VALUE obj, VALUE other)
915
+ {
916
+ GSL_TYPE(rbgsl_tensor) *t, *tnew;
917
+ VALUE tt;
918
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
919
+ switch (TYPE(other)) {
920
+ case T_FLOAT:
921
+ case T_FIXNUM:
922
+ case T_BIGNUM:
923
+ tnew = FUNCTION(rbgsl_tensor,alloc)(t->tensor->rank, t->tensor->dimension);
924
+ FUNCTION(tensor,set_all)(tnew->tensor, NUMCONV(other));
925
+ tt = Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), tnew);
926
+ return rb_ary_new3(2, tt, obj);
927
+ break;
928
+ default:
929
+ rb_raise(rb_eRuntimeError, "undefined operation with %s",
930
+ rb_class2name(CLASS_OF(other)));
931
+ break;
932
+ }
933
+ }
934
+
935
+ static VALUE FUNCTION(rb_tensor,info)(VALUE obj)
936
+ {
937
+ GSL_TYPE(rbgsl_tensor) *t;
938
+ char buf[256];
939
+ Data_Get_Struct(obj, GSL_TYPE(rbgsl_tensor), t);
940
+ sprintf(buf, "Class: %s\n", rb_class2name(CLASS_OF(obj)));
941
+ // sprintf(buf, "%sSuperClass: %s\n", buf, rb_class2name(RCLASS(CLASS_OF(obj))->super));
942
+ sprintf(buf, "%sRank: %d\n", buf, (int) t->tensor->rank);
943
+ sprintf(buf, "%sDimension: %d\n", buf, (int) t->tensor->dimension);
944
+ sprintf(buf, "%sSize: %d\n", buf, (int) t->tensor->size);
945
+ return rb_str_new2(buf);
946
+ }
947
+
948
+ void FUNCTION(Init_tensor,init)(VALUE module)
949
+ {
950
+ #ifdef BASE_DOUBLE
951
+ cgsl_tensor = rb_define_class_under(module, "Tensor", cGSL_Object);
952
+ cgsl_tensor_int = rb_define_class_under(cgsl_tensor, "Int", cGSL_Object);
953
+ cgsl_tensor_view = rb_define_class_under(cgsl_tensor, "View", cgsl_tensor);
954
+ cgsl_tensor_int_view = rb_define_class_under(cgsl_tensor_int, "View",
955
+ cgsl_tensor_int);
956
+ /*
957
+ cgsl_index = rb_define_class_under(cgsl_tensor, "Index",
958
+ cgsl_permutation);*/
959
+ #endif
960
+
961
+ rb_define_singleton_method(GSL_TYPE(cgsl_tensor), "new",
962
+ FUNCTION(rb_tensor,new), -1);
963
+ rb_define_singleton_method(GSL_TYPE(cgsl_tensor), "[]",
964
+ FUNCTION(rb_tensor,new), -1);
965
+ rb_define_singleton_method(GSL_TYPE(cgsl_tensor), "alloc",
966
+ FUNCTION(rb_tensor,new), -1);
967
+ rb_define_singleton_method(GSL_TYPE(cgsl_tensor), "calloc",
968
+ FUNCTION(rb_tensor,calloc), 2);
969
+ rb_define_singleton_method(GSL_TYPE(cgsl_tensor), "copy",
970
+ FUNCTION(rb_tensor,copy_singleton), 1);
971
+ rb_define_singleton_method(GSL_TYPE(cgsl_tensor), "memcpy",
972
+ FUNCTION(rb_tensor,memcpy_singleton), 2);
973
+ rb_define_singleton_method(GSL_TYPE(cgsl_tensor), "swap",
974
+ FUNCTION(rb_tensor,swap_singleton), 2);
975
+
976
+ /*****/
977
+
978
+ rb_define_method(GSL_TYPE(cgsl_tensor), "copy",
979
+ FUNCTION(rb_tensor,copy), 0);
980
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "clone", "copy");
981
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "duplicate", "copy");
982
+ rb_define_method(GSL_TYPE(cgsl_tensor), "set_zero",
983
+ FUNCTION(rb_tensor,set_zero), 0);
984
+ rb_define_method(GSL_TYPE(cgsl_tensor), "set_all",
985
+ FUNCTION(rb_tensor,set_all), 1);
986
+
987
+ rb_define_method(GSL_TYPE(cgsl_tensor), "position",
988
+ FUNCTION(rb_tensor,position), -1);
989
+ rb_define_method(GSL_TYPE(cgsl_tensor), "get",
990
+ FUNCTION(rb_tensor,get), -1);
991
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "[]", "get");
992
+ rb_define_method(GSL_TYPE(cgsl_tensor), "set",
993
+ FUNCTION(rb_tensor,set), -1);
994
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "[]=", "set");
995
+
996
+ rb_define_method(GSL_TYPE(cgsl_tensor), "fread",
997
+ FUNCTION(rb_tensor,fread), 1);
998
+ rb_define_method(GSL_TYPE(cgsl_tensor), "fwrite",
999
+ FUNCTION(rb_tensor,fwrite), 1);
1000
+ rb_define_method(GSL_TYPE(cgsl_tensor), "fprintf",
1001
+ FUNCTION(rb_tensor,fprintf), -1);
1002
+ rb_define_method(GSL_TYPE(cgsl_tensor), "printf",
1003
+ FUNCTION(rb_tensor,printf), -1);
1004
+ rb_define_method(GSL_TYPE(cgsl_tensor), "fscanf",
1005
+ FUNCTION(rb_tensor,fscanf), 1);
1006
+
1007
+ rb_define_method(GSL_TYPE(cgsl_tensor), "swap_indices",
1008
+ FUNCTION(rb_tensor,swap_indices), 2);
1009
+
1010
+ rb_define_method(GSL_TYPE(cgsl_tensor), "max",
1011
+ FUNCTION(rb_tensor,max), 0);
1012
+ rb_define_method(GSL_TYPE(cgsl_tensor), "min",
1013
+ FUNCTION(rb_tensor,min), 0);
1014
+ rb_define_method(GSL_TYPE(cgsl_tensor), "minmax",
1015
+ FUNCTION(rb_tensor,minmax), 0);
1016
+ rb_define_method(GSL_TYPE(cgsl_tensor), "max_index",
1017
+ FUNCTION(rb_tensor,max_index), 0);
1018
+ rb_define_method(GSL_TYPE(cgsl_tensor), "min_index",
1019
+ FUNCTION(rb_tensor,min_index), 0);
1020
+ rb_define_method(GSL_TYPE(cgsl_tensor), "minmax_index",
1021
+ FUNCTION(rb_tensor,minmax_index), 0);
1022
+
1023
+ rb_define_method(GSL_TYPE(cgsl_tensor), "isnull",
1024
+ FUNCTION(rb_tensor,isnull), 0);
1025
+ rb_define_method(GSL_TYPE(cgsl_tensor), "isnull?",
1026
+ FUNCTION(rb_tensor,isnull2), 0);
1027
+
1028
+ rb_define_method(GSL_TYPE(cgsl_tensor), "add",
1029
+ FUNCTION(rb_tensor,add), 1);
1030
+ rb_define_method(GSL_TYPE(cgsl_tensor), "sub",
1031
+ FUNCTION(rb_tensor,sub), 1);
1032
+ rb_define_method(GSL_TYPE(cgsl_tensor), "mul_elements",
1033
+ FUNCTION(rb_tensor,mul_elements), 1);
1034
+ rb_define_method(GSL_TYPE(cgsl_tensor), "div_elements",
1035
+ FUNCTION(rb_tensor,div_elements), 1);
1036
+ rb_define_method(GSL_TYPE(cgsl_tensor), "add_constant",
1037
+ FUNCTION(rb_tensor,add_constant), 1);
1038
+ rb_define_method(GSL_TYPE(cgsl_tensor), "add_diagonal",
1039
+ FUNCTION(rb_tensor,add_diagonal), 1);
1040
+ rb_define_method(GSL_TYPE(cgsl_tensor), "scale",
1041
+ FUNCTION(rb_tensor,scale), 1);
1042
+ rb_define_singleton_method(GSL_TYPE(cgsl_tensor), "product",
1043
+ FUNCTION(rb_tensor,product_singleton), 2);
1044
+ rb_define_method(GSL_TYPE(cgsl_tensor), "product",
1045
+ FUNCTION(rb_tensor,product), 1);
1046
+ rb_define_method(GSL_TYPE(cgsl_tensor), "contract",
1047
+ FUNCTION(rb_tensor,contract), 2);
1048
+
1049
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "+", "add");
1050
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "-", "sub");
1051
+ /* rb_define_alias(GSL_TYPE(cgsl_tensor), "*", "mul_elements");*/
1052
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "/", "div_elements");
1053
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "*", "product");
1054
+
1055
+ rb_define_method(GSL_TYPE(cgsl_tensor), "add!",
1056
+ FUNCTION(rb_tensor,add_bang), 1);
1057
+ rb_define_method(GSL_TYPE(cgsl_tensor), "sub!",
1058
+ FUNCTION(rb_tensor,sub_bang), 1);
1059
+ rb_define_method(GSL_TYPE(cgsl_tensor), "mul_elements!",
1060
+ FUNCTION(rb_tensor,mul_elements_bang), 1);
1061
+ rb_define_method(GSL_TYPE(cgsl_tensor), "div_elements!",
1062
+ FUNCTION(rb_tensor,div_elements_bang), 1);
1063
+ rb_define_method(GSL_TYPE(cgsl_tensor), "add_constant!",
1064
+ FUNCTION(rb_tensor,add_constant_bang), 1);
1065
+ rb_define_method(GSL_TYPE(cgsl_tensor), "add_diagonal!",
1066
+ FUNCTION(rb_tensor,add_diagonal_bang), 1);
1067
+ rb_define_method(GSL_TYPE(cgsl_tensor), "scale!",
1068
+ FUNCTION(rb_tensor,scale_bang), 1);
1069
+
1070
+ rb_define_method(GSL_TYPE(cgsl_tensor), "+@",
1071
+ FUNCTION(rb_tensor,uplus), 0);
1072
+ rb_define_method(GSL_TYPE(cgsl_tensor), "-@",
1073
+ FUNCTION(rb_tensor,uminus), 0);
1074
+
1075
+ rb_define_method(GSL_TYPE(cgsl_tensor), "size",
1076
+ FUNCTION(rb_tensor,size), 0);
1077
+ rb_define_method(GSL_TYPE(cgsl_tensor), "rank",
1078
+ FUNCTION(rb_tensor,rank), 0);
1079
+ rb_define_method(GSL_TYPE(cgsl_tensor), "dimension",
1080
+ FUNCTION(rb_tensor,dimension), 0);
1081
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "dim", "dimension");
1082
+ rb_define_method(GSL_TYPE(cgsl_tensor), "data",
1083
+ FUNCTION(rb_tensor,data), 0);
1084
+
1085
+ rb_define_method(GSL_TYPE(cgsl_tensor), "to_v",
1086
+ FUNCTION(rb_tensor,to_v), 0);
1087
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "to_gv", "to_v");
1088
+
1089
+ rb_define_method(GSL_TYPE(cgsl_tensor), "to_vector",
1090
+ FUNCTION(rb_tensor,2vector), 0);
1091
+ rb_define_method(GSL_TYPE(cgsl_tensor), "to_matrix",
1092
+ FUNCTION(rb_tensor,2matrix), 0);
1093
+
1094
+ rb_define_method(GSL_TYPE(cgsl_tensor), "subtensor",
1095
+ FUNCTION(rb_tensor,subtensor), -1);
1096
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "view", "subtensor");
1097
+
1098
+ rb_define_method(GSL_TYPE(cgsl_tensor), "to_s",
1099
+ FUNCTION(rb_tensor,to_s), 0);
1100
+ rb_define_method(GSL_TYPE(cgsl_tensor), "inspect",
1101
+ FUNCTION(rb_tensor,inspect), 0);
1102
+
1103
+ rb_define_method(GSL_TYPE(cgsl_tensor), "equal?",
1104
+ FUNCTION(rb_tensor,equal), -1);
1105
+ rb_define_alias(GSL_TYPE(cgsl_tensor), "==", "equal?");
1106
+
1107
+ rb_define_method(GSL_TYPE(cgsl_tensor), "coerce",
1108
+ FUNCTION(rb_tensor,coerce), 1);
1109
+
1110
+ rb_define_method(GSL_TYPE(cgsl_tensor), "info",
1111
+ FUNCTION(rb_tensor,info), 0);
1112
+ }
1113
+
1114
+ #undef NUMCONV
1115
+ #undef C_TO_VALUE
1116
+ #undef CHECK_TEN
1117
+ #undef TEN_P
1118
+ #undef VEC_P
1119
+ #undef MAT_P
1120
+
1121
+ #endif