Module: Dydx::Algebra::Set::Base

Includes:
Helper
Included in:
Cos, E, Log, Log10, Log2, Num, Pi, Sin, Tan
Defined in:
lib/dydx/algebra/set.rb

Constant Summary

Constants included from Helper

Helper::INVERSE_OPE_RELATION, Helper::SUPER_OPE_RELATION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#==, #combinable?, #distributable?, #distributive?, #formula?, #inverse?, #like_term?, #minus1?, #multiple_of?, #negative?, #num?, #one?, #rest, #to_numeric, #zero?

Class Method Details

.included(_klass) ⇒ Object

TODO: Pi should not have attr_accessor



22
23
24
25
# File 'lib/dydx/algebra/set.rb', line 22

def self.included(_klass)
  attr_accessor :n, :x
  alias_method :d, :differentiate
end

Instance Method Details

#differentiate(sym = :x) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dydx/algebra/set.rb', line 80

def differentiate(sym = :x)
  case self
  when Num, Pi, E then e0
  when Symbol     then self == sym ? e1 : e0
  when Sin        then cos(x) * x.d(sym)
  when Cos        then -1 * sin(x) * x.d(sym)
  when Tan        then 1 / (cos(x) ** 2)
  when Log        then x.d(sym) / (x)
  when Log10      then x.d(sym) / (x * log(10))
  when Log2       then x.d(sym) / (x * log(2))
  end
end

#initialize(x = nil) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/dydx/algebra/set.rb', line 27

def initialize(x = nil)
  case self
  when Num
    @n = x
  when Sin, Cos, Tan, Log, Log10, Log2
    @x = x
  end
end

#subst(hash = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dydx/algebra/set.rb', line 65

def subst(hash = {})
  case self
  when Num, Pi, E
    self
  when Symbol
    hash[self] || self
  when Sin    then sin(x.subst(hash))
  when Cos    then cos(x.subst(hash))
  when Tan    then tan(x.subst(hash))
  when Log    then log(x.subst(hash))
  when Log10  then log10(x.subst(hash))
  when Log2   then log2(x.subst(hash))
  end
end

#to_fObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dydx/algebra/set.rb', line 50

def to_f
  case self
  when Num    then n.to_f
  when Pi     then Math::PI
  when E      then Math::E
  when Symbol then fail ArgumentError
  when Sin    then Math.sin(x.to_f)
  when Cos    then Math.cos(x.to_f)
  when Tan    then Math.tan(x.to_f)
  when Log    then Math.log(x.to_f)
  when Log10  then Math.log(x.to_f, 10)
  when Log2   then Math.log(x.to_f, 2)
  end
end

#to_sObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dydx/algebra/set.rb', line 36

def to_s
  case self
  when Num   then n.to_s
  when Pi    then 'pi'
  when E     then 'e'
  when Sin   then "sin( #{x} )"
  when Cos   then "cos( #{x} )"
  when Tan   then "tan( #{x} )"
  when Log   then "log( #{x} )"
  when Log10 then "log10( #{x} )"
  when Log2  then "log2( #{x} )"
  end
end