Skip to content

Refactoring of eye, ones, and zeros #569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
2016-10-24 Nathan Russell <russell.nr2012@gmail.com>

* inst/include/Rcpp/vector/MatrixBase.h: Change sugar
functions eye(), ones(), and zeros() into static methods
in MatrixBase
* inst/include/Rcpp/traits/one_type.h: Idem
* inst/include/Rcpp/traits/traits.h: Idem
* inst/unitTests/cpp/Matrix.cpp: Idem
* inst/unitTests/runit.Matrix.R: Idem

2016-10-24 Qiang Kou <qkou@umail.iu.edu>

* inst/include/Rcpp/sugar/Range.h: Range sugar uses R_xlen_t as start/end type
Expand Down
6 changes: 3 additions & 3 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
\itemize{
\item String and vector elements now use extended \code{R_xlen_t} indices
(Qiang in PR \ghpr{560})
\item Hashing functions now return unsigned int (Qiang in PR \ghpr{561})
\item Hashing functions now return unsigned int (Qiang in PR \ghpr{561})
\item Added static methods \code{eye()}, \code{ones()}, and \code{zeros()}
for select matrix types (Nathan Russell in PR \ghpr{569})
}
\item Changes in Rcpp Sugar:
\itemize{
\item Added new Sugar functions \code{rowSums()}, \code{colSums()},
\code{rowMeans()}, \code{colMeans()} (PR \ghpr{551} by Nathan Russell
fixing \ghit{549})
\item Added new (matrix) Sugar functions \code{eye()},
\code{ones()}, \code{zeros()} (PR \ghpr{566} by Nathan Russell)
\item \code{Range} Sugar now used \code{R_xlen_t} type for start/end
(PR \ghpr{568} by Qiang Kou)
}
Expand Down
159 changes: 0 additions & 159 deletions inst/include/Rcpp/sugar/matrix/eye.h

This file was deleted.

1 change: 0 additions & 1 deletion inst/include/Rcpp/sugar/matrix/matrix_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@
#include <Rcpp/sugar/matrix/upper_tri.h>
#include <Rcpp/sugar/matrix/diag.h>
#include <Rcpp/sugar/matrix/as_vector.h>
#include <Rcpp/sugar/matrix/eye.h>

#endif
78 changes: 78 additions & 0 deletions inst/include/Rcpp/traits/one_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// one_type.h: Rcpp R/C++ interface class library -- traits functions for eye, ones, zeros
//
// Copyright (C) 2016 Nathan Russell
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.

#ifndef Rcpp__traits__one_type__h
#define Rcpp__traits__one_type__h

namespace Rcpp {
namespace traits {

template <bool>
struct allowed_matrix_type;

template <>
struct allowed_matrix_type<true> {};

template <typename T>
class one_type {
private:
Rcomplex op(true_type) const {
Rcomplex res;
res.i = 0.0;
res.r = 1.0;
return res;
}

T op(false_type) const {
return static_cast<T>(1);
}

public:
operator T() const {
return op(typename same_type<T, Rcomplex>::type());
}
};

template <typename T>
class zero_type {
private:
Rcomplex op(true_type) const {
Rcomplex res;
res.i = 0.0;
res.r = 0.0;
return res;
}

T op(false_type) const {
return static_cast<T>(0);
}

public:
operator T() const {
return op(typename same_type<T, Rcomplex>::type());
}
};

} // traits
} // Rcpp

#endif // Rcpp__traits__one_type__h

2 changes: 2 additions & 0 deletions inst/include/Rcpp/traits/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@ struct int2type { enum { value = I }; };
#include <Rcpp/traits/is_module_object.h>
#include <Rcpp/traits/is_primitive.h>

#include <Rcpp/traits/one_type.h>

#endif

37 changes: 35 additions & 2 deletions inst/include/Rcpp/vector/MatrixBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
//
// MatrixBase.h: Rcpp R/C++ interface class library --
//
// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
//
// Copyright (C) 2010 - 2016 Dirk Eddelbuettel and Romain Francois
// Copyright (C) 2016 Dirk Eddelbuettel and Romain Francois and Nathan Russell
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
Expand Down Expand Up @@ -47,6 +48,38 @@ namespace Rcpp{
inline R_xlen_t nrow() const { return static_cast<const MATRIX&>(*this).nrow() ; }
inline R_xlen_t ncol() const { return static_cast<const MATRIX&>(*this).ncol() ; }

static MATRIX eye(int n) {
const bool enabled =
traits::is_arithmetic<stored_type>::value ||
traits::same_type<stored_type, Rcomplex>::value;
(void)sizeof(traits::allowed_matrix_type<enabled>);

return MATRIX::diag(n, traits::one_type<stored_type>());
}

static MATRIX ones(int n) {
const bool enabled =
traits::is_arithmetic<stored_type>::value ||
traits::same_type<stored_type, Rcomplex>::value;
(void)sizeof(traits::allowed_matrix_type<enabled>);

MATRIX res(n, n);
std::fill(
res.begin(), res.end(),
traits::one_type<stored_type>()
);
return res;
}

static MATRIX zeros(int n) {
const bool enabled =
traits::is_arithmetic<stored_type>::value ||
traits::same_type<stored_type, Rcomplex>::value;
(void)sizeof(traits::allowed_matrix_type<enabled>);

return MATRIX(n, n);
}

class iterator {
public:
typedef stored_type reference ;
Expand Down
Loading