Class: NumRu::SubsetMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/numru/gphys/subsetmapping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shape, slicer, mapping1Ds = nil) ⇒ SubsetMapping

Returns a new instance of SubsetMapping.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/numru/gphys/subsetmapping.rb', line 7

def initialize(shape, slicer, mapping1Ds=nil)
   #USAGE:
   #   SubsetMapping.new(shape, slicer)
   # or 
   #   SubsetMapping.new(nil, nil, mapping1Ds)
   #
   # shape [Array]: shape of the array onto which mapping is made
   # slicer [Array]: mapping specification for each dim.
   #   (shape and slicer are used to create SubsetMapping1D's)
   # mapping1Ds [Array]: collected SubsetMapping1D's
   #
   # NOTE shape.length must be equal to mappings.length
   if mapping1Ds
      @maps = mapping1Ds
      len = mapping1Ds.length
   else
      len=shape.length

      if slicer.length == 0
  @maps = Array.new
  for i in 0...len do
      @maps[i] = SubsetMapping1D.new(shape[i],0..-1)
  end
      else
  if len != slicer.length
      raise "lengths of shape and slicer do not agree (#{len} vs #{slicer.length})"
  end
  @maps = Array.new
  for i in 0...len do
      @maps[i] = SubsetMapping1D.new(shape[i],slicer[i])
  end
      end
   end
   @dims_retained = Array.new
   @dims_collapsed = Array.new
   for i in 0...len do
     @maps[i].collapsed ? @dims_collapsed.push(i) : @dims_retained.push(i)
   end
   @rank = @dims_retained.length
   @shape = Array.new
   @maps.each{|mp| @shape.push(mp.length) if ! mp.collapsed}
end

Instance Attribute Details

#rankObject (readonly)

Returns the value of attribute rank.



50
51
52
# File 'lib/numru/gphys/subsetmapping.rb', line 50

def rank
  @rank
end

#shapeObject (readonly)

Returns the value of attribute shape.



50
51
52
# File 'lib/numru/gphys/subsetmapping.rb', line 50

def shape
  @shape
end

Instance Method Details

#composite(other) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/numru/gphys/subsetmapping.rb', line 64

def composite( other )
   if @rank != (other.maps.length)
      raise ArgumentError, "the original rank (#{other.maps.length}) must agree with the rank of this mapping (#{@rank})"
   end
   newmap = Array.new()
   for i in 0...@rank do
      id = @dims_retained[i]
      newmap[id] = @maps[id].composite( other.maps[i] )
   end
   @dims_collapsed.each{ |i| newmap[i] = @maps[i] }
   SubsetMapping.new(nil,nil,newmap)
end

#imap(*indices_mapped) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/numru/gphys/subsetmapping.rb', line 90

def imap( *indices_mapped )
   # inverse operation of the map method
   if @maps.length != (indices_mapped.length)
      raise ArgumentError, "number of arguments (#{indies_mapped.length}) must agree with the rank of the array mapped to (#{@maps.length})"
   end
   work = Array.new
   indices_mapped.each_index{|i|
      work[i] = @maps[i].imap( indices_mapped[i] )
   }
   if work.include?(nil)
      indices_sub = nil
   else
#     indices_sub = work.indices( *@dims_retained )  # indices: obsolete in ruby 1.8
      indices_sub = work.values_at( *@dims_retained )
   end
   indices_sub
end

#map(*indices_sub) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/numru/gphys/subsetmapping.rb', line 77

def map( *indices_sub )
   # map an index to an element in the subset to that in the original
   if @rank != (indices_sub.length)
      raise ArgumentError, "number of arguments (#{indices_sub.length}) must agree with the rank of this mapping (#{@rank})"
   end
   indices_mapped = Array.new
   for i in 0...@rank do
      id = @dims_retained[i]
      indices_mapped[id] = @maps[id].map(indices_sub[i])
   end
   @dims_collapsed.each{ |i| indices_mapped[i] = @maps[i].first }
   indices_mapped
end

#slicerObject



60
61
62
# File 'lib/numru/gphys/subsetmapping.rb', line 60

def slicer
   @maps.collect{|mp| mp.slicer}
end

#totalObject Also known as: length



53
54
55
56
57
# File 'lib/numru/gphys/subsetmapping.rb', line 53

def total
   tt=1
   @shape.each{|i| tt *= i}
   tt
end