Class: ValueGraphTransformation::Arithmetic::ArithmeticFunctionFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/value_graph_transformation/arithmetic.rb

Overview

A factory for creating and adding basic arithmetic functions to a Context.

Instance Method Summary (collapse)

Constructor Details

- (ArithmeticFunctionFactory) initialize(context)

Returns a new instance of ArithmeticFunctionFactory



19
20
21
# File 'lib/value_graph_transformation/arithmetic.rb', line 19

def initialize(context)
  @context = context
end

Instance Method Details

- (Value) add(term1, term2, result = nil)

Creates function for addition.

Parameters:

  • term1 (Object)

    the first term.

  • term2 (Object)

    the second term.

  • result (Value, Object) (defaults to: nil)

    the optional result identifier, will be created if not given.

Returns:

  • (Value)

    the function's resulting Value node.

Specifications:

behaves like a function factory

creates a function and returns the result node

%3 the function node the result node node0 1337 node3 + node0->node3 node1 42 node1->node3 node2 node3->node2
behaves like a inverse function

returns the inversed function that outputs to vertex 1337.

%3 the function node the result node the inversed function node node0 1337 node3 + node0->node3 node1 42 node1->node3 node4 - node1->node4 node2 node2->node4 node3->node2 node4->node0
behaves like a inverse function

returns the inversed function that outputs to vertex 42.

%3 the function node the result node the inversed function node node0 1337 node3 + node0->node3 node4 - node0->node4 node1 42 node1->node3 node2 node2->node4 node3->node2 node4->node1


29
30
31
# File 'lib/value_graph_transformation/arithmetic.rb', line 29

def add(term1, term2, result=nil)
  single_output_function(Add, [term1, term2], result)
end

- (Value) div(numerator, denominator, result = nil)

Creates function for division.

Parameters:

  • numerator (Object)
  • denominator (Object)
  • result (Value, Object) (defaults to: nil)

    the optional result identifier, will be created if not given.

Returns:

  • (Value)

    the function's resulting Value node.

Specifications:

behaves like a function factory

creates a function and returns the result node

%3 the function node the result node node0 n node3 ÷ node0->node3 node1 d node1->node3 node2 node3->node2
behaves like a inverse function

returns the inversed function that outputs to vertex n.

%3 the function node the result node the inversed function node node0 n node3 ÷ node0->node3 node1 d node1->node3 node4 × node1->node4 node2 node2->node4 node3->node2 node4->node0
behaves like a inverse function

returns the inversed function that outputs to vertex d.

%3 the function node the result node the inversed function node node0 n node3 ÷ node0->node3 node4 ÷ node0->node4 node1 d node1->node3 node2 node2->node4 node3->node2 node4->node1


68
69
70
# File 'lib/value_graph_transformation/arithmetic.rb', line 68

def div(numerator, denominator, result=nil)
  single_output_function(Div, [numerator, denominator], result)
end

- (Value) mul(factor1, factor2, result = nil)

Creates function for multiplication.

Parameters:

  • factor1 (Object)

    the first factor.

  • factor2 (Object)

    the second factor.

  • result (Value, Object) (defaults to: nil)

    the optional result identifier, will be created if not given.

Returns:

  • (Value)

    the function's resulting Value node.

Specifications:

behaves like a function factory

creates a function and returns the result node

%3 the function node the result node node0 13 node3 × node0->node3 node1 a node1->node3 node2 node3->node2
behaves like a inverse function

returns the inversed function that outputs to vertex 13.

%3 the function node the result node the inversed function node node0 13 node3 × node0->node3 node1 a node1->node3 node4 ÷ node1->node4 node2 node2->node4 node3->node2 node4->node0
behaves like a inverse function

returns the inversed function that outputs to vertex a.

%3 the function node the result node the inversed function node node0 13 node3 × node0->node3 node4 ÷ node0->node4 node1 a node1->node3 node2 node2->node4 node3->node2 node4->node1


58
59
60
# File 'lib/value_graph_transformation/arithmetic.rb', line 58

def mul(factor1, factor2, result=nil)
  single_output_function(Mul, [factor1, factor2], result)
end

- (Value) single_output_function(type, input, output = nil)

Creates a Function with the given type and input identifiers, and returns the result Value so that several invocations can be nested together.

Parameters:

  • type (Class)

    the Function class to instantiate.

  • input (Array<Object>)

    an array of identifiers or Value objects.

  • output (Object) (defaults to: nil)

    optional identifier or Value, will be created if not given.

Returns:

  • (Value)

    the result Value.



80
81
82
83
84
85
# File 'lib/value_graph_transformation/arithmetic.rb', line 80

def single_output_function(type, input, output=nil)
  function = @context.function(type, values(input), values([output]))
  fail "#{type} not a single output function" unless function.targets.size == 1

  function.targets.first.target
end

- (Value) sub(term1, term2, result = nil)

Creates function for subtraction.

Parameters:

  • term1 (Object)

    the first term.

  • term2 (Object)

    the second term.

  • result (Value, Object) (defaults to: nil)

    the optional result identifier, will be created if not given.

Returns:

  • (Value)

    the function's resulting Value node.

Specifications:

behaves like a function factory

creates a function and returns the result node

%3 the function node the result node node0 1337 node3 - node0->node3 node1 42 node1->node3 node2 node3->node2
behaves like a inverse function

returns the inversed function that outputs to vertex 1337.

%3 the function node the result node the inversed function node node0 1337 node3 - node0->node3 node1 42 node1->node3 node4 + node1->node4 node2 node2->node4 node3->node2 node4->node0
behaves like a inverse function

returns the inversed function that outputs to vertex 42.

%3 the function node the result node the inversed function node node0 1337 node3 - node0->node3 node4 - node0->node4 node1 42 node1->node3 node2 node2->node4 node3->node2 node4->node1


48
49
50
# File 'lib/value_graph_transformation/arithmetic.rb', line 48

def sub(term1, term2, result=nil)
  single_output_function(Sub, [term1, term2], result)
end

- (Value) sum(terms, result = nil)

Creates a function for addition, with multiple terms.

Parameters:

  • terms (Array<Object>)

    an array of identifiers or Value objects.

  • result (Value, Object) (defaults to: nil)

    the optional result identifier, will be created if not given.

Returns:

  • (Value)

    the function's resulting Value node.



38
39
40
# File 'lib/value_graph_transformation/arithmetic.rb', line 38

def sum(terms, result=nil)
  single_output_function(Add, terms, result)
end

- (Array<Value>) values(objs)

Returns the collection of Value nodes for the given objects.

Parameters:

  • objs (Array<Object>)

    a collection of identifiers.

Returns:

  • (Array<Value>)

    the collection of Value nodes for the given objects.



89
90
91
# File 'lib/value_graph_transformation/arithmetic.rb', line 89

def values(objs)
  objs.collect{|obj| obj.is_a?(Value) ? obj : @context.value_for(obj)}
end