Class: CAS::Variable
- Defined in:
- lib/numbers/variables.rb,
lib/Mr.CAS/graphviz.rb
Overview
Container for a variable. It can be resolved in a numerical value. It can also be used for derivatives.
Constant Summary collapse
- @@container =
Contains all define variable, in an hash. Variables are accessible through variable name.
{}
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The attribute ‘name` identifies the current variable.
Attributes inherited from Op
Class Method Summary collapse
-
.[](s) ⇒ Object
Returns a variable given its name.
-
.exist?(name) ⇒ Boolean
Returns ‘true` if a variable already exists.
-
.list ⇒ Object
Returns the ‘Hash` that contains all the variable.
-
.new(name) ⇒ Object
Overrides new method.
-
.size ⇒ Object
Return the number of variable defined.
Instance Method Summary collapse
-
#==(op) ⇒ Object
Equality operator, the standard operator is overloaded :warning: this operates on the graph, not on the math See ‘CAS::equal`, etc.
-
#args ⇒ Object
Returns an array containing ‘self`.
-
#call(f) ⇒ Object
Call resolves the operation tree in a ‘Numeric` (if `Fixnum`) or `Float` (depends upon promotions).
-
#depend?(v) ⇒ Boolean
Returns ‘TrueClass` if argument of the function is equal to `self`.
-
#diff(v) ⇒ Object
Returns the derivative of a variable.
-
#initialize(name) ⇒ Variable
constructor
Variable is a container for an atomic simbol that becomes a number when ‘CAS::Op#call` method is used.
-
#inspect ⇒ Object
Inspector for the current object.
-
#simplify ⇒ Object
Simplification callback.
-
#subs(dt) ⇒ Object
Terminal substitutions for variables.
-
#to_code ⇒ Object
Convert expression to code (internal, for ‘CAS::Op#to_proc` method).
-
#to_dot ⇒ Object
Return the local Graphviz node of the tree.
-
#to_s ⇒ Object
Convert expression to string.
Methods inherited from Op
#!=, #*, #**, #+, #-, #-@, #/, #as_proc, #dot_graph, #equal, #greater, #greater_equal, init_simplify_dict, #limit, numeric_to_const, simplify_dict, #simplify_dictionary, #smaller, #smaller_equal, #to_c_lib
Constructor Details
#initialize(name) ⇒ Variable
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
79 80 81 82 83 84 |
# 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 |
Instance Attribute Details
#name ⇒ Object (readonly)
The attribute ‘name` identifies the current variable. A variable with the same name of an existing variable connot be defined
72 73 74 |
# File 'lib/numbers/variables.rb', line 72 def name @name end |
Class Method Details
.[](s) ⇒ Object
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
58 59 60 |
# File 'lib/numbers/variables.rb', line 58 def self.[](s) @@container[s] || CAS::vars(s) end |
.exist?(name) ⇒ Boolean
Returns ‘true` if a variable already exists
* **argument**: `Object` that represent the variable
* **returns**: `TrueClass` if variable exists, `FalseClass` if not
66 67 68 |
# File 'lib/numbers/variables.rb', line 66 def self.exist?(name) @@container.keys.include? name end |
.list ⇒ Object
Returns the ‘Hash` that contains all the variable
* **returns**: `Hash`
43 44 45 |
# File 'lib/numbers/variables.rb', line 43 def self.list @@container end |
.new(name) ⇒ Object
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
90 91 92 |
# File 'lib/numbers/variables.rb', line 90 def Variable.new(name) @@container[name] || super end |
.size ⇒ Object
Return the number of variable defined
* **returns**: `Fixnum`
50 51 52 |
# File 'lib/numbers/variables.rb', line 50 def self.size @@container.keys.size end |
Instance Method Details
#==(op) ⇒ Object
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
123 124 125 126 127 128 129 130 |
# 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 |
#args ⇒ Object
Returns an array containing ‘self`
* **returns**: `Array` containing `self`
170 171 172 |
# File 'lib/numbers/variables.rb', line 170 def args [self] end |
#call(f) ⇒ Object
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(=> 1, y => 2) # => 2 “`
* **argument**: `Hash` with feed dictionary
* **returns**: `Numeric`
146 147 148 149 150 151 |
# 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 |
#depend?(v) ⇒ Boolean
Returns ‘TrueClass` if argument of the function is equal to `self`
* **argument**: `CAS::Op`
* **returns**: `TrueClass` or `FalseClass`
113 114 115 |
# File 'lib/numbers/variables.rb', line 113 def depend?(v) self == v end |
#diff(v) ⇒ Object
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
104 105 106 |
# File 'lib/numbers/variables.rb', line 104 def diff(v) (self == v ? CAS::One : CAS::Zero) end |
#inspect ⇒ Object
Inspector for the current object
* **returns**: `String`
196 197 198 |
# File 'lib/numbers/variables.rb', line 196 def inspect "Var(#{@name})" end |
#simplify ⇒ Object
Simplification callback. The only possible simplification is returning ‘self`
* **returns**: `CAS::Variable` as `self`
204 205 206 |
# File 'lib/numbers/variables.rb', line 204 def simplify self end |
#subs(dt) ⇒ Object
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
180 181 182 183 184 185 186 187 188 189 190 191 |
# 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 |
#to_code ⇒ Object
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`
163 164 165 |
# File 'lib/numbers/variables.rb', line 163 def to_code "#{@name}" end |
#to_dot ⇒ Object
Return the local Graphviz node of the tree
* **returns**: `String` of local Graphiz node
76 77 78 |
# File 'lib/Mr.CAS/graphviz.rb', line 76 def to_dot "#{@name}" end |
#to_s ⇒ Object
Convert expression to string
* **returns**: `String` to print on screen
156 157 158 |
# File 'lib/numbers/variables.rb', line 156 def to_s "#{@name}" end |