Class: EpiMath::Rational

Inherits:
Function show all
Defined in:
lib/epimath100/rational.class.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Function

#convert_hash, #get_degree, #get_degree_max

Constructor Details

#initialize(poly = Polynomial.new, div = Polynomial.new([1]), verb = 2) ⇒ Rational

Returns a new instance of Rational.



11
12
13
14
15
16
17
18
19
# File 'lib/epimath100/rational.class.rb', line 11

def initialize poly=Polynomial.new, div=Polynomial.new([1]), verb=2
  #Error.call "Rational::new : Your poly are invalid" if !poly.is_a?Polynomial
  Error.call "Rational::new : Your divider are invalid" if !div.is_a?Polynomial
  @poly = poly #todo : check each element of the array (hash to use select ?)
  @poly.verb = 0
  @div = div
  @div.verb = 0
  @verb = verb
end

Instance Attribute Details

#verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/epimath100/rational.class.rb', line 9

def verbose
  @verbose
end

Instance Method Details

#calc(x) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/epimath100/rational.class.rb', line 36

def calc x
  Error.call "Rational::calc: x is not a Numeric value" if !x.is_a?Numeric

  p = @poly.calc x
  q = @div.calc x
  return 0 if q == 0
  return p / q
end

#deriveObject



21
22
23
# File 'lib/epimath100/rational.class.rb', line 21

def derive
  return nil
end

#divObject



57
58
59
# File 'lib/epimath100/rational.class.rb', line 57

def div
  @div
end

#div=(p) ⇒ Object



50
51
52
53
# File 'lib/epimath100/rational.class.rb', line 50

def div= p
  Error.call "Rational::new : Your divider are invalid" if !p.is_a?Polynomial
  @div = p
end

#polyObject



54
55
56
# File 'lib/epimath100/rational.class.rb', line 54

def poly
  @poly
end

#poly=(p) ⇒ Object

accessors



46
47
48
49
# File 'lib/epimath100/rational.class.rb', line 46

def poly= p
  Error.call "Rational::new : Your poly are invalid" if !p.is_a?Polynomial
  @poly = p
end

#to_sObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/epimath100/rational.class.rb', line 25

def to_s
  max_space = [@poly.to_s.size, @div.to_s.size].max
  top_space = (max_space - @poly.to_s.size) / 2
  bot_space = (max_space - @div.to_s.size) / 2

  string =  "       #{" " * top_space}#{@poly.to_s}\n"
  string += "f(x) = #{"-" * max_space}"
  string << "\n       #{" " * bot_space}#{@div.to_s}"
  return string
end