Maple error - "illegal use of a formal parameter"

231 views Asked by At

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

1

There are 1 answers

0
acer On
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.