Post Reply 
Matrix in a program
10-27-2020, 02:28 AM
Post: #2
RE: Matrix in a program
(10-26-2020 10:21 PM)Tonig00 Wrote:  When I tried to run it it gives me "Error: Invalid input".
I put in the command line:
ImpGa([[1 2 x1] [2 1 x2] [0 1 x3] [1 2 x4] [3 -1 x5]]
If I delete the columna [x1 x2 x3 x4 x5] it works.

It does not work even with variables deleted. It just seems it did.
Anyway, you should not do pivot on the variables. It make no sense.

Some Issues:
1. SWAPROW(Ma1,r,n) does nothing. You have to save the returns.
2. pivot on 1 is preferred. But more importantly, pivot must be non-zero.
3. pivot row/col can only be used once.

see Gauss Jordan Elimination Through Pivoting

This is XCas implementation of revised code.
note: XCas is setup 0-based, but POS returns 1-based (0 signals not found)

Code:
ImpGa(M, n) := {              // pivot upto n columns
  local r, c, p, j, k;
  M := copy(M);               // work with a copy  
  [r, c] := dim(M);
  if (n <= 0) n += c;         // 0 = all columns
  for(j:=0; j<n; j++) {       // 0-based columns
    p := abs(row(col(M,j),j..r));
    if (sum(p)==0) return M;  // no nonzero pivot
    k := POS(p,1);            // pivot on 1, if possible
    if (k==0) k := POS(p,max(p));
    M := swaprow(M, j+k-1, j);
    M := pivot(M, j, j);
  }
}

XCas> M := [[1,2,x1],[2,1,x2],[0,1,x3],[1,2,x4],[3,-1,x5]]
XCas> ImpGa(M, 2)

\(\left(\begin{array}{ccc}
1 & 0 & \mathrm{x1}-2\cdot \mathrm{x3} \\
0 & 1 & \mathrm{x3} \\
0 & 0 & -2\cdot \mathrm{x1}+\mathrm{x2}+3\cdot \mathrm{x3} \\
0 & 0 & -\mathrm{x1}+\mathrm{x4} \\
0 & 0 & -3\cdot \mathrm{x1}+7\cdot \mathrm{x3}+\mathrm{x5}
\end{array}\right) \)

(10-27-2020 01:03 AM)Major Wrote:  If you are wanting to use variables then I think it needs to be a CAS program instead of a Home program.

Another way is replacing variables with an identity matrix.

XCas> M := [[1,2],[2,1],[0,1],[1,2],[3,-1]]
XCas> ImpGa(augment(M, identity(5)), 2)

\(\left(\begin{array}{ccccccc}
1 & 0 & 1 & 0 & -2 & 0 & 0 \\
0 & 1 & 0 & 0 & 1 & 0 & 0 \\
0 & 0 & -2 & 1 & 3 & 0 & 0 \\
0 & 0 & -1 & 0 & 0 & 1 & 0 \\
0 & 0 & -3 & 0 & 7 & 0 & 1
\end{array}\right) \)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Matrix in a program - Tonig00 - 10-26-2020, 10:21 PM
RE: Matrix in a program - Albert Chan - 10-27-2020 02:28 AM
RE: Matrix in a program - Tonig00 - 10-27-2020, 07:37 PM
RE: Matrix in a program - Tonig00 - 10-27-2020, 07:59 PM



User(s) browsing this thread: 1 Guest(s)