Class: LinearAlgebra::Function

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

Direct Known Subclasses

ArthmeticSequence

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


5
6
7
# File 'lib/linear_algebra/function.rb', line 5

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

Instance Attribute Details

#powerObject (readonly)

Returns the value of attribute power


3
4
5
# File 'lib/linear_algebra/function.rb', line 3

def power
  @power
end

#slopeObject (readonly)

Returns the value of attribute slope


3
4
5
# File 'lib/linear_algebra/function.rb', line 3

def slope
  @slope
end

#y_interceptObject (readonly)

Returns the value of attribute y_intercept


3
4
5
# File 'lib/linear_algebra/function.rb', line 3

def y_intercept
  @y_intercept
end

Class Method Details

.find(index1) ⇒ Object


4
# File 'lib/linear_algebra/function.rb', line 4

def self.find(index1); end

Instance Method Details

#direct_variation?Boolean Also known as: dv?

Returns:

  • (Boolean)

24
25
26
# File 'lib/linear_algebra/function.rb', line 24

def direct_variation?
	y_intercept.zero? && power == 1
end

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

Parameters:

  • x (Integer, Float)

Returns:

  • (Integer, Float)

Raises:

  • (ArgumentError)

10
11
12
13
# File 'lib/linear_algebra/function.rb', line 10

def execute x
	raise ArgumentError unless x.kind_of? Numeric
	return slope * x ** power + y_intercept
end

#to_direct_variationObject Also known as: to_dv


28
29
30
31
32
33
34
35
# File 'lib/linear_algebra/function.rb', line 28

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

#to_sObject


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

def to_s
	"f(x) = #{idx slope}x#{power_string unless power == 1}#{" + #{@y_intercept}" unless direct_variation?}"
end

#to_slope_interceptObject


36
37
38
39
# File 'lib/linear_algebra/function.rb', line 36

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

#x_interceptObject Also known as: zero, solution, root


15
16
17
# File 'lib/linear_algebra/function.rb', line 15

def x_intercept
	f(0)
end