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.



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

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.



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

def collapsed
  @collapsed
end

#firstObject (readonly)

Returns the value of attribute first.



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

def first
  @first
end

#index_arrayObject (readonly)

Returns the value of attribute index_array.



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

def index_array
  @index_array
end

#regularObject (readonly)

Returns the value of attribute regular.



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

def regular
  @regular
end

#slicerObject (readonly)

Returns the value of attribute slicer.



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

def slicer
  @slicer
end

#stepObject (readonly)

Returns the value of attribute step.



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

def step
  @step
end

Class Method Details

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



155
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
# File 'lib/numru/gphys/subsetmapping.rb', line 155

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



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

alias  new0  new

Instance Method Details

#composite(mapping) ⇒ Object



201
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
# File 'lib/numru/gphys/subsetmapping.rb', line 201

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)


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

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



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

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



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

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



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

def length
	 @length || @index_array.length
end

#map(i) ⇒ Object

Raises:

  • (ArgumentError)


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

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