|
| 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 |
0 commit comments