|
| 1 | +#pragma once |
| 2 | +#include <algorithm> |
| 3 | +#include <functional> |
| 4 | + |
| 5 | +#include <ttl/bits/std_shape.hpp> |
| 6 | +#include <ttl/bits/std_tensor.hpp> |
| 7 | + |
| 8 | +namespace ttl |
| 9 | +{ |
| 10 | +namespace internal |
| 11 | +{ |
| 12 | +template <typename R, typename S, typename D = typename S::dimension_type> |
| 13 | +D argmax(const basic_tensor_view<R, 1, S> &t) |
| 14 | +{ |
| 15 | + return std::max_element(t.data(), t.data_end()) - t.data(); |
| 16 | +} |
| 17 | + |
| 18 | +template <typename R, typename R1, rank_t r, typename S> |
| 19 | +void cast(const basic_tensor_view<R, r, S> &x, |
| 20 | + const basic_tensor_ref<R1, r, S> &y) |
| 21 | +{ |
| 22 | + std::transform(x.data(), x.data_end(), y.data(), |
| 23 | + [](const R &e) -> R1 { return static_cast<R1>(e); }); |
| 24 | +} |
| 25 | + |
| 26 | +template <typename R, rank_t r, typename S> |
| 27 | +void fill(const basic_tensor_ref<R, r, S> &t, const R &x) |
| 28 | +{ |
| 29 | + std::fill(t.data(), t.data_end(), x); |
| 30 | +} |
| 31 | + |
| 32 | +template <typename R, rank_t r, typename S, |
| 33 | + typename D = typename S::dimension_type> |
| 34 | +D hamming_distance(const basic_tensor_view<R, r, S> &x, |
| 35 | + const basic_tensor_view<R, r, S> &y) |
| 36 | +{ |
| 37 | + return std::inner_product(x.data(), x.data_end(), y.data(), |
| 38 | + static_cast<D>(0), std::plus<D>(), |
| 39 | + std::not_equal_to<R>()); |
| 40 | +} |
| 41 | + |
| 42 | +template <typename R, rank_t r, typename S> |
| 43 | +R max(const basic_tensor_view<R, r, S> &t) |
| 44 | +{ |
| 45 | + return *std::max_element(t.data(), t.data_end()); |
| 46 | +} |
| 47 | + |
| 48 | +template <typename R, rank_t r, typename S> |
| 49 | +R min(const basic_tensor_view<R, r, S> &t) |
| 50 | +{ |
| 51 | + return *std::min_element(t.data(), t.data_end()); |
| 52 | +} |
| 53 | + |
| 54 | +template <typename R, rank_t r, typename S> |
| 55 | +R sum(const basic_tensor_view<R, r, S> &t) |
| 56 | +{ |
| 57 | + return std::accumulate(t.data(), t.data_end(), static_cast<R>(0)); |
| 58 | +} |
| 59 | + |
| 60 | +} // namespace internal |
| 61 | +} // namespace ttl |
0 commit comments