class Keisan::AST::Function

Attributes

name[R]

Public Class Methods

new(arguments = [], name) click to toggle source
Calls superclass method
# File lib/keisan/ast/function.rb, line 6
def initialize(arguments = [], name)
  @name = name
  super(arguments)
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/keisan/ast/function.rb, line 48
def ==(other)
  case other
  when Function
    name == other.name && super
  else
    false
  end
end
differentiate(variable, context = nil) click to toggle source
# File lib/keisan/ast/function.rb, line 83
def differentiate(variable, context = nil)
  function = function_from_context(context)
  function.differentiate(self, variable, context)

rescue Exceptions::UndefinedFunctionError, Exceptions::NotImplementedError
  unless unbound_variables(context).include?(variable.name)
    return Number.new(0)
  end

  self.class.new([self, variable], "diff")
end
evaluate(context = nil) click to toggle source
# File lib/keisan/ast/function.rb, line 57
def evaluate(context = nil)
  context ||= Context.new

  if function_defined?(context)
    function_from_context(context).evaluate(self, context)
  else
    @children = children.map {|child| child.evaluate(context).to_node}
    self
  end
end
evaluate_assignments(context = nil) click to toggle source
# File lib/keisan/ast/function.rb, line 35
def evaluate_assignments(context = nil)
  self
end
function_defined?(context = nil) click to toggle source
# File lib/keisan/ast/function.rb, line 39
def function_defined?(context = nil)
  context ||= Context.new
  context.has_function?(name)
end
function_from_context(context) click to toggle source
# File lib/keisan/ast/function.rb, line 44
def function_from_context(context)
  context.function(name)
end
is_constant?() click to toggle source

Functions cannot be guaranteed to be constant even if the arguments are constants, because there might be randomness involved in the outputs.

# File lib/keisan/ast/function.rb, line 98
def is_constant?
  false
end
simplify(context = nil) click to toggle source
# File lib/keisan/ast/function.rb, line 68
def simplify(context = nil)
  context ||= Context.new

  if function_defined?(context)
    function_from_context(context).simplify(self, context)
  else
    @children = children.map {|child| child.simplify(context).to_node}
    self
  end
end
to_s() click to toggle source
# File lib/keisan/ast/function.rb, line 79
def to_s
  "#{name}(#{children.map(&:to_s).join(',')})"
end
unbound_functions(context = nil) click to toggle source
# File lib/keisan/ast/function.rb, line 25
def unbound_functions(context = nil)
  context ||= Context.new

  functions = children.inject(Set.new) do |res, child|
    res | child.unbound_functions(context)
  end

  context.has_function?(name) ? functions : functions | Set.new([name])
end
unbound_variables(context = nil) click to toggle source
Calls superclass method
# File lib/keisan/ast/function.rb, line 16
def unbound_variables(context = nil)
  context ||= Context.new
  if context.has_function?(name)
    function_from_context(context).unbound_variables(children, context)
  else
    super
  end
end
value(context = nil) click to toggle source
# File lib/keisan/ast/function.rb, line 11
def value(context = nil)
  context ||= Context.new
  function_from_context(context).value(self, context)
end