Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy complex numbers/vectors/matrices from R to C++ and viceversa #427

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: cpp11
Title: A C++11 Interface for R's C Interface
Version: 0.5.1.9000
Version: 0.6.0.9000
Authors@R:
c(
person("Davis", "Vaughan", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-4777-038X")),
Expand Down
1 change: 1 addition & 0 deletions cpp11test/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
^\.here$
3 changes: 2 additions & 1 deletion cpp11test/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Authors@R:
Description: Provides a test suite and benchmarking code for the 'cpp11' package.
License: MIT + file LICENSE
Encoding: UTF-8
Imports: Rcpp
LinkingTo: Rcpp, cpp11, testthat
Suggests:
covr,
Expand All @@ -20,4 +21,4 @@ Suggests:
xml2
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.3.2
40 changes: 40 additions & 0 deletions cpp11test/R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ grow_ <- function(n) {
.Call(`_cpp11test_grow_`, n)
}

grow_cplx_ <- function(n) {
.Call(`_cpp11test_grow_cplx_`, n)
}

cpp11_insert_ <- function(num_sxp) {
.Call(`_cpp11test_cpp11_insert_`, num_sxp)
}
Expand Down Expand Up @@ -196,6 +200,42 @@ sum_dbl_accumulate2_ <- function(x_sxp) {
.Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp)
}

sum_cplx_for_ <- function(x) {
.Call(`_cpp11test_sum_cplx_for_`, x)
}

sum_cplx_for_2_ <- function(x) {
.Call(`_cpp11test_sum_cplx_for_2_`, x)
}

sum_cplx_for_3_ <- function(x_sxp) {
.Call(`_cpp11test_sum_cplx_for_3_`, x_sxp)
}

sum_cplx_for_4_ <- function(x_sxp) {
.Call(`_cpp11test_sum_cplx_for_4_`, x_sxp)
}

sum_cplx_for_5_ <- function(x_sxp) {
.Call(`_cpp11test_sum_cplx_for_5_`, x_sxp)
}

sum_cplx_for_6_ <- function(x_sxp) {
.Call(`_cpp11test_sum_cplx_for_6_`, x_sxp)
}

sum_cplx_foreach_ <- function(x) {
.Call(`_cpp11test_sum_cplx_foreach_`, x)
}

sum_cplx_accumulate_ <- function(x) {
.Call(`_cpp11test_sum_cplx_accumulate_`, x)
}

sum_cplx_for2_ <- function(x_sxp) {
.Call(`_cpp11test_sum_cplx_for2_`, x_sxp)
}

sum_int_for_ <- function(x) {
.Call(`_cpp11test_sum_int_for_`, x)
}
Expand Down
80 changes: 80 additions & 0 deletions cpp11test/src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ extern "C" SEXP _cpp11test_grow_(SEXP n) {
return cpp11::as_sexp(grow_(cpp11::as_cpp<cpp11::decay_t<R_xlen_t>>(n)));
END_CPP11
}
// grow.cpp
cpp11::writable::complexes grow_cplx_(R_xlen_t n);
extern "C" SEXP _cpp11test_grow_cplx_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(grow_cplx_(cpp11::as_cpp<cpp11::decay_t<R_xlen_t>>(n)));
END_CPP11
}
// insert.cpp
SEXP cpp11_insert_(SEXP num_sxp);
extern "C" SEXP _cpp11test_cpp11_insert_(SEXP num_sxp) {
Expand Down Expand Up @@ -373,6 +380,69 @@ extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) {
return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
END_CPP11
}
// sum.cpp
cpp11::r_complex sum_cplx_for_(cpp11::complexes x);
extern "C" SEXP _cpp11test_sum_cplx_for_(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(sum_cplx_for_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
END_CPP11
}
// sum.cpp
cpp11::complexes sum_cplx_for_2_(cpp11::complexes x);
extern "C" SEXP _cpp11test_sum_cplx_for_2_(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(sum_cplx_for_2_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
END_CPP11
}
// sum.cpp
std::complex<double> sum_cplx_for_3_(cpp11::complexes x_sxp);
extern "C" SEXP _cpp11test_sum_cplx_for_3_(SEXP x_sxp) {
BEGIN_CPP11
return cpp11::as_sexp(sum_cplx_for_3_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x_sxp)));
END_CPP11
}
// sum.cpp
std::complex<double> sum_cplx_for_4_(SEXP x_sxp);
extern "C" SEXP _cpp11test_sum_cplx_for_4_(SEXP x_sxp) {
BEGIN_CPP11
return cpp11::as_sexp(sum_cplx_for_4_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
END_CPP11
}
// sum.cpp
SEXP sum_cplx_for_5_(SEXP x_sxp);
extern "C" SEXP _cpp11test_sum_cplx_for_5_(SEXP x_sxp) {
BEGIN_CPP11
return cpp11::as_sexp(sum_cplx_for_5_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
END_CPP11
}
// sum.cpp
cpp11::complexes sum_cplx_for_6_(SEXP x_sxp);
extern "C" SEXP _cpp11test_sum_cplx_for_6_(SEXP x_sxp) {
BEGIN_CPP11
return cpp11::as_sexp(sum_cplx_for_6_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
END_CPP11
}
// sum.cpp
std::complex<double> sum_cplx_foreach_(cpp11::complexes x);
extern "C" SEXP _cpp11test_sum_cplx_foreach_(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(sum_cplx_foreach_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
END_CPP11
}
// sum.cpp
std::complex<double> sum_cplx_accumulate_(cpp11::complexes x);
extern "C" SEXP _cpp11test_sum_cplx_accumulate_(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(sum_cplx_accumulate_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
END_CPP11
}
// sum.cpp
std::complex<double> sum_cplx_for2_(SEXP x_sxp);
extern "C" SEXP _cpp11test_sum_cplx_for2_(SEXP x_sxp) {
BEGIN_CPP11
return cpp11::as_sexp(sum_cplx_for2_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
END_CPP11
}
// sum_int.cpp
double sum_int_for_(cpp11::integers x);
extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) {
Expand Down Expand Up @@ -488,6 +558,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11test_gibbs_rcpp", (DL_FUNC) &_cpp11test_gibbs_rcpp, 2},
{"_cpp11test_gibbs_rcpp2", (DL_FUNC) &_cpp11test_gibbs_rcpp2, 2},
{"_cpp11test_grow_", (DL_FUNC) &_cpp11test_grow_, 1},
{"_cpp11test_grow_cplx_", (DL_FUNC) &_cpp11test_grow_cplx_, 1},
{"_cpp11test_my_message", (DL_FUNC) &_cpp11test_my_message, 2},
{"_cpp11test_my_message_n1", (DL_FUNC) &_cpp11test_my_message_n1, 1},
{"_cpp11test_my_message_n1fmt", (DL_FUNC) &_cpp11test_my_message_n1fmt, 1},
Expand Down Expand Up @@ -520,6 +591,15 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11test_row_sums", (DL_FUNC) &_cpp11test_row_sums, 1},
{"_cpp11test_string_proxy_assignment_", (DL_FUNC) &_cpp11test_string_proxy_assignment_, 0},
{"_cpp11test_string_push_back_", (DL_FUNC) &_cpp11test_string_push_back_, 0},
{"_cpp11test_sum_cplx_accumulate_", (DL_FUNC) &_cpp11test_sum_cplx_accumulate_, 1},
{"_cpp11test_sum_cplx_for2_", (DL_FUNC) &_cpp11test_sum_cplx_for2_, 1},
{"_cpp11test_sum_cplx_for_", (DL_FUNC) &_cpp11test_sum_cplx_for_, 1},
{"_cpp11test_sum_cplx_for_2_", (DL_FUNC) &_cpp11test_sum_cplx_for_2_, 1},
{"_cpp11test_sum_cplx_for_3_", (DL_FUNC) &_cpp11test_sum_cplx_for_3_, 1},
{"_cpp11test_sum_cplx_for_4_", (DL_FUNC) &_cpp11test_sum_cplx_for_4_, 1},
{"_cpp11test_sum_cplx_for_5_", (DL_FUNC) &_cpp11test_sum_cplx_for_5_, 1},
{"_cpp11test_sum_cplx_for_6_", (DL_FUNC) &_cpp11test_sum_cplx_for_6_, 1},
{"_cpp11test_sum_cplx_foreach_", (DL_FUNC) &_cpp11test_sum_cplx_foreach_, 1},
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},
{"_cpp11test_sum_dbl_accumulate_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate_, 1},
{"_cpp11test_sum_dbl_for2_", (DL_FUNC) &_cpp11test_sum_dbl_for2_, 1},
Expand Down
12 changes: 12 additions & 0 deletions cpp11test/src/grow.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "cpp11/complexes.hpp"
#include "cpp11/doubles.hpp"

[[cpp11::register]] cpp11::writable::doubles grow_(R_xlen_t n) {
Expand All @@ -9,3 +10,14 @@

return x;
}

[[cpp11::register]] cpp11::writable::complexes grow_cplx_(R_xlen_t n) {
cpp11::writable::complexes x;
R_xlen_t i = 0;
while (i < n) {
x.push_back(std::complex<double>(i, i));
i++;
}

return x;
}
109 changes: 109 additions & 0 deletions cpp11test/src/sum.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <numeric>
#include "cpp11/complexes.hpp"
#include "cpp11/doubles.hpp"

[[cpp11::register]] double sum_dbl_for_(cpp11::doubles x) {
Expand Down Expand Up @@ -58,3 +59,111 @@
const cpp11::doubles x(x_sxp, false);
return std::accumulate(x.cbegin(), x.cend(), 0.);
}

// Pacha: Functions for complex data type

[[cpp11::register]] cpp11::r_complex sum_cplx_for_(cpp11::complexes x) {
std::complex<double> sum = {0.0, 0.0};
R_xlen_t n = x.size();
for (R_xlen_t i = 0; i < n; ++i) {
// sum.real(sum.real() + x[i].real());
// sum.imag(sum.imag() + x[i].imag());
sum += x[i];
}

return cpp11::r_complex(sum.real(), sum.imag());
}

[[cpp11::register]] cpp11::complexes sum_cplx_for_2_(cpp11::complexes x) {
std::complex<double> sum = {0.0, 0.0};
R_xlen_t n = x.size();
for (R_xlen_t i = 0; i < n; ++i) {
// sum.real(sum.real() + x[i].real());
// sum.imag(sum.imag() + x[i].imag());
sum += x[i];
}

cpp11::writable::complexes result(1);
// result[0] = cpp11::r_complex(sum.real(), sum.imag());
result[0] = sum;

return result;
}

[[cpp11::register]] std::complex<double> sum_cplx_for_3_(cpp11::complexes x_sxp) {
std::complex<double> sum = {0.0, 0.0};
const cpp11::complexes x(x_sxp, false);
R_xlen_t n = x.size();
for (R_xlen_t i = 0; i < n; ++i) {
// sum.real(sum.real() + x[i].real());
// sum.imag(sum.imag() + x[i].imag());
sum += x[i];
}

return sum;
}

[[cpp11::register]] std::complex<double> sum_cplx_for_4_(SEXP x_sxp) {
std::complex<double> sum = {0.0, 0.0};
const cpp11::complexes x(x_sxp, false);
R_xlen_t n = x.size();
for (R_xlen_t i = 0; i < n; ++i) {
// sum.real(sum.real() + x[i].real());
// sum.imag(sum.imag() + x[i].imag());
sum += x[i];
}

return sum;
}

[[cpp11::register]] SEXP sum_cplx_for_5_(SEXP x_sxp) {
std::complex<double> sum = {0.0, 0.0};
const cpp11::complexes x(x_sxp, false);
R_xlen_t n = x.size();
for (R_xlen_t i = 0; i < n; ++i) {
// sum.real(sum.real() + x[i].real());
// sum.imag(sum.imag() + x[i].imag());
sum += x[i];
}

return cpp11::as_sexp(sum);
}

[[cpp11::register]] cpp11::complexes sum_cplx_for_6_(SEXP x_sxp) {
std::complex<double> sum = {0.0, 0.0};
const cpp11::complexes x(x_sxp, false);
R_xlen_t n = x.size();
for (R_xlen_t i = 0; i < n; ++i) {
// sum.real(sum.real() + x[i].real());
// sum.imag(sum.imag() + x[i].imag());
sum += x[i];
}

return cpp11::as_sexp(sum);
}

[[cpp11::register]] std::complex<double> sum_cplx_foreach_(cpp11::complexes x) {
std::complex<double> sum = {0.0, 0.0};
for (const auto&& val : x) {
// sum.real(sum.real() + val.real());
// sum.imag(sum.imag() + val.imag());
sum += val;
}

return sum;
}

[[cpp11::register]] std::complex<double> sum_cplx_accumulate_(cpp11::complexes x) {
return std::accumulate(x.cbegin(), x.cend(), std::complex<double>(0.0, 0.0));
}

[[cpp11::register]] std::complex<double> sum_cplx_for2_(SEXP x_sxp) {
std::complex<double> sum = {0.0, 0.0};
const cpp11::complexes x(x_sxp);
R_xlen_t n = x.size();
for (R_xlen_t i = 0; i < n; ++i) {
sum += x[i];
}

return sum;
}
Loading
Loading