Class: Symbolic::Function

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, deriv = nil, op = nil, &block) ⇒ Function

deriv can be either a function or a proc that takes an argument – it can be set later operation can be passed as a block, a proc, or set later



7
8
9
10
11
12
13
14
# File 'lib/symbolic/function.rb', line 7

def initialize(name, deriv = nil, op = nil, &block)
  @name, @deriv = name, deriv
  unless op == nil
    @operation = op
  else
    @operation = block
  end
end

Instance Attribute Details

#derivObject (readonly)

Returns the value of attribute deriv.



3
4
5
# File 'lib/symbolic/function.rb', line 3

def deriv
  @deriv
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/symbolic/function.rb', line 3

def name
  @name
end

#operationObject (readonly)

Returns the value of attribute operation.



3
4
5
# File 'lib/symbolic/function.rb', line 3

def operation
  @operation
end

Instance Method Details

#[](arg) ⇒ Object

returns a FunctionWrapper with the argument or a number



28
29
30
31
32
33
34
# File 'lib/symbolic/function.rb', line 28

def [] (arg)
  if arg.is_a?(::Numeric) #take care of the case where arg is a number
    self.call(arg)
  else
    FunctionWrapper.new(arg, self)
  end
end

#arg(arg) ⇒ Object

same but with different syntax



37
38
39
# File 'lib/symbolic/function.rb', line 37

def arg(arg)
  self[arg]
end

#call(arg) ⇒ Object



52
53
54
# File 'lib/symbolic/function.rb', line 52

def call(arg)
  @operation.call(arg)
end

#derivative(arg) ⇒ Object

returns the derivitve with arg plugged in – for use with chainrule



42
43
44
45
46
47
48
49
50
# File 'lib/symbolic/function.rb', line 42

def derivative(arg)
  if @deriv.is_a?(Proc)
    @deriv.call(arg)
  elsif @deriv.is_a?(Function)
    @deriv[arg]
  else  #by process of elimination, it's a Symbolic
    @deriv.subs(Symbolic::Math::Arg,arg)
  end
end

#set_derivative(deriv) ⇒ Object

it may be easier to set the derivitve after the object is made



17
18
19
20
# File 'lib/symbolic/function.rb', line 17

def set_derivative(deriv)
  #only allow it to be set if @derivative is nil
  @deriv = deriv if @deriv == nil
end

#set_operation(op) ⇒ Object



22
23
24
25
# File 'lib/symbolic/function.rb', line 22

def set_operation(op)
  #only allow it to be set if @derivative is nil
  @operation = op if @operation == nil
end