Class: CAS::Abs

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

Overview

Absolute value of a function. It can be also implemented as a Piecewise function.

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


516
517
518
519
520
# File 'lib/functions/fnc-base.rb', line 516

def self.init_simplify_dict
  @simplify_dict = {
    CAS::Zero => CAS::Zero
  }
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. In this case it will call the `Fixnum#overloaded_plus`, that is the old plus function.

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

488
489
490
491
492
493
# File 'lib/functions/fnc-base.rb', line 488

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

  s = (@x.call(f) >= 0 ? 1 : -1)
  return s * @x.call(f)
end

#diff(v) ⇒ Object

Performs the absolute value of a `CAS::Op`

“`

d

—- |f(x)| = f'(x) * (f(x) / |f(x)|)

dx

“`

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

472
473
474
475
476
477
478
# File 'lib/functions/fnc-base.rb', line 472

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

#simplifyObject

Same as `CAS::Op`

Simplifcation engine supports:

* |-x| = x
* |0| = 0

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

510
511
512
513
514
# File 'lib/functions/fnc-base.rb', line 510

def simplify
  super
  return CAS.abs(@x.x) if @x.is_a? CAS::Invert
  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`

525
526
527
# File 'lib/functions/fnc-base.rb', line 525

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

#to_sObject

Convert expression to string

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

498
499
500
# File 'lib/functions/fnc-base.rb', line 498

def to_s
  "|#{@x}|"
end