Class: Linear1::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/linear1/function.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slope = 1, y_intercept = 0, power = 1) ⇒ Function

Parameters:

  • i1 (Fixnum)

    the index to start search



22
23
24
# File 'lib/linear1/function.rb', line 22

def initialize(slope=1, y_intercept=0, power=1)
  @slope, @y_intercept, @power = display_num(slope), display_num(y_intercept), display_num(power)
end

Instance Attribute Details

#powerObject (readonly)

Returns the value of attribute power



19
20
21
# File 'lib/linear1/function.rb', line 19

def power
  @power
end

#slopeObject (readonly)

Returns the value of attribute slope



19
20
21
# File 'lib/linear1/function.rb', line 19

def slope
  @slope
end

#y_interceptObject (readonly)

Returns the value of attribute y_intercept



19
20
21
# File 'lib/linear1/function.rb', line 19

def y_intercept
  @y_intercept
end

Class Method Details

.display_num(num) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/linear1/function.rb', line 8

def display_num num
  return num unless num.is_a? String
  case num
    when  Complex(num).to_s   then Complex(num)
    when  Rational(num).to_s  then  Rational(num)
    when  num.to_i.to_s then  num.to_i
    when  num.to_f.to_s  then   num.to_f
    when  BigDecimal.new(num).to_s then  BigDecimal.new num
  end
end

Instance Method Details

#direct_variation?Boolean Also known as: dv?

Returns:

  • (Boolean)


41
42
43
# File 'lib/linear1/function.rb', line 41

def direct_variation? # @return [Boolean]
  y_intercept.zero? and power == 1
end

#execute(x) ⇒ Integer, Float Also known as: f

Parameters:

  • x (Integer, Float)

Returns:

  • (Integer, Float)


27
28
29
# File 'lib/linear1/function.rb', line 27

def execute x
  slope * display_num(x) ** power + y_intercept
end

#to_direct_variationDirectVariation Also known as: to_dv

Returns:

Raises:

  • (TypeError)


47
48
49
50
51
# File 'lib/linear1/function.rb', line 47

def to_direct_variation
  if direct_variation? then DirectVariation.new slope
  else raise TypeError, "Unable to convert to DirectVariation"
  end
end

#to_functionObject Also known as: to_func



61
62
63
# File 'lib/linear1/function.rb', line 61

def to_function
  self
end

#to_sString

Returns the equation

Returns:

  • (String)

    the equation



38
39
40
# File 'lib/linear1/function.rb', line 38

def to_s # @return [String] the equation
  "f(x) = #{idx display_num slope}x#{power_string unless power == 1}#{" + #{display_num @y_intercept}" unless direct_variation?}"
end

#to_slope_interceptSlopeIntercept Also known as: to_si

Implicit converter to SlopeIntercept

Returns:

Raises:

  • (TypeError)


56
57
58
59
# File 'lib/linear1/function.rb', line 56

def to_slope_intercept
  raise TypeError, "Could not convert #{self.inspect} to SlopeIntercept" unless power == 1
  SlopeIntercept.new slope, y_intercept
end

#x_interceptObject Also known as: zero, solution, root



31
32
33
# File 'lib/linear1/function.rb', line 31

def x_intercept
  f(0)
end