4

Hello guys I am writing program to compute determinant(this part i already did) and Inverse matrix with GEPP. Here problem arises since i have completely no idea how to inverse Matrix using GEPP, i know how to inverse using Gauss Elimination ([A|I]=>[I|B]). I have searched through internet but still no clue, could you please explain me?

Here is my matlab code (maybe someone will find it useful), as of now it solves AX=b and computes determinant:

function [det1,X ] = gauss_czesciowy( A, b )
%GEPP
perm=0;

n = length(b);
if n~=m 
error('vector has wrong size');
end
for j = 1:n
    p=j;
    % choice of main element
    for i = j:n
        if abs(A(i,j)) >= abs(A(p,j))
            p = i;
        end
    end
    if A(p,j) == 0
        error('Matrix A is singular');
    end
    %rows permutation
    t       = A(p,:);
    A(p,:)  = A(j,:);
    A(j,:) = t;
    t       = b(p);
    b(p)    = b(j);
    b(j)    = t;
    if~(p==i)
    perm=perm+1;
    end

    % reduction
    for i = j+1:n
        t       = (A(i,j)/A(j,j)); 
        A(i,:)  = A(i,:)-A(j,:)*t; 
        b(i)    = b(i)-b(j)*t; 
    end 
end
%determinant
mn=1;
for i=1:n
    mn=mn*A(i,i);
end
det1=mn*(-1)^perm;
% solution
X   = zeros(1,n); 
X(n) = b(n)/A(n,n); 

if (det1~=0)
for i = 1:n
    s = sum( A(i, (i+1):n) .* X((i+1):n) ); 
    X(i) = (b(i) - s) / A(i,i); 
end
end
end
e0n
  • 41
  • 2
  • 3
  • If it's the algorithm (and not the programming part) you're searching for, it might be that http://math.stackexchange.com/ is a better place to ask this question. – Stewie Griffin Jun 09 '13 at 14:27

1 Answers1

3

Here is the algorithm for Guassian elimination with partial pivoting. Basically you do Gaussian elimination as usual, but at each step you exchange rows to pick the largest-valued pivot available.

To get the inverse, you have to keep track of how you are switching rows and create a permutation matrix P. The permutation matrix is just the identity matrix of the same size as your A-matrix, but with the same row switches performed. Then you have:

[A] --> GEPP --> [B] and [P]

[A]^(-1) = [B]*[P]

I would try this on a couple of matrices just to be sure.

EDIT: Rather than empirically testing this, let's reason it out. Basically what you are doing when you switch rows in A is you are multiplying it by your permutation matrix P. You could just do this before you started GE and end up with the same result, which would be:

[P*A|I] --> GE --> [I|B] or
(P*A)^(-1) = B

Due to the properties of the inverse operation, this can be rewritten:

A^(-1) * P^(-1) = B

And you can multiply both sides by P on the right to get:

A^(-1) * P^(-1)*P = B*P
A^(-1) * I = B*P
A^(-1) = B*P
Engineero
  • 12,340
  • 5
  • 53
  • 75
  • What is B ? Row echelon form of matrix A? – e0n Jun 09 '13 at 17:38
  • B would be the reduced row echelon for after you have gotten A->I through some permutations. Since you have switched rows around thought, this is not your inverse yet. You still need to multiply by your permutation matrix to undo all the switching you have done. – Engineero Jun 09 '13 at 17:49
  • Ok are you sure that this one works [A]^(-1) = [B]*[P] , cause it looks fishy to me tbh. – e0n Jun 09 '13 at 18:04
  • it doesn't work like that, imagine that your GEPP is so fortunate that u dont need to perform row swapping, so your permutation matrix is just Identity matrix, so u claim that A^(-1) = B*I (which is B) it doesn't make sense... – e0n Jun 09 '13 at 19:12
  • Sure it does. In the situation you are describing, you would have [A|I] --> GE --> [I|B], or A^{-1} = B. I edited my answer to make this point more clear. – Engineero Jun 09 '13 at 19:14