isMatrixSymmetric
C++ Function Reference
1 Signature
bool BigDataStatMeth::isMatrixSymmetric(const Eigen::MatrixXd &X, int sample_size=100)2 Description
Improved matrix symmetry detection for big-omics data.
3 Parameters
X(const Eigen::MatrixXd &): Input matrix to check for symmetrysample_size(int): Number of elements to sample for symmetry check (default 100)
4 Returns
True if matrix is approximately symmetric within tolerance
5 Details
XInput matrix to check for symmetry sample_sizeNumber of elements to sample for symmetry check (default 100) True if matrix is approximately symmetric within tolerance Optimized for large biological matrices with potential noise
6 Caller Graph
7 Source Code
NoteImplementation
File: inst/include/hdf5Algebra/matrixEigenDecomposition.hpp • Lines 101-142
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; // Too small to meaningfully check
// For small matrices, check all elements
if (n <= 50) {
double max_diff = (X - X.transpose()).cwiseAbs().maxCoeff();
double matrix_scale = X.cwiseAbs().maxCoeff();
return max_diff <= 1e-12 * std::max(1.0, matrix_scale);
}
// For large matrices, use optimized sampling with vectorized operations
sample_size = std::min(sample_size, n * n / 4);
// Sample diagonal and off-diagonal elements efficiently
Eigen::VectorXd diffs(sample_size);
int count = 0;
// Random sampling with good coverage
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);
int j = dis(gen);
if (i != j) {
diffs(count++) = std::abs(X(i, j) - X(j, i));
}
}
if (count == 0) return true;
// Use vectorized operations to compute statistics
double max_diff = diffs.head(count).maxCoeff();
double matrix_scale = X.cwiseAbs().maxCoeff();
double tolerance = 1e-12 * std::max(1.0, matrix_scale);
return max_diff <= tolerance;
}8 Usage Example
#include "BigDataStatMeth.hpp"
// Example usage
auto result = isMatrixSymmetric(...);