Diagonally Dominant Matrix In C++

Diagonally dominant is a term given to a matrix if the sum of all elements not on the main diagonal is less than that on the main diagonal. In this case, an integer of a square matrix, if the value of any element on the main diagonal, which stretches from the top left down to the bottom right, is equal to or greater than the absolute value of the sum of all other elements in each row. Such matrices are very significant and have numerous uses in mathematics and engineering, especially in numerical analysis and linear algebra. In C++, we can define and manipulate diagonally dominant matrices using arrays or matrices from external libraries like Eigen or Armadillo , or libraries like the Standard Template Library.

Example:

A matrix with diagonal dominance is the one shown below:

Example

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

The total of the non-diagonal elements is either greater than or equal to the diagonal element for each diagonal element in a row.

Example 1:

Let us take an example to illustrate 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:

This code defines the function isDiagonallyDominant with a 2D vector representing a square matrix as input, which returns true if the matrix is diagonally dominant and false otherwise. An example matrix is provided in the main function to demonstrate its use.

Example 2:

Let us take another example to illustrate the Diagonally dominant matrix in C++.

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 program finds out if a given size random square matrix is diagonally dominant. It ensures the diagonal dominance property by creating random elements for the matrix first and then confirming that the generated matrix satisfies this property. Finally, the generated matrix and the resultant diagonally dominant check are printed.

Input Required

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