class Algebra::MatrixAlgebra

Constants

Matrices

require 'algebra/linear-algebra' require 'algebra/matrix-algebra-triplet' require 'algebra/elementary-divisor' require 'algebra/jordan-form'

Public Class Methods

*(otype) click to toggle source

matrix type conversion

# File lib/algebra/matrix-algebra.rb, line 114
def self.*(otype)
  if csize != otype.rsize
    raise "type error (#{self} * #{otype}, #{csize}:#{otype.rsize})"
  end

  if ground.respond_to?(:wedge)
    g = ground.wedge otype.ground
  elsif ground <= Numeric
    g = otype.ground
  else
    raise "*: unkown type conversion (#{ground}) * (#{oground})"
  end

  if otype <= Vector
    Algebra::Vector.create(g, rsize)
  elsif self <= Covector
    Algebra::Covector.create(g, csize)
  elsif rsize == otype.csize
    SquareMatrix.create(g, rsize)
  else
    MatrixAlgebra.create(g, rsize, otype.csize)
  end
end
**(m) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 555
def self.**(m)
  m.convert_to(self)
end
[](*a) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 104
def self.[](*a)
   new(a, true)
end
collect_column(n = csize) { |j| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 434
def self.collect_column(n = csize)
  columns = (0...n).collect{|j| yield j}
  matrix{|i, j| columns[j][i]}
end
collect_ij(m = rsize, n = csize) { |i, j| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 392
def self.collect_ij(m = rsize, n = csize)
  (0...m).collect do |i|
    (0...n).collect do |j|
      yield(i, j)
    end
  end
end
collect_row(m = rsize) { |i| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 426
def self.collect_row(m = rsize)
  new((0...m).collect{|i| yield i})
end
convert_from(m) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 551
def self.convert_from(m)
  matrix{|i, j| m[i, j].convert_to(ground)}
end
create(ground, rsize, csize) click to toggle source
Calls superclass method
# File lib/algebra/matrix-algebra.rb, line 234
def self.create(ground, rsize, csize)
  if klass = matrices[[ground, rsize, csize]] #flyweight
    klass
  else
    klass = super(ground)
    klass.sysvar(:sizes, [rsize, csize])
    klass.sysvar(:rsize, rsize)
    klass.sysvar(:csize, csize)
    matrices[[ground, rsize, csize]] = klass
    klass
  end
end
dsum(otype) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 156
def self.dsum(otype)
  #unless ground == otype.ground
  #  raise "type error #{ground}.dsum #{otype.ground}"
  #end
  if self <= SquareMatrix and otype <= SquareMatrix
    superclass.create(ground, size + otype.size)
  else
    superclass.create(ground, rsize + otype.rsize, csize + otype.csize)
  end
end
dsum_c(otype) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 169
def self.dsum_c(otype); raise "not implemented"; end
dsum_r(otype) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 167
def self.dsum_r(otype); raise "not implemented"; end
each_ij()
Alias for: each_index
each_index() { |i, j| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 340
def self.each_index
  (0...rsize).each do |i|
    (0...csize).each do |j|
      yield i, j
    end
  end
end
Also aliased as: each_ij
matrices() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 102
def self.matrices; Matrices; end
matrix(m = rsize, n = csize) { |i, j| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 402
def self.matrix(m = rsize, n = csize)
  new(collect_ij(m, n) {|i, j| yield i, j})
end
minor() click to toggle source

def self.covector_type

Algebra::Covector.create(ground, csize)

end

# File lib/algebra/matrix-algebra.rb, line 146
def self.minor
  if self <= SquareMatrix
    superclass.create(ground, size-1)
  elsif self <= MatrixAlgebra
    superclass.create(ground, rsize-1, csize-1)
  else
    raise "minor: unknown type #{self.class}"
  end
end
new(array, conv = false) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 69
def initialize(array, conv = false)
  raise "rsize error (#{array.size} for #{rsize})" if array.size != rsize
  array.each do |v|
    raise "csize error (#{v.size} for #{csize})" if v.size != csize
  end
  if conv
    @bone = array.collect{|cv| cv.collect{|x|
        ground.regulate(x) or
          raise "initialize: unknown type #{x.class} (#{x})"
      }}
  else
    @bone = array
  end
end
regulate(x) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 319
def self.regulate(x)
  case x
  when Vector, Covector, MatrixAlgebra #,SquareMatrix
    x
  else
    nil
  end
end
transpose() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 247
def self.transpose
  if sizes == [csize, rsize]
    self
  else
    superclass.create(ground, csize, rsize)
  end
end
zero() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 314
  def self.zero
#    new((0...rsize).collect{(0...csize).collect{ground.zero}})
    matrix{ground.zero}
  end

Public Instance Methods

*(other) click to toggle source
Calls superclass method Algebra::AlgebraBase#*
# File lib/algebra/matrix-algebra.rb, line 473
def *(other)
  #without regulation!
  if other.is_a?(ground) || other.is_a?(Numeric)#1.is_a?(Rational) is false
    matrix{|i, j| self[i, j]*other}
  elsif self.is_a?(other.ground)
    other.matrix{|i, j| self*other[i, j]}
  else
    super{|o|
      msize = csize
      raise "type error" unless msize == o.rsize
      (self.class * o.class).matrix(rsize, o.csize){|i, j|
        (0...msize).sum(ground.zero){|k| self[i, k] * o[k, j]}
      }
    }
  end
end
+(other) click to toggle source
Calls superclass method Algebra::AlgebraBase#+
# File lib/algebra/matrix-algebra.rb, line 443
def +(other)
  super{ |o|
    raise "type error" unless sizes == o.sizes
    matrix{|i, j| self[i, j] + o[i, j]}
  }
end
-(other) click to toggle source
Calls superclass method Algebra::AlgebraBase#-
# File lib/algebra/matrix-algebra.rb, line 450
def -(other)
  super{ |o|
    raise "type error" unless sizes == o.sizes
    matrix{|i, j| self[i, j] - o[i, j]}
  }
end
/(other) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 490
def /(other)
  case other
  when ground, Numeric
    matrix{|i, j| self[i, j] / other}
  else
    raise "(/): unknown type #{other.class}"
  end
end
==(other) click to toggle source
Calls superclass method Algebra::AlgebraBase#==
# File lib/algebra/matrix-algebra.rb, line 382
def ==(other)
  super{ |o|
    rais "type error" unless sizes == o.sizes
    each_index do |i, j|
      return false unless self[i, j] == o[i, j]
    end
    true
  }
end
[](i, j) click to toggle source

def [](i, j = nil)

j ? @bone[i][j] : @bone[i]

end

# File lib/algebra/matrix-algebra.rb, line 178
def [](i, j)
  @bone[i][j]
end
[]=(i, j, x) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 182
def []=(i, j, x)
  @bone[i][j] = x
end
adjoint()
Alias for: cofactor_matrix
coerce(other) click to toggle source
Calls superclass method Algebra::AlgebraBase#coerce
# File lib/algebra/matrix-algebra.rb, line 328
def coerce(other)#MatrixAlgebra
  if x = ground.regulate(other)
    [Algebra::Scalar.new(x), self]
  else
    super
  end
end
cofactor(i, j) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 416
def cofactor(i, j)
  minor(i, j).determinant * (-1)**(i+j)
end
cofactor_matrix() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 420
def cofactor_matrix
  self.transpose.matrix{|i, j| cofactor(j, i)}
end
Also aliased as: adjoint
collect_column(n = csize) { |j| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 439
def collect_column(n = csize)
  self.class.collect_columns(n){|j| yield j}
end
collect_ij(*x, &b) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 400
def collect_ij(*x, &b); self.class.collect_ij(*x, &b); end
collect_row(m = rsize) { |i| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 430
def collect_row(m = rsize)
  self.class.collect_row(m){|i| yield i}
end
column(j) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 217
def column(j)
  raise "size error" unless 0 <= j && j < csize
  (0...rsize).collect{|i| @bone[i][j]}
end
columns() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 207
def columns
  (0...csize).collect{|j| column(j)}
end
convert_to(mat_alg) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 547
def convert_to(mat_alg)
  mat_alg.matrix{|i, j| mat_alg.ground.regulate(self[i, j])}
end
csize() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 232
def csize; self.class.csize; end
diag() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 537
def diag
  ed = []
  size = rsize < csize ? rsize : csize
  (0...size).each do |i|
    break if self[i, i].zero?
    ed.push self[i, i]
    end
  ed
end
display(out = $stdout) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 259
  def display(out = $stdout)
    @bone.each do |col|
#      out << col.inspect << "\n"
      first = true
      col.each do |x|
        if first
          first = false
        else
          out << ", "
        end
        out << sprintf("%3s", x)
      end
      out << "\n"
    end
    out
  end
display_by_latex(out = $stdout) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 276
  def display_by_latex(out = $stdout)
#    out << "\\left(\n"
#    out "\\begin{array}{" + "c" * @bone[0].size + "}\n"
    @bone.each do |col|
#      out << col.inspect << "\n"
      first = true
      col.each do |x|
        if first
          first = false
        else
          out << " & "
        end
        out << sprintf("%3s", x)
      end
      out << "\\\\\n"
    end
#    out << "\\end{array}"
#    out << "\\right)"
    out
  end
dsum(other) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 509
def dsum(other)
  mytype = self.class.dsum(other.class)
  mytype.matrix{|i, j|
    if i < rsize
      if j < csize
        self[i, j]
      else
        other.ground.zero
      end
    else
      if j < csize
        ground.zero
      else
        other[i - rsize, j - csize]
      end
    end
  }
end
dup() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 84
  def dup
#    matrix{|x| self[* x.collect{|y| y.dup}]}
#    matrix{|x| self[* x.collect{|y| y}]} #in 1.8.0, Integer can't be dup..
    matrix{|i, j| self[i, j]} #in 1.8.0, Integer can't be dup..
  end
e_deg() click to toggle source
# File lib/algebra/elementary-divisor.rb, line 28
def e_deg
  max = -1
  each_ij do |i, j|
    if !self[i, j].zero? && (d = self[i, j].deg) > max
      max = d
    end
  end
  max
end
each(&b) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 336
def each(&b)
  @bone.each(&b)
end
each_column() { |column(i)| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 376
def each_column
  (0...rsize).each do |i|
    yield column(i)
  end
end
each_i() { |i| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 358
def each_i
  (0...rsize).each do |i|
    yield i
  end
end
each_ij(&b)
Alias for: each_index
each_index(&b) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 352
def each_index(&b)
  self.class.each_index(&b)
end
Also aliased as: each_ij
each_j() { |j| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 364
def each_j
  (0...csize).each do |j|
    yield j
  end
end
each_row() { |row(i)| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 370
def each_row
  (0...rsize).each do |i|
    yield row(i)
  end
end
flatten() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 532
def flatten
  #'to_a' is defined in Enumerable
  to_a.flatten
end
i2o() click to toggle source
# File lib/algebra/elementary-divisor.rb, line 11
def i2o
  mground = self.class.ground.ground
  r = if self.class <= Algebra::SquareMatrix
        Algebra.SquareMatrix(mground, size)
      else
        Algebra.MatrixAlgebra(mground, rsize, csize)
      end

  rx = Algebra.Polynomial(r, 'x')
  x = rx.var
  s = 0
  (0..e_deg).each do |n|
    s += x**n * r.matrix { |i, j| self[i, j][n] }
  end
  s
end
inspect() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 305
def inspect
  "#{self.class}#{to_s}"
end
jordan_form() click to toggle source
# File lib/algebra/jordan-form.rb, line 10
def jordan_form
  Algebra::JordanForm.decompose(self).first
end
jordan_form_info() click to toggle source
# File lib/algebra/jordan-form.rb, line 14
def jordan_form_info
  Algebra::JordanForm.decompose(self)
end
latex(env = "pmatrix") click to toggle source
# File lib/algebra/matrix-algebra.rb, line 297
def latex(env = "pmatrix")
  s = ""
  s << "\\begin{#{env}}\n" if env
  s << display_by_latex("")
  s << "\\end{#{env}}\n" if env
  s
end
map() { |self| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 499
def map
  matrix{|i, j| yield(self[i, j])}
end
matrix(m = rsize, n = csize) { |i, j| ... } click to toggle source
# File lib/algebra/matrix-algebra.rb, line 406
def matrix(m = rsize, n = csize)
  self.class.matrix(m, n){|i, j| yield i, j}
end
minor(i, j) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 410
def minor(i, j)
  self.class.minor.matrix{|a, b|
    self[a < i ? a : a+1, b < j ? b : b+1]
  }
end
old_mul(other) click to toggle source
Calls superclass method
# File lib/algebra/matrix-algebra.rb, line 457
def old_mul(other)
  #without regulation
  case other
  when ground, Numeric # 1.is_a?(Rational) is false
    matrix{|i, j| self[i, j]*other}
  else
    super{|o|
      msize = csize
      raise "type error" unless msize == o.rsize
      (self.class * o.class).matrix(rsize, o.csize){|i, j|
        (0...msize).sum(ground.zero){|k| self[i, k] * o[k, j]}
      }
    }
  end
end
orthogonal_basis() click to toggle source
# File lib/algebra/gaussian-elimination.rb, line 224
def orthogonal_basis
  transpose.kernel_basis
end
rank() click to toggle source
# File lib/algebra/gaussian-elimination.rb, line 219
def rank
  n, k, pi = dup.left_eliminate!
  pi
end
replace(m) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 90
def replace(m)
  if m.is_a?(self.class) and sizes == m.sizes
    each_index do |i, j|
      @bone[i][j] = m[i, j]
    end
  else
    raise "Type Error #{m.class}"
  end
end
row(i) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 190
def row(i)
  raise "size error" unless 0 <= i && i < rsize
  @bone[i].clone
end
row!(i) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 195
def row!(i) # column!(j) is not defined!
  raise "size error" unless 0 <= i && i < rsize
  @bone[i]
end
rows() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 186
def rows
  (0...rsize).collect{|i| row(i)}
end
rsize() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 231
def rsize; self.class.rsize; end
set_column(j, array) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 222
def set_column(j, array)
  raise "size error" unless 0 <= j && j < csize
  raise "size error" unless rsize == array.size
  (0...rsize).each{|i| @bone[i][j] = array[i]}
  self
end
set_row(i, array) click to toggle source
# File lib/algebra/matrix-algebra.rb, line 200
def set_row(i, array)
  raise "size error" unless 0 <= i && i < rsize
  raise "size error" unless csize == array.size
  @bone[i] = array
  self
end
simplify() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 100
def simplify; matrix{|i, j| self[i, j].simplify}; end
sizes() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 230
def sizes; self.class.sizes; end
t()
Alias for: transpose
to_ary() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 528
def to_ary
  to_a
end
to_quint() click to toggle source
# File lib/algebra/matrix-algebra-triplet.rb, line 16
def to_quint
  Algebra::MatrixAlgebraQuint.new(self)
end
to_s() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 255
def to_s
  @bone.inspect
end
to_triplet() click to toggle source
# File lib/algebra/matrix-algebra-triplet.rb, line 12
def to_triplet
  Algebra::MatrixAlgebraTriplet.new(self)
end
transpose() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 503
def transpose
  self.class.transpose.matrix(csize, rsize){|i, j| self[j, i]}
end
Also aliased as: t
types() click to toggle source

check entries

# File lib/algebra/matrix-algebra.rb, line 310
def types
  matrix{|i, j| self[i, j].class}
end
vectors() click to toggle source
# File lib/algebra/matrix-algebra.rb, line 211
  def vectors
#    vc = self.class.vector_type
    vc = Algebra::Vector.create(ground, rsize)
    columns.collect{|v| vc[*v]}
  end