class CAS::Piecewise
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
Attributes
Public Class Methods
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
CAS::BinaryOp::new
# 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
Public Instance Methods
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`
# 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
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
# 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
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
# 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
Convert piecewise function into a dot graphviz representation
* **returns**: `String`
# 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
Convert the piecewise funtion to a String of Ruby code
* **returns**: `String` of code
# File lib/functions/fnc-piecewise.rb, line 111 def to_code "(#{@condition.to_code} ? (#{@x.to_code}) : (#{@y.to_code}))" end
Convert piecewise function into LaTeX representation
* **returns**: `String` of LaTeX code
# 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
Convert the piecewise function into a String
* **returns**: `String`
# File lib/functions/fnc-piecewise.rb, line 118 def to_s "(#{@condition} ? #{@x} : #{@y})" end