线性代数学习三,将矩阵变为行最简形,标准形以及计算矩阵的秩

  1. #!/usr/bin/env lua
  2.  
  3. function Matrix(data,row,column)
  4.         local size=table.maxn(data)
  5.         assert(size==row*column)
  6.  
  7.         local matrix={}
  8.         matrix.data=data
  9.         matrix.row=row
  10.         matrix.column=column
  11.  
  12.         matrix.at=function(this,row,column)
  13.                 return this.data[(row-1)*this.column+column]
  14.         end
  15.  
  16.         matrix.set=function(this,row,column,num)
  17.                 this.data[(row-1)*this.column+column]=num
  18.         end
  19.  
  20.         matrix.swaprow=function(this,r1,r2)
  21.                 local temp
  22.                 for i=1,this.column do
  23.                         temp=this:at(r1,i)
  24.                         this:set(r1,i,this:at(r2,i))
  25.                         this:set(r2,i,temp)
  26.                 end
  27.         end
  28.  
  29.         matrix.simplifycolumn=function(this,column,layer)
  30.                 if this:at(layer,column) == 0 then
  31.                         for i=layer+1,this.row do
  32.                                 if this:at(i,column) ~= 0 then
  33.                                         this:swaprow(i,layer)
  34.                                         break
  35.                                 end
  36.                         end
  37.                 end
  38.  
  39.                 if this:at(layer,column) == 0 then
  40.                         return layer
  41.                 end
  42.  
  43.                 for i=1,this.row do
  44.                         if i~= layer then
  45.                                 local tomultiply=-this:at(i,column)/this:at(layer,column)
  46.                                 for j=1,this.column do
  47.                                         this:set(i,j,this:at(layer,j)*tomultiply+this:at(i,j))
  48.                                 end
  49.                         end
  50.                 end
  51.  
  52.                 return layer+1
  53.         end
  54.  
  55.         matrix.swapcolumn=function(this,c1,c2)
  56.                 local temp
  57.                 for i=1,this.row do
  58.                         temp=this:at(i,c1)
  59.                         this:set(i,c1,this:at(i,c2))
  60.                         this:set(i,c2,temp)
  61.                 end
  62.         end
  63.  
  64.         matrix.simplifyrow=function(this,row,layer)
  65.                 if this:at(row,layer) == 0 then
  66.                         for i=layer+1,this.column do
  67.                                 if this:at(row,i) ~= 0 then
  68.                                         this:swapcolumn(i,layer)
  69.                                         break
  70.                                 end
  71.                         end
  72.                 end
  73.                
  74.                 if this:at(row,layer) == 0 then
  75.                         return layer
  76.                 end
  77.  
  78.                 for i=1,this.column do
  79.                         if i ~= layer then
  80.                                 local tomultiply=-this:at(row,i)/this:at(row,layer)
  81.                                 for j=1,this.row do
  82.                                         this:set(j,i,this:at(j,layer)*tomultiply+this:at(j,i))
  83.                                 end
  84.                         end
  85.                 end
  86.  
  87.                 return layer+1
  88.         end
  89.  
  90.         -- Make the matrix to reduced form
  91.         matrix.reduce=function(this)
  92.                 local layer=1
  93.                 for i=1,this.column do
  94.                         layer=this:simplifycolumn(i,layer)
  95.                         if layer>this.row then
  96.                                 break
  97.                         end
  98.                 end
  99.                
  100.                
  101.                 for i=1,this.row do
  102.                         local tomultiply=nil
  103.                         for j=1,this.column do
  104.                                 if tomultiply then
  105.                                         this:set(i,j,this:at(i,j)*tomultiply)
  106.                                 elseif this:at(i,j)~= 0 then
  107.                                         tomultiply=1/this:at(i,j)
  108.                                         this:set(i,j,1)
  109.                                 end
  110.                         end
  111.                 end     
  112.         end
  113.  
  114.         -- Make the matrix to canonical form
  115.         matrix.canonicalize=function(this)
  116.                 this:reduce()
  117.                 local layer=1
  118.                 for i=1,this.row do
  119.                         layer=this:simplifyrow(i,layer)
  120.                         if layer>this.column then
  121.                                 break
  122.                         end
  123.                 end     
  124.         end
  125.  
  126.         -- Get the rank of the matrix
  127.         -- Matrix should be reduced before.
  128.         matrix.rank=function(this)
  129.                 local rank=0
  130.                 for i=1,this.row do
  131.                         for j=1,this.column do
  132.                                 if this:at(i,j) ~= 0 then
  133.                                         rank=rank+1
  134.                                         break
  135.                                 end
  136.                         end
  137.                 end
  138.  
  139.                 return rank
  140.         end
  141.  
  142.         matrix.print=function(this)
  143.                 for i=1,this.row do
  144.                         for j=1,this.column do
  145.                                 io.write(this:at(i,j),'\t')
  146.                         end
  147.                         io.write('\n')
  148.                 end
  149.         end
  150.  
  151.         return matrix
  152. end

Algorithm Comments(0) 2008年8月23日 05:00