Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solve *sparse* upper triangular system

If I want to solve a full upper triangular system, I can call linsolve(A,b,'UT'). However this currently is not supported for sparse matrices. How can I overcome this?

like image 468
olamundo Avatar asked Oct 07 '12 00:10

olamundo


People also ask

How do you solve for the upper triangular system?

If U is an n × n upper-triangular matrix, we know how to solve the linear system Ux = b using back substitution. In fact, this is the final step in the Gaussian elimination algorithm that we discussed in Chapter 2. Compute the value of xn = bn/unn, and then insert this value into equation (n − 1) to solve for xn−1.

Is upper triangular matrix sparse?

The Upper triangular regular sparse matrix is where all the elements below the main diagonal are zero value. The following matrix is an Upper triangular regular sparse matrix. In an upper triangular regular sparse matrix, the non-zero elements are stored in a 1-dimensional array column by column.

What is a 2x2 upper triangular matrix?

In other words, a square matrix is upper triangular if all its entries below the main diagonal are zero. Example of a 2 × 2 upper triangular matrix: A square matrix with elements sij = 0 for j > i is termed lower triangular matrix.

What is upper triangular matrix 3x3?

The upper triangular matrix has all the elements below the main diagonal as zero. Also, the matrix which has elements above the main diagonal as zero is called a lower triangular matrix. Upper Triangular Matrix (U)


1 Answers

UT and LT systems are amongst the easiest systems to solve. Have a read on the wiki about it. Knowing this, it is easy to write your own UT or LT solver:

%# some example data
A = sparse( triu(rand(100)) );
b = rand(100,1);

%# solve UT system by back substitution    
x = zeros(size(b));
for n = size(A,1):-1:1    
    x(n) = (b(n) - A(n,n+1:end)*x(n+1:end) ) / A(n,n);    
end

The procedure is quite similar for LT systems.

Having said that, it is generally much easier and faster to use Matlab's backslash operator:

x = A\b

which also works for spares matrices, as nate already indicated.

Note that this operator also solves UT systems which have non-square A or if A has some elements equal to zero (or < eps) on the main diagonal. It solves these cases in a least-squares sense, which may or may not be desirable for you. You could check for these cases before carrying out the solve:

if size(A,1)==size(A,2) && all(abs(diag(A)) > eps)
    x = A\b;
else
    %# error, warning, whatever you want
end

Read more about the (back)slash operator by typing

>> help \

or

>> help slash

on the Matlab command prompt.

like image 123
Rody Oldenhuis Avatar answered Sep 28 '22 07:09

Rody Oldenhuis