How to resolve the algorithm LU decomposition step by step in the EchoLisp programming language
How to resolve the algorithm LU decomposition step by step in the EchoLisp programming language
Table of Contents
Problem Statement
Every square matrix
A
{\displaystyle A}
can be decomposed into a product of a lower triangular matrix
L
{\displaystyle L}
and a upper triangular matrix
U
{\displaystyle U}
, as described in LU decomposition. It is a modified form of Gaussian elimination. While the Cholesky decomposition only works for symmetric, positive definite matrices, the more general LU decomposition works for any square matrix. There are several algorithms for calculating L and U. To derive Crout's algorithm for a 3x3 example, we have to solve the following system: We now would have to solve 9 equations with 12 unknowns. To make the system uniquely solvable, usually the diagonal elements of
L
{\displaystyle L}
are set to 1 so we get a solvable system of 9 unknowns and 9 equations. Solving for the other
l
{\displaystyle l}
and
u
{\displaystyle u}
, we get the following equations: and for
l
{\displaystyle l}
: We see that there is a calculation pattern, which can be expressed as the following formulas, first for
U
{\displaystyle U}
and then for
L
{\displaystyle L}
We see in the second formula that to get the
l
i j
{\displaystyle l_{ij}}
below the diagonal, we have to divide by the diagonal element (pivot)
u
j j
{\displaystyle u_{jj}}
, so we get problems when
u
j j
{\displaystyle u_{jj}}
is either 0 or very small, which leads to numerical instability. The solution to this problem is pivoting
A
{\displaystyle A}
, which means rearranging the rows of
A
{\displaystyle A}
, prior to the
L U
{\displaystyle LU}
decomposition, in a way that the largest element of each column gets onto the diagonal of
A
{\displaystyle A}
. Rearranging the rows means to multiply
A
{\displaystyle A}
by a permutation matrix
P
{\displaystyle P}
: Example: The decomposition algorithm is then applied on the rearranged matrix so that
The task is to implement a routine which will take a square nxn matrix
A
{\displaystyle A}
and return a lower triangular matrix
L
{\displaystyle L}
, a upper triangular matrix
U
{\displaystyle U}
and a permutation matrix
P
{\displaystyle P}
, so that the above equation is fulfilled. You should then test it on the following two examples and include your output.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm LU decomposition step by step in the EchoLisp programming language
Source code in the echolisp programming language
(lib 'matrix) ;; the matrix library provides LU-decomposition
(decimals 5)
(define A (list->array' (1 3 5 2 4 7 1 1 0 ) 3 3))
(define PLU (matrix-lu-decompose A)) ;; -> list of three matrices, P, Lower, Upper
(array-print (first PLU))
0 1 0
1 0 0
0 0 1
(array-print (second PLU))
1 0 0
0.5 1 0
0.5 -1 1
(array-print (caddr PLU))
2 4 7
0 1 1.5
0 0 -2
(define A (list->array '(11 9 24 2 1 5 2 6 3 17 18 1 2 5 7 1 ) 4 4))
(define PLU (matrix-lu-decompose A)) ;; -> list of three matrices, P, Lower, Upper
(array-print (first PLU))
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
(array-print (second PLU))
1 0 0 0
0.27273 1 0 0
0.09091 0.2875 1 0
0.18182 0.23125 0.0036 1
(array-print (caddr PLU))
11 9 24 2
0 14.54545 11.45455 0.45455
0 0 -3.475 5.6875
0 0 0 0.51079
You may also check:How to resolve the algorithm Leap year step by step in the MIPS Assembly programming language
You may also check:How to resolve the algorithm GUI component interaction step by step in the Ada programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Erlang programming language
You may also check:How to resolve the algorithm Burrows–Wheeler transform step by step in the jq programming language
You may also check:How to resolve the algorithm Word search step by step in the Python programming language