Class: LinearAlgebra::Function
- Inherits:
-
Object
- Object
- LinearAlgebra::Function
- Defined in:
- lib/linear_algebra/function.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#power ⇒ Object
readonly
Returns the value of attribute power.
-
#slope ⇒ Object
readonly
Returns the value of attribute slope.
-
#y_intercept ⇒ Object
readonly
Returns the value of attribute y_intercept.
Class Method Summary collapse
Instance Method Summary collapse
- #direct_variation? ⇒ Boolean (also: #dv?)
- #execute(x) ⇒ Integer, Float (also: #f)
-
#initialize(slope = 1, y_intercept = 0, power = 1) ⇒ Function
constructor
A new instance of Function.
- #to_direct_variation ⇒ Object (also: #to_dv)
- #to_s ⇒ Object
- #to_slope_intercept ⇒ Object
- #x_intercept ⇒ Object (also: #zero, #solution, #root)
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
#power ⇒ Object (readonly)
Returns the value of attribute power
3 4 5 |
# File 'lib/linear_algebra/function.rb', line 3 def power @power end |
#slope ⇒ Object (readonly)
Returns the value of attribute slope
3 4 5 |
# File 'lib/linear_algebra/function.rb', line 3 def slope @slope end |
#y_intercept ⇒ Object (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?
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
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_variation ⇒ Object 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_s ⇒ Object
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_intercept ⇒ Object
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_intercept ⇒ Object Also known as: zero, solution, root
15 16 17 |
# File 'lib/linear_algebra/function.rb', line 15 def x_intercept f(0) end |