feat(math): begin work on the tensor/math library
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
#ifndef CENET_MATH_TENSOR_TRAITS_HPP
|
||||
#define CENET_MATH_TENSOR_TRAITS_HPP
|
||||
|
||||
#include "tensor_fwd.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace cenet {
|
||||
namespace math {
|
||||
|
||||
template <typename T>
|
||||
struct tensor_storage {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_storage<tensor<T, Dims...>> {
|
||||
static_assert(std::is_arithmetic_v<T>);
|
||||
|
||||
using value_type = T;
|
||||
using storage_type = T*;
|
||||
};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_storage<tensor<inline_storage<T>, Dims...>> {
|
||||
static_assert(std::is_arithmetic_v<T>);
|
||||
|
||||
using value_type = T;
|
||||
using storage_type = T[(Dims * ... * 1)];
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct is_storage_inline : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_storage_inline<inline_storage<T>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
static constexpr auto is_storage_inline_v = is_storage_inline<T>::value;
|
||||
|
||||
template <typename T>
|
||||
struct is_tensor : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct is_tensor<tensor<T, Dims...>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
static constexpr auto is_tensor_v = is_tensor<T>::value;
|
||||
|
||||
template <typename T>
|
||||
struct is_tensor_dynamic : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_tensor_dynamic<tensor<T>> : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t First, std::size_t... Rest>
|
||||
struct is_tensor_dynamic<tensor<T, First, Rest...>> : is_tensor_dynamic<tensor<T, Rest...>> {};
|
||||
|
||||
template <typename T, std::size_t... Rest>
|
||||
struct is_tensor_dynamic<tensor<T, dynamic_extent, Rest...>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
static constexpr auto is_tensor_dynamic_v = is_tensor_dynamic<T>::value;
|
||||
|
||||
template <typename T>
|
||||
struct tensor_traits {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_traits<tensor<T, Dims...>> {
|
||||
using tensor_type = tensor<T, Dims...>;
|
||||
using value_type = tensor_storage<tensor_type>::value_type;
|
||||
using storage_type = tensor_storage<tensor_type>::storage_type;
|
||||
|
||||
static constexpr auto rank = sizeof...(Dims);
|
||||
static constexpr auto dynamic = is_tensor_dynamic_v<tensor_type>;
|
||||
|
||||
static constexpr auto inline_storage = is_storage_inline_v<T>;
|
||||
static_assert(!inline_storage || !dynamic, "Tensor storage set to inline with dynamic extents!");
|
||||
|
||||
static constexpr std::enable_if_t<(sizeof...(Dims) > 0), std::size_t[sizeof...(Dims)]> dimensions = { Dims... };
|
||||
|
||||
static constexpr std::size_t size = dynamic ? dynamic_extent : (Dims * ... * 1);
|
||||
};
|
||||
|
||||
} // namespace math
|
||||
} // namespace cenet
|
||||
|
||||
#endif // CENET_MATH_TENSOR_TRAITS_HPP
|
||||
Reference in New Issue
Block a user