Skip to content

Commit a107194

Browse files
authored
Merge pull request #15 from pachadotdev/complex
Complex
2 parents 5e945fc + b5e5e34 commit a107194

File tree

16 files changed

+1378
-9
lines changed

16 files changed

+1378
-9
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.2.9000
3+
Version: 0.6.0.9000
44
Authors@R:
55
c(
66
person("Davis", "Vaughan", email = "davis@posit.co", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-4777-038X")),

cpp11test/R/cpp11.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ grow_ <- function(n) {
8484
.Call(`_cpp11test_grow_`, n)
8585
}
8686

87+
grow_cplx_ <- function(n) {
88+
.Call(`_cpp11test_grow_cplx_`, n)
89+
}
90+
8791
cpp11_insert_ <- function(num_sxp) {
8892
.Call(`_cpp11test_cpp11_insert_`, num_sxp)
8993
}
@@ -297,6 +301,42 @@ sum_dbl_accumulate2_ <- function(x_sxp) {
297301
.Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp)
298302
}
299303

304+
sum_cplx_for_ <- function(x) {
305+
.Call(`_cpp11test_sum_cplx_for_`, x)
306+
}
307+
308+
sum_cplx_for_2_ <- function(x) {
309+
.Call(`_cpp11test_sum_cplx_for_2_`, x)
310+
}
311+
312+
sum_cplx_for_3_ <- function(x_sxp) {
313+
.Call(`_cpp11test_sum_cplx_for_3_`, x_sxp)
314+
}
315+
316+
sum_cplx_for_4_ <- function(x_sxp) {
317+
.Call(`_cpp11test_sum_cplx_for_4_`, x_sxp)
318+
}
319+
320+
sum_cplx_for_5_ <- function(x_sxp) {
321+
.Call(`_cpp11test_sum_cplx_for_5_`, x_sxp)
322+
}
323+
324+
sum_cplx_for_6_ <- function(x_sxp) {
325+
.Call(`_cpp11test_sum_cplx_for_6_`, x_sxp)
326+
}
327+
328+
sum_cplx_foreach_ <- function(x) {
329+
.Call(`_cpp11test_sum_cplx_foreach_`, x)
330+
}
331+
332+
sum_cplx_accumulate_ <- function(x) {
333+
.Call(`_cpp11test_sum_cplx_accumulate_`, x)
334+
}
335+
336+
sum_cplx_for2_ <- function(x_sxp) {
337+
.Call(`_cpp11test_sum_cplx_for2_`, x_sxp)
338+
}
339+
300340
sum_int_for_ <- function(x) {
301341
.Call(`_cpp11test_sum_int_for_`, x)
302342
}

cpp11test/src/cpp11.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ extern "C" SEXP _cpp11test_grow_(SEXP n) {
166166
return cpp11::as_sexp(grow_(cpp11::as_cpp<cpp11::decay_t<R_xlen_t>>(n)));
167167
END_CPP11
168168
}
169+
// grow.cpp
170+
cpp11::writable::complexes grow_cplx_(R_xlen_t n);
171+
extern "C" SEXP _cpp11test_grow_cplx_(SEXP n) {
172+
BEGIN_CPP11
173+
return cpp11::as_sexp(grow_cplx_(cpp11::as_cpp<cpp11::decay_t<R_xlen_t>>(n)));
174+
END_CPP11
175+
}
169176
// insert.cpp
170177
SEXP cpp11_insert_(SEXP num_sxp);
171178
extern "C" SEXP _cpp11test_cpp11_insert_(SEXP num_sxp) {
@@ -499,6 +506,69 @@ extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) {
499506
return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
500507
END_CPP11
501508
}
509+
// sum.cpp
510+
cpp11::r_complex sum_cplx_for_(cpp11::complexes x);
511+
extern "C" SEXP _cpp11test_sum_cplx_for_(SEXP x) {
512+
BEGIN_CPP11
513+
return cpp11::as_sexp(sum_cplx_for_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
514+
END_CPP11
515+
}
516+
// sum.cpp
517+
cpp11::complexes sum_cplx_for_2_(cpp11::complexes x);
518+
extern "C" SEXP _cpp11test_sum_cplx_for_2_(SEXP x) {
519+
BEGIN_CPP11
520+
return cpp11::as_sexp(sum_cplx_for_2_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
521+
END_CPP11
522+
}
523+
// sum.cpp
524+
std::complex<double> sum_cplx_for_3_(cpp11::complexes x_sxp);
525+
extern "C" SEXP _cpp11test_sum_cplx_for_3_(SEXP x_sxp) {
526+
BEGIN_CPP11
527+
return cpp11::as_sexp(sum_cplx_for_3_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x_sxp)));
528+
END_CPP11
529+
}
530+
// sum.cpp
531+
std::complex<double> sum_cplx_for_4_(SEXP x_sxp);
532+
extern "C" SEXP _cpp11test_sum_cplx_for_4_(SEXP x_sxp) {
533+
BEGIN_CPP11
534+
return cpp11::as_sexp(sum_cplx_for_4_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
535+
END_CPP11
536+
}
537+
// sum.cpp
538+
SEXP sum_cplx_for_5_(SEXP x_sxp);
539+
extern "C" SEXP _cpp11test_sum_cplx_for_5_(SEXP x_sxp) {
540+
BEGIN_CPP11
541+
return cpp11::as_sexp(sum_cplx_for_5_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
542+
END_CPP11
543+
}
544+
// sum.cpp
545+
cpp11::complexes sum_cplx_for_6_(SEXP x_sxp);
546+
extern "C" SEXP _cpp11test_sum_cplx_for_6_(SEXP x_sxp) {
547+
BEGIN_CPP11
548+
return cpp11::as_sexp(sum_cplx_for_6_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
549+
END_CPP11
550+
}
551+
// sum.cpp
552+
std::complex<double> sum_cplx_foreach_(cpp11::complexes x);
553+
extern "C" SEXP _cpp11test_sum_cplx_foreach_(SEXP x) {
554+
BEGIN_CPP11
555+
return cpp11::as_sexp(sum_cplx_foreach_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
556+
END_CPP11
557+
}
558+
// sum.cpp
559+
std::complex<double> sum_cplx_accumulate_(cpp11::complexes x);
560+
extern "C" SEXP _cpp11test_sum_cplx_accumulate_(SEXP x) {
561+
BEGIN_CPP11
562+
return cpp11::as_sexp(sum_cplx_accumulate_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
563+
END_CPP11
564+
}
565+
// sum.cpp
566+
std::complex<double> sum_cplx_for2_(SEXP x_sxp);
567+
extern "C" SEXP _cpp11test_sum_cplx_for2_(SEXP x_sxp) {
568+
BEGIN_CPP11
569+
return cpp11::as_sexp(sum_cplx_for2_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
570+
END_CPP11
571+
}
502572
// sum_int.cpp
503573
double sum_int_for_(cpp11::integers x);
504574
extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) {
@@ -630,6 +700,7 @@ static const R_CallMethodDef CallEntries[] = {
630700
{"_cpp11test_gibbs_rcpp", (DL_FUNC) &_cpp11test_gibbs_rcpp, 2},
631701
{"_cpp11test_gibbs_rcpp2", (DL_FUNC) &_cpp11test_gibbs_rcpp2, 2},
632702
{"_cpp11test_grow_", (DL_FUNC) &_cpp11test_grow_, 1},
703+
{"_cpp11test_grow_cplx_", (DL_FUNC) &_cpp11test_grow_cplx_, 1},
633704
{"_cpp11test_mat_mat_copy_dimnames", (DL_FUNC) &_cpp11test_mat_mat_copy_dimnames, 1},
634705
{"_cpp11test_mat_mat_create_dimnames", (DL_FUNC) &_cpp11test_mat_mat_create_dimnames, 0},
635706
{"_cpp11test_mat_sexp_copy_dimnames", (DL_FUNC) &_cpp11test_mat_sexp_copy_dimnames, 1},
@@ -677,6 +748,15 @@ static const R_CallMethodDef CallEntries[] = {
677748
{"_cpp11test_roxcpp7_", (DL_FUNC) &_cpp11test_roxcpp7_, 1},
678749
{"_cpp11test_string_proxy_assignment_", (DL_FUNC) &_cpp11test_string_proxy_assignment_, 0},
679750
{"_cpp11test_string_push_back_", (DL_FUNC) &_cpp11test_string_push_back_, 0},
751+
{"_cpp11test_sum_cplx_accumulate_", (DL_FUNC) &_cpp11test_sum_cplx_accumulate_, 1},
752+
{"_cpp11test_sum_cplx_for2_", (DL_FUNC) &_cpp11test_sum_cplx_for2_, 1},
753+
{"_cpp11test_sum_cplx_for_", (DL_FUNC) &_cpp11test_sum_cplx_for_, 1},
754+
{"_cpp11test_sum_cplx_for_2_", (DL_FUNC) &_cpp11test_sum_cplx_for_2_, 1},
755+
{"_cpp11test_sum_cplx_for_3_", (DL_FUNC) &_cpp11test_sum_cplx_for_3_, 1},
756+
{"_cpp11test_sum_cplx_for_4_", (DL_FUNC) &_cpp11test_sum_cplx_for_4_, 1},
757+
{"_cpp11test_sum_cplx_for_5_", (DL_FUNC) &_cpp11test_sum_cplx_for_5_, 1},
758+
{"_cpp11test_sum_cplx_for_6_", (DL_FUNC) &_cpp11test_sum_cplx_for_6_, 1},
759+
{"_cpp11test_sum_cplx_foreach_", (DL_FUNC) &_cpp11test_sum_cplx_foreach_, 1},
680760
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},
681761
{"_cpp11test_sum_dbl_accumulate_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate_, 1},
682762
{"_cpp11test_sum_dbl_for2_", (DL_FUNC) &_cpp11test_sum_dbl_for2_, 1},

cpp11test/src/grow.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "cpp11/complexes.hpp"
12
#include "cpp11/doubles.hpp"
23

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

1011
return x;
1112
}
13+
14+
[[cpp11::register]] cpp11::writable::complexes grow_cplx_(R_xlen_t n) {
15+
cpp11::writable::complexes x;
16+
R_xlen_t i = 0;
17+
while (i < n) {
18+
x.push_back(std::complex<double>(i, i));
19+
i++;
20+
}
21+
22+
return x;
23+
}

cpp11test/src/sum.cpp

Lines changed: 109 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,111 @@
5859
const cpp11::doubles x(x_sxp, false);
5960
return std::accumulate(x.cbegin(), x.cend(), 0.);
6061
}
62+
63+
// Pacha: 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+
sum += x[i];
72+
}
73+
74+
return cpp11::r_complex(sum.real(), sum.imag());
75+
}
76+
77+
[[cpp11::register]] cpp11::complexes sum_cplx_for_2_(cpp11::complexes x) {
78+
std::complex<double> sum = {0.0, 0.0};
79+
R_xlen_t n = x.size();
80+
for (R_xlen_t i = 0; i < n; ++i) {
81+
// sum.real(sum.real() + x[i].real());
82+
// sum.imag(sum.imag() + x[i].imag());
83+
sum += x[i];
84+
}
85+
86+
cpp11::writable::complexes result(1);
87+
// result[0] = cpp11::r_complex(sum.real(), sum.imag());
88+
result[0] = sum;
89+
90+
return result;
91+
}
92+
93+
[[cpp11::register]] std::complex<double> sum_cplx_for_3_(cpp11::complexes x_sxp) {
94+
std::complex<double> sum = {0.0, 0.0};
95+
const cpp11::complexes x(x_sxp, false);
96+
R_xlen_t n = x.size();
97+
for (R_xlen_t i = 0; i < n; ++i) {
98+
// sum.real(sum.real() + x[i].real());
99+
// sum.imag(sum.imag() + x[i].imag());
100+
sum += x[i];
101+
}
102+
103+
return sum;
104+
}
105+
106+
[[cpp11::register]] std::complex<double> sum_cplx_for_4_(SEXP x_sxp) {
107+
std::complex<double> sum = {0.0, 0.0};
108+
const cpp11::complexes x(x_sxp, false);
109+
R_xlen_t n = x.size();
110+
for (R_xlen_t i = 0; i < n; ++i) {
111+
// sum.real(sum.real() + x[i].real());
112+
// sum.imag(sum.imag() + x[i].imag());
113+
sum += x[i];
114+
}
115+
116+
return sum;
117+
}
118+
119+
[[cpp11::register]] SEXP sum_cplx_for_5_(SEXP x_sxp) {
120+
std::complex<double> sum = {0.0, 0.0};
121+
const cpp11::complexes x(x_sxp, false);
122+
R_xlen_t n = x.size();
123+
for (R_xlen_t i = 0; i < n; ++i) {
124+
// sum.real(sum.real() + x[i].real());
125+
// sum.imag(sum.imag() + x[i].imag());
126+
sum += x[i];
127+
}
128+
129+
return cpp11::as_sexp(sum);
130+
}
131+
132+
[[cpp11::register]] cpp11::complexes sum_cplx_for_6_(SEXP x_sxp) {
133+
std::complex<double> sum = {0.0, 0.0};
134+
const cpp11::complexes x(x_sxp, false);
135+
R_xlen_t n = x.size();
136+
for (R_xlen_t i = 0; i < n; ++i) {
137+
// sum.real(sum.real() + x[i].real());
138+
// sum.imag(sum.imag() + x[i].imag());
139+
sum += x[i];
140+
}
141+
142+
return cpp11::as_sexp(sum);
143+
}
144+
145+
[[cpp11::register]] std::complex<double> sum_cplx_foreach_(cpp11::complexes x) {
146+
std::complex<double> sum = {0.0, 0.0};
147+
for (const auto&& val : x) {
148+
// sum.real(sum.real() + val.real());
149+
// sum.imag(sum.imag() + val.imag());
150+
sum += val;
151+
}
152+
153+
return sum;
154+
}
155+
156+
[[cpp11::register]] std::complex<double> sum_cplx_accumulate_(cpp11::complexes x) {
157+
return std::accumulate(x.cbegin(), x.cend(), std::complex<double>(0.0, 0.0));
158+
}
159+
160+
[[cpp11::register]] std::complex<double> sum_cplx_for2_(SEXP x_sxp) {
161+
std::complex<double> sum = {0.0, 0.0};
162+
const cpp11::complexes x(x_sxp);
163+
R_xlen_t n = x.size();
164+
for (R_xlen_t i = 0; i < n; ++i) {
165+
sum += x[i];
166+
}
167+
168+
return sum;
169+
}

0 commit comments

Comments
 (0)