narray_miss 1.2.1.1 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in narray_miss.gemspec
4
+ gemspec
@@ -0,0 +1,34 @@
1
+ NArrayMiss is copyrighted free software by Seiya Nishizawa and GFD
2
+ Dennou Club (http://www.gfd-dennou.org/).
3
+
4
+ Copyright 2001 (C) Seiya Nishizawa and GFD Dennou Club
5
+ (http://www.gfd-dennou.org/) All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions are
9
+ met:
10
+
11
+ 1. Redistributions of source code must retain the above copyright
12
+ notice, this list of conditions and the following disclaimer.
13
+
14
+ 2. Redistributions in binary form must reproduce the above copyright
15
+ notice, this list of conditions and the following disclaimer in
16
+ the documentation and/or other materials provided with the
17
+ distribution.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY GFD DENNOU CLUB AND CONTRIBUTORS ``AS IS''
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GFD DENNOU CLUB OR
23
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ The views and conclusions contained in the software and documentation
32
+ are those of the authors and should not be interpreted as representing
33
+ official policies, either expressed or implied, of Seiya Nishizawa
34
+ and GFD Dennou Club.
@@ -0,0 +1,40 @@
1
+ = What's NArrayMiss
2
+
3
+ NArrayMiss is an additional Ruby class with processing of missing value
4
+ to NArray which is a numerical multi-dimensional array Ruby class.
5
+
6
+
7
+ = NArrayMiss home-page
8
+
9
+ The URL of the NArrayMiss home-page is:
10
+ http://ruby.gfd-dennou.org/products/narray_miss/
11
+
12
+
13
+ = Requires
14
+
15
+ * Ruby (http://www.ruby-lang.org/)
16
+ * NArray (http://narray.rubyforge.org/index.html.en)
17
+
18
+
19
+ = Install
20
+
21
+ # gem install narray_miss
22
+
23
+
24
+ = Copying
25
+
26
+ See the file LICENSE.txt.
27
+
28
+
29
+ = Usage
30
+
31
+ To use this library, put the following in your script.
32
+ require 'narray_miss'
33
+
34
+
35
+ = The Author
36
+
37
+ Feel free to send comments and bug reports to the author.
38
+ The author's e-mail addess is
39
+
40
+ seiya@gfd-dennou.org
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new
5
+ task :default => :test
@@ -1,1423 +1,2 @@
1
- =begin
2
- = NArrayMiss Class
3
-
4
- NArrayMiss is a additional class processing of missing value with to
5
- ((<NArray|URL:http://www.ruby-lang.org/en/raa-list.rhtml?name=NArray>))
6
- for Ruby.
7
-
8
- To use NArrayMiss class, you need invoking "require 'narray_miss.rb'" in your script.
9
-
10
- == Index
11
-
12
- * ((<Class Constants>))
13
- * ((<Class Methods>))
14
- * ((<Class Instance Methods>))
15
- * ((<NArrayMiss information>))
16
- * ((<Slicing Array>))
17
- * ((<Filling values>))
18
- * ((<Arithmetic operator>))
19
- * ((<Bitwise operator|Bitwise operator (only for byte, sint and int)>))
20
- * ((<Comparison>))
21
- * ((<Statistics>))
22
- * ((<Sort>))
23
- * ((<Transpose>))
24
- * ((<Changing Shapes of indices>))
25
- * ((<Type conversion>))
26
- * ((<Iteration>))
27
- * ((<Boolean and mask related|Boolean and mask related (only for byte, sint and int)>))
28
- * ((<Complex compound number|Complex compound number (only for scomplex and complex)>))
29
- * ((<Byte swap>))
30
- * ((<Mask and missing value>))
31
- * ((<Others>))
32
-
33
-
34
- =end
35
-
36
- require 'narray'
37
-
38
-
39
- class NArrayMiss
40
-
41
- =begin
42
- == Class Constants
43
- --- NArrayMiss::BYTE
44
- type code for 1 byte unsigned integer.
45
- --- NArrayMiss::SINT
46
- type code for 2 byte signed integer.
47
- --- NArrayMiss::INT
48
- type code for 4 byte signed integer.
49
- --- NArrayMiss::SFLOAT
50
- type code for single precision float.
51
- --- NArrayMiss::FLOAT
52
- type code for double precision float.
53
- --- NArrayMiss::SCOMPLEX
54
- type code for single precision complex.
55
- --- NArrayMiss::COMPLEX
56
- type code for double precision complex.
57
- --- NArrayMiss::OBJECT
58
- type code for Ruby object.
59
-
60
- go back to ((<Index>))
61
- =end
62
-
63
- BYTE = NArray::BYTE
64
- SINT = NArray::SINT
65
- INT = NArray::INT
66
- SFLOAT = NArray::SFLOAT
67
- FLOAT = NArray::FLOAT
68
- SCOMPLEX = NArray::SCOMPLEX
69
- COMPLEX = NArray::COMPLEX
70
- OBJECT = NArray::OBJECT
71
-
72
- class << self
73
- alias :__new__ :new
74
- private :__new__
75
- end
76
-
77
- def initialize(array, mask)
78
- if array.shape!=mask.shape
79
- raise "array and mask must have the same shape"
80
- end
81
- @array = array
82
- @mask = mask
83
- end
84
- private :initialize
85
-
86
- =begin
87
- == Class Methods
88
- --- NArrayMiss.new(typecode, size, ...)
89
- create (({NArrayMiss})) of ((|typecode|)).
90
- All elements are initialized with 0.
91
- --- NArrayMiss.byte(size, ...)
92
- same as NArrayMiss.new(NArrayMiss::BYTE, ((|size|)), ...).
93
- --- NArrayMiss.sint(size, ...)
94
- same as NArrayMiss.new(NArrayMiss::SINT, ((|size|)), ...).
95
- --- NArrayMiss.int(size, ...)
96
- same as NArrayMiss.new(NArrayMiss::INT, ((|size|)), ...).
97
- --- NArrayMiss.sfloat(size, ...)
98
- same as NArrayMiss.new(NArrayMiss::SFLOAT, ((|size|)), ...).
99
- --- NArrayMiss.float(size, ...)
100
- same as NArrayMiss.new(NArrayMiss::FLOAT, ((|size|)), ...).
101
- --- NArrayMiss.scomplex(size, ...)
102
- same as NArrayMiss.new(NArrayMiss::SCOMPLEX, ((|size|)), ...).
103
- --- NArrayMiss.complex(size, ...)
104
- same as NArrayMiss.new(NArrayMiss::COMPLEX, ((|size|)), ...).
105
- --- NArrayMiss.object(size, ...)
106
- same as NArrayMiss.new(NArrayMiss::OBJECT, ((|size|)), ...).
107
- --- NArrayMiss[](value, ...)
108
- create (({NArrayMiss})) form [((|value|)), ...].
109
- --- NArrayMiss.to_nam(array [,mask])
110
- create (({NArrayMiss})) from ((|array|)).
111
- ((|array|)) must be (({Numeric})) (({Array})) or (({NArray})).
112
- --- NArrayMiss.to_nam_no_dup(array [,mask])
113
- convert from ((|array|)) to (({NArrayMiss})).
114
-
115
- go back to ((<Index>))
116
- =end
117
-
118
- def self.new(*arg)
119
- array = NArray.new(*arg)
120
- mask = NArray.byte(*arg[1..-1])
121
- __new__(array, mask)
122
- end
123
- def self.byte(*arg)
124
- NArrayMiss.new(BYTE,*arg)
125
- end
126
- def self.sint(*arg)
127
- NArrayMiss.new(SINT,*arg)
128
- end
129
- def self.int(*arg)
130
- NArrayMiss.new(INT,*arg)
131
- end
132
- def self.sfloat(*arg)
133
- NArrayMiss.new(SFLOAT,*arg)
134
- end
135
- def self.float(*arg)
136
- NArrayMiss.new(FLOAT,*arg)
137
- end
138
- def self.scomplex(*arg)
139
- NArrayMiss.new(SCOMPLEX,*arg)
140
- end
141
- def self.complex(*arg)
142
- NArrayMiss.new(COMPLEX,*arg)
143
- end
144
- def self.object(*arg)
145
- NArrayMiss.new(OBJECT,*arg)
146
- end
147
- def self.[](*arg)
148
- NArrayMiss.to_nam(NArray[*arg])
149
- end
150
- def self.to_nam_no_dup(*arg)
151
- if arg.length > 2 || arg.length==0 then
152
- raise("NArrayMiss.to_nar( array [,mask]] )")
153
- end
154
-
155
- array = arg[0]
156
- if Numeric===array then array = NArray[array] end
157
- if Array===array then array = NArray.to_na(array) end
158
- if !array.is_a?(NArray) then
159
- raise("argument must be Numeric, NArray or Array")
160
- end
161
-
162
- if arg.length==2 then
163
- mask = arg[1]
164
- if Numeric===mask then mask = array.ne(mask) end
165
- if Array===mask then
166
- mask = NArray.to_na(mask).ne(0)
167
- end
168
- if mask.class == FalseClass then
169
- mask = NArray.byte(*array.shape)
170
- end
171
- if mask.class == TrueClass then
172
- mask = NArray.byte(*array.shape).fill(1)
173
- end
174
- if !(NArray===mask && mask.typecode==BYTE) then
175
- raise("mask must be Numeric, Array, true, false or NArray(byte)")
176
- end
177
- if mask.length!=array.length
178
- raise "mask.length must be same as array.length"
179
- end
180
- else
181
- mask = NArray.byte(*array.shape).fill(1)
182
- end
183
- __new__(array,mask)
184
- end
185
- def self.to_nam(*arg)
186
- if !(Numeric===arg[0]) && !(Array===arg[0]) && !arg[0].is_a?(NArray)
187
- raise "first argument must be Numeric, NArray or Array"
188
- end
189
- arg[0] = arg[0].dup if !(Numeric===arg[0])
190
- if arg.length==2 && !(Numeric===arg[1]) && arg[1].class!=TrueClass && arg[1].class!=FalseClass then
191
- arg[1] = arg[1].dup
192
- end
193
- NArrayMiss.to_nam_no_dup(*arg)
194
- end
195
-
196
-
197
- =begin
198
- == Class Instance Methods
199
- =end
200
-
201
-
202
- =begin
203
- === NArrayMiss information
204
- --- NArrayMiss#dim
205
- return the dimension which is the number of indices.
206
- --- NArrayMiss#rank
207
- same as (({NArrayMiss#dim})).
208
- --- NArrayMiss#shape
209
- return the (({Array})) of sizes of each index.
210
- --- NArrayMiss#size
211
- return the number of total elements.
212
- --- NArrayMiss#total
213
- alias to size
214
- --- NArrayMiss#length
215
- alias to size
216
- --- NArrayMiss#rank_total
217
- return the number of total of the shape.
218
- --- NArrayMiss#typecode
219
- return the typecode.
220
- =end
221
-
222
- def dim
223
- @array.dim
224
- end
225
- def rank
226
- @array.rank
227
- end
228
- def shape
229
- @array.shape
230
- end
231
- def size
232
- @array.size
233
- end
234
- alias :total :size
235
- alias :length :size
236
-
237
- def rank_total(*arg)
238
- @array.rank_total(*arg)
239
- end
240
-
241
- def typecode
242
- @array.typecode
243
- end
244
-
245
-
246
- =begin
247
- === Slicing Array
248
- --- NArrayMiss#[](index)
249
- return the value at [((|index|))].
250
- ((|index|)) must be (({Integer, Range, Array, true})).
251
- Index order is FORTRAN type.
252
- --- NArrayMiss#slice(index)
253
- same as (({NArrayMiss#[]})) but keeps the rank of original array by not elimiting dimensions whose length became equal to 1 (which (({NArrayMiss#[]})) dose).
254
- This is not the case with the 1-dimensional indexing and masking.
255
- --- NArrayMiss#set_without_validation(index,value)
256
- replace elements at ((|index|)) by ((|value|)).
257
- --- NArrayMiss#[]=(index, value)
258
- replace elements at ((|index|)) by ((|value|)) and
259
- make replaced elements valid.
260
- =end
261
-
262
- def [](*arg)
263
- if arg[0].class == NArrayMiss && arg[0].typecode == BYTE
264
- obj = @array[arg[0].to_na(0)]
265
- if Numeric===obj
266
- return obj
267
- else
268
- return NArrayMiss.to_nam_no_dup(obj)
269
- end
270
- else
271
- obj = @array[*arg]
272
- if Numeric===obj
273
- return obj
274
- else
275
- return NArrayMiss.to_nam_no_dup(obj,@mask[*arg])
276
- end
277
- end
278
- end
279
- def slice(*arg)
280
- NArrayMiss.to_nam_no_dup(@array.slice(*arg),@mask.slice(*arg))
281
- end
282
-
283
- def set_without_validation(*arg)
284
- if arg.length==1 then
285
- if !arg[0] then
286
- @mask[] = 0
287
- elsif arg[0].class == NArrayMiss then
288
- @array[] = arg[0].get_array!
289
- @mask[] = arg[0].get_mask!
290
- else
291
- @array[] = arg[0]
292
- end
293
- else
294
- if !arg[-1] then
295
- @mask[*arg[0..-2]] = 0
296
- elsif arg[-1].class == NArrayMiss then
297
- @array[*arg[0..-2]] = arg[-1].get_array!
298
- @mask[*arg[0..-2]] = arg[-1].get_mask!
299
- else
300
- @array[*arg[0..-2]] = arg[-1]
301
- end
302
- end
303
- return self
304
- end
305
-
306
- def []=(*arg)
307
- if arg.length == 2 && arg[0].class == NArrayMiss && arg[0].typecode == BYTE
308
- idx = arg[0].to_na(0)
309
- self.set_without_validation(idx,arg[-1])
310
- if arg[-1].class != NArrayMiss && arg[-1] then
311
- @mask[idx] = 1
312
- end
313
- else
314
- self.set_without_validation(*arg)
315
- if arg[-1].class != NArrayMiss && arg[-1] then
316
- if arg.length==1 then
317
- @mask=1
318
- else
319
- @mask[*arg[0..-2]] = 1
320
- end
321
- end
322
- end
323
- return self
324
- end
325
-
326
-
327
- =begin
328
- === Filling values
329
- --- NArrayMiss#indgen!([start[,step]])
330
- set values from ((|start|)) with ((|step|)) increment.
331
- --- NArrayMiss#indgen([start[,step]])
332
- same as (({NArrayMiss#indgen!})) but create new object.
333
- --- NArrayMiss#fill!(value)
334
- fill elements with ((|value|)).
335
- --- NArrayMiss#fill(value)
336
- same as (({NArrayMiss#fill!})) but create new object.
337
- --- NArrayMiss#random!(max)
338
- set random values between 0<=x<((|max|)).
339
- --- NArrayMiss#random(max)
340
- same as (({NArrayMiss#random!})) but create new object.
341
- --- NArrayMiss#randomn(max)
342
- set normally distributed random values with mean=0, dispersion=1 (Box-Muller)
343
- =end
344
-
345
- for operator in ["indgen","fill","random"]
346
- eval(<<-EOL,nil,__FILE__,__LINE__+1)
347
- def #{operator}(*arg)
348
- obj = self.dup
349
- obj.#{operator}!(*arg)
350
- obj
351
- end
352
- def #{operator}!(*arg)
353
- @array = @array.#{operator}!(*arg)
354
- @mask[true] = 1
355
- self
356
- end
357
- EOL
358
- end
359
- def randomn
360
- obj = self.dup
361
- obj[@mask] = @array[@mask].randomn
362
- obj
363
- end
364
-
365
-
366
- =begin
367
- === Arithmetic operator
368
- --- NArrayMiss#-@
369
- --- NArrayMiss#+(other)
370
- --- NArrayMiss#-(other)
371
- --- NArrayMiss#*(other)
372
- --- NArrayMiss#/(other)
373
- --- NArrayMiss#%(other)
374
- --- NArrayMiss#**(other)
375
- --- NArrayMiss#abs
376
- --- NArrayMiss#add!(other)
377
- --- NArrayMiss#sbt!(other)
378
- --- NArrayMiss#mul!(other)
379
- --- NArrayMiss#div!(other)
380
- --- NArrayMiss#mod!(other)
381
- --- NArrayMiss#mul_add(other, dim, ...)
382
- =end
383
-
384
- def -@
385
- array = @array.dup
386
- array[@mask] = -@array[@mask]
387
- NArrayMiss.to_nam_no_dup(array, @mask.dup)
388
- end
389
- for operator in ["+","-","*","/","%","**"]
390
- dummy = {"+"=>0,"-"=>0,"*"=>1,"/"=>1,"%"=>1,"**"=>1}[operator]
391
- eval(<<-EOL,nil,__FILE__,__LINE__+1)
392
- def #{operator}(arg)
393
- if !arg then
394
- @mask = 0
395
- return self
396
- else
397
- term1,term2,mask,flag = routine1(arg,#{dummy})
398
- result = term1 #{operator} term2
399
- routine2(result,mask,flag)
400
- end
401
- end
402
- EOL
403
- end
404
- def abs
405
- array = @array.dup
406
- array[@mask] = @array[@mask].abs
407
- NArrayMiss.to_nam_no_dup(array, @mask.dup)
408
- end
409
-
410
- for operator in ["add!","sbt!","mul!","div!","mod!"]
411
- dummy = {"add!"=>0,"sbt!"=>0,"mul!"=>1,"div!"=>1,"mod!"=>1}[operator]
412
- eval(<<-EOL,nil,__FILE__,__LINE__+1)
413
- def #{operator}(arg)
414
- term1,term2,mask,flag = routine1(arg,#{dummy})
415
- result = term1.#{operator} term2
416
- routine2(result,mask,flag)
417
- end
418
- EOL
419
- end
420
- def mul_add(*arg)
421
- if arg.length==1 then
422
- return (self*arg[0]).sum
423
- else
424
- return (self*arg[0]).sum(*arg[1..-1])
425
- end
426
- end
427
-
428
-
429
- =begin
430
- === Bitwise operator (only for byte, sint and int)
431
- --- NArrayMiss#~@
432
- --- NArrayMiss#&(other)
433
- --- NArrayMiss#|(other)
434
- --- NArrayMiss#^(other)
435
- =end
436
-
437
- def ~@
438
- NArrayMiss.to_nam_to_dup(~@array, @mask.dup)
439
- end
440
- for operator in ["&","|","^"]
441
- dummy = {"&"=>1,"|"=>0,"^"=>1}[operator]
442
- eval(<<-EOL,nil,__FILE__,__LINE__+1)
443
- def #{operator}(arg)
444
- term1,term2,mask,flag = routine1(arg,#{dummy})
445
- result = term1 #{operator} term2
446
- routine2(result,mask,flag)
447
- end
448
- EOL
449
- end
450
-
451
-
452
- =begin
453
- === Comparison
454
- --- NArrayMiss#eq(other)
455
- --- NArrayMiss#ne(other)
456
- --- NArrayMiss#gt(other)
457
- --- NArrayMiss#ge(other)
458
- --- NArrayMiss#lt(other)
459
- --- NArrayMiss#le(other)
460
- --- NArrayMiss#>(other)
461
- --- NArrayMiss#>=(other)
462
- --- NArrayMiss#<(other)
463
- --- NArrayMiss#<=(other)
464
- --- NArrayMiss#and(other)
465
- --- NArrayMiss#or(other)
466
- --- NArrayMiss#xor(other)
467
- --- NArrayMiss#not(other)
468
- =end
469
-
470
- for operator in ["eq","ne","gt","ge","lt","le"]
471
- eval(<<-EOL,nil,__FILE__,__LINE__+1)
472
- def #{operator}(arg)
473
- term1,term2,mask,flag = routine1(arg,0)
474
- result = term1.#{operator} term2
475
- routine2(result,mask,flag)
476
- end
477
- EOL
478
- end
479
- for operator in [">",">=","<","<="]
480
- eval(<<-EOL,nil,__FILE__,__LINE__+1)
481
- def #{operator}(arg)
482
- term1,term2,mask,flag = routine1(arg,0)
483
- result = term1 #{operator} term2
484
- routine2(result,mask,flag)
485
- end
486
- EOL
487
- end
488
-
489
- for operator in ["and","or","xor"]
490
- dummy = {"and"=>1,"or"=>0,"xor"=>1}[operator]
491
- eval(<<-EOL,nil,__FILE__,__LINE__+1)
492
- def #{operator}(arg)
493
- term1,term2,mask,flag = routine1(arg,#{dummy})
494
- result = term1.#{operator} term2
495
- routine2(result,mask,flag)
496
- end
497
- EOL
498
- end
499
- def not
500
- NArrayMiss.to_nam_no_dup(@array.not, @mask.dup)
501
- end
502
-
503
- # def ==(arg)
504
- # if art.kind_of?(NArrayMiss) then
505
- # @array==arg.get_array! && @mask==arg.get_mask!
506
- # else
507
- # false
508
- # end
509
- # end
510
-
511
-
512
- =begin
513
- === Statistics
514
- --- NArrayMiss#sum(dim, ... ["min_count"=>i])
515
- return summation of elements in specified dimensions.
516
- Elements at which the number of elements for summation is less than ((|i|)) is invalid.
517
- --- NArrayMiss#accum(dim, ...)
518
- same as (({NArrayMiss#sum})) but not elimiting dimensions whose length became equal to 1.
519
- --- NArrayMiss#min(dim, ...)
520
- return minimum in specified dimensions.
521
- Elements at which the number of valid elements in the dimension is less than ((|i|)) is invalid.
522
- --- NArrayMiss#max(dim, ...)
523
- return maximum in specified dimensions.
524
- Elements at which the number of valid elements in the dimension is less than ((|i|)) is invalid.
525
- --- NArrayMiss#median(dim, ...)
526
- return median in specified dimensions.
527
- Elements at which the number of valid elements in the dimension is less than ((|i|)) is invalid.
528
- --- NArrayMiss#mean(dim, ...)
529
- return mean of elements in specified dimensions.
530
- Elements at which the number of elements for then mean is less than ((|i|)) is invalid.
531
- --- NArrayMiss#stddev(dim, ...)
532
- return standard deviation of elements in specified dimensions.
533
- Elements at which the number of elements for calculation the standard deviation is less than ((|i|)) is invalid.
534
- =end
535
-
536
- def accum(*arg)
537
- if @mask.count_true == 0 then
538
- return nil
539
- else
540
- array = @array.dup
541
- array[@mask.not] = 0
542
- return NArrayMiss.to_nam_no_dup(array.accum(*arg),
543
- @mask.to_type(NArray::INT).accum(*arg).ne(0))
544
- end
545
- end
546
-
547
- for operator in ["sum","min","max"]
548
- str = {"sum"=>"0","min"=>"array.max","max"=>"array.min",}[operator]
549
- eval(<<-EOL,nil,__FILE__,__LINE__+1)
550
- def #{operator}(*arg)
551
- if @mask.count_true == 0 then
552
- return nil
553
- end
554
- min_count=1
555
- options = ["min_count"]
556
- if arg.length!=0 && arg[-1].class==Hash then
557
- option = arg[-1]
558
- arg = arg[0..-2]
559
- option.each_key{|key|
560
- if !options.index(key) then
561
- raise(ArgumentError,key+" option is not exist")
562
- end
563
- }
564
- min_count = option["min_count"]
565
- end
566
- mask = @mask.to_type(NArray::INT)
567
- array = @array.dup
568
- array[@mask.not] = #{str}
569
- mask = mask.sum(*arg)
570
- if NArray===mask then
571
- if mask.ge(min_count).count_true == 0 then
572
- return nil
573
- else
574
- return NArrayMiss.to_nam_no_dup(array.#{operator}(*arg),
575
- mask.ge(min_count))
576
- end
577
- else
578
- if mask < min_count
579
- return nil
580
- else
581
- return array.#{operator}
582
- end
583
- end
584
- end
585
- EOL
586
- end
587
- def mean(*arg)
588
- if @mask.count_true == 0 then
589
- return nil
590
- end
591
- min_count = 1
592
- options=["min_count"]
593
- if arg.length!=0 && arg[-1].class==Hash then
594
- option = arg[-1]
595
- arg2=arg[0..-2]
596
- option.each_key{|key|
597
- if !options.index(key) then
598
- raise(ArgumentError,key+" option is not exist")
599
- end
600
- }
601
- min_count = option["min_count"]
602
- else
603
- arg2=arg
604
- end
605
- count = @mask.to_type(NArray::INT).sum(*arg2)
606
- if count.class == NArray then
607
- count = NArrayMiss.to_nam(count,count.ge(min_count))
608
- if count.ge(min_count).count_true == 0
609
- return nil
610
- else
611
- return self.sum(*arg2)/count
612
- end
613
- else
614
- if count < min_count then
615
- return nil
616
- else
617
- return self.sum(*arg2)/count
618
- end
619
- end
620
- end
621
- def stddev(*arg)
622
- if @mask.count_true == 0 then
623
- return nil
624
- end
625
- min_count=2
626
- options=["min_count"]
627
- if arg.length!=0 && arg[-1].class==Hash then
628
- option = arg[-1]
629
- arg = arg[0..-2]
630
- option.each_key{|key|
631
- if !options.index(key) then
632
- raise(ArgumentError,key+" option is not exist")
633
- end
634
- }
635
- min_count = option["min_count"].to_i
636
- if min_count<2
637
- raise(ArgumentError, "min_count must be >= 2")
638
- end
639
- end
640
- count = @mask.to_type(NArray::INT).sum(*arg)
641
- count2 = @mask.to_type(NArray::INT).accum(*arg)
642
- if count.class==NArray then
643
- count = NArrayMiss.to_nam_no_dup(count,count.ge(min_count))
644
- if count.get_mask!.count_true == 0
645
- return nil
646
- end
647
- else
648
- if count < min_count then
649
- return nil
650
- end
651
- end
652
- if self.integer? then
653
- a = self.to_f
654
- else
655
- a = self
656
- end
657
- var = ( (a-a.accum(*arg)/count2)**2 ).sum(*arg)/(count-1)
658
- obj = NMMath::sqrt(var)
659
- return obj
660
- end
661
-
662
- def median(*arg)
663
- if arg.length==0 then
664
- return @array[@mask].median
665
- else
666
- nshape = NArray.to_na(@array.shape)
667
- nshape[arg]=1
668
- nslice = nshape[nshape.ne(1).where]
669
- index = NArray.object(@mask.rank)
670
- index[nshape.eq(1).where] = true
671
- obj = NArrayMiss.new(@array.typecode,*nslice.to_a)
672
- total = 1
673
- nslice.each{|n| total *= n}
674
- for i in 0...total
675
- index[nshape.ne(1).where] = pos(i,nslice)
676
- mask = NArray.byte(*@array.shape).fill(0)
677
- mask[*index] = 1
678
- mask = @mask&mask
679
- if mask.count_true != 0 then
680
- obj[*pos(i,nslice)] = @array[mask].median
681
- end
682
- end
683
- return obj
684
- end
685
- end
686
-
687
-
688
- =begin
689
- === Sort
690
- --- NArrayMiss#sort(dim)
691
- sort in the 0..((|dim|)) (All dimensions if omitted)
692
- --- NArrayMiss#sort_index(dim)
693
- return index of sort result.
694
- =end
695
-
696
- for operator in ["sort","sort_index"]
697
- eval(<<-EOL,nil,__FILE__,__LINE__+1)
698
- def #{operator}(*arg)
699
- obj=NArrayMiss.new(@array.typecode,*@array.shape)
700
- if arg.length==0 then
701
- obj[@mask] = @array[@mask].#{operator}
702
- return obj
703
- else
704
- nshape = NArray.to_na(@array.shape)
705
- nshape[arg]=1
706
- nslice = nshape[nshape.ne(1).where]
707
- index = NArray.object(@mask.rank)
708
- index[nshape.eq(1).where] = true
709
- obj = NArrayMiss.new(@array.typecode,*@array.shape)
710
- total = 1
711
- nslice.each{|n| total *= n}
712
- for i in 0...total
713
- index[nshape.ne(1).where] = pos(i,nslice)
714
- mask = NArray.byte(*@array.shape).fill(0)
715
- mask[*index] = 1
716
- mask = @mask&mask
717
- if mask.count_true != 0 then
718
- obj[mask] = @array[mask].#{operator}
719
- end
720
- end
721
- return obj
722
- end
723
- end
724
- EOL
725
- end
726
-
727
-
728
- =begin
729
- === Transpose
730
- --- NArrayMiss#transpose(dim0, dim1, ...)
731
- transpose array. The 0-th dimension goes to the ((|dim0|))-th dimension of new array.
732
- =end
733
-
734
- def transpose(*arg)
735
- obj = self.dup
736
- shape = arg.collect{|i| obj.shape[i]}
737
- obj.reshape!(*shape)
738
- obj.set_without_validation( @array.transpose(*arg) )
739
- obj.set_mask(@mask.transpose(*arg))
740
- obj
741
- end
742
-
743
-
744
- =begin
745
- === Changing Shapes of indices
746
- --- NArrayMiss#reshape!(size, ...)
747
- change shape of array.
748
- --- NArrayMiss#reshape(size, ...)
749
- same as (({NArrayMiss#reshape!})) but create new object.
750
- --- NArrayMiss#shape=(size, ...)
751
- same as (({NArrayMiss#reshape!})).
752
- --- NArrayMiss#newdim!(dim)
753
- insert new dimension with size=1
754
- --- NArrayMiss#newdim(dim)
755
- same as (({NArrayMiss#newdim!})) but create new object.
756
- --- NArrayMiss#rewrank!(dim)
757
- same as (({NArrayMiss#newdim!})).
758
- --- NArrayMiss#rewrank=(dim)
759
- same as (({NArrayMiss#newdim!})).
760
- =end
761
-
762
- def reshape!(*arg)
763
- @array = @array.reshape!(*arg)
764
- @mask = @mask.reshape!(*arg)
765
- self
766
- end
767
- def reshape(*arg)
768
- obj = self.dup
769
- obj.reshape!(*arg)
770
- end
771
- alias :shape= :reshape!
772
- def newdim!(*arg)
773
- @array = @array.newdim!(*arg)
774
- @mask = @mask.newdim!(*arg)
775
- self
776
- end
777
- alias :rewrank! :newdim!
778
- alias :rewrank= :newdim!
779
- def newdim(*arg)
780
- obj = self.dup
781
- obj.newdim!(*arg)
782
- end
783
- alias :rewrank :newdim
784
-
785
-
786
- =begin
787
- === Type conversion
788
- --- NArrayMiss#floor
789
- return (({NArrayMiss})) of integer whose elements processed (({floor})).
790
- --- NArrayMiss#ceil
791
- return (({NArrayMiss})) of integer whose elements processed (({ceil})).
792
- --- NArrayMiss#round
793
- return (({NArrayMiss})) of integer whose elements processed (({round})).
794
- --- NArrayMiss#to_i
795
- return (({NArrayMiss})) of integer whose elements processed (({to_i})).
796
- --- NArrayMiss#to_f
797
- return (({NArrayMiss})) of float whose elements processed (({to_f})).
798
- --- NArrayMiss#to_type(typecode)
799
- return (({NArrayMiss})) of ((|typecode|)).
800
- --- NArrayMiss#to_a
801
- convert (({NArrayMiss})) to (({Array})).
802
- --- NArrayMiss#to_na!([missing_value])
803
- convert (({NArrayMiss})) to (({NArray})).
804
- if there is argument, set missing_value for invalid elements.
805
- --- NArrayMiss#to_na([missing_value])
806
- convert (({NArrayMiss})) to (({NArray})).
807
- if there is argument, set missing_value for invalid elements.
808
- --- NArrayMiss#to_s
809
- convert (({NArrayMiss})) to (({String})) as a binary data.
810
- --- NArrayMiss#to_string
811
- create (({NArrayMiss})) of object whose elements are processed (({to_s}))
812
- =end
813
-
814
- for operator in ["floor","ceil","round","to_i","to_f"]
815
- eval(<<-EOL,nil,__FILE__,__LINE__+1)
816
- def #{operator}
817
- NArrayMiss.to_nam_no_dup(@array.#{operator}, @mask.dup)
818
- end
819
- EOL
820
- end
821
- def to_type(typecode)
822
- NArrayMiss.to_nam_no_dup(@array.to_type(typecode), @mask.dup)
823
- end
824
- def to_a
825
- @array.to_a
826
- end
827
- def to_na!(*arg)
828
- if arg.length==0
829
- return @array
830
- elsif arg.length==1 then
831
- self.set_missing_value!(arg[0])
832
- return @array
833
- else
834
- raise(ArgumentError, "Usage: NArray#to_na([missing_value])")
835
- end
836
- end
837
- def to_na(*arg)
838
- return self.dup.to_na!(*arg)
839
- end
840
- def to_s
841
- @array.to_s
842
- end
843
- def to_string
844
- obj = NArrayMiss.obj(*@array.shape)
845
- obj.set_without_validation( @array.to_string )
846
- obh.set_mask(@mask)
847
- obj
848
- end
849
-
850
-
851
- =begin
852
- === Iteration
853
- --- NArrayMiss#each{|x| ...}
854
- --- NArrayMiss#each_valid{|x| ...}
855
- --- NArrayMiss#each_valid_with_index{|x,i| ...}
856
- --- NArrayMiss#collect{|x| ...}
857
- --- NArrayMiss#collect!{|x| ...}
858
- =end
859
-
860
- def each
861
- for i in 0..self.total-1
862
- yield(@array[i])
863
- end
864
- end
865
- def each_valid
866
- for i in 0..self.total-1
867
- yield(@array[i]) if @mask[i]
868
- end
869
- end
870
- def each_valid_with_index
871
- for i in 0..self.total-1
872
- yield(@array[i],i) if @mask[i]
873
- end
874
- end
875
- def collect!
876
- for i in 0..self.total-1
877
- self[i] = yield(self[i])
878
- end
879
- self
880
- end
881
- def collect(&blk)
882
- self.dup.collect!(&blk)
883
- end
884
-
885
-
886
- =begin
887
- === Boolean and mask related (only for byte, sint and int)
888
- --- NArrayMiss#count_false
889
- return the number of elements whose value==0 and valid.
890
- --- NArrayMiss#count_true
891
- return the number of elements whose value!=0 and valid.
892
- --- NArrayMiss#mask(mask)
893
- return (({NArrayMiss#get_mask!&((|mask|))})).
894
- --- NArrayMiss#all?
895
- return true if all the valid elements are not 0, else false.
896
- --- NArrayMiss#any?
897
- return true if any valid element is not 0, else false.
898
- --- NArrayMiss#none?
899
- return true if none of the valid elements is not 0, else false.
900
- --- NArrayMiss#where
901
- return (({NArray})) of indices where valid elements are not 0.
902
- --- NArrayMiss#where2
903
- return (({Array})) including two (({NArray}))s of indices,
904
- where valid elements are not 0, and 0, respectively.
905
- =end
906
-
907
- def count_false
908
- if @array.typecode==BYTE then
909
- return @array.count_false-@mask.count_false
910
- else
911
- raise("cannot count_true NArrayMiss except BYTE type")
912
- end
913
- end
914
- def count_true
915
- if @array.typecode==BYTE then
916
- return (@array&@mask).count_true
917
- else
918
- raise("cannot count_true NArrayMiss except BYTE type")
919
- end
920
- end
921
- def mask(arg)
922
- obj = self.dup
923
- if arg.class==NArrayMiss then
924
- arg = arg.get_array!&arg.get_mask!
925
- end
926
- obj.set_mask(@mask&arg)
927
- end
928
-
929
- def all?
930
- @array[@mask].all?
931
- end
932
- def any?
933
- @array[@mask].any?
934
- end
935
- def none?
936
- @array[@mask].none?
937
- end
938
-
939
- def where
940
- (@array&@mask).where
941
- end
942
- def where2
943
- self.where-@mask.where
944
- end
945
-
946
-
947
-
948
-
949
- =begin
950
- === Complex compound number (only for scomplex and complex)
951
- --- NArrayMiss#real
952
- --- NArrayMiss#imag
953
- --- NArrayMiss#conj
954
- --- NArrayMiss#angle
955
- --- NArrayMiss#imag=(other)
956
- --- NArrayMiss#im
957
- =end
958
-
959
- def real
960
- NArrayMiss.to_nam_no_dup(@array.real,@mask)
961
- end
962
- def imag
963
- NArrayMiss.to_nam_no_dup(@array.imag,@mask)
964
- end
965
- def conj
966
- NArrayMiss.to_nam_no_dup(@array.conj,@mask)
967
- end
968
- def angle
969
- NArrayMiss.to_nam_no_dup(@array.angle,@mask)
970
- end
971
- def imag=(arg)
972
- @array.image=(arg)
973
- self
974
- end
975
- def im
976
- NArrayMiss.to_nam_no_dup(@array.im,@mask)
977
- end
978
-
979
-
980
- =begin
981
- === Byte swap
982
- --- NArrayMiss#swap_byte
983
- swap byte order.
984
- --- NArrayMiss#hton
985
- convert to network byte order.
986
- --- NArrayMiss#ntoh
987
- convert from network byte order.
988
- --- NArrayMiss#htov
989
- convert to VAX byte order.
990
- --- NArrayMiss#vtoh
991
- convert from VAX byte order.
992
- =end
993
-
994
- def swap_byte
995
- obj = self.dup
996
- obj.set_without_validation(@array.swap_byte)
997
- obj
998
- end
999
- def hton
1000
- NArrayMiss.to_nam(@array.hton,@mask.hton)
1001
- end
1002
- alias :ntoh :hton
1003
- def htov
1004
- NArrayMiss.to_nam(@array.htov,@mask.htov)
1005
- end
1006
- alias :vtoh :htov
1007
-
1008
-
1009
- =begin
1010
- === Mask and missing value
1011
- --- NArrayMiss#set_valid(index)
1012
- validate element at ((|index|)).
1013
- ((|index|)) must be (({Integer, Range, Array, or ture})).
1014
- --- NArrayMiss#validation(index)
1015
- alias to set_valid
1016
- --- NArrayMiss#set_invalid(index)
1017
- invaliadate element at ((|index|)).
1018
- ((|index|)) must be (({Integer, Range, Array, or ture})).
1019
- --- NArrayMiss#invalidation(index)
1020
- alias to set_invalid
1021
- --- NArrayMiss#all_valid
1022
- set all elements valid
1023
- --- NArrayMiss#all_invalid
1024
- set all elements invalid
1025
- --- NArrayMiss#set_mask(mask)
1026
- masking by ((|mask|))
1027
- --- NArrayMiss#set_missing_value(value)
1028
- replace invalid elements by ((|value|)).
1029
- --- NArrayMiss#get_mask!
1030
- return (({NArray})) of byte as mask.
1031
- --- NArrayMiss#get_mask
1032
- return (({NArray})) of byte as mask.
1033
- --- NArrayMiss#get_array!
1034
- return (({NArray})) as data.
1035
- --- NArrayMiss#get_array
1036
- return (({NArray})) as data.
1037
- --- NArrayMiss#valid?
1038
- return (({Array})) whose elements are true or false corresponding to valid or invalid of elements, respectively.
1039
- --- NArrayMiss#all_valid?
1040
- return true if all elements are valid, else false.
1041
- --- NArrayMiss#none_valid?
1042
- return true if all elements are invalid, else false.
1043
- --- NArrayMiss#all_invalid?
1044
- alias to none_valid?
1045
- --- NArrayMiss#any_valid?
1046
- return true if any elements are valid, else false.
1047
-
1048
- --- NArrayMiss#count_valid
1049
- return the number of valid elements.
1050
- --- NArrayMiss#count_invalid
1051
- return the number of invalid elements.
1052
- =end
1053
-
1054
- def set_valid(*pos)
1055
- @mask[*pos] = 1
1056
- self
1057
- end
1058
- alias validation set_valid
1059
- def set_invalid(*pos)
1060
- @mask[*pos] = 0
1061
- self
1062
- end
1063
- alias invalidation set_invalid
1064
- def all_valid
1065
- @mask[true]=1
1066
- self
1067
- end
1068
- def all_invalid
1069
- @mask[true]=0
1070
- self
1071
- end
1072
- def set_mask(mask)
1073
- if mask.class == Array then
1074
- tmp = NArray.byte(*@mask.shape)
1075
- tmp[true] = mask
1076
- mask = tmp
1077
- end
1078
- if mask.class == NArrayMiss then
1079
- mask = mask.to_na(0)
1080
- end
1081
- if mask.class == NArray then
1082
- if mask.typecode != BYTE then
1083
- raise("mask must be NArrayMiss.byte, NArray.byte or Array")
1084
- end
1085
- if @array.shape != mask.shape then
1086
- raise("mask.shape must be same as array")
1087
- end
1088
- @mask = mask.dup
1089
- return self
1090
- else
1091
- raise("mask must be NArray.byte or Array")
1092
- end
1093
- end
1094
-
1095
- def set_missing_value!(val)
1096
- @array[@mask.not] = val
1097
- self
1098
- end
1099
- def set_missing_value(val)
1100
- obj = self.dup
1101
- obj.set_missing_value!(val)
1102
- end
1103
-
1104
- def get_mask!
1105
- @mask
1106
- end
1107
- def get_mask
1108
- @mask.dup
1109
- end
1110
- def get_array!
1111
- @array
1112
- end
1113
- def get_array
1114
- @array.dup
1115
- end
1116
-
1117
- def valid?
1118
- where = self.get_mask!.where2
1119
- tf = Array.new(self.total)
1120
- for i in where[0]
1121
- tf[i] = true
1122
- end
1123
- for i in where[1]
1124
- tf[i] = false
1125
- end
1126
- tf
1127
- end
1128
- def all_valid?
1129
- @mask.all?
1130
- end
1131
- def none_valid?
1132
- @mask.none?
1133
- end
1134
- alias :all_invalid? :none_valid?
1135
- def any_valid?
1136
- @mask.any?
1137
- end
1138
-
1139
- def count_valid(*arg)
1140
- if arg.length==0 then
1141
- return @mask.count_true
1142
- else
1143
- return @mask.to_type(NArray::INT).sum(*arg)
1144
- end
1145
- end
1146
- def count_invalid(*arg)
1147
- if arg.length==0 then
1148
- return @mask.count_false
1149
- else
1150
- return NArray.int(*@mask.shape).fill(1).sum(*arg)-
1151
- @mask.to_type(NArray::INT).sum(*arg)
1152
- end
1153
- end
1154
-
1155
-
1156
- =begin
1157
- === Others
1158
- --- NArrayMiss#integer?
1159
- return true if (({NArrayMiss})) is byte, sint or int, else false.
1160
- --- NArrayMiss#complex?
1161
- return true if (({NArrayMiss})) is scomplex or complex, else false.
1162
- --- NArrayMiss#dup
1163
- --- NArrayMiss#coerce(object)
1164
- --- NArrayMiss#inspect
1165
-
1166
- go back to ((<Index>))
1167
- =end
1168
-
1169
- def integer?
1170
- @array.integer?
1171
- end
1172
- def complex?
1173
- @array.complex?
1174
- end
1175
-
1176
-
1177
- def dup
1178
- NArrayMiss.to_nam(@array,@mask)
1179
- end
1180
-
1181
- alias __clone__ clone
1182
- def clone
1183
- obj = __clone__
1184
- obj.set_array(@array.clone)
1185
- obj.set_mask(@mask.clone)
1186
- return obj
1187
- end
1188
-
1189
- def coerce(x)
1190
- if Numeric===x then
1191
- return [NArrayMiss.new(NArray[x].typecode,*self.shape).fill(x),self]
1192
- elsif x.class==Array || x.class==NArray then
1193
- return [NArrayMiss.to_nam(x), self]
1194
- else
1195
- raise("donnot know how to cange #{x.class} to NArrayMiss")
1196
- end
1197
- end
1198
-
1199
-
1200
- def inspect
1201
- # "array -> " + @array.inspect + "\nmask -> " + @mask.inspect
1202
- count_line = 0
1203
- max_line = 10
1204
- max_col = 80
1205
- sep = ", "
1206
- const = Hash.new
1207
- NArray.constants.each{|c| const[NArray.const_get(c)] = c}
1208
- str_ret = "NArrayMiss."+const[typecode].to_s.downcase+"("+shape.join(",")+"):"
1209
- if rank == 0 then
1210
- str_ret += " []"
1211
- return str_ret
1212
- else
1213
- str_ret += "\n"
1214
- end
1215
- str = ""
1216
- index = Array.new(rank,0)
1217
- index[0] = true
1218
- i = 1
1219
- (rank-1).times{ str_ret += "[ " }
1220
- while(true)
1221
- i.times{ str_ret += "[ " }
1222
-
1223
- str = @array[*index].inspect
1224
- ary = str[str.index("[")+1..str.index("]")-1].strip.split(/\s*,\s*/)
1225
- miss = @mask[*index].where2[1]
1226
- miss = miss[miss<ary.length].to_a
1227
- if ary[-1]=="..." && miss[-1]==ary.length-1 then miss.pop end
1228
- for j in miss
1229
- ary[j] = "-"
1230
- end
1231
- while ( rank*4+ary.join(", ").length > max_col )
1232
- ary.pop
1233
- ary[-1] = "..."
1234
- end
1235
- str_ret += ary.join(", ")
1236
- i = 1
1237
- while (i<rank)
1238
- if index[i]<shape[i]-1 then
1239
- str_ret += " ]"+sep+"\n"
1240
- count_line += 1
1241
- index[i] += 1
1242
- break
1243
- else
1244
- str_ret += " ]"
1245
- index[i] = 0
1246
- i += 1
1247
- end
1248
- end
1249
-
1250
- if i>=rank then
1251
- str_ret += " ]"
1252
- return str_ret
1253
- elsif count_line>=max_line then
1254
- str_ret += " ..."
1255
- return str_ret
1256
- end
1257
-
1258
- (rank-i).times{ print(" ") }
1259
- end
1260
- return str_ret
1261
- end
1262
-
1263
-
1264
- def _dump(limit)
1265
- Marshal::dump([@array._dump(nil),@mask._dump(nil)])
1266
- end
1267
- def self._load(o)
1268
- ary, mask = Marshal::load(o)
1269
- ary = NArray._load(ary)
1270
- mask = NArray._load(mask)
1271
- NArrayMiss.to_nam_no_dup(ary,mask)
1272
- end
1273
-
1274
-
1275
-
1276
-
1277
- # private
1278
- private
1279
- def pos(n,shape)
1280
- rank = shape.length
1281
- result = NArray.int(rank)
1282
- m=n
1283
- for i in 0..rank-2
1284
- j = rank-1-i
1285
- result[j] = m/shape[j-1]
1286
- m = m%shape[j-1]
1287
- end
1288
- result[0] = m
1289
- result
1290
- end
1291
- def routine1(arg,dummy)
1292
- flag=true
1293
- if Numeric===arg then
1294
- term1 = @array.dup
1295
- term2 = arg
1296
- mask = true
1297
- elsif arg.class == Array then
1298
- term1 = @array
1299
- term1[@mask.not] = dummy
1300
- term2 = NArray.to_na(arg)
1301
- mask = NArray.byte(*term2.shape).fill(1)
1302
- elsif arg.class == NArray then
1303
- term1 = @array.dup
1304
- term1[@mask.not] = dummy
1305
- term2 = arg.dup
1306
- mask = NArray.byte(*term2.shape).fill(1)
1307
- elsif arg.class == NArrayMiss then
1308
- mask = arg.get_mask
1309
- term1 = @array.dup
1310
- term1[@mask.not] = dummy
1311
- term2 = arg.to_na
1312
- term2[arg.get_mask!.not] = dummy
1313
- else
1314
- term1,term2 = arg.coerce(self)
1315
- flag=false
1316
- end
1317
- [term1,term2,mask,flag]
1318
- end
1319
- def routine2(result,mask,flag)
1320
- if flag then
1321
- obj = NArrayMiss.to_nam_no_dup(result)
1322
- if mask==true then
1323
- obj.set_mask(@mask)
1324
- else
1325
- mask = @mask+mask
1326
- obj.set_mask(mask.eq(2))
1327
- end
1328
- # obj.set_without_validation(@mask.not,@array[@mask.not])
1329
- if Numeric===obj && obj.get_mask![0]==1 then
1330
- return obj.get_array[0]
1331
- else
1332
- return obj
1333
- end
1334
- else
1335
- result
1336
- end
1337
- end
1338
-
1339
- end
1340
-
1341
-
1342
-
1343
- module NMMath
1344
-
1345
-
1346
- func1 = ["sqrt","exp","log","log10","log2",
1347
- "sin","cos","tan","sinh","cosh","tanh",
1348
- "asin","acos","atan","asinh","acosh","atanh"]
1349
- func2 = ["atan2"]
1350
-
1351
- for operator in func1
1352
- eval <<-EOL,nil,__FILE__,__LINE__+1
1353
- def #{operator}(x)
1354
- if Numeric===x || x.class==Array || x.class==NArray then
1355
- NMath::#{operator}(x)
1356
- elsif x.class == NArrayMiss then
1357
- obj = NArrayMiss.new(x.typecode,*x.shape)
1358
- mask = x.get_mask!
1359
- obj[mask] = NMath::#{operator}(x.get_array![mask])
1360
- obj[mask.not] = x[mask.not]
1361
- obj.set_mask(mask)
1362
- obj
1363
- end
1364
- end
1365
- module_function :#{operator}
1366
- EOL
1367
- end
1368
-
1369
- for operator in func2
1370
- eval <<-EOL,nil,__FILE__,__LINE__+1
1371
- def #{operator}(x,y)
1372
- if Numeric===x || x.class==Array || x.class==NArray then
1373
- mask1 = nil
1374
- elsif x.class == NArrayMiss then
1375
- obj = NArrayMiss.new(x.typecode,*x.shape)
1376
- mask1 = x.get_mask!
1377
- end
1378
- if Numeric===y || y.class==Array || y.class==NArray then
1379
- mask2 = nil
1380
- elsif y.class == NArrayMiss then
1381
- obj = NArrayMiss.new(y.typecode,*y.shape)
1382
- mask2 = y.get_mask!
1383
- end
1384
- if mask2.nil? then
1385
- if mask1.nil? then
1386
- return NMath::#{operator}(x,y)
1387
- else
1388
- obj[mask1] = NMath::#{operator}(x.get_array![mask1],y)
1389
- obj[mask1.not] = x[mask1.not]
1390
- obj.set_mask(mask1)
1391
- return obj
1392
- end
1393
- else
1394
- if mask1.nil? then
1395
- obj[mask2] = NMath::#{operator}(x,y.get_array![mask2])
1396
- obj[mask2.not] = y[mask2.not]
1397
- obj.set_mask(mask2)
1398
- return obj
1399
- else
1400
- obj[mask1&mask2] = NMath::#{operator}(x.get_array![mask1],y.get_array![mask2])
1401
- obj[(mask1&mask2).not] = y[(mask1&mask2).not]
1402
- obj[(mask1&mask2).not] = x[(mask1&mask2).not]
1403
- return obj
1404
- end
1405
- end
1406
- end
1407
- module_function :#{operator}
1408
- EOL
1409
- end
1410
-
1411
- for operator in func1+func2
1412
- eval <<-EOL,nil,__FILE__,__LINE__+1
1413
- def #{operator}(*x)
1414
- x = [self]+x if NArrayMiss===self
1415
- NMMath::#{operator}(*x)
1416
- end
1417
- EOL
1418
- end
1419
- end
1420
-
1421
- class NArrayMiss
1422
- include NMMath
1423
- end
1
+ require "narray_miss/version"
2
+ require "narray_miss/narray_miss.rb"