class CAS::Function

Unknown function class. Will allow to make symbolic differentiation and so on.

Attributes

name[R]

The attribute `name` identifies the current function. A function with the same name of an existing function connot be defined

Public Class Methods

[](s) click to toggle source

Returns a function given its name

* **argument**: `Object` name of the function
* **returns**: `CAS::Function` instance if exists, raises a `CAS::CASError`
  if not
# File lib/numbers/functions.rb, line 62
def self.[](s)
  return @@container[s] if self.exist? s
  raise CASError, "Function #{s} not found"
end
exist?(name) click to toggle source

Return `true` if a function was already defined

* **argument**: name of the function to be checked
# File lib/numbers/functions.rb, line 47
def self.exist?(name)
  CAS::Help.assert_name name
  (@@container[name] ? true : false)
end
list() click to toggle source

Return the `Hash` of the functions

* **returns**: `Hash`
# File lib/numbers/functions.rb, line 42
def self.list; @@container; end
new(name, *xs) click to toggle source
Calls superclass method CAS::NaryOp::new
# File lib/Mr.CAS/c-opt.rb, line 241
def Function.new(name, *xs)
  a = super
  a.c_name = name
  return a
end
new(name, *xs) click to toggle source

Initializes a new function. It requires a name and a series of arguments that will be the functions on which it depends.

* **argument**: `String` or `Symbol` name of the variable
* **argument**: `Array` of `CAS::Variable` that are argument of the function
* **returns**: `CAS::Function`
# File lib/numbers/functions.rb, line 83
def initialize(name, *xs)
  xs.flatten!
  CAS::Help.assert_name name
  xs.each do |x|
    CAS::Help.assert x, CAS::Op
  end
  # raise CASError, "Function #{name} already exists" if CAS::Function.exist? name

  @x = xs.uniq
  @name = name
  @@container[@name] = self
end
size() click to toggle source

Return the number of functions defined

* **returns**: `Fixnum`
# File lib/numbers/functions.rb, line 55
def self.size; @@container.keys.size; end

Public Instance Methods

==(op) click to toggle source

Checks if two functions can be considered equal (same name, same args)

* **requires**: another op to be checked against
* **returns**: `TrueClass` if functions are equal, `FalseClass` if not equal
# File lib/numbers/functions.rb, line 203
def ==(op)
  return false if not self.class == op.class
  return false if not (@name == op.name and @x.uniq == op.x.uniq)
  true
end
[](i) click to toggle source

Get an element in a particular position of the argument

* **requires**: an iterator
* **returns**: element of the argument `CAS::Op` or `NilClass`
# File lib/numbers/functions.rb, line 127
def [](i); @x[i]; end
args() click to toggle source

Returns an array containing `CAS::Variable`s argument of the function Plese note that sub Op will return their args.

* **returns**: `Array` containing `CAs::Variable`
# File lib/numbers/functions.rb, line 115
def args
  ret = []
  @x.each do |e|
    ret << e.args
  end
  ret.flatten.uniq
end
c_name=(s) click to toggle source
# File lib/Mr.CAS/c-opt.rb, line 236
def c_name=(s)
  CAS::Help.assert_name s
  @c_name = s
end
call(_v) click to toggle source

Trying to call a `CAS::Function` will always return a `CAS::Error`

* **raises**: `CAS::CASError`
# File lib/numbers/functions.rb, line 181
def call(_v)
  raise CASError, "Cannot call a #{self.class}"
end
diff(v) click to toggle source

Performs the derivative with respect to one of the variable. The new function has a name with respect to a schema that for now is fixed (TODO: make it variable and user defined).

* **requires**: a `CAS::Variable` for derivative
* **returns**: the `CAS::Variable` derivated function
# File lib/numbers/functions.rb, line 164
def diff(v)
  # return CAS.declare :"d#{@name}[#{v}]", @x
  ret = []
  @x.each_with_index do |y, k|
    dy = (y.depend?(v) ? y.diff(v) : CAS::Zero)
    dfy = CAS.declare :"D#{@name}[#{k}]", @x
    ret << (dy * dfy)
  end
  return CAS::Zero if ret == []
  a = ret[0]
  ret[1..-1].each { |e| a += e } if ret.size > 1
  return a
end
simplify() click to toggle source

Simplifications cannot be performed on anonymous function, thus it will always return the `self` `CAS::Function` object

* **returns**: `CAS::Function` self instance
# File lib/numbers/functions.rb, line 133
def simplify; self; end
subs(s) click to toggle source

Substitutions in which a function is involved directly generates a CAS::Error unless the substitution will involve another variable. Example:

“` ruby (CAS.declare :f [x, y, z]).subs { x => x ** 2 } # this raises CASError (CAS.declare :f [x, y, z]).subs { x => y } # this returns f(y, z) “`

* **requires**: a substitution `Hash`
* **returns**: a `CAS::Function` with modified argument list
* **raises**: `CASError` if something different with resppect to a `CAS::Variable` is a active substitution
# File lib/numbers/functions.rb, line 154
def subs(s)
  @x.each { |e| e.subs(s) }
  self
end
to_code() click to toggle source

Tries to convert an anonymous function into Ruby code will always raise a `CASError` because it is not possible to generate code for such a fuction

* **raises**: `CAS::CASError`: Ruby code for CAs::Function cannot be generated
# File lib/numbers/functions.rb, line 139
def to_code
  raise CASError, "Ruby code for #{self.class} cannot be generated"
end
to_s() click to toggle source

Returns a description `String` for the `CAS::Function`

* **returns**: `String`
# File lib/numbers/functions.rb, line 195
def to_s
  "#{@name}(#{@x.map(&:to_s).join(", ")})"
end