Class: Keisan::Functions::ExpressionFunction

Inherits:
Keisan::Function show all
Defined in:
lib/keisan/functions/expression_function.rb

Instance Attribute Summary collapse

Attributes inherited from Keisan::Function

#arity, #name

Instance Method Summary collapse

Methods inherited from Keisan::Function

#unbound_variables

Constructor Details

#initialize(name, arguments, expression, transient_definitions) ⇒ ExpressionFunction

Returns a new instance of ExpressionFunction.



6
7
8
9
10
11
12
13
14
15
# File 'lib/keisan/functions/expression_function.rb', line 6

def initialize(name, arguments, expression, transient_definitions)
  super(name, arguments.count)
  if expression.is_a?(::String)
    @expression = AST::parse(expression)
  else
    @expression = expression.deep_dup
  end
  @arguments = arguments
  @transient_definitions = transient_definitions
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



4
5
6
# File 'lib/keisan/functions/expression_function.rb', line 4

def arguments
  @arguments
end

#expressionObject (readonly)

Returns the value of attribute expression.



4
5
6
# File 'lib/keisan/functions/expression_function.rb', line 4

def expression
  @expression
end

Instance Method Details

#call(context, *args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/keisan/functions/expression_function.rb', line 23

def call(context, *args)
  validate_arguments!(args.count)

  local = local_context_for(context)
  arguments.each.with_index do |arg_name, i|
    local.register_variable!(arg_name, args[i])
  end

  expression.value(local)
end

#differentiate(ast_function, variable, context = nil) ⇒ Object

Multi-argument functions work as follows: Given f(x, y), in general we will take the derivative with respect to t, and x = x(t), y = y(t). For instance d/dt f(2*t, t+1). In this case, chain rule gives derivative: dx(t)/dt * f_x(x(t), y(t)) + dy(t)/dt * f_y(x(t), y(t)), where f_x and f_y are the x and y partial derivatives respectively.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/keisan/functions/expression_function.rb', line 78

def differentiate(ast_function, variable, context = nil)
  validate_arguments!(ast_function.children.count)

  local = local_context_for(context)

  argument_values = ast_function.children.map {|child| child.evaluated(local)}

  argument_derivatives = ast_function.children.map do |child|
    child.differentiated(variable, context)
  end

  partial_derivatives = calculate_partial_derivatives(context)

  AST::Plus.new(
    argument_derivatives.map.with_index {|argument_derivative, i|
      partial_derivative = partial_derivatives[i]
      argument_variables.each.with_index {|argument_variable, j|
        partial_derivative = partial_derivative.replaced(argument_variable, argument_values[j])
      }
      AST::Times.new([argument_derivative, partial_derivative])
    }
  )
end

#evaluate(ast_function, context = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/keisan/functions/expression_function.rb', line 42

def evaluate(ast_function, context = nil)
  validate_arguments!(ast_function.children.count)

  context ||= Context.new
  local = local_context_for(context)

  argument_values = ast_function.children.map {|child| child.evaluate(context)}

  arguments.each.with_index do |arg_name, i|
    local.register_variable!(arg_name, argument_values[i].evaluate(context))
  end

  expression.evaluated(local)
end

#freezeObject



17
18
19
20
21
# File 'lib/keisan/functions/expression_function.rb', line 17

def freeze
  @arguments.freeze
  @expression.freeze
  super
end

#simplify(ast_function, context = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/keisan/functions/expression_function.rb', line 57

def simplify(ast_function, context = nil)
  validate_arguments!(ast_function.children.count)

  ast_function.instance_variable_set(
    :@children,
    ast_function.children.map {|child| child.evaluate(context)}
  )

  if ast_function.children.all? {|child| child.is_a?(AST::ConstantLiteral)}
    value(ast_function, context).to_node.simplify(context)
  else
    ast_function
  end
end

#value(ast_function, context = nil) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/keisan/functions/expression_function.rb', line 34

def value(ast_function, context = nil)
  validate_arguments!(ast_function.children.count)

  context ||= Context.new
  argument_values = ast_function.children.map {|child| child.value(context)}
  call(context, *argument_values)
end