11 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_TRIANGULAR_MATRIX_H
12 #define INCLUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_TRIANGULAR_MATRIX_H
17 namespace Molassembler {
19 namespace UpperTriangularMatrixImpl {
22 template<
typename T,
size_t size>
23 using ArrayType = std::array<T, size>;
25 namespace index_conversion {
27 template<
size_t N,
typename Un
signedType>
28 PURITY_STRONG constexpr
unsigned toSingleIndex(
const UnsignedType i,
const UnsignedType j) {
31 - (N - i) * ((N - i) - 1) / 2
37 template<
size_t N,
typename Un
signedType>
38 PURITY_STRONG constexpr std::pair<UnsignedType, UnsignedType> toDoubleIndex(
const UnsignedType k) {
41 -
static_cast<unsigned>(
59 + (N - i) * ((N - i) - 1) / 2
66 PURITY_STRONG constexpr
bool isValidMatrixSize(
const size_t dataSize) {
67 auto root = Math::sqrt(1.0 + 8.0 * dataSize);
70 Floating::isCloseRelative(
72 static_cast<double>(Math::floor(root)),
79 PURITY_STRONG constexpr
size_t getMatrixSize(
const size_t dataSize) {
81 1 + Math::sqrt(1.0 + 8.0 * dataSize)
92 template<
typename ValueType,
size_t dataSize>
95 using DataType = UpperTriangularMatrixImpl::ArrayType<ValueType, dataSize>;
99 static constexpr
unsigned N = UpperTriangularMatrixImpl::getMatrixSize(dataSize);
109 template<
typename,
size_t>
class ArrayType
111 const ArrayType<ValueType, dataSize>& data
115 UpperTriangularMatrixImpl::isValidMatrixSize(dataSize) && dataSize > 0,
116 "Passed data size is not consistent with an upper triangular matrix!"
128 constexpr ValueType&
at(
const unsigned i,
const unsigned j) {
129 if(i >= j || i >= N || j >= N) {
130 throw "Index out of bounds!";
134 UpperTriangularMatrixImpl::index_conversion::toSingleIndex<N>(i, j)
147 if(i >= j || i >= N || j >= N) {
148 throw "Index out of bounds!";
152 UpperTriangularMatrixImpl::index_conversion::toSingleIndex<N>(i, j)
169 template<
typename,
size_t>
class ArrayType,
173 const ArrayType<ValueType, size>& data
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.