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

Constructor Details

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

Returns a new instance of ExpressionFunction.



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

def initialize(name, arguments, expression, transient_definitions)
  super(name, arguments.count)
  @expression = expression.deep_dup
  @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



13
14
15
16
17
18
19
20
21
22
# File 'lib/keisan/functions/expression_function.rb', line 13

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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/keisan/functions/expression_function.rb', line 68

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

  local = local_context_for(context)

  # expression.differentiate(variable, context)

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

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

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

#evaluate(ast_function, context = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/keisan/functions/expression_function.rb', line 32

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

#simplify(ast_function, context = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/keisan/functions/expression_function.rb', line 47

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



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

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