isMatrixSymmetric

C++ Function Reference

1 Signature

bool BigDataStatMeth::isMatrixSymmetric(const Eigen::MatrixXd &X, int sample_size=100)

2 Description

Matrix symmetry detection.

3 Parameters

  • X (const Eigen::MatrixXd &)
  • sample_size (int)

4 Returns

Type: bool

5 Caller Graph

Function dependencies

6 Source Code

File: inst/include/hdf5Algebra/matrixEigenDecomposition.hppLines 84-104

inline bool isMatrixSymmetric(const Eigen::MatrixXd& X, int sample_size = 100) {
        if (X.rows() != X.cols()) return false;
        int n = X.rows();
        if (n <= 3) return true;
        if (n <= 50) {
            double max_diff = (X - X.transpose()).cwiseAbs().maxCoeff();
            return max_diff <= 1e-12 * std::max(1.0, X.cwiseAbs().maxCoeff());
        }
        sample_size = std::min(sample_size, n * n / 4);
        Eigen::VectorXd diffs(sample_size);
        int count = 0;
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<> dis(0, n - 1);
        for (int s = 0; s < sample_size && count < sample_size; ++s) {
            int i = dis(gen), j = dis(gen);
            if (i != j) diffs(count++) = std::abs(X(i,j) - X(j,i));
        }
        if (count == 0) return true;
        return diffs.head(count).maxCoeff() <= 1e-12 * std::max(1.0, X.cwiseAbs().maxCoeff());
    }

7 Usage Example

#include "BigDataStatMeth.hpp"

// Example usage
auto result = isMatrixSymmetric(...);