2

I am trying to write an algorithm in MatLab which takes as its input a lower triangular matrix. The output should be the inverse of this matrix (which also should be in lower triangular form). I have almost managed to solve this, but one part of my algorithm still leaves me scratching my head. So far I have:

function AI = inverse(A)
n = length(A);
I = eye(n);
AI = zeros(n);
for k = 1:n
    AI(k,k) = (I(k,k) - A(k,1:(k-1))*AI(1:(k-1),k))/A(k,k);
    for i = k+1:n
        AI(i,k) = (I(i,k) - (??????????????))/A(i,i);
    end
end

I have marked with question marks the part I am unsure of. I have tried to find a pattern for this part of the code by writing out the procedure on paper, but I just can't seem to find a proper way to solve this part.

If anyone can help me out, I would be very grateful!

2
  • Why can't you just use inv(A)?
    – user1639464
    Commented Sep 2, 2012 at 20:23
  • @diophantine: Because I am trying to learn the nuts and bolts of algorithms :). And the problem I posted above is an exercise given in my textbook. Of course, I know that I could just use inv(A), but learning how to construct algorithms from scratch is the goal of the course I am currently taking.
    – Kristian
    Commented Sep 2, 2012 at 20:37

3 Answers 3

4

Here is my code to get the inverse of a lower triangular matrix by using row transformation:

function AI = inverse(A)
    len = length(A);
    I  = eye(len);
    M  = [A I];
    for row = 1:len
        M(row,:) = M(row,:)/M(row,row);
        for idx = 1:row-1
            M(row,:) = M(row,:) - M(idx,:)*M(row,idx);
        end
    end
    AI = M(:,len+1:end);
end
0
2

You can see how it's done on Octave's source. This seems to be implemented in different places depending on the class of the matrix. For Float type Diagonal Matrix it's on liboctave/array/fDiagMatrix.cc, for Complex Diagonal matrix it's on liboctave/array/CDiagMatrix.cc, etc...

One of the advantages of free (as in freedom) software is that you are free to study how things are implemented ;)

0
1

Thanks for all the input! I was actually able to find a very nice and easy way to solve this problem today, given that the input is a lower triangular matrix:

function AI = inverse(A)
n = length(A);
I = eye(n);
AI = zeros(n);
for k = 1:n
    for i = 1:n
        AI(k,i) = (I(k,i) - A(k,1:(k-1))*AI(1:(k-1),i))/A(k,k);
    end
end

Not the answer you're looking for? Browse other questions tagged or ask your own question.