Class: Hornetseye::Lambda

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

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, #demand, descriptor, dimension, #dimension, #dup, empty?, #empty?, finalised?, float, float_scalar, floating, #force, #get, height, #height, indgen, #inspect, match, maxint, #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 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

Constructor Details

#initialize(index, term) ⇒ Lambda

Returns a new instance of Lambda.



22
23
24
25
# File 'lib/multiarray/lambda.rb', line 22

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

Instance Method Details

#array_typeClass

Get type of result of delayed operation

Returns:

  • (Class)

    Type of result.



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

def array_type
  Hornetseye::Sequence @term.array_type, @index.size.get
end

#compilable?Boolean

Check whether this term is compilable

Returns:

  • (Boolean)

    Returns whether this term is compilable.



176
177
178
# File 'lib/multiarray/lambda.rb', line 176

def compilable?
  @term.compilable?
end

#decompose(i) ⇒ Node

Decompose composite elements

This method decomposes composite elements into array.

Returns:

  • (Node)

    Result of decomposition.



167
168
169
# File 'lib/multiarray/lambda.rb', line 167

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,



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

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.



129
130
131
132
133
134
135
136
137
138
# File 'lib/multiarray/lambda.rb', line 129

def element( i )
  unless i.is_a? Node
    unless ( 0 ... shape.last ).member? i
      raise "Index must be in 0 ... #{shape.last} (was #{i})"
    end
    i = Node.match( i ).new i
  end
  i.size.store @index.size if @index.size.get and i.is_a? Variable
  @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.



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

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:



100
101
102
103
104
105
106
# File 'lib/multiarray/lambda.rb', line 100

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

#memoryObject



27
28
29
# File 'lib/multiarray/lambda.rb', line 27

def memory
  @term.memory
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.



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

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.



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/multiarray/lambda.rb', line 148

def slice( start, length )
  unless start.is_a?( Node ) or length.is_a?( Node )
    if start < 0 or start + length > shape.last
      raise "Range must be in 0 ... #{shape.last} " +
            "(was #{start} ... #{start + length})"
    end
  end
  start = Node.match( start ).new start unless start.is_a? Node
  length = Node.match( length ).new length unless length.is_a? Node
  index = Variable.new Hornetseye::INDEX( length )
  Lambda.new( index, @term.subst( @index => index ).
                     skip( index, start ) ).unroll
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:



70
71
72
73
74
# File 'lib/multiarray/lambda.rb', line 70

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.



85
86
87
88
89
90
# File 'lib/multiarray/lambda.rb', line 85

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

#variablesSet

Get variables contained in this term

Returns:

  • (Set)

    Returns list of variables.



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

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