enumerable-statistics 2.0.5 → 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fa86766eb3c5369422d08bac77705368b0bde5faa216eecd9f78115eb7548b6
4
- data.tar.gz: 313dc69fe5d6b193278489b576af4bb90679eebf6814c36a2924a1241cd5c8be
3
+ metadata.gz: 9ca7750e402868e65da3619203a19ad1b7bd86348aee8ca2168c35db6fe6f262
4
+ data.tar.gz: 425bd2623655dd3f189ee50f12b0d44d3c25bed86194d2f31eb530431cab1090
5
5
  SHA512:
6
- metadata.gz: 13fd8a73ae4e83aa85210cbbec1882a2129802aa377f7100ffa72e48ec7df685ba5eff1553cd2ef0b29eddd2b13715769de822adcc881c6d57122212d4c8c13a
7
- data.tar.gz: d4dc08f32d2d9a43d743e213b2725f0bc41ac7d6d14c68ccecd1b3563d022c8c1c8b60eea682bce554bc7b5425e3c4b552766bb8b3a7faa386164ee8f00303b7
6
+ metadata.gz: 37a9f4e270fee3e34852bccd0adc18d288514c1d8363807b2db16877ada2174aea7c1f8c8ef7cea94cb3274bb5716c35ee689ecde95a5a71cb7724e1afd2a164
7
+ data.tar.gz: 5426b9ff42ce8033cf403be706da8a3dea94c88056a7a621218c0bc9aabe676571f868c263bef55266175759c95cc7f54267108f86cb483b0adacd11ce844968
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 2.0.6
2
+
3
+ - Add edges parameter in histogram
4
+ - Rename parameter in histogram to fix typo: `weight` to `weights`
5
+
1
6
  # 2.0.5
2
7
 
3
8
  - Add weighted histogram support
@@ -2280,13 +2280,47 @@ ary_histogram_calculate_edge(VALUE ary, const long nbins, const int left_p)
2280
2280
  return edge;
2281
2281
  }
2282
2282
 
2283
+ static VALUE
2284
+ check_histogram_weight_array(VALUE weight_array, const long ary_len)
2285
+ {
2286
+ if (weight_array == Qundef) return Qnil;
2287
+
2288
+ weight_array = rb_check_convert_type(weight_array, T_ARRAY, "Array", "to_ary");
2289
+ if (RARRAY_LEN(weight_array) != ary_len) {
2290
+ rb_raise(rb_eArgError, "weight array must have the same number of items as the receiver array");
2291
+ }
2292
+
2293
+ return weight_array;
2294
+ }
2295
+
2296
+ static VALUE
2297
+ check_histogram_edges(VALUE edges)
2298
+ {
2299
+ if (edges == Qundef) return Qnil;
2300
+ return rb_check_convert_type(edges, T_ARRAY, "Array", "to_ary");
2301
+ }
2302
+
2303
+ static int
2304
+ check_histogram_left_p(VALUE closed)
2305
+ {
2306
+ int left_p = (closed != sym_right);
2307
+ if (left_p && closed != Qundef && closed != sym_left) {
2308
+ rb_raise(rb_eArgError, "invalid value for :closed keyword "
2309
+ "(%"PRIsVALUE" for :left or :right)", closed);
2310
+ }
2311
+ return left_p;
2312
+ }
2313
+
2283
2314
  /* call-seq:
2284
2315
  * ary.histogram(nbins=:auto, weight: nil, closed: :left)
2285
2316
  *
2286
2317
  * @param [Integer] nbins The approximate number of bins
2287
- * @params [Array<Numeric>] weight
2318
+ * @params [Array<Numeric>] weights
2288
2319
  * An optional weight array, that has the same length as the receiver.
2289
2320
  * `weight[i]` means the weight value of the i-th element in the receiver.
2321
+ * @params [Array<Numeric>] edges
2322
+ * An optional edge array, that specify the bin edges.
2323
+ * This array must be sorted.
2290
2324
  * @param [:left, :right] closed
2291
2325
  * If :left (the default), the bin interval are left-closed.
2292
2326
  * If :right, the bin interval are right-closed.
@@ -2296,10 +2330,11 @@ ary_histogram_calculate_edge(VALUE ary, const long nbins, const int left_p)
2296
2330
  static VALUE
2297
2331
  ary_histogram(int argc, VALUE *argv, VALUE ary)
2298
2332
  {
2299
- VALUE arg0, kwargs, edge, bin_weights;
2333
+ VALUE arg0, kwargs, bin_weights;
2300
2334
  long nbins, n_bin_weights, i;
2301
2335
 
2302
2336
  VALUE weight_array = Qnil;
2337
+ VALUE edges = Qnil;
2303
2338
  int left_p = 1;
2304
2339
 
2305
2340
  rb_scan_args(argc, argv, "01:", &arg0, &kwargs);
@@ -2311,48 +2346,36 @@ ary_histogram(int argc, VALUE *argv, VALUE ary)
2311
2346
  }
2312
2347
 
2313
2348
  if (!NIL_P(kwargs)) {
2314
- enum { kw_weight, kw_closed };
2315
- static ID kwarg_keys[2];
2316
- VALUE kwarg_vals[2];
2317
- VALUE closed;
2349
+ enum { kw_weights, kw_edges, kw_closed };
2350
+ static ID kwarg_keys[3];
2351
+ VALUE kwarg_vals[3];
2318
2352
 
2319
2353
  if (!kwarg_keys[0]) {
2320
- kwarg_keys[kw_weight] = rb_intern("weight");
2354
+ kwarg_keys[kw_weights] = rb_intern("weights");
2355
+ kwarg_keys[kw_edges] = rb_intern("edges");
2321
2356
  kwarg_keys[kw_closed] = rb_intern("closed");
2322
2357
  }
2323
2358
 
2324
- rb_get_kwargs(kwargs, kwarg_keys, 0, 2, kwarg_vals);
2359
+ rb_get_kwargs(kwargs, kwarg_keys, 0, 3, kwarg_vals);
2325
2360
 
2326
- weight_array = kwarg_vals[kw_weight];
2327
- if (weight_array != Qundef) {
2328
- weight_array = rb_check_convert_type(weight_array, T_ARRAY, "Array", "to_ary");
2329
- if (RARRAY_LEN(weight_array) != RARRAY_LEN(ary)) {
2330
- rb_raise(rb_eArgError, "weight array must have the same number of items as the receiver array");
2331
- }
2332
- }
2333
- else {
2334
- weight_array = Qnil;
2335
- }
2336
-
2337
- closed = kwarg_vals[kw_closed];
2338
- left_p = (closed != sym_right);
2339
- if (left_p && closed != Qundef && closed != sym_left) {
2340
- rb_raise(rb_eArgError, "invalid value for :closed keyword "
2341
- "(%"PRIsVALUE" for :left or :right)", closed);
2342
- }
2361
+ weight_array = check_histogram_weight_array(kwarg_vals[kw_weights], RARRAY_LEN(ary));
2362
+ edges = check_histogram_edges(kwarg_vals[kw_edges]);
2363
+ left_p = check_histogram_left_p(kwarg_vals[kw_closed]);
2343
2364
  }
2344
2365
 
2345
- edge = ary_histogram_calculate_edge(ary, nbins, left_p);
2366
+ if (NIL_P(edges)) {
2367
+ edges = ary_histogram_calculate_edge(ary, nbins, left_p);
2368
+ }
2346
2369
 
2347
- n_bin_weights = RARRAY_LEN(edge) - 1;
2370
+ n_bin_weights = RARRAY_LEN(edges) - 1;
2348
2371
  bin_weights = rb_ary_new_capa(n_bin_weights);
2349
2372
  for (i = 0; i < n_bin_weights; ++i) {
2350
2373
  rb_ary_store(bin_weights, i, INT2FIX(0));
2351
2374
  }
2352
2375
 
2353
- histogram_weights_push_values(bin_weights, edge, ary, weight_array, left_p);
2376
+ histogram_weights_push_values(bin_weights, edges, ary, weight_array, left_p);
2354
2377
 
2355
- return rb_struct_new(cHistogram, edge, bin_weights,
2378
+ return rb_struct_new(cHistogram, edges, bin_weights,
2356
2379
  left_p ? sym_left : sym_right,
2357
2380
  Qfalse);
2358
2381
  }
@@ -1,5 +1,6 @@
1
1
  module EnumerableStatistics
2
- class Histogram < Struct.new(:edge, :weights, :closed, :isdensity)
2
+ class Histogram < Struct.new(:edges, :weights, :closed, :isdensity)
3
+ alias edge edges
3
4
  alias density? isdensity
4
5
  end
5
6
  end
@@ -1,5 +1,5 @@
1
1
  module EnumerableStatistics
2
- VERSION = '2.0.5'
2
+ VERSION = '2.0.6'
3
3
 
4
4
  module Version
5
5
  numbers, TAG = VERSION.split('-', 2)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerable-statistics
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenta Murata