Molassembler  1.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
UpperTriangularMatrix.h
Go to the documentation of this file.
1 
11 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_TRIANGULAR_MATRIX_H
12 #define INCLUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_TRIANGULAR_MATRIX_H
13 
16 
17 namespace Scine {
18 namespace Molassembler {
19 namespace Temple {
20 namespace UpperTriangularMatrixImpl {
21 
26 template<typename T, size_t size>
27 using ArrayType = Array<T, size>;
28 
29 namespace index_conversion {
31  template<size_t N, typename UnsignedType>
32  PURITY_STRONG constexpr unsigned toSingleIndex(const UnsignedType i, const UnsignedType j) {
33  return (
34  N * (N - 1) / 2
35  - (N - i) * ((N - i) - 1) / 2
36  + j - i - 1
37  );
38  }
39 
41  template<size_t N, typename UnsignedType>
42  PURITY_STRONG constexpr std::pair<UnsignedType, UnsignedType> toDoubleIndex(const UnsignedType k) {
43  UnsignedType i {
44  N - 2
45  - static_cast<unsigned>(
46  Math::floor(
47  Math::sqrt(
48  0.0
49  + 4 * N * (N - 1)
50  - 8 * k
51  - 7
52  ) / 2.0
53  - 0.5
54  )
55  )
56  };
57 
58  return {
59  i,
60  (
61  k + i + 1
62  - N * (N - 1) / 2
63  + (N - i) * ((N - i) - 1) / 2
64  )
65  };
66  }
67 } // namespace index_conversion
68 
70 PURITY_STRONG constexpr bool isValidMatrixSize(const size_t dataSize) {
71  auto root = Math::sqrt(1.0 + 8 * dataSize);
72 
73  return(
74  Floating::isCloseRelative(
75  root,
76  static_cast<double>(Math::floor(root)),
77  1e-6
78  )
79  );
80 }
81 
83 PURITY_STRONG constexpr size_t getMatrixSize(const size_t dataSize) {
84  return (
85  1 + Math::sqrt(1.0 + 8 * dataSize)
86  ) / 2;
87 }
88 
89 } // namespace UpperTriangularMatrixImpl
90 
96 template<typename ValueType, size_t dataSize>
98 public:
100 
103  static constexpr unsigned N = UpperTriangularMatrixImpl::getMatrixSize(dataSize);
105 
109  constexpr UpperTriangularMatrix() : data_ {} {}
110 
112  template<
113  template<typename, size_t> class ArrayType
114  > constexpr explicit UpperTriangularMatrix(
115  const ArrayType<ValueType, dataSize>& data
116  ) : data_(data)
117  {
118  static_assert(
119  UpperTriangularMatrixImpl::isValidMatrixSize(dataSize) && dataSize > 0,
120  "Passed data size is not consistent with an upper triangular matrix!"
121  );
122  }
124 
127 
132  constexpr ValueType& at(const unsigned i, const unsigned j) {
133  if(i >= j || i >= N || j >= N) {
134  throw "Index out of bounds!";
135  }
136 
137  return data_.at(
138  UpperTriangularMatrixImpl::index_conversion::toSingleIndex<N>(i, j)
139  );
140  }
141 
147  PURITY_WEAK constexpr const ValueType& at(
148  const unsigned i,
149  const unsigned j
150  ) const {
151  if(i >= j || i >= N || j >= N) {
152  throw "Index out of bounds!";
153  }
154 
155  return data_.at(
156  UpperTriangularMatrixImpl::index_conversion::toSingleIndex<N>(i, j)
157  );
158  }
160 
162  constexpr const DataType& getData() const {
163  return data_;
164  }
165 
166 private:
167 /* State */
168  DataType data_;
169 };
170 
172 template<
173  template<typename, size_t> class ArrayType,
174  typename ValueType,
175  size_t size
177  const ArrayType<ValueType, size>& data
178 ) {
180 }
181 
182 } // namespace Temple
183 } // namespace Molassembler
184 } // namespace Scine
185 
186 #endif
constexpr const DataType & getData() const
Get full underlying data in raw form.
Definition: UpperTriangularMatrix.h:162
constexpr UpperTriangularMatrix< ValueType, size > makeUpperTriangularMatrix(const ArrayType< ValueType, size > &data)
Helper constructing function that deduces the required type signature.
Definition: UpperTriangularMatrix.h:176
constexpr ValueType & at(const unsigned i, const unsigned j)
Modifiable matrix accessor.
Definition: UpperTriangularMatrix.h:132
constexpr UpperTriangularMatrix(const ArrayType< ValueType, dataSize > &data)
Constructor from existing data.
Definition: UpperTriangularMatrix.h:114
#define PURITY_WEAK
Definition: Preprocessor.h:36
constexpr UpperTriangularMatrix()
Default constructor.
Definition: UpperTriangularMatrix.h:109
std::array-like class
Temple::Array< T, size > ArrayType
Definition: Properties.h:143
Strictly upper triangular matrix.
Definition: UpperTriangularMatrix.h:97
PURITY_WEAK constexpr const ValueType & at(const unsigned i, const unsigned j) const
Definition: UpperTriangularMatrix.h:147
#define PURITY_STRONG
Definition: Preprocessor.h:65
Helpers for comparing floating point values.