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.
- You cannot use
with
inside a procedure.
- Your code uses commands from the
linalg
package, not the MTM
package.
- Ideally you'd use Matrix&Vector&LinearAlgebra
(instead of your mix of matrix&Vector&linalg(.
- 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.
- Several of you lines have just
=
instead of
:=
for assignments. The =
does nothing.
- 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.
- You should be using
LinearAlgebra
equivalents instead of the linalg
commands
commands swaprow
, coldim
.
- You forgot to have your procedure actually
return the Matrix
a
.
- When your code calls
swaprow
is was not
actually updating a
. It was just throwing
way the result.
- It's a sin to not indent your code. It will
lead you to overlook mistakes.