algebra 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +53 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +21 -0
- data/README-ja.txt +60 -0
- data/README.md +61 -0
- data/Rakefile +43 -0
- data/algebra.gemspec +33 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/doc-ja/README-ja.html +69 -0
- data/doc-ja/README-ja.rd +60 -0
- data/doc-ja/README.html +0 -0
- data/doc-ja/README.rd +0 -0
- data/doc-ja/algebra-ja.html +66 -0
- data/doc-ja/algebra-ja.rd +60 -0
- data/doc-ja/algebraic-equation-ja.html +36 -0
- data/doc-ja/algebraic-equation-ja.rd +26 -0
- data/doc-ja/algebraic-extension-field-ja.html +122 -0
- data/doc-ja/algebraic-extension-field-ja.rd +118 -0
- data/doc-ja/algebraic-parser-ja.html +87 -0
- data/doc-ja/algebraic-parser-ja.rd +95 -0
- data/doc-ja/changes.html +161 -0
- data/doc-ja/changes.rd +119 -0
- data/doc-ja/elementary-divisor-ja.html +56 -0
- data/doc-ja/elementary-divisor-ja.rd +45 -0
- data/doc-ja/euclidian-ring-ja.html +78 -0
- data/doc-ja/euclidian-ring-ja.rd +69 -0
- data/doc-ja/finite-group-ja.html +285 -0
- data/doc-ja/finite-group-ja.rd +276 -0
- data/doc-ja/finite-map-ja.html +224 -0
- data/doc-ja/finite-map-ja.rd +227 -0
- data/doc-ja/finite-set-ja.html +393 -0
- data/doc-ja/finite-set-ja.rd +408 -0
- data/doc-ja/index-ja.html +113 -0
- data/doc-ja/index-ja.rd +112 -0
- data/doc-ja/jordan-form-ja.html +106 -0
- data/doc-ja/jordan-form-ja.rd +99 -0
- data/doc-ja/localized-ring-ja.html +121 -0
- data/doc-ja/localized-ring-ja.rd +131 -0
- data/doc-ja/m-polynomial-ja.html +542 -0
- data/doc-ja/m-polynomial-ja.rd +596 -0
- data/doc-ja/matrix-algebra-ja.html +686 -0
- data/doc-ja/matrix-algebra-ja.rd +727 -0
- data/doc-ja/matrix-algebra-triplet-ja.html +124 -0
- data/doc-ja/matrix-algebra-triplet-ja.rd +129 -0
- data/doc-ja/permutation-group-ja.html +159 -0
- data/doc-ja/permutation-group-ja.rd +151 -0
- data/doc-ja/polynomial-converter-ja.html +64 -0
- data/doc-ja/polynomial-converter-ja.rd +54 -0
- data/doc-ja/polynomial-ja.html +446 -0
- data/doc-ja/polynomial-ja.rd +473 -0
- data/doc-ja/residue-class-ring-ja.html +112 -0
- data/doc-ja/residue-class-ring-ja.rd +113 -0
- data/doc-ja/sample-algebraic-equation01.rb.v.rd +11 -0
- data/doc-ja/sample-algebraic-equation02.rb.v.rd +11 -0
- data/doc-ja/sample-algebraic-root01.rb.v.rd +20 -0
- data/doc-ja/sample-algebraicfield01.rb.v.rd +26 -0
- data/doc-ja/sample-algebraicfield02.rb.v.rd +15 -0
- data/doc-ja/sample-cayleyhamilton01.rb.v.rd +14 -0
- data/doc-ja/sample-diagonalization01.rb.v.rd +48 -0
- data/doc-ja/sample-divmod01.rb.v.rd +17 -0
- data/doc-ja/sample-elementary-divisor01.rb.v.rd +48 -0
- data/doc-ja/sample-factorize01.rb.v.rd +11 -0
- data/doc-ja/sample-factorize02.rb.v.rd +13 -0
- data/doc-ja/sample-factorize03.rb.v.rd +14 -0
- data/doc-ja/sample-factorize04.rb.v.rd +14 -0
- data/doc-ja/sample-factorize05.rb.v.rd +53 -0
- data/doc-ja/sample-galois-group01.rb.v.rd +27 -0
- data/doc-ja/sample-gaussian-elimination01.rb.v.rd +19 -0
- data/doc-ja/sample-geometry01.rb.v.rd +33 -0
- data/doc-ja/sample-geometry02.rb.v.rd +34 -0
- data/doc-ja/sample-geometry03.rb.v.rd +45 -0
- data/doc-ja/sample-geometry04.rb.v.rd +33 -0
- data/doc-ja/sample-geometry07.rb.v.rd +62 -0
- data/doc-ja/sample-groebner01.rb.v.rd +13 -0
- data/doc-ja/sample-groebner02.rb.v.rd +18 -0
- data/doc-ja/sample-groebner03.rb.v.rd +19 -0
- data/doc-ja/sample-group01.rb.v.rd +21 -0
- data/doc-ja/sample-jordan-form01.rb.v.rd +56 -0
- data/doc-ja/sample-jordanform01.rb.v.rd +55 -0
- data/doc-ja/sample-lagrange-multiplier01.rb.v.rd +35 -0
- data/doc-ja/sample-m-factorize01.rb.v.rd +15 -0
- data/doc-ja/sample-m-factorize02.rb.v.rd +12 -0
- data/doc-ja/sample-m-polynomial01.rb.v.rd +10 -0
- data/doc-ja/sample-map01.rb.v.rd +9 -0
- data/doc-ja/sample-polynomial01.rb.v.rd +9 -0
- data/doc-ja/sample-polynomial02.rb.v.rd +11 -0
- data/doc-ja/sample-primefield01.rb.v.rd +13 -0
- data/doc-ja/sample-quotientfield01.rb.v.rd +11 -0
- data/doc-ja/sample-quotientfield02.rb.v.rd +18 -0
- data/doc-ja/sample-quotientfield03.rb.v.rd +16 -0
- data/doc-ja/sample-quotientfield04.rb.v.rd +16 -0
- data/doc-ja/sample-set01.rb.v.rd +18 -0
- data/doc-ja/sample-splitting-field01.rb.v.rd +19 -0
- data/doc-ja/samples-ja.html +885 -0
- data/doc-ja/samples-ja.rd +221 -0
- data/doc-ja/style.css +85 -0
- data/doc-ja/todo.html +20 -0
- data/doc-ja/todo.rd +9 -0
- data/lib/algebra.rb +36 -0
- data/lib/algebra/algebraic-equation.rb +56 -0
- data/lib/algebra/algebraic-extension-field.rb +135 -0
- data/lib/algebra/algebraic-parser.rb +160 -0
- data/lib/algebra/algebraic-system.rb +224 -0
- data/lib/algebra/annihilate.rb +52 -0
- data/lib/algebra/array-supplement.rb +53 -0
- data/lib/algebra/auto-require.rb +84 -0
- data/lib/algebra/chinese-rem-th.rb +135 -0
- data/lib/algebra/combinatorial.rb +145 -0
- data/lib/algebra/elementary-divisor.rb +193 -0
- data/lib/algebra/euclidian-ring.rb +161 -0
- data/lib/algebra/factors.rb +305 -0
- data/lib/algebra/finite-group.rb +374 -0
- data/lib/algebra/finite-map.rb +201 -0
- data/lib/algebra/finite-set.rb +456 -0
- data/lib/algebra/galois-group.rb +129 -0
- data/lib/algebra/gaussian-elimination.rb +385 -0
- data/lib/algebra/groebner-basis-coeff.rb +228 -0
- data/lib/algebra/groebner-basis.rb +197 -0
- data/lib/algebra/import-module-single-thread.rb +86 -0
- data/lib/algebra/import-module.rb +491 -0
- data/lib/algebra/jordan-form.rb +114 -0
- data/lib/algebra/linear-algebra.rb +143 -0
- data/lib/algebra/localized-ring.rb +294 -0
- data/lib/algebra/m-index.rb +282 -0
- data/lib/algebra/m-polynomial-factor-int.rb +186 -0
- data/lib/algebra/m-polynomial-factor-zp.rb +114 -0
- data/lib/algebra/m-polynomial-factor.rb +315 -0
- data/lib/algebra/m-polynomial-gcd.rb +40 -0
- data/lib/algebra/m-polynomial.rb +875 -0
- data/lib/algebra/matrix-algebra-triplet.rb +292 -0
- data/lib/algebra/matrix-algebra.rb +929 -0
- data/lib/algebra/numeric-supplement.rb +123 -0
- data/lib/algebra/permutation-group.rb +257 -0
- data/lib/algebra/polynomial-converter.rb +193 -0
- data/lib/algebra/polynomial-factor-alg.rb +148 -0
- data/lib/algebra/polynomial-factor-int.rb +252 -0
- data/lib/algebra/polynomial-factor-zp.rb +165 -0
- data/lib/algebra/polynomial-factor.rb +140 -0
- data/lib/algebra/polynomial.rb +592 -0
- data/lib/algebra/powers.rb +31 -0
- data/lib/algebra/prime-gen.rb +32 -0
- data/lib/algebra/rational.rb +81 -0
- data/lib/algebra/residue-class-ring.rb +219 -0
- data/lib/algebra/sets-system.rb +43 -0
- data/lib/algebra/splitting-field.rb +103 -0
- data/lib/algebra/version.rb +3 -0
- data/sample/Makefile +10 -0
- data/sample/do-testscripts.rb +100 -0
- data/sample/sample-algebraic-equation01.rb +6 -0
- data/sample/sample-algebraic-equation02.rb +6 -0
- data/sample/sample-algebraic-root01.rb +15 -0
- data/sample/sample-algebraicfield01.rb +21 -0
- data/sample/sample-algebraicfield02.rb +10 -0
- data/sample/sample-cayleyhamilton01.rb +9 -0
- data/sample/sample-diagonalization01.rb +43 -0
- data/sample/sample-divmod01.rb +12 -0
- data/sample/sample-elementary-divisor01.rb +43 -0
- data/sample/sample-factorize01.rb +6 -0
- data/sample/sample-factorize02.rb +8 -0
- data/sample/sample-factorize03.rb +9 -0
- data/sample/sample-factorize04.rb +9 -0
- data/sample/sample-factorize05.rb +48 -0
- data/sample/sample-galois-group01.rb +22 -0
- data/sample/sample-gaussian-elimination01.rb +14 -0
- data/sample/sample-geometry01.rb +28 -0
- data/sample/sample-geometry02.rb +29 -0
- data/sample/sample-geometry03.rb +40 -0
- data/sample/sample-geometry04.rb +28 -0
- data/sample/sample-geometry07.rb +58 -0
- data/sample/sample-groebner01.rb +8 -0
- data/sample/sample-groebner02.rb +13 -0
- data/sample/sample-groebner03.rb +14 -0
- data/sample/sample-group01.rb +16 -0
- data/sample/sample-jordan-form01.rb +51 -0
- data/sample/sample-lagrange-multiplier01.rb +30 -0
- data/sample/sample-m-factorize01.rb +10 -0
- data/sample/sample-m-factorize02.rb +7 -0
- data/sample/sample-m-polynomial01.rb +5 -0
- data/sample/sample-map01.rb +4 -0
- data/sample/sample-polynomial01.rb +4 -0
- data/sample/sample-polynomial02.rb +6 -0
- data/sample/sample-primefield01.rb +8 -0
- data/sample/sample-quotientfield01.rb +6 -0
- data/sample/sample-quotientfield02.rb +13 -0
- data/sample/sample-quotientfield03.rb +11 -0
- data/sample/sample-quotientfield04.rb +11 -0
- data/sample/sample-set01.rb +13 -0
- data/sample/sample-splitting-field01.rb +14 -0
- data/sample/test-00-cayley-hamilton.rb +76 -0
- data/sample/test-00-groebner-basis.rb +274 -0
- data/sample/test-00-polynomial-factor-alg.rb +89 -0
- data/sample/test-00.list +4 -0
- data/sample/time-trial.rb +65 -0
- metadata +373 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
module Algebra
|
2
|
+
module Powers
|
3
|
+
def **(n)
|
4
|
+
return unity if n == 0
|
5
|
+
|
6
|
+
if n > 0
|
7
|
+
m = self
|
8
|
+
else
|
9
|
+
n = -n
|
10
|
+
m = inverse
|
11
|
+
end
|
12
|
+
|
13
|
+
z = x = m
|
14
|
+
n -= 1
|
15
|
+
while n & 1 != 0 && (z *= x); (n >>= 1) != 0
|
16
|
+
x *= x
|
17
|
+
end
|
18
|
+
z
|
19
|
+
|
20
|
+
# if n == 1
|
21
|
+
# m
|
22
|
+
# else
|
23
|
+
# q , r = n.divmod 2
|
24
|
+
# x = m ** q
|
25
|
+
# x = x * x
|
26
|
+
# x = x * m if r > 0
|
27
|
+
# x
|
28
|
+
# end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Prime Generator
|
2
|
+
#
|
3
|
+
# by Shin-ichiro Hara
|
4
|
+
#
|
5
|
+
# Version 1.0 (2001.09.07)
|
6
|
+
|
7
|
+
class Primes
|
8
|
+
List = [2, 3]
|
9
|
+
def [](n); List[n]; end
|
10
|
+
def succ
|
11
|
+
n = List[-1] + 2
|
12
|
+
n += 2 while List.find{|k| n % k == 0}
|
13
|
+
List << n
|
14
|
+
n
|
15
|
+
end
|
16
|
+
|
17
|
+
def each
|
18
|
+
List.each do |x|
|
19
|
+
yield x
|
20
|
+
end
|
21
|
+
loop do
|
22
|
+
yield succ
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.include?(x)
|
27
|
+
new.each do |prm|
|
28
|
+
return false if prm > x
|
29
|
+
return true if prm == x
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# if ENV["RUBY_RAT"]
|
2
|
+
# $LOAD_PATH.unshift ENV["RUBY_RAT"]
|
3
|
+
# require "rational.rb"
|
4
|
+
# elsif ENV["RUBY_USE_FRACTION"]
|
5
|
+
# require "frac"
|
6
|
+
# class Fraction < Numeric
|
7
|
+
# instance_eval do
|
8
|
+
# alias new! new
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# def initialize(n, d = 1)
|
12
|
+
# @numerator, @denominator = n, d
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# Rational = Fraction
|
17
|
+
# alias Rational frac
|
18
|
+
#
|
19
|
+
# class Rational < Numeric
|
20
|
+
# def self.unity; new(1,1); end
|
21
|
+
# def self.zero; new(0,1); end
|
22
|
+
# def inverse; 1 / self; end
|
23
|
+
# end
|
24
|
+
# else
|
25
|
+
# require "rational"
|
26
|
+
# end
|
27
|
+
|
28
|
+
class Rational
|
29
|
+
# public_class_method :new
|
30
|
+
|
31
|
+
# def pdivmod(other); [self / other, zero]; end
|
32
|
+
alias pdivmod divmod
|
33
|
+
|
34
|
+
def self.ground; Integer; end
|
35
|
+
|
36
|
+
def self.regulate(o)
|
37
|
+
case o
|
38
|
+
when self
|
39
|
+
o
|
40
|
+
when Numeric
|
41
|
+
Rational(o)
|
42
|
+
else
|
43
|
+
nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.wedge(otype)
|
48
|
+
if otype <= Integer || otype <= self
|
49
|
+
self
|
50
|
+
else
|
51
|
+
otype
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def devide?(other); true; end
|
56
|
+
|
57
|
+
def gcd_coeff(b)
|
58
|
+
if b.zero?
|
59
|
+
[self, self.class.unity, self.class.zero]
|
60
|
+
else
|
61
|
+
q, r = divmod b
|
62
|
+
d, x, y = b.gcd_coeff(r)
|
63
|
+
[d, y, x - y * q]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# def self.unity; new(1, 1); end
|
68
|
+
# def self.zero; new(0, 1); end
|
69
|
+
def self.unity; Rational(1); end
|
70
|
+
def self.zero; Rational(0); end
|
71
|
+
|
72
|
+
|
73
|
+
def mod(o)
|
74
|
+
raise ZeroDivisionError, "devided by 0" if o.zero?
|
75
|
+
den, a, = denominator.gcd_coeff(o)
|
76
|
+
num = (numerator * a) % o
|
77
|
+
q, r = num.divmod den
|
78
|
+
raise "denominator(#{denominator}) can not divide numerator(#{numerator}) mod #{o}" unless r.zero?
|
79
|
+
q
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,219 @@
|
|
1
|
+
# Residue Class Ring
|
2
|
+
#
|
3
|
+
# by Shin-ichiro Hara
|
4
|
+
#
|
5
|
+
# Version 1.2 (2001.04.08)
|
6
|
+
|
7
|
+
require "algebra/euclidian-ring"
|
8
|
+
require "algebra/algebraic-system"
|
9
|
+
#require "algebra/algebraic-extension-field.rb"
|
10
|
+
|
11
|
+
module Algebra
|
12
|
+
def ResidueClassRing(klass, mod)
|
13
|
+
ResidueClassRing.create(klass, mod)
|
14
|
+
end
|
15
|
+
|
16
|
+
=begin
|
17
|
+
def AlgebraicExtensionField(field, var)
|
18
|
+
ring = Polynomial(field, var)
|
19
|
+
k = ResidueClassRing(ring, yield(ring.var))
|
20
|
+
def k.var; self[ground.var]; end
|
21
|
+
k
|
22
|
+
end
|
23
|
+
module_function :ResidueClassRing, :AlgebraicExtensionField
|
24
|
+
=end
|
25
|
+
module_function :ResidueClassRing
|
26
|
+
|
27
|
+
class ResidueClassRing
|
28
|
+
extend AlgebraCreator
|
29
|
+
include AlgebraBase
|
30
|
+
|
31
|
+
attr_reader :x
|
32
|
+
|
33
|
+
def lift; @x; end
|
34
|
+
|
35
|
+
def self.[](m); new(m); end
|
36
|
+
|
37
|
+
def self.indeterminate(obj)
|
38
|
+
new(ground.indeterminate(obj))
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.**(elem)
|
42
|
+
#elem: Polynomial, ResidueClassRing
|
43
|
+
elem.convert_to(self)
|
44
|
+
end
|
45
|
+
|
46
|
+
def initialize(x)
|
47
|
+
@x = x % modulus
|
48
|
+
end
|
49
|
+
|
50
|
+
def monomial?
|
51
|
+
!@x.respond_to?(:monomial?) || @x.monomial?
|
52
|
+
#^^^^^^^^^ numeric ^^^^^^^^^
|
53
|
+
end
|
54
|
+
|
55
|
+
def need_paren_in_coeff?
|
56
|
+
if @x.respond_to?(:need_paren_in_coeff?)
|
57
|
+
@x.need_paren_in_coeff?
|
58
|
+
elsif @x.is_a?(Numeric)
|
59
|
+
false
|
60
|
+
else
|
61
|
+
true
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def evaluate(*a)
|
66
|
+
#p [333, @x, @x.class, a[0], a[0].class]
|
67
|
+
#b = a[0].lift
|
68
|
+
#c = b.evaluate(@x)
|
69
|
+
#p [c, c.class]
|
70
|
+
#d = self.class[c]
|
71
|
+
#p [d, d.class]
|
72
|
+
#k = @x.evaluate(*a)
|
73
|
+
#p [444, k, k.class, k.lift, k.lift.class]
|
74
|
+
self.class[@x.evaluate(*a)]
|
75
|
+
#d
|
76
|
+
end
|
77
|
+
|
78
|
+
def convert_to(k)
|
79
|
+
b = lift
|
80
|
+
c = b.evaluate(k.ground.var)
|
81
|
+
k[c]
|
82
|
+
end
|
83
|
+
|
84
|
+
def ==(other)
|
85
|
+
super{ |o|
|
86
|
+
((@x - o.x) % modulus).zero?
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
def +(other)
|
91
|
+
super{ |o|
|
92
|
+
self.class[@x + o.x]
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
def -(other)
|
97
|
+
super{ |o|
|
98
|
+
self.class[@x - o.x]
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
def *(other)
|
103
|
+
super{ |o|
|
104
|
+
self.class[@x * o.x]
|
105
|
+
}
|
106
|
+
end
|
107
|
+
|
108
|
+
def inverse
|
109
|
+
return self.class.inverse(@x) if self.class.respond_to? :inverse
|
110
|
+
return self.class[@x.inverse] if @x.respond_to? :inverse
|
111
|
+
d, a, = @x.gcd_coeff(modulus)
|
112
|
+
self.class.new(a / d)
|
113
|
+
end
|
114
|
+
|
115
|
+
def /(other)
|
116
|
+
if other.is_a?(self.class)
|
117
|
+
self * other.inverse
|
118
|
+
else
|
119
|
+
super{ |o|
|
120
|
+
self.class[@x / o.x]
|
121
|
+
}
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def pdivmod(other); [self / other, zero]; end
|
126
|
+
|
127
|
+
# def unit?
|
128
|
+
# inverse
|
129
|
+
# end
|
130
|
+
|
131
|
+
def to_s
|
132
|
+
(@x % modulus).to_s
|
133
|
+
end
|
134
|
+
|
135
|
+
alias inspect to_s unless $DEBUG
|
136
|
+
|
137
|
+
def modulus; self.class.modulus; end
|
138
|
+
def modulus=(var); self.class.modulus = var; end
|
139
|
+
|
140
|
+
def self.create(ground, mod)
|
141
|
+
klass = super(ground)
|
142
|
+
klass.sysvar(:modulus, mod)
|
143
|
+
|
144
|
+
if mod.is_a?(Integer) && (ground <= Integer || defined?(Rational) &&
|
145
|
+
ground <= Rational)
|
146
|
+
klass.class_eval <<__END_OF_CLASS_DEFINITION__
|
147
|
+
|
148
|
+
# @@modulus = #{mod}
|
149
|
+
@@inverse = [nil] + (1...@@modulus).collect{|x|
|
150
|
+
d, a, = x.gcd_coeff(@@modulus)
|
151
|
+
(y = a % @@modulus / d).zero? ? nil : y
|
152
|
+
}
|
153
|
+
require "algebra/prime-gen"
|
154
|
+
def self.field?; Primes.include?(@@modulus); end
|
155
|
+
def self.char; @@modulus; end
|
156
|
+
def self.inverse(k); @@inverse[k]; end
|
157
|
+
def self.to_ary; (0...@@modulus).collect{|x| self[x]}; end
|
158
|
+
|
159
|
+
__END_OF_CLASS_DEFINITION__
|
160
|
+
end
|
161
|
+
klass
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
if __FILE__ == $0
|
167
|
+
include Algebra
|
168
|
+
Z13 = ResidueClassRing.create(Integer, 13)
|
169
|
+
|
170
|
+
_a, b, c, d, e, _f, _g = Z13
|
171
|
+
p [e + c, e - c, e * c, e * 2001, 3 + c, 1/c, 1/c * c,
|
172
|
+
d / d, b * 1 / b] #=> [6, 2, 8, 9, 5, 7, 1, 1, 1]
|
173
|
+
p( (1...13).collect{|i| Z13[i]**12} )
|
174
|
+
#=> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
|
175
|
+
|
176
|
+
require "algebra/polynomial"
|
177
|
+
require "algebra/rational"
|
178
|
+
|
179
|
+
Px = Polynomial(Rational, "x")
|
180
|
+
# Px = Polynomial(Z13, "x")
|
181
|
+
|
182
|
+
x = Px.var
|
183
|
+
F = ResidueClassRing(Px, x**2 + x + 1)
|
184
|
+
x = F[x]
|
185
|
+
p( (x + 1)**100 ) #=> -x - 1
|
186
|
+
|
187
|
+
G = Polynomial(F, "y")
|
188
|
+
y = G.var
|
189
|
+
|
190
|
+
p( (x + y + 1)** 7 )
|
191
|
+
#=> y^7 + (7x + 7)y^6 + 8xy^5 + 4y^4 + (4x + 4)y^3 + 5xy^2 + 7y + x + 1
|
192
|
+
|
193
|
+
H = ResidueClassRing(G, y**5 + x*y + 1)
|
194
|
+
y = H[y]
|
195
|
+
|
196
|
+
p( (x + y + 1)**7 )
|
197
|
+
#=> 4y^4 + (3x + 4)y^3 + (5x + 6)y^2 + (x + 8)y + 6x + 1
|
198
|
+
p( 1/(x + y + 1)**7 )
|
199
|
+
#=> (1798/3x + 1825/9)y^4 + (-74x + 5176/9)y^3 +
|
200
|
+
# (-6886/9x - 5917/9)y^2 + (1826/3x - 3101/9)y + 2146/9x + 4702/9
|
201
|
+
|
202
|
+
require "algebra/polynomial"
|
203
|
+
# A = Polynomial(Rational, "x")
|
204
|
+
Z7 = ResidueClassRing.create(Integer, 7)
|
205
|
+
A = Polynomial(Z7, "x")
|
206
|
+
x = A.var
|
207
|
+
B = Polynomial(A, "y")
|
208
|
+
y = B.var
|
209
|
+
C = Polynomial(B, "z")
|
210
|
+
z = C.var
|
211
|
+
D = Polynomial(C, "w")
|
212
|
+
w = D.var
|
213
|
+
p( (x+y+z+w)**7 ) #=> w^7 + z^7 + y^7 + x^7
|
214
|
+
|
215
|
+
require "algebra/algebraic-extension-field.rb"
|
216
|
+
AEF = AlgebraicExtensionField(Rational, "x") {|x1| x1**2 + x1 + 1}
|
217
|
+
x = AEF.var
|
218
|
+
p( (x-1)** 3 / (x**2 - 1) )
|
219
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Class
|
2
|
+
def use_setname
|
3
|
+
const = 'C__names__'
|
4
|
+
evalstr = <<-__END_OF_EVAL__
|
5
|
+
class #{name}
|
6
|
+
raise '#{const} is already defined.' if defined? #{const}
|
7
|
+
|
8
|
+
#{const} = {}
|
9
|
+
|
10
|
+
raise 'setname is already defined.' if defined? setname
|
11
|
+
|
12
|
+
def setname(s)
|
13
|
+
#{const}[self] = s
|
14
|
+
end
|
15
|
+
|
16
|
+
alias inspect! inspect
|
17
|
+
|
18
|
+
def inspect
|
19
|
+
#{const}[self] || super
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
__END_OF_EVAL__
|
24
|
+
eval(evalstr, TOPLEVEL_BINDING)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module Marshal
|
29
|
+
def self.put(obj, filename)
|
30
|
+
open(filename, "wb") do |f|
|
31
|
+
Marshal.dump(obj, f)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
def self.get(filename)
|
35
|
+
open(filename, "rb") do |f|
|
36
|
+
Marshal.load(f)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def p!(x); puts x.inspect!; end
|
42
|
+
|
43
|
+
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# Splitting Field of Polynomial over Rational
|
2
|
+
#
|
3
|
+
# by Shin-ichiro Hara
|
4
|
+
#
|
5
|
+
# Version 1.11 (2002.02.23)
|
6
|
+
|
7
|
+
require 'algebra/polynomial'
|
8
|
+
require 'algebra/polynomial-factor'
|
9
|
+
require 'algebra/algebraic-extension-field'
|
10
|
+
|
11
|
+
module Algebra
|
12
|
+
module SplittingField
|
13
|
+
def decompose(pre_facts = nil, var_obj = 'a', no_sq = false)
|
14
|
+
poly = self
|
15
|
+
# poly may have duplicate roots
|
16
|
+
ext_field = poly.ground
|
17
|
+
|
18
|
+
polys = pre_facts ? pre_facts.dup : poly.factorize
|
19
|
+
roots = []
|
20
|
+
# roots == addelems + cmpelems, as set.
|
21
|
+
# the difference is ordering.
|
22
|
+
addelems = []
|
23
|
+
cmpelems = []
|
24
|
+
fac = Factors.new
|
25
|
+
|
26
|
+
while fn = polys.shift
|
27
|
+
f, n = fn # f: irreducible, n: duplication
|
28
|
+
if f.deg <= 1
|
29
|
+
fac.push [f, n]
|
30
|
+
roots.concat([-f[0] / f[1]] * n) unless f[1].zero?
|
31
|
+
cmpelems.concat([-f[0] / f[1]] * n) unless f[1].zero?
|
32
|
+
else
|
33
|
+
if !no_sq && f.deg == 2
|
34
|
+
newF, r, r0 = Algebra.QuadraticExtensionField(ext_field, &f)
|
35
|
+
roots.concat([r, r0] * n)
|
36
|
+
|
37
|
+
addelems.push r
|
38
|
+
cmpelems.concat([r] * (n - 1))
|
39
|
+
cmpelems.concat([r0] * n)
|
40
|
+
|
41
|
+
px, x = Algebra.Polynomial(newF, 'x')
|
42
|
+
fac.push [x - r, n]
|
43
|
+
fac.push [x - r0, n]
|
44
|
+
q = f.convert_to(px) / (x - r) / (x - r0) # scalar
|
45
|
+
else
|
46
|
+
newF = Algebra.AlgebraicExtensionField(ext_field, var_obj) do |v|
|
47
|
+
f.evaluate(v)
|
48
|
+
end
|
49
|
+
r = newF.var
|
50
|
+
|
51
|
+
roots.concat([r] * n)
|
52
|
+
addelems.push r
|
53
|
+
cmpelems.concat([r] * (n - 1))
|
54
|
+
|
55
|
+
px, x = Algebra.Polynomial(newF, 'x')
|
56
|
+
fac.push [x - r, n]
|
57
|
+
q = f.convert_to(px) / (x - r)
|
58
|
+
end
|
59
|
+
|
60
|
+
polys_new = q.factorize**n
|
61
|
+
polys.each do |g, m|
|
62
|
+
g0 = g.convert_to(px)
|
63
|
+
fc = g0.factorize
|
64
|
+
polys_new.concat fc**m
|
65
|
+
end
|
66
|
+
|
67
|
+
ext_field = newF
|
68
|
+
polys = polys_new
|
69
|
+
var_obj = /^[a-qs-z]\d*$/ =~ var_obj ? var_obj.succ : var_obj + '_'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
mods = if ext_field <= Algebra::AlgebraicExtensionField
|
74
|
+
ext_field.def_polys
|
75
|
+
else
|
76
|
+
[]
|
77
|
+
end
|
78
|
+
|
79
|
+
proots = addelems + cmpelems
|
80
|
+
|
81
|
+
[ext_field, mods, fac, roots, proots]
|
82
|
+
end
|
83
|
+
|
84
|
+
def splitting_field(pre_facts = nil, var_obj = 'a', no_sq = false)
|
85
|
+
var_obj ||= 'a'
|
86
|
+
poly = self
|
87
|
+
field, def_polys, fac, roots, proots =
|
88
|
+
poly.decompose(pre_facts, var_obj, no_sq)
|
89
|
+
roots = roots.collect { |r| field.regulate(r) }
|
90
|
+
proots = proots.collect { |r| field.regulate(r) }
|
91
|
+
|
92
|
+
Struct.new(:poly, :field, :roots, :def_polys, :proots)[
|
93
|
+
poly, field, roots, def_polys, proots
|
94
|
+
]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# for backward compatibility
|
99
|
+
def MinimalDecompositionField(f, *a)
|
100
|
+
f.decompose(*a)
|
101
|
+
end
|
102
|
+
module_function :MinimalDecompositionField
|
103
|
+
end
|