Class: Numpy::NDArray

Inherits:
Object
  • Object
show all
Defined in:
lib/numpy/ndarray.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.narray_class_mapObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/numpy/ndarray.rb', line 46

def self.narray_class_map
  @narray_class_map ||= [
    [ Numpy.int64   , Numo::Int64 ],
    [ Numpy.int32   , Numo::Int32 ],
    [ Numpy.int16   , Numo::Int16 ],
    [ Numpy.int8    , Numo::Int8 ],
    [ Numpy.uint64  , Numo::Int64 ],
    [ Numpy.uint32  , Numo::Int32 ],
    [ Numpy.uint16  , Numo::Int16 ],
    [ Numpy.uint8   , Numo::Int8 ],
    [ Numpy.bool8   , Numo::UInt8 ],
    [ Numpy.float32 , Numo::SFloat ],
    [ Numpy.float64 , Numo::DFloat ],
    [ Numpy.object  , Numo::RObject ]
  ].freeze
end

Instance Method Details

#[](*index) ⇒ Object



6
7
8
# File 'lib/numpy/ndarray.rb', line 6

def [](*index)
  Conversion.to_ruby(super)
end

#to_aObject



10
11
12
# File 'lib/numpy/ndarray.rb', line 10

def to_a
  recursive_to_a(tolist)
end

#to_narrayObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/numpy/ndarray.rb', line 25

def to_narray
  load_numo_narray

  # Suppress warning from PyCall
  begin
    save, $VERBOSE = $VERBOSE, nil
    dtype = self.dtype
  ensure
    $VERBOSE = save
  end

  _, narray_klass = self.class.narray_class_map.find { |dt, kl| dtype == dt }
  unless narray_klass
    raise TypeError,
          "Unable to convert numpy's %p array to Numo::NArray" % dtype
  end

  # TODO: Use MemoryView feature
  narray_klass[*to_a]
end