so i am working on a Problem which has two parts. I did finish part one with help of :this useful Forum. Some body already tried to do the first part of the problem and i took their code.
To the Problem:
The Code to the first Part ( the Code is tested and i claim that it runs correctly)
multiplikation:= proc(
m::posint,
a::depends(And(posint, satisfies(a-> a <= m))),
b::And({float, rational}, Not(identical(0,0.)))
)
Matrix((m,m), (i,j)-> `if`(i=j, `if`(i=a, b, 1), 0))
end proc:
addition:= proc(
m::posint,
a::depends(And(posint, satisfies(a-> a <= m))),
b::depends(And(posint, satisfies(b-> b <= m))),
c::And({float, rational}, Not(identical(0,0.)))
)
Matrix((m,m), (i,j)-> `if`(i=a and j=b, c, `if`(i=j, 1, 0)))
end proc:
perm:= proc(
m::posint,
a::depends(And(posint, satisfies(a-> a <= m))),
b::depends(And(posint, satisfies(b-> b <= m and a<>b)))
)
Matrix((m,m), (i,j)-> `if`({i,j}={a,b} or i=j and not i in {a,b}, 1, 0))
end proc:
and the main proc :
reduced:= proc(B::Matrix)
uses LA= LinearAlgebra;
local
M:= B, l:= 1, #l is current column.
m:= LA:-RowDimension(M), n:= LA:-ColumnDimension(M), i, j
;
for i to m do #going through every row item
#l needs to be less than column number n.
if n < l then return M end if;
j:= i; #Initialize current row number.
while M[j,l]=0 do #Search for 1st row item <> 0.
j:= j+1;
if m < j then #End of row: Go to next column.
j:= i;
l:= l+1;
if n < l then return M fi #end of column and row
end if
end do;
if j<>i then M:= perm(m,j,i).M end if; #Permute rows j and i
#Multiply row i with 1/M[i,l], if it's not 0.
if M[i,l] <> 0 then M:= multiplikation(m,i,1/M[i,l]).M fi;
#Subtract each row j with row i for M[j,l]-times.
for j to m do if j<>i then M:= addition(m,j,i,-M[j,l]).M fi od;
l:= l+1 #Increase l by 1; next iteration i increase either.
end do;
return M
end proc:
If you need any additional Information about the Code above, i will explain more.
for the second part i am thinking to use the Gauss Jordan Algorithmus but i have a problem: i cannot use the identity matrix as a parameter in "reduced". because it has 0 in rows and columns.
Do you have any idea how i could implement the Gauss Jordan Algorithmus with the Help of my proc : reduced ?
The stated goal was to utilize the reduced
procedure.
One way to do that is to augment the input Matrix by the identity Matrix, reduce that, and then return the right half the augmented Matrix.
The steps that transform the input Matrix into the identity Matrix also transform the identity Matrix into the inverse (of the input Matrix).
For example, using your procdures,
inv := proc(B::Matrix(square))
local augmented,m;
uses LinearAlgebra;
m := RowDimension(B);
augmented := <<B|IdentityMatrix(m)>>;
return reduced(augmented)[..,m+1..-1];
end proc:
MM := LinearAlgebra:-RandomMatrix(3,generator=1..5);
[1 4 2]
[ ]
MM := [1 5 3]
[ ]
[2 3 5]
ans := inv(MM);
[ 8 -7 1]
[ - -- -]
[ 3 3 3]
[ ]
[ 1 1 -1]
ans := [ - - --]
[ 6 6 6 ]
[ ]
[-7 5 1]
[-- - -]
[6 6 6]
ans.MM, MM.ans;
[1 0 0] [1 0 0]
[ ] [ ]
[0 1 0], [0 1 0]
[ ] [ ]
[0 0 1] [0 0 1]
ps. You might want to also consider what your reduce
prodedure does when the Matrix is not invertible.