Skip to content

Commit fbca4bf

Browse files
authored
allow the syntax Matrix mat(no_init(n,nc)) (#905)
1 parent bec856b commit fbca4bf

File tree

5 files changed

+54
-31
lines changed

5 files changed

+54
-31
lines changed

ChangeLog

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
* inst/include/Rcpp/date_datetime/Date.h: Remove the empty destructor
44
to make g++-9 (prerelease) happy [CRAN request]
55

6+
* inst/unitTests/runit.Matrix.R: Correct typo
7+
8+
2018-09-27 Romain Francois <[email protected]>
9+
10+
* inst/include/Rcpp/vector/Matrix.h: Fix Matrix(no_init(int,int))
11+
constructor
12+
* inst/unitTests/runit.Matrix.R: test for above
13+
* inst/unitTests/cpp/Matrix.cpp: same
14+
615
2018-09-26 Dirk Eddelbuettel <[email protected]>
716

817
* docker/ci/Dockerfile: Set env var RunAllRcppTests=yes

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: Rcpp
22
Title: Seamless R and C++ Integration
3-
Version: 0.12.19
4-
Date: 2018-09-21
3+
Version: 0.12.19.1
4+
Date: 2018-09-27
55
Author: Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey, Qiang Kou,
66
Nathan Russell, Douglas Bates and John Chambers
77
Maintainer: Dirk Eddelbuettel <[email protected]>

inst/include/Rcpp/vector/Matrix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
9090
Matrix& operator=( const SubMatrix<RTYPE>& ) ;
9191

9292
explicit Matrix( const no_init_matrix& obj) {
93-
Storage::set__( Rf_allocMatrix( RTYPE, obj.nrow(), obj.ncol() ) );
93+
VECTOR::set__( Rf_allocMatrix( RTYPE, obj.nrow(), obj.ncol() ) );
9494
}
9595

9696
inline int ncol() const {

inst/unitTests/cpp/Matrix.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,17 @@ NumericMatrix runit_no_init_matrix() {
279279
return x;
280280
}
281281

282+
// [[Rcpp::export]]
283+
NumericMatrix runit_no_init_matrix_ctor() {
284+
NumericMatrix x(no_init(2, 2));
285+
for (int i = 0; i < 4; i++) {
286+
x[i] = i;
287+
}
288+
return x;
289+
}
290+
282291
void runit_const_Matrix_column_set( NumericMatrix::Column& col1, const NumericMatrix::Column& col2 ){
283-
col1 = col2 ;
292+
col1 = col2 ;
284293
}
285294

286295
// [[Rcpp::export]]
@@ -332,7 +341,7 @@ NumericMatrix matrix_scalar_divide2(const NumericMatrix & x, double y) {
332341
return y / x;
333342
}
334343

335-
// 24 October 2016
344+
// 24 October 2016
336345
// eye, ones, and zeros static member functions
337346

338347
// [[Rcpp::export]]

inst/unitTests/runit.Matrix.R

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ if (.runThisTest) {
197197
checkEquals(m, matrix(c(0, 1, 2, 3), nrow = 2))
198198
}
199199

200+
test.NumericMatrix.no.init.ctor <- function() {
201+
m <- runit_no_init_matrix_ctor()
202+
checkEquals(m, matrix(c(0, 1, 2, 3), nrow = 2))
203+
}
204+
200205
test.NumericMatrix.const.Column <- function(){
201206
m <- matrix(as.numeric(1:9), nrow = 3)
202207
res <- runit_const_Matrix_column(m)
@@ -241,7 +246,7 @@ if (.runThisTest) {
241246
colnames(M) <- LETTERS[1:ncol(M)]
242247
checkEquals(transposeCharacter(M), t(M), msg="character transpose with row and colnames")
243248
}
244-
249+
245250
test.Matrix.Scalar.op <- function() {
246251
M <- matrix(c(1:12), 3, 4)
247252
checkEquals(matrix_scalar_plus(M, 2), M + 2, msg="matrix + scalar")
@@ -253,26 +258,26 @@ if (.runThisTest) {
253258
## 23 October 2016
254259
## eye function
255260
test.Matrix.eye <- function() {
256-
261+
257262
checkEquals(
258263
dbl_eye(3),
259264
diag(1.0, 3, 3),
260265
"eye - numeric"
261266
)
262-
267+
263268
checkEquals(
264269
int_eye(3),
265270
diag(1L, 3, 3),
266271
"eye - integer"
267272
)
268-
273+
269274
checkEquals(
270275
cx_eye(3),
271276
diag(1.0 + 0i, 3, 3),
272277
"eye - complex"
273278
)
274-
275-
## diag(TRUE, 3, 3) was registering as
279+
280+
## diag(TRUE, 3, 3) was registering as
276281
## a numeric matrix on Travis for some reason
277282
mat <- matrix(FALSE, 3, 3)
278283
diag(mat) <- TRUE
@@ -282,86 +287,86 @@ if (.runThisTest) {
282287
"eye - logical"
283288
)
284289
}
285-
290+
286291
## ones function
287292
test.Matrix.ones <- function() {
288-
293+
289294
checkEquals(
290295
dbl_ones(3),
291296
matrix(1.0, 3, 3),
292297
"ones - numeric"
293298
)
294-
299+
295300
checkEquals(
296301
int_ones(3),
297302
matrix(1L, 3, 3),
298303
"ones - integer"
299304
)
300-
305+
301306
checkEquals(
302307
cx_ones(3),
303308
matrix(1.0 + 0i, 3, 3),
304309
"ones - complex"
305310
)
306-
311+
307312
checkEquals(
308313
lgl_ones(3),
309314
matrix(TRUE, 3, 3),
310315
"ones - logical"
311316
)
312317
}
313-
318+
314319
## zeros function
315320
test.Matrix.zeros <- function() {
316-
321+
317322
checkEquals(
318323
dbl_zeros(3),
319324
matrix(0.0, 3, 3),
320325
"zeros - numeric"
321326
)
322-
327+
323328
checkEquals(
324329
int_zeros(3),
325330
matrix(0L, 3, 3),
326331
"zeros - integer"
327332
)
328-
333+
329334
checkEquals(
330335
cx_zeros(3),
331336
matrix(0.0 + 0i, 3, 3),
332337
"zeros - complex"
333338
)
334-
339+
335340
checkEquals(
336341
lgl_zeros(3),
337342
matrix(FALSE, 3, 3),
338343
"zeros - logical"
339344
)
340345
}
341-
342-
346+
347+
343348
test.Matrix.diagfill <- function() {
344-
349+
345350
checkEquals(num_diag_fill(diag(1.0, 2, 4), 0.0),
346351
matrix(0.0, 2, 4),
347352
msg = "diagonal fill - case: n < p")
348-
353+
349354
checkEquals(num_diag_fill(diag(1.0, 4, 2), 0.0),
350355
matrix(0.0, 4, 2),
351356
msg = "diagonal fill - case: n > p")
352-
357+
353358
checkEquals(num_diag_fill(diag(1.0, 3, 3), 0.0),
354359
matrix(0.0, 3, 3),
355360
msg = "diagonal fill - case: n = p")
356-
361+
357362
m <- matrix("", 2, 4)
358363
diag(m) <- letters[1:2]
359-
360-
checkEquals(char_diag_fill(m, ""),
364+
365+
checkEquals(char_diag_fill(m, ""),
361366
matrix("", 2, 4),
362367
msg = "diagonal fill - char")
363-
364-
368+
369+
365370
}
366371

367372
}

0 commit comments

Comments
 (0)