Class: EpiMath::Polynomial
- Defined in:
- lib/epimath100/polynomial.class.rb
Instance Attribute Summary collapse
-
#coef ⇒ Object
Returns the value of attribute coef.
-
#verb ⇒ Object
Returns the value of attribute verb.
Instance Method Summary collapse
-
#calc(x) ⇒ Object
Calculate the value of f(x) from x.
-
#derive ⇒ Object
calculate the derivated function of the current polynomial == Returns: Polynomial (the derivated function).
-
#initialize(coef = [], verb = 0) ⇒ Polynomial
constructor
Initialize the polynominal function Its coeficients are 1, 2, 3, 4 …
- #to_s ⇒ Object
Methods inherited from Function
#convert_hash, #get_degree, #get_degree_max
Constructor Details
#initialize(coef = [], verb = 0) ⇒ Polynomial
Initialize the polynominal function Its coeficients are 1, 2, 3, 4 … with ‘1’x⁰ + ‘2’x¹ + ‘3’x² … = y Each coeficients has an associated value (exemple : 2 => 1 correspond to 1x²) exemple : a = [] ; a = 1 ; #correspond to 0 + 0x + 2x³
Parameters:
- coef
-
coef is an array which have several keys 0, 1, 2,… which correspond to the coeficients
- verb
-
default is false
It will display more information if turned on when to_s.
It's a integer, it must be in the list :
- 0 : returns ""
- 1 : "y = equation"
- 2 : "f(x) = equation" (default value)
26 27 28 29 30 31 |
# File 'lib/epimath100/polynomial.class.rb', line 26 def initialize coef=[], verb=0 Error.call "Polynomial::new : Your coef is invalid" if !coef.is_a?Hash and !coef.is_a?Array coef = convert_hash(coef) if coef.is_a?Hash @coef = coef.select{|v| v.is_a?Numeric} @verbose = 2 end |
Instance Attribute Details
#coef ⇒ Object
Returns the value of attribute coef.
9 10 11 |
# File 'lib/epimath100/polynomial.class.rb', line 9 def coef @coef end |
#verb ⇒ Object
Returns the value of attribute verb.
9 10 11 |
# File 'lib/epimath100/polynomial.class.rb', line 9 def verb @verb end |
Instance Method Details
#calc(x) ⇒ Object
Calculate the value of f(x) from x
67 68 69 70 71 72 73 |
# File 'lib/epimath100/polynomial.class.rb', line 67 def calc x y = 0 [@coef.size].max.times do |coef| y += @coef[coef] * x**coef end return y end |
#derive ⇒ Object
calculate the derivated function of the current polynomial
Returns:
Polynomial (the derivated function)
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/epimath100/polynomial.class.rb', line 36 def derive dérivé = Polynomial.new if @coef.size > 0 (1..(@coef.size)).times do |coef| dérivé.coef[coef - 1] = dérivé[coef] * coef end end return dérivé end |
#to_s ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/epimath100/polynomial.class.rb', line 48 def to_s str = "" str = "#{@coef[0].to_i}" + str #if @coef[:a] if @coef.size > 0 (1..(@coef.size)).each do |coef| #sign = "+" #sign = "-" if value < 0 str = "#{@coef[coef]}x^#{coef} + " + str if @coef[coef].to_f != 0 end end str = "f(x) = " + str if @verb == 2 str = "y = " + str if @verb == 1 return str end |