Class: CAS::Constant

Inherits:
Op
  • Object
show all
Defined in:
lib/numbers/constants.rb,
lib/Mr.CAS/graphviz.rb,
lib/numbers/constants.rb

Overview

Constant is a ‘CAS::Op` container for a `Numeric` value, that is not a `CAS::Variable`, thus its derivative it is always zero

Constant Summary collapse

@@simplify_dict =
{
  0 => CAS::Zero,
  1 => CAS::One,
  Math::PI => CAS::Pi,
  Math::E => CAS::E,
  (1.0/0.0) => CAS::Infinity
}

Instance Attribute Summary

Attributes inherited from Op

#x

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, #to_code

Constructor Details

#initialize(x) ⇒ Constant

Returns a new instance of Constant.



36
37
38
# File 'lib/numbers/constants.rb', line 36

def initialize(x)
  @x = x
end

Instance Method Details

#==(op) ⇒ Object

Check if a constant is equal to another ‘CAS::Op` object

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


107
108
109
110
111
112
113
# File 'lib/numbers/constants.rb', line 107

def ==(op)
  if op.is_a? CAS::Constant
    return @x == op.x
  else
    return false
  end
end

#argsObject

Args of a constant is an empty ‘Array`

* **returns**: `Array` empty


99
100
101
# File 'lib/numbers/constants.rb', line 99

def args
  []
end

#call(_f) ⇒ Object

Calling a constant will return the value of the constant itself.

* **argument**: Unused argument
* **returns**: `Numeric` value of the constant


57
58
59
# File 'lib/numbers/constants.rb', line 57

def call(_f)
  @x
end

#depend?(_v) ⇒ Boolean

There is no dependency in a constant, thus this method will always return false

* **argument**: Unused argument
* **returns**: `FalseClass`

Returns:

  • (Boolean)


66
67
68
# File 'lib/numbers/constants.rb', line 66

def depend?(_v)
  false
end

#diff(_v) ⇒ Object

Evaluates the derivative of a constant. The derivative is always a ‘CAS::Zero`

“‘

d

– c = 0 dx “‘



48
49
50
# File 'lib/numbers/constants.rb', line 48

def diff(_v)
  CAS::Zero
end

#inspectObject

Inspection for ‘CAS::Constant` class

* **returns**: `String`


118
119
120
# File 'lib/numbers/constants.rb', line 118

def inspect
  "Const(#{self})"
end

#simplifyObject

Simplification callback. It simplify the subgraph of each node until all possible simplification are performed (thus the execution time is not deterministic).

* **returns**: `CAS::Op` simplified version


91
92
93
# File 'lib/numbers/constants.rb', line 91

def simplify
  return self
end

#subs(_dt) ⇒ Object

Subs for a constant is a dummy method that returns always ‘self`

* **argument**: Unused argument
* **returns**: `CAS::Constant` that represent `self`


82
83
84
# File 'lib/numbers/constants.rb', line 82

def subs(_dt)
  return self
end

#to_dotObject

Return the local Graphviz node of the tree

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


85
86
87
# File 'lib/Mr.CAS/graphviz.rb', line 85

def to_dot
  "Const(#{@x})"
end

#to_sObject

The string representation of a constant is the value of the constant

* **returns**: `String`


74
75
76
# File 'lib/numbers/constants.rb', line 74

def to_s
  "#{@x}"
end