Class: Numo::Bit

Inherits:
NArray show all
Defined in:
ext/numo/narray/src/t_bit.c,
ext/numo/narray/src/t_bit.c

Overview

Binary digit (bit) N-dimensional array class.

Constant Summary collapse

UPCAST =

Upcasting rules of Bit.

hCast
ELEMENT_BIT_SIZE =

Element size of Bit in bits.

INT2FIX(1)
ELEMENT_BYTE_SIZE =

Element size of Bit in bytes.

rb_float_new(1.0 / 8)
CONTIGUOUS_STRIDE =

Stride size of contiguous Bit array.

INT2FIX(1)

Constants inherited from NArray

NArray::ALTERNATIVE, NArray::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NArray

#==, alternative?, #append, #argsort, array_type, asarray, #at, byte_size, #byte_size, #byte_swapped?, #cast_to, #coerce, #column_major?, column_stack, concatenate, #concatenate, #contiguous?, #cov, debug=, #debug_info, #deg2rad, #delete, #diag, #diag_indices, diag_indices, #diagonal, #diff, #dot, #dsplit, dstack, #each_over_axis, #empty?, #expand_dims, eye, #flatten, #fliplr, #flipud, #fortran_contiguous?, #free, from_binary, #host_order?, #hsplit, hstack, #initialize, #initialize_copy, #inner, #inplace, #inplace!, #inplace?, #insert, inspect_cols, inspect_cols=, inspect_rows, inspect_rows=, #kron, linspace, logspace, #marshal_dump, #marshal_load, #ndim, #new_fill, new_like, #new_narray, #new_ones, #new_zeros, ones, #out_of_place!, #outer, parse, #percentile, profile, profile=, #rad2deg, #repeat, #reshape, #reshape!, #reverse, #rot90, #row_major?, #shape, #size, #split, #store_binary, #swap_byte, #swapaxes, #tile, #to_binary, #to_c, #to_f, #to_host, #to_i, #to_network, #to_swapped, #to_vacs, #trace, #transpose, #tril, #tril!, #tril_indices, tril_indices, #triu, #triu!, #triu_indices, triu_indices, upcast, #view, #vsplit, vstack, zeros

Constructor Details

This class inherits a constructor from Numo::NArray

Class Method Details

.[](elements) ⇒ Object .cast(array) ⇒ Numo::Bit

Cast object to Numo::Bit.

Overloads:

  • .cast(array) ⇒ Numo::Bit

    Parameters:

    • elements (Numeric, Array)
    • array (Array)

    Returns:



1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
# File 'ext/numo/narray/src/t_bit.c', line 1584

static VALUE bit_s_cast(VALUE type, VALUE obj) {
  VALUE v;
  narray_t* na;
  dtype x;

  if (rb_obj_class(obj) == cT) {
    return obj;
  }
  if (RTEST(rb_obj_is_kind_of(obj, rb_cNumeric))) {
    x = m_num_to_data(obj);
    return bit_new_dim0(x);
  }
  if (RTEST(rb_obj_is_kind_of(obj, rb_cArray))) {
    return bit_cast_array(obj);
  }
  if (IsNArray(obj)) {
    GetNArray(obj, na);
    v = nary_new(cT, NA_NDIM(na), NA_SHAPE(na));
    if (NA_SIZE(na) > 0) {
      bit_store(v, obj);
    }
    return v;
  }
  if (rb_respond_to(obj, id_to_a)) {
    obj = rb_funcall(obj, id_to_a, 0);
    if (TYPE(obj) != T_ARRAY) {
      rb_raise(rb_eTypeError, "`to_a' did not return Array");
    }
    return bit_cast_array(obj);
  }

  rb_raise(nary_eCastError, "cannot cast to %s", rb_class2name(type));
  return Qnil;
}

.[](elements) ⇒ Object .cast(array) ⇒ Numo::Bit

Cast object to Numo::Bit.

Overloads:

  • .cast(array) ⇒ Numo::Bit

    Parameters:

    • elements (Numeric, Array)
    • array (Array)

    Returns:



1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
# File 'ext/numo/narray/src/t_bit.c', line 1584

static VALUE bit_s_cast(VALUE type, VALUE obj) {
  VALUE v;
  narray_t* na;
  dtype x;

  if (rb_obj_class(obj) == cT) {
    return obj;
  }
  if (RTEST(rb_obj_is_kind_of(obj, rb_cNumeric))) {
    x = m_num_to_data(obj);
    return bit_new_dim0(x);
  }
  if (RTEST(rb_obj_is_kind_of(obj, rb_cArray))) {
    return bit_cast_array(obj);
  }
  if (IsNArray(obj)) {
    GetNArray(obj, na);
    v = nary_new(cT, NA_NDIM(na), NA_SHAPE(na));
    if (NA_SIZE(na) > 0) {
      bit_store(v, obj);
    }
    return v;
  }
  if (rb_respond_to(obj, id_to_a)) {
    obj = rb_funcall(obj, id_to_a, 0);
    if (TYPE(obj) != T_ARRAY) {
      rb_raise(rb_eTypeError, "`to_a' did not return Array");
    }
    return bit_cast_array(obj);
  }

  rb_raise(nary_eCastError, "cannot cast to %s", rb_class2name(type));
  return Qnil;
}

Instance Method Details

#&(other) ⇒ Numo::NArray

Binary and.

Returns and of self and other.

Parameters:

Returns:



1978
1979
1980
1981
1982
1983
1984
# File 'ext/numo/narray/src/t_bit.c', line 1978

static VALUE bit_and(VALUE self, VALUE other) {
  ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
  ndfunc_arg_out_t aout[1] = { { cT, 0 } };
  ndfunc_t ndf = { iter_bit_and, FULL_LOOP, 2, 1, ain, aout };

  return na_ndloop(&ndf, 2, self, other);
}

#[](dim0, ..., dimL) ⇒ Numeric, Numo::Bit

Multi-dimensional element reference.

Examples:

a = Numo::Int32.new(3,4).seq
# => Numo::Int32#shape=[3,4]
# [[0, 1, 2, 3],
#  [4, 5, 6, 7],
#  [8, 9, 10, 11]]

b = (a%2).eq(0)
# => Numo::Bit#shape=[3,4]
# [[1, 0, 1, 0],
#  [1, 0, 1, 0],
#  [1, 0, 1, 0]]

b[true,(0..-1)%2]
# => Numo::Bit(view)#shape=[3,2]
# [[1, 1],
#  [1, 1],
#  [1, 1]]

b[1,1]
# => 0

dim0,…,dimL multi-dimensional indices.

Parameters:

Returns:

  • (Numeric, Numo::Bit)

    an element or NArray view.

See Also:



50
51
52
53
54
55
56
57
58
59
60
# File 'ext/numo/narray/src/t_bit.c', line 50

static VALUE bit_aref(int argc, VALUE* argv, VALUE self) {
  size_t pos;
  int nd = na_get_result_dimension(self, argc, argv, 1, &pos);
  if (nd) {
    return na_aref_main(argc, argv, self, 0, nd);
  }
  char* ptr = na_get_pointer_for_read(self);
  BIT_DIGIT x;
  LOAD_BIT(ptr, pos, x);
  return m_data_to_num(x);
}

#[]=(dim0, ..., dimL, val) ⇒ Numeric, ...

Multi-dimensional element assignment.

Examples:

a = Numo::Bit.new(4,5).fill(0)
# => Numo::Bit#shape=[4,5]
# [[0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0]]

a[(0..-1)%2,(1..-1)%2] = 1
a
# => Numo::Bit#shape=[4,5]
# [[0, 1, 0, 1, 0],
#  [0, 0, 0, 0, 0],
#  [0, 1, 0, 1, 0],
#  [0, 0, 0, 0, 0]]

dim0,…,dimL multi-dimensional indices.

Parameters:

Returns:

  • (Numeric, Numo::NArray, Array)

    returns ‘val` (last argument).

See Also:



1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
# File 'ext/numo/narray/src/t_bit.c', line 1645

static VALUE bit_aset(int argc, VALUE* argv, VALUE self) {
  int nd;
  size_t pos;
  char* ptr;
  VALUE a;
  dtype x;

  argc--;
  if (argc == 0) {
    bit_store(self, argv[argc]);
  } else {
    nd = na_get_result_dimension(self, argc, argv, 1, &pos);
    if (nd) {
      a = na_aref_main(argc, argv, self, 0, nd);
      bit_store(a, argv[argc]);
    } else {
      x = bit_extract_data(argv[argc]);
      ptr = na_get_pointer_for_read_write(self);
      STORE_BIT(ptr, pos, x);
    }
  }
  return argv[argc];
}

#^(other) ⇒ Numo::NArray

Binary xor.

Returns xor of self and other.

Parameters:

Returns:



2236
2237
2238
2239
2240
2241
2242
# File 'ext/numo/narray/src/t_bit.c', line 2236

static VALUE bit_xor(VALUE self, VALUE other) {
  ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
  ndfunc_arg_out_t aout[1] = { { cT, 0 } };
  ndfunc_t ndf = { iter_bit_xor, FULL_LOOP, 2, 1, ain, aout };

  return na_ndloop(&ndf, 2, self, other);
}

#all?(axis: nil, keepdims: false) ⇒ Numo::Bit

Return true if all of bits are one (true). If argument is supplied, return Bit-array reduced along the axes.

as dimensions with size one.

Parameters:

  • axis (Integer, Array, Range) (defaults to: nil)

    (keyword) axes to be reduced.

  • keepdims (TrueClass) (defaults to: false)

    (keyword) If true, the reduced axes are left in the result array

Returns:



2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
# File 'ext/numo/narray/src/t_bit.c', line 2648

static VALUE bit_all_p(int argc, VALUE* argv, VALUE self) {
  VALUE v, reduce;
  narray_t* na;
  ndfunc_arg_in_t ain[3] = { { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
  ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
  ndfunc_t ndf = { iter_bit_all_p, FULL_LOOP_NIP, 3, 1, ain, aout };

  GetNArray(self, na);
  if (NA_SIZE(na) == 0) {
    return Qfalse;
  }
  reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
  v = na_ndloop(&ndf, 3, self, reduce, INT2FIX(1));
  if (argc > 0) {
    return v;
  }
  v = bit_extract(v);
  switch (v) {
  case INT2FIX(0):
    return Qfalse;
  case INT2FIX(1):
    return Qtrue;
  default:
    rb_bug("unexpected result");
    return v;
  }
}

#allocateObject



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'ext/numo/narray/src/t_bit.c', line 515

static VALUE bit_allocate(VALUE self) {
  narray_t* na;
  char* ptr;

  GetNArray(self, na);

  switch (NA_TYPE(na)) {
  case NARRAY_DATA_T:
    ptr = NA_DATA_PTR(na);
    if (na->size > 0 && ptr == NULL) {
      ptr = xmalloc(((na->size - 1) / 8 / sizeof(BIT_DIGIT) + 1) * sizeof(BIT_DIGIT));
      NA_DATA_PTR(na) = ptr;
      NA_DATA_OWNED(na) = TRUE;
    }
    break;
  case NARRAY_VIEW_T:
    rb_funcall(NA_VIEW_DATA(na), rb_intern("allocate"), 0);
    break;
  default:
    rb_raise(rb_eRuntimeError, "invalid narray type");
  }
  return self;
}

#any?(axis: nil, keepdims: false) ⇒ Numo::Bit

Return true if any of bits is one (true). If argument is supplied, return Bit-array reduced along the axes.

as dimensions with size one.

Parameters:

  • axis (Integer, Array, Range) (defaults to: nil)

    (keyword) axes to be reduced.

  • keepdims (TrueClass) (defaults to: false)

    (keyword) If true, the reduced axes are left in the result array

Returns:



2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
# File 'ext/numo/narray/src/t_bit.c', line 2776

static VALUE bit_any_p(int argc, VALUE* argv, VALUE self) {
  VALUE v, reduce;
  narray_t* na;
  ndfunc_arg_in_t ain[3] = { { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
  ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
  ndfunc_t ndf = { iter_bit_any_p, FULL_LOOP_NIP, 3, 1, ain, aout };

  GetNArray(self, na);
  if (NA_SIZE(na) == 0) {
    return Qfalse;
  }
  reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
  v = na_ndloop(&ndf, 3, self, reduce, INT2FIX(0));
  if (argc > 0) {
    return v;
  }
  v = bit_extract(v);
  switch (v) {
  case INT2FIX(0):
    return Qfalse;
  case INT2FIX(1):
    return Qtrue;
  default:
    rb_bug("unexpected result");
    return v;
  }
}

#coerce_cast(type) ⇒ nil

return NArray with cast to the type of self.

Returns:

  • (nil)


62
63
64
# File 'ext/numo/narray/src/t_bit.c', line 62

static VALUE bit_coerce_cast(VALUE self, VALUE type) {
  return Qnil;
}

#copyNumo::Bit

Unary copy.

Returns copy of self.

Returns:



1755
1756
1757
1758
1759
1760
1761
# File 'ext/numo/narray/src/t_bit.c', line 1755

static VALUE bit_copy(VALUE self) {
  ndfunc_arg_in_t ain[1] = { { cT, 0 } };
  ndfunc_arg_out_t aout[1] = { { cT, 0 } };
  ndfunc_t ndf = { iter_bit_copy, FULL_LOOP, 1, 1, ain, aout };

  return na_ndloop(&ndf, 1, self);
}

#count_false(axis: nil, keepdims: false) ⇒ Numo::Int64 Also known as: count_0

Returns the number of bits. If argument is supplied, return Int-array counted along the axes.

as dimensions with size one.

Parameters:

  • axis (Integer, Array, Range) (defaults to: nil)

    (keyword) axes to be counted.

  • keepdims (TrueClass) (defaults to: false)

    (keyword) If true, the reduced axes are left in the result array

Returns:



2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
# File 'ext/numo/narray/src/t_bit.c', line 2533

static VALUE bit_count_false(int argc, VALUE* argv, VALUE self) {
  VALUE v, reduce;
  narray_t* na;
  ndfunc_arg_in_t ain[3] = { { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
  ndfunc_arg_out_t aout[1] = { { numo_cInt64, 0 } };
  ndfunc_t ndf = { iter_bit_count_false, FULL_LOOP_NIP, 3, 1, ain, aout };

  GetNArray(self, na);
  if (NA_SIZE(na) == 0) {
    return INT2FIX(0);
  }
  reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
  v = na_ndloop(&ndf, 3, self, reduce, INT2FIX(0));
  return rb_funcall(v, rb_intern("extract"), 0);
}

#count_true(axis: nil, keepdims: false) ⇒ Numo::Int64 Also known as: count_1, count

Returns the number of bits. If argument is supplied, return Int-array counted along the axes.

as dimensions with size one.

Parameters:

  • axis (Integer, Array, Range) (defaults to: nil)

    (keyword) axes to be counted.

  • keepdims (TrueClass) (defaults to: false)

    (keyword) If true, the reduced axes are left in the result array

Returns:



2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
# File 'ext/numo/narray/src/t_bit.c', line 2445

static VALUE bit_count_true(int argc, VALUE* argv, VALUE self) {
  VALUE v, reduce;
  narray_t* na;
  ndfunc_arg_in_t ain[3] = { { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
  ndfunc_arg_out_t aout[1] = { { numo_cInt64, 0 } };
  ndfunc_t ndf = { iter_bit_count_true, FULL_LOOP_NIP, 3, 1, ain, aout };

  GetNArray(self, na);
  if (NA_SIZE(na) == 0) {
    return INT2FIX(0);
  }
  reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
  v = na_ndloop(&ndf, 3, self, reduce, INT2FIX(0));
  return rb_funcall(v, rb_intern("extract"), 0);
}

#each {|x| ... } ⇒ Numo::NArray

Calls the given block once for each element in self, passing that element as a parameter.

For a block {|x| … }

Yields:

  • (x)

    x is element of NArray.

Returns:



293
294
295
296
297
298
# File 'ext/numo/narray/src/t_bit.c', line 293

static VALUE bit_each(VALUE self) {
  ndfunc_arg_in_t ain[1] = { { Qnil, 0 } };
  ndfunc_t ndf = { iter_bit_each, FULL_LOOP_NIP, 1, 0, ain, 0 };
  na_ndloop(&ndf, 1, self);
  return self;
}

#each_with_index {|x, i, j, ...| ... } ⇒ Numo::NArray

Invokes the given block once for each element of self, passing that element and indices along each axis as parameters.

For a block {|x,i,j,…| … }

Yields:

  • (x, i, j, ...)

    x is an element, i,j,… are multidimensional indices.

Returns:



339
340
341
342
343
344
# File 'ext/numo/narray/src/t_bit.c', line 339

static VALUE bit_each_with_index(VALUE self) {
  ndfunc_arg_in_t ain[1] = { { Qnil, 0 } };
  ndfunc_t ndf = { iter_bit_each_with_index, FULL_LOOP_NIP, 1, 0, ain, 0 };
  na_ndloop_with_index(&ndf, 1, self);
  return self;
}

#eq(other) ⇒ Numo::NArray

Binary eq.

Returns eq of self and other.

Parameters:

Returns:



2365
2366
2367
2368
2369
2370
2371
# File 'ext/numo/narray/src/t_bit.c', line 2365

static VALUE bit_eq(VALUE self, VALUE other) {
  ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
  ndfunc_arg_out_t aout[1] = { { cT, 0 } };
  ndfunc_t ndf = { iter_bit_eq, FULL_LOOP, 2, 1, ain, aout };

  return na_ndloop(&ndf, 2, self, other);
}

#extractNumeric, Numo::NArray

Extract an element only if self is a dimensionless NArray.

— Extract element value as Ruby Object if self is a dimensionless NArray, otherwise returns self.

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'ext/numo/narray/src/t_bit.c', line 34

static VALUE bit_extract(VALUE self) {
  BIT_DIGIT* ptr;
  BIT_DIGIT val;
  size_t pos;
  narray_t* na;
  GetNArray(self, na);
  if (na->ndim == 0) {
    pos = na_get_offset(self);
    ptr = (BIT_DIGIT*)na_get_pointer_for_read(self);
    val = ((*((ptr) + (pos) / NB)) >> ((pos) % NB)) & 1u;
    na_release_lock(self);
    return INT2FIX(val);
  }
  return self;
}

#fill(other) ⇒ Numo::Bit

Fill elements with other.

Returns self.

Parameters:

  • other (Numeric)

Returns:



155
156
157
158
159
160
# File 'ext/numo/narray/src/t_bit.c', line 155

static VALUE bit_fill(VALUE self, VALUE val) {
  ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { sym_option } };
  ndfunc_t ndf = { iter_bit_fill, FULL_LOOP, 2, 0, ain, 0 };
  na_ndloop(&ndf, 2, self, val);
  return self;
}

#format(format) ⇒ Numo::RObject

Format elements into strings.

Returns array of formatted strings.

Parameters:

  • format (String)

Returns:



205
206
207
208
209
210
211
212
# File 'ext/numo/narray/src/t_bit.c', line 205

static VALUE bit_format(int argc, VALUE* argv, VALUE self) {
  VALUE fmt = Qnil;
  ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_option } };
  ndfunc_arg_out_t aout[1] = { { numo_cRObject, 0 } };
  ndfunc_t ndf = { iter_bit_format, FULL_LOOP_NIP, 2, 1, ain, aout };
  rb_scan_args(argc, argv, "01", &fmt);
  return na_ndloop(&ndf, 2, self, fmt);
}

#format_to_a(format) ⇒ Array

Format elements into strings.

Returns array of formatted strings.

Parameters:

  • format (String)

Returns:

  • (Array)

    array of formatted strings.



247
248
249
250
251
252
253
254
# File 'ext/numo/narray/src/t_bit.c', line 247

static VALUE bit_format_to_a(int argc, VALUE* argv, VALUE self) {
  VALUE fmt = Qnil;
  ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { sym_loop_opt }, { sym_option } };
  ndfunc_arg_out_t aout[1] = { { rb_cArray, 0 } };
  ndfunc_t ndf = { iter_bit_format_to_a, FULL_LOOP_NIP, 3, 1, ain, aout };
  rb_scan_args(argc, argv, "01", &fmt);
  return na_ndloop_cast_narray_to_rarray(&ndf, self, fmt);
}

#inspectString

Returns a string containing a human-readable representation of NArray.

Returns:

  • (String)


262
263
264
# File 'ext/numo/narray/src/t_bit.c', line 262

static VALUE bit_inspect(VALUE ary) {
  return na_ndloop_inspect(ary, iter_bit_inspect, Qnil);
}

#mask(array) ⇒ Numo::NArray

Return subarray of argument masked with self bit array.

Returns view of masked array.

Parameters:

Returns:



3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
# File 'ext/numo/narray/src/t_bit.c', line 3073

static VALUE bit_mask(VALUE mask, VALUE val) {
  int i;
  VALUE idx_1, view;
  narray_data_t* nidx;
  narray_view_t *nv, *nv_val;
  narray_t *na, *na_mask;
  stridx_t stridx0;
  size_t n_1;
  where_opt_t g;
  ndfunc_arg_in_t ain[2] = { { cT, 0 }, { Qnil, 0 } };
  ndfunc_t ndf = { iter_bit_mask, FULL_LOOP, 2, 0, ain, 0 };

  // cast val to NArray
  if (!rb_obj_is_kind_of(val, numo_cNArray)) {
    val = rb_funcall(numo_cNArray, id_cast, 1, val);
  }
  // shapes of mask and val must be same
  GetNArray(val, na);
  GetNArray(mask, na_mask);
  if (na_mask->ndim != na->ndim) {
    shape_error();
  }
  for (i = 0; i < na->ndim; i++) {
    if (na_mask->shape[i] != na->shape[i]) {
      shape_error();
    }
  }

  n_1 = NUM2SIZET(bit_count_true(0, NULL, mask));
  idx_1 = nary_new(cIndex, 1, &n_1);
  g.count = 0;
  g.elmsz = SIZEOF_VOIDP;
  g.idx1 = na_get_pointer_for_write(idx_1);
  g.idx0 = NULL;
  na_ndloop3(&ndf, &g, 2, mask, val);

  view = na_s_allocate_view(rb_obj_class(val));
  GetNArrayView(view, nv);
  na_setup_shape((narray_t*)nv, 1, &n_1);

  GetNArrayData(idx_1, nidx);
  SDX_SET_INDEX(stridx0, (size_t*)nidx->ptr);
  nidx->ptr = NULL;
  RB_GC_GUARD(idx_1);

  nv->stridx = ALLOC_N(stridx_t, 1);
  nv->stridx[0] = stridx0;
  nv->offset = 0;

  switch (NA_TYPE(na)) {
  case NARRAY_DATA_T:
    nv->data = val;
    break;
  case NARRAY_VIEW_T:
    GetNArrayView(val, nv_val);
    nv->data = nv_val->data;
    break;
  default:
    rb_raise(rb_eRuntimeError, "invalid NA_TYPE: %d", NA_TYPE(na));
  }

  return view;
}

#mean(axis: nil, keepdims: false, nan: false) ⇒ Numo::DFloat

mean of self.

Returns result of mean.

Parameters:

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs mean along the axis.

  • keepdims (Boolean) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

  • nan (Boolean) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or return NaN for min/max etc).

Returns:



361
362
363
364
365
366
367
368
# File 'ext/numo/narray/src/t_bit.c', line 361

static VALUE bit_mean(int argc, VALUE* argv, VALUE self) {
  ndfunc_arg_in_t ain[2] = { { numo_cBit, 0 }, { sym_reduce, 0 } };
  ndfunc_arg_out_t aout[1] = { { numo_cDFloat, 0 } };
  ndfunc_t ndf = { iter_bit_mean, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
  VALUE reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
  VALUE v = na_ndloop(&ndf, 2, self, reduce);
  return rb_funcall(v, rb_intern("extract"), 0);
}

#none?(*args) ⇒ Boolean

Returns:

  • (Boolean)


2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
# File 'ext/numo/narray/src/t_bit.c', line 2804

static VALUE bit_none_p(int argc, VALUE* argv, VALUE self) {
  VALUE v;

  v = bit_any_p(argc, argv, self);

  if (v == Qtrue) {
    return Qfalse;
  } else if (v == Qfalse) {
    return Qtrue;
  }
  return bit_not(v);
}

#rms(axis: nil, keepdims: false, nan: false) ⇒ Numo::DFloat

rms of self.

Returns result of rms.

Parameters:

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs rms along the axis.

  • keepdims (Boolean) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

  • nan (Boolean) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

Returns:



433
434
435
436
437
438
439
440
# File 'ext/numo/narray/src/t_bit.c', line 433

static VALUE bit_rms(int argc, VALUE* argv, VALUE self) {
  ndfunc_arg_in_t ain[2] = { { numo_cBit, 0 }, { sym_reduce, 0 } };
  ndfunc_arg_out_t aout[1] = { { numo_cDFloat, 0 } };
  ndfunc_t ndf = { iter_bit_rms, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
  VALUE reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
  VALUE v = na_ndloop(&ndf, 2, self, reduce);
  return rb_funcall(v, rb_intern("extract"), 0);
}

#stddev(axis: nil, keepdims: false, nan: false) ⇒ Numo::DFloat

stddev of self.

Returns result of stddev.

Parameters:

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs stddev along the axis.

  • keepdims (Boolean) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

  • nan (Boolean) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

Returns:



409
410
411
412
413
414
415
416
# File 'ext/numo/narray/src/t_bit.c', line 409

static VALUE bit_stddev(int argc, VALUE* argv, VALUE self) {
  ndfunc_arg_in_t ain[2] = { { numo_cBit, 0 }, { sym_reduce, 0 } };
  ndfunc_arg_out_t aout[1] = { { numo_cDFloat, 0 } };
  ndfunc_t ndf = { iter_bit_stddev, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
  VALUE reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
  VALUE v = na_ndloop(&ndf, 2, self, reduce);
  return rb_funcall(v, rb_intern("extract"), 0);
}

#store(other) ⇒ Numo::Bit

Store elements to Numo::Bit from other.

Returns self.

Parameters:

  • other (Object)

Returns:



1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
# File 'ext/numo/narray/src/t_bit.c', line 1370

static VALUE bit_store(VALUE self, VALUE obj) {
  VALUE r, klass;

  klass = rb_obj_class(obj);

  if (klass == numo_cBit) {
    bit_store_bit(self, obj);
    return self;
  }

  if (IS_INTEGER_CLASS(klass) || klass == rb_cFloat || klass == rb_cComplex) {
    bit_store_numeric(self, obj);
    return self;
  }

  if (klass == numo_cDFloat) {
    bit_store_dfloat(self, obj);
    return self;
  }

  if (klass == numo_cSFloat) {
    bit_store_sfloat(self, obj);
    return self;
  }

  if (klass == numo_cInt64) {
    bit_store_int64(self, obj);
    return self;
  }

  if (klass == numo_cInt32) {
    bit_store_int32(self, obj);
    return self;
  }

  if (klass == numo_cInt16) {
    bit_store_int16(self, obj);
    return self;
  }

  if (klass == numo_cInt8) {
    bit_store_int8(self, obj);
    return self;
  }

  if (klass == numo_cUInt64) {
    bit_store_uint64(self, obj);
    return self;
  }

  if (klass == numo_cUInt32) {
    bit_store_uint32(self, obj);
    return self;
  }

  if (klass == numo_cUInt16) {
    bit_store_uint16(self, obj);
    return self;
  }

  if (klass == numo_cUInt8) {
    bit_store_uint8(self, obj);
    return self;
  }

  if (klass == numo_cRObject) {
    bit_store_robject(self, obj);
    return self;
  }

  if (klass == rb_cArray) {
    bit_store_array(self, obj);
    return self;
  }

  if (IsNArray(obj)) {
    r = rb_funcall(obj, rb_intern("coerce_cast"), 1, cT);
    if (rb_obj_class(r) == cT) {
      bit_store(self, r);
      return self;
    }
  }

  rb_raise(
    nary_eCastError, "unknown conversion from %s to %s", rb_class2name(rb_obj_class(obj)),
    rb_class2name(rb_obj_class(self))
  );

  return self;
}

#to_aArray

Convert self to Array.

Returns:

  • (Array)


97
98
99
100
101
102
# File 'ext/numo/narray/src/t_bit.c', line 97

static VALUE bit_to_a(VALUE self) {
  ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { sym_loop_opt }, { sym_option } };
  ndfunc_arg_out_t aout[1] = { { rb_cArray, 0 } };
  ndfunc_t ndf = { iter_bit_to_a, FULL_LOOP_NIP, 3, 1, ain, aout };
  return na_ndloop_cast_narray_to_rarray(&ndf, self, Qnil);
}

#var(axis: nil, keepdims: false, nan: false) ⇒ Numo::DFloat

var of self.

Returns result of var.

Parameters:

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs var along the axis.

  • keepdims (Boolean) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

  • nan (Boolean) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

Returns:



385
386
387
388
389
390
391
392
# File 'ext/numo/narray/src/t_bit.c', line 385

static VALUE bit_var(int argc, VALUE* argv, VALUE self) {
  ndfunc_arg_in_t ain[2] = { { numo_cBit, 0 }, { sym_reduce, 0 } };
  ndfunc_arg_out_t aout[1] = { { numo_cDFloat, 0 } };
  ndfunc_t ndf = { iter_bit_var, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
  VALUE reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
  VALUE v = na_ndloop(&ndf, 2, self, reduce);
  return rb_funcall(v, rb_intern("extract"), 0);
}

#whereNumo::Int32, Numo::Int64

Returns the array of index where the bit is one (true).

Returns:



2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
# File 'ext/numo/narray/src/t_bit.c', line 2874

static VALUE bit_where(VALUE self) {
  volatile VALUE idx_1;
  size_t size, n_1;
  where_opt_t* g;

  ndfunc_arg_in_t ain[1] = { { cT, 0 } };
  ndfunc_t ndf = { iter_bit_where, FULL_LOOP, 1, 0, ain, 0 };

  size = RNARRAY_SIZE(self);
  n_1 = NUM2SIZET(bit_count_true(0, NULL, self));
  g = ALLOCA_N(where_opt_t, 1);
  g->count = 0;
  if (size > 4294967295ul) {
    idx_1 = nary_new(numo_cInt64, 1, &n_1);
    g->elmsz = 8;
  } else {
    idx_1 = nary_new(numo_cInt32, 1, &n_1);
    g->elmsz = 4;
  }
  g->idx1 = na_get_pointer_for_write(idx_1);
  g->idx0 = NULL;
  na_ndloop3(&ndf, g, 1, self);
  na_release_lock(idx_1);
  return idx_1;
}

#where2Numo::Int32, Numo::Int64

Returns two index arrays. The first array contains index where the bit is one (true). The second array contains index where the bit is zero (false).

Returns *2.

Returns:



2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
# File 'ext/numo/narray/src/t_bit.c', line 2958

static VALUE bit_where2(VALUE self) {
  VALUE idx_1, idx_0;
  size_t size, n_1, n_0;
  where_opt_t* g;

  ndfunc_arg_in_t ain[1] = { { cT, 0 } };
  ndfunc_t ndf = { iter_bit_where2, FULL_LOOP, 1, 0, ain, 0 };

  size = RNARRAY_SIZE(self);
  n_1 = NUM2SIZET(bit_count_true(0, NULL, self));
  n_0 = size - n_1;
  g = ALLOCA_N(where_opt_t, 1);
  g->count = 0;
  if (size > 4294967295ul) {
    idx_1 = nary_new(numo_cInt64, 1, &n_1);
    idx_0 = nary_new(numo_cInt64, 1, &n_0);
    g->elmsz = 8;
  } else {
    idx_1 = nary_new(numo_cInt32, 1, &n_1);
    idx_0 = nary_new(numo_cInt32, 1, &n_0);
    g->elmsz = 4;
  }
  g->idx1 = na_get_pointer_for_write(idx_1);
  g->idx0 = na_get_pointer_for_write(idx_0);
  na_ndloop3(&ndf, g, 1, self);
  na_release_lock(idx_0);
  na_release_lock(idx_1);
  return rb_assoc_new(idx_1, idx_0);
}

#|(other) ⇒ Numo::NArray

Binary or.

Returns or of self and other.

Parameters:

Returns:



2107
2108
2109
2110
2111
2112
2113
# File 'ext/numo/narray/src/t_bit.c', line 2107

static VALUE bit_or(VALUE self, VALUE other) {
  ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
  ndfunc_arg_out_t aout[1] = { { cT, 0 } };
  ndfunc_t ndf = { iter_bit_or, FULL_LOOP, 2, 1, ain, aout };

  return na_ndloop(&ndf, 2, self, other);
}

#notNumo::Bit

Unary not.

Returns not of self.

Returns:



1849
1850
1851
1852
1853
1854
1855
# File 'ext/numo/narray/src/t_bit.c', line 1849

static VALUE bit_not(VALUE self) {
  ndfunc_arg_in_t ain[1] = { { cT, 0 } };
  ndfunc_arg_out_t aout[1] = { { cT, 0 } };
  ndfunc_t ndf = { iter_bit_not, FULL_LOOP, 1, 1, ain, aout };

  return na_ndloop(&ndf, 1, self);
}