Class: CAS::Prod

Inherits:
NaryOp show all
Defined in:
lib/functions/fnc-prod.rb

Overview

Product class. Performs the product between two elements. This class will be soon modified as an n-ary operator.

Instance Attribute Summary

Attributes inherited from NaryOp

#x

Attributes inherited from Op

#x

Instance Method Summary collapse

Methods inherited from NaryOp

#==, #__reduce_constants, #__reduce_multeplicity, #args, #depend?, #dot_graph, #initialize, #inspect, #subs

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::NaryOp

Instance Method Details

#*(op) ⇒ Object

The new element of a sum accumulates inside the vector that holds the elements



39
40
41
42
43
# File 'lib/functions/fnc-prod.rb', line 39

def *(op)
  CAS::Help.assert(op, CAS::Op)
  @x << op
  self
end

#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`


76
77
78
79
80
81
82
83
# File 'lib/functions/fnc-prod.rb', line 76

def call(f)
  CAS::Help.assert(f, Hash)
  p = 1
  @x.each do  |y|
    p = p.overloaded_mul(y.call(f))
  end
  p
end

#diff(v) ⇒ Object

Performs the product between two ‘CAS::Op`

“‘

d

—- (f(x) * g(x) * h(x)) = f’(x) * g(x) * h(x) +

dx
                         + f(x) * g'(x) * h(x) +

                         + f(x) * g(x) * h'(x)

“‘

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


58
59
60
61
62
63
64
65
66
# File 'lib/functions/fnc-prod.rb', line 58

def diff(v)
  xdiff = @x.map { |y| y.diff(v) }

  xdiff.each_with_index { |y, i|
    xdiff[i] = y * CAS::Prod.new(@x[0...i] + @x[(i + 1)..-1])
  }

  return CAS::Sum.new(xdiff)
end

#simplifyObject

Same as ‘CAS::Op`

Simplifcation engine supports:

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

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


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/functions/fnc-prod.rb', line 103

def simplify
  super
  return CAS::Zero if @x.include? CAS::Zero
  @x = @x - [CAS::One]
  return CAS::One if @x.size == 0
  return @x[0] if @x.size == 1

  @x = self.__reduce_constants(@x) do |cs, xs|
    [cs.inject { |t, c| t *= c.call({}) }] + xs
  end

  @x = self.__reduce_multeplicity(@x) do |op, count|
    count > 1 ? (op ** count) : op
  end
  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`


123
124
125
# File 'lib/functions/fnc-prod.rb', line 123

def to_code
  "(#{@x.map(&:to_code).join(" * ")})"
end

#to_sObject

Convert expression to string

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


88
89
90
# File 'lib/functions/fnc-prod.rb', line 88

def to_s
  "(#{@x.map(&:to_s).join(" * ")})"
end