Exercise

Calculation of polynomials

--------------------^ sample-polynomial01.rb
require "polynomial"
P = Polynomial.create(Integer, "x")
x = P.var
p((x + 1)**100) #=> x^100 + 100x^99 + ... + 100x + 1
--------------------$ sample-polynomial01.rb

Calculation of multi-variate polynomials

--------------------^ sample-polynomial02.rb
require "polynomial"
P = Polynomial.create(Integer, "x", "y", "z")
x, y, z = P.vars
p((-x + y + z)*(x + y - z)*(x - y + z))
#=> -z^3 + (y + x)z^2 + (y^2 - 2xy + x^2)z - y^3 + xy^2 + x^2y - x^3
--------------------$ sample-polynomial02.rb

Calculation of multi-variate polynomials (2)

--------------------^ sample-m-polynomial01.rb
require "m-polynomial"
P = MPolynomial(Integer)
x, y, z, w = P.vars("xyz")
p((-x + y + z)*(x + y - z)*(x - y + z))
#=> -x^3 + x^2y + x^2z + xy^2 - 2xyz + xz^2 - y^3 + y^2z + yz^2 - z^3
--------------------$ sample-m-polynomial01.rb

Remainder of the division of a polynomial by polynomials

--------------------^ sample-divmod01.rb
require "m-polynomial"
require "rational"
P = MPolynomial(Rational)
x, y, z = P.vars("xyz")
f = x**2*y + x*y**2 + y*2 + z**3
g = x*y-z**3
h = y*2-6*z

MPolynomial.set_ord(:lex) # lex, grlex, grevlex
puts "(#{f}).divmod([#{g}, #{h}]) =>", "#{f.divmod(g, h).inspect}"
#=> (x^2y + xy^2 + 2y + z^3).divmod([xy - z^3, 2y - 6z]) =>
#   [[x + y, 1/2z^3 + 1], xz^3 + 3z^4 + z^3 + 6z]
#   = [[Quotient1,Quotient2], Remainder]
--------------------$ sample-divmod01.rb

Groebner basis

--------------------^ sample-groebner01.rb
require "groebner-basis"
require "rational"
P = MPolynomial(Rational, "xyz")
x, y, z = P.vars("xyz")
f1 = x**2 + y**2 + z**2 -1
f2 = x**2 + z**2 - y
f3 = x - z
p Groebner.basis([f1, f2, f3])
#=> [x - z, y - 2z^2, z^4 + 1/2z^2 - 1/4]
--------------------$ sample-groebner01.rb

Prime field

--------------------^ sample-primefield01.rb
require "residue-class-ring"
Z13 = ResidueClassRing(Integer, 13)

a, b, c, d, e, f, g = Z13
p [e + c, e - c, e * c, e * 2001, 3 + c, 1/c, 1/c * c, d / d, b * 1 / b]
  #=> [6, 2, 8, 9, 5, 7, 1, 1, 1]
p( (1...13).collect{|i|  Z13[i]**12} )
  #=> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
--------------------$ sample-primefield01.rb

Algebraic field

--------------------^ sample-algebraicfield01.rb
require "residue-class-ring"
require "polynomial"
require "rational"

Px = Polynomial(Rational, "x")
x = Px.var
F = ResidueClassRing(Px, x**2 + x + 1)
x = F[x]
p( (x + 1)**100 )
    #=> -x - 1
p( (x-1)** 3 / (x**2 - 1) )
    #=> -3x - 3

G = Polynomial(F, "y")
y = G.var
p( (x + y + 1)** 7 )
    #=> y^7 + (7x + 7)y^6 + 8xy^5 + 4y^4 + (4x + 4)y^3 + 5xy^2 + 7y + x + 1

H = ResidueClassRing(G, y**5 + x*y + 1)
y = H[y]
p( 1/(x + y + 1)**7 )
  #=> (1798/3x + 1825/9)y^4 + (-74x + 5176/9)y^3 + 
  #     (-6886/9x - 5917/9)y^2 + (1826/3x - 3101/9)y + 2146/9x + 4702/9
--------------------$ sample-algebraicfield01.rb

This can be done as following

--------------------^ sample-algebraicfield02.rb
require "residue-class-ring"
require "polynomial"
require "rational"

F = AlgebraicExtentionField(Rational, "x") {|x| x**2 + x + 1}
x = F.var
p( (x + 1)**100 )
p( (x-1)** 3 / (x**2 - 1) )

H = AlgebraicExtentionField(F, "y") {|y| y**5 + x*y + 1}
y = H.var
p( 1/(x + y + 1)**7 )
--------------------$ sample-algebraicfield02.rb

Quotient fields

By taking the quotient field of the ring of Integer, rational numbers are obtained.

--------------------^ sample-quotientfield01.rb
require "localized-ring"
require "rational"
Q = LocalizedRing(Integer)
a = Q.new(3, 5)
b = Q.new(5, 3)
p [a + b, a - b, a * b, a / b, a + 3, 1 + a]
  #=> [34/15, -16/15, 15/15, 9/25, 18/5, 8/5]
--------------------$ sample-quotientfield01.rb

Rational function field

--------------------^ sample-quotientfield02.rb
require "localized-ring"
require "polynomial"
require "residue-class-ring"

F13 = ResidueClassRing(Integer, 13)

P = Polynomial(F13, "x")
Q = LocalizedRing(P)
x = Q[P.var]
p ( 1 / (x**2 - 1) - 1 / (x**3 - 1) )

#This is equivalent to the following
F = RationalFunctionField(F13, "x")
x = F.var
p ( 1 / (x**2 - 1) - 1 / (x**3 - 1) )
--------------------$ sample-quotientfield02.rb

Rational function field over the algebraic extension field

--------------------^ sample-quotientfield03.rb
require "localized-ring"
require "polynomial"
require "residue-class-ring"

F13 = ResidueClassRing(Integer, 13)
F = AlgebraicExtentionField(F13, "a") {|a| a**2 - 2}
a = F.var
RF = RationalFunctionField(F, "x")
x = RF.var

p( (a/4*x + RF.unity/2)/(x**2 + a*x + 1) +
  (-a/4*x + RF.unity/2)/(x**2 - a*x + 1) )
#=> 1/(x**4 + 1)
--------------------$ sample-quotientfield03.rb

Algebraic function field

--------------------^ sample-quotientfield04.rb
require "localized-ring"
require "polynomial"
require "residue-class-ring"

F13 = ResidueClassRing(Integer, 13)
F = RationalFunctionField(F13, "x")
x = F.var
AF = AlgebraicExtentionField(F, "a") {|a| a**2 - 2*x}
a = AF.var

p( (a/4*x + AF.unity/2)/(x**2 + a*x + 1) +
  (-a/4*x + AF.unity/2)/(x**2 - a*x + 1) )
#=> (-x^3 + x^2 + 1)/(x^4 + 11x^3 + 2x^2 + 1)
--------------------$ sample-quotientfield04.rb

Linear Algebra

Linear equations

--------------------^ sample-gaussian-elimination01.rb
require "matrix-algebra"
require "mathn"
M = MatrixAlgebra(Rational, 5, 4)
a = M.matrix{|i, j| i + j}
a.display #=>
  #[0, 1, 2, 3]
  #[1, 2, 3, 4]
  #[2, 3, 4, 5]
  #[3, 4, 5, 6]
  #[4, 5, 6, 7]
a.kernel_basis.each do |v|
  puts "a * #{v} = #{a * v}"
  #=> a * [1, -2, 1, 0] = [0, 0, 0, 0, 0]
  #=> a * [2, -3, 0, 1] = [0, 0, 0, 0, 0]
end
--------------------$ sample-gaussian-elimination01.rb

Diagonalization of Square Matrix

--------------------^ sample-diagonalization01.rb
require "linear-algebra"
require "rational"
class Rational < Numeric
  def inspect; to_s; end
end

M = SquareMatrix(Rational, 3)
a = M[[1,-1,-1], [-1,1,-1], [2,1,-1]]
puts "A = "; a.display; puts
#A =
#  1,  -1,  -1
# -1,   1,  -1
#  2,   1,  -1

extfield, roots, tmatrix, evalues, evectors, espaces,
      chpoly, facts = a.diagonalize

puts "Charactoristic Poly.: #{chpoly} => #{facts}"
#Charactoristic Poly.: t^3 - t^2 + t - 6 => (t - 2)(t^2 + t + 3)

puts "Algebraic Numbers:"
roots.each do |po, rs|
  puts "#{rs.join(', ')} : roots of #{po} == 0"
end
puts
#Algebraic Numbers:
#a, -a - 1 : roots of t^2 + t + 3 == 0

puts "EigenSpaces: "
evalues.uniq.each do |ev|
  puts "W_{#{ev}} = <#{espaces[ev].join(', ')}>"
end
puts
#EigenSpaces:
#W_{2} = <[4, -5, 1]>
#W_{a} = <[1/3a + 1/3, 1/3a + 1/3, 1]>
#W_{-a - 1} = <[-1/3a, -1/3a, 1]>

puts "P = "; tmatrix.display; puts
puts "P^-1 * A * P = "; (tmatrix.inverse * a * tmatrix).display; puts
#P =
#  4, 1/3a + 1/3, -1/3a
# -5, 1/3a + 1/3, -1/3a
#  1,   1,   1
#
#P^-1 * A * P =
#  2,   0,   0
#  0,   a,   0
#  0,   0, -a - 1
--------------------$ sample-diagonalization01.rb

The proofs of the theorem of Cayley-Hamilton

--------------------^ sample-cayleyhamilton01.rb
require "matrix-algebra"
require "m-polynomial"
require "polynomial"

n = 4
R = MPolynomial(Integer)
MR = SquareMatrix(R, n)
m = MR.matrix{|i, j| R.var("x#{i}#{j}") }
Rx = Polynomial(R, "x")
ch = m.char_polynomial(Rx)
p ch.evaluate(m) #=> 0
--------------------$ sample-cayleyhamilton01.rb

The expression of Groebner basis by original generators

--------------------^ sample-groebner02.rb
require "groebner-basis"
require "rational"

P = MPolynomial(Rational)
x, y, z = P.vars "xyz"
f1 = x**2 + y**2 + z**2 -1
f2 = x**2 + z**2 - y
f3 = x - z

coeff, basis = Groebner.basis_coeff([f1, f2, f3])
basis.each_with_index do |b, i|
  p [coeff[i].inner_product([f1, f2, f3]), b]
  p coeff[i].inner_product([f1, f2, f3]) == b #=> true
end
--------------------$ sample-groebner02.rb

The quotients and the remainder by arbitrary basis

--------------------^ sample-groebner03.rb
require "groebner-basis"
require "residue-class-ring"
F5 = ResidueClassRing(Integer, 2)
F = AlgebraicExtentionField(F5, "a") {|a| a**3 + a + 1}
a = F.var
P = MPolynomial(F)

x, y, z = P.vars("xyz")
f1 = x + y**2 + z**2 - 1
f2 = x**2 + z**2 - y * a
f3 = x - z - a

f = x**3 + y**3 + z**3
q, r = f.divmod_s(f1, f2, f3)
p f == q.inner_product([f1, f2, f3]) + r #=> true
--------------------$ sample-groebner03.rb

Factorization

Factorization of Integer coefficient polynomial

--------------------^ sample-factorize01.rb
require "polynomial"
require "polynomial-factor"

P = Polynomial(Integer, "x")
x = P.var
f = 8*x**7 - 20*x**6 + 6*x**5 - 11*x**4 + 44*x**3 - 9*x**2 - 27
p f.factorize #=> (2x - 3)^3(x^2 + x + 1)^2
--------------------$ sample-factorize01.rb

Factorization of Zp coefficient polynomial

--------------------^ sample-factorize02.rb
require "polynomial"
require "polynomial-factor"
require "residue-class-ring"

Z7 = ResidueClassRing(Integer, 7)
P = Polynomial(Z7, "x")
x = P.var
f = 8*x**7 - 20*x**6 + 6*x**5 - 11*x**4 + 44*x**3 - 9*x**2 - 27
p f.factorize #=> (x + 5)^2(x + 3)^2(x + 2)^3

--------------------$ sample-factorize02.rb

Factorization of the algebraic extension of Rational polynomial

--------------------^ sample-factorize03.rb
require "polynomial"
require "polynomial-factor"
require "residue-class-ring"
require "rational"

A = AlgebraicExtentionField(Rational, "a") {|a| a**2 + a + 1}
a = A.var
P = Polynomial(A, "x")
x = P.var
f = x**4 + (2*a + 1)*x**3 + 3*a*x**2 + (-3*a - 5)*x - a + 1
p f.factorize #=> (x + a)^3(x - a + 1)

--------------------$ sample-factorize03.rb

Factorization of the algebraic extension of the algebraic extension of Rational

--------------------^ sample-factorize04.rb
require "polynomial"
require "polynomial-factor"
require "residue-class-ring"
require "rational"

A = AlgebraicExtentionField(Rational, "a") {|a| a**2 - 2}
B = AlgebraicExtentionField(A, "b"){|b| b**2 + 1}
P = Polynomial(B, "x")
x = P.var
f = x**4 + 1
p f.factorize
#=> (x - 1/2ab - 1/2a)(x + 1/2ab - 1/2a)(x + 1/2ab + 1/2a)(x - 1/2ab + 1/2a)
--------------------$ sample-factorize04.rb