Class: CAS::Variable

Inherits:
Op
  • Object
show all
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

Attributes inherited from Op

#x

Class Method Summary collapse

Instance Method Summary collapse

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

Raises:



56
57
58
59
60
61
# 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

Instance Attribute Details

#nameObject (readonly)

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



49
50
51
# File 'lib/numbers/variables.rb', line 49

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


35
36
37
# File 'lib/numbers/variables.rb', line 35

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

Returns:

  • (Boolean)


43
44
45
# File 'lib/numbers/variables.rb', line 43

def self.exist?(name)
  @@container.keys.include? name
end

.listObject

Returns the ‘Hash` that contains all the variable

* **returns**: `Hash`


20
21
22
# File 'lib/numbers/variables.rb', line 20

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



67
68
69
# File 'lib/numbers/variables.rb', line 67

def Variable.new(name)
  @@container[name] || super
end

.sizeObject

Return the number of variable defined

* **returns**: `Fixnum`


27
28
29
# File 'lib/numbers/variables.rb', line 27

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


100
101
102
103
104
105
106
107
# 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

#argsObject

Returns an array containing ‘self`

* **returns**: `Array` containing `self`


147
148
149
# File 'lib/numbers/variables.rb', line 147

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`


123
124
125
126
127
128
# 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) ⇒ Boolean

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

* **argument**: `CAS::Op`
* **returns**: `TrueClass` or `FalseClass`

Returns:

  • (Boolean)


90
91
92
# File 'lib/numbers/variables.rb', line 90

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


81
82
83
# File 'lib/numbers/variables.rb', line 81

def diff(v)
  (self == v ? CAS::One : CAS::Zero)
end

#inspectObject

Inspector for the current object

* **returns**: `String`


173
174
175
# File 'lib/numbers/variables.rb', line 173

def inspect
  "Var(#{@name})"
end

#simplifyObject

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

* **returns**: `CAS::Variable` as `self`


181
182
183
# File 'lib/numbers/variables.rb', line 181

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


157
158
159
160
161
162
163
164
165
166
167
168
# 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_codeObject

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`


140
141
142
# File 'lib/numbers/variables.rb', line 140

def to_code
  "#{@name}"
end

#to_dotObject

Return the local Graphviz node of the tree

* **returns**: `String` of local Graphiz node


53
54
55
# File 'lib/Mr.CAS/graphviz.rb', line 53

def to_dot
  "#{@name}"
end

#to_sObject

Convert expression to string

* **returns**: `String` to print on screen


133
134
135
# File 'lib/numbers/variables.rb', line 133

def to_s
  "#{@name}"
end