Jelajahi Sumber

add 3x3 inverse matrix prgm

Noah Vogt 3 tahun lalu
induk
melakukan
092c609827
1 mengubah file dengan 248 tambahan dan 0 penghapusan
  1. 248 0
      src/inverse.tibasic

+ 248 - 0
src/inverse.tibasic

@@ -0,0 +1,248 @@
+# this is a program to inverse a 3x3 matrix and visually present all steps
+# that are made using the gauss-jordan eliminination
+
+# exit if the determinent of the input matrix is 0
+If det([C])=0
+    Then
+    Disp "ERROR: DET([C]) = 0"
+    Disp "CANNOT INVERT MATRIX"
+    Stop
+End
+
+# set the 3x3 identity matrix to working matrix 2 (B)
+randM(3,3)->[B]
+Fill(0,[B])
+1->[B](1,1)
+1->[B](2,2)
+1->[B](3,3)
+
+# set the augmented matrix of [A] (input matrix) and [B] to [B]
+augment([A],[B])->[B]
+
+# COLUMN 1
+
+# swap rows so that the row containing the biggest number of the first column
+# is at the top
+
+{[B](1,1),[B](2,1),[B](3,1)}->[list]MAX
+
+If max([list]MAX)!=[B](1,1)
+    Then
+    RowSwap([B],1,2)->[B]
+    ClrHome
+    Disp "R1 <> R2"
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+If max([list]MAX)!=[B](1,1)
+    Then
+    RowSwap([B],1,3)->[B]
+    ClrHome
+    Disp "R1 <> R3"
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+
+# multiply the top row by the reverse of its left-most entry.
+If [B](1,1)!=0
+    Then
+    (1/[B](1,1))->Z
+    *row(Z,[B],1)->[B]
+    ClrHome
+    Disp toString([neg]Z)+"R3"
+    Disp [neg]Z->FRAC
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# get row 3, column 1 to zero
+If [B](3,1)!=0
+    Then
+    [neg][B](3,1)/[B](1,1)->Z
+    *row+(Z,[B],1,3)->[B]
+    ClrHome
+    Disp "R2 - "+toString([neg]Z)+"R1"
+    Disp [neg]Z->FRAC
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# get row 2, column 1 to zero
+If [B](2,1)!=0
+    Then
+    [neg][B](2,1)/[B](1,1)->Z
+    *row+(Z,[B],1,2)->[B]
+    ClrHome
+    Disp "R2 - "+toString([neg]Z)+"R1"
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# COLUMN 2
+
+# swap rows so that the row containing the biggest number of the second column
+# is in the middle
+
+{[B](1,2),[B](2,2),[B](3,2)}->[list]MAX
+
+If max([list]MAX)!=[B](2,2)
+    Then
+    RowSwap([B],1,2)->[B]
+    ClrHome
+    Disp "R1 <> R2"
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+If max([list]MAX)!=[B](2,2)
+    Then
+    RowSwap([B],2,3)->[B]
+    ClrHome
+    Disp "R2 <> R3"
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# correct the error when r2,c1 = 1 (r2,c1 != 0)
+# TODO: evaluate negating before this step here
+If [B](2,1)!=0
+    Then
+    RowSwap([B],1,2)->[B]
+    ClrHome
+    Disp "R2 <> R3"
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# multiply the second row by the reverse of its middle entry.
+If [B](2,2)!=0
+    Then
+    (1/[B](2,2))->Z
+    *row(Z,[B],2)->[B]
+    ClrHome
+    Disp toString(Z)+"R2"
+    Disp [neg]Z->FRAC
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# get row 3, column 2 to zero
+If [B](3,2)!=0
+    Then
+    [neg][B](3,2)/[B](2,2)->Z
+    *row+(Z,[B],2,3)->[B]
+    ClrHome
+    Disp "R3 - "+toString([neg]Z)+"R2"
+    Disp [neg]Z->FRAC
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# get row 1, column 2 to zero
+If [B](1,2)!=0
+    Then
+    [neg][B](1,2)/[B](2,2)->Z
+    *row+(Z,[B],2,1)->[B]
+    ClrHome
+    Disp "R1 - "+toString([neg]Z)+"R2"
+    Disp [neg]Z->FRAC
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# correct the error when still r3,c1 = 1 (r3,c1 != 0)
+# this seems to work fine with just swapping rows 1 and 3
+# TODO: evaluate negating before and after this step here
+If [B](3,1)!=0
+    Then
+    RowSwap([B],1,3)->[B]
+    ClrHome
+    Disp "R1 <> R3"
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# multiply the third row by the reverse of its last entry.
+If [B](3,3)!=0
+    Then
+    (1/[B](3,3))->Z
+    *row(Z,[B],3)->[B]
+    ClrHome
+    Disp toString(Z)+"R3"
+    Disp [neg]Z->FRAC
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# get row 2, column 3 to zero
+If [B](2,3)!=0
+    Then
+    [neg][B](2,3)/[B](3,3)->Z
+    *row+(Z,[B],3,2)->[B]
+    ClrHome
+    Disp "R2 - "+toString([neg]Z)+"R3"
+    Disp [neg]Z->FRAC
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# get row 1, column 3 to zero
+If [B](1,3)!=0
+    Then
+    [neg][B](1,3)/[B](3,3)->Z
+    *row+(Z,[B],3,1)->[B]
+    ClrHome
+    Disp "R1 - "+toString([neg]Z)+"R3"
+    Disp [neg]Z->FRAC
+    Disp [B]->FRAC
+    Disp [B](1,6)->FRAC
+    Disp [B](2,6)->FRAC
+    Disp [B](3,6)->FRAC
+    Pause
+End
+
+# TODO: checking / validation for the result