Class: CAS::Sqrt

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

Overview

Square Root of a function. Even if it can be implemented as a power function, it is a separated class.

Instance Attribute Summary

Attributes inherited from Op

#x

Instance Method Summary collapse

Methods inherited from Op

#!=, #*, #**, #+, #-, #[email protected], #/, #==, #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::Op

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`

320
321
322
323
324
# File 'lib/functions/fnc-base.rb', line 320

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

  Math::sqrt @x.call(f)
end

#diff(v) ⇒ Object

Performs the square root between two `CAS::Op`

“`

d

—- √f(x) = 1/2 * f'(x) * √f(x)

dx

“`

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

304
305
306
307
308
309
310
# File 'lib/functions/fnc-base.rb', line 304

def diff(v)
  if @x.depend? v
    return (@x.diff(v) / (CAS.const(2.0) * CAS.sqrt(@x)))
  else
    return CAS::Zero
  end
end

#simplifyObject

Same as `CAS::Op`

Simplifcation engine supports:

* √(x^z) = x^(z - 1/2)
* √x = 0
* √x = 1
* √a = b (constants reduction)

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

343
344
345
346
347
348
349
350
# File 'lib/functions/fnc-base.rb', line 343

def simplify
  super
  return (CAS.pow(@x.x, @x.y - 0.5)).simplify if @x.is_a? CAS::Pow
  return CAS::Zero if @x == CAS::Zero
  return CAS::One if @x == CAS::One
  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`

355
356
357
# File 'lib/functions/fnc-base.rb', line 355

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

#to_sObject

Convert expression to string

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

329
330
331
# File 'lib/functions/fnc-base.rb', line 329

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