feat(math): add multiply operation
This commit is contained in:
@@ -99,6 +99,14 @@ binary_expression<L, R, Op> operator-(L&& lhs, R&& rhs) {
|
|||||||
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename L,
|
||||||
|
typename R,
|
||||||
|
typename Op = ops::multiply,
|
||||||
|
typename = std::void_t<typename detail::expression_result<binary_expression<L, R, Op>>::type>>
|
||||||
|
binary_expression<L, R, Op> operator*(L&& lhs, R&& rhs) {
|
||||||
|
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace math
|
} // namespace math
|
||||||
} // namespace cenet
|
} // namespace cenet
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#ifndef CENET_MATH_OPS_HPP
|
#ifndef CENET_MATH_OPS_HPP
|
||||||
#define CENET_MATH_OPS_HPP
|
#define CENET_MATH_OPS_HPP
|
||||||
|
|
||||||
#include "cenet/math/tensor_shape.hpp"
|
#include "tensor_shape.hpp"
|
||||||
#include "tensor_traits.hpp"
|
#include "tensor_traits.hpp"
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
@@ -90,6 +90,45 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace ops
|
} // namespace ops
|
||||||
} // namespace math
|
} // namespace math
|
||||||
} // namespace cenet
|
} // namespace cenet
|
||||||
|
|||||||
Reference in New Issue
Block a user