Skip to content

Commit b1d8858

Browse files
committed
almost complete implementation of r_complex
1 parent aeb1895 commit b1d8858

File tree

13 files changed

+1129
-7
lines changed

13 files changed

+1129
-7
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: cpp11
22
Title: A C++11 Interface for R's C Interface
3-
Version: 0.5.1.9000
3+
Version: 0.6.0.9000
44
Authors@R:
55
c(
66
person("Davis", "Vaughan", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-4777-038X")),

cpp11test/DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ Suggests:
2020
xml2
2121
LazyData: true
2222
Roxygen: list(markdown = TRUE)
23-
RoxygenNote: 7.1.1
23+
RoxygenNote: 7.3.2

cpp11test/R/cpp11.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,30 @@ sum_dbl_accumulate2_ <- function(x_sxp) {
196196
.Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp)
197197
}
198198

199+
sum_cplx_for_ <- function(x) {
200+
.Call(`_cpp11test_sum_cplx_for_`, x)
201+
}
202+
203+
sum_cplx_for_2_ <- function(x) {
204+
.Call(`_cpp11test_sum_cplx_for_2_`, x)
205+
}
206+
207+
sum_cplx_for_3_ <- function(x_sxp) {
208+
.Call(`_cpp11test_sum_cplx_for_3_`, x_sxp)
209+
}
210+
211+
sum_cplx_for_4_ <- function(x_sxp) {
212+
.Call(`_cpp11test_sum_cplx_for_4_`, x_sxp)
213+
}
214+
215+
sum_cplx_for_5_ <- function(x_sxp) {
216+
.Call(`_cpp11test_sum_cplx_for_5_`, x_sxp)
217+
}
218+
219+
sum_cplx_for_6_ <- function(x_sxp) {
220+
.Call(`_cpp11test_sum_cplx_for_6_`, x_sxp)
221+
}
222+
199223
sum_int_for_ <- function(x) {
200224
.Call(`_cpp11test_sum_int_for_`, x)
201225
}

cpp11test/src/cpp11.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,48 @@ extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) {
373373
return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
374374
END_CPP11
375375
}
376+
// sum.cpp
377+
cpp11::r_complex sum_cplx_for_(cpp11::complexes x);
378+
extern "C" SEXP _cpp11test_sum_cplx_for_(SEXP x) {
379+
BEGIN_CPP11
380+
return cpp11::as_sexp(sum_cplx_for_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
381+
END_CPP11
382+
}
383+
// sum.cpp
384+
cpp11::complexes sum_cplx_for_2_(cpp11::complexes x);
385+
extern "C" SEXP _cpp11test_sum_cplx_for_2_(SEXP x) {
386+
BEGIN_CPP11
387+
return cpp11::as_sexp(sum_cplx_for_2_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
388+
END_CPP11
389+
}
390+
// sum.cpp
391+
std::complex<double> sum_cplx_for_3_(cpp11::complexes x_sxp);
392+
extern "C" SEXP _cpp11test_sum_cplx_for_3_(SEXP x_sxp) {
393+
BEGIN_CPP11
394+
return cpp11::as_sexp(sum_cplx_for_3_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x_sxp)));
395+
END_CPP11
396+
}
397+
// sum.cpp
398+
std::complex<double> sum_cplx_for_4_(SEXP x_sxp);
399+
extern "C" SEXP _cpp11test_sum_cplx_for_4_(SEXP x_sxp) {
400+
BEGIN_CPP11
401+
return cpp11::as_sexp(sum_cplx_for_4_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
402+
END_CPP11
403+
}
404+
// sum.cpp
405+
SEXP sum_cplx_for_5_(SEXP x_sxp);
406+
extern "C" SEXP _cpp11test_sum_cplx_for_5_(SEXP x_sxp) {
407+
BEGIN_CPP11
408+
return cpp11::as_sexp(sum_cplx_for_5_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
409+
END_CPP11
410+
}
411+
// sum.cpp
412+
cpp11::complexes sum_cplx_for_6_(SEXP x_sxp);
413+
extern "C" SEXP _cpp11test_sum_cplx_for_6_(SEXP x_sxp) {
414+
BEGIN_CPP11
415+
return cpp11::as_sexp(sum_cplx_for_6_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
416+
END_CPP11
417+
}
376418
// sum_int.cpp
377419
double sum_int_for_(cpp11::integers x);
378420
extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) {
@@ -520,6 +562,12 @@ static const R_CallMethodDef CallEntries[] = {
520562
{"_cpp11test_row_sums", (DL_FUNC) &_cpp11test_row_sums, 1},
521563
{"_cpp11test_string_proxy_assignment_", (DL_FUNC) &_cpp11test_string_proxy_assignment_, 0},
522564
{"_cpp11test_string_push_back_", (DL_FUNC) &_cpp11test_string_push_back_, 0},
565+
{"_cpp11test_sum_cplx_for_", (DL_FUNC) &_cpp11test_sum_cplx_for_, 1},
566+
{"_cpp11test_sum_cplx_for_2_", (DL_FUNC) &_cpp11test_sum_cplx_for_2_, 1},
567+
{"_cpp11test_sum_cplx_for_3_", (DL_FUNC) &_cpp11test_sum_cplx_for_3_, 1},
568+
{"_cpp11test_sum_cplx_for_4_", (DL_FUNC) &_cpp11test_sum_cplx_for_4_, 1},
569+
{"_cpp11test_sum_cplx_for_5_", (DL_FUNC) &_cpp11test_sum_cplx_for_5_, 1},
570+
{"_cpp11test_sum_cplx_for_6_", (DL_FUNC) &_cpp11test_sum_cplx_for_6_, 1},
523571
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},
524572
{"_cpp11test_sum_dbl_accumulate_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate_, 1},
525573
{"_cpp11test_sum_dbl_for2_", (DL_FUNC) &_cpp11test_sum_dbl_for2_, 1},

cpp11test/src/sum.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <numeric>
2+
#include "cpp11/complexes.hpp"
23
#include "cpp11/doubles.hpp"
34

45
[[cpp11::register]] double sum_dbl_for_(cpp11::doubles x) {
@@ -58,3 +59,77 @@
5859
const cpp11::doubles x(x_sxp, false);
5960
return std::accumulate(x.cbegin(), x.cend(), 0.);
6061
}
62+
63+
// Functions for complex data type
64+
65+
[[cpp11::register]] cpp11::r_complex sum_cplx_for_(cpp11::complexes x) {
66+
std::complex<double> sum = {0.0, 0.0};
67+
R_xlen_t n = x.size();
68+
for (R_xlen_t i = 0; i < n; ++i) {
69+
sum.real(sum.real() + x[i].real());
70+
sum.imag(sum.imag() + x[i].imag());
71+
}
72+
73+
return cpp11::r_complex(sum.real(), sum.imag());
74+
}
75+
76+
[[cpp11::register]] cpp11::complexes sum_cplx_for_2_(cpp11::complexes x) {
77+
std::complex<double> sum = {0.0, 0.0};
78+
R_xlen_t n = x.size();
79+
for (R_xlen_t i = 0; i < n; ++i) {
80+
sum.real(sum.real() + x[i].real());
81+
sum.imag(sum.imag() + x[i].imag());
82+
}
83+
84+
cpp11::writable::complexes result(1);
85+
result[0] = cpp11::r_complex(sum.real(), sum.imag());
86+
return result;
87+
}
88+
89+
[[cpp11::register]] std::complex<double> sum_cplx_for_3_(cpp11::complexes x_sxp) {
90+
std::complex<double> sum = {0.0, 0.0};
91+
const cpp11::complexes x(x_sxp, false);
92+
R_xlen_t n = x.size();
93+
for (R_xlen_t i = 0; i < n; ++i) {
94+
sum.real(sum.real() + x[i].real());
95+
sum.imag(sum.imag() + x[i].imag());
96+
}
97+
98+
return sum;
99+
}
100+
101+
[[cpp11::register]] std::complex<double> sum_cplx_for_4_(SEXP x_sxp) {
102+
std::complex<double> sum = {0.0, 0.0};
103+
const cpp11::complexes x(x_sxp, false);
104+
R_xlen_t n = x.size();
105+
for (R_xlen_t i = 0; i < n; ++i) {
106+
sum.real(sum.real() + x[i].real());
107+
sum.imag(sum.imag() + x[i].imag());
108+
}
109+
110+
return sum;
111+
}
112+
113+
[[cpp11::register]] SEXP sum_cplx_for_5_(SEXP x_sxp) {
114+
std::complex<double> sum = {0.0, 0.0};
115+
const cpp11::complexes x(x_sxp, false);
116+
R_xlen_t n = x.size();
117+
for (R_xlen_t i = 0; i < n; ++i) {
118+
sum.real(sum.real() + x[i].real());
119+
sum.imag(sum.imag() + x[i].imag());
120+
}
121+
122+
return cpp11::as_sexp(sum);
123+
}
124+
125+
[[cpp11::register]] cpp11::complexes sum_cplx_for_6_(SEXP x_sxp) {
126+
std::complex<double> sum = {0.0, 0.0};
127+
const cpp11::complexes x(x_sxp, false);
128+
R_xlen_t n = x.size();
129+
for (R_xlen_t i = 0; i < n; ++i) {
130+
sum.real(sum.real() + x[i].real());
131+
sum.imag(sum.imag() + x[i].imag());
132+
}
133+
134+
return cpp11::as_sexp(sum);
135+
}

0 commit comments

Comments
 (0)