Class: FastBinaryOperator

Inherits:
BinaryOperator show all
Defined in:
lib/mdarray/fast_operators.rb

Instance Attribute Summary

Attributes inherited from Operator

#arity, #exec_type, #fmap, #force_type, #helper, #name, #other_args, #post_condition, #pre_condition, #type

Instance Method Summary collapse

Methods inherited from BinaryOperator

#initialize

Methods inherited from Operator

#exec, #initialize

Constructor Details

This class inherits a constructor from BinaryOperator

Instance Method Details

#complex_reduce(*args) ⇒ Object





149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mdarray/fast_operators.rb', line 149

def complex_reduce(*args)

  calc = nil

  get_args(*args) do |op1, op2, shape, *other_args|
    # return nil if op2 == nil
    helper = @helper::ComplexReduceBinaryOperator
    calc = @pre_condition_result
    calc = helper.send("apply", calc, op1, op2, @do_func)
  end

  return calc

end

#default(*args) ⇒ Object


A default binary operator takes two arrays where one array can be degenerated (a number and loops through all elements of the arrays applying a given method to them.

For instance, operator ‘+’ in a + b is a default binary operator.




77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mdarray/fast_operators.rb', line 77

def default(*args)

  calc = nil
  get_args(*args) do |op1, op2, shape, *other_args|
    calc = MDArray.build(@type, shape)
    if (@coerced)
      helper = @helper::CoerceBinaryOperator
      helper.send("apply", calc.nc_array, op1, op2, @do_func)
    elsif (@op1.is_a? NumericalMDArray)
      helper = @helper::DefaultBinaryOperator
      helper.send("apply", calc.nc_array, op1, op2, @do_func)
    else
      helper = @helper::DefaultBinaryOperator
      helper.send("apply#{@op1.class}", calc.nc_array, op1, op2, @do_func)
    end
  end
  return calc

end

#fill(*args) ⇒ Object


A fill binary operator takes two arrays where one array can be degenerated (a number) and loops through all elements of the arrays, setting the value of the first array to the values of the second.




103
104
105
106
107
108
109
110
111
# File 'lib/mdarray/fast_operators.rb', line 103

def fill(*args)

  get_args(*args) do |op1, op2, shape, *other_args|
    helper = @helper::FillBinaryOperator
    helper.send("apply", op1, op2)

  end

end

#get_args(*args) {|@op1.nc_array, arg2, @op1.shape, args| ... } ⇒ Object



Yields:

  • (@op1.nc_array, arg2, @op1.shape, args)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mdarray/fast_operators.rb', line 33

def get_args(*args)

  parse_args(*args)

  if (@op1.is_a? NumericalMDArray)
    if (@op2.is_a? Numeric)
      arg2 = @op2
    elsif (@op2.is_a? NumericalMDArray)
      if (!@op1.compatible(@op2))
        raise "Invalid operation - arrays are incompatible"
      end
      arg2 = @op2.nc_array
    else # Operation with another user defined type
      false
      # *TODO: make it more general using coerce if other_val type is not recognized
      # if (arg is not recognized)
      #  self_equiv, arg_equiv = arg.coerce(self)
      #  self_equiv * arg_equiv
      # end
    end
    
  else  # NonNumericalMDArray
    if (!@op1.compatible(@op2))
      raise "Invalid operation - arrays are incompatible"
    end

    # Will not work if we have subclasses!!!!
    if (@op1.class != @op2.class)
      raise "Invalid operation - array are not of compatible types"
    end

    arg2 = @op2.nc_array
  end

  yield @op1.nc_array, arg2, @op1.shape, *args

end

#in_place(*args) ⇒ Object





117
118
119
120
121
122
123
124
# File 'lib/mdarray/fast_operators.rb', line 117

def in_place(*args)

  get_args(*args) do |op1, op2, shape, *other_args|
    helper = @helper::InplaceBinaryOperator
    helper.send("apply", op1, op2, @do_func)
  end

end

#reduce(*args) ⇒ Object





130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/mdarray/fast_operators.rb', line 130

def reduce(*args)

  calc = nil

  get_args(*args) do |op1, op2, shape, *other_args|
    # return nil if op2 == nil
    helper = @helper::ReduceBinaryOperator
    calc = @pre_condition_result
    calc = helper.send("apply", calc, op1, op2, @do_func)
  end

  return calc

end