multiarray 0.12.0 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/multiarray/histogram.rb +14 -10
- data/lib/multiarray/operations.rb +8 -6
- data/test/tc_multiarray.rb +7 -4
- data/test/tc_sequence.rb +5 -2
- metadata +3 -3
data/Rakefile
CHANGED
data/lib/multiarray/histogram.rb
CHANGED
@@ -36,11 +36,12 @@ module Hornetseye
|
|
36
36
|
# Constructor
|
37
37
|
#
|
38
38
|
# @param [Node] dest Target array to write histogram to.
|
39
|
+
# @param [Node] weight The weight(s) for the histogram elements.
|
39
40
|
# @param [Array<Node>] sources Arrays with elements to compute histogram of.
|
40
41
|
#
|
41
42
|
# @private
|
42
|
-
def initialize( dest, *sources )
|
43
|
-
@dest, @sources = dest, sources
|
43
|
+
def initialize( dest, weight, *sources )
|
44
|
+
@dest, @weight, @sources = dest, weight, sources
|
44
45
|
end
|
45
46
|
|
46
47
|
# Get unique descriptor of this object
|
@@ -52,6 +53,7 @@ module Hornetseye
|
|
52
53
|
# @private
|
53
54
|
def descriptor( hash )
|
54
55
|
"Histogram(#{@dest.descriptor( hash )}," +
|
56
|
+
"#{@weight.descriptor( hash )}," +
|
55
57
|
"#{@sources.collect { |source| source.descriptor( hash ) }.join ','})"
|
56
58
|
end
|
57
59
|
|
@@ -78,20 +80,21 @@ module Hornetseye
|
|
78
80
|
def demand
|
79
81
|
if variables.empty?
|
80
82
|
if @sources.any? { |source| source.dimension > 0 }
|
81
|
-
source_type = @sources.
|
82
|
-
inject { |a,b| a.coercion b }
|
83
|
+
source_type = @sources.
|
84
|
+
collect { |source| source.array_type }.inject { |a,b| a.coercion b }
|
83
85
|
source_type.shape.last.times do |i|
|
84
86
|
sources = @sources.collect do |source|
|
85
87
|
source.dimension > 0 ? source.element( INT.new( i ) ) : source
|
86
88
|
end
|
87
|
-
|
89
|
+
weight = @weight.dimension > 0 ? @weight.element( INT.new( i ) ) : @weight
|
90
|
+
Histogram.new( @dest, weight, *sources ).demand
|
88
91
|
end
|
89
92
|
else
|
90
93
|
dest = @dest
|
91
94
|
( @dest.dimension - 1 ).downto( 0 ) do |i|
|
92
95
|
dest = dest.element @sources[ i ].demand
|
93
96
|
end
|
94
|
-
dest.store dest +
|
97
|
+
dest.store dest + @weight
|
95
98
|
end
|
96
99
|
@dest
|
97
100
|
else
|
@@ -109,7 +112,7 @@ module Hornetseye
|
|
109
112
|
#
|
110
113
|
# @private
|
111
114
|
def subst( hash )
|
112
|
-
self.class.new @dest.subst( hash ),
|
115
|
+
self.class.new @dest.subst( hash ), @weight.subst( hash ),
|
113
116
|
*@sources.collect { |source| source.subst hash }
|
114
117
|
end
|
115
118
|
|
@@ -119,7 +122,7 @@ module Hornetseye
|
|
119
122
|
#
|
120
123
|
# @private
|
121
124
|
def variables
|
122
|
-
@sources.inject( @dest.variables ) { |a,b| a + b.variables }
|
125
|
+
@sources.inject( @dest.variables + @weight.variables ) { |a,b| a + b.variables }
|
123
126
|
end
|
124
127
|
|
125
128
|
# Strip of all values
|
@@ -132,7 +135,7 @@ module Hornetseye
|
|
132
135
|
#
|
133
136
|
# @private
|
134
137
|
def strip
|
135
|
-
stripped = ( [ @dest ] + @sources ).collect { |source| source.strip }
|
138
|
+
stripped = ( [ @dest, @weight ] + @sources ).collect { |source| source.strip }
|
136
139
|
return stripped.inject( [] ) { |vars,elem| vars + elem[ 0 ] },
|
137
140
|
stripped.inject( [] ) { |values,elem| values + elem[ 1 ] },
|
138
141
|
self.class.new( *stripped.collect { |elem| elem[ 2 ] } )
|
@@ -144,7 +147,8 @@ module Hornetseye
|
|
144
147
|
#
|
145
148
|
# @private
|
146
149
|
def compilable?
|
147
|
-
@dest.compilable? and @
|
150
|
+
@dest.compilable? and @weight.compilable? and
|
151
|
+
@sources.all? { |source| source.compilable? }
|
148
152
|
end
|
149
153
|
|
150
154
|
end
|
@@ -456,14 +456,14 @@ module Hornetseye
|
|
456
456
|
#
|
457
457
|
# @overload histogram( *ret_shape, options = {} )
|
458
458
|
# @param [Array<Integer>] ret_shape Dimensions of resulting histogram.
|
459
|
-
# @option options [
|
459
|
+
# @option options [Node] :weight (UINT(1)) Weights for computing the histogram.
|
460
460
|
# @option options [Boolean] :safe (true) Do a boundary check before creating the
|
461
461
|
# histogram.
|
462
462
|
#
|
463
463
|
# @return [Node] The histogram.
|
464
464
|
def histogram( *ret_shape )
|
465
465
|
options = ret_shape.last.is_a?( Hash ) ? ret_shape.pop : {}
|
466
|
-
options = { :
|
466
|
+
options = { :weight => UINT.new( 1 ), :safe => true }.merge options
|
467
467
|
if shape.first != 1 and ret_shape.size == 1
|
468
468
|
[ self ].histogram *( ret_shape + [ options ] )
|
469
469
|
else
|
@@ -518,7 +518,7 @@ class Array
|
|
518
518
|
#
|
519
519
|
# @overload histogram( *ret_shape, options = {} )
|
520
520
|
# @param [Array<Integer>] ret_shape Dimensions of resulting histogram.
|
521
|
-
# @option options [
|
521
|
+
# @option options [Node] :weight (Hornetseye::UINT(1)) Weights for computing the
|
522
522
|
# histogram.
|
523
523
|
# @option options [Boolean] :safe (true) Do a boundary check before creating the
|
524
524
|
# histogram.
|
@@ -526,7 +526,8 @@ class Array
|
|
526
526
|
# @return [Node] The histogram.
|
527
527
|
def histogram( *ret_shape )
|
528
528
|
options = ret_shape.last.is_a?( Hash ) ? ret_shape.pop : {}
|
529
|
-
options = { :
|
529
|
+
options = { :weight => Hornetseye::UINT.new( 1 ), :safe => true }.merge options
|
530
|
+
weight = options[ :weight ]
|
530
531
|
if options[ :safe ]
|
531
532
|
if size != ret_shape.size
|
532
533
|
raise "Number of arrays for histogram (#{size}) differs from number of " +
|
@@ -535,6 +536,7 @@ class Array
|
|
535
536
|
array_types = collect { |source| source.array_type }
|
536
537
|
source_type = array_types.inject { |a,b| a.coercion b }
|
537
538
|
source_type.check_shape *array_types
|
539
|
+
source_type.check_shape options[ :weight ]
|
538
540
|
for i in 0 ... size
|
539
541
|
range = self[ i ].range
|
540
542
|
if range.begin < 0
|
@@ -547,9 +549,9 @@ class Array
|
|
547
549
|
end
|
548
550
|
end
|
549
551
|
end
|
550
|
-
left = Hornetseye::MultiArray.new
|
552
|
+
left = Hornetseye::MultiArray.new weight.typecode, *ret_shape
|
551
553
|
left[] = 0
|
552
|
-
block = Hornetseye::Histogram.new left, *self
|
554
|
+
block = Hornetseye::Histogram.new left, weight, *self
|
553
555
|
if block.compilable?
|
554
556
|
Hornetseye::GCCFunction.run block
|
555
557
|
else
|
data/test/tc_multiarray.rb
CHANGED
@@ -429,13 +429,16 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
429
429
|
|
430
430
|
def test_histogram
|
431
431
|
assert_equal S( I, 5 )[ 0, 1, 1, 2, 0 ],
|
432
|
-
M[ [ 1, 2 ], [ 3, 3 ] ].histogram( 5, :
|
432
|
+
M[ [ 1, 2 ], [ 3, 3 ] ].histogram( 5, :weight => I.new( 1 ) )
|
433
433
|
assert_equal M( I, 2, 2 )[ [ 1, 0 ], [ 1, 1 ] ],
|
434
|
-
M[ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ] ].
|
434
|
+
M[ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ] ].
|
435
|
+
histogram( 2, 2, :weight => I.new( 1 ) )
|
435
436
|
assert_equal M( I, 2, 2 )[ [ 1, 0 ], [ 1, 1 ] ],
|
436
|
-
[ S[ 0, 0, 1 ], S[ 0, 1, 1 ] ].
|
437
|
+
[ S[ 0, 0, 1 ], S[ 0, 1, 1 ] ].
|
438
|
+
histogram( 2, 2, :weight => I.new( 1 ) )
|
437
439
|
assert_equal M( I, 2, 2, 1 )[ [ [ 0, 1 ], [ 1, 0 ] ] ],
|
438
|
-
S[ C( 1, 0, 0 ), C( 0, 1, 0 ) ].
|
440
|
+
S[ C( 1, 0, 0 ), C( 0, 1, 0 ) ].
|
441
|
+
histogram( 2, 2, 1, :weight => I.new( 1 ) )
|
439
442
|
assert_raise( RuntimeError ) { S[ 1, 2, 3 ].histogram 4, 4 }
|
440
443
|
assert_raise( RuntimeError ) { M[ [ -1, 0 ] ].histogram 3, 2 }
|
441
444
|
assert_raise( RuntimeError ) { M[ [ 0, -1 ] ].histogram 3, 2 }
|
data/test/tc_sequence.rb
CHANGED
@@ -344,13 +344,16 @@ class TC_Sequence < Test::Unit::TestCase
|
|
344
344
|
def test_histogram
|
345
345
|
[ O, I ].each do |t|
|
346
346
|
assert_equal S( t, 5 )[ 0, 1, 2, 1, 1 ],
|
347
|
-
S( t, 5 )[ 1, 2, 2, 3, 4 ].histogram( 5, :
|
347
|
+
S( t, 5 )[ 1, 2, 2, 3, 4 ].histogram( 5, :weight => t.new( 1 ) )
|
348
|
+
assert_equal S( t, 5 )[ 0, 1, 2, 3, 0 ],
|
349
|
+
S( t, 3 )[ 1, 3, 2 ].histogram( 5, :weight => S( t, 3 )[ 1, 3, 2 ] )
|
348
350
|
assert_equal S( t, 4 )[ 0, 1, 1, 0 ],
|
349
|
-
S( t, 2 )[ 1.0, 2.0 ].histogram( 4, :
|
351
|
+
S( t, 2 )[ 1.0, 2.0 ].histogram( 4, :weight => t.new( 1 ) )
|
350
352
|
end
|
351
353
|
assert_raise( RuntimeError ) { S[ -1, 0, 1 ].histogram 3 }
|
352
354
|
assert_raise( RuntimeError ) { S[ 1, 2, 3 ].histogram 3 }
|
353
355
|
assert_raise( RuntimeError ) { S[ 0, 0, 0 ].histogram 3, 2 }
|
356
|
+
assert_raise( RuntimeError ) { S[ 0, 1 ].histogram 3, :weight => S[ 0 ] }
|
354
357
|
end
|
355
358
|
|
356
359
|
def test_lut
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 13
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.13.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jan Wedekind
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-12-
|
17
|
+
date: 2010-12-06 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|