Class: CAS::Pow

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

Overview

Power function.

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`


141
142
143
144
# File 'lib/functions/fnc-base.rb', line 141

def call(f)
  CAS::Help.assert(f, Hash)
  @x.call(f).overloaded_pow(@y.call(f))
end

#diff(v) ⇒ Object

Performs the power between two ‘CAS::Op`

“‘

d

—- (f(x)^a) = f(x)^(a - 1) * a * f’(x)

dx

 d

—- (a^f(x)) = a^f(x) * f’(x) * ln a

dx

 d

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

dx

“‘

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


122
123
124
125
126
127
128
129
130
131
# File 'lib/functions/fnc-base.rb', line 122

def diff(v)
  diff_x, diff_y = super v
  if diff_y == CAS::Zero
    return ((@x ** (@y - 1.0)) * @y * diff_x)
  elsif diff_x == CAS::Zero
    return (@x ** @y) * diff_y * CAS.ln(@x)
  else
    return (@x ** @y) * ((diff_y * CAS.ln(@x)) + (@y * diff_x / @x))
  end
end

#simplifyObject

Same as ‘CAS::Op`

Simplifcation engine supports:

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

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


164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/functions/fnc-base.rb', line 164

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::One if @x == CAS::One
  return @x if @y == CAS::One
  return CAS::One if @y == CAS::Zero
  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`


182
183
184
# File 'lib/functions/fnc-base.rb', line 182

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

#to_sObject

Convert expression to string

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


149
150
151
# File 'lib/functions/fnc-base.rb', line 149

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