multiarray 0.11.3 → 0.11.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,6 +17,7 @@
17
17
  # Namespace of Hornetseye computer vision library
18
18
  module Hornetseye
19
19
 
20
+ # Class for representing native array index types
20
21
  class INDEX_ < Element
21
22
 
22
23
  class << self
@@ -96,12 +97,22 @@ module Hornetseye
96
97
 
97
98
  end
98
99
 
100
+ # This value must not be instantiated
101
+ #
102
+ # The method throws an exception.
103
+ #
104
+ # @private
99
105
  def initialize
100
106
  raise "#{self.class.inspect} must not be instantiated"
101
107
  end
102
108
 
103
109
  end
104
110
 
111
+ # Instantiate the type of an array index
112
+ #
113
+ # @param [INT_,Variable] size Dimension of array.
114
+ #
115
+ # @return [Class] Returns a class deriving from +INDEX_+.
105
116
  def INDEX( size )
106
117
  retval = Class.new INDEX_
107
118
  size = INT.new( size ) unless size.is_a? Node
@@ -17,6 +17,7 @@
17
17
  # Namespace of Hornetseye computer vision library
18
18
  module Hornetseye
19
19
 
20
+ # Class for representing injections
20
21
  class Inject < Node
21
22
 
22
23
  class << self
@@ -32,6 +33,18 @@ module Hornetseye
32
33
 
33
34
  end
34
35
 
36
+ # Constructor
37
+ #
38
+ # @param [Node] value Initial value of injection.
39
+ # @param [Node] index Index to iterate over +value+.
40
+ # @param [Node,NilClass] initial Initial value for injection.
41
+ # @param [Node] block Expression with body of injection.
42
+ # @param [Variable] var1 Variable for performing substitutions on body of
43
+ # injection.
44
+ # @param [Variable] var2 Variable for performing substitutions on body of
45
+ # injection.
46
+ #
47
+ # @private
35
48
  def initialize( value, index, initial, block, var1, var2 )
36
49
  @value, @index, @initial, @block, @var1, @var2 =
37
50
  value, index, initial, block, var1, var2
@@ -59,7 +72,11 @@ module Hornetseye
59
72
  #
60
73
  # @private
61
74
  def array_type
62
- @value.to_type( @block.typecode ).array_type
75
+ retval = @value.to_type( @block.typecode ).array_type
76
+ ( class << self; self; end ).instance_eval do
77
+ define_method( :array_type ) { retval }
78
+ end
79
+ retval
63
80
  end
64
81
 
65
82
  # Reevaluate computation
@@ -60,7 +60,7 @@ module Hornetseye
60
60
  0
61
61
  end
62
62
 
63
- # Get corresponding maximal integer type.
63
+ # Get corresponding maximal integer type
64
64
  #
65
65
  # @return [Class] Returns 32 bit integer or self whichever has more bits.
66
66
  #
@@ -99,37 +99,51 @@ module Hornetseye
99
99
  end
100
100
  end
101
101
 
102
+ # Hash table used internally
103
+ #
104
+ # @private
105
+ DIRECTIVES = { [ 8, true ] => 'c',
106
+ [ 8, false ] => 'C',
107
+ [ 16, true ] => 's',
108
+ [ 16, false ] => 'S',
109
+ [ 32, true ] => 'i',
110
+ [ 32, false ] => 'I',
111
+ [ 64, true ] => 'q',
112
+ [ 64, false ] => 'Q' }
113
+
102
114
  # Directive for packing/unpacking elements of this type
103
115
  #
104
116
  # @private
105
117
  def directive
106
- retval = { [ 8, true ] => 'c',
107
- [ 8, false ] => 'C',
108
- [ 16, true ] => 's',
109
- [ 16, false ] => 'S',
110
- [ 32, true ] => 'i',
111
- [ 32, false ] => 'I',
112
- [ 64, true ] => 'q',
113
- [ 64, false ] => 'Q' }[ [ bits, signed ] ]
118
+ retval = DIRECTIVES[ [ bits, signed ] ]
114
119
  raise "No directive for packing/unpacking #{inspect}" unless retval
115
120
  retval
116
121
  end
117
122
 
118
- # Get string with information about this class
123
+ # Hash table used internally
119
124
  #
120
- # @return [String] Returns string with information about this class (e.g.
121
- # "BYTE").
122
- def inspect
123
- unless bits.nil? or signed.nil?
124
- retval = { [ 8, true ] => 'BYTE',
125
+ # @private
126
+ IDENTIFIER = { [ 8, true ] => 'BYTE',
125
127
  [ 8, false ] => 'UBYTE',
126
128
  [ 16, true ] => 'SINT',
127
129
  [ 16, false ] => 'USINT',
128
130
  [ 32, true ] => 'INT',
129
131
  [ 32, false ] => 'UINT',
130
132
  [ 64, true ] => 'LONG',
131
- [ 64, false ] => 'ULONG' }[ [ bits, signed ] ] ||
133
+ [ 64, false ] => 'ULONG' }
134
+
135
+ # Get string with information about this class
136
+ #
137
+ # @return [String] Returns string with information about this class (e.g.
138
+ # "BYTE").
139
+ def inspect
140
+ unless bits.nil? or signed.nil?
141
+ retval = IDENTIFIER[ [ bits, signed ] ] ||
132
142
  "INT(#{bits.inspect},#{ signed ? 'SIGNED' : 'UNSIGNED' })"
143
+ ( class << self; self; end ).instance_eval do
144
+ define_method( :inspect ) { retval }
145
+ end
146
+ retval
133
147
  else
134
148
  super
135
149
  end
@@ -160,7 +174,7 @@ module Hornetseye
160
174
  bits == other.bits and signed == other.signed
161
175
  end
162
176
 
163
- # Compute hash value for this class.
177
+ # Compute hash value for this class
164
178
  #
165
179
  # @return [Fixnum] Hash value
166
180
  #
@@ -182,11 +196,26 @@ module Hornetseye
182
196
 
183
197
  end
184
198
 
199
+ # Loop statement
200
+ #
201
+ # @param [Proc] action Loop body.
202
+ #
203
+ # @return [GCCValue] Returns +self+.
204
+ #
205
+ # @private
185
206
  def times( &action )
186
207
  get.times &action
187
208
  self
188
209
  end
189
210
 
211
+ # Loop statement
212
+ #
213
+ # @param [Node] other Upper limit for loop.
214
+ # @param [Proc] action Loop body.
215
+ #
216
+ # @return [GCCValue] Returns +self+.
217
+ #
218
+ # @private
190
219
  def upto( other, &action )
191
220
  get.upto other.get, &action
192
221
  self
@@ -256,7 +285,7 @@ module Hornetseye
256
285
  # Create a class deriving from +INT_+ or instantiate an +INT+ object
257
286
  #
258
287
  # @overload INT( bits, signed )
259
- # Create a class deriving from +INT_+. The aprameters +bits+ and +signed+
288
+ # Create a class deriving from +INT_+. The parameters +bits+ and +signed+
260
289
  # are assigned to the corresponding attributes of the resulting class.
261
290
  # @param [Integer] bits Number of bits of native integer.
262
291
  # @param [Boolean] signed Specify +UNSIGNED+ or +SIGNED+ here.
@@ -267,6 +296,8 @@ module Hornetseye
267
296
  # @param [Integer] value Initial value for integer object.
268
297
  # @return [INT] Wrapped integer value.
269
298
  #
299
+ # @return [Class,INT] A class deriving from +INT_+ or a wrapped integer value.
300
+ #
270
301
  # @see INT_
271
302
  # @see INT_.bits
272
303
  # @see INT_.signed
@@ -14,30 +14,66 @@
14
14
  # You should have received a copy of the GNU General Public License
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
+ # Namespace of Hornetseye computer vision library
17
18
  module Hornetseye
18
19
 
20
+ # Class for representing integral image computations
19
21
  class Integral < Node
20
22
 
21
23
  class << self
22
24
 
25
+ # Check whether objects of this class are finalised computations
26
+ #
27
+ # @return [Boolean] Returns +false+.
28
+ #
29
+ # @private
23
30
  def finalised?
24
31
  false
25
32
  end
26
33
 
27
34
  end
28
35
 
36
+ # Constructor
37
+ #
38
+ # @param [Node] dest Target array to write histogram to.
39
+ # @param [Node] source Expression to compute histogram of.
40
+ #
41
+ # @private
29
42
  def initialize( dest, source )
30
43
  @dest, @source = dest, source
31
44
  end
32
45
 
46
+ # Get unique descriptor of this object
47
+ #
48
+ # @param [Hash] hash Labels for any variables.
49
+ #
50
+ # @return [String] Descriptor of this object,
51
+ #
52
+ # @private
33
53
  def descriptor( hash )
34
54
  "Integral(#{@dest.descriptor( hash )},#{@source.descriptor( hash )})"
35
55
  end
36
56
 
57
+ # Get type of result of delayed operation
58
+ #
59
+ # @return [Class] Type of result.
60
+ #
61
+ # @private
37
62
  def array_type
38
- @dest.array_type
63
+ retval = @dest.array_type
64
+ ( class << self; self; end ).instance_eval do
65
+ define_method( :array_type ) { retval }
66
+ end
67
+ retval
39
68
  end
40
69
 
70
+ # Reevaluate computation
71
+ #
72
+ # @return [Node,Object] Result of computation
73
+ #
74
+ # @see #force
75
+ #
76
+ # @private
41
77
  def demand
42
78
  if variables.empty?
43
79
  if @source.dimension > 0
@@ -58,20 +94,48 @@ module Hornetseye
58
94
  end
59
95
  end
60
96
 
97
+ # Substitute variables
98
+ #
99
+ # Substitute the variables with the values given in the hash.
100
+ #
101
+ # @param [Hash] hash Substitutions to apply.
102
+ #
103
+ # @return [Node] Term with substitutions applied.
104
+ #
105
+ # @private
61
106
  def subst( hash )
62
107
  self.class.new @dest.subst( hash ), @source.subst( hash )
63
108
  end
64
109
 
110
+ # Get variables contained in this term
111
+ #
112
+ # @return [Set] Returns list of variables.
113
+ #
114
+ # @private
65
115
  def variables
66
116
  @dest.variables + @source.variables
67
117
  end
68
118
 
119
+ # Strip of all values
120
+ #
121
+ # Split up into variables, values, and a term where all values have been
122
+ # replaced with variables.
123
+ #
124
+ # @return [Array<Array,Node>] Returns an array of variables, an array of
125
+ # values, and the term based on variables.
126
+ #
127
+ # @private
69
128
  def strip
70
129
  vars1, values1, term1 = @dest.strip
71
130
  vars2, values2, term2 = @source.strip
72
131
  return vars1 + vars2, values1 + values2, self.class.new( term1, term2 )
73
132
  end
74
133
 
134
+ # Check whether this term is compilable
135
+ #
136
+ # @return [Boolean] Returns whether this term is compilable.
137
+ #
138
+ # @private
75
139
  def compilable?
76
140
  @dest.compilable? and @source.compilable?
77
141
  end
@@ -17,13 +17,23 @@
17
17
  # Namespace of Hornetseye computer vision library
18
18
  module Hornetseye
19
19
 
20
+ # Class for representing lambda expressions
20
21
  class Lambda < Node
21
22
 
23
+ # Construct lambda term
24
+ #
25
+ # @param [Variable] index Variable to bind.
26
+ # @param [Node] term The term based on that variable.
27
+ #
28
+ # @return [Lambda] Lambda object.
22
29
  def initialize( index, term )
23
30
  @index = index
24
31
  @term = term
25
32
  end
26
33
 
34
+ # Get storage object if there is any
35
+ #
36
+ # @return [Malloc,List,NilClass] Object storing the data.
27
37
  def memory
28
38
  @term.memory
29
39
  end
@@ -46,7 +56,11 @@ module Hornetseye
46
56
  #
47
57
  # @private
48
58
  def array_type
49
- Hornetseye::Sequence @term.array_type, @index.size.get
59
+ retval = Hornetseye::Sequence @term.array_type, @index.size.get
60
+ ( class << self; self; end ).instance_eval do
61
+ define_method( :array_type ) { retval }
62
+ end
63
+ retval
50
64
  end
51
65
 
52
66
  # Get variables contained in this term
@@ -131,9 +145,9 @@ module Hornetseye
131
145
  unless ( 0 ... shape.last ).member? i
132
146
  raise "Index must be in 0 ... #{shape.last} (was #{i})"
133
147
  end
134
- i = Node.match( i ).new i
148
+ i = INT.new i
135
149
  end
136
- i.size.store @index.size if @index.size.get and i.is_a? Variable
150
+ i.size = @index.size if @index.size.get and i.is_a? Variable
137
151
  @term.subst @index => i
138
152
  end
139
153
 
@@ -152,8 +166,8 @@ module Hornetseye
152
166
  "(was #{start} ... #{start + length})"
153
167
  end
154
168
  end
155
- start = Node.match( start ).new start unless start.is_a? Node
156
- length = Node.match( length ).new length unless length.is_a? Node
169
+ start = INT.new start unless start.is_a? Node
170
+ length = INT.new length unless length.is_a? Node
157
171
  index = Variable.new Hornetseye::INDEX( length )
158
172
  Lambda.new( index, @term.subst( @index => index ).
159
173
  skip( index, start ) ).unroll
@@ -59,6 +59,10 @@ module Hornetseye
59
59
  # Retrieve value of specified typecode
60
60
  #
61
61
  # @param [Class] typecode The type of the value.
62
+ #
63
+ # @return [Object] The referenced value.
64
+ #
65
+ # @private
62
66
  def load( typecode )
63
67
  @array[ @offset ]
64
68
  end
@@ -66,6 +70,10 @@ module Hornetseye
66
70
  # Store value
67
71
  #
68
72
  # @param [Node] value Value to store.
73
+ #
74
+ # @return [Object] Returns +value+.
75
+ #
76
+ # @private
69
77
  def save( value )
70
78
  @array[ @offset ] = value.get
71
79
  value
@@ -29,6 +29,9 @@ module Hornetseye
29
29
  @p, @index, @stride = p, index, stride
30
30
  end
31
31
 
32
+ # Get storage object if there is any
33
+ #
34
+ # @return [Malloc,List,NilClass] Object storing the data.
32
35
  def memory
33
36
  #if array_type.storage_size != @stride.get * typecode.storage_size
34
37
  # raise 'Memory is not contiguous'
@@ -54,7 +57,11 @@ module Hornetseye
54
57
  #
55
58
  # @private
56
59
  def array_type
57
- @p.array_type
60
+ retval = @p.array_type
61
+ ( class << self; self; end ).instance_eval do
62
+ define_method( :array_type ) { retval }
63
+ end
64
+ retval
58
65
  end
59
66
 
60
67
  # Substitute variables
@@ -14,54 +14,130 @@
14
14
  # You should have received a copy of the GNU General Public License
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
+ # Namespace of Hornetseye computer vision library
17
18
  module Hornetseye
18
19
 
20
+ # Class for representing lookup operations
19
21
  class Lut < Node
20
22
 
21
23
  class << self
22
24
 
25
+ # Check whether objects of this class are finalised computations
26
+ #
27
+ # @return [Boolean] Returns +false+.
28
+ #
29
+ # @private
23
30
  def finalised?
24
31
  false
25
32
  end
26
33
 
27
34
  end
28
35
 
29
- def initialize( source, table, n )
36
+ # Constructor
37
+ #
38
+ # @param [Node] dest Target array to write histogram to.
39
+ # @param [Node] source Expression to compute histogram of.
40
+ # @param [Integer] n Number of dimensions of lookup.
41
+ #
42
+ # @private
43
+ def initialize( source, table, n = nil )
30
44
  @source, @table = source, table
31
45
  @n = n || @source.shape.first
32
46
  end
33
47
 
48
+ # Get unique descriptor of this object
49
+ #
50
+ # @param [Hash] hash Labels for any variables.
51
+ #
52
+ # @return [String] Descriptor of this object,
53
+ #
54
+ # @private
34
55
  def descriptor( hash )
35
56
  "Lut(#{@source.descriptor( hash )},#{@table.descriptor( hash )},#{@n})"
36
57
  end
37
58
 
59
+ # Get type of result of delayed operation
60
+ #
61
+ # @return [Class] Type of result.
62
+ #
63
+ # @private
38
64
  def array_type
39
65
  shape = @table.shape.first( @table.dimension - @n ) + @source.shape[ 1 .. -1 ]
40
- Hornetseye::MultiArray @table.typecode, *shape
66
+ retval = Hornetseye::MultiArray @table.typecode, *shape
67
+ ( class << self; self; end ).instance_eval do
68
+ define_method( :array_type ) { retval }
69
+ end
70
+ retval
41
71
  end
42
72
 
73
+ # Reevaluate computation
74
+ #
75
+ # @return [Node,Object] Result of computation
76
+ #
77
+ # @see #force
78
+ #
79
+ # @private
43
80
  def demand
44
81
  @source.lut @table, :n => @n, :safe => false
45
82
  end
46
83
 
84
+ # Substitute variables
85
+ #
86
+ # Substitute the variables with the values given in the hash.
87
+ #
88
+ # @param [Hash] hash Substitutions to apply.
89
+ #
90
+ # @return [Node] Term with substitutions applied.
91
+ #
92
+ # @private
47
93
  def subst( hash )
48
94
  self.class.new @source.subst( hash ), @table.subst( hash ), @n
49
95
  end
50
96
 
97
+ # Get variables contained in this term
98
+ #
99
+ # @return [Set] Returns list of variables.
100
+ #
101
+ # @private
51
102
  def variables
52
103
  @source.variables + @table.variables
53
104
  end
54
105
 
106
+ # Strip of all values
107
+ #
108
+ # Split up into variables, values, and a term where all values have been
109
+ # replaced with variables.
110
+ #
111
+ # @return [Array<Array,Node>] Returns an array of variables, an array of
112
+ # values, and the term based on variables.
113
+ #
114
+ # @private
55
115
  def strip
56
116
  vars1, values1, term1 = @source.strip
57
117
  vars2, values2, term2 = @table.strip
58
118
  return vars1 + vars2, values1 + values2, self.class.new( term1, term2, @n )
59
119
  end
60
120
 
121
+ # Skip elements of an array
122
+ #
123
+ # @param [Variable] index Variable identifying index of array.
124
+ # @param [Node] start Wrapped integer with number of elements to skip.
125
+ #
126
+ # @return [Node] Returns element-wise operation with elements skipped on each
127
+ # operand.
128
+ #
129
+ # @private
61
130
  def skip( index, start )
62
131
  self.class.new @source.skip( index, start ), @table.skip( index, start ), @n
63
132
  end
64
133
 
134
+ # Get element of unary operation
135
+ #
136
+ # @param [Integer,Node] i Index of desired element.
137
+ #
138
+ # @return [Node,Object] Element of unary operation.
139
+ #
140
+ # @private
65
141
  def element( i )
66
142
  source, table = @source, @table
67
143
  if source.dimension > 1
@@ -74,6 +150,14 @@ module Hornetseye
74
150
  end
75
151
  end
76
152
 
153
+ # Extract array view with part of array
154
+ #
155
+ # @param [Integer,Node] start Number of elements to skip.
156
+ # @param [Integer,Node] length Size of array view.
157
+ #
158
+ # @return [Node] Array view with the specified elements.
159
+ #
160
+ # @private
77
161
  def slice( start, length )
78
162
  source, table = @source, @table
79
163
  if source.dimension > 1
@@ -88,10 +172,20 @@ module Hornetseye
88
172
  end
89
173
  end
90
174
 
175
+ # Decompose composite elements
176
+ #
177
+ # This method decomposes composite elements into array.
178
+ #
179
+ # @return [Node] Result of decomposition.
91
180
  def decompose( i )
92
181
  self.class.new @source, @table.decompose( i ), @n
93
182
  end
94
183
 
184
+ # Check whether this term is compilable
185
+ #
186
+ # @return [Boolean] Returns whether this term is compilable.
187
+ #
188
+ # @private
95
189
  def compilable?
96
190
  @source.compilable? and @table.compilable?
97
191
  end