Module: Hornetseye::Methods

Included in:
Math
Defined in:
lib/multiarray/methods.rb

Class Method Summary collapse

Class Method Details

.define_binary_method(mod, op, coercion = :coercion) ⇒ Object

Extend binary method with capability to handle arrays.

Parameters:

  • mod (Module)

    The mathematics module.

  • op (Symbol, String)

    The binary method to extend.

  • conversion (Symbol, String)

    A method for doing the type balancing.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/multiarray/methods.rb', line 81

def define_binary_method( mod, op, coercion = :coercion )
  mod.module_eval do
    define_method( "#{op}_with_hornetseye" ) do |a,b|
      if a.is_a? Node or b.is_a? Node
        a = Node.match( a, b ).new a unless a.is_a? Node
        b = Node.match( b, a ).new b unless b.is_a? Node
        if a.dimension == 0 and a.variables.empty? and
           b.dimension == 0 and b.variables.empty?
          target = a.typecode.send coercion, b.typecode
          target.new mod.send( op, a.simplify.get, b.simplify.get )
        else
          Hornetseye::ElementWise( lambda { |x,y| mod.send op, x, y },
                                   "#{mod}.#{op}",
                                   lambda { |t,u| t.send coercion, u } ).
            new( a, b ).force
        end
      else
        send "#{op}_without_hornetseye", a, b
      end
    end
    alias_method_chain op, :hornetseye
    module_function "#{op}_without_hornetseye"
    module_function op
  end
end

.define_unary_method(mod, op, conversion = :contiguous) ⇒ Object

Extend unary method with capability to handle arrays.

Parameters:

  • mod (Module)

    The mathematics module.

  • op (Symbol, String)

    The unary method to extend.

  • conversion (Symbol, String) (defaults to: :contiguous)

    A method for doing the type conversion.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/multiarray/methods.rb', line 51

def define_unary_method( mod, op, conversion = :contiguous )
  mod.module_eval do
    define_method( "#{op}_with_hornetseye" ) do |a|
      if a.is_a? Node
        if a.dimension == 0 and a.variables.empty?
          target = a.typecode.send conversion
          target.new mod.send( op, a.simplify.get )
        else
          Hornetseye::ElementWise( lambda { |x| mod.send op, x },
                                   "#{mod}.#{op}",
                                   lambda { |x| x.send conversion } ).
            new( a ).force
        end
      else
        send "#{op}_without_hornetseye", a
      end
    end
    alias_method_chain op, :hornetseye
    module_function "#{op}_without_hornetseye"
    module_function op
  end
end

.included(mod) ⇒ Object

Extend some methods in the specified module.

Parameters:

  • mod (Module)

    The mathematics module.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/multiarray/methods.rb', line 25

def Methods.included( mod )
  define_unary_method  mod, :sqrt , :float
  define_unary_method  mod, :log  , :float
  define_unary_method  mod, :log10, :float
  define_unary_method  mod, :exp  , :float
  define_unary_method  mod, :cos  , :float
  define_unary_method  mod, :sin  , :float
  define_unary_method  mod, :tan  , :float
  define_unary_method  mod, :acos , :float
  define_unary_method  mod, :asin , :float
  define_unary_method  mod, :atan , :float
  define_unary_method  mod, :cosh , :float
  define_unary_method  mod, :sinh , :float
  define_unary_method  mod, :tanh , :float
  define_unary_method  mod, :acosh, :float
  define_unary_method  mod, :asinh, :float
  define_unary_method  mod, :atanh, :float
  define_binary_method mod, :atan2, :floating
  define_binary_method mod, :hypot, :floating
end