class KXI::Math::Polynomial

Represents a polynomial

Public Class Methods

new(*cfs) click to toggle source

Instantiates the {KXI::Math::Polynomial} class @param [Numeric] cfs Coefficients of polynomial

# File lib/kxi/math/polynomial.rb, line 21
def initialize(*cfs)
        cfs.shift while cfs.length > 1 and cfs[0] == 0
        @cfs = cfs.collect do |i|
                raise(KXI::Exceptions::InvalidTypeException.new(i.class, Numeric)) unless i.is_a?(Numeric)
                i.to_f
        end
end

Public Instance Methods

*(other) click to toggle source

Multiplies polynomial @overload *(other)

Multiplies polynomial with coefficient
@param [Numeric] other Coefficient to multiply with
@return [KXI::Math::Polynomial] Result of multiplication

@overload *(other)

Multiplies polynomial with other polynomial
@param [KXI::Math::Polynomial] other Polynomial to multiply with
@return [KXI::Math::Polynomial] Result of multiplication
# File lib/kxi/math/polynomial.rb, line 125
def *(other)
        if other.is_a?(Polynomial)
                cfs = [0] * (degree + other.degree + 1)
                foreach do |k1, d1|
                        other.foreach do |k2, d2|
                                cfs[d1 + d2] += k1 * k2
                        end
                end
                return Polynomial.new(*cfs)
        elsif other.is_a?(Numeric)
                return @cfs.collect { |k| k * other }
        else
                raise(KXI::Exceptions::InvalidTypeException.new(other.class, Numeric, KXI::Math::Polynomial))
        end
end
at(x) click to toggle source

Returns the value of polynomial @param [Numeric] x Value of x @return [Numeric] Value of polynomial at x

# File lib/kxi/math/polynomial.rb, line 97
def at(x)
        sum = 0
        foreach do |k, d|
                sum += k * (x ** d)
        end
        return sum
end
coefficients() click to toggle source

Returns the coefficients of polynomial @return [Array<Numeric>] Coefficients of polynomial

# File lib/kxi/math/polynomial.rb, line 15
def coefficients
        @cfs
end
degree() click to toggle source

Returns the degree of polynomial @return [Integer] Degree of polynomial

# File lib/kxi/math/polynomial.rb, line 9
def degree
        @cfs.length - 1
end
derivative(n = 1) click to toggle source

Takes the derivative of polynomial @param [Integer] n Order of derivation @return [KXI::Math::Polynomial] Polynomial that represents the n-th derivative

# File lib/kxi/math/polynomial.rb, line 61
def derivative(n = 1)
        return Polynomial.new(0) if degree <= n
        cfs = []
        foreach do |k, d|
                if d >= n
                        cfs.push(k * KXI::Math.pfact(d, d - n))
                end
        end
        return Polynomial.new(*cfs)
end
foreach() { |k, d| ... } click to toggle source

Iterates over every coefficient of polynomial (from higher powers to lower powers) @yield [k] Iterator @yieldparam [Numeric] k Coefficient of polynomial

# File lib/kxi/math/polynomial.rb, line 108
def foreach
        d = degree
        @cfs.each do |k|
                yield(k, d)
                d -= 1
        end
end
integral() click to toggle source

Takes the integral of polynomial @return [KXI::Math::Polynomial] Polynomial that represents the integral

# File lib/kxi/math/polynomial.rb, line 74
def integral
        cfs = []
        foreach do |k, d|
                cfs.push(k / (d + 1))
        end
        cfs.push(0)
        return Polynomial.new(*cfs)
end
integrate(x1, x2) click to toggle source

Integrates the polynomial @param [Numeric] x1 First bound of integration @param [Numeric] x2 Second bound of integration @return [Numeric] Result of integration

# File lib/kxi/math/polynomial.rb, line 87
def integrate(x1, x2)
        min = x1 > x2 ? x2 : x1
        max = x1 > x2 ? x1 : x2
        i   = integral
        return i.at(max) - i.at(min)
end
to_s() click to toggle source

Converts polynomial to string @return [String] String equivalent to polynomial

# File lib/kxi/math/polynomial.rb, line 31
def to_s
        return (@cfs[0] == @cfs[0].to_i ? @cfs[0].to_i : @cfs[0]) if degree == 0
        ret = nil
        foreach do |k, d|
                if k != 0
                        k = k.to_i if k.to_i == k
                        if d > 0
                                if ret == nil
                                        ret = k >= 0 ? '' : '-'
                                else
                                        ret += k >= 0 ? ' + ' : ' - '
                                end
                                ret += (k >= 0 ? k : -k).to_s if k != 1 and k != -1
                                ret += "x#{d > 1 ? "^#{d.to_s}" : ''}"
                        else
                                if ret == nil
                                        ret = k >= 0 ? '' : '-'
                                else
                                        ret += k >= 0 ? ' + ' : ' - '
                                end
                                ret += (k >= 0 ? k : -k).to_s
                        end
                end
        end
        return ret
end