# lu

LU factorization

## Syntax

``[L,U] = lu(A)``
``[L,U,P] = lu(A)``
``[L,U,p] = lu(A,'vector')``
``[L,U,p,q] = lu(A,'vector')``
``[L,U,P,Q,R] = lu(A)``
``[L,U,p,q,R] = lu(A,'vector')``
``lu(A)``

## Description

example

````[L,U] = lu(A)` returns an upper triangular matrix `U` and a matrix `L`, such that ```A = L*U```. Here, `L` is a product of the inverse of the permutation matrix and a lower triangular matrix.```

example

````[L,U,P] = lu(A)` returns an upper triangular matrix `U`, a lower triangular matrix `L`, and a permutation matrix `P`, such that `P*A = L*U`. The syntax `lu(A,'matrix')` is identical.```

example

````[L,U,p] = lu(A,'vector')` returns the permutation information as a vector `p`, such that `A(p,:) = L*U`.```

example

````[L,U,p,q] = lu(A,'vector')` returns the permutation information as two row vectors `p` and `q`, such that `A(p,q) = L*U`.```

example

````[L,U,P,Q,R] = lu(A)` returns an upper triangular matrix `U`, a lower triangular matrix `L`, permutation matrices `P` and `Q`, and a scaling matrix `R`, such that `P*(R\A)*Q = L*U`. The syntax `lu(A,'matrix')` is identical.```

example

````[L,U,p,q,R] = lu(A,'vector')` returns the permutation information in two row vectors `p` and `q`, such that `R(:,p)\A(:,q) = L*U`.```

example

````lu(A)` returns the matrix that contains the strictly lower triangular matrix `L` (the matrix without its unit diagonal) and the upper triangular matrix `U` as submatrices. Thus, `lu(A)` returns the matrix `U + L - eye(size(A))`, where `L` and `U` are defined as ```[L,U,P] = lu(A)```. The matrix `A` must be square.```

## Examples

collapse all

Compute the LU factorization of this matrix. Because the numbers are not symbolic objects, you get floating-point results.

```M = [2 -3 -1; 1/2 1 -1; 0 1 -1]; [L, U] = lu(M)```
```L = 1.0000 0 0 0.2500 1.0000 0 0 0.5714 1.0000 U = 2.0000 -3.0000 -1.0000 0 1.7500 -0.7500 0 0 -0.5714```

Now convert this matrix to a symbolic object, and compute the LU factorization.

```M = sym(M); [L, U] = lu(M)```
```L = [ 1, 0, 0] [ 1/4, 1, 0] [ 0, 4/7, 1] U = [ 2, -3, -1] [ 0, 7/4, -3/4] [ 0, 0, -4/7]```

Return the lower and upper triangular matrices and the permutation matrix by providing three output arguments.

```syms a [L, U, P] = lu(sym([0 0 a; a 2 3; 0 a 2]))```
```L = [ 1, 0, 0] [ 0, 1, 0] [ 0, 0, 1] U = [ a, 2, 3] [ 0, a, 2] [ 0, 0, a] P = 0 1 0 0 0 1 1 0 0```

Return the permutation information as a vector by using the `'vector'` flag.

```syms a A = [0 0 a; a 2 3; 0 a 2]; [L, U, p] = lu(A, 'vector')```
```L = [ 1, 0, 0] [ 0, 1, 0] [ 0, 0, 1] U = [ a, 2, 3] [ 0, a, 2] [ 0, 0, a] p = 2 3 1```

Check that `A(p,:) = L*U` by using `isAlways`.

`isAlways(A(p,:) == L*U)`
```ans = 3×3 logical array 1 1 1 1 1 1 1 1 1```

Restore the permutation matrix `P` from the vector `p`.

```P = zeros(3, 3); for i = 1:3 P(i, p(i)) = 1; end P```
```P = 0 1 0 0 0 1 1 0 0```

Return the permutation information as two vectors `p` and `q`.

```syms a A = [a, 2, 3*a; 2*a, 3, 4*a; 4*a, 5, 6*a]; [L, U, p, q] = lu(A, 'vector')```
```L = [ 1, 0, 0] [ 2, 1, 0] [ 4, 3, 1] U = [ a, 2, 3*a] [ 0, -1, -2*a] [ 0, 0, 0] p = 1 2 3 q = 1 2 3```

Check that `A(p, q) = L*U` by using `isAlways`.

`isAlways(A(p, q) == L*U)`
```ans = 3×3 logical array 1 1 1 1 1 1 1 1 1```

Return the lower and upper triangular matrices, permutation matrices, and scaling matrix.

```syms a A = [0, a; 1/a, 0; 0, 1/5; 0,-1]; [L, U, P, Q, R] = lu(A)```
```L = [ 1, 0, 0, 0] [ 0, 1, 0, 0] [ 0, 1/(5*a), 1, 0] [ 0, -1/a, 0, 1] U = [ 1/a, 0] [ 0, a] [ 0, 0] [ 0, 0] P = 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 1 Q = 1 0 0 1 R = [ 1, 0, 0, 0] [ 0, 1, 0, 0] [ 0, 0, 1, 0] [ 0, 0, 0, 1]```

Check that `P*(R\A)*Q = L*U` by using `isAlways`.

`isAlways(P*(R\A)*Q == L*U)`
```ans = 4×2 logical array 1 1 1 1 1 1 1 1```

Return the permutation information as vectors `p` and `q` by using the `'vector'` flag. Also, compute the scaling matrix `R`.

```syms a A = [0, a; 1/a, 0; 0, 1/5; 0,-1]; [L, U, p, q, R] = lu(A,'vector')```
```L = [ 1, 0, 0, 0] [ 0, 1, 0, 0] [ 0, 1/(5*a), 1, 0] [ 0, -1/a, 0, 1] U = [ 1/a, 0] [ 0, a] [ 0, 0] [ 0, 0] p = 2 1 3 4 q = 1 2 R = [ 1, 0, 0, 0] [ 0, 1, 0, 0] [ 0, 0, 1, 0] [ 0, 0, 0, 1]```

Check that `R(:,p)\A(:,q) = L*U` by using `isAlways`.

`isAlways(R(:,p)\A(:,q) == L*U)`
```ans = 4×2 logical array 1 1 1 1 1 1 1 1```

Return triangular matrices as submatrices by specifying one or no output arguments.

```syms a A = [0 0 a; a 2 3; 0 a 2]; lu(A)```
```ans = [ a, 2, 3] [ 0, a, 2] [ 0, 0, a]```

Verify that the resulting matrix is equal to ```U + L - eye(size(A))```, where `L` and `U` are defined as `[L,U,P] = lu(A)`.

```[L,U,P] = lu(A); U + L - eye(size(A))```
```ans = [ a, 2, 3] [ 0, a, 2] [ 0, 0, a]```

## Input Arguments

collapse all

Input, specified as a numeric or symbolic matrix.

collapse all

### LU Factorization of a Matrix

LU factorization expresses an m-by-n matrix A as P* A = L *U. Here, L is an m-by-m lower triangular matrix, U is an m-by-n upper triangular matrix, and P is a permutation matrix.

### Permutation Vector

The permutation vector `p` contains numbers corresponding to row exchanges in the matrix `A`. For an m-by-m matrix, `p` represents the following permutation matrix with indices i and j ranging from 1 to m.

## Tips

• Calling `lu` for numeric arguments that are not symbolic objects invokes the MATLAB® `lu` function.

• The `thresh` option supported by the MATLAB `lu` function does not affect symbolic inputs.

• If you use `'matrix'` instead of `'vector'`, then `lu` returns permutation matrices, as it does by default.

• `L` and `U` are nonsingular if and only if `A` is nonsingular. `lu` also can compute the LU factorization of a singular matrix `A`. In this case, `L` or `U` is a singular matrix.

• Most algorithms for computing LU factorization are variants of Gaussian elimination.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7281433,"math_prob":0.99987435,"size":1767,"snap":"2020-45-2020-50","text_gpt3_token_len":516,"char_repetition_ratio":0.17583664,"word_repetition_ratio":0.18037975,"special_character_ratio":0.270515,"punctuation_ratio":0.165,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99960905,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-23T06:22:09Z\",\"WARC-Record-ID\":\"<urn:uuid:189abebc-7652-4ff2-a584-cb5cffff507a>\",\"Content-Length\":\"91055\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ccdc87cc-6a19-49f1-9a56-4d748cf3095c>\",\"WARC-Concurrent-To\":\"<urn:uuid:faeba01b-07ee-408d-b42f-e65bb45af6e9>\",\"WARC-IP-Address\":\"23.223.252.57\",\"WARC-Target-URI\":\"https://la.mathworks.com/help/symbolic/lu.html\",\"WARC-Payload-Digest\":\"sha1:XE3XEHDIKBYTEMP72TNZHJOFJIKSY4HQ\",\"WARC-Block-Digest\":\"sha1:GVTVN5TPRTC4TTOIWHB3CJJTULCFQCWL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107880656.25_warc_CC-MAIN-20201023043931-20201023073931-00518.warc.gz\"}"}