feat(math): introduce scalar operations
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
#define CENET_MATH_EXPR_HPP
|
||||
|
||||
#include "ops.hpp"
|
||||
#include "tensor_traits.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
|
||||
namespace cenet {
|
||||
namespace math {
|
||||
@@ -15,12 +16,16 @@ template <typename Op>
|
||||
struct generic_binary_op {
|
||||
template <typename L, typename R>
|
||||
static bool compatible(const L& lhs, const R& rhs) {
|
||||
if constexpr (std::is_arithmetic_v<L> || std::is_arithmetic_v<R>) {
|
||||
return true;
|
||||
} else {
|
||||
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("tensor shape mismatch");
|
||||
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
|
||||
// TODO: introduce "best shape" function/trait
|
||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
||||
return lhs.shape();
|
||||
@@ -31,8 +36,14 @@ struct generic_binary_op {
|
||||
|
||||
template <typename L, typename R>
|
||||
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
|
||||
if constexpr (std::is_arithmetic_v<L>) {
|
||||
return Op{}(lhs, rhs.coeff(index));
|
||||
} else if constexpr (std::is_arithmetic_v<R>) {
|
||||
return Op{}(lhs.coeff(index), rhs);
|
||||
} else {
|
||||
return Op{}(lhs.coeff(index), rhs.coeff(index));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct plus : generic_binary_op<std::plus<>> {};
|
||||
|
||||
Reference in New Issue
Block a user