Class: Hornetseye::Field_
Overview
Class for representing n-dimensional native arrays
Defined Under Namespace
Modules: Match
Class Attribute Summary collapse
-
.dimension ⇒ Integer
Number of dimensions.
-
.typecode ⇒ Class
Type of array elements.
Class Method Summary collapse
-
.[](*args) ⇒ Node
Construct native array from Ruby array.
-
.basetype ⇒ Class
Base type of this data type.
-
.bool ⇒ Class
Get corresponding boolean type.
-
.byte ⇒ Class
Get corresponding byte type.
-
.coercion(other) ⇒ Class
Type coercion for native elements.
-
.coercion_bool(other) ⇒ Class
Coerce and convert to boolean type.
-
.coercion_byte(other) ⇒ Class
Coerce and convert to byte type.
-
.coercion_maxint(other) ⇒ Class
Coerce and convert to maximum integer type.
-
.compilable? ⇒ Boolean
Check whether this array expression allows compilation.
-
.cond(a, b) ⇒ Class
Coerce with two other types.
-
.float ⇒ Class
Convert to type based on floating point numbers.
-
.float_scalar ⇒ Class
Get corresponding floating point type.
-
.floating(other) ⇒ Class
Coerce and convert to type based on floating point numbers.
-
.identity ⇒ Class
Get this type.
-
.indgen(*shape, offset = 0, increment = 1) ⇒ Node
Create (lazy) index array.
- .inherit(typecode, dimension) ⇒ Object
-
.inspect ⇒ String
Display this type.
-
.maxint ⇒ Class
Get corresponding maximum integer type.
- .new(*shape) ⇒ Object
-
.random(*shape, n) ⇒ Node
Generate random number array.
-
.rgb? ⇒ Boolean
Check whether delayed operation will have colour.
-
.scalar ⇒ Class
Get corresponding scalar type.
-
.storage_size(*shape) ⇒ Integer
Get storage size of array type.
- .to_s ⇒ Object
-
.to_type(dest) ⇒ Class
Replace element type.
Instance Method Summary collapse
Class Attribute Details
.dimension ⇒ Integer
Number of dimensions
33 34 35 |
# File 'lib/multiarray/field.rb', line 33 def dimension @dimension end |
.typecode ⇒ Class
Type of array elements
28 29 30 |
# File 'lib/multiarray/field.rb', line 28 def typecode @typecode end |
Class Method Details
.[](*args) ⇒ Node
Construct native array from Ruby array
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/multiarray/field.rb', line 85 def [](*args) def arr_shape(args) if args.is_a? Array args.collect do |arg| arr_shape arg end.inject([]) do |a,b| (0 ... [a.size, b.size].max).collect do |i| [i < a.size ? a[i] : 0, i < b.size ? b[i] : 0].max end end + [args.size] else [] end end retval = new *arr_shape(args) def recursion(element, args) if element.dimension > 0 args.each_with_index do |arg,i| recursion element.element(INT.new(i)), arg end else element[] = args end end recursion retval, args retval end |
.basetype ⇒ Class
Base type of this data type
166 167 168 |
# File 'lib/multiarray/field.rb', line 166 def basetype typecode.basetype end |
.bool ⇒ Class
Get corresponding boolean type
213 214 215 |
# File 'lib/multiarray/field.rb', line 213 def bool Hornetseye::MultiArray typecode.bool, dimension end |
.byte ⇒ Class
Get corresponding byte type
267 268 269 |
# File 'lib/multiarray/field.rb', line 267 def byte Hornetseye::MultiArray typecode.byte, dimension end |
.coercion(other) ⇒ Class
Type coercion for native elements
186 187 188 189 |
# File 'lib/multiarray/field.rb', line 186 def coercion( other ) Hornetseye::MultiArray typecode.coercion(other.typecode), [dimension, other.dimension].max end |
.coercion_bool(other) ⇒ Class
Coerce and convert to boolean type
222 223 224 |
# File 'lib/multiarray/field.rb', line 222 def coercion_bool(other) coercion(other).bool end |
.coercion_byte(other) ⇒ Class
Coerce and convert to byte type
276 277 278 |
# File 'lib/multiarray/field.rb', line 276 def coercion_byte(other) coercion(other).byte end |
.coercion_maxint(other) ⇒ Class
Coerce and convert to maximum integer type
258 259 260 |
# File 'lib/multiarray/field.rb', line 258 def coercion_maxint(other) coercion(other).maxint end |
.compilable? ⇒ Boolean
Check whether this array expression allows compilation
320 321 322 |
# File 'lib/multiarray/field.rb', line 320 def compilable? typecode.compilable? end |
.cond(a, b) ⇒ Class
Coerce with two other types
303 304 305 306 |
# File 'lib/multiarray/field.rb', line 303 def cond(a, b) t = a.coercion b Hornetseye::MultiArray t.typecode, dimension end |
.float ⇒ Class
Convert to type based on floating point numbers
285 286 287 |
# File 'lib/multiarray/field.rb', line 285 def float Hornetseye::MultiArray typecode.field, dimension end |
.float_scalar ⇒ Class
Get corresponding floating point type
240 241 242 |
# File 'lib/multiarray/field.rb', line 240 def float_scalar Hornetseye::MultiArray typecode.float_scalar, dimension end |
.floating(other) ⇒ Class
Coerce and convert to type based on floating point numbers
294 295 296 |
# File 'lib/multiarray/field.rb', line 294 def floating(other) coercion(other).float end |
.identity ⇒ Class
Get this type
204 205 206 |
# File 'lib/multiarray/field.rb', line 204 def identity self end |
.indgen(*shape, offset = 0, increment = 1) ⇒ Node
Create (lazy) index array
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/multiarray/field.rb', line 121 def indgen(*args) unless args.size.between? dimension, dimension + 2 raise "#{inspect}.indgen requires between #{dimension} and #{dimension + 2} arguments" end shape = args[0 ... dimension] offset = args.size > dimension ? args[dimension] : 0 increment = args.size > dimension + 1 ? args[dimension + 1] : 1 step = shape[0 ... -1].inject 1, :* Hornetseye::lazy(shape.last) do |i| (step * increment * i + Hornetseye::MultiArray(typecode, dimension - 1). indgen(*(shape[0 ... -1] + [offset, increment]))).to_type typecode end end |
.inherit(typecode, dimension) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/multiarray/field.rb', line 35 def inherit(typecode, dimension) retval = Class.new self retval.typecode = typecode retval.dimension = dimension retval end |
.inspect ⇒ String
Display this type
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/multiarray/field.rb', line 64 def inspect if typecode and dimension if dimension != 1 "MultiArray(#{typecode.inspect},#{dimension})" else "Sequence(#{typecode.inspect})" end else 'Field(?,?)' end end |
.maxint ⇒ Class
Get corresponding maximum integer type
249 250 251 |
# File 'lib/multiarray/field.rb', line 249 def maxint Hornetseye::MultiArray typecode.maxint, dimension end |
.new(*shape) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/multiarray/field.rb', line 42 def new(*shape) = shape.last.is_a?( Hash ) ? shape.pop : {} raise "Constructor requires #{dimension} arguments" unless dimension == shape.size count = [:count] || 1 if shape.empty? memory = [:memory] || typecode.memory_type.new(typecode.storage_size * count) Hornetseye::Pointer( typecode ).new memory else size = shape.pop stride = shape.inject 1, :* Hornetseye::lazy(size) do |index| pointer = Field_.inherit(typecode, dimension - 1). new *(shape + [:count => count * size, :memory => [:memory]]) Lookup.new pointer, index, INT.new(stride) end end end |
.random(*shape, n) ⇒ Node
Generate random number array
Generate integer or floating point random numbers in the range 0 … n.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/multiarray/field.rb', line 145 def random(*args) unless args.size.between? dimension, dimension + 1 raise "#{inspect}.random requires between #{dimension} and #{dimension + 1} arguments" end shape = args[0 ... dimension] n = args.size > dimension ? args[dimension] : 1 n = typecode.maxint.new n unless n.matched? retval = new *shape unless compilable? and dimension > 0 Random.new(retval, n).demand else GCCFunction.run Random.new(retval, n) end retval end |
.rgb? ⇒ Boolean
Check whether delayed operation will have colour
195 196 197 |
# File 'lib/multiarray/field.rb', line 195 def rgb? typecode.rgb? end |
.scalar ⇒ Class
Get corresponding scalar type
231 232 233 |
# File 'lib/multiarray/field.rb', line 231 def scalar Hornetseye::MultiArray typecode.scalar, dimension end |
.storage_size(*shape) ⇒ Integer
Get storage size of array type
175 176 177 |
# File 'lib/multiarray/field.rb', line 175 def storage_size(*shape) shape.inject typecode.storage_size, :* end |
.to_s ⇒ Object
76 77 78 |
# File 'lib/multiarray/field.rb', line 76 def to_s inspect end |
.to_type(dest) ⇒ Class
Replace element type
313 314 315 |
# File 'lib/multiarray/field.rb', line 313 def to_type(dest) Hornetseye::MultiArray typecode.to_type(dest), dimension end |
Instance Method Details
#arr_shape(args) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/multiarray/field.rb', line 86 def arr_shape(args) if args.is_a? Array args.collect do |arg| arr_shape arg end.inject([]) do |a,b| (0 ... [a.size, b.size].max).collect do |i| [i < a.size ? a[i] : 0, i < b.size ? b[i] : 0].max end end + [args.size] else [] end end |