Class: Hornetseye::Inject

Inherits:
Node show all
Defined in:
lib/multiarray/inject.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

===, #[], #[]=, array_type, basetype, #basetype, bool, byte, #check_shape, #coerce, coercion_bool, coercion_byte, coercion_maxint, compilable?, cond, contiguous, #decompose, descriptor, dimension, #dimension, #dup, empty?, #empty?, #finalised?, float, float_scalar, floating, #force, #get, height, #height, indgen, #inspect, match, maxint, #memory, #pointer_type, pointer_type, rgb?, #rgb?, scalar, #shape, shape, #simplify, size, #size, #storage_size, strip, subst, #to_a, #to_s, to_s, to_type, #typecode, typecode, typecodes, variables, #width, width

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 Hornetseye::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, #histogram_with_composite, #imag=, #imag_with_decompose, #inject, #integral, #lut, #lut_with_composite, #max, #min, #normalise, #product, #r=, #r_with_decompose, #range, #real=, #real_with_decompose, #roll, #sum, #to_type, #to_type_with_rgb, #transpose, #unroll

Constructor Details

#initialize(value, index, initial, block, var1, var2) ⇒ Inject

Returns a new instance of Inject.



35
36
37
38
# File 'lib/multiarray/inject.rb', line 35

def initialize( value, index, initial, block, var1, var2 )
  @value, @index, @initial, @block, @var1, @var2 =
    value, index, initial, block, var1, var2
end

Class Method Details

.finalised?Boolean

Check whether objects of this class are finalised computations

Returns:

  • (Boolean)

    Returns false.



29
30
31
# File 'lib/multiarray/inject.rb', line 29

def finalised?
  false
end

Instance Method Details

#array_typeClass

Get type of result of delayed operation

Returns:

  • (Class)

    Type of result.



61
62
63
# File 'lib/multiarray/inject.rb', line 61

def array_type
  @value.to_type( @block.typecode ).array_type
end

#compilable?Boolean

Check whether this term is compilable

Returns:

  • (Boolean)

    Returns whether this term is compilable.



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

def compilable?
  initial_compilable = @initial ? @initial.compilable? : true
  @value.compilable? and initial_compilable and @block.compilable?
end

#demandNode, Object

Reevaluate computation

Returns:

See Also:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/multiarray/inject.rb', line 72

def demand
  if @initial
    retval = @initial.to_type( typecode ).simplify # !!!
    offset = INT.new 0
  else
    retval = @value.subst( @index => INT.new( 0 ) ).simplify # !!!
    offset = INT.new 1
  end
  offset.upto @index.size - 1 do |i|
    sub = @value.subst @index => INT.new( i )
    retval.store @block.subst( @var1 => retval, @var2 => sub )
  end
  retval
end

#descriptor(hash) ⇒ String

Get unique descriptor of this object

Parameters:

  • hash (Hash)

    Labels for any variables.

Returns:

  • (String)

    Descriptor of this object,



47
48
49
50
51
52
53
54
# File 'lib/multiarray/inject.rb', line 47

def descriptor( hash )
  hash = hash.merge @index => ( ( hash.values.max || 0 ) + 1 )
  hash = hash.merge @var1 => ( ( hash.values.max || 0 ) + 1 )
  hash = hash.merge @var2 => ( ( hash.values.max || 0 ) + 1 )
  "Inject(#{@value.descriptor( hash )}," +
     "#{@initial ? @initial.descriptor( hash ) : 'nil'}," +
     "#{@index.descriptor( hash )},#{@block.descriptor( hash )})"
end

#element(i) ⇒ Node, Object

Get element of injection

Parameters:

  • i (Integer, Node)

    Index of desired element.

Returns:



94
95
96
# File 'lib/multiarray/inject.rb', line 94

def element( i )
  Inject.new @value.element( i ), @index, @initial, @block, @var1, @var2
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:



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/multiarray/inject.rb', line 117

def strip
  meta_vars, meta_values, var = @index.strip
  vars1, values1, term1 = @value.subst( @index => var ).strip
  if @initial
    vars2, values2, term2 = @initial.strip
  else
    vars2, values2 = [], [], nil
  end
  vars3, values3, term3 = @block.strip
  return vars1 + vars2 + vars3 + meta_vars,
    values1 + values2 + values3 + meta_values,
    Inject.new( term1, var, term2, term3, @var1, @var2 )
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.



140
141
142
143
144
145
146
# File 'lib/multiarray/inject.rb', line 140

def subst( hash )
  subst_var = @index.subst hash
  value = @value.subst( @index => subst_var ).subst hash
  initial = @initial ? @initial.subst( hash ) : nil
  block = @block.subst hash
  Inject.new value, subst_var, initial, block, @var1, @var2
end

#variablesSet

Get variables contained in this term

Returns:

  • (Set)

    Returns set of variables.



103
104
105
106
# File 'lib/multiarray/inject.rb', line 103

def variables
  initial_variables = @initial ? @initial.variables : Set[]
  ( @value.variables + initial_variables ) - @index.variables
end