一个算24点的算法
线性代数学习二,用克拉默法则求解线性方程组

线性代数学习一,计算行列式

太阳神上 posted @ 2008年8月22日 09:15 in Algorithm with tags Determinant , 1646 阅读

复习线性代数,写了一个计算行列式的Lua脚本:

  1. #!/usr/bin/env lua
  2.  
  3. -- This is the constructor of a determinant
  4. -- It get an array of numbers as argument
  5. -- And add methods for it
  6. function Determinant(data)
  7.         local det={}
  8.         det.row=math.sqrt(table.maxn(data))
  9.  
  10.         -- If the number is not proper, raise error
  11.         if math.floor (det.row) ~= det.row then
  12.                 error("Determinant's data number must be n^2!")
  13.         end
  14.  
  15.         det.data=data
  16.        
  17.         -- index function
  18.         det.at=function(this,row,column)
  19.                 return this.data[(row-1)*this.row+column];
  20.         end
  21.        
  22.         det.cofactor=function(this,row,column)
  23.                 local cofactor={}
  24.                 local r=1
  25.                 local c=0
  26.                 for _,v in ipairs(this.data) do
  27.                         c=c+1;
  28.                        
  29.                         if c==this.row+1 then
  30.                                 r=r+1
  31.                                 c=1
  32.                         end
  33.  
  34.                         if not (r==row or c==column) then
  35.                                 table.insert(cofactor,v)
  36.                         end
  37.                 end
  38.                
  39.                 return cofactor
  40.         end     
  41.  
  42.         -- evaluation function
  43.         det.eval=function(this)
  44.                 local a=this.data
  45.                 -- this is the simplest case
  46.                 if this.row==2 then
  47.                         return a[1]*a[4]-a[2]*a[3]
  48.                
  49.                 -- for accerleration
  50.                 elseif this.row==3 then
  51.                         return a[1]*a[5]*a[9]+a[3]*a[4]*a[8]+a[2]*a[6]*a[7]-a[3]*a[5]*a[7]-a[1]*a[6]*a[8]-a[2]*a[4]*a[9]
  52.                 else
  53.                         local value=0
  54.                         local cofactor=0
  55.                         local add=true
  56.  
  57.                         for i=1,this.row do
  58.                                 cofactor=Determinant(this:cofactor(1,i))
  59.                                 cofactor=this.data[i]*cofactor:eval()
  60.                                
  61.                                 if add then
  62.                                         value=value+cofactor
  63.                                 else
  64.                                         value=value-cofactor
  65.                                 end
  66.                                 add = not add
  67.                         end
  68.  
  69.                         return value
  70.                 end
  71.         end                         
  72.        
  73.         return det
  74. end
  75.  
  76.  

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter