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

Returns a new instance of Function.



12
13
14
# File 'lib/linear1/function.rb', line 12

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.



6
7
8
# File 'lib/linear1/function.rb', line 6

def power
  @power
end

#slopeObject (readonly)

Returns the value of attribute slope.



6
7
8
# File 'lib/linear1/function.rb', line 6

def slope
  @slope
end

#y_interceptObject (readonly)

Returns the value of attribute y_intercept.



6
7
8
# File 'lib/linear1/function.rb', line 6

def y_intercept
  @y_intercept
end

Class Method Details

.find(i1) ⇒ Function

Parameters:

  • the index to start search

Returns:



9
10
11
# File 'lib/linear1/function.rb', line 9

def self.find i1
  Function.new ARGV[i1], ARGV[i1 + 1], ARGV[i1 + 2]
end

Instance Method Details

#direct_variation?Boolean Also known as: dv?

Returns:



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

def direct_variation? # @return [Boolean]

  y_intercept.zero? and power == 1
end

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

Parameters:

Returns:



17
18
19
# File 'lib/linear1/function.rb', line 17

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

#to_direct_variationDirectVariation Also known as: to_dv

Returns:

Raises:



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

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

#to_sString

Returns the equation.

Returns:

  • the equation



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

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_interceptObject

Raises:



43
44
45
46
# File 'lib/linear1/function.rb', line 43

def to_slope_intercept
  raise TypeError, "power must be 1" unless power == 1
  SlopeIntercept.new slope, y_intercept
end

#x_interceptObject Also known as: zero, solution, root



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

def x_intercept
  f(0)
end