multiarray 0.22.0 → 0.23.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/Rakefile +1 -1
  2. data/lib/multiarray.rb +53 -16
  3. data/lib/multiarray/bool.rb +1 -1
  4. data/lib/multiarray/complex.rb +76 -68
  5. data/lib/multiarray/components.rb +11 -10
  6. data/lib/multiarray/composite.rb +1 -1
  7. data/lib/multiarray/diagonal.rb +11 -12
  8. data/lib/multiarray/element.rb +3 -3
  9. data/lib/multiarray/elementwise.rb +14 -14
  10. data/lib/multiarray/field.rb +380 -0
  11. data/lib/multiarray/float.rb +10 -10
  12. data/lib/multiarray/gcccache.rb +1 -1
  13. data/lib/multiarray/gcccontext.rb +35 -54
  14. data/lib/multiarray/gccfunction.rb +12 -19
  15. data/lib/multiarray/gcctype.rb +1 -1
  16. data/lib/multiarray/gccvalue.rb +63 -43
  17. data/lib/multiarray/histogram.rb +17 -19
  18. data/lib/multiarray/index.rb +7 -8
  19. data/lib/multiarray/inject.rb +11 -12
  20. data/lib/multiarray/int.rb +12 -11
  21. data/lib/multiarray/integral.rb +11 -12
  22. data/lib/multiarray/lambda.rb +23 -18
  23. data/lib/multiarray/list.rb +1 -1
  24. data/lib/multiarray/lookup.rb +18 -13
  25. data/lib/multiarray/lut.rb +13 -16
  26. data/lib/multiarray/malloc.rb +1 -1
  27. data/lib/multiarray/mask.rb +11 -8
  28. data/lib/multiarray/methods.rb +10 -10
  29. data/lib/multiarray/multiarray.rb +15 -44
  30. data/lib/multiarray/node.rb +64 -138
  31. data/lib/multiarray/object.rb +2 -6
  32. data/lib/multiarray/operations.rb +116 -134
  33. data/lib/multiarray/pointer.rb +7 -19
  34. data/lib/multiarray/random.rb +11 -8
  35. data/lib/multiarray/rgb.rb +53 -53
  36. data/lib/multiarray/sequence.rb +11 -496
  37. data/lib/multiarray/shortcuts.rb +4 -4
  38. data/lib/multiarray/store.rb +14 -11
  39. data/lib/multiarray/unmask.rb +10 -7
  40. data/lib/multiarray/variable.rb +11 -3
  41. data/test/tc_bool.rb +0 -8
  42. data/test/tc_compile.rb +72 -0
  43. data/test/tc_float.rb +0 -8
  44. data/test/tc_int.rb +0 -8
  45. data/test/tc_lazy.rb +22 -3
  46. data/test/tc_multiarray.rb +100 -126
  47. data/test/tc_object.rb +0 -16
  48. data/test/tc_rgb.rb +0 -16
  49. data/test/tc_sequence.rb +151 -165
  50. data/test/ts_multiarray.rb +2 -0
  51. metadata +7 -4
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -46,15 +46,14 @@ module Hornetseye
46
46
  "INDEX(#{size.descriptor( hash )})"
47
47
  end
48
48
 
49
- # Get type of result of delayed operation
50
- #
51
- # @return [Class] Type of result.
52
- #
53
- # @private
54
- def array_type
49
+ def typecode
55
50
  INT
56
51
  end
57
52
 
53
+ def shape
54
+ []
55
+ end
56
+
58
57
  # Strip of all values
59
58
  #
60
59
  # Split up into variables, values, and a term where all values have been
@@ -115,7 +114,7 @@ module Hornetseye
115
114
  # @return [Class] Returns a class deriving from +INDEX_+.
116
115
  def INDEX( size )
117
116
  retval = Class.new INDEX_
118
- size = INT.new( size ) unless size.is_a? Node
117
+ size = INT.new(size) unless size.matched?
119
118
  retval.size = size
120
119
  retval
121
120
  end
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -50,6 +50,10 @@ module Hornetseye
50
50
  value, index, initial, block, var1, var2
51
51
  end
52
52
 
53
+ def sexp?
54
+ true
55
+ end
56
+
53
57
  # Get unique descriptor of this object
54
58
  #
55
59
  # @param [Hash] hash Labels for any variables.
@@ -66,17 +70,12 @@ module Hornetseye
66
70
  "#{@index.descriptor( hash )},#{@block.descriptor( hash )})"
67
71
  end
68
72
 
69
- # Get type of result of delayed operation
70
- #
71
- # @return [Class] Type of result.
72
- #
73
- # @private
74
- def 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
73
+ def typecode
74
+ @block.typecode
75
+ end
76
+
77
+ def shape
78
+ @value.to_type(@block.typecode).shape
80
79
  end
81
80
 
82
81
  # Reevaluate computation
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -33,6 +33,13 @@ module Hornetseye
33
33
  # signed integer or not.
34
34
  attr_accessor :signed
35
35
 
36
+ def inherit(bits, signed)
37
+ retval = Class.new self
38
+ retval.bits = bits
39
+ retval.signed = signed
40
+ retval
41
+ end
42
+
36
43
  # Memory type required to store elements of this type
37
44
  #
38
45
  # @return [Class] Returns +Malloc+.
@@ -138,12 +145,8 @@ module Hornetseye
138
145
  # "BYTE").
139
146
  def inspect
140
147
  unless bits.nil? or signed.nil?
141
- retval = IDENTIFIER[ [ bits, signed ] ] ||
142
- "INT(#{bits.inspect},#{ signed ? 'SIGNED' : 'UNSIGNED' })"
143
- ( class << self; self; end ).instance_eval do
144
- define_method( :inspect ) { retval }
145
- end
146
- retval
148
+ IDENTIFIER[ [ bits, signed ] ] ||
149
+ "INT(#{bits.inspect},#{ signed ? 'SIGNED' : 'UNSIGNED' })"
147
150
  else
148
151
  super
149
152
  end
@@ -305,10 +308,7 @@ module Hornetseye
305
308
  if signed.nil?
306
309
  INT.new arg
307
310
  else
308
- retval = Class.new INT_
309
- retval.bits = arg
310
- retval.signed = signed
311
- retval
311
+ INT_.inherit arg, signed
312
312
  end
313
313
  end
314
314
 
@@ -439,3 +439,4 @@ module Hornetseye
439
439
 
440
440
 
441
441
  end
442
+
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -43,6 +43,10 @@ module Hornetseye
43
43
  @dest, @source = dest, source
44
44
  end
45
45
 
46
+ def sexp?
47
+ true
48
+ end
49
+
46
50
  # Get unique descriptor of this object
47
51
  #
48
52
  # @param [Hash] hash Labels for any variables.
@@ -54,17 +58,12 @@ module Hornetseye
54
58
  "Integral(#{@dest.descriptor( hash )},#{@source.descriptor( hash )})"
55
59
  end
56
60
 
57
- # Get type of result of delayed operation
58
- #
59
- # @return [Class] Type of result.
60
- #
61
- # @private
62
- def 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
61
+ def typecode
62
+ @dest.typecode
63
+ end
64
+
65
+ def shape
66
+ @dest.shape
68
67
  end
69
68
 
70
69
  # Reevaluate computation
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -26,11 +26,15 @@ module Hornetseye
26
26
  # @param [Node] term The term based on that variable.
27
27
  #
28
28
  # @return [Lambda] Lambda object.
29
- def initialize( index, term )
29
+ def initialize(index, term)
30
30
  @index = index
31
31
  @term = term
32
32
  end
33
33
 
34
+ def sexp?
35
+ true
36
+ end
37
+
34
38
  # Get storage object if there is any
35
39
  #
36
40
  # @return [Malloc,List,NilClass] Object storing the data.
@@ -69,17 +73,18 @@ module Hornetseye
69
73
  "Lambda(#{@index.descriptor( hash )},#{@term.descriptor( hash )})"
70
74
  end
71
75
 
72
- # Get type of result of delayed operation
76
+ # Element-type of this term
73
77
  #
74
- # @return [Class] Type of result.
78
+ # @return [Class] Element-type of this datatype.
79
+ def typecode
80
+ @term.typecode
81
+ end
82
+
83
+ # Get shape of this term
75
84
  #
76
- # @private
77
- def array_type
78
- retval = Hornetseye::Sequence @term.array_type, @index.size.get
79
- ( class << self; self; end ).instance_eval do
80
- define_method( :array_type ) { retval }
81
- end
82
- retval
85
+ # @return [Array<Integer>] Returns shape of array.
86
+ def shape
87
+ @term.shape + [@index.size.get]
83
88
  end
84
89
 
85
90
  # Get variables contained in this term
@@ -159,14 +164,14 @@ module Hornetseye
159
164
  # @return [Node,Object] Result of inserting +i+ for lambda argument.
160
165
  #
161
166
  # @private
162
- def element( i )
163
- unless i.is_a? Node
164
- unless ( 0 ... shape.last ).member? i
167
+ def element(i)
168
+ unless i.matched?
169
+ unless (0 ... shape.last).member? i
165
170
  raise "Index must be in 0 ... #{shape.last} (was #{i})"
166
171
  end
167
172
  i = INT.new i
168
173
  end
169
- i.size = @index.size if i.is_a?( Variable ) and @index.size.get
174
+ i.size = @index.size if i.is_a?(Variable) and @index.size.get
170
175
  @term.subst @index => i
171
176
  end
172
177
 
@@ -179,14 +184,14 @@ module Hornetseye
179
184
  #
180
185
  # @private
181
186
  def slice( start, length )
182
- unless start.is_a?( Node ) or length.is_a?( Node )
187
+ unless start.matched? or length.matched?
183
188
  if start < 0 or start + length > shape.last
184
189
  raise "Range must be in 0 ... #{shape.last} " +
185
190
  "(was #{start} ... #{start + length})"
186
191
  end
187
192
  end
188
- start = INT.new start unless start.is_a? Node
189
- length = INT.new length unless length.is_a? Node
193
+ start = INT.new start unless start.matched?
194
+ length = INT.new length unless length.matched?
190
195
  index = Variable.new Hornetseye::INDEX( length )
191
196
  Lambda.new( index, @term.subst( @index => index ).
192
197
  skip( index, start ) ).unroll
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -29,6 +29,10 @@ module Hornetseye
29
29
  @p, @index, @stride = p, index, stride
30
30
  end
31
31
 
32
+ def sexp?
33
+ true
34
+ end
35
+
32
36
  # Get storage object if there is any
33
37
  #
34
38
  # @return [Malloc,List,NilClass] Object storing the data.
@@ -36,6 +40,13 @@ module Hornetseye
36
40
  @p.memory
37
41
  end
38
42
 
43
+ # Get shape of this term
44
+ #
45
+ # @return [Array<Integer>] Returns shape of array.
46
+ def shape
47
+ @p.shape
48
+ end
49
+
39
50
  # Get strides of array
40
51
  #
41
52
  # @return [Array<Integer>,NilClass] Array strides of this type.
@@ -66,17 +77,11 @@ module Hornetseye
66
77
  "#{@stride.descriptor( hash )})"
67
78
  end
68
79
 
69
- # Get type of result of delayed operation
80
+ # Element-type of this term
70
81
  #
71
- # @return [Class] Type of result.
72
- #
73
- # @private
74
- def array_type
75
- retval = @p.array_type
76
- ( class << self; self; end ).instance_eval do
77
- define_method( :array_type ) { retval }
78
- end
79
- retval
82
+ # @return [Class] Element-type of this datatype.
83
+ def typecode
84
+ @p.typecode
80
85
  end
81
86
 
82
87
  # Substitute variables
@@ -154,8 +159,8 @@ module Hornetseye
154
159
  # @return [Node,Object] Element of lookup term.
155
160
  #
156
161
  # @private
157
- def element( i )
158
- Lookup.new @p.element( i ), @index, @stride
162
+ def element(i)
163
+ Lookup.new @p.element(i), @index, @stride
159
164
  end
160
165
 
161
166
  # Extract array view with part of array
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -44,6 +44,10 @@ module Hornetseye
44
44
  @sources, @table = args[ 0 ... -1 ], args.last
45
45
  end
46
46
 
47
+ def sexp?
48
+ true
49
+ end
50
+
47
51
  # Get unique descriptor of this object
48
52
  #
49
53
  # @param [Hash] hash Labels for any variables.
@@ -56,21 +60,14 @@ module Hornetseye
56
60
  "#{@table.descriptor( hash )})"
57
61
  end
58
62
 
59
- # Get type of result of delayed operation
60
- #
61
- # @return [Class] Type of result.
62
- #
63
- # @private
64
- def array_type
65
- source_type = @sources.collect { |source| source.array_type }.
66
- inject { |a,b| a.coercion b }
67
- shape = @table.shape.first( @table.dimension - @sources.size ) +
68
- source_type.shape
69
- retval = Hornetseye::MultiArray @table.typecode, *shape
70
- ( class << self; self; end ).instance_eval do
71
- define_method( :array_type ) { retval }
72
- end
73
- retval
63
+ def typecode
64
+ @table.typecode
65
+ end
66
+
67
+ def shape
68
+ source_shape = @sources.collect { |source| source.shape }.
69
+ inject { |a,b| a.size > b.size ? a : b }
70
+ @table.shape.first(@table.dimension - @sources.size) + source_shape
74
71
  end
75
72
 
76
73
  # Reevaluate computation
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -45,6 +45,10 @@ module Hornetseye
45
45
  @dest, @source, @m, @index = dest, source, m, index
46
46
  end
47
47
 
48
+ def sexp?
49
+ true
50
+ end
51
+
48
52
  # Get unique descriptor of this object
49
53
  #
50
54
  # @param [Hash] hash Labels for any variables.
@@ -57,13 +61,12 @@ module Hornetseye
57
61
  "#{@m.descriptor( hash )},#{@index.descriptor( hash )})"
58
62
  end
59
63
 
60
- # Get type of result of delayed operation
61
- #
62
- # @return [Class] Type of result.
63
- #
64
- # @private
65
- def array_type
66
- @dest.array_type
64
+ def typecode
65
+ @dest.typecode
66
+ end
67
+
68
+ def shape
69
+ @dest.shape
67
70
  end
68
71
 
69
72
  # Perform masking operation
@@ -1,5 +1,5 @@
1
1
  # multiarray - Lazy multi-dimensional arrays for Ruby
2
- # Copyright (C) 2010 Jan Wedekind
2
+ # Copyright (C) 2010, 2011 Jan Wedekind
3
3
  #
4
4
  # This program is free software: you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
@@ -57,8 +57,8 @@ module Hornetseye
57
57
  # @private
58
58
  def define_unary_method( mod, op, conversion = :identity )
59
59
  mod.module_eval do
60
- define_method( "#{op}_with_hornetseye" ) do |a|
61
- if a.is_a? Node
60
+ define_method "#{op}_with_hornetseye" do |a|
61
+ if a.matched?
62
62
  if a.dimension == 0 and a.variables.empty?
63
63
  target = a.typecode.send conversion
64
64
  target.new mod.send( op, a.simplify.get )
@@ -66,7 +66,7 @@ module Hornetseye
66
66
  Hornetseye::ElementWise( lambda { |x| mod.send op, x },
67
67
  "#{mod}.#{op}",
68
68
  lambda { |x| x.send conversion } ).
69
- new( a ).force
69
+ new(a.sexp).force
70
70
  end
71
71
  else
72
72
  send "#{op}_without_hornetseye", a
@@ -90,19 +90,19 @@ module Hornetseye
90
90
  # @private
91
91
  def define_binary_method( mod, op, coercion = :coercion )
92
92
  mod.module_eval do
93
- define_method( "#{op}_with_hornetseye" ) do |a,b|
94
- if a.is_a? Node or b.is_a? Node
95
- a = Node.match( a, b ).new a unless a.is_a? Node
96
- b = Node.match( b, a ).new b unless b.is_a? Node
93
+ define_method "#{op}_with_hornetseye" do |a,b|
94
+ if a.matched? or b.matched?
95
+ a = Node.match(a, b).new a unless a.matched?
96
+ b = Node.match(b, a).new b unless b.matched?
97
97
  if a.dimension == 0 and a.variables.empty? and
98
98
  b.dimension == 0 and b.variables.empty?
99
99
  target = a.typecode.send coercion, b.typecode
100
- target.new mod.send( op, a.simplify.get, b.simplify.get )
100
+ target.new mod.send(op, a.simplify.get, b.simplify.get)
101
101
  else
102
102
  Hornetseye::ElementWise( lambda { |x,y| mod.send op, x, y },
103
103
  "#{mod}.#{op}",
104
104
  lambda { |t,u| t.send coercion, u } ).
105
- new( a, b ).force
105
+ new(a.sexp, b.sexp).force
106
106
  end
107
107
  else
108
108
  send "#{op}_without_hornetseye", a, b