Advertisement
bigman95sch

Inverse Matrix

Feb 10th, 2014
2,969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'matrix.bmk
  2.  
  3. SuperStrict
  4.  
  5. Type TMatrix
  6.  
  7.     Field cols:Byte
  8.     Field rows:Byte
  9.     Field index:Double[][]
  10.    
  11.     Method minor:Double(row:Byte, col:Byte)
  12.    
  13.         Local matrix:TMatrix = New TMatrix
  14.         Local i:Byte
  15.         Local j:Byte
  16.        
  17.         Assert ((cols > 0) And (rows > 0)), " Matrix is empty"
  18.         Assert (cols = rows), " Matrix is not square one"
  19.        
  20.         matrix.cols = cols-1
  21.         matrix.rows = rows-1
  22.         matrix.index = New Double[][rows - 1]
  23.  
  24.         For i = 0 To rows - 2
  25.             matrix.index[i] = New Double[cols - 1]
  26.             For j = 0 To cols - 2      
  27.                 matrix.index[i][j] = index[i+(i => row)][j+(j => col)]
  28.             Next
  29.         Next
  30.        
  31.         If (row+col) Mod 2 = 1
  32.             Return matrix.determinant()
  33.         Else
  34.             Return -matrix.determinant()
  35.         End If
  36.                
  37.     End Method
  38.    
  39.     Method determinant:Double()
  40.        
  41.         Local sum:Double
  42.         Local i:Byte
  43.         Local j:Byte
  44.  
  45.         Assert ((cols > 0) And (rows > 0)), " Matrix is empty"
  46.         Assert (cols = rows), " Matrix is not square one"
  47.        
  48.         If cols = 1
  49.             Return index[0][0]
  50.         End If
  51.        
  52.         For i = 0 To cols-1
  53.             sum :+ minor(0, i) * index[0][i]
  54.         Next
  55.        
  56.         Return sum
  57.            
  58.     End Method
  59.    
  60.     Method transpose:TMatrix()
  61.        
  62.         Local array:Double[][] = New Double[][cols]
  63.         Local i:Byte
  64.         Local j:Byte
  65.        
  66.         For i = 0 To cols-1
  67.             array[i] = New Double[rows]
  68.             For j = 0 To rows-1
  69.                 array[i][j] = index[j][i]
  70.             Next
  71.         Next
  72.        
  73.         index = array
  74.        
  75.         Return Self
  76.        
  77.     End Method
  78.    
  79.     Method copy:TMatrix()
  80.        
  81.         Local matrix:TMatrix = New TMatrix
  82.         Local i:Byte
  83.         Local j:Byte
  84.         Local array:Double[][]
  85.        
  86.         matrix.cols = cols
  87.         matrix.rows = rows
  88.         matrix.index = array
  89.         array = New [rows]
  90.         For i = 0 To rows-1
  91.             array[i] = New [cols]
  92.             For j = 0 To cols-1
  93.                 array[i][j] = index[i][j]
  94.             Next
  95.         Next
  96.        
  97.         Return matrix
  98.    
  99.     End Method
  100.    
  101.     Method getInverse:TMatrix()
  102.    
  103.         Local matrix:TMatrix = New TMatrix
  104.         Local i:Byte
  105.         Local j:Byte
  106.         Local array:Double[][]
  107.         Local det:Double
  108.        
  109.         Assert ((cols > 0) And (rows > 0)), " Matrix is empty"
  110.         Assert (cols = rows), " Matrix is not square one"
  111.        
  112.         matrix.cols = cols
  113.         matrix.rows = rows
  114.         det = determinant()
  115.         Assert det, "Determinant is equal zero"
  116.  
  117.         array = New Double[][rows]
  118.         For i = 0 To rows-1
  119.             array[i] = New Double[cols]
  120.             For j = 0 To cols-1
  121.                 array[i][j] = minor(i,j)/det
  122.             Next
  123.         Next
  124.         matrix.index = array
  125.         matrix.transpose()
  126.         Return matrix
  127.        
  128.     End Method
  129.        
  130. End Type
  131.  
  132. Local matrix:TMatrix
  133. Local i:Byte
  134. Local j:Byte
  135. Local a:Byte
  136. Local size:Byte
  137. Local line:String
  138. Local numstrs:String[]
  139. Local numbers:Double[]
  140. Local quantity:Short
  141.  
  142. Repeat
  143.  
  144.     matrix = New TMatrix
  145.    
  146.     Repeat
  147.         Print "Enter 'exit' to close the program"
  148.         line = Input("Enter size of matrix (integer within 2..255) => ")
  149.         If line = "exit"
  150.             End
  151.         End If
  152.         size = Int(line)
  153.     Until size => 2
  154.     Print "Size of matrix = " + size
  155.    
  156.     quantity = 0
  157.     numbers = New Double[size*size]
  158.    
  159.     matrix.cols = size
  160.     matrix.rows = size
  161.    
  162.     Print "Enter " + (size*size) + " numbers separated by spaces:"
  163.  
  164.     Repeat
  165.            
  166.         line = Input()
  167.         If line.find("exit") => 0
  168.             End
  169.         End If
  170.  
  171.         numstrs = line.Split(" ")
  172.         a = numstrs.length
  173.         If quantity + a > size * size
  174.             a = size * size - quantity
  175.         End If
  176.         For i = 0 To a-1
  177.             numbers[quantity+i] = Double(numstrs[i])
  178.         Next
  179.        
  180.         quantity :+ a
  181.         If quantity < size*size
  182.             Print "Enter " + (size*size-quantity) + " more numbers"
  183.         End If
  184.        
  185.     Until quantity = size*size
  186.    
  187.     matrix.index = New Double[][size]
  188.     For i = 0 To size - 1
  189.         matrix.index[i] = New Double[size]
  190.         For j = 0 To size - 1
  191.             matrix.index[i][j] = numbers[i*size+j]
  192.         Next
  193.     Next
  194.    
  195.     Print
  196.     Print "--= YOUR MATRIX =--"
  197.     For i = 0 To matrix.rows-1
  198.         line = ""
  199.         For j = 0 To matrix.cols-1
  200.             line :+ Float(matrix.index[i][j]) + " "
  201.         Next
  202.         Print line
  203.     Next
  204.    
  205.     Print
  206.     Print "--= INVERSE MATRIX =-- "
  207.    
  208.     If matrix.determinant()
  209.    
  210.         matrix = matrix.getInverse()
  211.        
  212.         For i = 0 To matrix.rows-1
  213.             line = ""
  214.             For j = 0 To matrix.cols-1
  215.                 line :+ Float(matrix.index[i][j]) + " "
  216.             Next
  217.             Print line
  218.         Next
  219.        
  220.     Else
  221.    
  222.         Print "Inverse matrix doesn't exist"
  223.         Print "due to your matrix is degenerate"
  224.    
  225.     End If
  226.    
  227.     Print
  228.    
  229. Forever
  230. End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement