Skip to content

Commit ef07a32

Browse files
authored
Merge pull request #569 from nathan-russell/master
Refactoring of eye, ones, and zeros
2 parents 3348462 + e696eea commit ef07a32

File tree

11 files changed

+284
-321
lines changed

11 files changed

+284
-321
lines changed

ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2016-10-24 Nathan Russell <[email protected]>
2+
3+
* inst/include/Rcpp/vector/MatrixBase.h: Change sugar
4+
functions eye(), ones(), and zeros() into static methods
5+
in MatrixBase
6+
* inst/include/Rcpp/traits/one_type.h: Idem
7+
* inst/include/Rcpp/traits/traits.h: Idem
8+
* inst/unitTests/cpp/Matrix.cpp: Idem
9+
* inst/unitTests/runit.Matrix.R: Idem
10+
111
2016-10-24 Qiang Kou <[email protected]>
212

313
* inst/include/Rcpp/sugar/Range.h: Range sugar uses R_xlen_t as start/end type

inst/NEWS.Rd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
\itemize{
1010
\item String and vector elements now use extended \code{R_xlen_t} indices
1111
(Qiang in PR \ghpr{560})
12-
\item Hashing functions now return unsigned int (Qiang in PR \ghpr{561})
12+
\item Hashing functions now return unsigned int (Qiang in PR \ghpr{561})
13+
\item Added static methods \code{eye()}, \code{ones()}, and \code{zeros()}
14+
for select matrix types (Nathan Russell in PR \ghpr{569})
1315
}
1416
\item Changes in Rcpp Sugar:
1517
\itemize{
1618
\item Added new Sugar functions \code{rowSums()}, \code{colSums()},
1719
\code{rowMeans()}, \code{colMeans()} (PR \ghpr{551} by Nathan Russell
1820
fixing \ghit{549})
19-
\item Added new (matrix) Sugar functions \code{eye()},
20-
\code{ones()}, \code{zeros()} (PR \ghpr{566} by Nathan Russell)
2121
\item \code{Range} Sugar now used \code{R_xlen_t} type for start/end
2222
(PR \ghpr{568} by Qiang Kou)
2323
}

inst/include/Rcpp/sugar/matrix/eye.h

Lines changed: 0 additions & 159 deletions
This file was deleted.

inst/include/Rcpp/sugar/matrix/matrix_functions.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@
2929
#include <Rcpp/sugar/matrix/upper_tri.h>
3030
#include <Rcpp/sugar/matrix/diag.h>
3131
#include <Rcpp/sugar/matrix/as_vector.h>
32-
#include <Rcpp/sugar/matrix/eye.h>
3332

3433
#endif

inst/include/Rcpp/traits/one_type.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2+
//
3+
// one_type.h: Rcpp R/C++ interface class library -- traits functions for eye, ones, zeros
4+
//
5+
// Copyright (C) 2016 Nathan Russell
6+
//
7+
// This file is part of Rcpp.
8+
//
9+
// Rcpp is free software: you can redistribute it and/or modify it
10+
// under the terms of the GNU General Public License as published by
11+
// the Free Software Foundation, either version 2 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Rcpp is distributed in the hope that it will be useful, but
15+
// WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU General Public License for more details.
18+
//
19+
// You should have received a copy of the GNU General Public License
20+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21+
22+
#ifndef Rcpp__traits__one_type__h
23+
#define Rcpp__traits__one_type__h
24+
25+
namespace Rcpp {
26+
namespace traits {
27+
28+
template <bool>
29+
struct allowed_matrix_type;
30+
31+
template <>
32+
struct allowed_matrix_type<true> {};
33+
34+
template <typename T>
35+
class one_type {
36+
private:
37+
Rcomplex op(true_type) const {
38+
Rcomplex res;
39+
res.i = 0.0;
40+
res.r = 1.0;
41+
return res;
42+
}
43+
44+
T op(false_type) const {
45+
return static_cast<T>(1);
46+
}
47+
48+
public:
49+
operator T() const {
50+
return op(typename same_type<T, Rcomplex>::type());
51+
}
52+
};
53+
54+
template <typename T>
55+
class zero_type {
56+
private:
57+
Rcomplex op(true_type) const {
58+
Rcomplex res;
59+
res.i = 0.0;
60+
res.r = 0.0;
61+
return res;
62+
}
63+
64+
T op(false_type) const {
65+
return static_cast<T>(0);
66+
}
67+
68+
public:
69+
operator T() const {
70+
return op(typename same_type<T, Rcomplex>::type());
71+
}
72+
};
73+
74+
} // traits
75+
} // Rcpp
76+
77+
#endif // Rcpp__traits__one_type__h
78+

inst/include/Rcpp/traits/traits.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ struct int2type { enum { value = I }; };
7676
#include <Rcpp/traits/is_module_object.h>
7777
#include <Rcpp/traits/is_primitive.h>
7878

79+
#include <Rcpp/traits/one_type.h>
80+
7981
#endif
8082

inst/include/Rcpp/vector/MatrixBase.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
//
33
// MatrixBase.h: Rcpp R/C++ interface class library --
44
//
5-
// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
6-
//
5+
// Copyright (C) 2010 - 2016 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2016 Dirk Eddelbuettel and Romain Francois and Nathan Russell
7+
//
78
// This file is part of Rcpp.
89
//
910
// Rcpp is free software: you can redistribute it and/or modify it
@@ -47,6 +48,38 @@ namespace Rcpp{
4748
inline R_xlen_t nrow() const { return static_cast<const MATRIX&>(*this).nrow() ; }
4849
inline R_xlen_t ncol() const { return static_cast<const MATRIX&>(*this).ncol() ; }
4950

51+
static MATRIX eye(int n) {
52+
const bool enabled =
53+
traits::is_arithmetic<stored_type>::value ||
54+
traits::same_type<stored_type, Rcomplex>::value;
55+
(void)sizeof(traits::allowed_matrix_type<enabled>);
56+
57+
return MATRIX::diag(n, traits::one_type<stored_type>());
58+
}
59+
60+
static MATRIX ones(int n) {
61+
const bool enabled =
62+
traits::is_arithmetic<stored_type>::value ||
63+
traits::same_type<stored_type, Rcomplex>::value;
64+
(void)sizeof(traits::allowed_matrix_type<enabled>);
65+
66+
MATRIX res(n, n);
67+
std::fill(
68+
res.begin(), res.end(),
69+
traits::one_type<stored_type>()
70+
);
71+
return res;
72+
}
73+
74+
static MATRIX zeros(int n) {
75+
const bool enabled =
76+
traits::is_arithmetic<stored_type>::value ||
77+
traits::same_type<stored_type, Rcomplex>::value;
78+
(void)sizeof(traits::allowed_matrix_type<enabled>);
79+
80+
return MATRIX(n, n);
81+
}
82+
5083
class iterator {
5184
public:
5285
typedef stored_type reference ;

0 commit comments

Comments
 (0)