Class: Abst::Polynomial

Inherits:
Object
  • Object
show all
Includes:
Ring
Defined in:
lib/include/polynomial.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ring

#%, #**, #/, included

Constructor Details

#initialize(coef) ⇒ Polynomial

Returns a new instance of Polynomial.



14
15
16
17
# File 'lib/include/polynomial.rb', line 14

def initialize(coef)
	@coef = coef.to_a
	cutdown
end

Class Attribute Details

.coef_classObject (readonly)

Returns the value of attribute coef_class.



8
9
10
# File 'lib/include/polynomial.rb', line 8

def coef_class
  @coef_class
end

.oneObject (readonly)

Returns the value of attribute one.



8
9
10
# File 'lib/include/polynomial.rb', line 8

def one
  @one
end

.zeroObject (readonly)

Returns the value of attribute zero.



8
9
10
# File 'lib/include/polynomial.rb', line 8

def zero
  @zero
end

Instance Method Details

#*(other) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/include/polynomial.rb', line 28

def *(other)
	other = other.kind_of?(self.class.coef_class) ? [other] : other.coef
	rslt_coef = Array.new(@coef.size + other.size - 1, self.class.coef_class.zero)

	@coef.size.times do |j|
		other.size.times do |i|
			rslt_coef[i + j] += @coef[j] * other[i]
		end
	end

	return self.class.new(rslt_coef)
end

#==(other) ⇒ Object



62
63
64
65
# File 'lib/include/polynomial.rb', line 62

def ==(other)
	other = other.kind_of?(self.class.coef_class) ? [other] : other.coef
	return @coef == other
end

#add_sub(op, other) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/include/polynomial.rb', line 19

def add_sub(op, other)
	a = @coef
	b = other.kind_of?(self.class.coef_class) ? [other] : other.coef
	a, b = b, a if a.size < b.size
	rslt_coef = a.dup
	b.size.times {|i| rslt_coef[i] = rslt_coef[i].__send__(op, b[i])}
	return self.class.new(rslt_coef)
end

#cutdownObject

delete last zero-entries



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/include/polynomial.rb', line 95

def cutdown
	zero = self.class.coef_class.zero

	return if zero != @coef.last

	# find last non-zero entry
	idx = 0
	(@coef.size - 2).downto(1) do |i|
		if @coef[i] != zero
			idx = i
			break
		end
	end

	@coef[idx + 1, @coef.size] = []
end

#degreeObject



67
68
69
70
# File 'lib/include/polynomial.rb', line 67

def degree
	return -INFINITY if 1 == @coef.size and @coef[0] == self.class.coef_class.zero
	return @coef.size - 1
end

#divmod(other) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/include/polynomial.rb', line 41

def divmod(other)
	other = other.kind_of?(self.class.coef_class) ? [other] : other.coef
	q = [self.class.coef_class.zero]
	r = @coef.dup

	lc_other = other.last
	(@coef.size - other.size).downto(0) do |i|
		tq, tr = r.pop.divmod(lc_other)
		raise NotImplementedError unless self.class.coef_class.zero == tr
		q[i] = tq
		next if self.class.coef_class.zero == tq

		(other.size - 1).times do |j|
			r[i + j] -= tq * other[j]
		end
	end

	r[0] = self.class.coef_class.zero if r.empty?
	return self.class.new(q), self.class.new(r)
end

#eval(x) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/include/polynomial.rb', line 77

def eval(x)
	rslt = @coef.last
	(@coef.size - 2).downto(0) do |i|
		rslt = rslt * x + @coef[i]
	end

	return rslt
end

#lcObject

leading coefficient



73
74
75
# File 'lib/include/polynomial.rb', line 73

def lc
	return @coef.last
end

#normalizeObject



90
91
92
# File 'lib/include/polynomial.rb', line 90

def normalize
	return self.dup.normalize!
end

#normalize!Object

Raises:

  • (NotImplementedError)


86
87
88
# File 'lib/include/polynomial.rb', line 86

def normalize!
	raise NotImplementedError
end

#to_aObject



112
113
114
# File 'lib/include/polynomial.rb', line 112

def to_a
	return @coef.dup
end