feat(math): operation fundamentals

This commit is contained in:
2026-10-29 10:01:26 +01:00
parent 1f95dd51db
commit 0734de1386
4 changed files with 233 additions and 5 deletions
+63 -5
View File
@@ -1,6 +1,7 @@
#ifndef CENET_MATH_TENSOR_HPP
#define CENET_MATH_TENSOR_HPP
#include "expr.hpp"
#include "tensor_fwd.hpp"
#include "tensor_traits.hpp"
@@ -159,13 +160,15 @@ public:
template <typename... RtDims,
typename = std::enable_if_t<(
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
explicit tensor(RtDims... dimensions) {
explicit tensor(RtDims... dimensions)
: m_shape(detail::dynamic_shape<sizeof...(Dims)>{}) {
set_dimensions(dimensions...);
}
~tensor() { release(); }
tensor(tensor&& other) noexcept {
tensor(tensor&& other) noexcept
: m_shape(std::move(other.m_shape)) {
if constexpr (!traits::inline_storage) m_items = nullptr;
*this = std::move(other);
}
@@ -174,13 +177,12 @@ public:
if (this == &other) return *this;
release();
m_items = other.m_items;
std::memcpy(m_shape.dimensions, other.m_shape.dimensions, sizeof(m_shape.dimensions));
std::memcpy(m_shape.strides, other.m_shape.strides, sizeof(m_shape.strides));
if constexpr (!traits::inline_storage) other.m_items = nullptr;
return *this;
}
tensor(const tensor& other) {
tensor(const tensor& other)
: m_shape(other.m_shape) {
if constexpr (!traits::inline_storage) m_items = nullptr;
*this = other;
}
@@ -195,9 +197,36 @@ public:
return *this;
}
public:
// TODO: Introduce another type trait for tensor compability
template <typename E,
typename = std::enable_if_t<detail::is_expression_v<E> && !is_tensor_v<E> &&
std::is_same_v<tensor, typename detail::expression_result<E>::type>>>
tensor(E&& expr) { // NOLINT
if constexpr (!traits::inline_storage) {
static_assert(!traits::dynamic, "Dynamic tensors are not supported yet!");
m_items = new value_type[traits::size];
}
for (std::size_t i = 0; i < view().size(); ++i) {
m_items[i] = std::forward<E>(expr).eval(i);
}
}
template <typename E,
typename = std::enable_if_t<detail::is_expression_v<E> && !is_tensor_v<E> &&
std::is_same_v<tensor, typename detail::expression_result<E>::type>>>
tensor& operator=(E&& expr) {
for (std::size_t i = 0; i < view().size(); ++i) {
m_items[i] = std::forward<E>(expr);
}
}
public:
view_type view() { return { m_items, m_shape }; }
constant_view_type view() const { return { m_items, m_shape }; }
explicit operator view_type() { return view(); }
explicit operator constant_view_type() const { return view(); }
public:
template <typename... RtDims,
typename = std::enable_if_t<(
@@ -211,6 +240,26 @@ public:
m_items = new value_type[(dimensions * ... * 1)];
std::memset(m_items, 0, sizeof(*m_items) * (dimensions * ... * 1));
}
public:
constexpr std::size_t size() const { return size_impl(std::make_index_sequence<sizeof...(Dims)>{}); }
auto dimensions() const { return dimensions_impl(std::make_index_sequence<sizeof...(Dims)>{}); }
private:
template <std::size_t... Is>
constexpr std::size_t size_impl(std::index_sequence<Is...>) const {
return (dimension<Is>() * ... * 1);
}
template <std::size_t... Is>
constexpr auto dimensions_impl(std::index_sequence<Is...>) const {
return std::make_tuple(dimension<Is>()...);
}
public:
value_type& coeff(std::size_t index) { return m_items[index]; }
const value_type& coeff(std::size_t index) const { return m_items[index]; }
value_type* data() { return m_items; }
const value_type* data() const { return m_items; }
private:
template <typename... RtDims, std::size_t... Is>
constexpr bool valid_dims(std::index_sequence<Is...>, RtDims... dimensions) const {
@@ -226,6 +275,15 @@ private:
constexpr void store_dims(std::index_sequence<Is...>, RtDims... dimensions) {
((m_shape.dimensions[Is] = static_cast<std::size_t>(dimensions)), ...);
}
private:
template <std::size_t I>
constexpr std::size_t dimension() const {
if constexpr (traits::dynamic && traits::dimensions[I] == dynamic_extent) {
return m_shape.dimensions[I];
} else {
return traits::dimensions[I];
}
}
private:
template <std::size_t... Is>
void dyn_alloc(std::index_sequence<Is...>) {