Class: Symbolic::FunctionWrapper

Inherits:
Object
  • Object
show all
Includes:
Symbolic
Defined in:
lib/symbolic/function.rb

Overview

class combines a function with an argument or arguments this class is what allows functions to be used in symbolic expressions

Constant Summary

Constants included from Symbolic

OPERATIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Symbolic

#*, #**, #+, #+@, #-, #-@, #/, #coerce, #factorial, #inspect, #operations, #taylor, #to_s

Constructor Details

#initialize(arg, fctn) ⇒ FunctionWrapper

Returns a new instance of FunctionWrapper.



63
64
65
# File 'lib/symbolic/function.rb', line 63

def initialize(arg, fctn)
  @argument, @function = arg, fctn
end

Instance Attribute Details

#argumentObject (readonly)

Returns the value of attribute argument.



61
62
63
# File 'lib/symbolic/function.rb', line 61

def argument
  @argument
end

#functionObject (readonly)

Returns the value of attribute function.



61
62
63
# File 'lib/symbolic/function.rb', line 61

def function
  @function
end

Instance Method Details

#==(object) ⇒ Object



97
98
99
# File 'lib/symbolic/function.rb', line 97

def ==(object)
  (object.function == @function and object.argument == @argument) rescue false
end

#detailed_operationsObject



79
80
81
# File 'lib/symbolic/function.rb', line 79

def detailed_operations
  @argument.detailed_operations.tap {|it| it[@operation] += 1}
end

#diff(wrt) ⇒ Object



101
102
103
104
105
# File 'lib/symbolic/function.rb', line 101

def diff(wrt)
  return 0 unless self.variables.member?(wrt)
  #chain rule
  @function.derivative(@argument)  * @argument.diff(wrt)
end

#evalObject

simply dumps @argument in to the function – no gaurentee that the function will know how to handle it. Useful in some circumstances



93
94
95
# File 'lib/symbolic/function.rb', line 93

def eval
  @function.call(@argument)
end

#nameObject



67
68
69
# File 'lib/symbolic/function.rb', line 67

def name
  @function.name
end

#subs(to_replace, replacement = nil) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/symbolic/function.rb', line 83

def subs(to_replace, replacement=nil)
  if replacement == nil and to_replace.is_a?(Hash)
	super(to_replace)
  else
	@function[@argument.subs(to_replace, replacement)]
  end
end

#valueObject



71
72
73
# File 'lib/symbolic/function.rb', line 71

def value
  @function.call(@argument.value)
end

#variablesObject



75
76
77
# File 'lib/symbolic/function.rb', line 75

def variables
  @argument.variables
end