Advertisement
a3f

Inverse from Adjoint Matrix Method

a3f
Mar 8th, 2012
2,834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     REM "" Law 3andak mashakel fel copy, 5odha men 3l link dah: http://pastebin.com/raw.php?i=z9wbsCiS
  2.     REM Computing Inverse of a Matrix by using the Adjoint Matrix Method
  3.     REM Ver 0.3
  4.  
  5.         'input "size? "; size //TODO
  6.  
  7.         'USER INTERACTION
  8.             print "Computing Inverse of a Matrix by using the Adjoint Matrix Method"
  9.         input "Show Steps ? (yes?)"; ans$
  10.         if ans$ = "1" or lower$(ans$) = "yes" then
  11.         debug = 1
  12.         else
  13.             if ans$ = "2" or lower$(ans$) = "debug" then debug = 2
  14.         end if
  15.  
  16.  
  17.         size = 3 '7gm el matrix
  18.         DIM orig(size+1,size+1)
  19.         for r = 1 to size
  20.             for c = 1 to size
  21.                 input "Row :";r;" Column :";c;" :"; orig(r,c)
  22.             next c
  23.         next r
  24.  
  25.  
  26.         REM ~~ START
  27.  
  28.         dim modi(size, size)
  29.  
  30.         CrossX = 1 'X coord of the value which minor we want
  31.         CrossY = 1 'Y coord of same
  32.         n = 0 'Where to "goto" after execution
  33.         goto 80 'ro7 e7seb el minor
  34.         10 'back
  35.  
  36.         Determinant = Det(orig, 3, 0) 'Calculate Determinant
  37.         print
  38.         if Determinant <> 0 then
  39.              print "Determinant: "; Determinant
  40.         else
  41.              print "|A| = 0, No Inverse exists for Zero-Matrix"
  42.         end
  43.         end if
  44.                                                                     if debug > 0 then
  45.                                                                        print "Minors:"
  46.                                                                     end if
  47.         '------------------------------------- ne7seb el cofactors
  48.         DIM co(size+1,size+1) 'CoFactor Matrix
  49.         for Roww = 1 to size
  50.             for Coll = 1 to size
  51.                 CrossX = Roww
  52.                 CrossY = Coll
  53.                 n = 2
  54.                 goto 80 'Minor
  55.                 40 'back
  56.                                                                     if debug > 0 then
  57.                                                                         for r = 1 to size-1
  58.                                                                             print
  59.                                                                             print chr$(5);
  60.                                                                             for c = 1 to size-1
  61.                                                                                 print modi(r,c),
  62.                                                                             next c
  63.                                                                             print chr$(5);
  64.                                                                         next r
  65.                                                                     end if
  66.                 co(Roww,Coll) = Det(modi, size-1, 1)
  67.                 'print ">";co(Roww,Coll)
  68.                                                                     if debug > 0 then print "*";orig(Roww,Coll)
  69.             next Coll
  70.         next Roww
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.         '- ne5aiar el esharat we n2leb el matrix we n2sem 3l determinant
  80.         dim inv(size+1,size+1)
  81.         for r = 1 to size
  82.             for c = 1 to size
  83.                 inv(r,c) = co(c,r) * -1^(r+c) / Determinant
  84.                                                                     if debug > 1 then print inv(r,c)
  85.             next c
  86.                                                                     if debug > 0 then print
  87.         next r
  88.  
  89.  
  90.  
  91.         print 'nekteb el nateg
  92.         'print "!!!!!"
  93.         print chr$(23);"Inverse" , " " ,"","";chr$(2) 'awel satr
  94.         for r = 1 to size
  95.             print chr$(5) ;
  96.                 for c = 1 to size
  97.  
  98.                 if int(inv(r,c)) = inv(r,c) then print inv(r,c),
  99.  
  100.                 if int(inv(r,c)) <> inv(r,c) then
  101.                     if int(inv(r,c)*abs(Determinant))^2 = abs(inv(r,c)*Determinant)^2 then
  102.                         print  int(inv(r,c)*abs(Determinant)) ; "/" ; abs(Determinant),
  103.                     else
  104.                         print inv(r,c)*abs(Determinant) ; "/" ; abs(Determinant),
  105.                     end if
  106.                 end if
  107.                 'print Determinant/abs(Determinant) *inv(r,c)*Determinant ;"/"; abs(Determinant)
  108.                 next c
  109.             print chr$(5)
  110.         next r
  111.             print chr$(3);"" , " " ,"","";chr$(4) 'el a5eer
  112.                                                             end 'The
  113.         80 ' e7seb el minor lel 3onsor(CrossX, CrossY) fel matrix orig(n*n) : n = size
  114.  
  115.         x = 1
  116.         y = 1
  117.         for nRow = 1 to size
  118.             if nRow <> CrossX then
  119.                 for nCol = 1 to size
  120.                     if nCol <> CrossY then
  121.                         modi(x,y) = orig(nRow,nCol)
  122.                         if y >= size-1 then
  123.                             x = x+1
  124.                             y = 0
  125.                         end if
  126.                             y = y + 1
  127.                     end if
  128.                 next nCol
  129.             end if
  130.         next nRow
  131.  
  132.         if n = 0 then goto 10
  133.         if n = 1 then goto 94
  134.         if n = 2 then goto 40
  135.  
  136.  
  137.  
  138.  
  139.         'print Det(orig, 3, 1)
  140.  
  141.  
  142.  
  143.         Function Det(orig, DetSize, InsideJob)
  144.             if InsideJob then
  145.                 DIM basicsucks(DetSize,DetSize)
  146.                     for r = 1 to DetSize
  147.                         for c = 1 to DetSize
  148.                             basicsucks(r,c) = modi(r,c)
  149.                         next c
  150.                     next r
  151.             else
  152.                 DIM basicsucks(DetSize,DetSize)
  153.                     for r = 1 to DetSize
  154.                         for c = 1 to DetSize
  155.                             basicsucks(r,c) = orig(r,c)
  156.                         next c
  157.                     next r
  158.             end if
  159.  
  160.             if DetSize = 1 then Det = orig(1,1)
  161.             if DetSize = 2 then
  162.                 Det = basicsucks(1,1) * basicsucks(2,2) - basicsucks(2,1) * basicsucks(1,2)
  163.  
  164.                                                                     if debug > 1 then
  165.                                                                         print "[";orig(1,1);"*";orig(2,2);"-";
  166.                                                                         print  orig(2,1);"*" ;orig(1,2);"]"
  167.                                                                     end if
  168.             end if
  169.             if DetSize > 2 then
  170.  
  171.                 'for row = 1 to DetSize
  172.                 row = 1
  173.                 for col = 1 to DetSize
  174.                     CrossX = row
  175.                     CrossY = col
  176.                     pass = 1
  177.                     goto 96
  178.                         94 'code
  179.                         'for r = 1 to DetSize-1
  180.                         'for c = 1 to DetSize -1
  181.  
  182.  
  183.                         'print modi(r,c),
  184.  
  185.  
  186.                         'next c
  187.                         'print
  188.                         'next r
  189.                         'print
  190.                                                                     if debug > 1 then
  191.                                                                         print "Det= ";Det ," + (-1)^";(row+col);" * ";
  192.                                                                         print orig(row,col);"*";Det(modi,DetSize-1,1);
  193.                                                                     end if
  194.                     Det = Det + (-1)^(row+col) * basicsucks(row,col) * DuoDet(modi, DetSize - 1, 1)
  195.                     if debug > 1 then print "= ",Det
  196.                 next col
  197.                     'next row
  198.             end if
  199.  
  200.                     96
  201.                     if pass = 1 then
  202.                         'DIM modi(DetSize-1,DetSize-1)
  203.                         x = 1
  204.                         y = 1
  205.                         for nRow = 1 to DetSize
  206.                             if nRow <> CrossX then
  207.                                 for nCol = 1 to DetSize
  208.                                     if nCol <> CrossY then
  209.                                         modi(x,y) = orig(nRow,nCol)
  210.                                                                     if debug > 1 then print orig(nRow,nCol)
  211.                                         if y >= DetSize-1 then
  212.                                             x = x+1
  213.                                             y = 0
  214.                                         end if
  215.                                         y = y + 1
  216.                                     end if
  217.                                 next nCol
  218.                             end if
  219.                         next nRow
  220.                         pass = 0
  221.                         goto 94
  222.  
  223.                     end if
  224.         end function
  225.  
  226.         Function DuoDet(modi, DetSize, hmmm)
  227.                  DuoDet = modi(1,1) * modi(2,2) - modi(2,1) * modi(1,2)
  228.         End function
  229.  
  230.  
  231.         function GCD(u, v)
  232.           if v then
  233.             GCD = GCD(v, u mod v)
  234.           else
  235.             GCD = abs(u)
  236.             end if
  237.         end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement