Short answer: various things I can never remember.
A numeric vector (type vec
) is a column vector (same as type colvec
): Likewise, an integer vector (type ivec
) is a column vector (same as type icolvec
), and an unsigned vector (type uvec
) is a column vector (same as type ucolvec
):
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
//[[Rcpp::export]]
void foo (arma::vec v1_vec, arma::colvec v2_colvec, arma::rowvec v3_rowvec,
arma::ivec v4_ivec, arma::icolvec v5_icolvec, arma::irowvec v6_irowvec,
arma::uvec v7_uvec, arma::ucolvec v8_ucolvec, arma::urowvec v9_urowvec){
Rprintf("v1_vec:\n"); v1_vec.print();
Rprintf("v2_colvec:\n"); v2_colvec.print();
Rprintf("v3_rowvec:\n"); v3_rowvec.print();
Rprintf("v4_ivec:\n"); v4_ivec.print();
Rprintf("v5_icolvec:\n"); v5_icolvec.print();
Rprintf("v6_irowvec:\n"); v6_irowvec.print();
Rprintf("v7_uvec:\n"); v7_uvec.print();
Rprintf("v8_ucolvec:\n"); v8_ucolvec.print();
Rprintf("v9_urowvec:\n"); v9_urowvec.print();
}
v_num <- c(1, 2)
v_int <- c(1L, -2L)
v_uns <- c(1L, 2L)
foo(v_num, v_num, v_num, v_int, v_int, v_int, v_uns, v_uns, v_uns)
## v1_vec:
## 1.0000
## 2.0000
## v2_colvec:
## 1.0000
## 2.0000
## v3_rowvec:
## 1.0000 2.0000
## v4_ivec:
## 1
## -2
## v5_icolvec:
## 1
## -2
## v6_irowvec:
## 1 -2
## v7_uvec:
## 1
## 2
## v8_ucolvec:
## 1
## 2
## v9_urowvec:
## 1 2
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
// [[Rcpp::export]]
void coerce_and_print(int a, double b){
arma::vec a1 = {(double) a};
arma::ivec a2 = {a};
arma::uvec a3 = {(unsigned int) a};
// arma::vec a1 = {a}; // OK but throws warning
// arma::uvec a3 = {a}; // OK but throws warning
arma::vec b1 = {b};
arma::ivec b2 = {(int) b};
arma::uvec b3 = {(unsigned int) b};
// arma::ivec b2 = {b}; // OK but throws warning
Rcpp::NumericVector v1 = {(double) a, b};
Rcpp::IntegerVector v2 = {a, (int) b};
// Rcpp::NumericVector v1 = {a, b}; // OK but throws warning
int c1 = {(int) v1(0)};
double c2 = {v1(0)};
// int c2 = {v1(0)}; // OK but throws warning
// Rprintf can print standard C types
Rprintf("Rprintf Input: a=%d, b=%f\n", a, b);
Rcpp::Rcout << "Rcout for a\n" << a1 << a2 << a3 << "\n";
Rcpp::Rcout << "Rcout for b\n" << b1 << b2 << b3 << "\n";
Rcpp::Rcout << "Rcout for v\n" << "v1: " << v1 << " v2: " << v2 << "\n";
Rcpp::Rcout << "Rcout for c\n" << "c1: " << c1 << " c2: " << c2 << "\n";
Rprintf("Rprintf: c1=%d, c2=%f\n", c1, c2);
Rprintf("Rf_PrintValue v1 \n"); Rf_PrintValue(v1);
Rprintf("Rf_PrintValue a1 \n"); Rf_PrintValue(Rcpp::wrap(a1));
}
coerce_and_print(10L, 4.1)
## Rprintf Input: a=10, b=4.100000
## Rcout for a
## 10.0000
## 10
## 10
##
## Rcout for b
## 4.1000
## 4
## 4
##
## Rcout for v
## v1: 10 4.1 v2: 10 4
## Rcout for c
## c1: 10 c2: 10
## Rprintf: c1=10, c2=10.000000
## Rf_PrintValue v1
## [1] 10.0 4.1
## Rf_PrintValue a1
## [,1]
## [1,] 10
The ampersand (&) dicatates call-by-reference, but this only applies when the input type is as expected; otherwise a copying operation takes place (so that the call is effectively call-by-value):
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
//[[Rcpp::export]]
void foo (arma::vec& v1_, arma::vec& v2_, arma::ivec& v3_, arma::ivec& v4_){
v1_(0) = -9999;
v2_(0) = -9999;
v3_(0) = -9999;
v4_(0) = -9999;
}
v1_num <- c(1, -2)
v2_int <- c(1L, -2L)
v3_num <- c(1, -2)
v4_int <- c(1L, -2L)
foo(v1_num, v2_int, v3_num, v4_int)
v1_num ## Input argument is modified
## [1] -9999 -2
v2_int ## Input argument is not modified; a copy has been made
## [1] 1 -2
v3_num ## Input argument is not modified; a copy has been made
## [1] 1 -2
v4_int ## Input argument is modified
## [1] -9999 -2
setdiff
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace arma;
using namespace Rcpp;
// [[Rcpp::export]]
arma::uvec setdiff_(arma::uvec x, arma::uvec y){
x = arma::unique(x);
y = arma::unique(y);
for (size_t j = 0; j < y.n_elem; j++) {
arma::uvec q1 = arma::find(x == y[j]);
if (!q1.empty()) {
x.shed_row(q1(0));
}
}
return x;
}