Skip to content

Commit b3b0bea

Browse files
authored
Merge pull request #1040 from RcppCore/iss365_follow_up
make sure operations between scalar and matrix don't change rhs
2 parents 85cf95f + 8af61cd commit b3b0bea

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

inst/include/Rcpp/vector/Matrix.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ RCPP_GENERATE_MATRIX_SCALAR_OPERATOR(/)
269269
inline typename traits::enable_if< traits::is_convertible< typename traits::remove_const_and_reference< T >::type, \
270270
typename Matrix<RTYPE, StoragePolicy>::stored_type >::value, Matrix<RTYPE, StoragePolicy> >::type \
271271
operator __OPERATOR__ (const T &lhs, const Matrix<RTYPE, StoragePolicy> &rhs) { \
272-
Vector<RTYPE, StoragePolicy> v = static_cast<const Vector<RTYPE, StoragePolicy> &>(rhs); \
273-
v = lhs __OPERATOR__ v; \
272+
Vector<RTYPE, StoragePolicy> v = lhs __OPERATOR__ static_cast<const Vector<RTYPE, StoragePolicy> &>(rhs); \
274273
v.attr("dim") = Vector<INTSXP>::create(rhs.nrow(), rhs.ncol()); \
275274
return as< Matrix<RTYPE, StoragePolicy> >(v); \
276275
}

inst/tinytest/cpp/Matrix.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,20 @@ NumericMatrix matrix_scalar_plus2(const NumericMatrix & x, double y) {
337337
return y + x;
338338
}
339339

340+
// [[Rcpp::export]]
341+
NumericMatrix matrix_scalar_plus3(const NumericMatrix & x, double y) {
342+
NumericMatrix z(x.rows(), x.cols());
343+
z = x + y;
344+
return x;
345+
}
346+
347+
// [[Rcpp::export]]
348+
NumericMatrix matrix_scalar_plus4(const NumericMatrix & x, double y) {
349+
NumericMatrix z(x.rows(), x.cols());
350+
z = y + x;
351+
return x;
352+
}
353+
340354
// [[Rcpp::export]]
341355
NumericMatrix matrix_scalar_divide(const NumericMatrix & x, double y) {
342356
return x / y;
@@ -347,6 +361,20 @@ NumericMatrix matrix_scalar_divide2(const NumericMatrix & x, double y) {
347361
return y / x;
348362
}
349363

364+
// [[Rcpp::export]]
365+
NumericMatrix matrix_scalar_divide3(const NumericMatrix & x, double y) {
366+
NumericMatrix z(x.rows(), x.cols());
367+
z = x / y;
368+
return x;
369+
}
370+
371+
// [[Rcpp::export]]
372+
NumericMatrix matrix_scalar_divide4(const NumericMatrix & x, double y) {
373+
NumericMatrix z(x.rows(), x.cols());
374+
z = y / x;
375+
return x;
376+
}
377+
350378
// 24 October 2016
351379
// eye, ones, and zeros static member functions
352380

inst/tinytest/test_matrix.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,13 @@ expect_equal(transposeCharacter(M), t(M), info="character transpose with row and
213213
M <- matrix(c(1:12), 3, 4)
214214
expect_equal(matrix_scalar_plus(M, 2), M + 2, info="matrix + scalar")
215215
expect_equal(matrix_scalar_plus2(M, 2), 2 + M, info="scalar + matrix")
216+
expect_equal(matrix_scalar_plus3(M, 2), M, info="matrix + scalar should not change input matrix")
217+
expect_equal(matrix_scalar_plus4(M, 2), M, info="scalar + matrix should not change input matrix")
218+
216219
expect_equal(matrix_scalar_divide(M, 2), M / 2, info="matrix / scalar")
217220
expect_equal(matrix_scalar_divide2(M, 2), 2 / M, info="scalar / matrix")
221+
expect_equal(matrix_scalar_divide3(M, 2), M, info="matrix / scalar should not change input matrix")
222+
expect_equal(matrix_scalar_divide4(M, 2), M, info="scalar / matrix should not change input matrix")
218223

219224
## 23 October 2016
220225
## eye function

0 commit comments

Comments
 (0)