class CAS::Variable

Container for a variable. It can be resolved in a numerical value. It can also be used for derivatives.

Attributes

name[R]

The attribute `name` identifies the current variable. A variable with the same name of an existing variable connot be defined

Public Class Methods

[](s) click to toggle source

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 35
def self.[](s)
  @@container[s] || CAS::vars(s)
end
exist?(name) click to toggle source

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 43
def self.exist?(name)
  @@container.keys.include? name
end
list() click to toggle source

Returns the `Hash` that contains all the variable

* **returns**: `Hash`
# File lib/numbers/variables.rb, line 20
def self.list
  @@container
end
new(name) click to toggle source

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 56
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
new(name) click to toggle source

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

Calls superclass method CAS::Op::new
# File lib/numbers/variables.rb, line 67
def Variable.new(name)
  @@container[name] || super
end
size() click to toggle source

Return the number of variable defined

* **returns**: `Fixnum`
# File lib/numbers/variables.rb, line 27
def self.size
  @@container.keys.size
end

Public Instance Methods

==(op) click to toggle source

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 100
def ==(op)
  # CAS::Help.assert(op, CAS::Op)
  if op.is_a? CAS::Variable
    return self.inspect == op.inspect
  else
    false
  end
end
args() click to toggle source

Returns an array containing `self`

* **returns**: `Array` containing `self`
# File lib/numbers/variables.rb, line 147
def args
  [self]
end
call(f) click to toggle source

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 123
def call(f)
  CAS::Help.assert(f, Hash)

  return f[self] if f[self]
  return f[@name] if f[@name]
end
depend?(v) click to toggle source

Returns `TrueClass` if argument of the function is equal to `self`

* **argument**: `CAS::Op`
* **returns**: `TrueClass` or `FalseClass`
# File lib/numbers/variables.rb, line 90
def depend?(v)
  self == v
end
diff(v) click to toggle source

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 81
def diff(v)
  (self == v ? CAS::One : CAS::Zero)
end
inspect() click to toggle source

Inspector for the current object

* **returns**: `String`
# File lib/numbers/variables.rb, line 173
def inspect
  "Var(#{@name})"
end
simplify() click to toggle source

Simplification callback. The only possible simplification is returning `self`

* **returns**: `CAS::Variable` as `self`
# File lib/numbers/variables.rb, line 181
def simplify
  self
end
subs(dt) click to toggle source

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 157
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() click to toggle source

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 140
def to_code
  "#{@name}"
end
to_dot() click to toggle source

Return the local Graphviz node of the tree

* **returns**: `String` of local Graphiz node
# File lib/Mr.CAS/graphviz.rb, line 53
def to_dot
  "#{@name}"
end
to_s() click to toggle source

Convert expression to string

* **returns**: `String` to print on screen
# File lib/numbers/variables.rb, line 133
def to_s
  "#{@name}"
end