Class: NArrayMiss

Inherits:
Object
  • Object
show all
Includes:
NMMath
Defined in:
lib/narray_miss/version.rb,
lib/narray_miss/narray_miss.rb,
lib/narray_miss/narray_miss.rb

Constant Summary collapse

VERSION =
"1.4.0"
BYTE =

Class Constants

— NArrayMiss::BYTE

type code for 1 byte unsigned integer.

— NArrayMiss::SINT

type code for 2 byte signed integer.

— NArrayMiss::LINT

type code for 4 byte signed integer.

— NArrayMiss::SFLOAT

type code for single precision float.

— NArrayMiss::FLOAT

type code for double precision float.

— NArrayMiss::SCOMPLEX

type code for single precision complex.

— NArrayMiss::COMPLEX

type code for double precision complex.

— NArrayMiss::OBJECT

type code for Ruby object.

go back to ((<Index>))

NARRAY::BYTE
SINT =
NARRAY::SINT
INT =
NARRAY::INT
LINT =
NARRAY::LINT
LLINT =
NARRAY::LINT
MINT =

int for mask operations

LINT
SFLOAT =
NARRAY::SFLOAT
FLOAT =
NARRAY::FLOAT
SCOMPLEX =
NARRAY::SCOMPLEX
COMPLEX =
NARRAY::COMPLEX
OBJECT =
NARRAY::OBJECT
@@llint =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*arg) ⇒ Object



198
199
200
# File 'lib/narray_miss/narray_miss.rb', line 198

def self.[](*arg)
  NArrayMiss.to_nam(NARRAY[*arg])
end

.__new__Object



116
# File 'lib/narray_miss/narray_miss.rb', line 116

alias :__new__ :new

._load(o) ⇒ Object



1298
1299
1300
1301
1302
1303
# File 'lib/narray_miss/narray_miss.rb', line 1298

def self._load(o)
  ary, mask = Marshal::load(o)
  ary = NARRAY._load(ary)
  mask = NARRAY._load(mask)
  NArrayMiss.to_nam_no_dup(ary,mask)
end

.byte(*arg) ⇒ Object



166
167
168
# File 'lib/narray_miss/narray_miss.rb', line 166

def self.byte(*arg)
  NArrayMiss.new(BYTE,*arg)
end

.complex(*arg) ⇒ Object



192
193
194
# File 'lib/narray_miss/narray_miss.rb', line 192

def self.complex(*arg)
  NArrayMiss.new(COMPLEX,*arg)
end

.float(*arg) ⇒ Object



186
187
188
# File 'lib/narray_miss/narray_miss.rb', line 186

def self.float(*arg)
  NArrayMiss.new(FLOAT,*arg)
end

.int(*arg) ⇒ Object



172
173
174
# File 'lib/narray_miss/narray_miss.rb', line 172

def self.int(*arg)
  NArrayMiss.new(INT,*arg)
end

.lint(*arg) ⇒ Object



175
176
177
# File 'lib/narray_miss/narray_miss.rb', line 175

def self.lint(*arg)
  NArrayMiss.new(LINT,*arg)
end

.llint(*arg) ⇒ Object



179
180
181
# File 'lib/narray_miss/narray_miss.rb', line 179

def self.llint(*arg)
  NArrayMiss.new(LLINT,*arg)
end

.new(*arg) ⇒ Object

Class Methods

— NArrayMiss.new(typecode, size, …)

create (({NArrayMiss})) of ((|typecode|)).
All elements are initialized with 0.

— NArrayMiss.byte(size, …)

same as NArrayMiss.new(NArrayMiss::BYTE, ((|size|)), ...).

— NArrayMiss.sint(size, …)

same as NArrayMiss.new(NArrayMiss::SINT, ((|size|)), ...).

— NArrayMiss.int(size, …)

same as NArrayMiss.new(NArrayMiss::LINT, ((|size|)), ...).

— NArrayMiss.sfloat(size, …)

same as NArrayMiss.new(NArrayMiss::SFLOAT, ((|size|)), ...).

— NArrayMiss.float(size, …)

same as NArrayMiss.new(NArrayMiss::FLOAT, ((|size|)), ...).

— NArrayMiss.scomplex(size, …)

same as NArrayMiss.new(NArrayMiss::SCOMPLEX, ((|size|)), ...).

— NArrayMiss.complex(size, …)

same as NArrayMiss.new(NArrayMiss::COMPLEX, ((|size|)), ...).

— NArrayMiss.object(size, …)

same as NArrayMiss.new(NArrayMiss::OBJECT, ((|size|)), ...).

— NArrayMiss[](value, …)

create (({NArrayMiss})) form [((|value|)), ...].

— NArrayMiss.to_nam(array [,mask])

create (({NArrayMiss})) from ((|array|)).
((|array|)) must be (({Numeric})) (({Array})) or (({NArray})).

— NArrayMiss.to_nam_no_dup(array [,mask])

convert from ((|array|)) to (({NArrayMiss})).

go back to ((<Index>))



161
162
163
164
165
# File 'lib/narray_miss/narray_miss.rb', line 161

def self.new(*arg)
  array = NARRAY.new(*arg)
  mask = NARRAY.byte(*arg[1..-1])
  __new__(array, mask)
end

.object(*arg) ⇒ Object



195
196
197
# File 'lib/narray_miss/narray_miss.rb', line 195

def self.object(*arg)
  NArrayMiss.new(OBJECT,*arg)
end

.scomplex(*arg) ⇒ Object



189
190
191
# File 'lib/narray_miss/narray_miss.rb', line 189

def self.scomplex(*arg)
  NArrayMiss.new(SCOMPLEX,*arg)
end

.sfloat(*arg) ⇒ Object



183
184
185
# File 'lib/narray_miss/narray_miss.rb', line 183

def self.sfloat(*arg)
  NArrayMiss.new(SFLOAT,*arg)
end

.sint(*arg) ⇒ Object



169
170
171
# File 'lib/narray_miss/narray_miss.rb', line 169

def self.sint(*arg)
  NArrayMiss.new(SINT,*arg)
end

.to_nam(*arg) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/narray_miss/narray_miss.rb', line 236

def self.to_nam(*arg)
  arg[0] = arg[0].dup if !(Numeric===arg[0])
  if arg.length==2 && !(Numeric===arg[1]) && arg[1].class!=TrueClass && arg[1].class!=FalseClass then
    arg[1] = arg[1].dup
  end
  NArrayMiss.to_nam_no_dup(*arg)
end

.to_nam_no_dup(*arg) ⇒ 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
# File 'lib/narray_miss/narray_miss.rb', line 201

def self.to_nam_no_dup(*arg)
  if arg.length > 2 || arg.length==0 then
    raise("NArrayMiss.to_nar( array [,mask]] )")
  end

  array = arg[0]
  if Numeric===array then array = NARRAY[array] end
  if Array===array then array = NARRAY.to_na(array) end
  unless array.is_a?(NARRAY) || /NArray/=~ array.class.to_s then
    raise("argument must be Numeric, NArray or Array")
  end

  if arg.length==2 then
    mask = arg[1]
    if Numeric===mask then mask = array.ne(mask) end
    if Array===mask then
	mask = array.class.to_na(mask).ne(0)
    end
    if mask.class == FalseClass then
	mask = array.class.byte(*array.shape)
    end
    if mask.class == TrueClass then
	mask = array.class.byte(*array.shape).fill(1)
    end
    if !(array.class===mask && mask.typecode==BYTE) then
 raise("mask must be Numeric, Array, true, false or NArray(byte)")
    end
    if mask.length!=array.length
      raise "mask.length must be same as array.length"
    end
  else
    mask = array.class.byte(*array.shape).fill(1)
  end
  __new__(array,mask)
end

Instance Method Details

#%(arg) ⇒ Object



444
445
446
# File 'lib/narray_miss/narray_miss.rb', line 444

def %(arg)
  binary_operation(arg, 1){|t1, t2| t1 % t2}
end

#&(arg) ⇒ Object



493
494
495
# File 'lib/narray_miss/narray_miss.rb', line 493

def &(arg)
  binary_operation(arg, 1){|t1, t2| t1 & t2}
end

#*(arg) ⇒ Object



438
439
440
# File 'lib/narray_miss/narray_miss.rb', line 438

def *(arg)
  binary_operation(arg, 1){|t1, t2| t1 * t2}
end

#**(arg) ⇒ Object



447
448
449
# File 'lib/narray_miss/narray_miss.rb', line 447

def **(arg)
  binary_operation(arg, 1){|t1, t2| t1 ** t2}
end

#+(arg) ⇒ Object



432
433
434
# File 'lib/narray_miss/narray_miss.rb', line 432

def +(arg)
  binary_operation(arg, 0){|t1, t2| t1 + t2}
end

#-(arg) ⇒ Object



435
436
437
# File 'lib/narray_miss/narray_miss.rb', line 435

def -(arg)
  binary_operation(arg, 0){|t1, t2| t1 - t2}
end

#-@Object

Arithmetic operator

— NArrayMiss#-@ — NArrayMiss#+(other) — NArrayMiss#-(other) — NArrayMiss#*(other) — NArrayMiss#/(other) — NArrayMiss#%(other) — NArrayMiss#**(other) — NArrayMiss#abs — NArrayMiss#add!(other) — NArrayMiss#sbt!(other) — NArrayMiss#mul!(other) — NArrayMiss#div!(other) — NArrayMiss#mod!(other) — NArrayMiss#mul_add(other, dim, …)



427
428
429
430
431
# File 'lib/narray_miss/narray_miss.rb', line 427

def -@
  array = @array.dup
  array[@mask] = -@array[@mask]
  NArrayMiss.to_nam_no_dup(array, @mask.dup)
end

#/(arg) ⇒ Object



441
442
443
# File 'lib/narray_miss/narray_miss.rb', line 441

def /(arg)
  binary_operation(arg, 1){|t1, t2| t1 / t2}
end

#==(arg) ⇒ Object



555
556
557
558
559
560
561
# File 'lib/narray_miss/narray_miss.rb', line 555

def ==(arg)
  if arg.kind_of?(NArrayMiss) then
    @mask==arg.get_mask! && @array[@mask]==arg.get_array![@mask]
  else
    false
  end
end

#[](*arg) ⇒ Object

Slicing Array

— NArrayMiss#[](index)

return the value at [((|index|))].
((|index|)) must be (({Integer, Range, Array, true})).
Index order is FORTRAN type.

— NArrayMiss#slice(index)

same as (({NArrayMiss#[]})) but keeps the rank of original array by not elimiting dimensions whose length became equal to 1 (which (({NArrayMiss#[]})) dose).
This is not the case with the 1-dimensional indexing and masking.

— NArrayMiss#set_without_validation(index,value)

replace elements at ((|index|)) by ((|value|)).

— NArrayMiss#[]=(index, value)

replace elements at ((|index|)) by ((|value|)) and
make replaced elements valid.


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/narray_miss/narray_miss.rb', line 310

def [](*arg)
  if arg[0].class == NArrayMiss && arg[0].typecode == BYTE
    obj = @array[arg[0].to_na(0)]
    if Numeric===obj
      return obj
    else
      return NArrayMiss.to_nam_no_dup(obj)
    end
  else
    obj = @array[*arg]
    if Numeric===obj
      return obj
    else
      return NArrayMiss.to_nam_no_dup(obj,@mask[*arg])
    end
  end
end

#[]=(*arg) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/narray_miss/narray_miss.rb', line 354

def []=(*arg)
  if arg.length == 2 && arg[0].class == NArrayMiss && arg[0].typecode == BYTE
    idx = arg[0].to_na(0)
    self.set_without_validation(idx,arg[-1])
    if arg[-1].class != NArrayMiss && arg[-1] then
      @mask[idx] = 1
    end
  else
    self.set_without_validation(*arg)
    if arg[-1].class != NArrayMiss && arg[-1] then
      if arg.length==1 then
        @mask=1
      else
        @mask[*arg[0..-2]] = 1
      end
    end
  end
  return self
end

#^(arg) ⇒ Object



499
500
501
# File 'lib/narray_miss/narray_miss.rb', line 499

def ^(arg)
  binary_operation(arg, 1){|t1, t2| t1 ^ t2}
end

#__clone__Object



1212
# File 'lib/narray_miss/narray_miss.rb', line 1212

alias __clone__ clone

#_dump(limit) ⇒ Object



1295
1296
1297
# File 'lib/narray_miss/narray_miss.rb', line 1295

def _dump(limit)
  Marshal::dump([@array._dump(nil),@mask._dump(nil)])
end

#absObject



451
452
453
454
455
# File 'lib/narray_miss/narray_miss.rb', line 451

def abs
  array = @array.dup
  array[@mask] = @array[@mask].abs
  NArrayMiss.to_nam_no_dup(array, @mask.dup)
end

#accum(*arg) ⇒ Object

Statistics

— NArrayMiss#sum(dim, … [“min_count”=>i])

return summation of elements in specified dimensions.
Elements at which the number of elements for summation is less than ((|i|)) is invalid.

— NArrayMiss#accum(dim, …)

same as (({NArrayMiss#sum})) but not elimiting dimensions whose length became equal to 1.

— NArrayMiss#min(dim, …)

return minimum in specified dimensions.
Elements at which the number of valid elements in the dimension is less than ((|i|)) is invalid.

— NArrayMiss#max(dim, …)

return maximum in specified dimensions.
Elements at which the number of valid elements in the dimension is less than ((|i|)) is invalid.

— NArrayMiss#median(dim, …)

return median in specified dimensions.
Elements at which the number of valid elements in the dimension is less than ((|i|)) is invalid.

— NArrayMiss#mean(dim, …)

return mean of elements in specified dimensions.
Elements at which the number of elements for then mean is less than ((|i|)) is invalid.

— NArrayMiss#stddev(dim, …)

return standard deviation of elements in specified dimensions.
Elements at which the number of elements for calculation the standard deviation is less than ((|i|)) is invalid.

— NArrayMiss#rms(dim, …)

return root mean square of elements in specified dimensions.
Elements at which the number of elements for calculation the RMS is less than ((|i|)) is invalid.

— NArrayMiss#rmsdev(dim, …)

return root mean square deviation of elements in specified dimensions.
Elements at which the number of elements for calculation the RMS deviation is less than ((|i|)) is invalid.


594
595
596
597
598
599
600
601
602
603
# File 'lib/narray_miss/narray_miss.rb', line 594

def accum(*arg)
  if @mask.count_true == 0 then
    return nil
  else
    array = @array.dup
    array[@mask.not] = 0
    return NArrayMiss.to_nam_no_dup(array.accum(*arg),
	       @mask.to_type(MINT).accum(*arg).ne(0))
  end
end

#add!(arg) ⇒ Object



457
458
459
# File 'lib/narray_miss/narray_miss.rb', line 457

def add!(arg)
  binary_operation(arg, 0){|t1, t2| t1.add!(t2)}
end

#all?Boolean

Returns:

  • (Boolean)


952
953
954
# File 'lib/narray_miss/narray_miss.rb', line 952

def all?
  @array[@mask].all?
end

#all_invalidObject



1092
1093
1094
1095
# File 'lib/narray_miss/narray_miss.rb', line 1092

def all_invalid
  @mask[true]=0
  self
end

#all_validObject



1088
1089
1090
1091
# File 'lib/narray_miss/narray_miss.rb', line 1088

def all_valid
  @mask[true]=1
  self
end

#all_valid?Boolean

Returns:

  • (Boolean)


1159
1160
1161
# File 'lib/narray_miss/narray_miss.rb', line 1159

def all_valid?
  @mask.all?
end

#and(arg) ⇒ Object



541
542
543
# File 'lib/narray_miss/narray_miss.rb', line 541

def and(arg)
  binary_operation(arg, 1){|t1, t2| t1.and t2}
end

#angleObject



991
992
993
# File 'lib/narray_miss/narray_miss.rb', line 991

def angle
  NArrayMiss.to_nam_no_dup(@array.angle,@mask)
end

#any?Boolean

Returns:

  • (Boolean)


955
956
957
# File 'lib/narray_miss/narray_miss.rb', line 955

def any?
  @array[@mask].any?
end

#any_valid?Boolean

Returns:

  • (Boolean)


1166
1167
1168
# File 'lib/narray_miss/narray_miss.rb', line 1166

def any_valid?
  @mask.any?
end

#ceilObject



831
832
833
# File 'lib/narray_miss/narray_miss.rb', line 831

def ceil
  NArrayMiss.to_nam_no_dup(@array.ceil, @mask.dup)
end

#cloneObject



1213
1214
1215
1216
1217
1218
# File 'lib/narray_miss/narray_miss.rb', line 1213

def clone
  obj = __clone__
  obj.set_array(@array.clone)
  obj.set_mask(@mask.clone)
  return obj
end

#coerce(x) ⇒ Object



1220
1221
1222
1223
1224
1225
1226
1227
1228
# File 'lib/narray_miss/narray_miss.rb', line 1220

def coerce(x)
  if Numeric===x then
    return [NArrayMiss.new(@array.class[x].typecode,*self.shape).fill(x),self]
  elsif x.class==Array || x.class==@array.class then
    return [NArrayMiss.to_nam(x), self]
  else
    raise("donnot know how to cange #{x.class} to NArrayMiss")
  end
end

#collect(&blk) ⇒ Object



904
905
906
# File 'lib/narray_miss/narray_miss.rb', line 904

def collect(&blk)
  self.dup.collect!(&blk)
end

#collect!Object



898
899
900
901
902
903
# File 'lib/narray_miss/narray_miss.rb', line 898

def collect!
  for i in 0..self.total-1
    self[i] = yield(self[i])
  end
  self
end

#complex?Boolean

Returns:

  • (Boolean)


1203
1204
1205
# File 'lib/narray_miss/narray_miss.rb', line 1203

def complex?
  @array.complex?
end

#conjObject



988
989
990
# File 'lib/narray_miss/narray_miss.rb', line 988

def conj
  NArrayMiss.to_nam_no_dup(@array.conj,@mask)
end

#count_falseObject

Boolean and mask related (only for byte, sint and int)

— NArrayMiss#count_false

return the number of elements whose value==0 and valid.

— NArrayMiss#count_true

return the number of elements whose value!=0 and valid.

— NArrayMiss#mask(mask)

return (({NArrayMiss#get_mask!&((|mask|))})).

— NArrayMiss#all?

return true if all the valid elements are not 0, else false.

— NArrayMiss#any?

return true if any valid element is not 0, else false.

— NArrayMiss#none?

return true if none of the valid elements is not 0, else false.

— NArrayMiss#where

return (({NArray})) of indices where valid elements are not 0.

— NArrayMiss#where2

return (({Array})) including two (({NArray}))s of indices,
where valid elements are not 0, and 0, respectively.


930
931
932
933
934
935
936
# File 'lib/narray_miss/narray_miss.rb', line 930

def count_false
  if @array.typecode==BYTE then
    return @array.count_false-@mask.count_false
  else
    raise("cannot count_true NArrayMiss except BYTE type")
  end
end

#count_invalid(*arg) ⇒ Object



1177
1178
1179
1180
1181
1182
1183
1184
# File 'lib/narray_miss/narray_miss.rb', line 1177

def count_invalid(*arg)
  if arg.length==0 then
    return @mask.count_false
  else
    return @array.class.int(*@mask.shape).fill(1).sum(*arg)-
    @mask.to_type(MINT).sum(*arg)
  end
end

#count_trueObject



937
938
939
940
941
942
943
# File 'lib/narray_miss/narray_miss.rb', line 937

def count_true
  if @array.typecode==BYTE then
    return (@array&@mask).count_true
  else
    raise("cannot count_true NArrayMiss except BYTE type")
  end
end

#count_valid(*arg) ⇒ Object



1170
1171
1172
1173
1174
1175
1176
# File 'lib/narray_miss/narray_miss.rb', line 1170

def count_valid(*arg)
  if arg.length==0 then
    return @mask.count_true
  else
    return @mask.to_type(MINT).sum(*arg)
  end    
end

#dimObject

NArrayMiss information

— NArrayMiss#dim

return the dimension which is the number of indices.

— NArrayMiss#rank

same as (({NArrayMiss#dim})).

— NArrayMiss#shape

return the (({Array})) of sizes of each index.

— NArrayMiss#size

return the number of total elements.

— NArrayMiss#total

alias to size

— NArrayMiss#length

alias to size

— NArrayMiss#rank_total

return the number of total of the shape.

— NArrayMiss#typecode

return the typecode.


270
271
272
# File 'lib/narray_miss/narray_miss.rb', line 270

def dim
  @array.dim
end

#div!(arg) ⇒ Object



466
467
468
# File 'lib/narray_miss/narray_miss.rb', line 466

def div!(arg)
  binary_operation(arg, 1){|t1, t2| t1.div!(t2)}
end

#dupObject



1208
1209
1210
# File 'lib/narray_miss/narray_miss.rb', line 1208

def dup
  NArrayMiss.to_nam(@array,@mask)
end

#eachObject

Iteration

— NArrayMiss#each{|x| …} — NArrayMiss#each_valid{|x| …} — NArrayMiss#each_valid_with_index{|x,i| …} — NArrayMiss#collect{|x| …} — NArrayMiss#collect{|x| …}



883
884
885
886
887
# File 'lib/narray_miss/narray_miss.rb', line 883

def each
  for i in 0..self.total-1
    yield(@array[i])
  end
end

#each_validObject



888
889
890
891
892
# File 'lib/narray_miss/narray_miss.rb', line 888

def each_valid
  for i in 0..self.total-1
    yield(@array[i]) if @mask[i]
  end
end

#each_valid_with_indexObject



893
894
895
896
897
# File 'lib/narray_miss/narray_miss.rb', line 893

def each_valid_with_index
  for i in 0..self.total-1
    yield(@array[i],i) if @mask[i]
  end
end

#eq(arg) ⇒ Object

Comparison

— NArrayMiss#eq(other) — NArrayMiss#ne(other) — NArrayMiss#gt(other) — NArrayMiss#ge(other) — NArrayMiss#lt(other) — NArrayMiss#le(other) — NArrayMiss#>(other) — NArrayMiss#>=(other) — NArrayMiss#<(other) — NArrayMiss#<=(other) — NArrayMiss#and(other) — NArrayMiss#or(other) — NArrayMiss#xor(other) — NArrayMiss#not(other)



522
523
524
# File 'lib/narray_miss/narray_miss.rb', line 522

def eq(arg)
  binary_operation(arg, 0){|t1, t2| t1.eq t2}
end

#floorObject

Type conversion

— NArrayMiss#floor

return (({NArrayMiss})) of integer whose elements processed (({floor})).

— NArrayMiss#ceil

return (({NArrayMiss})) of integer whose elements processed (({ceil})).

— NArrayMiss#round

return (({NArrayMiss})) of integer whose elements processed (({round})).

— NArrayMiss#to_i

return (({NArrayMiss})) of integer whose elements processed (({to_i})).

— NArrayMiss#to_f

return (({NArrayMiss})) of float whose elements processed (({to_f})).

— NArrayMiss#to_type(typecode)

return (({NArrayMiss})) of ((|typecode|)).

— NArrayMiss#to_a

convert (({NArrayMiss})) to (({Array})).

NArrayMiss#to_na!()

convert (({NArrayMiss})) to (({NArray})).
if there is argument, set missing_value for invalid elements.

NArrayMiss#to_na()

convert (({NArrayMiss})) to (({NArray})).
if there is argument, set missing_value for invalid elements.

— NArrayMiss#to_s

convert (({NArrayMiss})) to (({String})) as a binary data.

— NArrayMiss#to_string

create (({NArrayMiss})) of object whose elements are processed (({to_s}))


828
829
830
# File 'lib/narray_miss/narray_miss.rb', line 828

def floor
  NArrayMiss.to_nam_no_dup(@array.floor, @mask.dup)
end

#ge(arg) ⇒ Object



531
532
533
# File 'lib/narray_miss/narray_miss.rb', line 531

def ge(arg)
  binary_operation(arg, 0){|t1, t2| t1.ge t2}
end

#get_arrayObject



1137
1138
1139
# File 'lib/narray_miss/narray_miss.rb', line 1137

def get_array
  @array.dup
end

#get_array!Object



1134
1135
1136
# File 'lib/narray_miss/narray_miss.rb', line 1134

def get_array!
  @array
end

#get_maskObject



1131
1132
1133
# File 'lib/narray_miss/narray_miss.rb', line 1131

def get_mask
  @mask.dup
end

#get_mask!Object



1128
1129
1130
# File 'lib/narray_miss/narray_miss.rb', line 1128

def get_mask!
  @mask
end

#gt(arg) ⇒ Object



528
529
530
# File 'lib/narray_miss/narray_miss.rb', line 528

def gt(arg)
  binary_operation(arg, 0){|t1, t2| t1.gt t2}
end

#htonObject Also known as: ntoh



1022
1023
1024
# File 'lib/narray_miss/narray_miss.rb', line 1022

def hton
  NArrayMiss.to_nam(@array.hton,@mask.hton)
end

#htovObject Also known as: vtoh



1026
1027
1028
# File 'lib/narray_miss/narray_miss.rb', line 1026

def htov
  NArrayMiss.to_nam(@array.htov,@mask.htov)
end

#imObject



998
999
1000
# File 'lib/narray_miss/narray_miss.rb', line 998

def im
  NArrayMiss.to_nam_no_dup(@array.im,@mask)
end

#imagObject



985
986
987
# File 'lib/narray_miss/narray_miss.rb', line 985

def imag
  NArrayMiss.to_nam_no_dup(@array.imag,@mask)
end

#imag=(arg) ⇒ Object



994
995
996
997
# File 'lib/narray_miss/narray_miss.rb', line 994

def imag=(arg)
  @array.image=(arg)
  self
end

#inspectObject



1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
# File 'lib/narray_miss/narray_miss.rb', line 1231

def inspect
#    "array -> " + @array.inspect + "\nmask  -> " + @mask.inspect
  count_line = 0
  max_line = 10
  max_col = 80
  sep = ", "
  const = Hash.new
  @array.class.constants.each{|c| const[@array.class.const_get(c)] = c}
  str_ret = "NArrayMiss."+const[typecode].to_s.downcase+"("+shape.join(",")+"):"
  if rank == 0 then
    str_ret << " []"
    return str_ret
  else
     str_ret << "\n"
  end
  str = ""
  index = Array.new(rank,0)
  index[0] = true
  i = 1
  (rank-1).times{ str_ret << "[ " }
  while(true)
    i.times{ str_ret << "[ " }

    str = @array[*index].inspect
    ary = str[str.index("[")+1..str.index("]")-1].strip.split(/\s*,\s*/)
    miss = @mask[*index].where2[1]
    miss = miss[miss<ary.length].to_a
    if ary[-1]=="..." && miss[-1]==ary.length-1 then miss.pop end
    for j in miss
	ary[j] = "-"
    end
    while ( rank*4+ary.join(", ").length > max_col )
      ary.pop
      ary[-1] = "..."
    end
    str_ret << ary.join(", ")
    i = 1
    while (i<rank)
	if index[i]<shape[i]-1 then
 str_ret << " ]" << sep << "\n"
 count_line += 1
 index[i] += 1
 break
	else
 str_ret << " ]"
 index[i] = 0
 i += 1
	end
    end

    if i>=rank then
	str_ret << " ]"
	return str_ret
    elsif count_line>=max_line then
	str_ret << " ..."
	return str_ret
    end

    (rank-i).times{ str_ret << "  " }
  end
  return str_ret
end

#integer?Boolean

Others

— NArrayMiss#integer?

return true if (({NArrayMiss})) is byte, sint or int, else false.

— NArrayMiss#complex?

return true if (({NArrayMiss})) is scomplex or complex, else false.

— NArrayMiss#dup — NArrayMiss#coerce(object) — NArrayMiss#inspect

go back to ((<Index>))

Returns:

  • (Boolean)


1200
1201
1202
# File 'lib/narray_miss/narray_miss.rb', line 1200

def integer?
  @array.integer?
end

#le(arg) ⇒ Object



537
538
539
# File 'lib/narray_miss/narray_miss.rb', line 537

def le(arg)
  binary_operation(arg, 0){|t1, t2| t1.le t2}
end

#lt(arg) ⇒ Object



534
535
536
# File 'lib/narray_miss/narray_miss.rb', line 534

def lt(arg)
  binary_operation(arg, 0){|t1, t2| t1.lt t2}
end

#mask(arg) ⇒ Object



944
945
946
947
948
949
950
# File 'lib/narray_miss/narray_miss.rb', line 944

def mask(arg)
  obj = self.dup
  if arg.class==NArrayMiss then
    arg = arg.get_array!&arg.get_mask!
  end
  obj.set_mask(@mask&arg)
end

#max(*dims) ⇒ Object



624
625
626
627
628
629
630
631
632
633
# File 'lib/narray_miss/narray_miss.rb', line 624

def max(*dims)
  min_count = NArrayMiss.check_options(dims, 1)
  # 欠損値に最小値を入れて普通に max する
  # byte,sint,int,sfloat,float の MIN の値を入れるように変更すべき
  ary0 = @array.dup
  ary0[@mask.not] = @array.min
  NArrayMiss.reduction(@mask, rank, min_count, dims, false, typecode) do
    ary0.max(*dims)
  end
end

#mean(*dims) ⇒ Object



635
636
637
638
639
640
641
642
# File 'lib/narray_miss/narray_miss.rb', line 635

def mean(*dims)
  min_count = NArrayMiss.check_options(dims, 1)
  # 整数型の場合は浮動小数型へ変換
  ary0 = self.integer? ? self.to_type(@array.class.const_get(:DFLOAT)) : self
  NArrayMiss.reduction(@mask, rank, min_count, dims, true, typecode) do |count_sum, count_accum|
    ary0.sum(*dims)/count_sum
  end
end

#median(*arg) ⇒ Object



676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/narray_miss/narray_miss.rb', line 676

def median(*arg)
  if arg.length==0 then
    return @array[@mask].median
  else
    nshape = @array.class.to_na(@array.shape)
    nshape[arg]=1
    nslice = nshape[nshape.ne(1).where]
    index = @array.class.object(@mask.rank)
    index[nshape.eq(1).where] = true
    obj = NArrayMiss.new(@array.typecode,*nslice.to_a)
    total = 1
    nslice.each{|n| total *= n}
    for i in 0...total
      index[nshape.ne(1).where] = pos(i,nslice)
      mask = @array.class.byte(*@array.shape).fill(0)
      mask[*index] = 1
      mask = @mask&mask
      if mask.count_true != 0 then
        obj[*pos(i,nslice)] = @array[mask].median
      end
    end
    return obj
  end
end

#min(*dims) ⇒ Object



614
615
616
617
618
619
620
621
622
623
# File 'lib/narray_miss/narray_miss.rb', line 614

def min(*dims)
  min_count = NArrayMiss.check_options(dims, 1)
  # 欠損値に最大値を入れて普通に min する
  # byte,sint,int,sfloat,float の MAX の値を入れるように変更すべき
  ary0 = @array.dup
  ary0[@mask.not] = @array.max
  NArrayMiss.reduction(@mask, rank, min_count, dims, false, typecode) do
    ary0.min(*dims)
  end
end

#mod!(arg) ⇒ Object



469
470
471
# File 'lib/narray_miss/narray_miss.rb', line 469

def mod!(arg)
  binary_operation(arg, 1){|t1, t2| t1.mod!(t2)}
end

#mul!(arg) ⇒ Object



463
464
465
# File 'lib/narray_miss/narray_miss.rb', line 463

def mul!(arg)
  binary_operation(arg, 1){|t1, t2| t1.mul!(t2)}
end

#mul_add(*arg) ⇒ Object



473
474
475
476
477
478
479
# File 'lib/narray_miss/narray_miss.rb', line 473

def mul_add(*arg)
  if arg.length==1 then
    return (self*arg[0]).sum
  else
    return (self*arg[0]).sum(*arg[1..-1])
  end
end

#ne(arg) ⇒ Object



525
526
527
# File 'lib/narray_miss/narray_miss.rb', line 525

def ne(arg)
  binary_operation(arg, 0){|t1, t2| t1.ne t2}
end

#newdim(*arg) ⇒ Object Also known as: rewrank



793
794
795
796
# File 'lib/narray_miss/narray_miss.rb', line 793

def newdim(*arg)
  obj = self.dup
  obj.newdim!(*arg)
end

#newdim!(*arg) ⇒ Object Also known as: rewrank!, rewrank=



786
787
788
789
790
# File 'lib/narray_miss/narray_miss.rb', line 786

def newdim!(*arg)
  @array = @array.newdim!(*arg)
  @mask = @mask.newdim!(*arg)
  self
end

#none?Boolean

Returns:

  • (Boolean)


958
959
960
# File 'lib/narray_miss/narray_miss.rb', line 958

def none?
  @array[@mask].none?
end

#none_valid?Boolean Also known as: all_invalid?

Returns:

  • (Boolean)


1162
1163
1164
# File 'lib/narray_miss/narray_miss.rb', line 1162

def none_valid?
  @mask.none?
end

#notObject



551
552
553
# File 'lib/narray_miss/narray_miss.rb', line 551

def not
  NArrayMiss.to_nam_no_dup(@array.not, @mask.dup)
end

#or(arg) ⇒ Object



544
545
546
# File 'lib/narray_miss/narray_miss.rb', line 544

def or(arg)
  binary_operation(arg, 0){|t1, t2| t1.or t2}
end

#rankObject



273
274
275
# File 'lib/narray_miss/narray_miss.rb', line 273

def rank
  @array.rank
end

#rank_total(*arg) ⇒ Object



285
286
287
# File 'lib/narray_miss/narray_miss.rb', line 285

def rank_total(*arg)
  @array.rank_total(*arg)
end

#realObject

Complex compound number (only for scomplex and complex)

— NArrayMiss#real — NArrayMiss#imag — NArrayMiss#conj — NArrayMiss#angle — NArrayMiss#imag=(other) — NArrayMiss#im



982
983
984
# File 'lib/narray_miss/narray_miss.rb', line 982

def real
  NArrayMiss.to_nam_no_dup(@array.real,@mask)
end

#reshape(*arg) ⇒ Object



781
782
783
784
# File 'lib/narray_miss/narray_miss.rb', line 781

def reshape(*arg)
  obj = self.dup
  obj.reshape!(*arg)
end

#reshape!(*arg) ⇒ Object Also known as: shape=

Changing Shapes of indices

— NArrayMiss#reshape!(size, …)

change shape of array.

— NArrayMiss#reshape(size, …)

same as (({NArrayMiss#reshape!})) but create new object.

— NArrayMiss#shape=(size, …)

same as (({NArrayMiss#reshape!})).

— NArrayMiss#newdim!(dim)

insert new dimension with size=1

— NArrayMiss#newdim(dim)

same as (({NArrayMiss#newdim!})) but create new object.

— NArrayMiss#rewrank!(dim)

same as (({NArrayMiss#newdim!})).

— NArrayMiss#rewrank=(dim)

same as (({NArrayMiss#newdim!})).


776
777
778
779
780
# File 'lib/narray_miss/narray_miss.rb', line 776

def reshape!(*arg)
  @array = @array.reshape!(*arg)
  @mask = @mask.reshape!(*arg)
  self
end

#rms(*dims) ⇒ Object



654
655
656
657
658
659
660
661
662
663
# File 'lib/narray_miss/narray_miss.rb', line 654

def rms(*dims)
  min_count = NArrayMiss.check_options(dims, 1)
  # 整数型の場合は浮動小数型へ変換
  ary0 = self.integer? ? self.to_type(@array.class.const_get(:DFLOAT)) : self
  NArrayMiss.reduction(@mask, rank, min_count, dims, true, typecode) do |count_sum, count_accum|
    ary0 = ary0.abs if ary0.complex?
    ary0 = (ary0**2).sum(*dims) / count_sum
    NMMath.sqrt(ary0)
  end
end

#rmsdev(*dims) ⇒ Object



664
665
666
667
668
669
670
671
672
673
674
# File 'lib/narray_miss/narray_miss.rb', line 664

def rmsdev(*dims)
  min_count = NArrayMiss.check_options(dims, 1)
  # 整数型の場合は浮動小数型へ変換
  ary0 = self.integer? ? self.to_type(@array.class.const_get(:DFLOAT)) : self
  NArrayMiss.reduction(@mask, rank, min_count, dims, true, typecode) do |count_sum, count_accum|
    ary0 = ary0 - ary0.accum(*dims)/count_accum
    ary0 = ary0.abs if ary0.complex?
    ary0 = (ary0**2).sum(*dims) / count_sum
    NMMath.sqrt(ary0)
  end
end

#roundObject



834
835
836
# File 'lib/narray_miss/narray_miss.rb', line 834

def round
  NArrayMiss.to_nam_no_dup(@array.round, @mask.dup)
end

#sbt!(arg) ⇒ Object



460
461
462
# File 'lib/narray_miss/narray_miss.rb', line 460

def sbt!(arg)
  binary_operation(arg, 0){|t1, t2| t1.sbt!(t2)}
end

#set_invalid(*pos) ⇒ Object Also known as: invalidation



1083
1084
1085
1086
# File 'lib/narray_miss/narray_miss.rb', line 1083

def set_invalid(*pos)
  @mask[*pos] = 0
  self
end

#set_mask(mask) ⇒ Object



1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
# File 'lib/narray_miss/narray_miss.rb', line 1096

def set_mask(mask)
  if mask.class == Array then
    tmp = @array.class.byte(*@mask.shape)
    tmp[true] = mask
    mask = tmp
  end
  if mask.class == NArrayMiss then
    mask = mask.to_na(0)
  end
  if mask.class == @array.class then
    if mask.typecode != BYTE then
	raise("mask must be NArrayMiss.byte, NArray.byte or Array")
    end
    if @array.shape != mask.shape then
	raise("mask.shape must be same as array")
    end
    @mask = mask.dup
    return self
  else
    raise("mask must be NArray.byte or Array")
  end
end

#set_missing_value(val) ⇒ Object



1123
1124
1125
1126
# File 'lib/narray_miss/narray_miss.rb', line 1123

def set_missing_value(val)
  obj = self.dup
  obj.set_missing_value!(val)
end

#set_missing_value!(val) ⇒ Object



1119
1120
1121
1122
# File 'lib/narray_miss/narray_miss.rb', line 1119

def set_missing_value!(val)
  @array[@mask.not] = val
  self
end

#set_valid(*pos) ⇒ Object Also known as: validation

Mask and missing value

— NArrayMiss#set_valid(index)

validate element at ((|index|)).
((|index|)) must be (({Integer, Range, Array, or true})).

— NArrayMiss#validation(index)

alias to set_valid

— NArrayMiss#set_invalid(index)

invaliadate element at ((|index|)).
((|index|)) must be (({Integer, Range, Array, or true})).

— NArrayMiss#invalidation(index)

alias to set_invalid

— NArrayMiss#all_valid

set all elements valid

— NArrayMiss#all_invalid

set all elements invalid

— NArrayMiss#set_mask(mask)

masking by ((|mask|))

— NArrayMiss#set_missing_value(value)

replace invalid elements by ((|value|)).

— NArrayMiss#get_mask!

return (({NArray})) of byte as mask.

— NArrayMiss#get_mask

return (({NArray})) of byte as mask.

— NArrayMiss#get_array!

return (({NArray})) as data.

— NArrayMiss#get_array

return (({NArray})) as data.

— NArrayMiss#valid?(index)

return (({Array})) whose elements are true or false, 
or (({True}))/(({False})) corresponding to validity of the specified element(s) by the ((|index|))

— NArrayMiss#all_valid?

return true if all elements are valid, else false.

— NArrayMiss#none_valid?

return true if all elements are invalid, else false.

— NArrayMiss#all_invalid?

alias to none_valid?

— NArrayMiss#any_valid?

return true if any elements are valid, else false.

— NArrayMiss#count_valid

return the number of valid elements.

— NArrayMiss#count_invalid

return the number of invalid elements.


1078
1079
1080
1081
# File 'lib/narray_miss/narray_miss.rb', line 1078

def set_valid(*pos)
  @mask[*pos] = 1
  self
end

#set_without_validation(*arg) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/narray_miss/narray_miss.rb', line 331

def set_without_validation(*arg)
  if arg.length==1 then
    if !arg[0] then
	@mask[] = 0
    elsif arg[0].class == NArrayMiss then
	@array[] = arg[0].get_array!
	@mask[] = arg[0].get_mask!
    else
	@array[] = arg[0]
    end
  else
    if !arg[-1] then
	@mask[*arg[0..-2]] = 0
    elsif arg[-1].class == NArrayMiss then
	@array[*arg[0..-2]] = arg[-1].get_array!
	@mask[*arg[0..-2]] = arg[-1].get_mask!
    else
	@array[*arg[0..-2]] = arg[-1]
    end
  end
  return self
end

#shapeObject



276
277
278
# File 'lib/narray_miss/narray_miss.rb', line 276

def shape
  @array.shape
end

#sizeObject Also known as: total, length



279
280
281
# File 'lib/narray_miss/narray_miss.rb', line 279

def size
  @array.size
end

#slice(*arg) ⇒ Object



327
328
329
# File 'lib/narray_miss/narray_miss.rb', line 327

def slice(*arg)
  NArrayMiss.to_nam_no_dup(@array.slice(*arg),@mask.slice(*arg))
end

#stddev(*dims) ⇒ Object



643
644
645
646
647
648
649
650
651
652
653
# File 'lib/narray_miss/narray_miss.rb', line 643

def stddev(*dims)
  min_count = NArrayMiss.check_options(dims, 2)
  # 整数型の場合は浮動小数型へ変換
  ary0 = self.integer? ? self.to_type(@array.class.const_get(:DFLOAT)) : self
  NArrayMiss.reduction(@mask, rank, min_count, dims, true, typecode) do |count_sum, count_accum|
    ary0 = ary0 - ary0.accum(*dims)/count_accum
    ary0 = ary0.abs if ary0.complex?
    ary0 = (ary0**2).sum(*dims) / (count_sum-1)
    NMMath.sqrt(ary0)
  end
end

#sum(*dims) ⇒ Object



605
606
607
608
609
610
611
612
613
# File 'lib/narray_miss/narray_miss.rb', line 605

def sum(*dims)
  min_count = NArrayMiss.check_options(dims, 1)
  # 欠損値に 0 を入れて普通に sum する
  ary0 = @array.dup
  ary0[@mask.not] = 0
  NArrayMiss.reduction(@mask, rank, min_count, dims, false, typecode) do
    ary0.sum(*dims)
  end
end

#swap_byteObject

Byte swap

— NArrayMiss#swap_byte

swap byte order.

— NArrayMiss#hton

convert to network byte order.

— NArrayMiss#ntoh

convert from network byte order.

— NArrayMiss#htov

convert to VAX byte order.

— NArrayMiss#vtoh

convert from VAX byte order.


1017
1018
1019
1020
1021
# File 'lib/narray_miss/narray_miss.rb', line 1017

def swap_byte
  obj = self.dup
  obj.set_without_validation(@array.swap_byte)
  obj
end

#to_aObject



847
848
849
# File 'lib/narray_miss/narray_miss.rb', line 847

def to_a
  @array.to_a
end

#to_fObject



840
841
842
# File 'lib/narray_miss/narray_miss.rb', line 840

def to_f
  NArrayMiss.to_nam_no_dup(@array.to_f, @mask.dup)
end

#to_iObject



837
838
839
# File 'lib/narray_miss/narray_miss.rb', line 837

def to_i
  NArrayMiss.to_nam_no_dup(@array.to_i, @mask.dup)
end

#to_na(*arg) ⇒ Object



860
861
862
# File 'lib/narray_miss/narray_miss.rb', line 860

def to_na(*arg)
  return self.dup.to_na!(*arg)
end

#to_na!(*arg) ⇒ Object



850
851
852
853
854
855
856
857
858
859
# File 'lib/narray_miss/narray_miss.rb', line 850

def to_na!(*arg)
  if arg.length==0
    return @array
  elsif arg.length==1 then
    self.set_missing_value!(arg[0])
    return @array
  else
    raise(ArgumentError, "Usage: NArrayMiss#to_na([missing_value])")
  end
end

#to_sObject



863
864
865
# File 'lib/narray_miss/narray_miss.rb', line 863

def to_s
  @array.to_s
end

#to_stringObject



866
867
868
869
870
871
# File 'lib/narray_miss/narray_miss.rb', line 866

def to_string
  obj = NArrayMiss.obj(*@array.shape)
  obj.set_without_validation( @array.to_string )
  obh.set_mask(@mask)
  obj
end

#to_type(typecode) ⇒ Object



844
845
846
# File 'lib/narray_miss/narray_miss.rb', line 844

def to_type(typecode)
  NArrayMiss.to_nam_no_dup(@array.to_type(typecode), @mask.dup)
end

#transpose(*arg) ⇒ Object

Transpose

— NArrayMiss#transpose(dim0, dim1, …)

transpose array. The 0-th dimension goes to the ((|dim0|))-th dimension of new array.


748
749
750
751
752
753
754
755
# File 'lib/narray_miss/narray_miss.rb', line 748

def transpose(*arg)
  obj = self.dup
  shape = arg.collect{|i| obj.shape[i]}
  obj.reshape!(*shape)
  obj.set_without_validation( @array.transpose(*arg) )
  obj.set_mask(@mask.transpose(*arg))
  obj
end

#typecodeObject



289
290
291
# File 'lib/narray_miss/narray_miss.rb', line 289

def typecode
  @array.typecode
end

#valid?(*arg) ⇒ Boolean

Returns:

  • (Boolean)


1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
# File 'lib/narray_miss/narray_miss.rb', line 1141

def valid?(*arg)
  if arg.any?
    # For the subset specified by the argument(s) (in the same way as for []).
    # Returns true or false if a single value is specified.
    # Otherwise, returns an Array of true of false
    mask = @mask[*arg]
    if mask.is_a?(Numeric)
      return mask == 1  # true if mask (==1); false if not
    end
  else
    # no argument
    mask = @mask
  end
  ary = mask.to_a
  ary.flatten!
  ary.map!{|i| i==1}  # true if element == 1; false if not
  return ary
end

#whereObject



962
963
964
# File 'lib/narray_miss/narray_miss.rb', line 962

def where
  (@array&@mask).where
end

#where2Object



965
966
967
# File 'lib/narray_miss/narray_miss.rb', line 965

def where2
  self.where-@mask.where
end

#xor(arg) ⇒ Object



547
548
549
# File 'lib/narray_miss/narray_miss.rb', line 547

def xor(arg)
  binary_operation(arg, 1){|t1, t2| t1.xor t2}
end

#|(arg) ⇒ Object



496
497
498
# File 'lib/narray_miss/narray_miss.rb', line 496

def |(arg)
  binary_operation(arg, 0){|t1, t2| t1 | t2}
end

#~@Object

Bitwise operator (only for byte, sint and int)

— NArrayMiss#~@ — NArrayMiss#&(other) — NArrayMiss#|(other) — NArrayMiss#^(other)



490
491
492
# File 'lib/narray_miss/narray_miss.rb', line 490

def ~@
  NArrayMiss.to_nam_to_dup(~@array, @mask.dup)
end