class EpiMath::Rational
Attributes
verbose[RW]
Public Class Methods
new(poly=Polynomial.new, div=Polynomial.new([1]), verb=2)
click to toggle source
# 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
Public Instance Methods
calc(x)
click to toggle source
# 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
derive()
click to toggle source
# File lib/epimath100/rational.class.rb, line 21 def derive return nil end
div()
click to toggle source
# File lib/epimath100/rational.class.rb, line 57 def div @div end
div=(p)
click to toggle source
# 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
poly()
click to toggle source
# File lib/epimath100/rational.class.rb, line 54 def poly @poly end
poly=(p)
click to toggle source
accessors
# 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_s()
click to toggle source
# 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