lu
lu(A) -> L, U, p
Compute the LU factorization of A
, such that A[p,:] = L*U
.
Examples
- Compute the LU factorization of a matrix:
julia> A = [1 2 3; 4 5 6; 7 8 10]; julia> L, U, p = lu(A); julia> L 3×3 Array{Float64,2}: 1.0 0.0 0.0 0.571429 1.0 0.0 0.142857 0.5 1.0
julia> U 3×3 Array{Float64,2}: 7.0 8.0 10.0 0.0 0.857143 1.71429 0.0 0.0 -0.0
julia> p 3-element Array{Int64,1}: 3 2 1
This example computes the LU factorization of matrix `A` and assigns the lower triangular matrix `L`, upper triangular matrix `U`, and permutation vector `p` to the respective variables.
2. **Solve a system of linear equations using LU factorization:**
```julia
julia> A = [1 2 3; 4 5 6; 7 8 10];
julia> b = [3, 6, 9];
julia> L, U, p = lu(A);
julia> x = U \ (L \ (b[p]));
julia> x
3-element Array{Float64,1}:
-1.9999999999999996
2.0
1.0
In this example, LU factorization is used to solve a system of linear equations Ax = b
. The solution vector x
is computed by first permuting b
using p
, then applying forward and backward substitutions using L
and U
.
Common mistake example:
julia> A = [1 2 3; 4 5 6];
julia> L, U, p = lu(A);
ERROR: ArgumentError: input matrix must be square
Here, the LU factorization is attempted on a non-square matrix A
. The lu
function requires the input matrix to be square. Make sure to provide a square matrix to the lu
function to avoid this error.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.