class Matrix

Attributes

msize[RW]
nsize[RW]

Public Class Methods

I(n)
Alias for: identity
cols(cols) click to toggle source
# File lib/m500.rb, line 3586
def Matrix.cols(cols)
  a = instanciate(cols.length,cols.at(0).length)
  cols.each_index{|x|
    cols.at(x).each_index{|y|
      eval("a.at_#{x+1}_#{y+1}= cols.at(#{y}).at(#{x})")
    }
  }
  return a
end
csv(a) click to toggle source
# File lib/m500.rb, line 3573
def Matrix.csv(a)
  t =  "[[" << a.strip! << "]]"
  return Matrix.rows(t.gsub(/\s+/, '],['))
end
diagonal(v) click to toggle source
# File lib/m500.rb, line 3595
def Matrix.diagonal(v)
  a = Matrix(v.length,v.length)
  v.each_index{|i|
    eval("a.at_#{i+1}_#{i+1} = v.at(i)")
  }
  return a
end
identity(n) click to toggle source
# File lib/m500.rb, line 3605
def Matrix.identity(n)
  Matrix.scalar(n, 1)
end
Also aliased as: unit, I
instanciate(n,m) click to toggle source
# File lib/m500.rb, line 3570
def Matrix.instanciate(n,m)
  new(n,m)
end
new(a,b) click to toggle source
# File lib/m500.rb, line 3615
  def initialize(a,b)
    (1..a).to_a.each{|n|
      (1..b).to_a.each{|m|
        z = " @at_#{n}_#{m} = 0
      def at_#{n}_#{m}
      @at_#{n}_#{m}
      end
      def tr_#{m}_#{n}
      @at_#{n}_#{m}
      end
      def at_#{n}_#{m}=(a)
      @at_#{n}_#{m} = a
      end
      "
    instance_eval(z)
}
}
    @nsize = a
    @msize = b
    @tr = false
  end
rows(rows) click to toggle source
# File lib/m500.rb, line 3577
def Matrix.rows(rows)
  a = instanciate(rows.length,rows.at(0).length)
  rows.each_index{|x|
    rows.at(x).each_index{|y|
      eval("a.at_#{x+1}_#{y+1}= rows.at(#{x}).at(#{y})")
    }
  }
  return a
end
scalar(n,v) click to toggle source
# File lib/m500.rb, line 3602
def Matrix.scalar(n,v)
  Matrix.diagonal(Array.new(n).fill(v, 0, n))
end
unit(n)
Alias for: identity
zero(n) click to toggle source
# File lib/m500.rb, line 3612
def Matrix.zero(n)
  Matrix.scalar(n, 0)
end

Public Instance Methods

*(nm) click to toggle source
# File lib/m500.rb, line 3731
def *(nm)
   a = nil
   cmd = ""
   if nm.kind_of?(Matrix) then
     if  not(nm.is_tr? or @tr) then
       if nm.msize == @msize and nm.nsize == @nsize then
         a= Matrix(@nsize, @msize)
         (1..@nsize).to_a.each{|x|
           (1..@msize).to_a.each{|y|
             cmd += "a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm.at_#{x}_#{y};"
           }
         }
       else
       end
     elsif nm.is_tr? and not @tr then
       if nm.msize == @nsize and nm.nsize == @msize then
         a= Matrix(@nsize, @msize)
         (1..@nsize).to_a.each{|x|
           (1..@msize).to_a.each{|y|
             cmd += "a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm.tr_#{x}_#{y};"
           }
         }
       else
       end
     elsif not nm.is_tr? and @tr then
       if nm.msize == @nsize and nm.nsize == @msize then
         a= Matrix(@msize, @nsize)
         (1..@msize).to_a.each{|x|
           (1..@nsize).to_a.each{|y|
             cmd += "a.at_#{x}_#{y}= self.tr_#{x}_#{y} + nm.at_#{x}_#{y};"
           }
         }
       else
       end
     else
     end
   elsif nm.kind_of?(Numeric)
     a= Matrix(@msize, @nsize)
     (1..@msize).to_a.each{|x|
       (1..@nsize).to_a.each{|y|
         cmd += "a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm;"
       }
     }
   end
   eval(cmd)
   return a
 end
+(nm) click to toggle source
# File lib/m500.rb, line 3659
def +(nm)
  a = ""
  if  not(nm.is_tr? or @tr) then
    if nm.msize == @msize and nm.nsize == @nsize then
      a= Matrix(@nsize, @msize)
      (1..@nsize).to_a.each{|x|
        (1..@msize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} + nm.at_#{x}_#{y}")
        }
      }
    else
    end
  elsif nm.is_tr? and not @tr then
    if nm.msize == @nsize and nm.nsize == @msize then
      a= Matrix(@nsize, @msize)
      (1..@nsize).to_a.each{|x|
        (1..@msize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} + nm.tr_#{x}_#{y}")
        }
      }
    else
    end
  elsif not nm.is_tr? and @tr then
    if nm.msize == @nsize and nm.nsize == @msize then
      a= Matrix(@msize, @nsize)
      (1..@msize).to_a.each{|x|
        (1..@nsize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y} + nm.at_#{x}_#{y}")
        }
      }
    else
    end
  else
  end
  return a
end
-(nm) click to toggle source
# File lib/m500.rb, line 3695
def -(nm)
  a = ""
  if  not(nm.is_tr? or @tr) then
    if nm.msize == @msize and nm.nsize == @nsize then
      a= Matrix(@nsize, @msize)
      (1..@nsize).to_a.each{|x|
        (1..@msize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} - nm.at_#{x}_#{y}")
        }
      }
    else
    end
  elsif nm.is_tr? and not @tr then
    if nm.msize == @nsize and nm.nsize == @msize then
      a= Matrix(@nsize, @msize)
      (1..@nsize).to_a.each{|x|
        (1..@msize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} - nm.tr_#{x}_#{y}")
        }
      }
    else
    end
  elsif not nm.is_tr? and @tr then
    if nm.msize == @nsize and nm.nsize == @msize then
      a= Matrix(@msize, @nsize)
      (1..@msize).to_a.each{|x|
        (1..@nsize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y} - nm.at_#{x}_#{y}")
        }
      }
    else
    end
  else
  end
  return a
end
<=>(o) click to toggle source
# File lib/m500.rb, line 3778
def <=>(o)
  r = -1 if self.size < o.size
  r = 0 if self.size == o.size
  r = 1 if self.size > o.size
  r
end
==(o) click to toggle source
# File lib/m500.rb, line 3784
def ==(o)
  t = []
  if self.msize == o.msize and self.nsize == o.nsize then
    m = (1..self.msize)
    n = (1..self.nsize)
    for x in m
      for y in n
        self.send("at_#{x}_#{y}") == o.send("at_#{x}_#{y}") ? t << true : t << false
      end
    end
  end
  t.include?(false) ? false : true
end
column_vectors() click to toggle source
# File lib/m500.rb, line 3807
def column_vectors
  #each_m
end
each() { |send("at_#{x}_#{y}")| ... } click to toggle source
# File lib/m500.rb, line 3797
def each
  m = (1..self.msize)
  n = (1..self.nsize)
  for x in m
    for y in n
      #p  ".at_#{x}_#{y}"
      yield self.send("at_#{x}_#{y}")
    end
  end
end
each_m() { |r| ... } click to toggle source
# File lib/m500.rb, line 3814
def each_m
  m = (1..self.msize)
  n = (1..self.nsize)
  for x in m
    r = Matrix(1,self.nsize)
    for y in n
      r.send("at_1_#{y}=",self.send("at_#{x}_#{y}"))
    end
    yield r
  end  
end
each_n() { |r| ... } click to toggle source
# File lib/m500.rb, line 3825
def each_n
  m = (1..self.msize)
  n = (1..self.nsize)
  for x in n
    r = Matrix(self.msize,1)
    for y in m
      r.send("at_#{y}_1=",self.send("at_#{y}_#{x}"))
    end
    yield r
  end  
end
is_tr?() click to toggle source
# File lib/m500.rb, line 3646
def is_tr?
  @tr
end
og!() click to toggle source
# File lib/m500.rb, line 3653
def og!
  @tr =  false
end
row_vectors() click to toggle source
# File lib/m500.rb, line 3810
def row_vectors
  r = []
  # self.each_n{||}
end
size() click to toggle source
# File lib/m500.rb, line 3656
def size
  self.msize * self.nsize
end
to_csv() click to toggle source
# File lib/m500.rb, line 3849
def to_csv
  t = ""
  (1..@nsize).to_a.each{|a|
    (1..@msize).to_a.each{|b|
      t << eval("at_#{a}_#{b}").to_s
    unless b == @msize then  t << 44 end
    }
     t << 10
  }
  t
end
to_s() click to toggle source
# File lib/m500.rb, line 3836
def to_s
  t = ""
  (1..@nsize).to_a.each{|a|
    t << "|"
    (1..@msize).to_a.each{|b|
      t << eval("at_#{a}_#{b}").to_s
      unless b == @msize then t << 9 end
    }
    t << "|"
      unless a == @nsize then t << 10 end
  }
  t
end
to_sgml() click to toggle source
# File lib/m500.rb, line 3559
def to_sgml
  tmp =     "<mn #{sgml_id}class='matrix'><mrow><mo>(</mo><mtable>"
  (1..@nsize).to_a.each{|a|
    (1..@msize).to_a.each{|b|
      tmp += "<mn>" + eval("at_#{a}_#{b}").to_s + "</mn>"
    }
    tmp += "</mtr>"
  }
  tmp += "</mtable><mo>)</mo></mrow></mn>"
end
tr!() click to toggle source
# File lib/m500.rb, line 3649
def tr!
  unless @tr then @tr = true else @tr = false end
  return self
end
transpose() click to toggle source
# File lib/m500.rb, line 3637
def transpose
  a= Matrix(@msize, @nsize)
  (1..@msize).to_a.each{|x|
    (1..@nsize).to_a.each{|y|
      eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y}")
    }
  }
  return a
end