module Dydx::Algebra::Set::Base

Public Class Methods

included(_klass) click to toggle source

TODO: Pi should not have attr_accessor

# File lib/dydx/algebra/set.rb, line 22
def self.included(_klass)
  attr_accessor :n, :x
  alias_method :d, :differentiate
end
new(x = nil) click to toggle source
# 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

Public Instance Methods

differentiate(sym = :x) click to toggle source
# 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
subst(hash = {}) click to toggle source
# 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_f() click to toggle source
# 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_s() click to toggle source
# 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