Class: CAS::Exp

Inherits:
Op
  • Object
show all
Defined in:
lib/functions/fnc-trsc.rb

Overview

Representation for the `e^x` function. It is implemented as a `CAS::Op`

Instance Attribute Summary

Attributes inherited from Op

#x

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Op

#!=, #*, #**, #+, #-, #[email protected], #/, #==, #args, #as_proc, #depend?, #dot_graph, #equal, #greater, #greater_equal, #initialize, #inspect, #limit, numeric_to_const, simplify_dict, #simplify_dictionary, #smaller, #smaller_equal, #subs, #to_c_lib

Constructor Details

This class inherits a constructor from CAS::Op

Class Method Details

.init_simplify_dictObject


87
88
89
90
91
92
93
# File 'lib/functions/fnc-trsc.rb', line 87

def self.init_simplify_dict
  @simplify_dict = {
    CAS::Zero => CAS::One,
    CAS::One => CAS::E,
    CAS::Infinity => CAS::Infinity
  }
end

Instance Method Details

#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

* **argument**: `Hash` with feed dictionary
* **returns**: `Numeric`

64
65
66
67
# File 'lib/functions/fnc-trsc.rb', line 64

def call(f)
  CAS::Help.assert(f, Hash)
  Math::exp(@x.call(f))
end

#diff(v) ⇒ Object

Return the derivative of the `sin(x)` function using the chain rule. The input is a `CAS::Op` because it can handle derivatives with respect to functions.

“`

d

– exp(f(x)) = f'(x) exp(f(x)) dx “`

* **argument**: `CAS::Op` object of the derivative
* **returns**: `CAS::Op` a derivated object, or `CAS::Zero` for constants

49
50
51
52
53
54
55
# File 'lib/functions/fnc-trsc.rb', line 49

def diff(v)
  if @x.depend? v
    return @x.diff(v) * CAS.exp(@x)
  else
    return CAS::Zero
  end
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

81
82
83
84
85
# File 'lib/functions/fnc-trsc.rb', line 81

def simplify
  super
  return @x.x if @x.is_a? CAS::Ln
  return self.simplify_dictionary
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`

98
99
100
# File 'lib/functions/fnc-trsc.rb', line 98

def to_code
  "Math::exp(#{@x.to_code})"
end

#to_latexObject

Returns the latex representation of the current Op.

* **returns**: `String`

105
106
107
# File 'lib/functions/fnc-trsc.rb', line 105

def to_latex
  "e^{#{@x.to_latex}}"
end

#to_sObject

Convert expression to string

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

72
73
74
# File 'lib/functions/fnc-trsc.rb', line 72

def to_s
  "exp(#{@x})"
end