TVM  0.9.4
MatrixWithProperties.h
Go to the documentation of this file.
1 
3 #pragma once
4 
6 #include <tvm/internal/meta.h>
7 
8 #include <Eigen/Core>
9 
10 namespace tvm
11 {
12 
13 namespace internal
14 {
15 // forward declaration
16 template<typename MatrixType, bool refOnProperties>
17 class ObjectWithProperties;
18 
25 template<typename MatrixType, bool refOnProperties = false>
27 {
28 public:
35  KeepProperties(ObjectWithProperties<MatrixType, refOnProperties> & M, bool keep) : M_(M), keep_(keep) {}
36 
38  template<typename OtherDerived>
39  ObjectWithProperties<MatrixType, refOnProperties> & operator=(const Eigen::MatrixBase<OtherDerived> & other);
40 
43  template<bool b>
45 
46 private:
48  const bool keep_;
49 };
50 
52 template<typename MatrixType, bool refOnProperties = false>
53 class ObjectWithProperties : public MatrixType
54 {
55 public:
56  using MatrixType::MatrixType;
57 
59 
60  template<typename OtherDerived>
61  ObjectWithProperties(const Eigen::MatrixBase<OtherDerived> & other) : MatrixType(other), properties_()
62  {}
63 
64  template<typename OtherDerived>
65  ObjectWithProperties(const Eigen::MatrixBase<OtherDerived> & other,
67  : MatrixType(other), properties_(p)
68  {}
69 
70  template<typename OtherType>
72  : MatrixType(other), properties_(other.properties())
73  {}
74 
75  template<typename OtherType>
76  ObjectWithProperties(ObjectWithProperties<OtherType> & other) : MatrixType(other), properties_(other.properties())
77  {}
78 
79  template<typename OtherDerived>
80  ObjectWithProperties & operator=(const Eigen::MatrixBase<OtherDerived> & other)
81  {
82  assert(this->rows() == other.rows() && this->cols() == other.cols()
83  && "It is not allowed to assign an expression with a different size. Please explicitly resize the matrix "
84  "before.");
85  this->MatrixType::operator=(other);
86  properties_ = MatrixProperties();
87  return *this;
88  }
89 
90  template<typename OtherDerived>
91  ObjectWithProperties & operator+=(const Eigen::MatrixBase<OtherDerived> & other)
92  {
93  assert(this->rows() == other.rows() && this->cols() == other.cols() && "Matrices must have the same size");
94  this->MatrixType::operator+=(other);
95  properties_ = MatrixProperties();
96  return *this;
97  }
98 
99  template<typename OtherDerived>
100  ObjectWithProperties & operator-=(const Eigen::MatrixBase<OtherDerived> & other)
101  {
102  assert(this->rows() == other.rows() && this->cols() == other.cols() && "Matrices must have the same size");
103  this->MatrixType::operator-=(other);
104  properties_ = MatrixProperties();
105  return *this;
106  }
107 
108  template<typename T, disable_for_templated_t<T, Eigen::MatrixBase> = 0>
109  ObjectWithProperties & operator+=(const T & other) = delete;
110 
111  template<typename T, disable_for_templated_t<T, Eigen::MatrixBase> = 0>
112  ObjectWithProperties & operator-=(const T & other) = delete;
113 
114  template<typename T>
115  ObjectWithProperties & operator*=(const T & other) = delete;
116 
117  template<typename T>
118  ObjectWithProperties & operator/=(const T & other) = delete;
119 
120  template<typename OtherDerived>
121  ObjectWithProperties & assignKeepProperties(const Eigen::MatrixBase<OtherDerived> & other)
122  {
123  assert(this->rows() == other.rows() && this->cols() == other.cols()
124  && "It is not allowed to assign an expression with a different size. Please explicitly resize the matrix "
125  "before.");
126  this->MatrixType::operator=(other);
127  return *this;
128  }
129 
130  const MatrixProperties & properties() const { return properties_; }
131  MatrixProperties & properties() { return properties_; }
132  void properties(const MatrixProperties & p) { properties_ = p; }
133 
140  KeepProperties<MatrixType, refOnProperties> keepProperties(bool keep) { return {*this, keep}; }
141 
142 private:
143  std::conditional_t<refOnProperties, std::add_lvalue_reference_t<MatrixProperties>, MatrixProperties> properties_;
144 };
145 
146 template<typename MatrixType, bool refOnProperties>
147 template<typename OtherDerived>
149  const Eigen::MatrixBase<OtherDerived> & other)
150 {
151  if(keep_)
152  {
153  M_.assignKeepProperties(other);
154  }
155  else
156  {
157  M_ = other;
158  }
159  return M_;
160 }
161 
165 
166 } // namespace internal
167 
168 } // namespace tvm
Definition: MatrixWithProperties.h:27
KeepProperties(ObjectWithProperties< MatrixType, refOnProperties > &M, bool keep)
Definition: MatrixWithProperties.h:35
ObjectWithProperties< MatrixType, refOnProperties > & operator=(const ObjectWithProperties< MatrixType, b > &)=delete
ObjectWithProperties< MatrixType, refOnProperties > & operator=(const Eigen::MatrixBase< OtherDerived > &other)
Definition: MatrixWithProperties.h:148
Definition: MatrixProperties.h:17
Definition: MatrixWithProperties.h:54
const MatrixProperties & properties() const
Definition: MatrixWithProperties.h:130
ObjectWithProperties()
Definition: MatrixWithProperties.h:58
ObjectWithProperties(ObjectWithProperties< OtherType > &other)
Definition: MatrixWithProperties.h:76
KeepProperties< MatrixType, refOnProperties > keepProperties(bool keep)
Definition: MatrixWithProperties.h:140
ObjectWithProperties & operator+=(const T &other)=delete
ObjectWithProperties & operator*=(const T &other)=delete
ObjectWithProperties & operator-=(const T &other)=delete
ObjectWithProperties & operator+=(const Eigen::MatrixBase< OtherDerived > &other)
Definition: MatrixWithProperties.h:91
ObjectWithProperties & operator-=(const Eigen::MatrixBase< OtherDerived > &other)
Definition: MatrixWithProperties.h:100
MatrixProperties & properties()
Definition: MatrixWithProperties.h:131
ObjectWithProperties & operator/=(const T &other)=delete
ObjectWithProperties(const Eigen::MatrixBase< OtherDerived > &other)
Definition: MatrixWithProperties.h:61
ObjectWithProperties & assignKeepProperties(const Eigen::MatrixBase< OtherDerived > &other)
Definition: MatrixWithProperties.h:121
ObjectWithProperties & operator=(const Eigen::MatrixBase< OtherDerived > &other)
Definition: MatrixWithProperties.h:80
void properties(const MatrixProperties &p)
Definition: MatrixWithProperties.h:132
ObjectWithProperties(const Eigen::MatrixBase< OtherDerived > &other, tvm::internal::const_if_t< MatrixProperties, !refOnProperties > &p)
Definition: MatrixWithProperties.h:65
ObjectWithProperties(const ObjectWithProperties< OtherType > &other)
Definition: MatrixWithProperties.h:71
typename const_if< T, c >::type const_if_t
Definition: meta.h:99
Definition: Clock.h:12