algebra 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +53 -0
  3. data/.travis.yml +7 -0
  4. data/CODE_OF_CONDUCT.md +49 -0
  5. data/Gemfile +7 -0
  6. data/Gemfile.lock +28 -0
  7. data/LICENSE.txt +21 -0
  8. data/README-ja.txt +60 -0
  9. data/README.md +61 -0
  10. data/Rakefile +43 -0
  11. data/algebra.gemspec +33 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/doc-ja/README-ja.html +69 -0
  15. data/doc-ja/README-ja.rd +60 -0
  16. data/doc-ja/README.html +0 -0
  17. data/doc-ja/README.rd +0 -0
  18. data/doc-ja/algebra-ja.html +66 -0
  19. data/doc-ja/algebra-ja.rd +60 -0
  20. data/doc-ja/algebraic-equation-ja.html +36 -0
  21. data/doc-ja/algebraic-equation-ja.rd +26 -0
  22. data/doc-ja/algebraic-extension-field-ja.html +122 -0
  23. data/doc-ja/algebraic-extension-field-ja.rd +118 -0
  24. data/doc-ja/algebraic-parser-ja.html +87 -0
  25. data/doc-ja/algebraic-parser-ja.rd +95 -0
  26. data/doc-ja/changes.html +161 -0
  27. data/doc-ja/changes.rd +119 -0
  28. data/doc-ja/elementary-divisor-ja.html +56 -0
  29. data/doc-ja/elementary-divisor-ja.rd +45 -0
  30. data/doc-ja/euclidian-ring-ja.html +78 -0
  31. data/doc-ja/euclidian-ring-ja.rd +69 -0
  32. data/doc-ja/finite-group-ja.html +285 -0
  33. data/doc-ja/finite-group-ja.rd +276 -0
  34. data/doc-ja/finite-map-ja.html +224 -0
  35. data/doc-ja/finite-map-ja.rd +227 -0
  36. data/doc-ja/finite-set-ja.html +393 -0
  37. data/doc-ja/finite-set-ja.rd +408 -0
  38. data/doc-ja/index-ja.html +113 -0
  39. data/doc-ja/index-ja.rd +112 -0
  40. data/doc-ja/jordan-form-ja.html +106 -0
  41. data/doc-ja/jordan-form-ja.rd +99 -0
  42. data/doc-ja/localized-ring-ja.html +121 -0
  43. data/doc-ja/localized-ring-ja.rd +131 -0
  44. data/doc-ja/m-polynomial-ja.html +542 -0
  45. data/doc-ja/m-polynomial-ja.rd +596 -0
  46. data/doc-ja/matrix-algebra-ja.html +686 -0
  47. data/doc-ja/matrix-algebra-ja.rd +727 -0
  48. data/doc-ja/matrix-algebra-triplet-ja.html +124 -0
  49. data/doc-ja/matrix-algebra-triplet-ja.rd +129 -0
  50. data/doc-ja/permutation-group-ja.html +159 -0
  51. data/doc-ja/permutation-group-ja.rd +151 -0
  52. data/doc-ja/polynomial-converter-ja.html +64 -0
  53. data/doc-ja/polynomial-converter-ja.rd +54 -0
  54. data/doc-ja/polynomial-ja.html +446 -0
  55. data/doc-ja/polynomial-ja.rd +473 -0
  56. data/doc-ja/residue-class-ring-ja.html +112 -0
  57. data/doc-ja/residue-class-ring-ja.rd +113 -0
  58. data/doc-ja/sample-algebraic-equation01.rb.v.rd +11 -0
  59. data/doc-ja/sample-algebraic-equation02.rb.v.rd +11 -0
  60. data/doc-ja/sample-algebraic-root01.rb.v.rd +20 -0
  61. data/doc-ja/sample-algebraicfield01.rb.v.rd +26 -0
  62. data/doc-ja/sample-algebraicfield02.rb.v.rd +15 -0
  63. data/doc-ja/sample-cayleyhamilton01.rb.v.rd +14 -0
  64. data/doc-ja/sample-diagonalization01.rb.v.rd +48 -0
  65. data/doc-ja/sample-divmod01.rb.v.rd +17 -0
  66. data/doc-ja/sample-elementary-divisor01.rb.v.rd +48 -0
  67. data/doc-ja/sample-factorize01.rb.v.rd +11 -0
  68. data/doc-ja/sample-factorize02.rb.v.rd +13 -0
  69. data/doc-ja/sample-factorize03.rb.v.rd +14 -0
  70. data/doc-ja/sample-factorize04.rb.v.rd +14 -0
  71. data/doc-ja/sample-factorize05.rb.v.rd +53 -0
  72. data/doc-ja/sample-galois-group01.rb.v.rd +27 -0
  73. data/doc-ja/sample-gaussian-elimination01.rb.v.rd +19 -0
  74. data/doc-ja/sample-geometry01.rb.v.rd +33 -0
  75. data/doc-ja/sample-geometry02.rb.v.rd +34 -0
  76. data/doc-ja/sample-geometry03.rb.v.rd +45 -0
  77. data/doc-ja/sample-geometry04.rb.v.rd +33 -0
  78. data/doc-ja/sample-geometry07.rb.v.rd +62 -0
  79. data/doc-ja/sample-groebner01.rb.v.rd +13 -0
  80. data/doc-ja/sample-groebner02.rb.v.rd +18 -0
  81. data/doc-ja/sample-groebner03.rb.v.rd +19 -0
  82. data/doc-ja/sample-group01.rb.v.rd +21 -0
  83. data/doc-ja/sample-jordan-form01.rb.v.rd +56 -0
  84. data/doc-ja/sample-jordanform01.rb.v.rd +55 -0
  85. data/doc-ja/sample-lagrange-multiplier01.rb.v.rd +35 -0
  86. data/doc-ja/sample-m-factorize01.rb.v.rd +15 -0
  87. data/doc-ja/sample-m-factorize02.rb.v.rd +12 -0
  88. data/doc-ja/sample-m-polynomial01.rb.v.rd +10 -0
  89. data/doc-ja/sample-map01.rb.v.rd +9 -0
  90. data/doc-ja/sample-polynomial01.rb.v.rd +9 -0
  91. data/doc-ja/sample-polynomial02.rb.v.rd +11 -0
  92. data/doc-ja/sample-primefield01.rb.v.rd +13 -0
  93. data/doc-ja/sample-quotientfield01.rb.v.rd +11 -0
  94. data/doc-ja/sample-quotientfield02.rb.v.rd +18 -0
  95. data/doc-ja/sample-quotientfield03.rb.v.rd +16 -0
  96. data/doc-ja/sample-quotientfield04.rb.v.rd +16 -0
  97. data/doc-ja/sample-set01.rb.v.rd +18 -0
  98. data/doc-ja/sample-splitting-field01.rb.v.rd +19 -0
  99. data/doc-ja/samples-ja.html +885 -0
  100. data/doc-ja/samples-ja.rd +221 -0
  101. data/doc-ja/style.css +85 -0
  102. data/doc-ja/todo.html +20 -0
  103. data/doc-ja/todo.rd +9 -0
  104. data/lib/algebra.rb +36 -0
  105. data/lib/algebra/algebraic-equation.rb +56 -0
  106. data/lib/algebra/algebraic-extension-field.rb +135 -0
  107. data/lib/algebra/algebraic-parser.rb +160 -0
  108. data/lib/algebra/algebraic-system.rb +224 -0
  109. data/lib/algebra/annihilate.rb +52 -0
  110. data/lib/algebra/array-supplement.rb +53 -0
  111. data/lib/algebra/auto-require.rb +84 -0
  112. data/lib/algebra/chinese-rem-th.rb +135 -0
  113. data/lib/algebra/combinatorial.rb +145 -0
  114. data/lib/algebra/elementary-divisor.rb +193 -0
  115. data/lib/algebra/euclidian-ring.rb +161 -0
  116. data/lib/algebra/factors.rb +305 -0
  117. data/lib/algebra/finite-group.rb +374 -0
  118. data/lib/algebra/finite-map.rb +201 -0
  119. data/lib/algebra/finite-set.rb +456 -0
  120. data/lib/algebra/galois-group.rb +129 -0
  121. data/lib/algebra/gaussian-elimination.rb +385 -0
  122. data/lib/algebra/groebner-basis-coeff.rb +228 -0
  123. data/lib/algebra/groebner-basis.rb +197 -0
  124. data/lib/algebra/import-module-single-thread.rb +86 -0
  125. data/lib/algebra/import-module.rb +491 -0
  126. data/lib/algebra/jordan-form.rb +114 -0
  127. data/lib/algebra/linear-algebra.rb +143 -0
  128. data/lib/algebra/localized-ring.rb +294 -0
  129. data/lib/algebra/m-index.rb +282 -0
  130. data/lib/algebra/m-polynomial-factor-int.rb +186 -0
  131. data/lib/algebra/m-polynomial-factor-zp.rb +114 -0
  132. data/lib/algebra/m-polynomial-factor.rb +315 -0
  133. data/lib/algebra/m-polynomial-gcd.rb +40 -0
  134. data/lib/algebra/m-polynomial.rb +875 -0
  135. data/lib/algebra/matrix-algebra-triplet.rb +292 -0
  136. data/lib/algebra/matrix-algebra.rb +929 -0
  137. data/lib/algebra/numeric-supplement.rb +123 -0
  138. data/lib/algebra/permutation-group.rb +257 -0
  139. data/lib/algebra/polynomial-converter.rb +193 -0
  140. data/lib/algebra/polynomial-factor-alg.rb +148 -0
  141. data/lib/algebra/polynomial-factor-int.rb +252 -0
  142. data/lib/algebra/polynomial-factor-zp.rb +165 -0
  143. data/lib/algebra/polynomial-factor.rb +140 -0
  144. data/lib/algebra/polynomial.rb +592 -0
  145. data/lib/algebra/powers.rb +31 -0
  146. data/lib/algebra/prime-gen.rb +32 -0
  147. data/lib/algebra/rational.rb +81 -0
  148. data/lib/algebra/residue-class-ring.rb +219 -0
  149. data/lib/algebra/sets-system.rb +43 -0
  150. data/lib/algebra/splitting-field.rb +103 -0
  151. data/lib/algebra/version.rb +3 -0
  152. data/sample/Makefile +10 -0
  153. data/sample/do-testscripts.rb +100 -0
  154. data/sample/sample-algebraic-equation01.rb +6 -0
  155. data/sample/sample-algebraic-equation02.rb +6 -0
  156. data/sample/sample-algebraic-root01.rb +15 -0
  157. data/sample/sample-algebraicfield01.rb +21 -0
  158. data/sample/sample-algebraicfield02.rb +10 -0
  159. data/sample/sample-cayleyhamilton01.rb +9 -0
  160. data/sample/sample-diagonalization01.rb +43 -0
  161. data/sample/sample-divmod01.rb +12 -0
  162. data/sample/sample-elementary-divisor01.rb +43 -0
  163. data/sample/sample-factorize01.rb +6 -0
  164. data/sample/sample-factorize02.rb +8 -0
  165. data/sample/sample-factorize03.rb +9 -0
  166. data/sample/sample-factorize04.rb +9 -0
  167. data/sample/sample-factorize05.rb +48 -0
  168. data/sample/sample-galois-group01.rb +22 -0
  169. data/sample/sample-gaussian-elimination01.rb +14 -0
  170. data/sample/sample-geometry01.rb +28 -0
  171. data/sample/sample-geometry02.rb +29 -0
  172. data/sample/sample-geometry03.rb +40 -0
  173. data/sample/sample-geometry04.rb +28 -0
  174. data/sample/sample-geometry07.rb +58 -0
  175. data/sample/sample-groebner01.rb +8 -0
  176. data/sample/sample-groebner02.rb +13 -0
  177. data/sample/sample-groebner03.rb +14 -0
  178. data/sample/sample-group01.rb +16 -0
  179. data/sample/sample-jordan-form01.rb +51 -0
  180. data/sample/sample-lagrange-multiplier01.rb +30 -0
  181. data/sample/sample-m-factorize01.rb +10 -0
  182. data/sample/sample-m-factorize02.rb +7 -0
  183. data/sample/sample-m-polynomial01.rb +5 -0
  184. data/sample/sample-map01.rb +4 -0
  185. data/sample/sample-polynomial01.rb +4 -0
  186. data/sample/sample-polynomial02.rb +6 -0
  187. data/sample/sample-primefield01.rb +8 -0
  188. data/sample/sample-quotientfield01.rb +6 -0
  189. data/sample/sample-quotientfield02.rb +13 -0
  190. data/sample/sample-quotientfield03.rb +11 -0
  191. data/sample/sample-quotientfield04.rb +11 -0
  192. data/sample/sample-set01.rb +13 -0
  193. data/sample/sample-splitting-field01.rb +14 -0
  194. data/sample/test-00-cayley-hamilton.rb +76 -0
  195. data/sample/test-00-groebner-basis.rb +274 -0
  196. data/sample/test-00-polynomial-factor-alg.rb +89 -0
  197. data/sample/test-00.list +4 -0
  198. data/sample/time-trial.rb +65 -0
  199. metadata +373 -0
@@ -0,0 +1,29 @@
1
+ require 'algebra'
2
+ R = MPolynomial(Rational)
3
+ x,y,a1,a2,b1,b2,c1,c2 = R.vars('xya1a2b1b2c1c2')
4
+
5
+ V = Vector(R, 2)
6
+ X, A, B, C = V[x,y], V[a1,a2], V[b1,b2], V[c1,c2]
7
+
8
+ def line(p1, p2, p3)
9
+ SquareMatrix.det([[1, *p1], [1, *p2], [1, *p3]])
10
+ end
11
+
12
+ def vline(p1, p2, p3)
13
+ (p1-p2).norm2 - (p1-p3).norm2
14
+ end
15
+
16
+ l1 = vline(X, A, B)
17
+ l2 = vline(X, B, C)
18
+ l3 = vline(X, C, A)
19
+
20
+ s = line(A, B, C)
21
+
22
+ g = Groebner.basis [l1, l2, l3, s-1] #s-1 means non degeneracy
23
+
24
+ g.each do |f|
25
+ p f
26
+ end
27
+ #x - 1/2a1a2b1 + 1/2a1a2c1 + 1/2a1b1c2 - 1/2a1c1c2 - 1/2a1 - 1/2a2^2b2 + 1/2a2^2c2 + 1/2a2b1^2 - 1/2a2b1c1 + 1/2a2b2^2 - 1/2a2c2^2 - 1/2b1^2c2 + 1/2b1c1c2 - 1/2b2^2c2 + 1/2b2c2^2 - 1/2c1
28
+ #y + 1/2a1^2b1 - 1/2a1^2c1 - 1/2a1b1^2 + 1/2a1c1^2 + 1/2a2^2b1 - 1/2a2^2c1 - 1/2a2b1b2 - 1/2a2b1c2 + 1/2a2b2c1 + 1/2a2c1c2 + 1/2b1^2c1 + 1/2b1b2c2 - 1/2b1c1^2 -1/2b2c1c2 - 1/2b2 - 1/2c2
29
+ #a1b2 - a1c2 - a2b1 + a2c1 + b1c2 - b2c1 - 1
@@ -0,0 +1,40 @@
1
+ #incenter
2
+ puts "too heavy ... skip (#{$0})"
3
+ exit
4
+ require 'algebra'
5
+
6
+ R = MPolynomial(Rational)
7
+ x,y,a1,a2,b1,b2,c1,c2 = R.vars('xya1a2b1b2c1c2')
8
+ Q = LocalizedRing(R)
9
+ x0,y0,a10,a20,b10,b20,c10,c20 = R.vars.map{|v| Q[v]}
10
+ V = Vector(R, 2)
11
+ W = Vector(Q, 2)
12
+ X, A, B, C = V[x,y], V[a1,a2], V[b1,b2], V[c1,c2]
13
+
14
+ def bisector(p1, p2, p3)
15
+ p1,p2,p3 = [p1,p2,p3].map{|v| v.convert_to(W)}
16
+ k1 = (p1.inner_product(p2))**2/p2.norm2
17
+ k2 = (p1.inner_product(p3))**2/p3.norm2
18
+ (k1-k2).numerator
19
+ end
20
+
21
+ def line(p1, p2, p3)
22
+ SquareMatrix.det([[1, *p1], [1, *p2], [1, *p3]])
23
+ end
24
+
25
+ l1 = bisector(X-A, B-A, C-A)
26
+ p l1
27
+ l2 = bisector(X-B, C-B, A-B)
28
+ p l2
29
+ l3 = bisector(X-C, A-C, B-C)
30
+ p l3
31
+
32
+ s = line(A, B, C)
33
+
34
+ g = Groebner.basis [l1, l2, l3, s-1] #s-1 means non degeneracy
35
+
36
+ g.each do |f|
37
+ p f
38
+ end
39
+
40
+ #p g.size > 1
@@ -0,0 +1,28 @@
1
+ require 'algebra'
2
+
3
+ R = MPolynomial(Rational)
4
+ x,y,a1,a2,b1,b2,c1,c2 = R.vars('xya1a2b1b2c1c2')
5
+ V = Vector(R, 2)
6
+ X, A, B, C = V[x,y], V[a1,a2], V[b1,b2], V[c1,c2]
7
+
8
+ def perpendicular(p0, p1, p2, p3)
9
+ (p0-p1).inner_product(p2-p3)
10
+ end
11
+
12
+ def line(p1, p2, p3)
13
+ SquareMatrix.det([[1, *p1], [1, *p2], [1, *p3]])
14
+ end
15
+
16
+ l1 = perpendicular(X, A, B, C)
17
+ l2 = perpendicular(X, B, C, A)
18
+ l3 = perpendicular(X, C, A, B)
19
+
20
+ s = line(A, B, C)
21
+ g = Groebner.basis [l1, l2, l3, s-1] #s-1 means non degeneracy
22
+
23
+ g.each do |f|
24
+ p f
25
+ end
26
+ #x + a1a2b1 - a1a2c1 - a1b1c2 + a1c1c2 + a2^2b2 - a2^2c2 - a2b1^2 + a2b1c1 - a2b2^2 + a2c2^2 + b1^2c2 - b1c1c2 - b1 + b2^2c2 - b2c2^2
27
+ #y - a1^2b1 + a1^2c1 + a1b1^2 - a1c1^2 - a2^2b1 + a2^2c1 + a2b1b2 + a2b1c2 - a2b2c1 - a2c1c2 - a2 - b1^2c1 - b1b2c2 + b1c1^2 + b2c1c2
28
+ #a1b2 - a1c2 - a2b1 + a2c1 + b1c2 - b2c1 - 1
@@ -0,0 +1,58 @@
1
+ require "algebra"
2
+ R = MPolynomial(Rational)
3
+ m, b, a = R.vars("mba")
4
+ K = LocalizedRing(R)
5
+ a0, b0, m0 = K[a], K[b], K[m]
6
+
7
+ M = SquareMatrix(K, 3)
8
+ l0, l1, l2 = M[[0, 1, 0], [1, 1, -1], [1, 0, 0]]
9
+ m4 = M[[a0, 1, -a0], [1, b0, -b0], [m0, -1, 0]]
10
+ m40 = m4.dup; m40.set_row(0, l0)
11
+ m41 = m4.dup; m41.set_row(1, l1)
12
+ m42 = m4.dup; m42.set_row(2, l2)
13
+
14
+ def mdet(m, i)
15
+ i = i % 3
16
+ m.minor(i, 2).determinant * (-1) ** i
17
+ end
18
+
19
+ def tris(m, i)
20
+ pts = [0, 1, 2] - [i]
21
+ (m.determinant)**2 / mdet(m, pts[0]) / mdet(m, pts[1])
22
+ end
23
+
24
+ u0 = (tris(m4, 0) - tris(m40, 0)).numerator
25
+ u1 = (tris(m4, 1) - tris(m41, 1)).numerator
26
+ u2 = (tris(m4, 2) - tris(m42, 2)).numerator
27
+
28
+ puts [u0.to_s, u1.to_s, u2.to_s]
29
+ puts
30
+
31
+ [u0, u1, u2].each do |um|
32
+ p um.factorize
33
+ end
34
+ puts
35
+
36
+ x = u0 / a / (m*b+1)
37
+ y = u1 / (m+a) / (b-1)
38
+ z = u2 / (-b*a+1)
39
+
40
+ p x
41
+ p y
42
+ p z
43
+ puts
44
+
45
+ gb = Groebner.basis([x, y, z])
46
+ p gb
47
+ # puts gb
48
+ puts
49
+
50
+ gb.each do |v|
51
+ p v.factorize
52
+ end
53
+ #(-1)(-mb + m + ba^3 + ba^2 - ba - a^2)
54
+ #(-1)(-ma + m + ba^2 - a^2)
55
+ #(-1)(b + a - 1)(-ba + 1)(a)
56
+ #(-1)(-ba + 1)(a)(a^2 + a - 1)
57
+
58
+ # => a = -1/2 + \sqrt{5}/2
@@ -0,0 +1,8 @@
1
+ require "algebra"
2
+ P = MPolynomial(Rational, "xyz")
3
+ x, y, z = P.vars("xyz")
4
+ f1 = x**2 + y**2 + z**2 -1
5
+ f2 = x**2 + z**2 - y
6
+ f3 = x - z
7
+ p Groebner.basis([f1, f2, f3])
8
+ #=> [x - z, y - 2z^2, z^4 + 1/2z^2 - 1/4]
@@ -0,0 +1,13 @@
1
+ require "algebra"
2
+
3
+ P = MPolynomial(Rational)
4
+ x, y, z = P.vars "xyz"
5
+ f1 = x**2 + y**2 + z**2 -1
6
+ f2 = x**2 + z**2 - y
7
+ f3 = x - z
8
+
9
+ coeff, basis = Groebner.basis_coeff([f1, f2, f3])
10
+ basis.each_with_index do |b, i|
11
+ p [coeff[i].inner_product([f1, f2, f3]), b]
12
+ p coeff[i].inner_product([f1, f2, f3]) == b #=> true
13
+ end
@@ -0,0 +1,14 @@
1
+ require "algebra"
2
+ F5 = ResidueClassRing(Integer, 2)
3
+ F = AlgebraicExtensionField(F5, "a") {|a| a**3 + a + 1}
4
+ a = F.var
5
+ P = MPolynomial(F)
6
+
7
+ x, y, z = P.vars("xyz")
8
+ f1 = x + y**2 + z**2 - 1
9
+ f2 = x**2 + z**2 - y * a
10
+ f3 = x - z - a
11
+
12
+ f = x**3 + y**3 + z**3
13
+ q, r = f.divmod_s(f1, f2, f3)
14
+ p f == q.inner_product([f1, f2, f3]) + r #=> true
@@ -0,0 +1,16 @@
1
+ require "algebra"
2
+ e = Permutation[0, 1, 2, 3, 4]
3
+ a = Permutation[1, 0, 3, 4, 2]
4
+ b = Permutation[0, 2, 1, 3, 4]
5
+ p a * b #=> [2, 0, 3, 4, 1]
6
+
7
+ g = Group.new(e, a, b)
8
+ g.complete!
9
+ p g == PermutationGroup.symmetric(5) #=> true
10
+
11
+
12
+
13
+
14
+
15
+
16
+
@@ -0,0 +1,51 @@
1
+ require "algebra"
2
+
3
+ M4 = SquareMatrix(Rational, 4)
4
+ m = M4[
5
+ [-1, 1, 2, -1],
6
+ [-5, 3, 4, -2],
7
+ [3, -1, 0, 1],
8
+ [5, -2, -2, 3]
9
+ ]
10
+ m.jordan_form.display; #=>
11
+ # 2, 0, 0, 0
12
+ # 0, 1, 1, 0
13
+ # 0, 0, 1, 1
14
+ # 0, 0, 0, 1
15
+ puts
16
+
17
+ #-----------------------------------
18
+ m = M4[
19
+ [3, 1, -1, 1],
20
+ [-3, -1, 3, -1],
21
+ [-2, -2, 0, 0],
22
+ [0, 0, -4, 2]
23
+ ]
24
+ jf, pt, qt, field, modulus = m.jordan_form_info
25
+ p modulus #=> [a^2 + 4]
26
+ jf.display; puts #=>
27
+ # 2, 1, 0, 0
28
+ # 0, 2, 0, 0
29
+ # 0, 0, a, 0
30
+ # 0, 0, 0, -a
31
+
32
+ m = m.convert_to(jf.class)
33
+ p jf == pt * m * qt #=> true
34
+
35
+ #-----------------------------------
36
+ m = M4[
37
+ [-1, 1, 2, -1],
38
+ [-5, 3, 4, -2],
39
+ [3, -1, 0, 1],
40
+ [5, -2, -2, 0]
41
+ ]
42
+ jf, pt, qt, field, modulus = m.jordan_form_info
43
+ p modulus #=> [a^3 + 3a - 1, b^2 + ab + a^2 + 3]
44
+ jf.display; puts #=>
45
+ # 2, 0, 0, 0
46
+ # 0, a, 0, 0
47
+ # 0, 0, b, 0
48
+ # 0, 0, 0, -b - a
49
+
50
+ m = m.convert_to(jf.class)
51
+ p jf == pt * m * qt #=> true
@@ -0,0 +1,30 @@
1
+ require 'algebra'
2
+ P = MPolynomial(Rational)
3
+ t, x, y, z = P.vars('txyz')
4
+ f = x**3+2*x*y*z - z**2
5
+ g = x**2 + y**2 + z**2 - 1
6
+
7
+ fx = f.derivate(x)
8
+ fy = f.derivate(y)
9
+ fz = f.derivate(z)
10
+
11
+ gx = g.derivate(x)
12
+ gy = g.derivate(y)
13
+ gz = g.derivate(z)
14
+
15
+ F = [fx - t * gx, fy - t * gy, fz - t * gz, g]
16
+
17
+ Groebner.basis(F).each do |h|
18
+ p h.factorize
19
+ end
20
+
21
+ #(1/7670)(7670t - 11505x - 11505yz - 335232z^6 + 477321z^4 - 134419z^2)
22
+ #x^2 + y^2 + z^2 - 1
23
+ #(1/3835)(3835xy - 19584z^5 + 25987z^3 - 6403z)
24
+ #(1/3835)(3835x + 3835yz - 1152z^4 - 1404z^2 + 2556)(z)
25
+ #(1/3835)(3835y^3 + 3835yz^2 - 3835y - 9216z^5 + 11778z^3 - 2562z)
26
+ #(1/3835)(3835y^2 - 6912z^4 + 10751z^2 - 3839)(z)
27
+ #(1/118)(118y - 1152z^3 + 453z)(z)(z - 1)(z + 1)
28
+ #(1/1152)(z)(z - 1)(3z - 2)(3z + 2)(z + 1)(128z^2 - 11)
29
+
30
+
@@ -0,0 +1,10 @@
1
+ require "algebra"
2
+ P = MPolynomial(Integer)
3
+ x, y, z = P.vars("xyz")
4
+ f = x**3 + y**3 + z**3 - 3*x*y*z
5
+ p f.factorize #=> (x + y + z)(x^2 - xy - xz + y^2 - yz + z^2)
6
+
7
+ PQ = MPolynomial(Rational)
8
+ x, y, z = PQ.vars("xyz")
9
+ f = x**3 + y**3/8 + z**3 - 3*x*y*z/2
10
+ p f.factorize #=> (1/8)(2x + y + 2z)(4x^2 - 2xy - 4xz + y^2 - 2yz + 4z^2)
@@ -0,0 +1,7 @@
1
+ require "algebra"
2
+
3
+ Z7 = ResidueClassRing(Integer, 7)
4
+ P = MPolynomial(Z7)
5
+ x, y, z = P.vars("xyz")
6
+ f = x**3 + y**3 + z**3 - 3*x*y*z
7
+ p f.factorize #=> (x + 4y + 2z)(x + 2y + 4z)(x + y + z)
@@ -0,0 +1,5 @@
1
+ require "algebra"
2
+ P = MPolynomial(Integer)
3
+ x, y, z, w = P.vars("xyz")
4
+ p((-x + y + z)*(x + y - z)*(x - y + z))
5
+ #=> -x^3 + x^2y + x^2z + xy^2 - 2xyz + xz^2 - y^3 + y^2z + yz^2 - z^3
@@ -0,0 +1,4 @@
1
+ require "algebra"
2
+ a = Map[0=>2, 1=>2, 2=>0]
3
+ b = Map[0=>1, 1=>1, 2=>1]
4
+ p a * b #=> {0=>2, 1=>2, 2=>2}
@@ -0,0 +1,4 @@
1
+ require "algebra"
2
+ P = Polynomial.create(Integer, "x")
3
+ x = P.var
4
+ p((x + 1)**100) #=> x^100 + 100x^99 + ... + 100x + 1
@@ -0,0 +1,6 @@
1
+ require "algebra"
2
+ P = Polynomial(Integer, "x", "y", "z")
3
+ x, y, z = P.vars
4
+ p((-x + y + z)*(x + y - z)*(x - y + z))
5
+ #=> -z^3 + (y + x)z^2 + (y^2 - 2xy + x^2)z - y^3 + xy^2 + x^2y - x^3
6
+
@@ -0,0 +1,8 @@
1
+ require "algebra"
2
+ Z13 = ResidueClassRing(Integer, 13)
3
+
4
+ a, b, c, d, e, f, g = Z13
5
+ p [e + c, e - c, e * c, e * 2001, 3 + c, 1/c, 1/c * c, d / d, b * 1 / b]
6
+ #=> [6, 2, 8, 9, 5, 7, 1, 1, 1]
7
+ p( (1...13).collect{|i| Z13[i]**12} )
8
+ #=> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
@@ -0,0 +1,6 @@
1
+ require "algebra"
2
+ Q = LocalizedRing(Integer)
3
+ a = Q.new(3, 5)
4
+ b = Q.new(5, 3)
5
+ p [a + b, a - b, a * b, a / b, a + 3, 1 + a]
6
+ #=> [34/15, -16/15, 15/15, 9/25, 18/5, 8/5]
@@ -0,0 +1,13 @@
1
+ require "algebra"
2
+
3
+ F13 = ResidueClassRing(Integer, 13)
4
+
5
+ P = Polynomial(F13, "x")
6
+ Q = LocalizedRing(P)
7
+ x = Q[P.var]
8
+ p ( 1 / (x**2 - 1) - 1 / (x**3 - 1) )
9
+
10
+ #This is equivalent to the following
11
+ F = RationalFunctionField(F13, "x")
12
+ x = F.var
13
+ p ( 1 / (x**2 - 1) - 1 / (x**3 - 1) )
@@ -0,0 +1,11 @@
1
+ require "algebra"
2
+
3
+ F13 = ResidueClassRing(Integer, 13)
4
+ F = AlgebraicExtensionField(F13, "a") {|a| a**2 - 2}
5
+ a = F.var
6
+ RF = RationalFunctionField(F, "x")
7
+ x = RF.var
8
+
9
+ p( (a/4*x + RF.unity/2)/(x**2 + a*x + 1) +
10
+ (-a/4*x + RF.unity/2)/(x**2 - a*x + 1) )
11
+ #=> 1/(x**4 + 1)
@@ -0,0 +1,11 @@
1
+ require "algebra"
2
+
3
+ F13 = ResidueClassRing(Integer, 13)
4
+ F = RationalFunctionField(F13, "x")
5
+ x = F.var
6
+ AF = AlgebraicExtensionField(F, "a") {|a| a**2 - 2*x}
7
+ a = AF.var
8
+
9
+ p( (a/4*x + AF.unity/2)/(x**2 + a*x + 1) +
10
+ (-a/4*x + AF.unity/2)/(x**2 - a*x + 1) )
11
+ #=> (-x^3 + x^2 + 1)/(x^4 + 11x^3 + 2x^2 + 1)
@@ -0,0 +1,13 @@
1
+ require "algebra"
2
+ #intersection
3
+ p Set[0, 1, 2, 4] & Set[1, 3, 5] == Set[1]
4
+ p Set[0, 1, 2, 4] & Set[7, 3, 5] == Set.phi
5
+
6
+ #union
7
+ p Set[0, 1, 2, 4] | Set[1, 3, 5] == Set[0, 1, 2, 3, 4, 5]
8
+
9
+ #membership
10
+ p Set[1, 3, 2].has?(1)
11
+
12
+ #inclusion
13
+ p Set[3, 2, 1, 3] < Set[3, 1, 4, 2, 0]
@@ -0,0 +1,14 @@
1
+ require "algebra"
2
+
3
+ PQ = Polynomial(Rational, "x")
4
+ x = PQ.var
5
+ f = x**4 + 2
6
+ p f #=> x^4 + 2
7
+ field, modulus, facts = f.decompose
8
+ p modulus #=> [a^4 + 2, b^2 + a^2]
9
+ p facts #=> (x - a)(x + a)(x - b)(x + b)
10
+
11
+ fp = Polynomial(field, "x")
12
+ x = fp.var
13
+ facts = Factors.new(facts.collect{|g, n| [g.evaluate(x), n]})
14
+ p facts.pi == f.convert_to(fp) #=> true
@@ -0,0 +1,76 @@
1
+ # Test of Cayley-Hamilton's Theory
2
+ #
3
+ # by Shin-ichiro Hara
4
+ #
5
+ # Version 1.0 (2001.04.08)
6
+
7
+ require "algebra/matrix-algebra"
8
+ require "algebra/m-polynomial"
9
+ require "algebra/polynomial"
10
+ require "algebra/polynomial-converter"
11
+ include Algebra
12
+
13
+ n = Integer(ARGV.shift || 2)
14
+ $mul = ARGV.shift
15
+ case n
16
+ when 2
17
+ unless $mul
18
+ R = MPolynomial(Integer)
19
+ a, b, c, d = R.vars("abcd")
20
+ else
21
+ R = Polynomial.over(Integer, *"abcd".scan(/./))
22
+ a, b, c, d = R.vars
23
+ end
24
+ Rx = Polynomial(R, "x")
25
+ x = Rx.var
26
+
27
+ M2 = SquareMatrix(Rx, 2)
28
+ m = M2[
29
+ [a, b],
30
+ [c, d]
31
+ ]
32
+ when 3
33
+ unless $mul
34
+ R = MPolynomial(Integer)
35
+ a, b, c, d, e, f, g, h, i, j = R.vars("abcdefghij")
36
+ else
37
+ R = Polynomial.over(Integer, *"abcdefghi".scan(/./))
38
+ a, b, c, d, e, f, g, h, i = R.vars
39
+ end
40
+ Rx = Polynomial(R, "x")
41
+ x = Rx.var
42
+
43
+ M3 = SquareMatrix(Rx, 3)
44
+ m = M3[
45
+ [a, b, c],
46
+ [d, e, f],
47
+ [g, h, i]
48
+ ]
49
+ else
50
+ unless $mul
51
+ if true
52
+ R = MPolynomial(Integer)
53
+ MR = SquareMatrix(R, n)
54
+ m = MR.matrix{|i, j| R.var("x#{i}#{j}") }
55
+ Rx = Polynomial(R, "x")
56
+ ch = m.char_polynomial(Rx)
57
+ p ch.evaluate(m)
58
+ exit
59
+ else
60
+ R = MPolynomial(Integer)
61
+ Rx = Polynomial(R, "x")
62
+ M = SquareMatrix(Rx, n)
63
+ m = M.matrix{|i, j| R.var("x#{i}#{j}") }
64
+ x = Rx.var
65
+ end
66
+ else
67
+ puts "not implemented."
68
+ exit
69
+ end
70
+ end
71
+
72
+ p m
73
+ p (x - m)
74
+ det = (x - m).determinant
75
+ p det
76
+ p det.evaluate(m)