Class: Keisan::Functions::ExpressionFunction
- Inherits:
-
Keisan::Function
- Object
- Keisan::Function
- Keisan::Functions::ExpressionFunction
- Defined in:
- lib/keisan/functions/expression_function.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
Attributes inherited from Keisan::Function
Instance Method Summary collapse
- #call(context, *args) ⇒ Object
-
#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).
- #evaluate(ast_function, context = nil) ⇒ Object
-
#initialize(name, arguments, expression, transient_definitions) ⇒ ExpressionFunction
constructor
A new instance of ExpressionFunction.
- #simplify(ast_function, context = nil) ⇒ Object
- #value(ast_function, context = nil) ⇒ Object
Methods inherited from Keisan::Function
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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
4 5 6 |
# File 'lib/keisan/functions/expression_function.rb', line 4 def arguments @arguments end |
#expression ⇒ Object (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
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/keisan/functions/expression_function.rb', line 17 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.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/keisan/functions/expression_function.rb', line 72 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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/keisan/functions/expression_function.rb', line 36 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/keisan/functions/expression_function.rb', line 51 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
28 29 30 31 32 33 34 |
# File 'lib/keisan/functions/expression_function.rb', line 28 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 |