-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiagonal_product.cpp
More file actions
39 lines (31 loc) · 1.3 KB
/
diagonal_product.cpp
File metadata and controls
39 lines (31 loc) · 1.3 KB
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
#include <Rcpp.h>
#include <RcppEigen.h>
using namespace Rcpp;
using namespace Eigen;
// [[Rcpp::depends(RcppEigen)]]
// Function to compute only the diagonal elements of a product of two square sparse matrtices in CSC format
// [[Rcpp::export]]
Eigen::VectorXd diagonal_producto_csc(const Eigen::SparseMatrix<double>& A, const Eigen::SparseMatrix<double>& B) {
// Assure that both matrices have the same dimensions
if (A.rows() != A.cols() || B.rows() != B.cols() || A.rows() != B.rows()) {
throw std::invalid_argument("Matrices must be square and have the same dimensions.");
}
int n = A.rows();
Eigen::VectorXd diagonal(n);
diagonal.setZero(); // Initial values for the diagonal
// Iteration over A columns
for (int k = 0; k < n; ++k) {
// Iterations over the non-zero elements of the k-th column of A
for (Eigen::SparseMatrix<double>::InnerIterator it_A(A, k); it_A; ++it_A) {
int i = it_A.row(); // Row of A in the k-th column
// Iterations over the non-zero elements of the k-th row of B
for (Eigen::SparseMatrix<double>::InnerIterator it_B(B, k); it_B; ++it_B) {
int j = it_B.row(); // Row of B in the k-th column
if (i == j) {
diagonal(i) += it_A.value() * it_B.value();
}
}
}
}
return diagonal;
}