ALPSCore reference
outer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 #pragma once
7 
8 #include <Eigen/Core>
9 
10 #include <alps/alea/complex_op.hpp>
12 
13 // Forward declarations
14 
15 namespace alps { namespace alea { namespace internal {
16  template <typename Str, typename Arg1, typename Arg2> class outer_expr;
17 }}}
18 
19 // Actual declarations
20 
21 namespace alps { namespace alea { namespace internal {
22 
30 template <class Str, typename Arg1, typename Arg2>
31 outer_expr<Str, Arg1, Arg2> outer(const Eigen::MatrixBase<Arg1> &arg1,
32  const Eigen::MatrixBase<Arg2> &arg2)
33 {
34  return outer_expr<Str, Arg1, Arg2>(arg1.derived(), arg2.derived());
35 }
36 
38 template <typename Str, typename Arg1, typename Arg2>
39 class outer_expr
40  : public Eigen::MatrixBase< outer_expr<Str, Arg1, Arg2> >
41 {
42 public:
43  typedef typename Eigen::internal::ref_selector<outer_expr>::type Nested;
44  typedef Eigen::Index Index;
45  typedef typename Eigen::internal::ref_selector<Arg1>::type Arg1Nested;
46  typedef typename Eigen::internal::ref_selector<Arg2>::type Arg2Nested;
47 
48  outer_expr(const Arg1 &arg1, const Arg2 &arg2)
49  : arg1_(arg1), arg2_(arg2)
50  {
51  EIGEN_STATIC_ASSERT(Arg1::ColsAtCompileTime == 1,
52  YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);
53  EIGEN_STATIC_ASSERT(Arg2::ColsAtCompileTime == 1,
54  YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);
55  }
56  Index rows() const { return arg1_.rows(); }
57  Index cols() const { return arg2_.rows(); }
58 
59  Arg1Nested arg1_;
60  Arg2Nested arg2_;
61 };
62 
63 }}}
64 
65 namespace Eigen { namespace internal {
66 
68 
69 template <typename Str, typename Arg1, typename Arg2>
70 struct traits<outer_expr<Str, Arg1, Arg2> >
71 {
72  typedef Eigen::Dense StorageKind;
73  typedef Eigen::MatrixXpr XprKind;
74  typedef typename Arg1::StorageIndex StorageIndex;
75  typedef typename Str::cov_type Scalar;
76  typedef typename Str::cov_type CoeffReturnType;
77 
78  enum {
79  Flags = Eigen::ColMajor,
80  RowsAtCompileTime = Arg1::RowsAtCompileTime,
81  ColsAtCompileTime = Arg2::RowsAtCompileTime,
82  MaxRowsAtCompileTime = Arg1::MaxRowsAtCompileTime,
83  MaxColsAtCompileTime = Arg2::MaxRowsAtCompileTime
84  };
85 };
86 
87 template <typename Str, typename Arg1, typename Arg2>
88 struct evaluator<outer_expr<Str, Arg1, Arg2> >
89  : evaluator_base<outer_expr<Str, Arg1, Arg2> >
90 {
92 
93  // determines the true type argument for the evaluated arguments:
94  // nested_eval<...>::type translates to either a temporary or a on-the-fly
95  // evaluated object, while remove_all<...> removes const/ref qualifiers
96  typedef typename nested_eval<Arg1, XprType::ColsAtCompileTime>::type Arg1Nested;
97  typedef typename nested_eval<Arg2, XprType::ColsAtCompileTime>::type Arg2Nested;
98  typedef typename remove_all<Arg1Nested>::type Arg1NestedCleaned;
99  typedef typename remove_all<Arg2Nested>::type Arg2NestedCleaned;
100 
101  typedef typename XprType::CoeffReturnType CoeffReturnType;
102  enum {
103  CoeffReadCost = evaluator<Arg1NestedCleaned>::CoeffReadCost,
104  Flags = Eigen::ColMajor
105  };
106 
107  evaluator(const XprType& xpr)
108  : arg1_impl_(xpr.arg1_)
109  , arg2_impl_(xpr.arg2_)
110  , rows_(xpr.rows())
111  , cols_(xpr.cols())
112  { }
113 
114  CoeffReturnType coeff(Index i, Index j) const
115  {
116  assert(i >= 0 && i < rows_);
117  assert(j >= 0 && j < cols_);
118  return Str::outer(arg1_impl_.coeff(i), arg2_impl_.coeff(j));
119  }
120 
121 private:
122  evaluator<Arg1NestedCleaned> arg1_impl_;
123  evaluator<Arg2NestedCleaned> arg2_impl_;
124  const Index rows_, cols_;
125 };
126 
127 }} /* namespace Eigen::internal */
outer_expr< Str, Arg1, Arg2 > outer(const Eigen::MatrixBase< Arg1 > &arg1, const Eigen::MatrixBase< Arg2 > &arg2)
Definition: outer.hpp:31
remove_all< Arg1Nested >::type Arg1NestedCleaned
Definition: outer.hpp:98
outer_expr(const Arg1 &arg1, const Arg2 &arg2)
Definition: outer.hpp:48
remove_all< Arg2Nested >::type Arg2NestedCleaned
Definition: outer.hpp:99
Eigen::internal::ref_selector< outer_expr >::type Nested
Definition: outer.hpp:43
Eigen::internal::ref_selector< Arg2 >::type Arg2Nested
Definition: outer.hpp:46
nested_eval< Arg1, XprType::ColsAtCompileTime >::type Arg1Nested
Definition: outer.hpp:96
Eigen::internal::ref_selector< Arg1 >::type Arg1Nested
Definition: outer.hpp:45
nested_eval< Arg2, XprType::ColsAtCompileTime >::type Arg2Nested
Definition: outer.hpp:97
CoeffReturnType coeff(Index i, Index j) const
Definition: outer.hpp:114