File: is_sym.cpp

package info (click to toggle)
r-cran-rspectra 0.16-0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 736 kB
  • sloc: cpp: 4,917; ansic: 256; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 1,539 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <RcppEigen.h>

typedef Eigen::Map< Eigen::SparseMatrix<double, Eigen::ColMajor> > MapSpMat;
typedef Eigen::Map< Eigen::SparseMatrix<double, Eigen::RowMajor> > MapSpRMat;


RcppExport SEXP is_sym_dgCMatrix(SEXP mat, SEXP tol)
{
    BEGIN_RCPP

    MapSpMat x = Rcpp::as<MapSpMat>(mat);
    const double eps = Rcpp::as<double>(tol);

    // Early return if not a square matrix
    const int n = x.rows();
    if(x.cols() != n)
        return Rcpp::wrap(false);

    for(int j = 0; j < n; j++)
    {
        for(MapSpMat::InnerIterator it(x, j); it; ++it)
        {
            // Only consider i > j
            const int i = it.row();
            if(i <= j)
                continue;

            if(std::abs(it.value() - x.coeff(j, i)) >= eps)
                return Rcpp::wrap(false);
        }
    }

    return Rcpp::wrap(true);

    END_RCPP
}



RcppExport SEXP is_sym_dgRMatrix(SEXP mat, SEXP tol)
{
    BEGIN_RCPP

    MapSpRMat x = Rcpp::as<MapSpRMat>(mat);
    const double eps = Rcpp::as<double>(tol);

    // Early return if not a square matrix
    const int n = x.rows();
    if(x.cols() != n)
        return Rcpp::wrap(false);

    for(int i = 0; i < n; i++)
    {
        for(MapSpRMat::InnerIterator it(x, i); it; ++it)
        {
            // Only consider i < j
            const int j = it.col();
            if(i >= j)
                continue;

            if(std::abs(it.value() - x.coeff(j, i)) >= eps)
                return Rcpp::wrap(false);
        }
    }

    return Rcpp::wrap(true);

    END_RCPP
}