Module: NumRu::IndexMixin

Included in:
Gtool3, Gtool3TimeVal
Defined in:
lib/numru/gphys/gtool3.rb

Instance Method Summary collapse

Instance Method Details

#gen_idx(fst, lst, stp, len) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/numru/gphys/gtool3.rb', line 30

def gen_idx(fst,lst,stp,len)
  fst = 0 if fst.nil?
  lst = len-1 if lst.nil?
  stp = 1 if stp.nil?
  lst += len if lst<0
  idx = Array.new
  fst.step(lst,stp){|i| idx.push(i)}
  idx
end

#range_negproc(rg, len) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/numru/gphys/gtool3.rb', line 146

def range_negproc(rg,len)
  if ( (f=rg.first) < 0 or rg.last < 0 )
    f += len if f < 0
    l += len if (l=rg.last) < 0
    if rg.exclude_end?
      f...l
    else
      f..l
    end
  else
    rg
  end
end

#rubber_expansion(args, rank) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/numru/gphys/gtool3.rb', line 160

def rubber_expansion( args, rank )
  if (id = args.index(false))  # substitution into id
    # false is incuded
    alen = args.length
    if args.rindex(false) != id
      raise ArguemntError,"only one rubber dimension is permitted"
    elsif alen > rank+1
      raise ArgumentError, "too many args"
    end
    ar = ( id!=0 ? args[0..id-1] : [] )
    args = ar + [true]*(rank-alen+1) + args[id+1..-1]
  end
  args
end

#slicer2idx(sl, len) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/numru/gphys/gtool3.rb', line 40

def slicer2idx(sl,len)
  sl = slicer_negproc(sl,len)
  case sl
  when NArray
    if sl.length == len and sl.typecode == NArray::BYTE
      sl.where
    else
      sl
    end
  when Array
    NArray[*sl]
  when true
    NArray.int(len).indgen!
  when Integer
    sl
  when Range
    NArray[*sl.to_a]
  when Hash
    rg = sl.first[0]
    fst = rg.first
    lenr = (rg.last - fst + (rg.exclude_end? ? 0 : 1))
    stp = sl.first[1]
    len = (lenr-1)/stp+1
    NArray.int(len).indgen!(fst,stp)
  else
    raise ArgumentError, "Unsupported slicer #{sl.inspect}"
  end
end

#slicer_len(sl, len) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/numru/gphys/gtool3.rb', line 96

def slicer_len(sl,len)
  sl = slicer_negproc(sl,len)
  case sl
  when NArray
    if sl.length == len and sl.typecode == NArray::BYTE
      sl.count_true
    else
      sl.length
    end
  when Array
    sl.length
  when true
    len
  when Integer
    1
  when Range
    sl.length
  when Hash
    sl.first[0].length / sl.first[1]
  else
    raise ArgumentError, "Unsupported slicer #{sl.inspect}"
  end
end

#slicer_minmax(sl, len) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/numru/gphys/gtool3.rb', line 69

def slicer_minmax(sl,len)
  sl = slicer_negproc(sl,len)
  if sl.is_a?(Array)
    sl = NArray[*sl]
  end
  case sl
  when NArray
    if sl.length == len and sl.typecode == NArray::BYTE
      w = sl.where
      [ w[0], w[-1] ]
    else
      [ sl.min, sl.max ]
    end
  when true
    [ 0, len-1 ]
  when Integer
    [sl, sl]
  when Range
    [ sl.first, (sl.exclude_end? ? sl.last-1 : sl.last ) ]
  when Hash
    sl = slicer2idx(sl,len)
    [ sl.min, sl.max ]
  else
    raise ArgumentError, "Unsupported slicer #{sl.inspect}"
  end
end

#slicer_negproc(sl, len) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/numru/gphys/gtool3.rb', line 120

def slicer_negproc(sl,len)
  case sl
  when NArray
    if sl.min < 0
      sls = sl.dup
      sls[sl.lt(0)] += len
      sls
    else
      sl
    end
  when Array
    sl.collect{|i| i>=0 ? i : i+len}
  when true
    (0...len).collect{|i| i}
  when Integer
    sl += len if sl<0
    sl
  when Range
    range_negproc(sl,len)
  when Hash
    { range_negproc(sl.first[0],len) => sl.first[1] }
  else
    raise ArgumentError, "Unsupported slicer #{sl.inspect}"
  end
end