Class: Hornetseye::Lambda

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

Overview

Class for representing lambda expressions

Instance Method Summary collapse

Methods inherited from Node

#+@, #<=>, ===, #[], #[]=, #allocate, #b=, #b_with_decompose, basetype, #basetype, #between?, bool, byte, #check_shape, #clip, #coerce, coercion_bool, coercion_byte, coercion_maxint, #collect, compilable?, #components, cond, #conditional, #convolve, define_binary_op, define_unary_op, #demand, descriptor, #diagonal, #dilate, #dimension, dimension, #downsample, #dup, #each, #empty?, #eq_with_multiarray, #erode, #fill!, finalised?, #flip, float, float_scalar, floating, #fmod_with_float, #force, #g=, #g_with_decompose, #gauss_blur, #gauss_gradient, #get, #height, #histogram, #histogram_with_rgb, identity, #if, #if_else, #imag=, #imag_with_decompose, indgen, #inject, #inspect, #integral, #lut, #lut_with_rgb, #malloc, #mask, match, #matched?, #max, maxint, #mean, #memorise, #min, #normalise, #prod, #r=, #r_with_decompose, #range, #real=, #real_with_decompose, #reshape, #rgb?, rgb?, #roll, scalar, shape, #shift, #simplify, #size, #sobel, #stretch, strip, subst, #sum, #swap_rgb_with_scalar, #table, #to_a, #to_s, to_s, #to_type, to_type, #to_type_with_identity, #to_type_with_rgb, #transpose, typecode, typecodes, #unmask, #unroll, variables, #warp, #width

Methods included from Field_::Match

#align, #fit

Methods included from FLOAT_::Match

#align, #fit

Methods included from OBJECT::Match

#align, #fit

Methods included from COMPLEX_::Match

#align, #fit

Methods included from BOOL::Match

#fit

Methods included from RGB_::Match

#align, #fit

Methods included from INT_::Match

#fit

Constructor Details

#initialize(index, term) ⇒ Lambda

Construct lambda term

Parameters:

  • index (Variable)

    Variable to bind.

  • term (Node)

    The term based on that variable.



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

def initialize(index, term)
  @index = index
  @term = term
end

Instance Method Details

#compilable?Boolean

Check whether this term is compilable

Returns:

  • (Boolean)

    Returns whether this term is compilable.



210
211
212
# File 'lib/multiarray/lambda.rb', line 210

def compilable?
  @term.compilable?
end

#decompose(i) ⇒ Node

Decompose composite elements

This method decomposes composite elements into array.

Returns:

  • (Node)

    Result of decomposition.



201
202
203
# File 'lib/multiarray/lambda.rb', line 201

def decompose( i )
  Lambda.new @index, @term.decompose( i )
end

#descriptor(hash) ⇒ String

Get unique descriptor of this object

Parameters:

  • hash (Hash)

    Labels for any variables.

Returns:

  • (String)

    Descriptor of this object,



67
68
69
70
# File 'lib/multiarray/lambda.rb', line 67

def descriptor( hash )
  hash = hash.merge @index => ( ( hash.values.max || 0 ) + 1 )
  "Lambda(#{@index.descriptor( hash )},#{@term.descriptor( hash )})"
end

#element(i) ⇒ Node, Object

Get element of this term

Pass i as argument to this lambda object.

Parameters:

  • i (Integer, Node)

    Index of desired element.

Returns:

  • (Node, Object)

    Result of inserting i for lambda argument.



163
164
165
166
167
168
169
170
171
172
# File 'lib/multiarray/lambda.rb', line 163

def element(i)
  unless i.matched?
    unless (0 ... shape.last).member? i
      raise "Index must be in 0 ... #{shape.last} (was #{i})"
    end
    i = INT.new i
  end
  i.size = @index.size if i.is_a?(Variable) and @index.size.get
  @term.subst @index => i
end

#finalised?Boolean

Check whether this object is a finalised computation

Returns:

  • (Boolean)

    Returns boolean indicating whether the lambda term is finalised or not.



220
221
222
# File 'lib/multiarray/lambda.rb', line 220

def finalised?
  @term.finalised?
end

#lookup(value, stride) ⇒ Lookup, Lambda

Lookup element of an array

Parameters:

  • value (Node)

    Index of element.

  • stride (Node)

    Stride for iterating over elements.

Returns:



134
135
136
137
138
139
140
# File 'lib/multiarray/lambda.rb', line 134

def lookup( value, stride )
  if value.is_a? Variable
    Lookup.new self, value, stride
  else
    Lambda.new @index, @term.lookup( value, stride )
  end
end

#memoryMalloc, ...

Get storage object if there is any

Returns:



37
38
39
# File 'lib/multiarray/lambda.rb', line 37

def memory
  @term.memory
end

#shapeArray<Integer>

Get shape of this term

Returns:

  • (Array<Integer>)

    Returns shape of array.



82
83
84
# File 'lib/multiarray/lambda.rb', line 82

def shape
  @term.shape + [@index.size.get]
end

#skip(index, start) ⇒ Node

Skip elements of an array

Parameters:

  • index (Variable)

    Variable identifying index of array.

  • start (Node)

    Wrapped integer with number of elements to skip.

Returns:

  • (Node)

    Return lambda expression with elements skipped.



150
151
152
# File 'lib/multiarray/lambda.rb', line 150

def skip( index, start )
  Lambda.new @index, @term.skip( index, start )
end

#slice(start, length) ⇒ Node

Extract array view with part of array

Parameters:

  • start (Integer, Node)

    Number of elements to skip.

  • length (Integer, Node)

    Size of array view.

Returns:

  • (Node)

    Array view with the specified elements.



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/multiarray/lambda.rb', line 182

def slice( start, length )
  unless start.matched? or length.matched?
    if start < 0 or start + length > shape.last
      raise "Range must be in 0 ... #{shape.last} " +
            "(was #{start} ... #{start + length})"
    end
  end
  start = INT.new start unless start.matched?
  length = INT.new length unless length.matched?
  index = Variable.new Hornetseye::INDEX( length )
  Lambda.new( index, @term.subst( @index => index ).
                     skip( index, start ) ).unroll
end

#stride(index) ⇒ Integer, NilClass

Get stride for specific index

Returns:

  • (Integer, NilClass)

    Array stride of this index.



56
57
58
# File 'lib/multiarray/lambda.rb', line 56

def stride( index )
  @term.stride index
end

#stridesArray<Integer>, NilClass

Get strides of array

Returns:



46
47
48
49
# File 'lib/multiarray/lambda.rb', line 46

def strides
  other = @term.strides
  other ? other + [ stride( @index ) ] : nil
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:



104
105
106
107
108
# File 'lib/multiarray/lambda.rb', line 104

def strip
  meta_vars, meta_values, var = @index.strip
  vars, values, term = @term.subst( @index => var ).strip
  return vars + meta_vars, values + meta_values, Lambda.new( var, term )
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.



119
120
121
122
123
124
# File 'lib/multiarray/lambda.rb', line 119

def subst( hash )
  # subst_var = @index.subst hash
  # Lambda.new subst_var, @term.subst( @index => subst_var ).subst( hash )
  subst_var = @index.subst hash
  Lambda.new subst_var, @term.subst( hash.merge( @index => subst_var ) )
end

#typecodeClass

Element-type of this term

Returns:

  • (Class)

    Element-type of this datatype.



75
76
77
# File 'lib/multiarray/lambda.rb', line 75

def typecode
  @term.typecode
end

#variablesSet

Get variables contained in this term

Returns:

  • (Set)

    Returns list of variables.



91
92
93
# File 'lib/multiarray/lambda.rb', line 91

def variables
  @term.variables - @index.variables + @index.meta.variables
end