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
@@ -0,0 +1,403 @@
|
|
1
|
+
# ----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# carray/base/math.rb
|
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
|
+
class CArray
|
14
|
+
|
15
|
+
# complex number
|
16
|
+
|
17
|
+
# Return the real part of +self+.
|
18
|
+
# If +self+ is a complex array, the resulted array is CAMember object
|
19
|
+
# refers the appropriate part of +self+.
|
20
|
+
# Otherwise, the resulted array is CARefer object refers +self+.
|
21
|
+
# If you change the resulted array, the original array is also changed.
|
22
|
+
#
|
23
|
+
def real
|
24
|
+
if not @__real__
|
25
|
+
if complex?
|
26
|
+
@__real__ = case data_type
|
27
|
+
when CA_CMPLX64
|
28
|
+
field(0, CA_FLOAT32)
|
29
|
+
when CA_CMPLX128
|
30
|
+
field(0, CA_FLOAT64)
|
31
|
+
when CA_CMPLX128
|
32
|
+
field(0, CA_FLOAT128)
|
33
|
+
end
|
34
|
+
else
|
35
|
+
@__real__ = self[]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
@__real__
|
39
|
+
end
|
40
|
+
|
41
|
+
def real= (val)
|
42
|
+
real[] = val
|
43
|
+
end
|
44
|
+
|
45
|
+
# Return the imaginary part of +self+.
|
46
|
+
# If +self+ is a complex array, the resulted array is CAMember object
|
47
|
+
# refers the appropriate part of +self+. In this case,
|
48
|
+
# you change the resulted array, the original array is also changed.
|
49
|
+
#
|
50
|
+
# Otherwise, the resulted array is a dummy CArray object filled with 0.
|
51
|
+
# In this case, the change in the resulted array does not affect
|
52
|
+
# the original array. For this purpose, you should explicitly convert
|
53
|
+
# the array to complex array.
|
54
|
+
#
|
55
|
+
def imag
|
56
|
+
if not @__imag__
|
57
|
+
if complex?
|
58
|
+
@__imag__ = case data_type
|
59
|
+
when CA_CMPLX64
|
60
|
+
field(4, CA_FLOAT32)
|
61
|
+
when CA_CMPLX128
|
62
|
+
field(8, CA_FLOAT64)
|
63
|
+
when CA_CMPLX128
|
64
|
+
field(16, CA_FLOAT128)
|
65
|
+
end
|
66
|
+
else
|
67
|
+
@__imag__ = self.template { 0 }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
return @__imag__
|
71
|
+
end
|
72
|
+
|
73
|
+
def imag= (val)
|
74
|
+
if complex?
|
75
|
+
imag[] = val
|
76
|
+
else
|
77
|
+
raise "not a complex array"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# comparison operators
|
82
|
+
|
83
|
+
def <=> (other)
|
84
|
+
lower = self < other
|
85
|
+
upper = self > other
|
86
|
+
out = CArray.new(CA_INT8, lower.dim)
|
87
|
+
out[lower] = -1
|
88
|
+
out[upper] = 1
|
89
|
+
return out
|
90
|
+
end
|
91
|
+
|
92
|
+
alias cmp <=>
|
93
|
+
|
94
|
+
def is_equiv (other, rtol)
|
95
|
+
exact_eq = self.eq(other)
|
96
|
+
relative_eq = ((self - other).abs/CAMath.max(self.abs, other.abs) <= rtol)
|
97
|
+
return (exact_eq).or(relative_eq)
|
98
|
+
end
|
99
|
+
|
100
|
+
def is_close (other, atol)
|
101
|
+
return ((self - other).abs <= atol)
|
102
|
+
end
|
103
|
+
|
104
|
+
def is_divisible (n)
|
105
|
+
unless integer?
|
106
|
+
raise "data type of reciever of CArray#divisible? should be integer."
|
107
|
+
end
|
108
|
+
return (self % n).eq(0)
|
109
|
+
end
|
110
|
+
|
111
|
+
def is_not_divisible (n)
|
112
|
+
unless integer?
|
113
|
+
raise "data type of reciever of CArray#divisible? should be integer."
|
114
|
+
end
|
115
|
+
return (self % n).ne(0)
|
116
|
+
end
|
117
|
+
|
118
|
+
def real?
|
119
|
+
if complex?
|
120
|
+
imag.all_equal?(0)
|
121
|
+
elsif numeric?
|
122
|
+
true
|
123
|
+
else
|
124
|
+
nil
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def is_real
|
129
|
+
if complex?
|
130
|
+
imag.eq(0)
|
131
|
+
elsif numeric?
|
132
|
+
self.true
|
133
|
+
else
|
134
|
+
nil
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def sign
|
139
|
+
out = self.zero
|
140
|
+
out[self.lt(0)] = -1
|
141
|
+
out[self.gt(0)] = 1
|
142
|
+
if float?
|
143
|
+
out[self.is_nan] = 0.0/0.0
|
144
|
+
end
|
145
|
+
return out
|
146
|
+
end
|
147
|
+
|
148
|
+
def quo (other)
|
149
|
+
case
|
150
|
+
when integer?
|
151
|
+
return double/other
|
152
|
+
when object?
|
153
|
+
return quo_i(other)
|
154
|
+
else
|
155
|
+
return self/other
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
module CAMath
|
163
|
+
|
164
|
+
module_function
|
165
|
+
|
166
|
+
def min (*argv)
|
167
|
+
if ary = argv.find{|x| x.is_a?(CArray) }
|
168
|
+
out = ary.to_ca
|
169
|
+
argv.delete(ary)
|
170
|
+
argv.each do |x|
|
171
|
+
out.pmin!(x)
|
172
|
+
end
|
173
|
+
else
|
174
|
+
raise "args should contain more than one CArray object"
|
175
|
+
end
|
176
|
+
return out
|
177
|
+
end
|
178
|
+
|
179
|
+
def max (*argv)
|
180
|
+
if ary = argv.find{|x| x.is_a?(CArray) }
|
181
|
+
out = ary.to_ca
|
182
|
+
argv.delete(ary)
|
183
|
+
argv.each do |x|
|
184
|
+
out.pmax!(x)
|
185
|
+
end
|
186
|
+
else
|
187
|
+
raise "args should contain more than one CArray object"
|
188
|
+
end
|
189
|
+
return out
|
190
|
+
end
|
191
|
+
|
192
|
+
end
|
193
|
+
|
194
|
+
class CArray
|
195
|
+
|
196
|
+
#
|
197
|
+
# statistics
|
198
|
+
#
|
199
|
+
|
200
|
+
def random (*argv)
|
201
|
+
return template.random!(*argv)
|
202
|
+
end
|
203
|
+
|
204
|
+
def randomn!
|
205
|
+
if elements == 1
|
206
|
+
self[0] = CArray.new(data_type,[2]).randomn![0]
|
207
|
+
return self
|
208
|
+
end
|
209
|
+
x1 = CArray.new(data_type, [elements/2])
|
210
|
+
x2 = CArray.new(data_type, [elements/2])
|
211
|
+
fac = x1.random!.log!.mul!(-2.0).sqrt! ### fac = sqrt(-2*log(rnd()))
|
212
|
+
x2.random!.mul!(2.0*Math::PI) ### x2 = 2*PI*rnd()
|
213
|
+
x3 = x2.to_ca
|
214
|
+
self2 = reshape(2,elements/2)
|
215
|
+
self2[0,nil] = x2.cos!.mul!(fac) ### self[even] = fac*cos(x2)
|
216
|
+
self2[1,nil] = x3.sin!.mul!(fac) ### self[odd] = fac*sin(x2)
|
217
|
+
if elements % 2 == 1
|
218
|
+
self[[-1]].randomn!
|
219
|
+
end
|
220
|
+
return self
|
221
|
+
end
|
222
|
+
|
223
|
+
def randomn
|
224
|
+
return template.randomn!
|
225
|
+
end
|
226
|
+
|
227
|
+
def anomaly (*argv)
|
228
|
+
opt = argv.last.is_a?(Hash) ? argv.pop : {}
|
229
|
+
idxs = Array.new(self.rank) { |i| argv.include?(i) ? :* : nil }
|
230
|
+
if mn = opt[:mean]
|
231
|
+
return self - mn[*idxs]
|
232
|
+
else
|
233
|
+
return self - self.mean(*argv)[*idxs]
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
alias anom anomaly
|
238
|
+
|
239
|
+
def median (*argv)
|
240
|
+
opt = argv.last.is_a?(Hash) ? argv.pop : {}
|
241
|
+
min_count = opt[:mask_limit]
|
242
|
+
if min_count and min_count < 0
|
243
|
+
min_count += elements
|
244
|
+
end
|
245
|
+
fill_value = opt[:fill_value]
|
246
|
+
if argv.empty?
|
247
|
+
if has_mask?
|
248
|
+
if min_count and count_masked() > min_count
|
249
|
+
return fill_value || UNDEF
|
250
|
+
end
|
251
|
+
c = self[:is_not_masked].sort
|
252
|
+
n = self.count_not_masked
|
253
|
+
else
|
254
|
+
c = self.sort
|
255
|
+
n = c.elements
|
256
|
+
end
|
257
|
+
if n == 0
|
258
|
+
return fill_value || UNDEF
|
259
|
+
else
|
260
|
+
return (c[(n-1)/2] + c[n/2])/2.0
|
261
|
+
end
|
262
|
+
else
|
263
|
+
raise "CArray#median is not implemented for multiple ranks"
|
264
|
+
end
|
265
|
+
|
266
|
+
end
|
267
|
+
|
268
|
+
def percentile (*argv)
|
269
|
+
opt = argv.last.is_a?(Hash) ? argv.pop : {}
|
270
|
+
pers = argv
|
271
|
+
min_count = opt[:mask_limit]
|
272
|
+
if min_count and min_count < 0
|
273
|
+
min_count += elements
|
274
|
+
end
|
275
|
+
fill_value = opt[:fill_value]
|
276
|
+
if has_mask?
|
277
|
+
if min_count and count_masked() > min_count
|
278
|
+
return argv.map { fill_value || UNDEF }
|
279
|
+
end
|
280
|
+
ca = self[:is_not_masked].sort
|
281
|
+
n = self.count_not_masked
|
282
|
+
else
|
283
|
+
ca = self.sort
|
284
|
+
n = ca.elements
|
285
|
+
end
|
286
|
+
out = []
|
287
|
+
begin
|
288
|
+
pers.each do |per|
|
289
|
+
if per == 100
|
290
|
+
out << ca[n-1]
|
291
|
+
elsif per >= 0 and per < 100
|
292
|
+
if n > 1
|
293
|
+
f = (n-1)*per/100.0
|
294
|
+
k = f.floor
|
295
|
+
r = f - k
|
296
|
+
out << (1-r)*ca[k] + r*ca[k+1]
|
297
|
+
else
|
298
|
+
out << ca[0]
|
299
|
+
end
|
300
|
+
else
|
301
|
+
out << CA_NAN
|
302
|
+
end
|
303
|
+
end
|
304
|
+
rescue
|
305
|
+
p self[:is_not_masked]
|
306
|
+
p n
|
307
|
+
raise
|
308
|
+
end
|
309
|
+
return out
|
310
|
+
end
|
311
|
+
|
312
|
+
def quantile
|
313
|
+
return percentile(0, 25, 50, 75, 100)
|
314
|
+
end
|
315
|
+
|
316
|
+
def covariancep (y, min_count = nil, fill_value = nil)
|
317
|
+
x = self.double
|
318
|
+
y = y.double
|
319
|
+
if x.has_mask? or y.has_mask?
|
320
|
+
x.inherit_mask(y)
|
321
|
+
y.inherit_mask(x)
|
322
|
+
count = x.count_not_masked
|
323
|
+
xm = x.mean(:min_count => min_count)
|
324
|
+
ym = y.mean(:min_count => min_count)
|
325
|
+
if ( xm.undef? or ym.undef? )
|
326
|
+
return fill_value || UNDEF
|
327
|
+
else
|
328
|
+
return (x-xm).wsum(y-ym)/count
|
329
|
+
end
|
330
|
+
else
|
331
|
+
return (x-x.mean).wsum(y-y.mean)/elements
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
def covariance (y, min_count = nil, fill_value = nil)
|
336
|
+
x = self.double
|
337
|
+
y = y.double
|
338
|
+
if x.has_mask? or y.has_mask?
|
339
|
+
x.inherit_mask(y)
|
340
|
+
y.inherit_mask(x)
|
341
|
+
count = x.count_not_masked
|
342
|
+
xm = x.mean(:min_count=>min_count)
|
343
|
+
ym = y.mean(:min_count=>min_count)
|
344
|
+
if ( xm.undef? or ym.undef? )
|
345
|
+
return fill_value || UNDEF
|
346
|
+
else
|
347
|
+
return (x-xm).wsum(y-ym)/(count-1)
|
348
|
+
end
|
349
|
+
else
|
350
|
+
return (x-x.mean).wsum(y-y.mean)/(elements-1)
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
def correlation (y, min_count = nil, fill_value = nil)
|
355
|
+
x = self.double
|
356
|
+
y = y.double
|
357
|
+
if x.has_mask? or y.has_mask?
|
358
|
+
x.inherit_mask(y)
|
359
|
+
y.inherit_mask(x)
|
360
|
+
xm = x.mean(:min_count=>min_count)
|
361
|
+
ym = y.mean(:min_count=>min_count)
|
362
|
+
if ( xm.undef? or ym.undef? )
|
363
|
+
return fill_value || UNDEF
|
364
|
+
else
|
365
|
+
xd, yd = x-xm, y-ym
|
366
|
+
return xd.wsum(yd)/(xd.wsum(xd)*yd.wsum(yd)).sqrt
|
367
|
+
end
|
368
|
+
else
|
369
|
+
xd, yd = x-x.mean, y-y.mean
|
370
|
+
return xd.wsum(yd)/(xd.wsum(xd)*yd.wsum(yd)).sqrt
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
end
|
375
|
+
|
376
|
+
class CArray
|
377
|
+
|
378
|
+
def self.summation (*dim)
|
379
|
+
out = nil
|
380
|
+
first = true
|
381
|
+
CArray.each_index(*dim) { |*idx|
|
382
|
+
if first
|
383
|
+
out = yield(*idx)
|
384
|
+
first = false
|
385
|
+
else
|
386
|
+
out += yield(*idx)
|
387
|
+
end
|
388
|
+
}
|
389
|
+
return out
|
390
|
+
end
|
391
|
+
|
392
|
+
def by (other)
|
393
|
+
case other
|
394
|
+
when CArray
|
395
|
+
return (self[nil][nil,:*]*other[nil][:*,nil]).reshape(*(dim+other.dim))
|
396
|
+
else
|
397
|
+
return self * other
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
end
|
402
|
+
|
403
|
+
|
@@ -0,0 +1,93 @@
|
|
1
|
+
|
2
|
+
class CArray
|
3
|
+
|
4
|
+
def fa # :nodoc:
|
5
|
+
warn "CArray#fa will be obsolete, use CArray#t"
|
6
|
+
return self.t
|
7
|
+
end
|
8
|
+
|
9
|
+
def block_iterator (*argv) # :nodoc:
|
10
|
+
warn "CArray#block_iterator will be obsolete, use CArray#blocks"
|
11
|
+
return blocks(*argv)
|
12
|
+
end
|
13
|
+
|
14
|
+
def window_iterator (*argv) # :nodoc:
|
15
|
+
warn "CArray#window_iterator will be obsolete, use CArray#windows"
|
16
|
+
return windows(*argv)
|
17
|
+
end
|
18
|
+
|
19
|
+
def rotated (*argv) # :nodoc:
|
20
|
+
warn "CArray#rotated will be obsolete, use CArray#rolled"
|
21
|
+
argv.push({:roll => Array.new(rank){1} })
|
22
|
+
return shifted(*argv)
|
23
|
+
end
|
24
|
+
|
25
|
+
def rotate! (*argv) # :nodoc:
|
26
|
+
warn "CArray#rotate! will be obsolete, use CArray#roll!"
|
27
|
+
self[] = self.rolled(*argv)
|
28
|
+
return self
|
29
|
+
end
|
30
|
+
|
31
|
+
def rotate (*argv) # :nodoc:
|
32
|
+
warn "CArray#rotate will be obsolete, use CArray#roll"
|
33
|
+
return self.rolled(*argv).to_ca
|
34
|
+
end
|
35
|
+
|
36
|
+
def select (&block) # :nodoc:
|
37
|
+
warn "CArray#select will be obsolete"
|
38
|
+
case block.arity
|
39
|
+
when 1
|
40
|
+
return self[*yield(self)]
|
41
|
+
when -1, 0
|
42
|
+
return self[*instance_exec(&block)]
|
43
|
+
else
|
44
|
+
raise
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def transform (type, dim, opt = {}) # :nodoc:
|
49
|
+
warn("CArray#transform will be obsolete")
|
50
|
+
return refer(type, dim, opt).to_ca
|
51
|
+
end
|
52
|
+
|
53
|
+
def classify (klass, outlier = nil) # :nodoc:
|
54
|
+
warn "CArray#classify will be obsolete"
|
55
|
+
b = CArray.int32(*dim)
|
56
|
+
f = CArray.boolean(*dim) { 1 }
|
57
|
+
attach {
|
58
|
+
(klass.elements-1).times do |i|
|
59
|
+
r = f.and(self < klass[i+1])
|
60
|
+
b[r] = i
|
61
|
+
f[r] = 0
|
62
|
+
end
|
63
|
+
if outlier
|
64
|
+
b[self < klass[0]] = outlier
|
65
|
+
b[f] = outlier
|
66
|
+
else
|
67
|
+
b[self < klass[0]] = -1
|
68
|
+
b[f] = klass.elements-1
|
69
|
+
end
|
70
|
+
}
|
71
|
+
return b
|
72
|
+
end
|
73
|
+
|
74
|
+
=begin
|
75
|
+
def histogram (klass)
|
76
|
+
c = CArray.int32(klass.elements-1)
|
77
|
+
f = CArray.boolean(*dim) { 1 }
|
78
|
+
attach {
|
79
|
+
k = 0
|
80
|
+
r = f.and(self < klass[0])
|
81
|
+
f[r] = 0
|
82
|
+
(klass.elements-1).times do |i|
|
83
|
+
r = f.and(self < klass[i+1])
|
84
|
+
c[k] = r.count_true
|
85
|
+
f[r] = 0
|
86
|
+
k+=1
|
87
|
+
end
|
88
|
+
}
|
89
|
+
return c
|
90
|
+
end
|
91
|
+
=end
|
92
|
+
|
93
|
+
end
|