class CAS::Variable
Container for a variable. It can be resolved in a numerical value. It can also be used for derivatives.
Attributes
The attribute `name` identifies the current variable. A variable with the same name of an existing variable connot be defined
Public Class Methods
Returns a variable given its name
* **argument**: `Object` name of the variable * **returns**: `CAS::Variable` instance if exists, creates a new variable if does not
# File lib/numbers/variables.rb, line 58 def self.[](s) @@container[s] || CAS::vars(s) end
Returns `true` if a variable already exists
* **argument**: `Object` that represent the variable * **returns**: `TrueClass` if variable exists, `FalseClass` if not
# File lib/numbers/variables.rb, line 66 def self.exist?(name) @@container.keys.include? name end
Returns the `Hash` that contains all the variable
* **returns**: `Hash`
# File lib/numbers/variables.rb, line 43 def self.list @@container end
Variable
is a container for an atomic simbol that becomes a number when `CAS::Op#call` method is used.
* **argument**: `Object` that is a identifier for the variable * **returns**: `CAS::Variable` instance
# File lib/numbers/variables.rb, line 79 def initialize(name) CAS::Help.assert_name name raise CASError, "Variable #{name} already exists" if CAS::Variable.exist? name @name = name @@container[@name] = self end
Overrides new method. This will return an existing variable if in variable container
-
requires: `Object` that is an identifier for the variable
-
returns: new variable instance o
CAS::Op::new
# File lib/numbers/variables.rb, line 90 def Variable.new(name) @@container[name] || super end
Return the number of variable defined
* **returns**: `Fixnum`
# File lib/numbers/variables.rb, line 50 def self.size @@container.keys.size end
Public Instance Methods
Equality operator, the standard operator is overloaded :warning: this operates on the graph, not on the math See `CAS::equal`, etc.
* **argument**: `CAS::Op` to be tested against * **returns**: `TrueClass` if equal, `FalseClass` if differs
# File lib/numbers/variables.rb, line 123 def ==(op) # CAS::Help.assert(op, CAS::Op) if op.is_a? CAS::Variable return self.inspect == op.inspect else false end end
Returns an array containing `self`
* **returns**: `Array` containing `self`
# File lib/numbers/variables.rb, line 170 def args [self] end
Call resolves the operation tree in a `Numeric` (if `Fixnum`) or `Float` (depends upon promotions). As input, it requires an hash with `CAS::Variable` or `CAS::Variable#name` as keys, and a `Numeric` as a value
“` ruby x, y = CAS::vars
:x, :y f = (x ** 2) + (y ** 2) f.call({x => 1, y => 2}) # => 2 “`
* **argument**: `Hash` with feed dictionary * **returns**: `Numeric`
# File lib/numbers/variables.rb, line 146 def call(f) CAS::Help.assert(f, Hash) return f[self] if f[self] return f[@name] if f[@name] end
Returns `TrueClass` if argument of the function is equal to `self`
* **argument**: `CAS::Op` * **returns**: `TrueClass` or `FalseClass`
# File lib/numbers/variables.rb, line 113 def depend?(v) self == v end
Returns the derivative of a variable
“`
dx dx -- = 1; -- = 0 dx dy
“`
* **argument**: `CAS::Op` for the derivative denominator * **returns**: `CAS::Constant`, 0 if not depended, 1 if dependent
# File lib/numbers/variables.rb, line 104 def diff(v) (self == v ? CAS::One : CAS::Zero) end
Inspector for the current object
* **returns**: `String`
# File lib/numbers/variables.rb, line 196 def inspect "Var(#{@name})" end
Simplification callback. The only possible simplification is returning `self`
* **returns**: `CAS::Variable` as `self`
# File lib/numbers/variables.rb, line 204 def simplify self end
Terminal substitutions for variables. If input datatable contains the variable will perform the substitution with the value.
* **argument**: `Hash` of substitutions * **returns**: `CAS::Op` of substitutions
# File lib/numbers/variables.rb, line 180 def subs(dt) CAS::Help.assert(dt, Hash) if dt.keys.include? self if dt[self].is_a? CAS::Op return dt[self] elsif dt[self].is_a? Numeric return CAS::const(dt[self]) else raise CASError, "Impossible subs. Received a #{dt[self].class} = #{dt[self]}" end end end
Convert expression to code (internal, for `CAS::Op#to_proc` method)
* **returns**: `String` that represent Ruby code to be parsed in `CAS::Op#to_proc`
# File lib/numbers/variables.rb, line 163 def to_code "#{@name}" end
Return the local Graphviz node of the tree
* **returns**: `String` of local Graphiz node
# File lib/Mr.CAS/graphviz.rb, line 76 def to_dot "#{@name}" end
Convert expression to string
* **returns**: `String` to print on screen
# File lib/numbers/variables.rb, line 156 def to_s "#{@name}" end