Class: CAS::Piecewise

Inherits:
BinaryOp show all
Defined in:
lib/functions/fnc-piecewise.rb,
lib/Mr.CAS/graphviz.rb

Overview

Piecewise function. The function returns when called a result that dependes upon the evaluation of a condition. In practice:

“‘

 /
|  f(x)   if condition(x) is True

<

|  g(x)   otherwise
 \

“‘

From this class other classes will inherit like ‘CAS::Max` and `CAS::Min` classes

Direct Known Subclasses

Max, Min, MinMax

Instance Attribute Summary collapse

Attributes inherited from BinaryOp

#x, #y

Attributes inherited from Op

#x

Instance Method Summary collapse

Methods inherited from BinaryOp

#args, #depend?, #inspect, #simplify, #subs, #subs_lhs, #subs_rhs

Methods inherited from Op

#!=, #*, #**, #+, #-, #-@, #/, #args, #as_proc, #depend?, #equal, #greater, #greater_equal, init_simplify_dict, #inspect, #limit, numeric_to_const, #simplify, simplify_dict, #simplify_dictionary, #smaller, #smaller_equal, #subs, #to_c_lib

Constructor Details

#initialize(x, y, condition) ⇒ Piecewise

Initialize a new piecewise function. It requires first the function that returns when condition is true, than the function when condition is false, and finally the condition that must be of class ‘CAS::Condition`

* **argument**: `CAS::Op` first function
* **argument**: `CAS::Op` second function
* **argument**: `CAS::Condition` evaluated condition
* **returns**: `CAS::Piecewise` new instance


56
57
58
59
60
61
# File 'lib/functions/fnc-piecewise.rb', line 56

def initialize(x, y, condition)
  CAS::Help.assert(condition, CAS::Condition)

  super(x, y)
  @condition = condition
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



46
47
48
# File 'lib/functions/fnc-piecewise.rb', line 46

def condition
  @condition
end

Instance Method Details

#==(op) ⇒ Object

Checks if two ‘CAS::Piecewise` are equal. Checks equality on all functions+ and conditions

* **argument**: `CAS::Op` to be checked against
* **returns**: `TrueClass` or `FalseClass`


99
100
101
102
103
104
105
106
# File 'lib/functions/fnc-piecewise.rb', line 99

def ==(op)
  CAS::Help.assert(op, CAS::Op)
  if self.class != op.class
    return false
  else
    return ((@x == op.x) and (@y == op.y) and (@condition == op.condition))
  end
end

#call(fd) ⇒ Object

Executes the condition. If it is ‘true` it returns the first function, else it returns the value of the second function.

* **argument**: `Hash` with value tables
* **returns**: `Numeric` the result of the call


89
90
91
92
# File 'lib/functions/fnc-piecewise.rb', line 89

def call(fd)
  CAS::Help.assert(fd, Hash)
  (@condition.call(fd) ? @x.call(fd) : @y.call(fd))
end

#diff(v) ⇒ Object

Derivative of a function is performed as derivative of the two internal functions while condition is unchanged

warning

Piecewise functions are in general not differentiable. Thus differentiability

is left to the user

“‘

    /                                     /
d  |  f(x)   if condition(x) is True     | f'(x)   if condition(x) is True

– < = < dx | g(x) otherwise | g’(x) otherwise

\                                     \

“‘

* **argument**: `CAS::Op` argument of derivative
* **returns**: `CAS::Piecewise` with derivated functions and unchanged condition


79
80
81
82
# File 'lib/functions/fnc-piecewise.rb', line 79

def diff(v)
  CAS::Help.assert(v, CAS::Op)
  return CAS::Piecewise.new(@x.diff(v).simplify, @y.diff(v).simplify, @condition)
end

#dot_graphObject

Convert piecewise function into a dot graphviz representation

* **returns**: `String`


94
95
96
97
# File 'lib/Mr.CAS/graphviz.rb', line 94

def dot_graph
  cls = "#{self.class.to_s.gsub("CAS::", "")}_#{self.object_id}"
  "#{cls} -> #{@x.dot_graph}\n  #{cls} -> #{@y.dot_graph}\n  #{cls} -> #{@condition.dot_graph}"
end

#to_codeObject

Convert the piecewise funtion to a String of Ruby code

* **returns**: `String` of code


111
112
113
# File 'lib/functions/fnc-piecewise.rb', line 111

def to_code
  "(#{@condition.to_code} ? (#{@x.to_code}) : (#{@y.to_code}))"
end

#to_latexObject

Convert piecewise function into LaTeX representation

* **returns**: `String` of LaTeX code


125
126
127
# File 'lib/functions/fnc-piecewise.rb', line 125

def to_latex
  "\\left\\{ \\begin{array}{lr} #{@x.to_latex} & #{@condition.to_latex} \\\\ #{@y.to_latex} \\end{array} \\right."
end

#to_sObject

Convert the piecewise function into a String

* **returns**: `String`


118
119
120
# File 'lib/functions/fnc-piecewise.rb', line 118

def to_s
  "(#{@condition} ? #{@x} : #{@y})"
end