Class: Symbolic::Variable

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

Overview

This class is used to create symbolic variables. Symbolic variables presented by name and value. Name is neccessary for printing meaningful symbolic expressions. Value is neccesary for calculation of symbolic expressions. If value isn’t set for variable, but there is an associated proc, then value is taken from evaluating the proc.

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(options = {}, &proc) ⇒ Variable

Create a new Symbolic::Variable, with optional name, value and proc



15
16
17
18
# File 'lib/symbolic/variable.rb', line 15

def initialize(options={}, &proc)
  (@name, @value), @proc = options.values_at(:name, :value), proc
  @name = @name.to_s if @name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/symbolic/variable.rb', line 11

def name
  @name
end

#procObject

Returns the value of attribute proc.



11
12
13
# File 'lib/symbolic/variable.rb', line 11

def proc
  @proc
end

#valueObject



20
21
22
# File 'lib/symbolic/variable.rb', line 20

def value
  @value || @proc && @proc.call.value
end

Instance Method Details

#diff(wrt) ⇒ Object



39
40
41
42
# File 'lib/symbolic/variable.rb', line 39

def diff(wrt)
  return 1 if self == wrt
  0
end

#subs(to_replace, replacement = nil, expect_numeric = false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/symbolic/variable.rb', line 28

def subs(to_replace, replacement=nil, expect_numeric = false)
  if replacement == nil and to_replace.is_a?(Hash)
	super(to_replace)
  else
	return replacement if self == to_replace
	#Consider the possibility that @value is not numeric?
	return self.value if expect_numeric
	self
  end
end

#variablesObject



24
25
26
# File 'lib/symbolic/variable.rb', line 24

def variables
  [self]
end