Class: Hornetseye::Node

Inherits:
Object show all
Extended by:
BOOL::Match, COMPLEX_::Match, FLOAT_::Match, INT_::Match, OBJECT::Match, RGB_::Match, Sequence_::Match
Includes:
Operations
Defined in:
lib/multiarray/node.rb,
lib/multiarray/operations.rb

Overview

Base class for representing native datatypes and operations (terms)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FLOAT_::Match

align, fit

Methods included from OBJECT::Match

align, fit

Methods included from COMPLEX_::Match

align, fit

Methods included from Sequence_::Match

align, fit

Methods included from BOOL::Match

fit

Methods included from RGB_::Match

align, fit

Methods included from INT_::Match

fit

Methods included from Operations

#+@, #<=>, #b=, #b_with_decompose, #collect, #conditional, #convolve, define_binary_op, define_unary_op, #diagonal, #eq_with_multiarray, #fill!, #g=, #g_with_decompose, #histogram, #imag=, #imag_with_decompose, #inject, #integral, #lut, #max, #min, #normalise, #product, #r=, #r_with_decompose, #range, #real=, #real_with_decompose, #roll, #sum, #to_type, #to_type_with_rgb, #transpose, #unroll

Class Method Details

.===(other) ⇒ Boolean

Category operator

Returns:

  • (Boolean)

    Check for equality or kind.



266
267
268
# File 'lib/multiarray/node.rb', line 266

def ===( other )
  ( other == self ) or ( other.is_a? self ) or ( other.class == self )
end

.array_typeClass

Get type of result of delayed operation

Returns:

  • (Class)

    Type of result.



92
93
94
# File 'lib/multiarray/node.rb', line 92

def array_type
  self
end

.basetypeClass

Base type of this data type

Returns:

  • (Class)

    Returns element_type.



74
75
76
# File 'lib/multiarray/node.rb', line 74

def basetype
  self
end

.boolClass

Get corresponding boolean-based datatype

Returns:

  • (Class)

    Returns BOOL.



165
166
167
# File 'lib/multiarray/node.rb', line 165

def bool
  BOOL
end

.byteClass

Convert to type based on bytes

Returns:

  • (Class)

    Corresponding type based on bytes.



227
228
229
# File 'lib/multiarray/node.rb', line 227

def byte
  BYTE
end

.coercion_bool(other) ⇒ Class

Get boolean-based datatype for binary operation

Returns:

  • (Class)

    Returns BOOL.



186
187
188
# File 'lib/multiarray/node.rb', line 186

def coercion_bool( other )
  other.coercion( self ).bool
end

.coercion_byte(other) ⇒ Class

Get byte-based datatype for binary operation

Parameters:

  • other (Class)

    The other type.

Returns:

  • (Class)

    Returns type based on bytes.



236
237
238
# File 'lib/multiarray/node.rb', line 236

def coercion_byte( other )
  coercion( other ).byte
end

.coercion_maxint(other) ⇒ Class

Get maximum integer based datatype for binary operation

Returns:

  • (Class)

    Returns type based on maximum integer.



202
203
204
# File 'lib/multiarray/node.rb', line 202

def coercion_maxint( other )
  coercion( other ).maxint
end

.compilable?Boolean

Check whether this term is compilable

Returns:

  • (Boolean)

    Returns true.



301
302
303
# File 'lib/multiarray/node.rb', line 301

def compilable?
  true
end

.cond(a, b) ⇒ Object



240
241
242
243
# File 'lib/multiarray/node.rb', line 240

def cond( a, b )
  t = a.coercion b
  Hornetseye::MultiArray( t.typecode, *shape ).coercion t
end

.contiguousClass

Get corresponding contiguous datatype

Returns:

  • (Class)

    Returns self.



154
155
156
# File 'lib/multiarray/node.rb', line 154

def contiguous
  self
end

.descriptor(hash) ⇒ String

Get unique descriptor of this class

Parameters:

  • hash (Hash)

    Labels for any variables.

Returns:

  • (String)

    Descriptor of this class.



45
46
47
# File 'lib/multiarray/node.rb', line 45

def descriptor( hash )
  'Node'
end

.dimensionArray<Integer>

Get dimension of this term

Returns:

  • (Array<Integer>)

    Returns 0.



145
146
147
# File 'lib/multiarray/node.rb', line 145

def dimension
  0
end

.empty?Boolean

Check whether the type is an empty array

Returns:

  • (Boolean)

    Returns whether this type represents an empty array.



138
139
140
# File 'lib/multiarray/node.rb', line 138

def empty?
  size == 0
end

.finalised?Boolean

Check whether objects of this class are finalised computations

Returns:

  • (Boolean)

    Returns true.



310
311
312
# File 'lib/multiarray/node.rb', line 310

def finalised?
  true
end

.floatClass

Convert to type based on floating point numbers

Returns:

  • (Class)

    Corresponding type based on floating point numbers.



211
212
213
# File 'lib/multiarray/node.rb', line 211

def float
  DFLOAT
end

.float_scalarClass

Get corresponding type based on floating-point scalars

Returns:

  • (Class)

    Corresponding type based on floating-point scalars.



179
180
181
# File 'lib/multiarray/node.rb', line 179

def float_scalar
  float.scalar
end

.floating(other) ⇒ Class

Get floating point based datatype for binary operation

Returns:

  • (Class)

    Returns type based on floating point numbers.



218
219
220
# File 'lib/multiarray/node.rb', line 218

def floating( other )
  other.coercion( self ).float
end

.heightObject



124
125
126
# File 'lib/multiarray/node.rb', line 124

def height
  shape[1]
end

.indgen(offset = 0, increment = 1) ⇒ Object

Generate index array of this type

Parameters:

  • offset (Object) (defaults to: 0)

    First value.

  • offset (Object) (defaults to: 0)

    Increment for consecutive values.

Returns:

  • (Object)

    Returns offset.



109
110
111
# File 'lib/multiarray/node.rb', line 109

def indgen( offset = 0, increment = 1 )
  offset
end

.match(value, context = nil) ⇒ Class

Find matching native datatype to a Ruby value

Parameters:

  • value (Object)

    Value to find native datatype for.

Returns:

  • (Class)

    Matching native datatype.



56
57
58
59
60
# File 'lib/multiarray/node.rb', line 56

def match( value, context = nil )
  retval = fit value
  retval = retval.align context.basetype if context
  retval
end

.maxintClass

Get corresponding maximal integer type

Returns:

  • (Class)

    Returns self.



195
196
197
# File 'lib/multiarray/node.rb', line 195

def maxint
  self
end

.pointer_typeClass

Convert to pointer type

Returns:

  • (Class)

    Corresponding pointer type.



99
100
101
# File 'lib/multiarray/node.rb', line 99

def pointer_type
  Hornetseye::Pointer( self )
end

.rgb?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/multiarray/node.rb', line 158

def rgb?
  false
end

.scalarClass

Get corresponding scalar type

Returns:

  • (Class)

    Returns self.



172
173
174
# File 'lib/multiarray/node.rb', line 172

def scalar
  self
end

.shapeArray<Integer>

Get shape of this term

Returns:

  • (Array<Integer>)

    Returns [].



116
117
118
# File 'lib/multiarray/node.rb', line 116

def shape
  []
end

.sizeInteger

Get size (number of elements) of this value

Returns:

  • (Integer)

    Returns 1.



131
132
133
# File 'lib/multiarray/node.rb', line 131

def size
  1
end

.stripArray<Array,Node>

Strip of all values

Split up into variables, values, and a term where all values have been replaced with variables.

values, and the term based on variables.

Returns:



279
280
281
# File 'lib/multiarray/node.rb', line 279

def strip
  return [], [], self
end

.subst(hash) ⇒ Node

Substitute variables

Substitute the variables with the values given in the hash.

Parameters:

  • hash (Hash)

    Substitutions to apply.

Returns:

  • (Node)

    Term with substitutions applied.



292
293
294
# File 'lib/multiarray/node.rb', line 292

def subst( hash )
  hash[ self ] || self
end

.to_sString

Get unique descriptor of this class

The method calls descriptor( {} ).

Returns:

  • (String)

    Descriptor of this class.

See Also:



34
35
36
# File 'lib/multiarray/node.rb', line 34

def to_s
  descriptor( {} )
end

.to_type(dest) ⇒ Class

Convert to different element type

Parameters:

  • dest (Class)

    Element type to convert to.

Returns:

  • (Class)

    Type based on the different element type.



250
251
252
# File 'lib/multiarray/node.rb', line 250

def to_type( dest )
  dest
end

.typecodeClass

Element-type of this term

Returns:

  • (Class)

    Element-type of this datatype.



65
66
67
# File 'lib/multiarray/node.rb', line 65

def typecode
  self
end

.typecodesArray<Class>

Get list of types of composite type

Returns:

  • (Array<Class>)

    List of types.



83
84
85
# File 'lib/multiarray/node.rb', line 83

def typecodes
  [ self ]
end

.variablesSet

Get variables contained in this datatype

Returns:

  • (Set)

    Returns Set[].



259
260
261
# File 'lib/multiarray/node.rb', line 259

def variables
  Set[]
end

.widthObject



120
121
122
# File 'lib/multiarray/node.rb', line 120

def width
  shape[0]
end

Instance Method Details

#[](*indices) ⇒ Object, Node

Retrieve value of array element(s)

Parameters:

  • *indices (Array<Integer>)

    Index/indices to select element.

Returns:

  • (Object, Node)

    Value of array element or a sub-element.



543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/multiarray/node.rb', line 543

def []( *indices )
  if indices.empty?
    force
  else
    if indices.last.is_a? Range
      view = slice indices.last.min, indices.last.size
    else
      view = element indices.last
    end
    view[ *indices[ 0 ... -1 ] ]
  end
end

#[]=(*indices, value) ⇒ Object, Node

Assign value to array element(s)

Assign a value to an element of an array

Parameters:

  • *indices (Array<Integer>)

    Index/indices to select the element.

  • value (Object, Node)

    Ruby object with new value.

Returns:



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/multiarray/node.rb', line 579

def []=( *indices )
  value = indices.pop
  value = Node.match( value ).new value unless value.is_a? Node
  if indices.empty?
    check_shape value
    unless compilable? and value.compilable? and dimension > 0
      Store.new( self, value ).demand
    else
      GCCFunction.run Store.new( self, value )
    end
    value
  else
    if indices.last.is_a? Range
      view = slice indices.last.min, indices.last.size
    else
      view = element indices.last
    end
    view[ *indices[ 0 ... -1 ] ] = value
  end
end

#array_typeClass

Get type of result of delayed operation

Returns:

  • (Class)

    Type of result.



321
322
323
# File 'lib/multiarray/node.rb', line 321

def array_type
  self.class.array_type
end

#basetypeObject



339
340
341
# File 'lib/multiarray/node.rb', line 339

def basetype
  array_type.basetype
end

#check_shape(*args) ⇒ Object



556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/multiarray/node.rb', line 556

def check_shape( *args )
  args.each do |arg|
    if dimension < arg.dimension
      raise "#{arg.array_type.inspect} has #{arg.dimension} dimension(s) " +
            "but should not have more than #{dimension}"
    end
    if ( shape + arg.shape ).all? { |s| s.is_a? Integer }
      if shape.last( arg.dimension ) != arg.shape
        raise "#{arg.array_type.inspect} has shape #{arg.shape.inspect} " +
              "(does not match last value(s) of #{shape.inspect})"
      end
    end
  end
end

#coerce(other) ⇒ Array<Node>

Coerce with other object

Parameters:

Returns:



672
673
674
675
676
677
678
# File 'lib/multiarray/node.rb', line 672

def coerce( other )
  if other.is_a? Node
    return other, self
  else
    return Node.match( other, self ).new( other ), self
  end
end

#compilable?Boolean

Check whether this term is compilable

Returns:

  • (Boolean)

    Returns typecode.compilable?.



525
526
527
# File 'lib/multiarray/node.rb', line 525

def compilable?
  typecode.compilable?
end

#decompose(i) ⇒ Node

Decompose composite elements

This method decomposes composite elements into array.

Returns:

  • (Node)

    Returns self.



685
686
687
# File 'lib/multiarray/node.rb', line 685

def decompose( i )
  self
end

#demandNode, Object

Reevaluate computation

Returns:

See Also:



629
630
631
# File 'lib/multiarray/node.rb', line 629

def demand
  self
end

#descriptor(hash) ⇒ String

Get unique descriptor of this object

Parameters:

  • hash (Hash)

    Labels for any variables.

Returns:

  • (String)

    Descriptor of this object.



494
495
496
# File 'lib/multiarray/node.rb', line 494

def descriptor( hash )
  'Node()'
end

#dimensionArray<Integer>

Get dimension of this term

Returns:

  • (Array<Integer>)

    Returns array_type.dimension.



383
384
385
# File 'lib/multiarray/node.rb', line 383

def dimension
  array_type.dimension
end

#dupNode

Duplicate object

Returns:

  • (Node)

    Duplicate of self.



501
502
503
504
505
# File 'lib/multiarray/node.rb', line 501

def dup
  retval = array_type.new
  retval[] = self
  retval
end

#empty?Boolean

Check whether this object is an empty array.

Returns:

  • (Boolean)

    Returns whether this object is an empty array.



376
377
378
# File 'lib/multiarray/node.rb', line 376

def empty?
  array_type.empty?
end

#finalised?Boolean

Check whether this object is a finalised computation

Returns:

  • (Boolean)

    Returns self.class.finalised?.



534
535
536
# File 'lib/multiarray/node.rb', line 534

def finalised?
  self.class.finalised?
end

#forceNode, Object

Force delayed computation unless in lazy mode

Returns:

See Also:



640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/multiarray/node.rb', line 640

def force
  if ( dimension > 0 and Thread.current[ :lazy ] ) or not variables.empty?
    self
  elsif finalised?
    get
  elsif compilable?
    retval = pointer_type.new
    GCCFunction.run Store.new( retval, self )
    retval.demand.get
  else
    Store.new( array_type.new, self ).demand.get
  end
end

#getNode, Object

Extract native value if this is an element

Returns:



396
397
398
# File 'lib/multiarray/node.rb', line 396

def get
  self
end

#heightObject



354
355
356
# File 'lib/multiarray/node.rb', line 354

def height
  array_type.height
end

#inspect(indent = nil, lines = nil) ⇒ String

Display information about this object

Returns:

  • (String)

    String with information about this object.



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/multiarray/node.rb', line 418

def inspect( indent = nil, lines = nil )
  if variables.empty?
    if dimension == 0 and not indent
      "#{array_type.inspect}(#{force.inspect})" # !!!
    else
      if indent
        prepend = ''
      else
        prepend = "#{array_type.inspect}:\n"
        indent = 0
        lines = 0
      end
      if empty?
        retval = '[]'
      else
        retval = '[ '
        for i in 0 ... array_type.num_elements
          x = element i
          if x.dimension > 0
            if i > 0
              retval += ",\n  "
              lines += 1
              if lines >= 10
                retval += '...' if indent == 0
                break
              end
              retval += '  ' * indent
            end
            str = x.inspect indent + 1, lines
            lines += str.count "\n"
            retval += str
            if lines >= 10
              retval += '...' if indent == 0
              break
            end
          else
            retval += ', ' if i > 0
            str = x.force.inspect # !!!
            if retval.size + str.size >= 74 - '...'.size -
                '[  ]'.size * indent.succ
              retval += '...'
              break
            else
              retval += str
            end
          end
        end
        retval += ' ]' unless lines >= 10
      end
      prepend + retval
    end
  else
    to_s
  end
end

#memoryObject



369
370
371
# File 'lib/multiarray/node.rb', line 369

def memory
  nil
end

#pointer_typeClass

Convert to pointer type

Returns:

  • (Class)

    Corresponding pointer type.



328
329
330
# File 'lib/multiarray/node.rb', line 328

def pointer_type
  array_type.pointer_type
end

#rgb?Boolean

Returns:

  • (Boolean)


387
388
389
# File 'lib/multiarray/node.rb', line 387

def rgb?
  array_type.rgb?
end

#shapeArray<Integer>

Get shape of this term

Returns:

  • (Array<Integer>)

    Returns array_type.shape.



346
347
348
# File 'lib/multiarray/node.rb', line 346

def shape
  array_type.shape
end

#simplifyNode, Object

Reevaluate term

Returns:

See Also:



661
662
663
# File 'lib/multiarray/node.rb', line 661

def simplify
  dimension == 0 ? demand.dup : demand
end

#sizeInteger

Get size (number of elements) of this value

Returns:

  • (Integer)

    Returns array_type.size.



361
362
363
# File 'lib/multiarray/node.rb', line 361

def size
  array_type.size
end

#storage_sizeObject



365
366
367
# File 'lib/multiarray/node.rb', line 365

def storage_size
  array_type.storage_size
end

#stripArray<Array,Node>

Strip of all values

Split up into variables, values, and a term where all values have been replaced with variables.

values, and the term based on variables.

Returns:



618
619
620
# File 'lib/multiarray/node.rb', line 618

def strip
  return [], [], self
end

#subst(hash) ⇒ Node

Substitute variables

Substitute the variables with the values given in the hash.

Parameters:

  • hash (Hash)

    Substitutions to apply.

Returns:

  • (Node)

    Term with substitutions applied.



516
517
518
# File 'lib/multiarray/node.rb', line 516

def subst( hash )
  hash[ self ] || self
end

#to_aArray<Object>

Convert to Ruby array of objects

Perform pending computations and convert native array to Ruby array of objects.

Returns:



406
407
408
409
410
411
412
413
# File 'lib/multiarray/node.rb', line 406

def to_a
  if dimension == 0
    force
  else
    n = shape.last
    ( 0 ... n ).collect { |i| element( i ).to_a }
  end
end

#to_sString

Get unique descriptor of this object

The method calls descriptor( {} ).

Returns:

  • (String)

    Descriptor of this object.

See Also:



483
484
485
# File 'lib/multiarray/node.rb', line 483

def to_s
  descriptor( {} )
end

#typecodeClass

Element-type of this term

Returns:

  • (Class)

    Element-type of this datatype.



335
336
337
# File 'lib/multiarray/node.rb', line 335

def typecode
  array_type.typecode
end

#variablesSet

Get variables contained in this object

Returns:

  • (Set)

    Returns Set[].



605
606
607
# File 'lib/multiarray/node.rb', line 605

def variables
  Set[]
end

#widthObject



350
351
352
# File 'lib/multiarray/node.rb', line 350

def width
  array_type.width
end