Skip to content

Commit 1a5c426

Browse files
committed
complexes fix
1 parent 3936bdd commit 1a5c426

File tree

5 files changed

+82
-181
lines changed

5 files changed

+82
-181
lines changed

extended-tests/Rcppbenchmark/src/RcppExports.cpp

Lines changed: 60 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,92 +6,86 @@
66
using namespace Rcpp;
77

88
#ifdef RCPP_USE_GLOBAL_ROSTREAM
9-
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
9+
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
1010
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
1111
#endif
1212

1313
// add_two_rcpp_
1414
NumericMatrix add_two_rcpp_(NumericMatrix a, NumericMatrix b);
1515
RcppExport SEXP _Rcppbenchmark_add_two_rcpp_(SEXP aSEXP, SEXP bSEXP) {
16-
BEGIN_RCPP
17-
Rcpp::RObject rcpp_result_gen;
18-
Rcpp::RNGScope rcpp_rngScope_gen;
19-
Rcpp::traits::input_parameter<NumericMatrix>::type a(aSEXP);
20-
Rcpp::traits::input_parameter<NumericMatrix>::type b(bSEXP);
21-
rcpp_result_gen = Rcpp::wrap(add_two_rcpp_(a, b));
22-
return rcpp_result_gen;
23-
END_RCPP
16+
BEGIN_RCPP
17+
Rcpp::RObject rcpp_result_gen;
18+
Rcpp::RNGScope rcpp_rngScope_gen;
19+
Rcpp::traits::input_parameter< NumericMatrix >::type a(aSEXP);
20+
Rcpp::traits::input_parameter< NumericMatrix >::type b(bSEXP);
21+
rcpp_result_gen = Rcpp::wrap(add_two_rcpp_(a, b));
22+
return rcpp_result_gen;
23+
END_RCPP
2424
}
2525
// add_four_rcpp_
26-
NumericMatrix add_four_rcpp_(NumericMatrix a, NumericMatrix b, NumericMatrix c,
27-
NumericMatrix d);
28-
RcppExport SEXP _Rcppbenchmark_add_four_rcpp_(SEXP aSEXP, SEXP bSEXP, SEXP cSEXP,
29-
SEXP dSEXP) {
30-
BEGIN_RCPP
31-
Rcpp::RObject rcpp_result_gen;
32-
Rcpp::RNGScope rcpp_rngScope_gen;
33-
Rcpp::traits::input_parameter<NumericMatrix>::type a(aSEXP);
34-
Rcpp::traits::input_parameter<NumericMatrix>::type b(bSEXP);
35-
Rcpp::traits::input_parameter<NumericMatrix>::type c(cSEXP);
36-
Rcpp::traits::input_parameter<NumericMatrix>::type d(dSEXP);
37-
rcpp_result_gen = Rcpp::wrap(add_four_rcpp_(a, b, c, d));
38-
return rcpp_result_gen;
39-
END_RCPP
26+
NumericMatrix add_four_rcpp_(NumericMatrix a, NumericMatrix b, NumericMatrix c, NumericMatrix d);
27+
RcppExport SEXP _Rcppbenchmark_add_four_rcpp_(SEXP aSEXP, SEXP bSEXP, SEXP cSEXP, SEXP dSEXP) {
28+
BEGIN_RCPP
29+
Rcpp::RObject rcpp_result_gen;
30+
Rcpp::RNGScope rcpp_rngScope_gen;
31+
Rcpp::traits::input_parameter< NumericMatrix >::type a(aSEXP);
32+
Rcpp::traits::input_parameter< NumericMatrix >::type b(bSEXP);
33+
Rcpp::traits::input_parameter< NumericMatrix >::type c(cSEXP);
34+
Rcpp::traits::input_parameter< NumericMatrix >::type d(dSEXP);
35+
rcpp_result_gen = Rcpp::wrap(add_four_rcpp_(a, b, c, d));
36+
return rcpp_result_gen;
37+
END_RCPP
4038
}
4139
// multiply_four_rcpp_
42-
NumericMatrix multiply_four_rcpp_(NumericMatrix a, NumericMatrix b, NumericMatrix c,
43-
NumericMatrix d);
44-
RcppExport SEXP _Rcppbenchmark_multiply_four_rcpp_(SEXP aSEXP, SEXP bSEXP, SEXP cSEXP,
45-
SEXP dSEXP) {
46-
BEGIN_RCPP
47-
Rcpp::RObject rcpp_result_gen;
48-
Rcpp::RNGScope rcpp_rngScope_gen;
49-
Rcpp::traits::input_parameter<NumericMatrix>::type a(aSEXP);
50-
Rcpp::traits::input_parameter<NumericMatrix>::type b(bSEXP);
51-
Rcpp::traits::input_parameter<NumericMatrix>::type c(cSEXP);
52-
Rcpp::traits::input_parameter<NumericMatrix>::type d(dSEXP);
53-
rcpp_result_gen = Rcpp::wrap(multiply_four_rcpp_(a, b, c, d));
54-
return rcpp_result_gen;
55-
END_RCPP
40+
NumericMatrix multiply_four_rcpp_(NumericMatrix a, NumericMatrix b, NumericMatrix c, NumericMatrix d);
41+
RcppExport SEXP _Rcppbenchmark_multiply_four_rcpp_(SEXP aSEXP, SEXP bSEXP, SEXP cSEXP, SEXP dSEXP) {
42+
BEGIN_RCPP
43+
Rcpp::RObject rcpp_result_gen;
44+
Rcpp::RNGScope rcpp_rngScope_gen;
45+
Rcpp::traits::input_parameter< NumericMatrix >::type a(aSEXP);
46+
Rcpp::traits::input_parameter< NumericMatrix >::type b(bSEXP);
47+
Rcpp::traits::input_parameter< NumericMatrix >::type c(cSEXP);
48+
Rcpp::traits::input_parameter< NumericMatrix >::type d(dSEXP);
49+
rcpp_result_gen = Rcpp::wrap(multiply_four_rcpp_(a, b, c, d));
50+
return rcpp_result_gen;
51+
END_RCPP
5652
}
5753
// submatrix_manipulation_rcpp_
5854
NumericMatrix submatrix_manipulation_rcpp_(NumericMatrix a, NumericMatrix b);
5955
RcppExport SEXP _Rcppbenchmark_submatrix_manipulation_rcpp_(SEXP aSEXP, SEXP bSEXP) {
60-
BEGIN_RCPP
61-
Rcpp::RObject rcpp_result_gen;
62-
Rcpp::RNGScope rcpp_rngScope_gen;
63-
Rcpp::traits::input_parameter<NumericMatrix>::type a(aSEXP);
64-
Rcpp::traits::input_parameter<NumericMatrix>::type b(bSEXP);
65-
rcpp_result_gen = Rcpp::wrap(submatrix_manipulation_rcpp_(a, b));
66-
return rcpp_result_gen;
67-
END_RCPP
56+
BEGIN_RCPP
57+
Rcpp::RObject rcpp_result_gen;
58+
Rcpp::RNGScope rcpp_rngScope_gen;
59+
Rcpp::traits::input_parameter< NumericMatrix >::type a(aSEXP);
60+
Rcpp::traits::input_parameter< NumericMatrix >::type b(bSEXP);
61+
rcpp_result_gen = Rcpp::wrap(submatrix_manipulation_rcpp_(a, b));
62+
return rcpp_result_gen;
63+
END_RCPP
6864
}
6965
// multi_operation_rcpp_
7066
double multi_operation_rcpp_(NumericMatrix a, NumericMatrix b, NumericMatrix c);
7167
RcppExport SEXP _Rcppbenchmark_multi_operation_rcpp_(SEXP aSEXP, SEXP bSEXP, SEXP cSEXP) {
72-
BEGIN_RCPP
73-
Rcpp::RObject rcpp_result_gen;
74-
Rcpp::RNGScope rcpp_rngScope_gen;
75-
Rcpp::traits::input_parameter<NumericMatrix>::type a(aSEXP);
76-
Rcpp::traits::input_parameter<NumericMatrix>::type b(bSEXP);
77-
Rcpp::traits::input_parameter<NumericMatrix>::type c(cSEXP);
78-
rcpp_result_gen = Rcpp::wrap(multi_operation_rcpp_(a, b, c));
79-
return rcpp_result_gen;
80-
END_RCPP
68+
BEGIN_RCPP
69+
Rcpp::RObject rcpp_result_gen;
70+
Rcpp::RNGScope rcpp_rngScope_gen;
71+
Rcpp::traits::input_parameter< NumericMatrix >::type a(aSEXP);
72+
Rcpp::traits::input_parameter< NumericMatrix >::type b(bSEXP);
73+
Rcpp::traits::input_parameter< NumericMatrix >::type c(cSEXP);
74+
rcpp_result_gen = Rcpp::wrap(multi_operation_rcpp_(a, b, c));
75+
return rcpp_result_gen;
76+
END_RCPP
8177
}
8278

8379
static const R_CallMethodDef CallEntries[] = {
84-
{"_Rcppbenchmark_add_two_rcpp_", (DL_FUNC)&_Rcppbenchmark_add_two_rcpp_, 2},
85-
{"_Rcppbenchmark_add_four_rcpp_", (DL_FUNC)&_Rcppbenchmark_add_four_rcpp_, 4},
86-
{"_Rcppbenchmark_multiply_four_rcpp_", (DL_FUNC)&_Rcppbenchmark_multiply_four_rcpp_,
87-
4},
88-
{"_Rcppbenchmark_submatrix_manipulation_rcpp_",
89-
(DL_FUNC)&_Rcppbenchmark_submatrix_manipulation_rcpp_, 2},
90-
{"_Rcppbenchmark_multi_operation_rcpp_",
91-
(DL_FUNC)&_Rcppbenchmark_multi_operation_rcpp_, 3},
92-
{NULL, NULL, 0}};
80+
{"_Rcppbenchmark_add_two_rcpp_", (DL_FUNC) &_Rcppbenchmark_add_two_rcpp_, 2},
81+
{"_Rcppbenchmark_add_four_rcpp_", (DL_FUNC) &_Rcppbenchmark_add_four_rcpp_, 4},
82+
{"_Rcppbenchmark_multiply_four_rcpp_", (DL_FUNC) &_Rcppbenchmark_multiply_four_rcpp_, 4},
83+
{"_Rcppbenchmark_submatrix_manipulation_rcpp_", (DL_FUNC) &_Rcppbenchmark_submatrix_manipulation_rcpp_, 2},
84+
{"_Rcppbenchmark_multi_operation_rcpp_", (DL_FUNC) &_Rcppbenchmark_multi_operation_rcpp_, 3},
85+
{NULL, NULL, 0}
86+
};
9387

94-
RcppExport void R_init_Rcppbenchmark(DllInfo* dll) {
95-
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
96-
R_useDynamicSymbols(dll, FALSE);
88+
RcppExport void R_init_Rcppbenchmark(DllInfo *dll) {
89+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
90+
R_useDynamicSymbols(dll, FALSE);
9791
}

extended-tests/cpp4rbenchmark/R/cpp4r.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Generated by cpp4r: do not edit by hand
22

3-
#' @title Add Two Matrices (Optimized)
3+
#' @title Add Two Matrices
44
#' @export
55
add_two_cpp4r <- function(a, b) {
66
storage.mode(a) <- "double"
77
storage.mode(b) <- "double"
88
.Call(`_cpp4rbenchmark_add_two_cpp4r`, a, b)
99
}
1010

11-
#' @title Add Four Matrices (Optimized)
11+
#' @title Add Four Matrices
1212
#' @export
1313
add_four_cpp4r <- function(a, b, c, d) {
1414
storage.mode(a) <- "double"
@@ -18,7 +18,7 @@ add_four_cpp4r <- function(a, b, c, d) {
1818
.Call(`_cpp4rbenchmark_add_four_cpp4r`, a, b, c, d)
1919
}
2020

21-
#' @title Multiply Four Matrices (Optimized)
21+
#' @title Multiply Four Matrices
2222
#' @export
2323
multiply_four_cpp4r <- function(a, b, c, d) {
2424
storage.mode(a) <- "double"
@@ -28,15 +28,15 @@ multiply_four_cpp4r <- function(a, b, c, d) {
2828
.Call(`_cpp4rbenchmark_multiply_four_cpp4r`, a, b, c, d)
2929
}
3030

31-
#' @title Submatrix Manipulation (Optimized)
31+
#' @title Submatrix Manipulation
3232
#' @export
3333
submatrix_manipulation_cpp4r <- function(a, b) {
3434
storage.mode(a) <- "double"
3535
storage.mode(b) <- "double"
3636
.Call(`_cpp4rbenchmark_submatrix_manipulation_cpp4r`, a, b)
3737
}
3838

39-
#' @title Multi-Operation Expression (Optimized)
39+
#' @title Multi-Operation Expression
4040
#' @export
4141
multi_operation_cpp4r <- function(a, b, c) {
4242
storage.mode(a) <- "double"

extended-tests/cpp4rbenchmark/src/bench-cpp4r.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using namespace cpp4r;
66

77
/* roxygen
8-
@title Add Two Matrices (Optimized)
8+
@title Add Two Matrices
99
@export
1010
*/
1111
[[cpp4r::register]] doubles_matrix<> add_two_cpp4r(const doubles_matrix<>& a,
@@ -31,7 +31,7 @@ using namespace cpp4r;
3131
}
3232

3333
/* roxygen
34-
@title Add Four Matrices (Optimized)
34+
@title Add Four Matrices
3535
@export
3636
*/
3737
[[cpp4r::register]] doubles_matrix<> add_four_cpp4r(const doubles_matrix<>& a,
@@ -59,7 +59,7 @@ using namespace cpp4r;
5959
}
6060

6161
/* roxygen
62-
@title Multiply Four Matrices (Optimized)
62+
@title Multiply Four Matrices
6363
@export
6464
*/
6565
[[cpp4r::register]] doubles_matrix<> multiply_four_cpp4r(const doubles_matrix<>& a,
@@ -130,7 +130,7 @@ using namespace cpp4r;
130130
}
131131

132132
/* roxygen
133-
@title Submatrix Manipulation (Optimized)
133+
@title Submatrix Manipulation
134134
@export
135135
*/
136136
[[cpp4r::register]] doubles_matrix<> submatrix_manipulation_cpp4r(
@@ -160,7 +160,7 @@ using namespace cpp4r;
160160
}
161161

162162
/* roxygen
163-
@title Multi-Operation Expression (Optimized)
163+
@title Multi-Operation Expression
164164
@export
165165
*/
166166
[[cpp4r::register]] double multi_operation_cpp4r(const doubles_matrix<>& a,

inst/include/cpp4r/complexes.hpp

Lines changed: 10 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ inline void r_vector<r_complex>::set_elt(
6969
COMPLEX(x)[i] = value;
7070
}
7171

72+
// Comparison operator for proxy
73+
inline bool operator==(const r_vector<r_complex>::proxy& lhs, r_complex rhs) {
74+
return static_cast<r_complex>(lhs) == rhs;
75+
}
76+
77+
inline bool operator!=(const r_vector<r_complex>::proxy& lhs, r_complex rhs) {
78+
return !(lhs == rhs);
79+
}
80+
7281
typedef r_vector<r_complex> complexes;
7382

7483
} // namespace writable
@@ -92,109 +101,7 @@ inline complexes as_complexes(SEXP x) {
92101
throw type_error(CPLXSXP, detail::r_typeof(x));
93102
}
94103

95-
// Define comparison operators within the proxy class
96-
namespace writable {
97-
98-
template <>
99-
class r_vector<r_complex>::proxy {
100-
public:
101-
proxy(SEXP data, R_xlen_t index)
102-
: data_(data), index_(index), buf_(nullptr), is_altrep_(false) {}
103-
104-
proxy(SEXP data, R_xlen_t index, Rcomplex* buf, bool is_altrep)
105-
: data_(data), index_(index), buf_(buf), is_altrep_(is_altrep) {}
106-
107-
operator r_complex() const {
108-
if (is_altrep_ && buf_ != nullptr) {
109-
return r_complex(buf_->r, buf_->i);
110-
} else {
111-
Rcomplex r = COMPLEX_ELT(data_, index_);
112-
return r_complex(r.r, r.i);
113-
}
114-
}
115-
116-
proxy& operator=(const r_complex& value) {
117-
if (is_altrep_ && buf_ != nullptr) {
118-
buf_->r = value.real();
119-
buf_->i = value.imag();
120-
} else {
121-
Rcomplex r;
122-
r.r = value.real();
123-
r.i = value.imag();
124-
SET_COMPLEX_ELT(data_, index_, r);
125-
}
126-
return *this;
127-
}
128-
129-
proxy& operator=(const std::complex<double>& value) {
130-
if (is_altrep_ && buf_ != nullptr) {
131-
buf_->r = value.real();
132-
buf_->i = value.imag();
133-
} else {
134-
Rcomplex r;
135-
r.r = value.real();
136-
r.i = value.imag();
137-
SET_COMPLEX_ELT(data_, index_, r);
138-
}
139-
return *this;
140-
}
141-
142-
proxy& operator+=(const r_complex& value) {
143-
*this = static_cast<r_complex>(*this) + value;
144-
return *this;
145-
}
146-
147-
proxy& operator-=(const r_complex& value) {
148-
*this = static_cast<r_complex>(*this) - value;
149-
return *this;
150-
}
151-
152-
proxy& operator*=(const r_complex& value) {
153-
*this = static_cast<r_complex>(*this) * value;
154-
return *this;
155-
}
156-
157-
proxy& operator/=(const r_complex& value) {
158-
*this = static_cast<r_complex>(*this) / value;
159-
return *this;
160-
}
161-
162-
proxy& operator++() {
163-
*this += r_complex(1, 0);
164-
return *this;
165-
}
166-
167-
proxy operator++(int) {
168-
proxy tmp(*this);
169-
operator++();
170-
return tmp;
171-
}
172-
173-
proxy& operator--() {
174-
*this -= r_complex(1, 0);
175-
return *this;
176-
}
177-
178-
proxy operator--(int) {
179-
proxy tmp(*this);
180-
operator--();
181-
return tmp;
182-
}
183-
184-
friend bool operator==(const proxy& lhs, const r_complex& rhs) {
185-
return static_cast<r_complex>(lhs) == rhs;
186-
}
187-
188-
friend bool operator!=(const proxy& lhs, const r_complex& rhs) { return !(lhs == rhs); }
189-
190-
private:
191-
SEXP data_;
192-
R_xlen_t index_;
193-
Rcomplex* buf_;
194-
bool is_altrep_;
195-
};
196-
197-
} // namespace writable
104+
// No proxy redefinition here — use the generic proxy implementation from r_vector_writable_impl.hpp
198105

199106
// New complex_vector class for handling complex numbers in SEXP
200107
class complex_vector {

scripts/run-benchmark.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ message(
1919
"\n==============================================================================="
2020
)
2121

22-
n <- 500L
23-
iterations <- 100L
22+
n <- 1000L
23+
iterations <- 250L
2424

2525
set.seed(42)
2626
A <- matrix(rnorm(n * n), nrow = n, ncol = n)

0 commit comments

Comments
 (0)