Class: NumRu::SubsetMapping1D

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSubsetMapping1D

Returns a new instance of SubsetMapping1D.



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/numru/gphys/subsetmapping.rb', line 122

def initialize()
	 # To complete the initialization, use initialize_regular
	 # or initialize_irregular later.
	 @slicer = nil
	 @regular = nil
	 @collapsed = false
	 @first = nil
	 @length = nil
	 @step = nil
	 @index_array = nil
end

Instance Attribute Details

#collapsedObject (readonly)

Returns the value of attribute collapsed.



112
113
114
# File 'lib/numru/gphys/subsetmapping.rb', line 112

def collapsed
  @collapsed
end

#firstObject (readonly)

Returns the value of attribute first.



112
113
114
# File 'lib/numru/gphys/subsetmapping.rb', line 112

def first
  @first
end

#index_arrayObject (readonly)

Returns the value of attribute index_array.



112
113
114
# File 'lib/numru/gphys/subsetmapping.rb', line 112

def index_array
  @index_array
end

#regularObject (readonly)

Returns the value of attribute regular.



112
113
114
# File 'lib/numru/gphys/subsetmapping.rb', line 112

def regular
  @regular
end

#slicerObject (readonly)

Returns the value of attribute slicer.



112
113
114
# File 'lib/numru/gphys/subsetmapping.rb', line 112

def slicer
  @slicer
end

#stepObject (readonly)

Returns the value of attribute step.



112
113
114
# File 'lib/numru/gphys/subsetmapping.rb', line 112

def step
  @step
end

Class Method Details

.new(dim_len, slicer, no_collapse = false) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/numru/gphys/subsetmapping.rb', line 156

def SubsetMapping1D.new(dim_len, slicer, no_collapse=false)
	 case slicer
	 when Integer
	    idx = slicer
	    if idx < 0; idx += dim_len; end
	    if idx < 0 || idx >= dim_len
  raise "mapping #{slicer} is out of range (dim_len=#{dim_len})"
	    end
	    first=idx
	    length=step=1
	    collapsed = no_collapse ? false : true
	    SubsetMapping1D.new0.initialize_regular(slicer, collapsed, 
   first, length, step)
	 when Range, Hash, true
	    if (Range === slicer) 
  range = slicer
  step = 1
	    elsif (true === slicer) 
  range = 0..-1
  step = 1
	    else  # Hash
  if slicer.length != 1; raise "not 1-key-val Hash"; end
  range, step = slicer.to_a[0]
	    end
	    first = range.first
	    last = range.exclude_end? ? range.last-1 : range.last
	    if first < 0; first += dim_len; end
	    if last < 0; last += dim_len; end
	    if first<0 || first>=dim_len || last<0 || last>=dim_len || step==0
  raise "mapping #{slicer.inspect} is invalid (dim_len=#{dim_len})"
	    end
	    first = first
	    step = -step if ( (last-first)*step < 0 )
	    length = (last-first) / step + 1
	    SubsetMapping1D.new0.initialize_regular(slicer, false, 
   first, length, step)
	 else
	    index_array = NArray.int(1).coerce(slicer)[0]
	    if index_array.min < 0 || index_array.max >= dim_len
  raise "array index mapping out of range (dim_len=#{dim_len})"
	    end
	    slicer = index_array
	    SubsetMapping1D.new0.initialize_irregular(index_array)
	 end
end

.new0Object



120
# File 'lib/numru/gphys/subsetmapping.rb', line 120

alias  new0  new

Instance Method Details

#composite(mapping) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/numru/gphys/subsetmapping.rb', line 202

def composite(mapping)
	 # derive the composite mapping (mappig * self), where self is 
	 # applied first. ('*' here is meant to be a circle)
	 if @regular
	    if mapping.regular
  fst = @first + @step * mapping.first
  len = mapping.length
  if len > @length; raise "length too large"; end
  stp = @step * mapping.step
  slicer = __make_regular_slicer((scl=mapping.collapsed),fst,len,stp)
  SubsetMapping1D.new0.initialize_regular(slicer,scl,fst,len,stp)
	    else
  mida = mapping.index_array
  if  mida.min < 0 || mida.max >= @length
		  raise "array index mapping out of range"
  end
  idxary = @first + @step * mida
  SubsetMapping1D.new0.initialize_irregular(idxary)
	    end
	 else
	    if mapping.regular
  mida = mapping.first + 
  mapping.step * NArray.int(mapping.length).indgen!
	    else
  mida = mapping.index_array
	    end
	    if  mida.length < 0 || mida.max >= length
  raise "array index mapping out of range"
	    end
	    idxary = @index_array[mida]
	    if idxary.length==1
  fst = idxary[0]
  len = stp = 1
  slicer = __make_regular_slicer((scl=mapping.collapsed),fst,len,stp)
  SubsetMapping1D.new0.initialize_regular(slicer,scl,fst,len,stp)
	    else
 SubsetMapping1D.new0.initialize_irregular(idxary)
	    end
	 end
end

#imap(i) ⇒ Object

Raises:

  • (ArgumentError)


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/numru/gphys/subsetmapping.rb', line 259

def imap(i)
	 # inversely map an index value
	 raise ArgumentError, "Integer required" if ! i.is_a?(Integer)
	 if @regular
	    if ( (i-@first) % @step == 0 )
  imp = (i-@first)/@step
  if imp >= 0 && imp < @length
		  imp
  else
		  nil
  end
	    else
  nil     # not included
	    end
	 else
	    @index_array.index(i)  # returns nil if not included
	 end
end

#initialize_irregular(index_array) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/numru/gphys/subsetmapping.rb', line 134

def initialize_irregular(index_array)
	 @slicer = index_array
	 @regular = false
	 @collapsed = false
	 @first = nil
	 @length = nil
	 @step = nil
	 @index_array = index_array
	 self
end

#initialize_regular(slicer, collapsed, first, length, step) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/numru/gphys/subsetmapping.rb', line 145

def initialize_regular(slicer, collapsed, first, length, step)
	 @slicer = slicer
	 @regular = true
	 @collapsed = collapsed
	 @first = first
	 @length = length
	 @step = step
	 @index_array = nil
	 self
end

#lengthObject



243
244
245
# File 'lib/numru/gphys/subsetmapping.rb', line 243

def length
	 @length || @index_array.length
end

#map(i) ⇒ Object

Raises:

  • (ArgumentError)


247
248
249
250
251
252
253
254
255
256
257
# File 'lib/numru/gphys/subsetmapping.rb', line 247

def map(i)
	 # map an index value
	 # i: shoud be Integer or Array of Integer
	 raise ArgumentError, "Integer required" if ! i.is_a?(Integer)
	 if @regular
	    i += @length if i<0
	    @first + @step * i
	 else
	    @index_array[i]
	 end
end