Class: Kamelopard::Functions::Cubic

Inherits:
Function1D show all
Defined in:
lib/kamelopard/function.rb

Overview

Represents a cubic equation of the form c3 * x^3 + c2 * x^2 + c1 * x + c0

Direct Known Subclasses

Constant, Line, Quadratic

Instance Attribute Summary collapse

Attributes inherited from Function

#append, #compose, #end, #max, #min, #start, #verbose

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Function1D

#compose=

Methods inherited from Function

#get_value

Constructor Details

#initialize(c3 = 1.0, c2 = 0.0, c1 = 0.0, c0 = 0.0, min = -1.0,, max = 1.0) ⇒ Cubic

Returns a new instance of Cubic.



106
107
108
109
110
111
112
# File 'lib/kamelopard/function.rb', line 106

def initialize(c3 = 1.0, c2 = 0.0, c1 = 0.0, c0 = 0.0, min = -1.0, max = 1.0)
    @c3 = c3.to_f
    @c2 = c2.to_f
    @c1 = c1.to_f
    @c0 = c0.to_f
    super min, max
end

Instance Attribute Details

#c0Object

Returns the value of attribute c0.



105
106
107
# File 'lib/kamelopard/function.rb', line 105

def c0
  @c0
end

#c1Object

Returns the value of attribute c1.



105
106
107
# File 'lib/kamelopard/function.rb', line 105

def c1
  @c1
end

#c2Object

Returns the value of attribute c2.



105
106
107
# File 'lib/kamelopard/function.rb', line 105

def c2
  @c2
end

#c3Object

Returns the value of attribute c3.



105
106
107
# File 'lib/kamelopard/function.rb', line 105

def c3
  @c3
end

Class Method Details

.interpolate(ymin, ymax, x1, y1, x2, y2, min = -1.0,, max = 1.0) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/kamelopard/function.rb', line 119

def self.interpolate(ymin, ymax, x1, y1, x2, y2, min = -1.0, max = 1.0)
    xm = Matrix[[min ** 3, x1 ** 3, x2 ** 3, max ** 3], [min ** 2, x1 ** 2, x2 ** 2, max ** 2], [min, x1, x2, max], [1, 1, 1, 1]]
    ym = Matrix[[ymin, y1, y2, ymax]]
    m = ym * xm.inverse
    c3 = m[0,0]
    c2 = m[0,1]
    c1 = m[0,2]
    c0 = m[0,3]
    return Cubic.new(c3, c2, c1, c0, min, max)
end

Instance Method Details

#run_function(x) ⇒ Object



114
115
116
117
# File 'lib/kamelopard/function.rb', line 114

def run_function(x)
    puts "#{self.class.name}: [#{@min}, #{@max}] (#{@c3}, #{@c2}, #{@c1}, #{@c0}): #{x} -> #{ @c3 * x * x * x + @c2 * x * x + @c1 * x + @c0 }" if @verbose
    return @c3 * x ** 3 + @c2 * x ** 2 + @c1 * x + @c0
end