Class: Symbolic::Variable
- Inherits:
-
Object
- Object
- Symbolic::Variable
- 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
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#proc ⇒ Object
Returns the value of attribute proc.
- #value ⇒ Object
Instance Method Summary collapse
- #diff(wrt) ⇒ Object
-
#initialize(options = {}, &proc) ⇒ Variable
constructor
Create a new Symbolic::Variable, with optional name, value and proc.
- #subs(to_replace, replacement = nil, expect_numeric = false) ⇒ Object
- #variables ⇒ Object
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(={}, &proc) (@name, @value), @proc = .values_at(:name, :value), proc @name = @name.to_s if @name end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/symbolic/variable.rb', line 11 def name @name end |
#proc ⇒ Object
Returns the value of attribute proc.
11 12 13 |
# File 'lib/symbolic/variable.rb', line 11 def proc @proc end |
#value ⇒ Object
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 |
#variables ⇒ Object
24 25 26 |
# File 'lib/symbolic/variable.rb', line 24 def variables [self] end |