Skip to content

Commit 814b408

Browse files
committed
implement @stephematician suggestion
1 parent b919f41 commit 814b408

File tree

4 files changed

+58
-36
lines changed

4 files changed

+58
-36
lines changed

cpp11test/R/cpp11.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,16 @@ col_sums <- function(x) {
112112
.Call(`_cpp11test_col_sums`, x)
113113
}
114114

115-
log_mat_mat <- function(x) {
116-
.Call(`_cpp11test_log_mat_mat`, x)
115+
mat_mat_copy_dimnames <- function(x) {
116+
.Call(`_cpp11test_mat_mat_copy_dimnames`, x)
117117
}
118118

119-
log_mat_sexp <- function(x) {
120-
.Call(`_cpp11test_log_mat_sexp`, x)
119+
mat_sexp_copy_dimnames <- function(x) {
120+
.Call(`_cpp11test_mat_sexp_copy_dimnames`, x)
121+
}
122+
123+
mat_mat_create_dimnames <- function() {
124+
.Call(`_cpp11test_mat_mat_create_dimnames`)
121125
}
122126

123127
protect_one_ <- function(x, n) {

cpp11test/src/cpp11.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,24 @@ extern "C" SEXP _cpp11test_col_sums(SEXP x) {
216216
END_CPP11
217217
}
218218
// matrix.cpp
219-
cpp11::doubles_matrix<> log_mat_mat(cpp11::doubles_matrix<> x);
220-
extern "C" SEXP _cpp11test_log_mat_mat(SEXP x) {
219+
cpp11::doubles_matrix<> mat_mat_copy_dimnames(cpp11::doubles_matrix<> x);
220+
extern "C" SEXP _cpp11test_mat_mat_copy_dimnames(SEXP x) {
221221
BEGIN_CPP11
222-
return cpp11::as_sexp(log_mat_mat(cpp11::as_cpp<cpp11::decay_t<cpp11::doubles_matrix<>>>(x)));
222+
return cpp11::as_sexp(mat_mat_copy_dimnames(cpp11::as_cpp<cpp11::decay_t<cpp11::doubles_matrix<>>>(x)));
223223
END_CPP11
224224
}
225225
// matrix.cpp
226-
SEXP log_mat_sexp(cpp11::doubles_matrix<> x);
227-
extern "C" SEXP _cpp11test_log_mat_sexp(SEXP x) {
226+
SEXP mat_sexp_copy_dimnames(cpp11::doubles_matrix<> x);
227+
extern "C" SEXP _cpp11test_mat_sexp_copy_dimnames(SEXP x) {
228228
BEGIN_CPP11
229-
return cpp11::as_sexp(log_mat_sexp(cpp11::as_cpp<cpp11::decay_t<cpp11::doubles_matrix<>>>(x)));
229+
return cpp11::as_sexp(mat_sexp_copy_dimnames(cpp11::as_cpp<cpp11::decay_t<cpp11::doubles_matrix<>>>(x)));
230+
END_CPP11
231+
}
232+
// matrix.cpp
233+
cpp11::doubles_matrix<> mat_mat_create_dimnames();
234+
extern "C" SEXP _cpp11test_mat_mat_create_dimnames() {
235+
BEGIN_CPP11
236+
return cpp11::as_sexp(mat_mat_create_dimnames());
230237
END_CPP11
231238
}
232239
// protect.cpp
@@ -502,8 +509,9 @@ static const R_CallMethodDef CallEntries[] = {
502509
{"_cpp11test_gibbs_rcpp", (DL_FUNC) &_cpp11test_gibbs_rcpp, 2},
503510
{"_cpp11test_gibbs_rcpp2", (DL_FUNC) &_cpp11test_gibbs_rcpp2, 2},
504511
{"_cpp11test_grow_", (DL_FUNC) &_cpp11test_grow_, 1},
505-
{"_cpp11test_log_mat_mat", (DL_FUNC) &_cpp11test_log_mat_mat, 1},
506-
{"_cpp11test_log_mat_sexp", (DL_FUNC) &_cpp11test_log_mat_sexp, 1},
512+
{"_cpp11test_mat_mat_copy_dimnames", (DL_FUNC) &_cpp11test_mat_mat_copy_dimnames, 1},
513+
{"_cpp11test_mat_mat_create_dimnames", (DL_FUNC) &_cpp11test_mat_mat_create_dimnames, 0},
514+
{"_cpp11test_mat_sexp_copy_dimnames", (DL_FUNC) &_cpp11test_mat_sexp_copy_dimnames, 1},
507515
{"_cpp11test_my_message", (DL_FUNC) &_cpp11test_my_message, 2},
508516
{"_cpp11test_my_message_n1", (DL_FUNC) &_cpp11test_my_message_n1, 1},
509517
{"_cpp11test_my_message_n1fmt", (DL_FUNC) &_cpp11test_my_message_n1fmt, 1},

cpp11test/src/matrix.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "cpp11/matrix.hpp"
22
#include "Rmath.h"
33
#include "cpp11/doubles.hpp"
4+
#include "cpp11/list.hpp"
5+
#include "cpp11/strings.hpp"
46
using namespace cpp11;
57

68
[[cpp11::register]] SEXP gibbs_cpp(int N, int thin) {
@@ -105,34 +107,22 @@ using namespace Rcpp;
105107
return sums;
106108
}
107109

108-
[[cpp11::register]] cpp11::doubles_matrix<> log_mat_mat(cpp11::doubles_matrix<> x) {
109-
cpp11::writable::doubles_matrix<> out(x.nrow(), x.ncol());
110-
111-
for (int i = 0; i < x.nrow(); i++) {
112-
for (int j = 0; j < x.ncol(); j++) {
113-
out(i, j) = log(x(i, j));
114-
}
115-
}
110+
[[cpp11::register]] cpp11::doubles_matrix<> mat_mat_copy_dimnames(
111+
cpp11::doubles_matrix<> x) {
112+
cpp11::writable::doubles_matrix<> out = x;
116113

117114
// SEXP dimnames = x.attr("dimnames");
118115
// if (dimnames != R_NilValue) {
119116
// Rf_setAttrib(out.data(), R_DimNamesSymbol, dimnames);
120-
// std::cout << "dimnames set successfully" << std::endl;
121117
// }
122118

123119
out.attr("dimnames") = x.attr("dimnames");
124120

125121
return out;
126122
}
127123

128-
[[cpp11::register]] SEXP log_mat_sexp(cpp11::doubles_matrix<> x) {
129-
cpp11::writable::doubles_matrix<> out(x.nrow(), x.ncol());
130-
131-
for (int i = 0; i < x.nrow(); i++) {
132-
for (int j = 0; j < x.ncol(); j++) {
133-
out(i, j) = log(x(i, j));
134-
}
135-
}
124+
[[cpp11::register]] SEXP mat_sexp_copy_dimnames(cpp11::doubles_matrix<> x) {
125+
cpp11::writable::doubles_matrix<> out = x;
136126

137127
// SEXP dimnames = x.attr("dimnames");
138128
// if (dimnames != R_NilValue) {
@@ -143,3 +133,20 @@ using namespace Rcpp;
143133

144134
return out;
145135
}
136+
137+
[[cpp11::register]] cpp11::doubles_matrix<> mat_mat_create_dimnames() {
138+
cpp11::writable::doubles_matrix<> out(2, 2);
139+
140+
out(0, 0) = 1;
141+
out(0, 1) = 2;
142+
out(1, 0) = 3;
143+
out(1, 1) = 4;
144+
145+
cpp11::writable::list dimnames(2);
146+
dimnames[0] = cpp11::strings({"a", "b"});
147+
dimnames[1] = cpp11::strings({"c", "d"});
148+
149+
out.attr("dimnames") = dimnames;
150+
151+
return out;
152+
}

cpp11test/tests/testthat/test-matrix.R

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ test_that("col_sums gives same result as colSums", {
2424
expect_equal(col_sums(y), colSums(y))
2525
})
2626

27-
test_that("log_mat_mat returns a matrix with colnames and rownames", {
27+
test_that("doubles_matrix<> can return a matrix with colnames and rownames", {
2828
x <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
2929
colnames(x) <- letters[1:2]
3030
rownames(x) <- letters[3:4]
3131

32-
y <- log_mat_mat(x)
33-
z <- log_mat_sexp(x)
34-
r <- log(x)
35-
36-
expect_equal(y, r)
37-
expect_equal(z, r)
32+
y <- mat_mat_copy_dimnames(x)
33+
z <- mat_sexp_copy_dimnames(x)
34+
35+
expect_equal(x, y)
36+
expect_equal(x, z)
37+
38+
r <- mat_mat_create_dimnames()
39+
expect_equal(rownames(r), c("a", "b"))
40+
expect_equal(colnames(r), c("c", "d"))
3841
})

0 commit comments

Comments
 (0)