carray 1.1.4 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/COPYING +56 -0
- data/GPL +340 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +33 -0
- data/LEGAL +50 -0
- data/NOTE +73 -0
- data/Rakefile +20 -0
- data/TODO +5 -0
- data/ca_iter_block.c +242 -0
- data/ca_iter_dimension.c +287 -0
- data/ca_iter_window.c +202 -0
- data/ca_obj_array.c +1189 -0
- data/ca_obj_bitarray.c +523 -0
- data/ca_obj_bitfield.c +636 -0
- data/ca_obj_block.c +885 -0
- data/ca_obj_fake.c +405 -0
- data/ca_obj_farray.c +482 -0
- data/ca_obj_field.c +625 -0
- data/ca_obj_grid.c +738 -0
- data/ca_obj_mapping.c +614 -0
- data/ca_obj_object.c +777 -0
- data/ca_obj_reduce.c +299 -0
- data/ca_obj_refer.c +627 -0
- data/ca_obj_repeat.c +640 -0
- data/ca_obj_select.c +558 -0
- data/ca_obj_shift.c +952 -0
- data/ca_obj_transpose.c +582 -0
- data/ca_obj_unbound_repeat.c +557 -0
- data/ca_obj_window.c +1023 -0
- data/carray.h +1381 -0
- data/carray_access.c +1798 -0
- data/carray_attribute.c +903 -0
- data/carray_call_cfunc.c +1107 -0
- data/carray_cast.c +1155 -0
- data/carray_cast_func.rb +498 -0
- data/carray_class.c +132 -0
- data/carray_conversion.c +518 -0
- data/carray_copy.c +453 -0
- data/carray_core.c +1307 -0
- data/carray_element.c +572 -0
- data/carray_generate.c +681 -0
- data/carray_iterator.c +630 -0
- data/carray_loop.c +462 -0
- data/carray_mask.c +1174 -0
- data/carray_math.rb +834 -0
- data/carray_numeric.c +257 -0
- data/carray_operator.c +582 -0
- data/carray_order.c +1040 -0
- data/carray_random.c +529 -0
- data/carray_sort_addr.c +261 -0
- data/carray_stat.c +2102 -0
- data/carray_stat_proc.rb +1990 -0
- data/carray_test.c +602 -0
- data/carray_undef.c +69 -0
- data/carray_utils.c +740 -0
- data/ext/calculus/carray_calculus.c +792 -0
- data/ext/calculus/carray_interp.c +355 -0
- data/ext/calculus/extconf.rb +12 -0
- data/ext/calculus/lib/autoload/autoload_math_calculus.rb +2 -0
- data/ext/calculus/lib/math/calculus.rb +119 -0
- data/ext/calculus/lib/math/interp/adapter_interp1d.rb +31 -0
- data/ext/dataframe/API.txt +11 -0
- data/ext/dataframe/extconf.rb +3 -0
- data/ext/dataframe/lib/carray/autoload/autoload_dataframe_dataframe.rb +14 -0
- data/ext/dataframe/lib/carray/dataframe/dataframe.rb +1104 -0
- data/ext/dataframe/sample/test_uniq_sort.rb +5 -0
- data/ext/fortio/extconf.rb +3 -0
- data/ext/fortio/lib/carray/autoload/autoload_fortran_format.rb +5 -0
- data/ext/fortio/lib/carray/io/fortran_format.rb +43 -0
- data/ext/fortio/lib/fortio.rb +3 -0
- data/ext/fortio/lib/fortio/fortran_format.rb +603 -0
- data/ext/fortio/lib/fortio/fortran_format.tab.rb +536 -0
- data/ext/fortio/lib/fortio/fortran_format.y +215 -0
- data/ext/fortio/lib/fortio/fortran_namelist.rb +151 -0
- data/ext/fortio/lib/fortio/fortran_namelist.tab.rb +470 -0
- data/ext/fortio/lib/fortio/fortran_namelist.y +213 -0
- data/ext/fortio/lib/fortio/fortran_sequential.rb +345 -0
- data/ext/fortio/ruby_fortio.c +182 -0
- data/ext/fortio/test/test_H.rb +5 -0
- data/ext/fortio/test/test_T.rb +7 -0
- data/ext/fortio/test/test_fortran_format.rb +86 -0
- data/ext/fortio/test/test_namelist.rb +25 -0
- data/ext/fortio/test/test_sequential.rb +13 -0
- data/ext/fortio/test/test_sequential2.rb +13 -0
- data/ext/fortio/work/test.rb +10 -0
- data/ext/fortio/work/test_e.rb +19 -0
- data/ext/fortio/work/test_ep.rb +10 -0
- data/ext/fortio/work/test_parse.rb +12 -0
- data/ext/imagemap/carray_imagemap.c +495 -0
- data/ext/imagemap/doc/call_graph.dot +28 -0
- data/ext/imagemap/draw.c +567 -0
- data/ext/imagemap/extconf.rb +13 -0
- data/ext/imagemap/lib/autoload/autoload_graphics_imagemap.rb +1 -0
- data/ext/imagemap/lib/graphics/imagemap.rb +273 -0
- data/ext/imagemap/lib/image_map.rb +4 -0
- data/ext/imagemap/test/swath_index.rb +83 -0
- data/ext/imagemap/test/swath_warp.rb +99 -0
- data/ext/imagemap/test/test.rb +23 -0
- data/ext/imagemap/test/test_image.rb +42 -0
- data/ext/imagemap/test/test_line.rb +14 -0
- data/ext/imagemap/test/test_rotate.rb +17 -0
- data/ext/imagemap/test/test_triangle.rb +20 -0
- data/ext/imagemap/test/test_warp.rb +26 -0
- data/ext/mathfunc/carray_mathfunc.c +321 -0
- data/ext/mathfunc/extconf.rb +18 -0
- data/ext/mathfunc/lib/autoload/autoload_math_mathfunc.rb +1 -0
- data/ext/mathfunc/lib/math/mathfunc.rb +15 -0
- data/ext/mathfunc/test/test_hypot.rb +5 -0
- data/ext/mathfunc/test/test_j0.rb +22 -0
- data/ext/mathfunc/test/test_jn.rb +8 -0
- data/ext/mathfunc/test/test_sph.rb +9 -0
- data/ext/narray/README +22 -0
- data/ext/narray/ca_wrap_narray.c +491 -0
- data/ext/narray/carray_narray.c +21 -0
- data/ext/narray/extconf.rb +57 -0
- data/ext/narray/lib/autoload/autoload_math_narray.rb +1 -0
- data/ext/narray/lib/autoload/autoload_math_narray_miss.rb +11 -0
- data/ext/narray/lib/math/narray.rb +17 -0
- data/ext/narray/lib/math/narray_miss.rb +45 -0
- data/extconf.rb +3 -25
- data/lib/carray.rb +28 -0
- data/lib/carray/autoload/autoload_base.rb +23 -0
- data/lib/carray/autoload/autoload_graphics_gnuplot.rb +2 -0
- data/lib/carray/autoload/autoload_io_csv.rb +14 -0
- data/lib/carray/autoload/autoload_io_excel.rb +5 -0
- data/lib/carray/autoload/autoload_io_imagemagick.rb +6 -0
- data/lib/carray/autoload/autoload_io_pg.rb +6 -0
- data/lib/carray/autoload/autoload_io_sqlite3.rb +12 -0
- data/lib/carray/autoload/autoload_io_table.rb +1 -0
- data/lib/carray/autoload/autoload_math_histogram.rb +5 -0
- data/lib/carray/autoload/autoload_math_interp.rb +4 -0
- data/lib/carray/autoload/autoload_math_recurrence.rb +6 -0
- data/lib/carray/autoload/autoload_object_iterator.rb +1 -0
- data/lib/carray/autoload/autoload_object_link.rb +1 -0
- data/lib/carray/autoload/autoload_object_pack.rb +2 -0
- data/lib/carray/base/autoload.rb +94 -0
- data/lib/carray/base/basic.rb +1051 -0
- data/lib/carray/base/inspect.rb +252 -0
- data/lib/carray/base/iterator.rb +367 -0
- data/lib/carray/base/math.rb +403 -0
- data/lib/carray/base/obsolete.rb +93 -0
- data/lib/carray/base/serialize.rb +260 -0
- data/lib/carray/base/struct.rb +634 -0
- data/lib/carray/graphics/gnuplot.rb +2116 -0
- data/lib/carray/info.rb +112 -0
- data/lib/carray/io/csv.rb +560 -0
- data/lib/carray/io/excel.rb +26 -0
- data/lib/carray/io/imagemagick.rb +231 -0
- data/lib/carray/io/pg.rb +101 -0
- data/lib/carray/io/sqlite3.rb +202 -0
- data/lib/carray/io/table.rb +77 -0
- data/lib/carray/math/histogram.rb +179 -0
- data/lib/carray/math/interp.rb +57 -0
- data/lib/carray/math/interp/adapter_gsl_spline.rb +47 -0
- data/lib/carray/math/recurrence.rb +95 -0
- data/lib/carray/mkmf.rb +145 -0
- data/lib/carray/object/ca_obj_iterator.rb +52 -0
- data/lib/carray/object/ca_obj_link.rb +52 -0
- data/lib/carray/object/ca_obj_pack.rb +101 -0
- data/mkmath.rb +731 -0
- data/mt19937ar.c +182 -0
- data/mt19937ar.h +86 -0
- data/rdoc_main.rb +27 -0
- data/rdoc_math.rb +5 -0
- data/rdoc_stat.rb +31 -0
- data/ruby_carray.c +242 -0
- data/ruby_ccomplex.c +497 -0
- data/ruby_float_func.c +83 -0
- data/spec/CABlockIterator/CABlockIterator_spec.rb +113 -0
- data/spec/CArray/bug/store_spec.rb +27 -0
- data/spec/CArray/index/repeat_spec.rb +10 -0
- data/spec/CArray/method/eq_spec.rb +80 -0
- data/spec/CArray/method/is_nan_spec.rb +12 -0
- data/spec/CArray/method/ne_spec.rb +18 -0
- data/spec/CArray/method/round_spec.rb +11 -0
- data/spec/CArray/object/_attribute_spec.rb +32 -0
- data/spec/CArray/object/s_new_spec.rb +31 -0
- data/spec/CArray/serialize/Serialization_spec.rb +89 -0
- data/spec/spec_all.rb +11 -0
- data/test/test_ALL.rb +50 -0
- data/test/test_CABitfield.rb +59 -0
- data/test/test_CABlock.rb +208 -0
- data/test/test_CAField.rb +40 -0
- data/test/test_CAGrid.rb +76 -0
- data/test/test_CAMapping.rb +106 -0
- data/test/test_CAMmap.rb +11 -0
- data/test/test_CARefer.rb +94 -0
- data/test/test_CARepeat.rb +66 -0
- data/test/test_CASelect.rb +23 -0
- data/test/test_CAShift.rb +17 -0
- data/test/test_CATranspose.rb +61 -0
- data/test/test_CAVirtual.rb +214 -0
- data/test/test_CAWindow.rb +55 -0
- data/test/test_CAWrap.rb +9 -0
- data/test/test_CArray.rb +228 -0
- data/test/test_CComplex.rb +83 -0
- data/test/test_CScalar.rb +91 -0
- data/test/test_attribute.rb +281 -0
- data/test/test_block_iterator.rb +17 -0
- data/test/test_boolean.rb +99 -0
- data/test/test_cast.rb +33 -0
- data/test/test_class.rb +85 -0
- data/test/test_complex.rb +43 -0
- data/test/test_composite.rb +125 -0
- data/test/test_convert.rb +79 -0
- data/test/test_copy.rb +141 -0
- data/test/test_creation.rb +85 -0
- data/test/test_element.rb +146 -0
- data/test/test_extream.rb +55 -0
- data/test/test_generate.rb +75 -0
- data/test/test_index.rb +71 -0
- data/test/test_mask.rb +578 -0
- data/test/test_math.rb +98 -0
- data/test/test_narray.rb +64 -0
- data/test/test_order.rb +147 -0
- data/test/test_random.rb +15 -0
- data/test/test_ref_store.rb +211 -0
- data/test/test_stat.rb +414 -0
- data/test/test_struct.rb +72 -0
- data/test/test_virtual.rb +49 -0
- data/utils/ca_ase.rb +21 -0
- data/utils/ca_methods.rb +15 -0
- data/utils/cast_checker.rb +30 -0
- data/utils/create_rdoc.sh +9 -0
- data/utils/diff_method.rb +52 -0
- data/utils/extract_rdoc.rb +27 -0
- data/utils/make_tgz.sh +3 -0
- data/utils/remove_resource_fork.sh +5 -0
- data/version.h +3 -3
- metadata +266 -1
data/carray_numeric.c
ADDED
@@ -0,0 +1,257 @@
|
|
1
|
+
/* ---------------------------------------------------------------------------
|
2
|
+
|
3
|
+
carray_numeric.c
|
4
|
+
|
5
|
+
This file is part of Ruby/CArray extension library.
|
6
|
+
You can redistribute it and/or modify it under the terms of
|
7
|
+
the Ruby Licence.
|
8
|
+
|
9
|
+
Copyright (C) 2005 Hiroki Motoyoshi
|
10
|
+
|
11
|
+
---------------------------------------------------------------------------- */
|
12
|
+
|
13
|
+
#include "ruby.h"
|
14
|
+
#include "carray.h"
|
15
|
+
|
16
|
+
VALUE CA_NAN, CA_INF;
|
17
|
+
|
18
|
+
static ID id___or__;
|
19
|
+
static ID id___and__;
|
20
|
+
static ID id___xor__;
|
21
|
+
static ID id___rshift__;
|
22
|
+
static ID id___lshift__;
|
23
|
+
|
24
|
+
VALUE
|
25
|
+
rb_num_nan (VALUE self)
|
26
|
+
{
|
27
|
+
return CA_NAN;
|
28
|
+
}
|
29
|
+
|
30
|
+
VALUE
|
31
|
+
rb_num_inf (VALUE self)
|
32
|
+
{
|
33
|
+
return CA_INF;
|
34
|
+
}
|
35
|
+
|
36
|
+
static VALUE
|
37
|
+
rb_hack_or(VALUE x, VALUE y)
|
38
|
+
{
|
39
|
+
if ( rb_obj_is_carray(y) ) {
|
40
|
+
if ( rb_ca_is_boolean_type(y) ) {
|
41
|
+
return rb_funcall(y, rb_intern("bit_or"), 1, x);
|
42
|
+
}
|
43
|
+
else {
|
44
|
+
#if RUBY_VERSION_CODE >= 190
|
45
|
+
return rb_num_coerce_bin(x, y, '|');
|
46
|
+
#else
|
47
|
+
return rb_num_coerce_bin(x, y);
|
48
|
+
#endif
|
49
|
+
}
|
50
|
+
}
|
51
|
+
else {
|
52
|
+
return rb_funcall(x, id___or__, 1, y);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
static VALUE
|
57
|
+
rb_hack_and (VALUE x, VALUE y)
|
58
|
+
{
|
59
|
+
if ( rb_obj_is_carray(y) ) {
|
60
|
+
if ( rb_ca_is_boolean_type(y) ) {
|
61
|
+
return rb_funcall(y, rb_intern("bit_and"), 1, x);
|
62
|
+
}
|
63
|
+
else {
|
64
|
+
#if RUBY_VERSION_CODE >= 190
|
65
|
+
return rb_num_coerce_bin(x, y, '&');
|
66
|
+
#else
|
67
|
+
return rb_num_coerce_bin(x, y);
|
68
|
+
#endif
|
69
|
+
}
|
70
|
+
}
|
71
|
+
else {
|
72
|
+
return rb_funcall(x, id___and__, 1, y);
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
static VALUE
|
77
|
+
rb_hack_xor (VALUE x, VALUE y)
|
78
|
+
{
|
79
|
+
if ( rb_obj_is_carray(y) ) {
|
80
|
+
if ( rb_ca_is_boolean_type(y) ) {
|
81
|
+
return rb_funcall(y, rb_intern("bit_xor"), 1, x);
|
82
|
+
}
|
83
|
+
else {
|
84
|
+
#if RUBY_VERSION_CODE >= 190
|
85
|
+
return rb_num_coerce_bin(x, y, '^');
|
86
|
+
#else
|
87
|
+
return rb_num_coerce_bin(x, y);
|
88
|
+
#endif
|
89
|
+
}
|
90
|
+
}
|
91
|
+
else {
|
92
|
+
return rb_funcall(x, id___xor__, 1, y);
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
static VALUE
|
97
|
+
rb_hack_lshift (VALUE x, VALUE y)
|
98
|
+
{
|
99
|
+
if ( rb_obj_is_carray(y) ) {
|
100
|
+
#if RUBY_VERSION_CODE >= 190
|
101
|
+
return rb_num_coerce_bin(x, y, rb_intern("<<"));
|
102
|
+
#else
|
103
|
+
return rb_num_coerce_bin(x, y);
|
104
|
+
#endif
|
105
|
+
}
|
106
|
+
else {
|
107
|
+
return rb_funcall(x, id___lshift__, 1, y);
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
static VALUE
|
112
|
+
rb_hack_rshift (VALUE x, VALUE y)
|
113
|
+
{
|
114
|
+
if ( rb_obj_is_carray(y) ) {
|
115
|
+
#if RUBY_VERSION_CODE >= 190
|
116
|
+
return rb_num_coerce_bin(x, y, rb_intern(">>"));
|
117
|
+
#else
|
118
|
+
return rb_num_coerce_bin(x, y);
|
119
|
+
#endif
|
120
|
+
}
|
121
|
+
else {
|
122
|
+
return rb_funcall(x, id___rshift__, 1, y);
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
static VALUE
|
127
|
+
rb_hack_star (VALUE x, VALUE y)
|
128
|
+
{
|
129
|
+
return rb_funcall(y, rb_intern("*"), 1, x);
|
130
|
+
}
|
131
|
+
|
132
|
+
/* ------------------------------------------------------------------- */
|
133
|
+
|
134
|
+
#ifdef HAVE_COMPLEX_H
|
135
|
+
|
136
|
+
#include <math.h>
|
137
|
+
#include <float.h>
|
138
|
+
|
139
|
+
#define proc_arg_cmplx(type) \
|
140
|
+
{ \
|
141
|
+
type *p = (type *) ca->ptr; \
|
142
|
+
double *q = (double *) co->ptr; \
|
143
|
+
boolean8_t *m = (ca->mask) ? (boolean8_t *) ca->mask->ptr : NULL; \
|
144
|
+
int32_t i; \
|
145
|
+
if ( m ) { \
|
146
|
+
for (i=ca->elements; i; i--, p++, q++) { \
|
147
|
+
if ( ! *m++ ) \
|
148
|
+
*q = carg(*p); \
|
149
|
+
} \
|
150
|
+
} \
|
151
|
+
else { \
|
152
|
+
for (i=ca->elements; i; i--, p++, q++) { \
|
153
|
+
*q = carg(*p); \
|
154
|
+
} \
|
155
|
+
} \
|
156
|
+
}
|
157
|
+
|
158
|
+
static VALUE
|
159
|
+
rb_ca_arg (VALUE self)
|
160
|
+
{
|
161
|
+
volatile VALUE obj;
|
162
|
+
CArray *ca, *co;
|
163
|
+
|
164
|
+
Data_Get_Struct(self, CArray, ca);
|
165
|
+
|
166
|
+
co = carray_new(CA_FLOAT64, ca->rank, ca->dim, 0, NULL);
|
167
|
+
obj = ca_wrap_struct(co);
|
168
|
+
|
169
|
+
if ( ca_has_mask(ca) ) {
|
170
|
+
ca_copy_mask_overlay(co, co->elements, 1, ca);
|
171
|
+
}
|
172
|
+
|
173
|
+
ca_attach(ca);
|
174
|
+
|
175
|
+
switch ( ca->data_type ) {
|
176
|
+
case CA_FLOAT32: proc_arg_cmplx(float32_t); break;
|
177
|
+
case CA_FLOAT64: proc_arg_cmplx(float64_t); break;
|
178
|
+
case CA_FLOAT128: proc_arg_cmplx(float128_t); break;
|
179
|
+
case CA_CMPLX64: proc_arg_cmplx(cmplx64_t); break;
|
180
|
+
case CA_CMPLX128: proc_arg_cmplx(cmplx128_t); break;
|
181
|
+
case CA_CMPLX256: proc_arg_cmplx(cmplx256_t); break;
|
182
|
+
default: rb_raise(rb_eRuntimeError, "invalid data type");
|
183
|
+
}
|
184
|
+
|
185
|
+
ca_detach(ca);
|
186
|
+
|
187
|
+
return obj;
|
188
|
+
}
|
189
|
+
|
190
|
+
#endif
|
191
|
+
|
192
|
+
void
|
193
|
+
Init_carray_numeric ()
|
194
|
+
{
|
195
|
+
/* hack Fixnum and Bignum's "|", "&", "^", "<<", ">>" */
|
196
|
+
|
197
|
+
id___or__ = rb_intern("__or__");
|
198
|
+
id___and__ = rb_intern("__and__");
|
199
|
+
id___xor__ = rb_intern("__xor__");
|
200
|
+
id___rshift__ = rb_intern("__rshift__");
|
201
|
+
id___lshift__ = rb_intern("__lshift__");
|
202
|
+
|
203
|
+
CA_NAN = rb_float_new(0.0/0.0);
|
204
|
+
CA_INF = rb_float_new(1.0/0.0);
|
205
|
+
rb_define_const(rb_cObject, "CA_NAN", CA_NAN);
|
206
|
+
rb_define_const(rb_cObject, "CA_INF", CA_INF);
|
207
|
+
rb_define_global_function("nan", rb_num_nan, 0);
|
208
|
+
rb_define_global_function("inf", rb_num_inf, 0);
|
209
|
+
|
210
|
+
rb_define_alias(rb_cTrueClass, "__or__", "|");
|
211
|
+
rb_define_alias(rb_cTrueClass, "__and__", "&");
|
212
|
+
rb_define_alias(rb_cTrueClass, "__xor__", "^");
|
213
|
+
|
214
|
+
rb_define_alias(rb_cFalseClass, "__or__", "|");
|
215
|
+
rb_define_alias(rb_cFalseClass, "__and__", "&");
|
216
|
+
rb_define_alias(rb_cFalseClass, "__xor__", "^");
|
217
|
+
|
218
|
+
rb_define_alias(rb_cFixnum, "__or__", "|");
|
219
|
+
rb_define_alias(rb_cFixnum, "__and__", "&");
|
220
|
+
rb_define_alias(rb_cFixnum, "__xor__", "^");
|
221
|
+
rb_define_alias(rb_cFixnum, "__lshift__", "<<");
|
222
|
+
rb_define_alias(rb_cFixnum, "__rshift__", ">>");
|
223
|
+
|
224
|
+
rb_define_alias(rb_cBignum, "__or__", "|");
|
225
|
+
rb_define_alias(rb_cBignum, "__and__", "&");
|
226
|
+
rb_define_alias(rb_cBignum, "__xor__", "^");
|
227
|
+
rb_define_alias(rb_cBignum, "__lshift__", "<<");
|
228
|
+
rb_define_alias(rb_cBignum, "__rshift__", ">>");
|
229
|
+
|
230
|
+
rb_define_method(rb_cTrueClass, "|", rb_hack_or, 1);
|
231
|
+
rb_define_method(rb_cTrueClass, "&", rb_hack_and, 1);
|
232
|
+
rb_define_method(rb_cTrueClass, "^", rb_hack_xor, 1);
|
233
|
+
rb_define_method(rb_cTrueClass, "*", rb_hack_star, 1);
|
234
|
+
|
235
|
+
rb_define_method(rb_cFalseClass, "|", rb_hack_or, 1);
|
236
|
+
rb_define_method(rb_cFalseClass, "&", rb_hack_and, 1);
|
237
|
+
rb_define_method(rb_cFalseClass, "^", rb_hack_xor, 1);
|
238
|
+
rb_define_method(rb_cFalseClass, "*", rb_hack_star, 1);
|
239
|
+
|
240
|
+
rb_define_method(rb_cFixnum, "|", rb_hack_or, 1);
|
241
|
+
rb_define_method(rb_cFixnum, "&", rb_hack_and, 1);
|
242
|
+
rb_define_method(rb_cFixnum, "^", rb_hack_xor, 1);
|
243
|
+
rb_define_method(rb_cFixnum, "<<", rb_hack_lshift, 1);
|
244
|
+
rb_define_method(rb_cFixnum, ">>", rb_hack_rshift, 1);
|
245
|
+
|
246
|
+
rb_define_method(rb_cBignum, "|", rb_hack_or, 1);
|
247
|
+
rb_define_method(rb_cBignum, "&", rb_hack_and, 1);
|
248
|
+
rb_define_method(rb_cBignum, "^", rb_hack_xor, 1);
|
249
|
+
rb_define_method(rb_cBignum, "<<", rb_hack_lshift, 1);
|
250
|
+
rb_define_method(rb_cBignum, ">>", rb_hack_rshift, 1);
|
251
|
+
|
252
|
+
#ifdef HAVE_COMPLEX_H
|
253
|
+
rb_define_method(rb_cCArray, "arg", rb_ca_arg, 0);
|
254
|
+
#endif
|
255
|
+
|
256
|
+
}
|
257
|
+
|
data/carray_operator.c
ADDED
@@ -0,0 +1,582 @@
|
|
1
|
+
/* ---------------------------------------------------------------------------
|
2
|
+
|
3
|
+
carray_operator.c
|
4
|
+
|
5
|
+
This file is part of Ruby/CArray extension library.
|
6
|
+
You can redistribute it and/or modify it under the terms of
|
7
|
+
the Ruby Licence.
|
8
|
+
|
9
|
+
Copyright (C) 2005 Hiroki Motoyoshi
|
10
|
+
|
11
|
+
---------------------------------------------------------------------------- */
|
12
|
+
|
13
|
+
#include <math.h>
|
14
|
+
|
15
|
+
#include "carray.h"
|
16
|
+
|
17
|
+
VALUE rb_mCAMath;
|
18
|
+
|
19
|
+
extern ca_binop_func_t ca_binop_mul[CA_NTYPE];
|
20
|
+
extern ca_binop_func_t ca_binop_add[CA_NTYPE];
|
21
|
+
|
22
|
+
void
|
23
|
+
ca_zerodiv ()
|
24
|
+
{
|
25
|
+
#ifdef _OPENMP
|
26
|
+
#pragma omp master
|
27
|
+
#endif
|
28
|
+
rb_raise(rb_eZeroDivError, "divided by 0");
|
29
|
+
}
|
30
|
+
|
31
|
+
|
32
|
+
VALUE
|
33
|
+
rb_ca_call_monop (VALUE self, ca_monop_func_t func[])
|
34
|
+
{
|
35
|
+
volatile VALUE out;
|
36
|
+
CArray *ca1, *ca2; /* ca2 = ca1.op */
|
37
|
+
|
38
|
+
Data_Get_Struct(self, CArray, ca1);
|
39
|
+
|
40
|
+
if ( ca_has_mask(ca1) ) {
|
41
|
+
ca2 = ca_template_safe(ca1);
|
42
|
+
}
|
43
|
+
else {
|
44
|
+
ca2 = ca_template(ca1);
|
45
|
+
}
|
46
|
+
|
47
|
+
ca2 = ca_template(ca1);
|
48
|
+
out = ca_wrap_struct(ca2);
|
49
|
+
|
50
|
+
ca_attach(ca1);
|
51
|
+
ca_copy_mask_overlay(ca2, ca2->elements, 1, ca1);
|
52
|
+
func[ca1->data_type](ca1->elements,
|
53
|
+
( ca2->mask ) ? ca2->mask->ptr : NULL,
|
54
|
+
ca1->ptr, 1,
|
55
|
+
ca2->ptr, 1);
|
56
|
+
ca_detach(ca1);
|
57
|
+
|
58
|
+
/* unresolved unbound repeat array generates unbound repeat array again */
|
59
|
+
if ( ca1->obj_type == CA_OBJ_UNBOUND_REPEAT ) {
|
60
|
+
CAUnboundRepeat *cx = (CAUnboundRepeat *) ca1;
|
61
|
+
out = rb_ca_ubrep_new(out, cx->rep_rank, cx->rep_dim);
|
62
|
+
}
|
63
|
+
|
64
|
+
return out;
|
65
|
+
}
|
66
|
+
|
67
|
+
VALUE
|
68
|
+
rb_ca_call_monop_bang (VALUE self, ca_monop_func_t func[])
|
69
|
+
{
|
70
|
+
CArray *ca1; /* ca1.op! */
|
71
|
+
|
72
|
+
rb_ca_modify(self);
|
73
|
+
|
74
|
+
Data_Get_Struct(self, CArray, ca1);
|
75
|
+
|
76
|
+
ca_attach(ca1);
|
77
|
+
func[ca1->data_type](ca1->elements,
|
78
|
+
( ca1->mask ) ? ca1->mask->ptr : NULL,
|
79
|
+
ca1->ptr, 1,
|
80
|
+
ca1->ptr, 1);
|
81
|
+
ca_sync(ca1);
|
82
|
+
ca_detach(ca1);
|
83
|
+
|
84
|
+
return self;
|
85
|
+
}
|
86
|
+
|
87
|
+
VALUE
|
88
|
+
rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
|
89
|
+
ca_binop_func_t func[])
|
90
|
+
{
|
91
|
+
volatile VALUE out;
|
92
|
+
CArray *ca1, *ca2, *ca3; /* ca3 = ca1.op(ca2) */
|
93
|
+
|
94
|
+
/* do implicit casting and resolving unbound repeat array */
|
95
|
+
rb_ca_cast_self_or_other(&self, &other);
|
96
|
+
|
97
|
+
Data_Get_Struct(self, CArray, ca1);
|
98
|
+
Data_Get_Struct(other, CArray, ca2);
|
99
|
+
|
100
|
+
ca_attach_n(2, ca1, ca2);
|
101
|
+
|
102
|
+
/* main operation */
|
103
|
+
if ( rb_obj_is_cscalar(self) ) {
|
104
|
+
if ( rb_obj_is_cscalar(other) ) { /* scalar vs scalar */
|
105
|
+
if ( ca_has_mask(ca1) || ca_has_mask(ca2) ) {
|
106
|
+
ca3 = ca_template_safe(ca1);
|
107
|
+
}
|
108
|
+
else {
|
109
|
+
ca3 = ca_template(ca1);
|
110
|
+
}
|
111
|
+
out = ca_wrap_struct(ca3);
|
112
|
+
|
113
|
+
ca_copy_mask_overlay(ca3, ca3->elements, 2, ca1, ca2);
|
114
|
+
func[ca1->data_type](ca1->elements, ( ca3->mask ) ? ca3->mask->ptr : NULL,
|
115
|
+
ca1->ptr, 0,
|
116
|
+
ca2->ptr, 0,
|
117
|
+
ca3->ptr, 0);
|
118
|
+
}
|
119
|
+
else { /* scalar vs array */
|
120
|
+
if ( ca_has_mask(ca1) || ca_has_mask(ca2) ) {
|
121
|
+
ca3 = ca_template_safe(ca2);
|
122
|
+
}
|
123
|
+
else {
|
124
|
+
ca3 = ca_template(ca2);
|
125
|
+
}
|
126
|
+
out = ca_wrap_struct(ca3);
|
127
|
+
|
128
|
+
ca_copy_mask_overlay(ca3, ca3->elements, 2, ca1, ca2);
|
129
|
+
func[ca1->data_type](ca2->elements, ( ca3->mask ) ? ca3->mask->ptr : NULL,
|
130
|
+
ca1->ptr, 0,
|
131
|
+
ca2->ptr, 1,
|
132
|
+
ca3->ptr, 1);
|
133
|
+
}
|
134
|
+
}
|
135
|
+
else { /* array vs scalar */
|
136
|
+
if ( rb_obj_is_cscalar(other) ) {
|
137
|
+
if ( ca_has_mask(ca1) || ca_has_mask(ca2) ) {
|
138
|
+
ca3 = ca_template_safe(ca1);
|
139
|
+
}
|
140
|
+
else {
|
141
|
+
ca3 = ca_template(ca1);
|
142
|
+
}
|
143
|
+
out = ca_wrap_struct(ca3);
|
144
|
+
|
145
|
+
ca_copy_mask_overlay(ca3, ca3->elements, 2, ca1, ca2);
|
146
|
+
func[ca1->data_type](ca1->elements, ( ca3->mask ) ? ca3->mask->ptr : NULL,
|
147
|
+
ca1->ptr, 1,
|
148
|
+
ca2->ptr, 0,
|
149
|
+
ca3->ptr, 1);
|
150
|
+
}
|
151
|
+
else { /* array vs array */
|
152
|
+
if ( ca1->elements != ca2->elements ) {
|
153
|
+
rb_raise(rb_eRuntimeError, "elements mismatch (%i <-> %i)",
|
154
|
+
ca1->elements, ca2->elements);
|
155
|
+
}
|
156
|
+
if ( ca_has_mask(ca1) || ca_has_mask(ca2) ) {
|
157
|
+
ca3 = ca_template_safe(ca1);
|
158
|
+
}
|
159
|
+
else {
|
160
|
+
ca3 = ca_template(ca1);
|
161
|
+
}
|
162
|
+
out = ca_wrap_struct(ca3);
|
163
|
+
|
164
|
+
ca_copy_mask_overlay(ca3, ca3->elements, 2, ca1, ca2);
|
165
|
+
func[ca1->data_type](ca1->elements, ( ca3->mask ) ? ca3->mask->ptr : NULL,
|
166
|
+
ca1->ptr, 1,
|
167
|
+
ca2->ptr, 1,
|
168
|
+
ca3->ptr, 1);
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
ca_detach_n(2, ca1, ca2);
|
173
|
+
|
174
|
+
/* unresolved unbound repeat array generates unbound repeat array again */
|
175
|
+
if ( ca1->obj_type == CA_OBJ_UNBOUND_REPEAT ) {
|
176
|
+
CAUnboundRepeat *cx = (CAUnboundRepeat *) ca1;
|
177
|
+
out = rb_ca_ubrep_new(out, cx->rep_rank, cx->rep_dim);
|
178
|
+
}
|
179
|
+
|
180
|
+
/* unresolved unbound repeat array generates unbound repeat array again */
|
181
|
+
if ( ca2->obj_type == CA_OBJ_UNBOUND_REPEAT ) {
|
182
|
+
CAUnboundRepeat *cx = (CAUnboundRepeat *) ca2;
|
183
|
+
out = rb_ca_ubrep_new(out, cx->rep_rank, cx->rep_dim);
|
184
|
+
}
|
185
|
+
|
186
|
+
return out;
|
187
|
+
}
|
188
|
+
|
189
|
+
VALUE
|
190
|
+
rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[])
|
191
|
+
{
|
192
|
+
CArray *ca1, *ca2; /* ca1.op!(ca2) */
|
193
|
+
|
194
|
+
rb_ca_modify(self);
|
195
|
+
|
196
|
+
/* do implicit casting and resolving unbound repeat array */
|
197
|
+
rb_ca_cast_other(&self, &other);
|
198
|
+
|
199
|
+
Data_Get_Struct(self, CArray, ca1);
|
200
|
+
Data_Get_Struct(other, CArray, ca2);
|
201
|
+
|
202
|
+
ca_attach_n(2, ca1, ca2);
|
203
|
+
|
204
|
+
/* main operation */
|
205
|
+
if ( rb_obj_is_cscalar(self) ) {
|
206
|
+
if ( rb_obj_is_cscalar(other) ) { /* scalar vs scalar */
|
207
|
+
ca_copy_mask_overlay(ca1, ca1->elements, 2, ca1, ca2);
|
208
|
+
func[ca1->data_type](ca1->elements, ( ca1->mask ) ? ca1->mask->ptr : NULL,
|
209
|
+
ca1->ptr, 0,
|
210
|
+
ca2->ptr, 0,
|
211
|
+
ca1->ptr, 0);
|
212
|
+
}
|
213
|
+
else { /* scalar vs array */
|
214
|
+
if ( ca1->elements != ca2->elements ) {
|
215
|
+
rb_raise(rb_eRuntimeError, "elements mismatch (%i <-> %i)",
|
216
|
+
ca1->elements, ca2->elements);
|
217
|
+
}
|
218
|
+
|
219
|
+
ca_copy_mask_overlay(ca1, ca1->elements, 2, ca1, ca2);
|
220
|
+
func[ca1->data_type](ca1->elements, ( ca1->mask ) ? ca1->mask->ptr : NULL,
|
221
|
+
ca1->ptr, 0,
|
222
|
+
ca2->ptr, 0,
|
223
|
+
ca1->ptr, 0);
|
224
|
+
}
|
225
|
+
}
|
226
|
+
else {
|
227
|
+
if ( rb_obj_is_cscalar(other) ) { /* array vs scalar */
|
228
|
+
ca_copy_mask_overlay(ca1, ca1->elements, 2, ca1, ca2);
|
229
|
+
func[ca1->data_type](ca1->elements, ( ca1->mask ) ? ca1->mask->ptr : NULL,
|
230
|
+
ca1->ptr, 1,
|
231
|
+
ca2->ptr, 0,
|
232
|
+
ca1->ptr, 1);
|
233
|
+
}
|
234
|
+
else { /* array vs array */
|
235
|
+
if ( ca1->elements != ca2->elements ) {
|
236
|
+
rb_raise(rb_eRuntimeError, "elements mismatch in binop (%i <-> %i)",
|
237
|
+
ca1->elements, ca2->elements);
|
238
|
+
}
|
239
|
+
|
240
|
+
ca_copy_mask_overlay(ca1, ca1->elements, 2, ca1, ca2);
|
241
|
+
func[ca1->data_type](ca1->elements, ( ca1->mask ) ? ca1->mask->ptr : NULL,
|
242
|
+
ca1->ptr, 1,
|
243
|
+
ca2->ptr, 1,
|
244
|
+
ca1->ptr, 1);
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
ca_sync(ca1);
|
250
|
+
ca_detach_n(2, ca1, ca2);
|
251
|
+
|
252
|
+
return self;
|
253
|
+
}
|
254
|
+
|
255
|
+
VALUE
|
256
|
+
rb_ca_call_moncmp (VALUE self, ca_moncmp_func_t func[])
|
257
|
+
{
|
258
|
+
volatile VALUE out;
|
259
|
+
CArray *ca1, *ca2; /* ca2 = ca1.op */
|
260
|
+
|
261
|
+
Data_Get_Struct(self, CArray, ca1);
|
262
|
+
|
263
|
+
if ( ca_is_scalar(ca1) ) {
|
264
|
+
out = rb_cscalar_new(CA_BOOLEAN, 0, NULL);
|
265
|
+
}
|
266
|
+
else {
|
267
|
+
out = rb_carray_new(CA_BOOLEAN, ca1->rank, ca1->dim, 0, NULL);
|
268
|
+
}
|
269
|
+
|
270
|
+
Data_Get_Struct(out, CArray, ca2);
|
271
|
+
|
272
|
+
ca_attach(ca1);
|
273
|
+
ca_copy_mask_overlay(ca2, ca2->elements, 1, ca1);
|
274
|
+
func[ca1->data_type](ca1->elements, ( ca2->mask ) ? ca2->mask->ptr : NULL,
|
275
|
+
ca1->ptr, 1,
|
276
|
+
ca2->ptr, 1);
|
277
|
+
ca_detach(ca1);
|
278
|
+
|
279
|
+
/* unresolved unbound repeat array generates unbound repeat array again */
|
280
|
+
if ( ca1->obj_type == CA_OBJ_UNBOUND_REPEAT ) {
|
281
|
+
CAUnboundRepeat *cx = (CAUnboundRepeat *) ca1;
|
282
|
+
out = rb_ca_ubrep_new(out, cx->rep_rank, cx->rep_dim);
|
283
|
+
}
|
284
|
+
|
285
|
+
return out;
|
286
|
+
}
|
287
|
+
|
288
|
+
extern ca_monop_func_t ca_bincmp_eq[CA_NTYPE];
|
289
|
+
extern ca_monop_func_t ca_bincmp_ne[CA_NTYPE];
|
290
|
+
|
291
|
+
VALUE
|
292
|
+
rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
|
293
|
+
ca_bincmp_func_t func[])
|
294
|
+
{
|
295
|
+
volatile VALUE out = Qnil;
|
296
|
+
CArray *ca1, *ca2, *ca3; /* ca3 = ca1.op(ca2) */
|
297
|
+
|
298
|
+
/* check for comparison with CA_UNDEF */
|
299
|
+
if ( other == CA_UNDEF ) {
|
300
|
+
if ( func == ca_bincmp_eq ) { /* a.eq(UNDEF) -> a.is_masked */
|
301
|
+
return rb_ca_is_masked(self);
|
302
|
+
}
|
303
|
+
else if ( func == ca_bincmp_ne ) { /* a.ne(UNDEF) -> a.is_not_masked */
|
304
|
+
return rb_ca_is_not_masked(self);
|
305
|
+
}
|
306
|
+
else {
|
307
|
+
rb_raise(rb_eRuntimeError, "array can not be compared with UNDEF");
|
308
|
+
}
|
309
|
+
}
|
310
|
+
|
311
|
+
/* do implicit casting and resolving unbound repeat array */
|
312
|
+
rb_ca_cast_self_or_other(&self, &other);
|
313
|
+
|
314
|
+
Data_Get_Struct(self, CArray, ca1);
|
315
|
+
Data_Get_Struct(other, CArray, ca2);
|
316
|
+
|
317
|
+
ca_attach_n(2, ca1, ca2);
|
318
|
+
|
319
|
+
/* main operation */
|
320
|
+
if ( rb_obj_is_cscalar(self) ) {
|
321
|
+
if ( rb_obj_is_cscalar(other) ) { /* scalar vs scalar */
|
322
|
+
out = rb_cscalar_new(CA_BOOLEAN, 0, NULL);
|
323
|
+
Data_Get_Struct(out, CArray, ca3);
|
324
|
+
|
325
|
+
ca_copy_mask_overlay(ca3, ca3->elements, 2, ca1, ca2);
|
326
|
+
func[ca1->data_type](ca1->elements, ( ca3->mask ) ? ca3->mask->ptr : NULL,
|
327
|
+
ca1->ptr, ca1->bytes, 0,
|
328
|
+
ca2->ptr, ca2->bytes, 0,
|
329
|
+
ca3->ptr, ca3->bytes, 0);
|
330
|
+
}
|
331
|
+
else { /* scalar vs array */
|
332
|
+
out = rb_carray_new(CA_BOOLEAN, ca2->rank, ca2->dim, 0, NULL);
|
333
|
+
Data_Get_Struct(out, CArray, ca3);
|
334
|
+
|
335
|
+
ca_copy_mask_overlay(ca3, ca3->elements, 2, ca1, ca2);
|
336
|
+
func[ca1->data_type](ca2->elements, ( ca3->mask ) ? ca3->mask->ptr : NULL,
|
337
|
+
ca1->ptr, ca1->bytes, 0,
|
338
|
+
ca2->ptr, ca2->bytes, 1,
|
339
|
+
ca3->ptr, ca3->bytes, 1);
|
340
|
+
}
|
341
|
+
}
|
342
|
+
else {
|
343
|
+
if ( rb_obj_is_cscalar(other) ) { /* array vs scalar */
|
344
|
+
out = rb_carray_new(CA_BOOLEAN, ca1->rank, ca1->dim, 0, NULL);
|
345
|
+
Data_Get_Struct(out, CArray, ca3);
|
346
|
+
|
347
|
+
ca_copy_mask_overlay(ca3, ca3->elements, 2, ca1, ca2);
|
348
|
+
func[ca1->data_type](ca1->elements, ( ca3->mask ) ? ca3->mask->ptr : NULL,
|
349
|
+
ca1->ptr, ca1->bytes, 1,
|
350
|
+
ca2->ptr, ca2->bytes, 0,
|
351
|
+
ca3->ptr, ca3->bytes, 1);
|
352
|
+
}
|
353
|
+
else { /* array vs array */
|
354
|
+
if ( ca1->elements != ca2->elements ) {
|
355
|
+
rb_raise(rb_eRuntimeError, "elements mismatch in bincmp (%i <-> %i)",
|
356
|
+
ca1->elements, ca2->elements);
|
357
|
+
}
|
358
|
+
out = rb_carray_new(CA_BOOLEAN, ca1->rank, ca1->dim, 0, NULL);
|
359
|
+
Data_Get_Struct(out, CArray, ca3);
|
360
|
+
|
361
|
+
ca_copy_mask_overlay(ca3, ca3->elements, 2, ca1, ca2);
|
362
|
+
func[ca1->data_type](ca1->elements, ( ca3->mask ) ? ca3->mask->ptr : NULL,
|
363
|
+
ca1->ptr, ca1->bytes, 1,
|
364
|
+
ca2->ptr, ca2->bytes, 1,
|
365
|
+
ca3->ptr, ca3->bytes, 1);
|
366
|
+
}
|
367
|
+
}
|
368
|
+
|
369
|
+
ca_detach_n(2, ca1, ca2);
|
370
|
+
|
371
|
+
/* unresolved unbound repeat array generates unbound repeat array again */
|
372
|
+
if ( ca1->obj_type == CA_OBJ_UNBOUND_REPEAT ) {
|
373
|
+
CAUnboundRepeat *cx = (CAUnboundRepeat *) ca1;
|
374
|
+
out = rb_ca_ubrep_new(out, cx->rep_rank, cx->rep_dim);
|
375
|
+
}
|
376
|
+
|
377
|
+
return out;
|
378
|
+
}
|
379
|
+
|
380
|
+
void
|
381
|
+
ca_monop_not_implement(int32_t n, char *ptr1, char *ptr2)
|
382
|
+
{
|
383
|
+
rb_raise(rb_eCADataTypeError,
|
384
|
+
"invalid data type for monop (not implemented)");
|
385
|
+
}
|
386
|
+
|
387
|
+
void
|
388
|
+
ca_binop_not_implement(int32_t n, char *ptr1, char *ptr2, char *ptr3)
|
389
|
+
{
|
390
|
+
rb_raise(rb_eCADataTypeError,
|
391
|
+
"invalid data_type for binop (not implemented)");
|
392
|
+
}
|
393
|
+
|
394
|
+
void
|
395
|
+
ca_moncmp_not_implement(int32_t n, char *ptr1, char *ptr2)
|
396
|
+
{
|
397
|
+
rb_raise(rb_eCADataTypeError,
|
398
|
+
"invalid data_type for moncmp (not implemented)");
|
399
|
+
}
|
400
|
+
|
401
|
+
void
|
402
|
+
ca_bincmp_not_implement (int32_t n, char *ptr1, char *ptr2, char *ptr3)
|
403
|
+
{
|
404
|
+
rb_raise(rb_eTypeError, "invalid data_type for bincmp (not implemented)");
|
405
|
+
}
|
406
|
+
|
407
|
+
VALUE
|
408
|
+
ca_math_call (VALUE mod, VALUE arg, ID id)
|
409
|
+
{
|
410
|
+
if ( rb_obj_is_carray(arg) ) {
|
411
|
+
return rb_funcall(arg, id, 0);
|
412
|
+
}
|
413
|
+
#ifdef HAVE_COMPLEX_H
|
414
|
+
else if ( rb_obj_is_kind_of(arg, rb_cCComplex) ) {
|
415
|
+
if ( rb_respond_to(arg, id) ) {
|
416
|
+
return rb_funcall(arg, id, 0);
|
417
|
+
}
|
418
|
+
else if ( rb_respond_to(rb_mMath, id) ) {
|
419
|
+
return rb_funcall(rb_mMath, id, 1, arg);
|
420
|
+
}
|
421
|
+
else {
|
422
|
+
rb_raise(rb_eRuntimeError, "unknown method for Math");
|
423
|
+
}
|
424
|
+
}
|
425
|
+
#endif
|
426
|
+
else {
|
427
|
+
/* rb_funcall calls even for Math's private method -> infinite loop */
|
428
|
+
if ( rb_respond_to(rb_mMath, id) ) {
|
429
|
+
return rb_funcall(rb_mMath, id, 1, arg);
|
430
|
+
}
|
431
|
+
else {
|
432
|
+
rb_raise(rb_eRuntimeError, "unknown method for Math");
|
433
|
+
}
|
434
|
+
}
|
435
|
+
}
|
436
|
+
|
437
|
+
/* rdoc:
|
438
|
+
class CArray
|
439
|
+
def coerce (other)
|
440
|
+
end
|
441
|
+
end
|
442
|
+
*/
|
443
|
+
|
444
|
+
static VALUE
|
445
|
+
rb_ca_coerce (VALUE self, VALUE other)
|
446
|
+
{
|
447
|
+
CArray *ca;
|
448
|
+
Data_Get_Struct(self, CArray, ca);
|
449
|
+
|
450
|
+
if ( rb_obj_is_carray(other) ) {
|
451
|
+
return Qnil;
|
452
|
+
}
|
453
|
+
else if ( rb_respond_to(other, rb_intern("ca")) ) {
|
454
|
+
return rb_ca_coerce(self, rb_funcall(other,rb_intern("ca"),0));
|
455
|
+
}
|
456
|
+
else if ( rb_respond_to(other, rb_intern("to_ca")) ) {
|
457
|
+
return rb_ca_coerce(self, rb_funcall(other,rb_intern("to_ca"),0));
|
458
|
+
}
|
459
|
+
else {
|
460
|
+
/* do implicit casting and resolving unbound repeat array */
|
461
|
+
rb_ca_cast_self_or_other(&self, &other);
|
462
|
+
return rb_assoc_new(other, self);
|
463
|
+
}
|
464
|
+
}
|
465
|
+
|
466
|
+
|
467
|
+
/* CArray#mul_add(other, min_count, fill) */
|
468
|
+
|
469
|
+
#define proc_mul_add(type, conv, to) \
|
470
|
+
{ \
|
471
|
+
type *p1 = (type*)ca->ptr; \
|
472
|
+
type *p2; \
|
473
|
+
int s2; \
|
474
|
+
boolean8_t *m = mi; \
|
475
|
+
type sum = 0; \
|
476
|
+
int32_t count = 0; \
|
477
|
+
int32_t i; \
|
478
|
+
ca_set_iterator(1, cw, &p2, &s2); \
|
479
|
+
if ( m ) { \
|
480
|
+
count = 0; \
|
481
|
+
for (i=ca->elements; i; i--, p1++, p2+=s2) { \
|
482
|
+
if ( ! *m++ ) { \
|
483
|
+
sum += (type)conv(*p1) * (type)conv(*p2); \
|
484
|
+
} \
|
485
|
+
else { \
|
486
|
+
count++; \
|
487
|
+
} \
|
488
|
+
} \
|
489
|
+
} \
|
490
|
+
else { \
|
491
|
+
for (i=ca->elements; i; i--, p1++, p2+=s2) { \
|
492
|
+
sum += (type)conv(*p1) * (type)conv(*p2); \
|
493
|
+
} \
|
494
|
+
} \
|
495
|
+
if ( ( ! NIL_P(rmin_count) ) && count > min_count ) { \
|
496
|
+
out = ( NIL_P(rfval) ) ? CA_UNDEF : rfval;\
|
497
|
+
} \
|
498
|
+
else { \
|
499
|
+
out = to(sum); \
|
500
|
+
} \
|
501
|
+
}
|
502
|
+
|
503
|
+
static VALUE
|
504
|
+
rb_ca_mul_add (int argc, VALUE *argv, volatile VALUE self)
|
505
|
+
{
|
506
|
+
volatile VALUE out;
|
507
|
+
volatile VALUE weight = Qnil;
|
508
|
+
volatile VALUE rmin_count = Qnil;
|
509
|
+
volatile VALUE rfval = Qnil;
|
510
|
+
CArray *ca, *cw;
|
511
|
+
boolean8_t *mi = NULL;
|
512
|
+
int min_count;
|
513
|
+
|
514
|
+
/* FIXME: to parse :mask_limit, :fill_value */
|
515
|
+
rb_scan_args(argc, argv, "12", &weight, &rmin_count, &rfval);
|
516
|
+
|
517
|
+
/* do implicit casting and resolving unbound repeat array */
|
518
|
+
rb_ca_cast_self_or_other(&self, &weight);
|
519
|
+
|
520
|
+
Data_Get_Struct(self, CArray, ca);
|
521
|
+
Data_Get_Struct(weight, CArray, cw);
|
522
|
+
|
523
|
+
/* checking elements and data_type */
|
524
|
+
ca_check_same_elements(ca, cw);
|
525
|
+
ca_check_same_data_type(ca, cw);
|
526
|
+
|
527
|
+
if ( ca->elements == 0 ) {
|
528
|
+
return ( NIL_P(rfval) ) ? CA_UNDEF : rfval;
|
529
|
+
}
|
530
|
+
|
531
|
+
if ( ca_has_mask(ca) || ca_has_mask(cw) ) {
|
532
|
+
mi = ca_allocate_mask_iterator(2, ca, cw);
|
533
|
+
}
|
534
|
+
|
535
|
+
min_count = ( NIL_P(rmin_count) || ( ! mi ) ) ?
|
536
|
+
ca->elements - 1 : NUM2INT(rmin_count);
|
537
|
+
|
538
|
+
if ( min_count < 0 ) {
|
539
|
+
min_count += ca->elements;
|
540
|
+
}
|
541
|
+
|
542
|
+
ca_attach_n(2, ca, cw);
|
543
|
+
|
544
|
+
switch ( ca->data_type ) {
|
545
|
+
case CA_INT8: proc_mul_add(int8_t, ,LONG2NUM); break;
|
546
|
+
case CA_UINT8: proc_mul_add(uint8_t,,ULONG2NUM); break;
|
547
|
+
case CA_INT16: proc_mul_add(int16_t,,LONG2NUM); break;
|
548
|
+
case CA_UINT16: proc_mul_add(uint16_t,,ULONG2NUM); break;
|
549
|
+
case CA_INT32: proc_mul_add(int32_t,,LONG2NUM); break;
|
550
|
+
case CA_UINT32: proc_mul_add(uint32_t,,ULONG2NUM); break;
|
551
|
+
case CA_INT64: proc_mul_add(int64_t,,LL2NUM); break;
|
552
|
+
case CA_UINT64: proc_mul_add(uint64_t,,ULL2NUM); break;
|
553
|
+
case CA_FLOAT32: proc_mul_add(float32_t,,rb_float_new); break;
|
554
|
+
case CA_FLOAT64: proc_mul_add(float64_t,,rb_float_new); break;
|
555
|
+
case CA_FLOAT128: proc_mul_add(float128_t,,rb_float_new); break;
|
556
|
+
#ifdef HAVE_COMPLEX_H
|
557
|
+
case CA_CMPLX64: proc_mul_add(cmplx64_t,,rb_ccomplex_new); break;
|
558
|
+
case CA_CMPLX128: proc_mul_add(cmplx128_t,,rb_ccomplex_new); break;
|
559
|
+
case CA_CMPLX256: proc_mul_add(cmplx256_t,,rb_ccomplex_new); break;
|
560
|
+
#endif
|
561
|
+
/* case CA_OBJECT: proc_mul_add(VALUE,NUM2DBL,rb_float_new); break; */
|
562
|
+
default: rb_raise(rb_eCADataTypeError, "invalid data type");
|
563
|
+
}
|
564
|
+
|
565
|
+
ca_detach_n(2, ca, cw);
|
566
|
+
|
567
|
+
free(mi);
|
568
|
+
|
569
|
+
return out;
|
570
|
+
}
|
571
|
+
|
572
|
+
void
|
573
|
+
Init_carray_operator ()
|
574
|
+
{
|
575
|
+
rb_mCAMath = rb_define_module("CAMath");
|
576
|
+
|
577
|
+
rb_define_method(rb_cCArray, "coerce", rb_ca_coerce, 1);
|
578
|
+
rb_define_method(rb_cCArray, "mul_add", rb_ca_mul_add, -1);
|
579
|
+
}
|
580
|
+
|
581
|
+
|
582
|
+
|