Class: Function
- Inherits:
-
Object
- Object
- Function
- Defined in:
- lib/linmeric/Function_class.rb
Overview
This class provides a simple representation of a function and some method to manipulate it
- Author
-
Massimiliano Dal Mas ([email protected])
- License
-
Distributed under MIT license
Instance Method Summary collapse
-
#initialize(str) ⇒ Function
constructor
Creates a new ‘Function` object * argument: string to be converted (and representing a function).
-
#integrate(r, n, m = 'simpson') ⇒ Object
Integrates the function according to the specified method (see: Integrators).
-
#ok? ⇒ Boolean
Tells whether the function has been correctly converted from the string format.
-
#to_block(n = 0) ⇒ Object
Returns the function as a ruby block ({ |…| … }) * argument: number of variables needed for the block; default value is set if n == 0 * returns: string that represents the block;
nilif an error is found. -
#to_s ⇒ Object
-
returns: string representation of the function.
-
-
#vars(n = 0) ⇒ Object
Builds the list of the variables needed for the block.
Constructor Details
#initialize(str) ⇒ Function
Creates a new ‘Function` object
-
argument: string to be converted (and representing a function)
17 18 19 20 21 22 |
# File 'lib/linmeric/Function_class.rb', line 17 def initialize(str) @inp = str @vars = [] @i = 0 @funct = convert(str) end |
Instance Method Details
#integrate(r, n, m = 'simpson') ⇒ Object
Integrates the function according to the specified method (see: Integrators)
-
argument: dimension of the range; see: Dim
-
argument: number of points for integration
-
argument: method to integrate the function (optional). ‘simpson’ is default
-
returns:
-
Numeric value as result
-
nilif an error occourred
-
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/linmeric/Function_class.rb', line 85 def integrate(r,n,m = 'simpson') return nil unless self.ok? block = self.to_block(1) unless block == nil m = (m - '"').downcase case m when "trapezes" return eval("Integrators.trapezi(r.sx,r.dx,n)#{block}") when "midpoint" return eval("Integrators.simpson(r.sx,r.dx,n)#{block}") when "simpson" return eval("Integrators.simpson(r.sx,r.dx,n)#{block}") when "rectl" return eval("Integrators.rettsx(r.sx,r.dx,n)#{block}") when "rectr" return eval("Integrators.rettdx(r.sx,r.dx,n)#{block}") when "boole" return eval("Integrators.boole(r.sx,r.dx,n)#{block}") else puts "Argument Error: Invalid integration method"; return nil end end puts "Argument Error: bad function for integration"; return nil end |
#ok? ⇒ Boolean
Tells whether the function has been correctly converted from the string format
-
returns:
trueif the function has been successfully converted;falseelse
28 29 30 |
# File 'lib/linmeric/Function_class.rb', line 28 def ok? @funct == nil ? (return false) : (return true) end |
#to_block(n = 0) ⇒ Object
Returns the function as a ruby block ({ |…| … })
-
argument: number of variables needed for the block; default value is set if n == 0
-
returns: string that represents the block;
nilif an error is found
35 36 37 38 39 40 41 |
# File 'lib/linmeric/Function_class.rb', line 35 def to_block(n = 0) return nil if not self.ok? return nil if n < @vars.size n = @vars.size if n == 0 b_v = "|#{vars(n)}|" if n > 0 return "{ #{b_v} #{@funct} }" end |
#to_s ⇒ Object
-
returns: string representation of the function
73 74 75 |
# File 'lib/linmeric/Function_class.rb', line 73 def to_s return @inp end |
#vars(n = 0) ⇒ Object
Builds the list of the variables needed for the block
-
argument: number of variables needed for the block
- returns
-
nilif an error occourred while converting the function -
nilif ‘n` is less than the number of variables of the function -
string representation of the list
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/linmeric/Function_class.rb', line 50 def vars(n = 0) n = @vars.size if n == 0 return nil if not self.ok? return nil if n < @vars.size var = (@vars.size > 0) ? @vars[0] : "" for i in 1...@vars.length do var += ',' + @vars[i] end if n != @vars.size then i = 0 ('a'..'z').each do |ch| unless @vars.include? ch i +=1 var += ',' unless var == "" var += ch break if i == (n - @vars.size) end end end return var end |