Class: CAS::Div

Inherits:
BinaryOp show all
Defined in:
lib/functions/fnc-base.rb

Overview

Division between two functions. A function divided by zero it is considered as an Infinity.

Instance Attribute Summary

Attributes inherited from BinaryOp

#x, #y

Attributes inherited from Op

#x

Instance Method Summary collapse

Methods inherited from BinaryOp

#==, #args, #depend?, #dot_graph, #initialize, #inspect, #subs, #subs_lhs, #subs_rhs

Methods inherited from Op

#!=, #*, #**, #+, #-, #-@, #/, #==, #args, #as_proc, #depend?, #dot_graph, #equal, #greater, #greater_equal, init_simplify_dict, #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::BinaryOp

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. In this case it will call the `Fixnum#overloaded_plus`, that is the old plus function.

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


235
236
237
238
239
# File 'lib/functions/fnc-base.rb', line 235

def call(f)
  CAS::Help.assert(f, Hash)

  @x.call(f).overloaded_div(@y.call(f))
end

#diff(v) ⇒ Object

Performs the division between two ‘CAS::Op`

“‘

d

—- (f(x) / g(x)) = (f’(x) * g(x) - f(x) * g’(x))/(g(x)^2)

dx

“‘

* **argument**: `CAS::Op` argument of derivative
* **returns**: `CAS::Op` derivative


216
217
218
219
220
221
222
223
224
225
# File 'lib/functions/fnc-base.rb', line 216

def diff(v)
  diff_x, diff_y = super v
  if diff_y == CAS::Zero
    return (diff_x/@y)
  elsif diff_x == CAS::Zero
    return CAS.invert(@x * diff_y / CAS.pow(@y, CAS.const(2.0)))
  else
    return ((diff_x * @y) - (diff_y * @x))/CAS.pow(@y, CAS.const(2.0))
  end
end

#simplifyObject

Same as ‘CAS::Op`

Simplifcation engine supports:

* 0 / y = 0
* x / 0 = Inf
* x / 1 = x
* x / Inf = 0
* a / b = c (constants reduction)

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


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/functions/fnc-base.rb', line 259

def simplify
  super
  return self if (@x == CAS::Zero and @y == CAS::Zero)
  return self if (@x == CAS::Infinity and @y == CAS::Infinity)
  return self if (@x == CAS::Infinity and @y == CAS::Zero)
  return self if (@x == CAS::Zero and @y == CAS::Infinity)

  return CAS::Zero if @x == CAS::Zero
  return CAS::Infinity if @y == CAS::Zero
  return @x if @y == CAS::One
  return CAS::Zero if @y == CAS::Infinity
  return CAS::One if @x == @y
  return CAS.const(self.call({})) if (@x.is_a? CAS::Constant and @y.is_a? CAS::Constant)
  return self
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`


278
279
280
# File 'lib/functions/fnc-base.rb', line 278

def to_code
  "(#{@x.to_code} / #{@y.to_code})"
end

#to_sObject

Convert expression to string

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


244
245
246
# File 'lib/functions/fnc-base.rb', line 244

def to_s
  "(#{@x}) / (#{@y})"
end