Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/thelfer/tfel
Browse files Browse the repository at this point in the history
  • Loading branch information
thelfer committed Feb 5, 2024
2 parents be85069 + 07c8be9 commit 1bc8b57
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 86 deletions.
46 changes: 27 additions & 19 deletions include/TFEL/Math/Array/ArrayConcept.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,30 @@ namespace tfel::math {
*/
struct ArrayTag {}; // end of ArrayTag

/*!
* \class ArrayConceptBase
* \brief A class used to model the concept of arrays.
*/
template <typename T>
struct ArrayConcept {
//! \brief type alias
typedef ArrayTag ConceptTag;

protected:
ArrayConcept() = default;
ArrayConcept(ArrayConcept&&) = default;
ArrayConcept(const ArrayConcept&) = default;
ArrayConcept& operator=(const ArrayConcept&) = default;
~ArrayConcept() = default;
struct ArrayConceptBase {
using ConceptTag = ArrayTag;
};

/*!
* \brief an helper function which returns if the given type implements
* the `ArrayConcept`.
* \tparam ArrayType: type tested
* \brief definition of the ArrayConcept concept
* a class matching the array concept must expose the `ArrayTag` and have
* access operators.
*/
template <typename ArrayType>
TFEL_HOST_DEVICE constexpr bool implementsArrayConcept() {
return tfel::meta::implements<ArrayType, ArrayConcept>();
} // end of implementsArrayConcept
concept ArrayConcept =
(std::is_same_v<typename std::decay_t<ArrayType>::ConceptTag, ArrayTag>)&& //
(requires(const ArrayType t, const index_type<ArrayType> i) { t[i]; }) && //
(requires(const ArrayType t, const index_type<ArrayType> i) { t(i); });

//! paratial specialisation for arrays
template <typename Type>
struct ConceptRebind<ArrayTag, Type> {
using type = ArrayConcept<Type>;
template <typename ArrayType>
struct ConceptRebind<ArrayTag, ArrayType> {
using type = ArrayConceptBase<ArrayType>;
};

//! \brief partial specialisation of ComputeUnaryResult_ for arrays
Expand All @@ -73,6 +70,17 @@ namespace tfel::math {
Expr<Result, UnaryOperation<A, OpNeg>>>;
};

/*!
* \brief an helper function which returns if the given type implements
* the `ArrayConcept`.
* \tparam ArrayType: type tested
*/
template <typename ArrayType>
[[deprecated]] TFEL_HOST_DEVICE constexpr bool implementsArrayConcept() {
// return tfel::meta::implements<ArrayType, ArrayConceptBase>;
return ArrayConcept<ArrayType>;
} // end of implementsArrayConcept

} // end of namespace tfel::math

#endif /* LIB_TFEL_MATH_ARRAY_CONCEPT_HXX */
54 changes: 30 additions & 24 deletions include/TFEL/Math/Matrix/MatrixConcept.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,55 @@ namespace tfel::math {
struct MatrixTag {}; // end of MatrixTag

/*!
* \class MatrixConcept
* \class MatrixConceptBase
* \brief A class used to model the concept of matrices.
* Here we use the curiously recurring template pattern.
* \param T, a matrix type.
* \author Thomas Helfer
* \date 04 May 2006
*/
template <typename T>
struct MatrixConcept {
template <typename MatrixType>
struct MatrixConceptBase {
using ConceptTag = MatrixTag;

protected:
MatrixConcept() = default;
MatrixConcept(MatrixConcept&&) = default;
MatrixConcept(const MatrixConcept&) = default;
MatrixConcept& operator=(const MatrixConcept&) = default;
~MatrixConcept() = default;
};

/*!
* \brief an helper function which returns if the given type implements the
* `MatrixConcept`.
* \tparam MatrixType: type tested
* \brief definition of the MatrixConcept concept
* a class matching the matrix concept must expose the `MatrixTag` and have
* access operators.
*/
template <typename MatrixType>
TFEL_HOST_DEVICE constexpr bool implementsMatrixConcept() {
return tfel::meta::implements<MatrixType, MatrixConcept>();
} // end of implementsMatrixConcept
concept MatrixConcept =
(std::is_same_v<typename std::decay_t<MatrixType>::ConceptTag,
MatrixTag>)&&(requires(const MatrixType t,
const index_type<MatrixType> i,
const index_type<MatrixType> j) {
t(i, j);
});

//! paratial specialisation for matrices
template <typename Type>
struct ConceptRebind<MatrixTag, Type> {
using type = MatrixConcept<Type>;
template <typename MatrixType>
struct ConceptRebind<MatrixTag, MatrixType> {
using type = MatrixConceptBase<MatrixType>;
};

//! \brief a simple alias for backward compatibility with versions prior
//! to 4.0
template <typename MatrixType>
using MatrixTraits =
std::conditional_t<implementsMatrixConcept<MatrixType>(),
std::conditional_t<MatrixConcept<MatrixType>,
MathObjectTraits<MatrixType>,
MathObjectTraits<tfel::meta::InvalidType>>;

/*!
* \brief an helper function which returns if the given type implements the
* `MatrixConcept`.
* \tparam MatrixType: type tested
* \note function given for backward compatibility with versions prior
* to 5.0
*/
template <typename MatrixType>
[[deprecated]] TFEL_HOST_DEVICE constexpr bool implementsMatrixConcept() {
return MatrixConcept<MatrixType>;
// return tfel::meta::implements<MatrixType, MatrixConcept>();
} // end of implementsMatrixConcept

} // end of namespace tfel::math

#endif /* LIB_TFEL_MATH_MATRIXCONCEPT_HXX */
14 changes: 4 additions & 10 deletions include/TFEL/Math/Matrix/tmatrix.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,13 @@ namespace tfel::math::internals {
} // end of exe
}; // end of struct DerivativeViewFromTinyMatrixImplementation

template <typename Matrix>
constexpr std::enable_if_t<
implementsMatrixConcept<Matrix>(),
typename ComputeUnaryResult<numeric_type<Matrix>, Power<3>>::Result>
det2(const Matrix& m) {
template <MatrixConcept MatrixType>
TFEL_HOST_DEVICE constexpr auto det2(const MatrixType& m) noexcept {
return m(0, 0) * m(1, 1) - m(1, 0) * m(0, 1);
}

template <typename Matrix>
constexpr std::enable_if_t<
implementsMatrixConcept<Matrix>(),
typename ComputeUnaryResult<numeric_type<Matrix>, Power<3>>::Result>
det3(const Matrix& m) {
template <MatrixConcept MatrixType>
TFEL_HOST_DEVICE constexpr auto det3(const MatrixType& m) noexcept {
const auto a = m(0, 0);
const auto b = m(0, 1);
const auto c = m(0, 2);
Expand Down
4 changes: 2 additions & 2 deletions include/TFEL/Math/Tensor/MatrixViewFromTensor.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace tfel::math {
struct Expr<tmatrix<3u, 3u, numeric_type<TensorType>>,
MatrixViewFromTensorExpr<TensorType>>
: public ExprBase,
public MatrixConcept<Expr<tmatrix<3u, 3u, numeric_type<TensorType>>,
MatrixViewFromTensorExpr<TensorType>>> {
public MatrixConceptBase<Expr<tmatrix<3u, 3u, numeric_type<TensorType>>,
MatrixViewFromTensorExpr<TensorType>>> {
using NumType = numeric_type<TensorType>;
typedef unsigned short IndexType;
typedef EmptyRunTimeProperties RunTimeProperties;
Expand Down
8 changes: 4 additions & 4 deletions include/TFEL/Math/Vector/VectorConcept.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ namespace tfel::math {
* a class matching the vector concept must expose the `VectorTag` and have
* access operators.
*/
template <typename T>
template <typename VectorType>
concept VectorConcept =
(std::is_same_v<typename std::decay_t<T>::ConceptTag, VectorTag>)&& //
(requires(const T t, const unsigned short i) { t[i]; }) && //
(requires(const T t, const unsigned short i) { t(i); });
(std::is_same_v<typename std::decay_t<VectorType>::ConceptTag, VectorTag>)&& //
(requires(const VectorType t, const index_type<VectorType> i) { t[i]; }) && //
(requires(const VectorType t, const index_type<VectorType> i) { t(i); });

//! paratial specialisation for vectors
template <typename Type>
Expand Down
2 changes: 1 addition & 1 deletion include/TFEL/Math/fsarray.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace tfel::math {
template <unsigned short N, typename ValueType = double>
struct fsarray : GenericFixedSizeArray<fsarray<N, ValueType>,
FixedSizeVectorPolicy<N, ValueType>>,
ArrayConcept<fsarray<N, ValueType>> {
ArrayConceptBase<fsarray<N, ValueType>> {
//! \brief default constructor
constexpr fsarray() noexcept = default;
//! \brief move constructor
Expand Down
2 changes: 1 addition & 1 deletion include/TFEL/Math/matrix.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace tfel::math {
*/
template <typename ValueType>
struct matrix
: VectorConceptBase<matrix<ValueType>>,
: MatrixConceptBase<matrix<ValueType>>,
GenericRuntimeArray<matrix<ValueType>,
RuntimeRowMajorMatrixArrayPolicy<ValueType>> {
//! \brief a simple alias
Expand Down
2 changes: 1 addition & 1 deletion include/TFEL/Math/runtime_array.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace tfel::math {
struct runtime_array
: GenericRuntimeArray<runtime_array<ValueType>,
RuntimeVectorArrayPolicy<ValueType>>,
ArrayConcept<runtime_array<ValueType>> {
ArrayConceptBase<runtime_array<ValueType>> {
//! \brief a simple alias
using GenericRuntimeArrayBase =
GenericRuntimeArray<runtime_array<ValueType>,
Expand Down
2 changes: 1 addition & 1 deletion include/TFEL/Math/tmatrix.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ namespace tfel::math {

template <unsigned short N, unsigned short M, typename ValueType = double>
struct tmatrix
: MatrixConcept<tmatrix<N, M, ValueType>>,
: MatrixConceptBase<tmatrix<N, M, ValueType>>,
GenericFixedSizeArray<tmatrix<N, M, ValueType>,
FixedSizeRowMajorMatrixPolicy<N, M, ValueType>> {
static_assert((N != 0) && (M != 0));
Expand Down
14 changes: 7 additions & 7 deletions tests/Math/include/function_unary_tests.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ void function(const VectorType&,
const tfel::math::numeric_type<VectorType>,
const unsigned int);

template <class T>
void function(const tfel::math::MatrixConcept<T>&,
const typename tfel::math::MathObjectTraits<T>::NumType,
const typename tfel::math::MathObjectTraits<T>::NumType,
const typename tfel::math::MathObjectTraits<T>::NumType,
const typename tfel::math::MathObjectTraits<T>::NumType,
const typename tfel::math::MathObjectTraits<T>::NumType,
template <tfel::math::MatrixConcept MatrixType>
void function(const MatrixType&,
const tfel::math::numeric_type<MatrixType>,
const tfel::math::numeric_type<MatrixType>,
const tfel::math::numeric_type<MatrixType>,
const tfel::math::numeric_type<MatrixType>,
const tfel::math::numeric_type<MatrixType>,
const unsigned int);

void function(const tfel::math::StensorConcept auto&);
Expand Down
24 changes: 8 additions & 16 deletions tests/Math/include/function_unary_tests.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
} \
static_cast<void>(0)

// template <typename VectorType>
// std::enable_if_t<tfel::math::implementsVectorConcept<VectorType>(), void>
// function(const VectorType& x) {
// std::cout << x(0) << " " << x(1) << " " << x(2) << std::endl;
// std::cout << "------" << std::endl;
// }

void function(const tfel::math::VectorConcept auto& x) {
std::cout << x(0) << " " << x(1) << " " << x(2) << std::endl;
std::cout << "------" << std::endl;
Expand All @@ -54,15 +47,14 @@ void function(const VectorType& x,
ASSERT(std::abs(x(2) - v2) <= eps);
}

template <typename MatrixType>
std::enable_if_t<tfel::math::implementsMatrixConcept<MatrixType>(), void>
function(const MatrixType& x,
const tfel::math::numeric_type<MatrixType> v0,
const tfel::math::numeric_type<MatrixType> v1,
const tfel::math::numeric_type<MatrixType> v2,
const tfel::math::numeric_type<MatrixType> v3,
const tfel::math::numeric_type<MatrixType> eps,
const unsigned int test_number) {
template <tfel::math::MatrixConcept MatrixType>
void function(const MatrixType& x,
const tfel::math::numeric_type<MatrixType> v0,
const tfel::math::numeric_type<MatrixType> v1,
const tfel::math::numeric_type<MatrixType> v2,
const tfel::math::numeric_type<MatrixType> v3,
const tfel::math::numeric_type<MatrixType> eps,
const unsigned int test_number) {
#ifdef TFEL_VERBOSE
std::cout << "Test : " << test_number << std::endl;
#else
Expand Down

0 comments on commit 1bc8b57

Please sign in to comment.