Diagonally Dominant Matrix In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Diagonally Dominant Matrix In C++

Diagonally Dominant Matrix In C++

BLUF: Mastering Diagonally Dominant Matrix In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Diagonally Dominant Matrix In C++

C++ is renowned for its efficiency. Learn how Diagonally Dominant Matrix In C++ enables low-level control and high-performance computing in the tutorial below.

A matrix earns the label "diagonally dominant" when the total of all elements outside the main diagonal is lower than the sum on the main diagonal. Essentially, in the context of a square matrix, if any element on the main diagonal, running from the top left to the bottom right, is either equal to or greater than the absolute sum of all other elements in each row, it fits this criteria. These matrices hold immense importance and find extensive applications in various fields such as mathematics and engineering, particularly in numerical analysis and linear algebra. In C++, we have the option to define and handle diagonally dominant matrices by utilizing arrays or matrices from external libraries like Eigen or Armadillo, or by employing libraries such as the Standard Template Library.

Example:

A matrix that exhibits diagonal dominance is the following:

Example

832
521
421
8 > 3 + 2
5 > 2 + 1
4 > 2 + 1

The sum of non-diagonal elements in each row is either equal to or greater than the value of the diagonal element in that row.

Example 1:

Let's consider an example to demonstrate the Diagonally dominant matrix in C++.

Example

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

// Function to check if a square matrix is diagonally dominant
bool isDiagonallyDominant(const vector<vector<double>>& matrix) {
    int size = matrix.size();
    for (int i = 0; i < size; ++i) {
        double diagonalValue = abs(matrix[i][i]);
        double rowSum = 0.0;
        for (int j = 0; j < size; ++j) {
            if (j != i) {
                rowSum += abs(matrix[i][j]);
            }
        }
        if (diagonalValue <= rowSum) {
            return false; // Not diagonally dominant
        }
    }
    return true; // Diagonally dominant
}

int main() {
    // Example of a diagonally dominant matrix
    vector<vector<double>> matrix = {
        {10, 2, 3},
        {4, 20, 6},
        {7, 8, 30}
    };

    if (isDiagonallyDominant(matrix)) {
        cout << "The matrix is diagonally dominant." << endl;
    } else {
        cout << "The matrix is not diagonally dominant." << endl;
    }

    return 0;
}

Output:

Output

The matrix is diagonally dominant.

Explanation:

The following code establishes the function isDiagonallyDominant, which takes a 2D vector representing a square matrix as an argument and outputs true if the matrix exhibits diagonal dominance, otherwise false. Within the main function, an illustrative matrix is included to showcase the usage of this function.

Example 2:

Let's consider a different instance to demonstrate the concept of a Diagonally dominant matrix in the C++ programming language.

Example

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

// Function to generate a random diagonally dominant square matrix
vector<vector<double>> generateDiagonallyDominantMatrix(int size) {
    vector<vector<double>> matrix(size, vector<double>(size, 0.0));
    
    // Seed random number generator
    srand(time(0));

    // Generate random matrix elements
    for (int i = 0; i < size; ++i) {
        double diagonalValue = 0.0;
        for (int j = 0; j < size; ++j) {
            if (i != j) {
                matrix[i][j] = static_cast<double>(rand() % 100) + 1; // Random element between 1 and 100
                diagonalValue += abs(matrix[i][j]);
            }
        }
        // Ensure diagonal dominance
        matrix[i][i] = diagonalValue + 1; // Set diagonal element to be greater than sum of off-diagonal elements
    }

    return matrix;
}

// Function to check if a square matrix is diagonally dominant
bool isDiagonallyDominant(const vector<vector<double>>& matrix) {
    int size = matrix.size();
    for (int i = 0; i < size; ++i) {
        double diagonalValue = abs(matrix[i][i]);
        double rowSum = 0.0;
        for (int j = 0; j < size; ++j) {
            if (j != i) {
                rowSum += abs(matrix[i][j]);
            }
        }
        if (diagonalValue <= rowSum) {
            return false; // Not diagonally dominant
        }
    }
    return true; // Diagonally dominant
}

// Function to print a matrix
void printMatrix(const vector<vector<double>>& matrix) {
    for (const auto& row : matrix) {
        for (double element : row) {
            cout << element << "\t";
        }
        cout << endl;
    }
}

int main() {
    int size = 3; // Size of the square matrix
    vector<vector<double>> matrix = generateDiagonallyDominantMatrix(size);

    cout << "Generated Matrix:" << endl;
    printMatrix(matrix);

    if (isDiagonallyDominant(matrix)) {
        cout << "\nThe matrix is diagonally dominant." << endl;
    } else {
        cout << "\nThe matrix is not diagonally dominant." << endl;
    }

    return 0;
}

Output:

Output

Generated Matrix:
46	32	13	
63	121	57	
51	90	142	

The matrix is diagonally dominant.

Explanation:

This software determines whether a square matrix of a specified size is diagonally dominant. It guarantees the diagonal dominance condition by generating random values for the matrix and verifying if the created matrix meets this criterion. Subsequently, the program displays the generated matrix along with the outcome of the diagonally dominant validation.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience