ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Julia Matrix Decomposition: LU Factorization

Julia

Goal -- WPM

Ready
Exercise Algorithm Area
1function lu_decomposition_pivot(A::Matrix{Float64})
2n, m = size(A)
3if n != m
4error("Matrix must be square for LU decomposition.")
5end
6
7# Initialize L and U matrices
8L = Matrix{Float64}(undef, n, n)
9U = copy(A)
10P = Matrix{Float64}(undef, n, n)
11for i in 1:n
12P[i, i] = 1.0
13end
14
15for j in 1:n # Column index
16# Partial Pivoting: Find the pivot row
17pivot_row = j
18max_val = abs(U[j, j])
19for i in (j + 1):n
20if abs(U[i, j]) > max_val
21max_val = abs(U[i, j])
22pivot_row = i
23end
24end
25
26# Swap rows if necessary
27if pivot_row != j
28# Swap rows in U
29U[[j, pivot_row], :] = U[[pivot_row, j], :]
30# Swap corresponding rows in P
31P[[j, pivot_row], :] = P[[pivot_row, j], :]
32end
33
34# Check for singularity (or near singularity)
35if abs(U[j, j]) < 1e-10 # Using a small tolerance
36error("Matrix is singular or nearly singular.")
37end
38
39# Fill L matrix and perform row operations on U
40for i in (j + 1):n
41# Calculate multiplier
42multiplier = U[i, j] / U[j, j]
43L[i, j] = multiplier
44# Update row i in U
45U[i, j:end] -= multiplier * U[j, j:end]
46end
47end
48
49# Fill diagonal of L with 1s
50for i in 1:n
51L[i, i] = 1.0
52end
53
54# Set elements of L above diagonal to 0
55L[tril(L, -1) .== 0] = 0.0
56
57return P, L, U
58end
59
60# Helper to reconstruct A from P, L, U
61function reconstruct_matrix(P, L, U)
62return P' * L * U
63end
Algorithm description viewbox

Julia Matrix Decomposition: LU Factorization

Algorithm description:

This Julia function performs LU decomposition with partial pivoting on a square matrix. LU decomposition factors a matrix A into three matrices: P (a permutation matrix), L (a lower triangular matrix), and U (an upper triangular matrix), such that PA = LU. This is a fundamental technique in linear algebra used for solving systems of linear equations, inverting matrices, and calculating determinants efficiently.

Algorithm explanation:

The `lu_decomposition_pivot` function implements Gaussian elimination with partial pivoting to achieve LU decomposition. It iterates through columns, selecting the row with the largest absolute value in the current column (partial pivoting) to ensure numerical stability and swapping rows if necessary. This pivoting is tracked by the permutation matrix `P`. For each column `j`, it then eliminates the entries below the diagonal in `U` by subtracting multiples of row `j` from subsequent rows, storing the multipliers in `L`. The time complexity is O(n^3) due to the three nested loops, and the space complexity is O(n^2) for storing L, U, and P. Edge cases include non-square matrices and singular matrices, which are handled by errors.

Pseudocode:

function lu_decomposition_pivot(A):
  n, m = size(A)
  if n != m: error "Matrix must be square"

  L = identity_matrix(n)
  U = copy(A)
  P = identity_matrix(n)

  for j from 1 to n: // column
    find pivot_row in column j from row j downwards
    if abs(U[pivot_row, j]) < tolerance: error "Matrix is singular"

    if pivot_row != j:
      swap row j and pivot_row in U
      swap row j and pivot_row in P

    for i from j + 1 to n: // rows below pivot
      multiplier = U[i, j] / U[j, j]
      L[i, j] = multiplier
      U[i, j:end] = U[i, j:end] - multiplier * U[j, j:end]

  return P, L, U