Menu Close

LU decomposition Method – C++

LU decomposition is a widely used numerical method in linear algebra for solving systems of linear equations.

Here, we will explore the LU decomposition method and provide a comprehensive C++ program that allows users to input their own matrix.

Understanding this powerful technique can greatly enhance your problem-solving skills in various domains, such as engineering, physics, and computer science.

 

What is LU Decomposition?

LU decomposition factorizes a square matrix into two triangular matrices: an upper triangular matrix (U) and a lower triangular matrix (L).

The decomposition of a matrix A can be represented as A = LU, where L contains the lower triangular elements, U contains the upper triangular elements, and L has ones on its main diagonal.

LU decomposition is useful for solving linear equations, matrix inversion, and calculating determinants.

 

Implementing LU Decomposition

To implement LU decomposition in C++, you can use libraries like Eigen or implement it from scratch. In this article, we’ll demonstrate a basic implementation from scratch.

 

				
					#include <iostream>
#include <vector>

// Perform LU decomposition of a square matrix
void LU_Decomposition(const std::vector<std::vector<double>>& A, std::vector<std::vector<double>>& L, std::vector<std::vector<double>>& U) {
    int n = A.size();
    
    // Initialize L and U matrices
    L.resize(n, std::vector<double>(n, 0.0));
    U.resize(n, std::vector<double>(n, 0.0));
    
    // Perform LU decomposition
    for (int i = 0; i < n; i++) {
        // Calculate upper triangular matrix (U)
        for (int k = i; k < n; k++) {
            double sum = 0.0;
            for (int j = 0; j < i; j++)
                sum += L[i][j] * U[j][k];
            U[i][k] = A[i][k] - sum;
        }
        
        // Calculate lower triangular matrix (L)
        for (int k = i; k < n; k++) {
            if (i == k)
                L[i][i] = 1.0;
            else {
                double sum = 0.0;
                for (int j = 0; j < i; j++)
                    sum += L[k][j] * U[j][i];
                L[k][i] = (A[k][i] - sum) / U[i][i];
            }
        }
    }
}

int main() {
    // Get the matrix size from the user
    int n;
    std::cout << "Enter the size of the square matrix: ";
    std::cin >> n;

    // Get the matrix elements from the user
    std::vector<std::vector<double>> A(n, std::vector<double>(n, 0.0));
    std::cout << "Enter the elements of the matrix:\n";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cin >> A[i][j];
        }
    }

    // Perform LU decomposition
    std::vector<std::vector<double>> L, U;
    LU_Decomposition(A, L, U);

    // Print L and U matrices
    std::cout << "L matrix:\n";
    for (const auto& row : L) {
        for (double val : row)
            std::cout << val << " ";
        std::cout << std::endl;
    }
    
    std::cout << "U matrix:\n";
    for (const auto& row : U) {
        for (double val : row)
            std::cout << val << " ";
        std::cout << std::endl;
    }

    return 0;
}

				
			

More Related Stuff