Molassembler  3.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 
15 
16 namespace Scine {
17 namespace Molassembler {
18 namespace Temple {
19 namespace UpperTriangularMatrixImpl {
20 
22 template<typename T, size_t size>
23 using ArrayType = std::array<T, size>;
24 
25 namespace index_conversion {
27  template<size_t N, typename UnsignedType>
28  PURITY_STRONG constexpr unsigned toSingleIndex(const UnsignedType i, const UnsignedType j) {
29  return (
30  N * (N - 1) / 2
31  - (N - i) * ((N - i) - 1) / 2
32  + j - i - 1
33  );
34  }
35 
37  template<size_t N, typename UnsignedType>
38  PURITY_STRONG constexpr std::pair<UnsignedType, UnsignedType> toDoubleIndex(const UnsignedType k) {
39  UnsignedType i {
40  N - 2
41  - static_cast<unsigned>(
42  Math::floor(
43  Math::sqrt(
44  0.0
45  + 4 * N * (N - 1)
46  - 8 * k
47  - 7
48  ) / 2.0
49  - 0.5
50  )
51  )
52  };
53 
54  return {
55  i,
56  (
57  k + i + 1
58  - N * (N - 1) / 2
59  + (N - i) * ((N - i) - 1) / 2
60  )
61  };
62  }
63 } // namespace index_conversion
64 
66 PURITY_STRONG constexpr bool isValidMatrixSize(const size_t dataSize) {
67  auto root = Math::sqrt(1.0 + 8.0 * dataSize);
68 
69  return(
70  Floating::isCloseRelative(
71  root,
72  static_cast<double>(Math::floor(root)),
73  1e-6
74  )
75  );
76 }
77 
79 PURITY_STRONG constexpr size_t getMatrixSize(const size_t dataSize) {
80  return (
81  1 + Math::sqrt(1.0 + 8.0 * dataSize)
82  ) / 2;
83 }
84 
85 } // namespace UpperTriangularMatrixImpl
86 
92 template<typename ValueType, size_t dataSize>
94 public:
95  using DataType = UpperTriangularMatrixImpl::ArrayType<ValueType, dataSize>;
96 
99  static constexpr unsigned N = UpperTriangularMatrixImpl::getMatrixSize(dataSize);
101 
105  constexpr UpperTriangularMatrix() : data_ {} {}
106 
108  template<
109  template<typename, size_t> class ArrayType
110  > constexpr explicit UpperTriangularMatrix(
111  const ArrayType<ValueType, dataSize>& data
112  ) : data_(data)
113  {
114  static_assert(
115  UpperTriangularMatrixImpl::isValidMatrixSize(dataSize) && dataSize > 0,
116  "Passed data size is not consistent with an upper triangular matrix!"
117  );
118  }
120 
123 
128  constexpr ValueType& at(const unsigned i, const unsigned j) {
129  if(i >= j || i >= N || j >= N) {
130  throw "Index out of bounds!";
131  }
132 
133  return data_.at(
134  UpperTriangularMatrixImpl::index_conversion::toSingleIndex<N>(i, j)
135  );
136  }
137 
143  PURITY_WEAK constexpr const ValueType& at(
144  const unsigned i,
145  const unsigned j
146  ) const {
147  if(i >= j || i >= N || j >= N) {
148  throw "Index out of bounds!";
149  }
150 
151  return data_.at(
152  UpperTriangularMatrixImpl::index_conversion::toSingleIndex<N>(i, j)
153  );
154  }
156 
158  constexpr const DataType& getData() const {
159  return data_;
160  }
161 
162 private:
163 /* State */
164  DataType data_;
165 };
166 
168 template<
169  template<typename, size_t> class ArrayType,
170  typename ValueType,
171  size_t size
173  const ArrayType<ValueType, size>& data
174 ) {
176 }
177 
178 } // namespace Temple
179 } // namespace Molassembler
180 } // namespace Scine
181 
182 #endif
constexpr const DataType & getData() const
Get full underlying data in raw form.
Definition: UpperTriangularMatrix.h:158
constexpr UpperTriangularMatrix< ValueType, size > makeUpperTriangularMatrix(const ArrayType< ValueType, size > &data)
Helper constructing function that deduces the required type signature.
Definition: UpperTriangularMatrix.h:172
constexpr ValueType & at(const unsigned i, const unsigned j)
Modifiable matrix accessor.
Definition: UpperTriangularMatrix.h:128
constexpr UpperTriangularMatrix(const ArrayType< ValueType, dataSize > &data)
Constructor from existing data.
Definition: UpperTriangularMatrix.h:110
#define PURITY_WEAK
Definition: Preprocessor.h:36
constexpr UpperTriangularMatrix()
Default constructor.
Definition: UpperTriangularMatrix.h:105
Strictly upper triangular matrix.
Definition: UpperTriangularMatrix.h:93
PURITY_WEAK constexpr const ValueType & at(const unsigned i, const unsigned j) const
Definition: UpperTriangularMatrix.h:143
#define PURITY_STRONG
Definition: Preprocessor.h:65
Helpers for comparing floating point values.