Post Reply 
Matrix in a program
10-26-2020, 10:21 PM
Post: #1
Matrix in a program
Hello

I have no practice in programming and when I tried to develop some algebra with the HP prime to find implicit equations the program fails:


//Implicit equations by Gaus
EXPORT ImpGa(Ma1)
BEGIN
//d-dimension , c-column dimension
//n-actual column , r-row search
LOCAL d,c,n,r,Ma2;

c:=colDim(Ma1);
n:=1;

FOR n FROM n=1 TO c
DO
//Search for 1 in col n
r:=col(Ma1,n);
// RETURN r;
r:=CAS.POS(r,1);
IF r==0 THEN
r:=n;
ELSE
IF r≠n THEN
SWAPROW(Ma1,r,n);
END;
END;
Ma1:=pivot(Ma1,n,n);
END;

RETURN Ma1;

END;



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.

Seems that does not accept variables in the program.
Doing the same operations manually in CAS mode it works.

Can you give some clue about how to introduce this kind of matrix in this program?

Thanks very much

Toni Garçon
Find all posts by this user
Quote this message in a reply
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
10-27-2020, 07:37 PM
Post: #3
RE: Matrix in a program
Hello

Thanks very much for your help.

First I tried with the system #cas / #end, and ImpGa(matrix) gives the same as in XCas:
[[1,0,x1-2*x3],[0,1,x3],[0,0,-2*x1+x2+3*x3],[0,0,-x1+x4],[0,0,3*x3+x5]]

Following also the suggested corrections (not including variable column which really has not sense at all):

#cas
//Implicit ecuations by Gaus
ImpGa(Ma1):=
BEGIN
//d-dimension , c-column dimension
//n-actual column , r-row search
LOCAL d,c,n,r,Ma2;

c:=colDim(Ma1);
n:=1;

FOR n FROM 1 TO c-1
DO
//Search for 1 in col n
r:=col(Ma1,n);
// RETURN r;
r:=POS(r,1);
IF r==0 THEN
r:=n;
ELSE
IF r≠n THEN
SWAPROW(Ma1,r,n);
END;
END;
Ma1:=pivot(Ma1,n,n);
END;

RETURN Ma1;

END;

#end


I do not know if this is my interpretation, but the ImpGa() function has disappear from the "User" menu and has gone to the "Catlg" menu. Possibly all CAS functions go to general catalog.
This raises the question if it is possible to group CAS functions.
I have to practice much more.

Thanks again to all

Toni


Attached File(s) Thumbnail(s)
   
Find all posts by this user
Quote this message in a reply
10-27-2020, 07:59 PM
Post: #4
RE: Matrix in a program
Sorry it does not work with any matrix, so I have still errors.
I will correct and send it again.

Toni
Find all posts by this user
Quote this message in a reply
Post Reply 




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