TVM  0.9.4
MatrixProperties.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <tvm/api.h>
6 
7 #include <utility>
8 
9 namespace tvm
10 {
11 
12 namespace internal
13 {
14 
17 {
18 public:
20  enum Shape
21  {
39  ZERO
40  };
41 
46  {
48  NA,
60  NON_ZERO_INDEFINITE
61  };
62 
64  struct Constness
65  {
66  public:
67  Constness(bool b = false) : b_(b) {}
68  operator bool() const { return b_; }
69 
70  private:
71  bool b_;
72  };
73 
76  {
77  public:
78  Invertibility(bool b = false) : b_(b) {}
79  operator bool() const { return b_; }
80 
81  private:
82  bool b_;
83  };
84 
107  template<typename... Args>
108  MatrixProperties(Args &&... args);
112 
115 
116  Shape shape() const;
117  Positiveness positiveness() const;
118  Constness constness() const;
119  Invertibility invertibility() const;
120 
121  bool isConstant() const;
122  bool isInvertible() const;
123  bool isTriangular() const;
124  bool isLowerTriangular() const;
125  bool isUpperTriangular() const;
126  bool isDiagonal() const;
127  bool isMultipleOfIdentity() const;
128  bool isIdentity() const;
129  bool isMinusIdentity() const;
130  bool isZero() const;
131  bool isSymmetric() const;
132  bool isPositiveSemiDefinite() const;
133  bool isPositiveDefinite() const;
134  bool isNegativeSemidefinite() const;
135  bool isNegativeDefinite() const;
136  bool isIndefinite() const;
137  bool isNonZeroIndefinite() const;
138 
139 private:
146 #define ADD_ARGUMENT(T, member, b1, b2) \
147  T member = T(); \
148  template<typename... Args> \
149  std::pair<bool, bool> processArgs(const T & m, const Args &... args) \
150  { \
151  static_assert(!check_args<T, Args...>(), #T " has already been specified"); \
152  member = m; \
153  auto p = processArgs(args...); \
154  return {p.first || b1, p.second || b2}; \
155  }
156 
157  struct Arguments
158  {
159  ADD_ARGUMENT(Shape, shape, false, false)
160  ADD_ARGUMENT(Positiveness, positiveness, false, false)
161  ADD_ARGUMENT(Constness, constant, true, false)
162  ADD_ARGUMENT(Invertibility, invertible, false, true)
163 
164  std::pair<bool, bool> processArgs() { return {false, false}; }
165 
166  private:
167  template<typename T, typename Arg0, typename... Args>
168  static constexpr bool check_args()
169  { return std::is_same<T, Arg0>::value || check_args<T, Args...>(); }
170 
171  template<typename T>
172  static constexpr bool check_args()
173  { return false; }
174  };
175 
176  void build(const Arguments & args, const std::pair<bool, bool> & checks);
177 
178  bool constant_;
179  bool invertible_;
180  Shape shape_;
181  bool symmetric_;
182  Positiveness positiveness_;
183 };
184 
185 // operators
191 
192 template<typename... Args>
193 inline MatrixProperties::MatrixProperties(Args &&... args)
194 {
195  Arguments a;
196  auto p = a.processArgs(args...);
197  build(a, p);
198 }
199 
201 : constant_(other.constant_), invertible_(other.invertible_), shape_(other.shape_), symmetric_(other.symmetric_),
202  positiveness_(other.positiveness_)
203 {}
204 
206 : MatrixProperties(const_cast<const MatrixProperties &>(other))
207 {}
208 
209 inline MatrixProperties::Shape MatrixProperties::shape() const { return shape_; }
210 
211 inline MatrixProperties::Positiveness MatrixProperties::positiveness() const { return positiveness_; }
212 
213 inline MatrixProperties::Constness MatrixProperties::constness() const { return constant_; }
214 
216 
217 inline bool MatrixProperties::isConstant() const { return constant_; }
218 
219 inline bool MatrixProperties::isInvertible() const { return invertible_; }
220 
221 inline bool MatrixProperties::isTriangular() const { return shape_ >= Shape::LOWER_TRIANGULAR; }
222 
223 inline bool MatrixProperties::isLowerTriangular() const { return shape_ == Shape::LOWER_TRIANGULAR || isDiagonal(); }
224 
225 inline bool MatrixProperties::isUpperTriangular() const { return shape_ >= Shape::UPPER_TRIANGULAR; }
226 
227 inline bool MatrixProperties::isDiagonal() const { return shape_ >= Shape::DIAGONAL; }
228 
229 inline bool MatrixProperties::isMultipleOfIdentity() const { return shape_ >= Shape::MULTIPLE_OF_IDENTITY; }
230 
231 inline bool MatrixProperties::isIdentity() const { return shape_ == Shape::IDENTITY; }
232 
233 inline bool MatrixProperties::isMinusIdentity() const { return shape_ == Shape::MINUS_IDENTITY; }
234 
235 inline bool MatrixProperties::isZero() const { return shape_ == Shape::ZERO; }
236 
237 inline bool MatrixProperties::isSymmetric() const { return symmetric_; }
238 
240 { return positiveness_ == Positiveness::POSITIVE_SEMIDEFINITE || isPositiveDefinite() || isZero(); }
241 
242 inline bool MatrixProperties::isPositiveDefinite() const { return positiveness_ == Positiveness::POSITIVE_DEFINITE; }
243 
245 { return positiveness_ == Positiveness::NEGATIVE_SEMIDEFINITE || isNegativeDefinite() || isZero(); }
246 
247 inline bool MatrixProperties::isNegativeDefinite() const { return positiveness_ == Positiveness::NEGATIVE_DEFINITE; }
248 
249 inline bool MatrixProperties::isIndefinite() const { return positiveness_ != Positiveness::NA; }
250 
252 { return positiveness_ == Positiveness::NON_ZERO_INDEFINITE || isPositiveDefinite() || isNegativeDefinite(); }
253 
254 } // namespace internal
255 
256 } // namespace tvm
#define ADD_ARGUMENT(T, member, b1, b2)
Definition: MatrixProperties.h:146
#define TVM_DLLAPI
Definition: api.h:35
Definition: MatrixProperties.h:17
bool isNegativeSemidefinite() const
Definition: MatrixProperties.h:244
bool isDiagonal() const
Definition: MatrixProperties.h:227
bool isPositiveSemiDefinite() const
Definition: MatrixProperties.h:239
Shape shape() const
Definition: MatrixProperties.h:209
bool isMinusIdentity() const
Definition: MatrixProperties.h:233
Constness constness() const
Definition: MatrixProperties.h:213
bool isNegativeDefinite() const
Definition: MatrixProperties.h:247
Shape
Definition: MatrixProperties.h:21
@ MINUS_IDENTITY
Definition: MatrixProperties.h:37
@ GENERAL
Definition: MatrixProperties.h:23
@ MULTIPLE_OF_IDENTITY
Definition: MatrixProperties.h:33
@ DIAGONAL
Definition: MatrixProperties.h:31
@ IDENTITY
Definition: MatrixProperties.h:35
@ LOWER_TRIANGULAR
Definition: MatrixProperties.h:25
@ UPPER_TRIANGULAR
Definition: MatrixProperties.h:27
bool isMultipleOfIdentity() const
Definition: MatrixProperties.h:229
bool isUpperTriangular() const
Definition: MatrixProperties.h:225
bool isTriangular() const
Definition: MatrixProperties.h:221
bool isPositiveDefinite() const
Definition: MatrixProperties.h:242
bool isIndefinite() const
Definition: MatrixProperties.h:249
bool isInvertible() const
Definition: MatrixProperties.h:219
Positiveness
Definition: MatrixProperties.h:46
@ INDEFINITE
Definition: MatrixProperties.h:58
@ NEGATIVE_SEMIDEFINITE
Definition: MatrixProperties.h:54
@ POSITIVE_DEFINITE
Definition: MatrixProperties.h:52
@ NA
Definition: MatrixProperties.h:48
@ NEGATIVE_DEFINITE
Definition: MatrixProperties.h:56
@ POSITIVE_SEMIDEFINITE
Definition: MatrixProperties.h:50
MatrixProperties & operator=(MatrixProperties &&)=default
bool isIdentity() const
Definition: MatrixProperties.h:231
MatrixProperties(MatrixProperties &&)=default
bool isNonZeroIndefinite() const
Definition: MatrixProperties.h:251
Positiveness positiveness() const
Definition: MatrixProperties.h:211
bool isSymmetric() const
Definition: MatrixProperties.h:237
bool isLowerTriangular() const
Definition: MatrixProperties.h:223
Invertibility invertibility() const
Definition: MatrixProperties.h:215
bool isConstant() const
Definition: MatrixProperties.h:217
bool isZero() const
Definition: MatrixProperties.h:235
MatrixProperties & operator=(const MatrixProperties &)=default
TVM_DLLAPI MatrixProperties operator+(const MatrixProperties &, const MatrixProperties &)
TVM_DLLAPI MatrixProperties operator*(double, const MatrixProperties &)
TVM_DLLAPI MatrixProperties operator-(const MatrixProperties &)
@ ZERO
Definition: CompiledAssignment.h:69
@ DIAGONAL
Definition: CompiledAssignment.h:38
@ IDENTITY
Definition: CompiledAssignment.h:47
Definition: Clock.h:12
Definition: MatrixProperties.h:65
Constness(bool b=false)
Definition: MatrixProperties.h:67
Definition: MatrixProperties.h:76
Invertibility(bool b=false)
Definition: MatrixProperties.h:78