Skip to content

Commit d56d772

Browse files
committed
v0.5.0 simple support for dgCMatrix
1 parent cab5894 commit d56d772

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+854
-70
lines changed

CRAN-SUBMISSION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Version: 0.4.6
2-
Date: 2025-03-21 00:48:36 UTC
3-
SHA: 977c5b8118b83eee3353d70253d392d2cbab10c2
2+
Date: 2025-03-21 05:46:18 UTC
3+
SHA: cab5894a6e7270b6cc626a6b74930c42d21d024e

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Description: Provides function declarations and inline function definitions that
66
linear algebra and scientific computing. This implementation is detailed
77
in Vargas Sepulveda and Schneider Malamud (2024)
88
<doi:10.1016/j.softx.2025.102087>.
9-
Version: 0.4.6
9+
Version: 0.5.0
1010
Authors@R: c(
1111
person(
1212
given = "Mauricio",

cpp11armadillotest/R/cpp11.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,3 +1007,11 @@ ols_ <- function(x, y) {
10071007
poisson_ <- function(x, y) {
10081008
.Call(`_cpp11armadillotest_poisson_`, x, y)
10091009
}
1010+
1011+
test_dgCMatrix_to_SpMat <- function(x) {
1012+
.Call(`_cpp11armadillotest_test_dgCMatrix_to_SpMat`, x)
1013+
}
1014+
1015+
sum_matrices_ <- function(x) {
1016+
.Call(`_cpp11armadillotest_sum_matrices_`, x)
1017+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "00_main.h"
2+
3+
[[cpp11::register]] SEXP test_dgCMatrix_to_SpMat(SEXP x) {
4+
// Convert from dgCMatrix to SpMat
5+
SpMat<double> A = as_SpMat(x);
6+
7+
// Optional: perform some operation on A
8+
// For example, multiply each element by 2
9+
A *= 1.0;
10+
11+
// Convert back to dgCMatrix and return
12+
return as_dgCMatrix(A);
13+
}
14+
15+
// [[cpp11::register]] SEXP sum_matrices_(SEXP x) {
16+
// // Convert from dgCMatrix to SpMat
17+
// SpMat<double> A = as_SpMat(x);
18+
19+
// // Create a matrix B with a diagonal of random numbers
20+
// SpMat<double> B(A.n_rows, A.n_cols);
21+
// for (uword i = 0; i < A.n_rows; ++i) {
22+
// B(i, i) = randu<double>();
23+
// }
24+
25+
// A += B; // Add the two matrices
26+
27+
// // Convert back to dgCMatrix and return
28+
// return as_dgCMatrix(A);
29+
// }

cpp11armadillotest/src/cpp11.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,20 @@ extern "C" SEXP _cpp11armadillotest_poisson_(SEXP x, SEXP y) {
17691769
return cpp11::as_sexp(poisson_(cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(x), cpp11::as_cpp<cpp11::decay_t<const doubles&>>(y)));
17701770
END_CPP11
17711771
}
1772+
// 10_sparse_matrices.cpp
1773+
SEXP test_dgCMatrix_to_SpMat(SEXP x);
1774+
extern "C" SEXP _cpp11armadillotest_test_dgCMatrix_to_SpMat(SEXP x) {
1775+
BEGIN_CPP11
1776+
return cpp11::as_sexp(test_dgCMatrix_to_SpMat(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
1777+
END_CPP11
1778+
}
1779+
// 10_sparse_matrices.cpp
1780+
SEXP sum_matrices_(SEXP x);
1781+
extern "C" SEXP _cpp11armadillotest_sum_matrices_(SEXP x) {
1782+
BEGIN_CPP11
1783+
return cpp11::as_sexp(sum_matrices_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
1784+
END_CPP11
1785+
}
17721786

17731787
extern "C" {
17741788
static const R_CallMethodDef CallEntries[] = {
@@ -1989,6 +2003,7 @@ static const R_CallMethodDef CallEntries[] = {
19892003
{"_cpp11armadillotest_subview2_", (DL_FUNC) &_cpp11armadillotest_subview2_, 1},
19902004
{"_cpp11armadillotest_sum1_", (DL_FUNC) &_cpp11armadillotest_sum1_, 1},
19912005
{"_cpp11armadillotest_sum2_", (DL_FUNC) &_cpp11armadillotest_sum2_, 1},
2006+
{"_cpp11armadillotest_sum_matrices_", (DL_FUNC) &_cpp11armadillotest_sum_matrices_, 1},
19922007
{"_cpp11armadillotest_svd1_", (DL_FUNC) &_cpp11armadillotest_svd1_, 1},
19932008
{"_cpp11armadillotest_svd_econ1_", (DL_FUNC) &_cpp11armadillotest_svd_econ1_, 1},
19942009
{"_cpp11armadillotest_svds1_", (DL_FUNC) &_cpp11armadillotest_svds1_, 2},
@@ -1997,6 +2012,7 @@ static const R_CallMethodDef CallEntries[] = {
19972012
{"_cpp11armadillotest_swap_rows1_", (DL_FUNC) &_cpp11armadillotest_swap_rows1_, 1},
19982013
{"_cpp11armadillotest_syl1_", (DL_FUNC) &_cpp11armadillotest_syl1_, 3},
19992014
{"_cpp11armadillotest_symmatu1_", (DL_FUNC) &_cpp11armadillotest_symmatu1_, 1},
2015+
{"_cpp11armadillotest_test_dgCMatrix_to_SpMat", (DL_FUNC) &_cpp11armadillotest_test_dgCMatrix_to_SpMat, 1},
20002016
{"_cpp11armadillotest_toeplitz1_", (DL_FUNC) &_cpp11armadillotest_toeplitz1_, 1},
20012017
{"_cpp11armadillotest_trace1_", (DL_FUNC) &_cpp11armadillotest_trace1_, 1},
20022018
{"_cpp11armadillotest_trans1_", (DL_FUNC) &_cpp11armadillotest_trans1_, 1},
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test_that("sparse matrices copy works", {
2+
M <- Matrix::Matrix(c(0, 0, 0, 2, 6, 0, -1, 5, 0, 4, 3, 0, 0, 0, 5, 0),
3+
nrow = 4, ncol = 4, sparse = TRUE
4+
)
5+
6+
N <- test_dgCMatrix_to_SpMat(M)
7+
8+
expect_equal(N, M)
9+
})

0 commit comments

Comments
 (0)