class CAS::Function
Unknown function class. Will allow to make symbolic differentiation and so on.
Attributes
The attribute `name` identifies the current function. A function with the same name of an existing function connot be defined
Public Class Methods
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 39 def self.[](s) return @@container[s] if self.exist? s raise CASError, "Function #{s} not found" end
Return `true` if a function was already defined
* **argument**: name of the function to be checked
# File lib/numbers/functions.rb, line 24 def self.exist?(name) CAS::Help.assert_name name (@@container[name] ? true : false) end
Return the `Hash` of the functions
* **returns**: `Hash`
# File lib/numbers/functions.rb, line 19 def self.list; @@container; end
CAS::NaryOp::new
# File lib/Mr.CAS/c-opt.rb, line 218 def Function.new(name, *xs) a = super a.c_name = name return a end
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 60 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
Return the number of functions defined
* **returns**: `Fixnum`
# File lib/numbers/functions.rb, line 32 def self.size; @@container.keys.size; end
Public Instance Methods
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 176 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
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 104 def [](i); @x[i]; end
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 92 def args ret = [] @x.each do |e| ret << e.args end ret.flatten.uniq end
# File lib/Mr.CAS/c-opt.rb, line 213 def c_name=(s) CAS::Help.assert_name s @c_name = s end
Trying to call a `CAS::Function` will always return a `CAS::Error`
* **raises**: `CAS::CASError`
# File lib/numbers/functions.rb, line 156 def call(_v) raise CASError, "Cannot call a #{self.class}" end
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 141 def diff(v) # return CAS.declare :"d#{@name}[#{v}]", @x ret = [] @x.each_with_index do |x, k| dx = (x.depend?(v) ? x.diff(v) : CAS::Zero) dfx = CAS.declare :"D#{@name}[#{k}]", @x ret << dx * dfx end return CAS::Zero if ret == [] return ret.inject { |d, e| d += e } end
Returns the inspect string of the function, that is similar to `CAS::Function#to_s`
* **returns**: inspection `String`
# File lib/numbers/functions.rb, line 163 def inspect; self.to_s; end
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 110 def simplify; self; end
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 131 def subs(s) @x.each { |e| e.subs(s) } self end
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 116 def to_code raise CASError, "Ruby code for #{self.class} cannot be generated" end
Returns a description `String` for the `CAS::Function`
* **returns**: `String`
# File lib/numbers/functions.rb, line 168 def to_s "#{@name}(#{@x.map(&:to_s).join(", ")})" end