Maple - LU Factorization with Partial Pivoting

In summary, the conversation discusses the task of writing a procedure that will inverse square matrices using LU factorization with partial pivoting. The procedure should also return the inverse matrix and report an error if it cannot do so. The individual has come up with a code, but is unsure if it is correct and is struggling to find help with the next step of solving the linear systems. They share the code they have so far and ask for advice or suggestions on how to proceed and how to form the inverse matrix.
  • #1
Rupert11
2
0
1. I am asked to write a procedure that will inverse square matices using LU factorization with partial pivoting.



2. I am also told that the procedure should return the inverse matrix and report an error if it cannot do so.



3. So far I've come up with the code below but am not confident it is correct and I am also really struggling to find help with the next step for solving the linear systems i need too proceed with. Any adivce or suggestions:
restart :

n:= n; # all matrices are nxn matrices
A:= array (1..n, 1..n);
L:= array (1..n, 1..n);
U:= array (1..n, 1..n);
LU:= array (1..n, 1..n);

print('A is', A);

# carry out the Gaussian elimination
for j from 1 to n-1 do
for i from j+1 to n do
# calculate the multiplier for the ith row at the jth stage
m:= A[i,j]/A[j,j];
A[i,j]:= m;
for k from j+1 to n do
A[i,k]:= A[i,k]-m*A[j,k];
od:
od:
od:
print ('new A is', A);

# Extract L and U from the new A
for i from 1 to n do
for k from 1 to n do
if i<k then
U[i,k]:= A[i,k]
L[i,k]:= 0;
elif i=k then
U[i,k]:= A[i,j]
L[i,k]:=1;
else
U[i,k]:= 0
L[i,k]:= A[i,j]
fi;
od:
od:

print('L is',L);
print('U is',U);

# verify that A=LU
for i from 1 to n do
for k from 1 to n do
LU[i,k]:= add(L[i,j]*U[i,k], k=1..n);
od:
od:

print('LU is', LU);
 
Physics news on Phys.org
  • #2
So, I've been working on this problem for a few hours today and have come up with this procedure (below) but need advice on how to change it so that it works for a square matrix only and then finally how to form the inverse matrix to finish the question. Any ideas welcome...
# LU decomposition using Gaussian elimination with partial pivoting
# lufactor does the LU factorization
# lusolve solves a linear system using the LU factorization
lufactor := proc(A)
local n, r, k, p, i, temp, m, j;

n := rowdim(A); # get number of rows in matrix A
r := vector(n); # create pivot vector
for k from 1 to n do r[k] := k od; # initialize pivot vector

for k from 1 to n-1 do # loop over columns

# We find the maximal element in absolute value among the
# entries A[r[k],k], A[r[k+1],k], ... A[r[n],k].
# Interchange entries in pivot vector

p := k;
for i from k+1 to n do
if (abs(A[r,k]) > abs(A[r[p],k])) then p := i; fi;
od;
if A[r[p],k]=0 then
ERROR(`Matrix is singular`); # procedure will exit here
fi;
if (p <> k) then # interchange entries k and p in pivot vector
temp := r[k];
r[k] := r[p];
r[p] := temp;
fi;

# now do a gaussian elimination step

for i from k+1 to n do # loop down column below diagonal
m := A[r,k]/A[r[k],k]; # get multiplier
for j from k+1 to n do # subtract multiple of row i from
A[r,j] := A[r,j] - m*A[r[k],j]; # row k
od;
A[r,k] := m; # save multiplier
od;
od;
RETURN(op(r)); # return pivot vector
end:

# solve the linear system Ax=b given the LU factorization in a returned
# from ludecomp, the pivot vector r returned from ludecomp and the right
# hand side vector b.

lusolve := proc(A,r,b)
local n, y, x, i, s, j;
n := rowdim(A); # get number of rows in matrix A
y := vector(n); # create vector for solution of Ly=Pb
x := vector(n); # create vector for solution of Ux=y

# do forward substitution to solve Ly=Pb

y[1] := b[r[1]];
for i from 2 to n do
s := b[r];
for j from 1 to i-1 do
s := s - A[r,j]*y[j];
od;
y := s;
od;

# do backward substitution to solve Ux=y

x[n] := y[n]/A[r[n],n];
for i from n-1 to 1 by -1 do
s := y;
for j from i+1 to n do
s := s - A[r,j]*x[j];
od;
x := s / A[r,i];
od;
RETURN(op(x)); # return solution vector
end:
 

1. What is Maple LU Factorization with Partial Pivoting?

Maple LU Factorization with Partial Pivoting is a mathematical algorithm used to factorize a square matrix into a lower triangular matrix and an upper triangular matrix with partial pivoting. It is used to solve systems of linear equations and can handle matrices with large numbers and small or near-zero values.

2. How does Maple LU Factorization with Partial Pivoting work?

The algorithm works by finding the LU decomposition of a matrix, where L is a lower triangular matrix and U is an upper triangular matrix. Partial pivoting is used to swap rows in the matrix to reduce the impact of small or near-zero elements. The final result is a factorized matrix that can be used to solve linear equations.

3. What are the advantages of using Maple LU Factorization with Partial Pivoting?

Maple LU Factorization with Partial Pivoting has several advantages, including its ability to handle large and sparse matrices, its efficiency in solving systems of linear equations, and its numerical stability due to the use of partial pivoting. It is also a well-established and widely used algorithm in numerical linear algebra.

4. When is Maple LU Factorization with Partial Pivoting used?

This algorithm is commonly used in scientific computing, particularly in solving systems of linear equations, such as those found in engineering, physics, and other fields. It is also used in data analysis and machine learning applications.

5. Are there any limitations to using Maple LU Factorization with Partial Pivoting?

While Maple LU Factorization with Partial Pivoting is a powerful and widely used algorithm, it does have some limitations. It can be computationally expensive for very large matrices, and it may not be suitable for matrices that are ill-conditioned or close to singular. In these cases, other factorization methods may be more appropriate.

Similar threads

  • Calculus and Beyond Homework Help
Replies
5
Views
2K
  • Calculus and Beyond Homework Help
Replies
0
Views
151
  • Calculus and Beyond Homework Help
Replies
16
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
2K
  • Calculus and Beyond Homework Help
Replies
8
Views
233
  • Calculus and Beyond Homework Help
Replies
2
Views
962
  • Calculus and Beyond Homework Help
Replies
6
Views
1K
  • Calculus and Beyond Homework Help
Replies
7
Views
811
  • Calculus and Beyond Homework Help
Replies
3
Views
982
  • Calculus and Beyond Homework Help
Replies
4
Views
498
Back
Top