0

I'M TRYIN TO CREATE A PROGRAM USING MAPLE FOR GAUSSING ELIMINATION BUT I KEEP GETTING THIS ERROR

     Gauss := proc (n::posint, A::matrix, c::Vector) 
local a, i, k, j, p; 
with(MTM): 
n := linalg[rowdim](A); 
if det(A) = 0 then print('matrice*doit*etre*caree') 
else if det(A) <> 0 
then a := `<|>`(A, c);
 for k to n-1 do
 for i from k+1 to n do 
if a[i, i] = 0 then swaprow(a, k, i) 
else p = a[i, k]/a[k, k]; 
for j from k+1 to n+1 do a[i, j] = a[i, j]-p*a[k, j] 
end do;
 end if; 
end do;
 end do;
 else print('rien') 
end if; end if; end proc;

Error, (in Gauss) illegal use of a formal parameter

Chafi Aya
  • 11
  • 1
  • 1
    Post actual code in plaintext, not an image of code. – acer Dec 06 '22 at 00:25
  • You've updated your question to now provide plaintext code for the procedure. Aside from the fact that it uses the deprecated lowercase `matrix` instead of the modern capitalized `Matrix`, it's not clear what your problem is. You are still missing the example of how you call it (and what you pass to it) that emits the claimed error message. If this is coursework then why copy code that seems written by someone else a long time ago? (It's very outdated.) If it's not coursework then why not use the stock routines already in Maple? – acer Dec 06 '22 at 01:45
  • can u please take a look on this one it gimme the same exact error and yes it 's my own code – Chafi Aya Dec 06 '22 at 18:14
  • The first plaintext version you provided did not give me that error, when I tried it on a sensible input example (which matched results I got from `LUDecomposition`, btw). I won't try again until you provide an input example for your code, and the exact code lines in which you call it. – acer Dec 06 '22 at 21:59
  • c := Vector([2, 3, 4]) A := matrix(3, 3, [4, 1, 2, 3, 6, 5, 2, 1, 9]) Gauss(3, A, c) and then it gamme illegal use of form ... like i mentioned earlier – Chafi Aya Dec 07 '22 at 09:41

1 Answers1

1
restart;
Gauss := proc(A::Matrix, c::Vector) 
  local a, i, k, j, m, n, p; 
  n := linalg[rowdim](A);
  m := linalg[coldim](A);
  if m <> n then
    print("matrice doit etre caree");
  else
    a := `<|>`(A, c);
    for k to n-1 do
      for i from k+1 to n do 
        if a[i, i] = 0 then
          a := linalg[swaprow](a, k, i);
        else
          p := a[i, k]/a[k, k]; 
          for j from k to n+1 do
            a[i, j] := a[i, j]-p*a[k, j];
          end do;
        end if; 
      end do;
    end do;
  end if;
  return a;
end proc:

c := Vector([2, 3, 4]);
A := Matrix(3, 3, [4, 1, 2, 3, 6, 5, 2, 1, 9]);

Gauss(A, c);

LinearAlgebra:-LUDecomposition(<A|c>, output=U);

There were quite a few mistakes, so let's hope I get most of them.

I didn't bother doing 7. You should do it.

  1. You cannot use with inside a procedure.
  2. Your code uses commands from thelinalg package, not the MTM package.
  3. Ideally you'd use Matrix&Vector&LinearAlgebra (instead of your mix of matrix&Vector&linalg(.
  4. Your procedure has n as one of its parameters, but inside it you also try to assign a value to n, the argument for which you passed in as the number 3. That's where your error message is coming from. You can't do that.
  5. Several of you lines have just = instead of := for assignments. The = does nothing.
  6. The test against det(A)=0 is wrong is wrong in several ways. I'll just say that it doesn't actually test whether the A is square. Compare the row & column dimensions if you want to test that A is square.
  7. You should be using LinearAlgebra equivalents instead of the linalg commands commands swaprow, coldim.
  8. You forgot to have your procedure actually return the Matrix a.
  9. When your code calls swaprow is was not actually updating a. It was just throwing way the result.
  10. It's a sin to not indent your code. It will lead you to overlook mistakes.
acer
  • 6,671
  • 15
  • 15