Class: NumRu::VArrayComposite

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

Constant Summary

Constants inherited from VArray

NumRu::VArray::Binary_operators, NumRu::VArray::Binary_operatorsL, NumRu::VArray::Binary_operatorsL_comp, NumRu::VArray::Binary_operatorsL_other, NumRu::VArray::Binary_operators_Uconv, NumRu::VArray::Binary_operators_Unone, NumRu::VArray::Binary_operators_Uop, NumRu::VArray::Math_funcs, NumRu::VArray::Math_funcs_nondim, NumRu::VArray::Math_funcs_radian, NumRu::VArray::NArray_type1_methods, NumRu::VArray::NArray_type2_methods, NumRu::VArray::NArray_type2_methods1, NumRu::VArray::NArray_type2_methods2, NumRu::VArray::NArray_type2_methods3, NumRu::VArray::NArray_type3_methods, NumRu::VArray::Unary_operators

Instance Method Summary collapse

Methods inherited from VArray

#+@, #att_names, #attr_copy, #axis_cyclic?, #axis_cyclic_extendible?, #axis_draw_positive, #axis_modulo, #bin_mean, #bin_sum, #coerce, #convert_units, #copy, #del_att, #get_att, #long_name, #long_name=, #mapped?, #marshal_dump, #marshal_load, #name, new2, #rename!, #replace_val, #reshape, #reshape!, #sqrt, #to_g1D, #transpose, #units, #units=

Constructor Details

#initialize(varrays) ⇒ VArrayComposite

Returns a new instance of VArrayComposite.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
145
146
147
148
149
150
# File 'lib/numru/gphys/varraycomposite.rb', line 72

def initialize( varrays )

  if !varrays.is_a?(NArray) || varrays.typecode != NArray::OBJECT
  raise ArgumentError, "argument must be a NArray of VArray (or nil)"
  end

  nvas = varrays.shape
  vrank = nvas.length
  varrays.each{|va|
  if !va.nil?
    @first_vary = va
    @attr = @first_vary.attr_copy
    @name = @first_vary.name
    @crank = va.rank   # rank of the component VArrays
    break
  end
  }
  if vrank > @crank
    @rank = vrank       #=>  @rank > @crank
  else
  @rank = @crank
    if vrank < @crank
      (@crank - vrank).times{
        varrays = varrays.newdim(vrank)
        nvas.push(1)
      }
      vrank = @crank   # rank of varrays is increased to @crank (==@rank)
    end
  end
  un0 = varrays[0].units
  for i in 1...varrays.length
    va = varrays[i]
    varrays[i] = va.convert_units(un0) if va && va.units != un0
  end

  @bound_idx = Array.new     # will be Array of Array
  for dim in 0...vrank
  @bound_idx[dim] = [0]    # the frst element is always 0
  for i in 0...nvas[dim]
    idx = [true]*dim + [i..i,false]
    set=false
    len=0
    varrays[*idx].each{|va|
 if !set && va
          ### commented out for the duck typing:
   #if !va.is_a?(VArray)
          #  raise ArgumentError,"Not a VArray: #{va.inspect}"
   #end
          if dim < @crank
            len = va.shape_current[dim]
          else
            len = 1
          end
   @bound_idx[dim][i+1] = @bound_idx[dim][i] + len
   set=true
 elsif va
          if dim < @crank
            lc = va.shape_current[dim]
          else
            lc = 1
          end
   if lc != len
    raise ArgumentError,"Non-uniformity in the #{i}th element"+
               " of the #{dim}th dimension (#{va.shape_current[dim]} for #{len})"
   end
 end
    }
    if !set
 raise "No VArray is found in the #{i}th elem of the #{dim}th dim"
    end
  end
  end
  #< finish >
  @varrays = varrays.dup
  @shape = @bound_idx.collect{|a| a[-1]}
  @length = 1
  @shape.each{|l| @length *= l}
  # p @bound_idx, @varrays
end

Instance Method Details

#[](*slicer) ⇒ Object



232
233
234
235
236
237
238
239
240
# File 'lib/numru/gphys/varraycomposite.rb', line 232

def [](*slicer)
  slicer = __rubber_expansion(slicer)
  varrays = _div_idx(*slicer)
  if varrays.length == 1
  varrays[0]
  else
  VArrayComposite.new( varrays )
  end
end

#[]=(*args) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/numru/gphys/varraycomposite.rb', line 242

def []=(*args)
  args = __rubber_expansion(args)
  val = args.pop
  slicer = args
  slicer.collect!{|i| (i.is_a?(Numeric)) ? i..i : i }
  subva = self[*slicer]
  val = val.val if val.is_a?(VArray)
  subva.val= val
  val
end

#fileObject



264
265
266
# File 'lib/numru/gphys/varraycomposite.rb', line 264

def file
  @varrays.collect{|va| va.file}
end

#inspectObject



163
164
165
166
# File 'lib/numru/gphys/varraycomposite.rb', line 163

def inspect
  "<#{self.class.to_s} shape=#{shape.inspect} #_of_tiles=#{@bound_idx.collect{|b| b.length-1}.inspect}>"
  # "bounds=#{@bound_idx.collect{|b| b[1..-2]}.inspect}"
end

#lengthObject Also known as: total



175
176
177
# File 'lib/numru/gphys/varraycomposite.rb', line 175

def length
  @length
end

#name=(nm) ⇒ Object

Raises:

  • (ArgumentError)


268
269
270
271
272
273
# File 'lib/numru/gphys/varraycomposite.rb', line 268

def name=(nm)
  raise ArgumentError, "name should be a String" if ! nm.is_a?(String)
  @name = nm
  @varrays.each{|va| va.name=nm}
  nm
end

#ntypeObject

def ntype

  @first_vary.ntype
end


256
257
258
# File 'lib/numru/gphys/varraycomposite.rb', line 256

def ntype
  __ntype(typecode)
end

#rankObject



172
173
174
# File 'lib/numru/gphys/varraycomposite.rb', line 172

def rank
  @rank
end

#rename(nm) ⇒ Object



274
275
276
277
# File 'lib/numru/gphys/varraycomposite.rb', line 274

def rename(nm)
  raise "method rename is not vailable. (But rename! and name= are available)"
  ## self.dup.set_name_shallow(nm)
end

#set_att(name, val) ⇒ Object



154
155
156
157
158
159
# File 'lib/numru/gphys/varraycomposite.rb', line 154

def set_att(name,val)
  @varrays.each{|va|
  va.set_att(name,val)
  }
  self
end

#shapeObject Also known as: shape_current



168
169
170
# File 'lib/numru/gphys/varraycomposite.rb', line 168

def shape
  @shape
end

#typecodeObject



260
261
262
# File 'lib/numru/gphys/varraycomposite.rb', line 260

def typecode
  @first_vary.typecode
end

#valObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/numru/gphys/varraycomposite.rb', line 180

def val
  val = nil
  loop_multi_dim_index( @varrays.shape ){|index|
  vidx = Array.new
  for d in 0...@rank
      if d < @crank
        vidx[d] = (@bound_idx[d][index[d]])..(@bound_idx[d][index[d]+1]-1)
      else
        vidx[d] = @bound_idx[d][index[d]]
      end
  end
    units = @attr["units"]
    if units
      v = @varrays[*index].convert_units(units).val
    else
      v = @varrays[*index].val
    end
  if !val
    if NArray===v
 val = NArray.new(v.typecode,*@shape)
    elsif NArrayMiss===v
 val = NArrayMiss.new(v.typecode,*@shape)
    else
 raise TypeError, "Unexpected type #{v.class}"
    end
  end
  val[*vidx] = v
  }
  val
end

#val=(narray) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/numru/gphys/varraycomposite.rb', line 211

def val=(narray)
  loop_multi_dim_index( @varrays.shape ){|index|
  if narray.is_a?(Numeric)
    sub = narray
  else
    __check_ary_class(narray)
    vidx = Array.new
    for d in 0...@rank
        if d < @crank
          vidx[d] = (@bound_idx[d][index[d]])..(@bound_idx[d][index[d]+1]-1)
        else
          vidx[d] = @bound_idx[d][index[d]]
        end
    end
    sub=narray[*vidx]
  end
  @varrays[*index].val = sub
  }
  narray
end