Class: KXI::Math::Polynomial

Inherits:
Object
  • Object
show all
Defined in:
lib/kxi/math/polynomial.rb

Overview

Represents a polynomial

Instance Method Summary collapse

Constructor Details

#initialize(*cfs) ⇒ Polynomial

Instantiates the KXI::Math::Polynomial class

Parameters:

  • cfs (Numeric)

    Coefficients of polynomial



21
22
23
24
25
26
27
# 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

Instance Method Details

#*(other) ⇒ KXI::Math::Polynomial #*(other) ⇒ KXI::Math::Polynomial

Multiplies polynomial

Overloads:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Numeric

Returns the value of polynomial

Parameters:

  • x (Numeric)

    Value of x

Returns:

  • (Numeric)

    Value of polynomial at x



97
98
99
100
101
102
103
# 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

#coefficientsArray<Numeric>

Returns the coefficients of polynomial

Returns:

  • (Array<Numeric>)

    Coefficients of polynomial



15
16
17
# File 'lib/kxi/math/polynomial.rb', line 15

def coefficients
	@cfs
end

#degreeInteger

Returns the degree of polynomial

Returns:

  • (Integer)

    Degree of polynomial



9
10
11
# File 'lib/kxi/math/polynomial.rb', line 9

def degree
	@cfs.length - 1
end

#derivative(n = 1) ⇒ KXI::Math::Polynomial

Takes the derivative of polynomial

Parameters:

  • n (Integer) (defaults to: 1)

    Order of derivation

Returns:



61
62
63
64
65
66
67
68
69
70
# 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| ... } ⇒ Object

Iterates over every coefficient of polynomial (from higher powers to lower powers)

Yields:

  • (k)

    Iterator

Yield Parameters:

  • k (Numeric)

    Coefficient of polynomial



108
109
110
111
112
113
114
# File 'lib/kxi/math/polynomial.rb', line 108

def foreach
	d = degree
	@cfs.each do |k|
		yield(k, d)
		d -= 1
	end
end

#integralKXI::Math::Polynomial

Takes the integral of polynomial

Returns:



74
75
76
77
78
79
80
81
# 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) ⇒ Numeric

Integrates the polynomial

Parameters:

  • x1 (Numeric)

    First bound of integration

  • x2 (Numeric)

    Second bound of integration

Returns:

  • (Numeric)

    Result of integration



87
88
89
90
91
92
# 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_sString

Converts polynomial to string

Returns:

  • (String)

    String equivalent to polynomial



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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