4
$\begingroup$

I have a program, which makes Gauss-Jordan transformations (not too pretty, I suppose). I want to get output with Manipulator like that: Two-Phase Simplex Method with only "step number" slider. One table for each step. Is it possible?

Matrix:

A = {{6, 15, 6, 1, 0, 0, 9}, {14, 42, 16, 0, 1, 0, 21}, {2, 8, 2, 0, 
0, 1, 4}, {10, -7, -5, 0, 0, 0, 0}};

Code:

Clear[GaussJordanStep]
GaussJordanStep[M_] := Module[{i, j, m, n, a, b, B = M},
  {m, n} = Dimensions@M;
  {a, b} = 
   Flatten@Thread[{First@
       Ordering[
        Replace[Thread[{Flatten@
            M[[;; -2, Flatten@Position[Last@M, Min[Last@M]]]] ,
           M[[;; -2, -1]]}], {{x1_?Positive, x2_} :> x2/x1, 
          else_ :> \[Infinity]}, {1}]], 
      Position[Last@M, Min[Last@M]]}];
  For[i = 1, i <= m, i++,
   For[j = 1, j <= n, j++,
    If[i == a, B[[i, j]] = M[[i, j]]/M[[a, b]], 
     B[[i, j]] = M[[i, j]] - (M[[i, b]] M[[a, j]])/M[[a, b]]]]];
  B]

Clear[SimplexMethod]
SimplexMethod[M_] := Module[{B = M},
  Print[Grid[B, Dividers -> {-2 -> True, -2 -> True}]];
  Do[B = GaussJordanStep[B]; Print[Grid[B, Dividers -> {-2 -> True, -2 -> True}]]; 
   If[Length@Position[B[[-1]], _?Negative] > 0, Continue[], 
    Break[]], {100}]]

Program output:

SimplexMethod[A]

enter image description here

$\endgroup$

1 Answer 1

6
$\begingroup$

First get SimplexMethod to return a list, instead of printing them. Using NestWhileList is a good way to structure the computation of your iterative algorithm.

Mapping the formatting, Grid[#, Dividers -> {-2 -> True, -2 -> True}] & /@ ..., on to the list of tables might better occur outside the function SimplexMethod. I usually format at the output point. Then the tables would remain in a form that is easier to compute with.

Clear[SimplexMethod]
SimplexMethod[M_] :=
 Grid[#, Dividers -> {-2 -> True, -2 -> True}] & /@
  NestWhileList[
   GaussJordanStep,
   M,
   Length@Position[#[[-1]], _?Negative] > 0 &,
   1,
   100]

Then displaying them in Manipulate is relatively straightforward.

With[{tables = SimplexMethod[A]},
 Manipulate[
  Pane[tables[[n]], {180, 100}],
  {n, 1, Length@tables, 1}]
 ]

Mathematica graphics

Alternative: Remove the Grid[#, Dividers -> {-2 -> True, -2 -> True}] & /@ line from SimplexMethod and put Grid in Manipulate.

Manipulate[
 Grid[SimplexMethod[A][[n]], Dividers -> {-2 -> True, -2 -> True}],
 {n, 1, Length@SimplexMethod[A], 1}]

This recomputes SimplexMethod[A] every time the slider is moved. That would only be a problem if it takes a long time to recompute it. Otherwise, it's ok. Also, you can see how Pane above makes a fixed-size window for your tables.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.