eigen.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2019 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Edward Valeev
19  * Department of Chemistry, Virginia Tech
20  *
21  * eigen.h
22  * Oct 1, 2019
23  *
24  */
25 
26 #ifndef TILEDARRAY_UTIL_EIGEN_H__INCLUDED
27 #define TILEDARRAY_UTIL_EIGEN_H__INCLUDED
28 
29 //
30 // Configure Eigen and include Eigen/Core, all dependencies should include this
31 // before including any Eigen headers
32 //
33 
35 
36 namespace std {
37 
38 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
39 constexpr auto begin(
40  Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>& m) {
41  return m.data();
42 }
43 
44 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
45 constexpr auto begin(
46  const Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>& m) {
47  return m.data();
48 }
49 
50 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
51 constexpr auto end(Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>& m) {
52  return m.data() + m.size();
53 }
54 
55 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
56 constexpr auto end(
57  const Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>& m) {
58  return m.data() + m.size();
59 }
60 
61 } // namespace std
62 
63 #include <TiledArray/type_traits.h>
64 
65 namespace TiledArray {
66 
67 namespace eigen {
68 
69 template <typename Range,
70  typename = std::enable_if_t<detail::is_integral_range_v<Range> &&
71  detail::is_sized_range_v<Range>>>
72 Eigen::Matrix<detail::value_t<Range>, Eigen::Dynamic, 1> iv(Range&& rng) {
73  Eigen::Matrix<detail::value_t<Range>, Eigen::Dynamic, 1> result(
74  std::size(rng));
75  long count = 0;
76  for (auto&& i : rng) {
77  result(count) = i;
78  ++count;
79  }
80  return result;
81 }
82 
83 // TODO constrain this to run only at compile time
84 // template <typename Range, typename =
85 // std::enable_if_t<detail::is_integral_range_v<Range>>> constexpr auto
86 // ivn(Range&& rng) {
87 // Eigen::Matrix<detail::value_t<Range>, std::size(rng), 1> result;
88 // long count = 0;
89 // for(auto && i : rng) {
90 // result(count) = i;
91 // ++count;
92 // }
93 // return result;
94 //}
95 
96 template <typename Int, typename = std::enable_if_t<std::is_integral_v<Int>>>
97 Eigen::Matrix<Int, Eigen::Dynamic, 1> iv(std::initializer_list<Int> list) {
98  Eigen::Map<const Eigen::Matrix<Int, Eigen::Dynamic, 1>> result(data(list),
99  size(list));
100  return result;
101 }
102 
103 namespace detail {
104 template <typename Mat, typename T, typename... Ts>
105 void iv_assign(Mat& m, int i, T v, Ts... vrest) {
106  m(i) = v;
107  if constexpr (sizeof...(Ts) > 0) {
108  iv_assign(m, i + 1, vrest...);
109  }
110 }
111 } // namespace detail
112 
113 template <typename Int, typename... Ints,
114  typename = std::enable_if_t<std::is_integral_v<Int> &&
115  (std::is_integral_v<Ints> && ...)>>
116 constexpr auto iv(Int i0, Ints... rest) {
117  Eigen::Matrix<Int, sizeof...(Ints) + 1, 1> result;
118  detail::iv_assign(result, 0, i0, rest...);
119  return result;
120 }
121 
123 template <typename Derived>
124 auto iv(const Eigen::MatrixBase<Derived>& mat) {
125  Eigen::Matrix<typename Eigen::internal::traits<Derived>::Scalar,
126  Eigen::internal::traits<Derived>::RowsAtCompileTime,
127  Eigen::internal::traits<Derived>::ColsAtCompileTime>
128  result = mat;
129  return result;
130 }
131 
132 } // namespace eigen
133 
134 } // namespace TiledArray
135 
136 #include <boost/range.hpp>
137 
138 namespace boost {
139 
140 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
141 struct range_mutable_iterator<
142  Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>> {
143  typedef _Scalar* type;
144 };
145 
146 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
147 struct range_const_iterator<
148  Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>> {
149  typedef const _Scalar* type;
150 };
151 
152 } // namespace boost
153 namespace Eigen {
154 
155 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
157  const Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>& mat) {
158  return mat.data();
159 }
160 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
161 auto range_begin(Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>& mat) {
162  return mat.data();
163 }
164 
165 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
167  const Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>& mat) {
168  return mat.data() + mat.size();
169 }
170 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
171 auto range_end(Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>& mat) {
172  return mat.data() + mat.size();
173 }
174 
175 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
177  const Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>& mat) {
178  return mat.size();
179 }
180 template <typename _Scalar, int _Rows, int _Options, int _MaxRows>
182  Eigen::Matrix<_Scalar, _Rows, 1, _Options, _MaxRows, 1>& mat) {
183  return mat.size();
184 }
185 
186 } // namespace Eigen
187 
188 #endif // TILEDARRAY_UTIL_EIGEN_H__INCLUDED
Forward declarations.
Definition: foreach.h:34
Eigen::Matrix< detail::value_t< Range >, Eigen::Dynamic, 1 > iv(Range &&rng)
Definition: eigen.h:72
Definition: eigen.h:138
auto range_end(const Eigen::Matrix< _Scalar, _Rows, 1, _Options, _MaxRows, 1 > &mat)
Definition: eigen.h:166
constexpr auto end(Eigen::Matrix< _Scalar, _Rows, 1, _Options, _MaxRows, 1 > &m)
Definition: eigen.h:51
auto range_calculate_size(const Eigen::Matrix< _Scalar, _Rows, 1, _Options, _MaxRows, 1 > &mat)
Definition: eigen.h:176
constexpr auto begin(Eigen::Matrix< _Scalar, _Rows, 1, _Options, _MaxRows, 1 > &m)
Definition: eigen.h:39
void iv_assign(Mat &m, int i, T v, Ts... vrest)
Definition: eigen.h:105
auto range_begin(const Eigen::Matrix< _Scalar, _Rows, 1, _Options, _MaxRows, 1 > &mat)
Definition: eigen.h:156
::Eigen::Matrix< T, ::Eigen::Dynamic, ::Eigen::Dynamic, Options > Matrix
Definition: blas.h:63
A (hyperrectangular) interval on , space of integer -indices.
Definition: range.h:46