Class: Hornetseye::ElementWise_

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

Overview

Class for representing element-wise operations

Class Attribute Summary collapse

Class Method Summary collapse

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, #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, #memory, #min, #normalise, #prod, #r=, #r_with_decompose, #range, #real=, #real_with_decompose, #reshape, #rgb?, rgb?, #roll, scalar, shape, #shift, #simplify, #size, #sobel, #stretch, #stride, #strides, 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(*values) ⇒ ElementWise_

Initialise unary operation

Parameters:

  • value (Node)

    Value to apply operation to.



72
73
74
75
# File 'lib/multiarray/elementwise.rb', line 72

def initialize( *values )
  @values = values
  check_shape *values
end

Class Attribute Details

.conversionProc

Name of method for type conversion

Returns:

  • (Proc)

    A closure for doing the type conversion.



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

def conversion
  @conversion
end

.keySymbol, String

Unique key to identify operation

Returns:

  • (Symbol, String)

    A unique key to identify this operation.



33
34
35
# File 'lib/multiarray/elementwise.rb', line 33

def key
  @key
end

.operationProc

Name of operation

Returns:

  • (Proc)

    A closure with the operation.



28
29
30
# File 'lib/multiarray/elementwise.rb', line 28

def operation
  @operation
end

Class Method Details

.descriptor(hash) ⇒ String

Get unique descriptor of this class

Parameters:

  • hash (Hash)

    Labels for any variables.

Returns:

  • (String)

    Descriptor of this class.



54
55
56
# File 'lib/multiarray/elementwise.rb', line 54

def descriptor( hash )
  inspect
end

.finalised?Boolean

Check whether objects of this class are finalised computations

Returns:

  • (Boolean)

    Returns false.



63
64
65
# File 'lib/multiarray/elementwise.rb', line 63

def finalised?
  false
end

.inspectString

Get string with information about this class

Returns:

  • (String)

    Return string with information about this class.



43
44
45
# File 'lib/multiarray/elementwise.rb', line 43

def inspect
  key.to_s
end

Instance Method Details

#compilable?Boolean

Check whether this term is compilable

Returns:

  • (Boolean)

    Returns whether this term is compilable.



206
207
208
# File 'lib/multiarray/elementwise.rb', line 206

def compilable?
  typecode.compilable? and @values.all? { |value| value.compilable? }
end

#decompose(i) ⇒ Node

Decompose composite elements

This method decomposes composite elements into array.

Returns:

  • (Node)

    Result of decomposition.



196
197
198
199
# File 'lib/multiarray/elementwise.rb', line 196

def decompose( i )
  values = @values.collect { |value| value.decompose i }
  self.class.new( *values ).demand
end

#demandNode, Object

Reevaluate computation

Returns:

See Also:



106
107
108
# File 'lib/multiarray/elementwise.rb', line 106

def demand
  self.class.operation.call *@values
end

#descriptor(hash) ⇒ String

Get unique descriptor of this object

Parameters:

  • hash (Hash)

    Labels for any variables.

Returns:

  • (String)

    Descriptor of this object,



84
85
86
87
# File 'lib/multiarray/elementwise.rb', line 84

def descriptor( hash )
  "#{self.class.descriptor( hash )}" +
    "(#{@values.collect { |value| value.descriptor( hash ) }.join ','})"
end

#element(i) ⇒ Node, Object

Get element of unary operation

Parameters:

  • i (Integer, Node)

    Index of desired element.

Returns:



169
170
171
172
173
174
# File 'lib/multiarray/elementwise.rb', line 169

def element( i )
  values = @values.collect do |value|
    value.dimension == 0 ? value : value.element( i )
  end
  self.class.new( *values ).demand
end

#shapeObject



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

def shape
  shapes = @values.collect { |value| value.shape }
  shapes.inject { |a,b| a.size > b.size ? a : b }
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)

    Returns element-wise operation with elements skipped on each operand.



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

def skip( index, start )
  skipped = *@values.collect { |value| value.skip( index, start ) }
  self.class.new( *skipped ).demand
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.



184
185
186
187
188
189
# File 'lib/multiarray/elementwise.rb', line 184

def slice( start, length )
  values = @values.collect do |value|
    value.dimension == 0 ? value : value.slice( start, length )
  end
  self.class.new( *values ).demand
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:



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

def strip
  stripped = @values.collect { |value| value.strip }
  return stripped.inject([]) { |vars,elem| vars + elem[0] },
       stripped.inject([]) { |values,elem| values + elem[1] },
       self.class.new(*stripped.collect { |elem| elem[2] })
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
# File 'lib/multiarray/elementwise.rb', line 119

def subst( hash )
  self.class.new *@values.collect { |value| value.subst( hash ) }
end

#typecodeObject



89
90
91
92
# File 'lib/multiarray/elementwise.rb', line 89

def typecode
  typecodes = @values.collect { |value| value.typecode }
  self.class.conversion.call *typecodes
end

#variablesSet

Get variables contained in this term

Returns:

  • (Set)

    Returns set of variables.



128
129
130
# File 'lib/multiarray/elementwise.rb', line 128

def variables
  @values.inject(Set[]) { |vars,value| vars + value.variables }
end