Skip to content

Commit 4e14955

Browse files
authored
Merge pull request #566 from nathan-russell/master
Matrix sugar functions eye, ones, and zeros - part of #564
2 parents d541611 + 7d7e5c8 commit 4e14955

File tree

6 files changed

+322
-0
lines changed

6 files changed

+322
-0
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
2016-10-23 Nathan Russell <[email protected]>
22

3+
* inst/include/Rcpp/sugar/matrix/eye.h: New functions
4+
eye(), ones(), and zeros()
5+
* inst/include/Rcpp/sugar/matrix/matrix_functions.h: Idem
6+
* inst/unitTests/cpp/sugar.cpp: Unit tests for new functions
7+
* inst/unitTests/runit.sugar.R: Idem
38
* inst/unitTests/runit.dispatch.R (test.ExpressionVector): Use
49
expression rather than parse, correct typo
510

inst/NEWS.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
\item Added new Sugar functions \code{rowSums()}, \code{colSums()},
1616
\code{rowMeans()}, \code{colMeans()} (PR \ghpr{551} by Nathan Russell
1717
fixing \ghit{549})
18+
\item Added new (matrix) Sugar functions \code{eye()},
19+
\code{ones()}, \code{zeros()} (PR \ghpr{566} by Nathan Russell)
1820
}
1921
\item Changes in Rcpp unit tests
2022
\itemize{

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

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2+
//
3+
// eye.h: Rcpp R/C++ interface class library -- 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__sugar__eye_h
23+
#define Rcpp__sugar__eye_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+
76+
class eye {
77+
private:
78+
int n;
79+
80+
public:
81+
eye(const int n_)
82+
: n(n_)
83+
{}
84+
85+
template <int RTYPE>
86+
operator Matrix<RTYPE>() const {
87+
typedef typename traits::storage_type<RTYPE>::type stored_type;
88+
89+
const bool enabled =
90+
traits::is_arithmetic<stored_type>::value ||
91+
traits::same_type<stored_type, Rcomplex>::value;
92+
93+
(void)sizeof(traits::allowed_matrix_type<enabled>);
94+
95+
typedef traits::one_type<stored_type> one_type;
96+
97+
return Matrix<RTYPE>::diag(n, one_type());
98+
}
99+
};
100+
101+
class ones {
102+
private:
103+
int n;
104+
105+
public:
106+
ones(const int n_)
107+
: n(n_)
108+
{}
109+
110+
template <int RTYPE>
111+
operator Matrix<RTYPE>() const {
112+
typedef typename traits::storage_type<RTYPE>::type stored_type;
113+
114+
const bool enabled =
115+
traits::is_arithmetic<stored_type>::value ||
116+
traits::same_type<stored_type, Rcomplex>::value;
117+
118+
(void)sizeof(traits::allowed_matrix_type<enabled>);
119+
120+
typedef traits::one_type<stored_type> one_type;
121+
122+
Matrix<RTYPE> res(n, n);
123+
std::fill(res.begin(), res.end(), one_type());
124+
125+
return res;
126+
}
127+
};
128+
129+
class zeros {
130+
private:
131+
int n;
132+
133+
public:
134+
zeros(const int n_)
135+
: n(n_)
136+
{}
137+
138+
template <int RTYPE>
139+
operator Matrix<RTYPE>() const {
140+
typedef typename traits::storage_type<RTYPE>::type stored_type;
141+
142+
const bool enabled =
143+
traits::is_arithmetic<stored_type>::value ||
144+
traits::same_type<stored_type, Rcomplex>::value;
145+
146+
(void)sizeof(traits::allowed_matrix_type<enabled>);
147+
148+
typedef traits::zero_type<stored_type> zero_type;
149+
150+
Matrix<RTYPE> res(n, n);
151+
std::fill(res.begin(), res.end(), zero_type());
152+
153+
return res;
154+
}
155+
};
156+
157+
} // Rcpp
158+
159+
#endif // Rcpp__sugar__eye_h

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
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>
3233

3334
#endif

inst/unitTests/cpp/sugar.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,3 +1116,68 @@ Rcpp::ComplexVector cx_col_means(Rcpp::ComplexMatrix x, bool na_rm = false) {
11161116
return colMeans(x, na_rm);
11171117
}
11181118

1119+
1120+
// 23 October 2016
1121+
// non-member eye, ones, and zeros functions
1122+
1123+
// [[Rcpp::export]]
1124+
Rcpp::NumericMatrix dbl_eye(int n) {
1125+
return Rcpp::eye(n);
1126+
}
1127+
1128+
// [[Rcpp::export]]
1129+
Rcpp::IntegerMatrix int_eye(int n) {
1130+
return Rcpp::eye(n);
1131+
}
1132+
1133+
// [[Rcpp::export]]
1134+
Rcpp::ComplexMatrix cx_eye(int n) {
1135+
return Rcpp::eye(n);
1136+
}
1137+
1138+
// [[Rcpp::export]]
1139+
Rcpp::LogicalMatrix lgl_eye(int n) {
1140+
return Rcpp::eye(n);
1141+
}
1142+
1143+
1144+
// [[Rcpp::export]]
1145+
Rcpp::NumericMatrix dbl_ones(int n) {
1146+
return Rcpp::ones(n);
1147+
}
1148+
1149+
// [[Rcpp::export]]
1150+
Rcpp::IntegerMatrix int_ones(int n) {
1151+
return Rcpp::ones(n);
1152+
}
1153+
1154+
// [[Rcpp::export]]
1155+
Rcpp::ComplexMatrix cx_ones(int n) {
1156+
return Rcpp::ones(n);
1157+
}
1158+
1159+
// [[Rcpp::export]]
1160+
Rcpp::LogicalMatrix lgl_ones(int n) {
1161+
return Rcpp::ones(n);
1162+
}
1163+
1164+
1165+
// [[Rcpp::export]]
1166+
Rcpp::NumericMatrix dbl_zeros(int n) {
1167+
return Rcpp::zeros(n);
1168+
}
1169+
1170+
// [[Rcpp::export]]
1171+
Rcpp::IntegerMatrix int_zeros(int n) {
1172+
return Rcpp::zeros(n);
1173+
}
1174+
1175+
// [[Rcpp::export]]
1176+
Rcpp::ComplexMatrix cx_zeros(int n) {
1177+
return Rcpp::zeros(n);
1178+
}
1179+
1180+
// [[Rcpp::export]]
1181+
Rcpp::LogicalMatrix lgl_zeros(int n) {
1182+
return Rcpp::zeros(n);
1183+
}

inst/unitTests/runit.sugar.R

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,5 +1610,95 @@ if (.runThisTest) {
16101610

16111611
}
16121612

1613+
1614+
## 23 October 2016
1615+
## sugar eye function
1616+
test.sugar.eye <- function() {
1617+
1618+
checkEquals(
1619+
dbl_eye(3),
1620+
diag(1.0, 3, 3),
1621+
"eye - numeric"
1622+
)
1623+
1624+
checkEquals(
1625+
int_eye(3),
1626+
diag(1L, 3, 3),
1627+
"eye - integer"
1628+
)
1629+
1630+
checkEquals(
1631+
cx_eye(3),
1632+
diag(1.0 + 0i, 3, 3),
1633+
"eye - complex"
1634+
)
1635+
1636+
## diag(TRUE, 3, 3) was registering as
1637+
## a numeric matrix on Travis for some reason
1638+
mat <- matrix(FALSE, 3, 3)
1639+
diag(mat) <- TRUE
1640+
checkEquals(
1641+
lgl_eye(3),
1642+
mat,
1643+
"eye - logical"
1644+
)
1645+
}
1646+
1647+
## sugar ones function
1648+
test.sugar.ones <- function() {
1649+
1650+
checkEquals(
1651+
dbl_ones(3),
1652+
matrix(1.0, 3, 3),
1653+
"ones - numeric"
1654+
)
1655+
1656+
checkEquals(
1657+
int_ones(3),
1658+
matrix(1L, 3, 3),
1659+
"ones - integer"
1660+
)
1661+
1662+
checkEquals(
1663+
cx_ones(3),
1664+
matrix(1.0 + 0i, 3, 3),
1665+
"ones - complex"
1666+
)
1667+
1668+
checkEquals(
1669+
lgl_ones(3),
1670+
matrix(TRUE, 3, 3),
1671+
"ones - logical"
1672+
)
1673+
}
1674+
1675+
## sugar ones function
1676+
test.sugar.zeros <- function() {
1677+
1678+
checkEquals(
1679+
dbl_zeros(3),
1680+
matrix(0.0, 3, 3),
1681+
"zeros - numeric"
1682+
)
1683+
1684+
checkEquals(
1685+
int_zeros(3),
1686+
matrix(0L, 3, 3),
1687+
"zeros - integer"
1688+
)
1689+
1690+
checkEquals(
1691+
cx_zeros(3),
1692+
matrix(0.0 + 0i, 3, 3),
1693+
"zeros - complex"
1694+
)
1695+
1696+
checkEquals(
1697+
lgl_zeros(3),
1698+
matrix(FALSE, 3, 3),
1699+
"zeros - logical"
1700+
)
1701+
}
1702+
16131703
}
16141704

0 commit comments

Comments
 (0)