refactor(math): generalize tensor operations
This commit is contained in:
+10
-99
@@ -2,42 +2,26 @@
|
||||
#define CENET_MATH_OPS_HPP
|
||||
|
||||
#include "tensor_shape.hpp"
|
||||
#include "tensor_traits.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
|
||||
namespace cenet {
|
||||
namespace math {
|
||||
namespace ops {
|
||||
|
||||
class plus {
|
||||
private:
|
||||
template <typename Op>
|
||||
struct generic_binary_op {
|
||||
template <typename L, typename R>
|
||||
struct result_type_impl {};
|
||||
|
||||
template <typename LT, std::size_t... LDims, typename RT, std::size_t... RDims>
|
||||
struct result_type_impl<tensor_view<LT, LDims...>, tensor_view<RT, RDims...>> {
|
||||
using lhs_view = tensor_view<LT, LDims...>;
|
||||
using rhs_view = tensor_view<RT, RDims...>;
|
||||
|
||||
using type = std::enable_if_t<
|
||||
detail::is_shape_equivalent<typename lhs_view::shape_type, typename rhs_view::shape_type>::value,
|
||||
tensor<decltype(std::declval<detach_storage_t<LT>>() + std::declval<detach_storage_t<RT>>()), LDims...>>;
|
||||
};
|
||||
public:
|
||||
template <typename L, typename R>
|
||||
using result_type = typename result_type_impl<L, R>::type;
|
||||
|
||||
template <typename L, typename R, typename = std::enable_if_t<(is_tensor_v<L> && is_tensor_v<R>)>>
|
||||
static bool compatible(const L& lhs, const R& rhs) {
|
||||
return lhs.shape() == rhs.shape();
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
// TODO: introduce "best shape" function/trait
|
||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
||||
return lhs.shape();
|
||||
} else {
|
||||
@@ -47,87 +31,14 @@ public:
|
||||
|
||||
template <typename LT, typename R>
|
||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
||||
return lhs + rhs.coeff(index);
|
||||
return Op{}(lhs, rhs.coeff(index));
|
||||
}
|
||||
};
|
||||
|
||||
class minus {
|
||||
private:
|
||||
template <typename L, typename R>
|
||||
struct result_type_impl {};
|
||||
|
||||
template <typename LT, std::size_t... LDims, typename RT, std::size_t... RDims>
|
||||
struct result_type_impl<tensor_view<LT, LDims...>, tensor_view<RT, RDims...>> {
|
||||
using lhs_view = tensor_view<LT, LDims...>;
|
||||
using rhs_view = tensor_view<RT, RDims...>;
|
||||
|
||||
using type = std::enable_if_t<
|
||||
detail::is_shape_equivalent<typename lhs_view::shape_type, typename rhs_view::shape_type>::value,
|
||||
tensor<decltype(std::declval<detach_storage_t<LT>>() - std::declval<detach_storage_t<RT>>()), LDims...>>;
|
||||
};
|
||||
public:
|
||||
template <typename L, typename R>
|
||||
using result_type = typename result_type_impl<L, R>::type;
|
||||
|
||||
template <typename L, typename R, typename = std::enable_if_t<(is_tensor_v<L> && is_tensor_v<R>)>>
|
||||
static bool compatible(const L& lhs, const R& rhs) {
|
||||
return lhs.shape() == rhs.shape();
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
||||
return lhs.shape();
|
||||
} else {
|
||||
return rhs.shape();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename LT, typename R>
|
||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
||||
return lhs - rhs.coeff(index);
|
||||
}
|
||||
};
|
||||
|
||||
class multiply {
|
||||
private:
|
||||
template <typename L, typename R>
|
||||
struct result_type_impl {};
|
||||
|
||||
template <typename LT, std::size_t... LDims, typename RT, std::size_t... RDims>
|
||||
struct result_type_impl<tensor_view<LT, LDims...>, tensor_view<RT, RDims...>> {
|
||||
using lhs_view = tensor_view<LT, LDims...>;
|
||||
using rhs_view = tensor_view<RT, RDims...>;
|
||||
|
||||
using type = std::enable_if_t<
|
||||
detail::is_shape_equivalent<typename lhs_view::shape_type, typename rhs_view::shape_type>::value,
|
||||
tensor<decltype(std::declval<detach_storage_t<LT>>() * std::declval<detach_storage_t<RT>>()), LDims...>>;
|
||||
};
|
||||
public:
|
||||
template <typename L, typename R>
|
||||
using result_type = typename result_type_impl<L, R>::type;
|
||||
|
||||
template <typename L, typename R, typename = std::enable_if_t<(is_tensor_v<L> && is_tensor_v<R>)>>
|
||||
static bool compatible(const L& lhs, const R& rhs) {
|
||||
return lhs.shape() == rhs.shape();
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
||||
return lhs.shape();
|
||||
} else {
|
||||
return rhs.shape();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename LT, typename R>
|
||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
||||
return lhs * rhs.coeff(index);
|
||||
}
|
||||
};
|
||||
struct plus : generic_binary_op<std::plus<>> {};
|
||||
struct minus : generic_binary_op<std::minus<>> {};
|
||||
struct multiply : generic_binary_op<std::multiplies<>> {};
|
||||
struct divide : generic_binary_op<std::divides<>> {};
|
||||
|
||||
} // namespace ops
|
||||
} // namespace math
|
||||
|
||||
Reference in New Issue
Block a user