Tatooine
tensor.h
Go to the documentation of this file.
1#ifndef TATOOINE_TENSOR_H
2#define TATOOINE_TENSOR_H
3//==============================================================================
5//==============================================================================
6#include <tatooine/math.h>
8#include <tatooine/tags.h>
10#include <tatooine/utility.h>
11//==============================================================================
12namespace tatooine {
13//==============================================================================
14template <arithmetic_or_complex ValueType, std::size_t... Dims>
15struct tensor
16 : static_multidim_array<ValueType, x_fastest, stack, Dims...>,
17 base_tensor<tensor<ValueType, Dims...>, ValueType, Dims...> {
18 //============================================================================
19 using this_type = tensor<ValueType, Dims...>;
20 using tensor_parent_type = base_tensor<this_type, ValueType, Dims...>;
23 static_multidim_array<ValueType, x_fastest, stack, Dims...>;
24
29 //============================================================================
30 public:
31 constexpr tensor() = default;
32 constexpr tensor(tensor const&) = default;
33 constexpr tensor(tensor&& other) noexcept = default;
34 constexpr auto operator=(tensor const&) -> tensor& = default;
35 constexpr auto operator=(tensor&& other) noexcept -> tensor& = default;
36 ~tensor() = default;
37 //============================================================================
38 auto constexpr at(integral auto const... is) -> decltype(auto) {
39 return array_parent_type::at(is...);
40 }
41 auto constexpr at(integral auto const... is) const -> decltype(auto) {
42 return array_parent_type::at(is...);
43 }
44 auto constexpr at(einstein_notation::index auto const... is)
45 -> decltype(auto) {
46 return tensor_parent_type::at(is...);
47 }
48 auto constexpr at(einstein_notation::index auto const... is) const
49 -> decltype(auto) {
50 return tensor_parent_type::at(is...);
51 }
52 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53 constexpr auto at(integral_range auto const& indices) const -> auto const& {
55 }
56 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57 constexpr auto at(integral_range auto const& indices) -> auto& {
59 }
60 //----------------------------------------------------------------------------
61 template <typename... Is>
62 requires((einstein_notation::index<Is> && ...) ||
63 (integral<Is> && ...))
64 auto constexpr operator()(Is const... is) -> decltype(auto) {
65 return at(is...);
66 }
67 //----------------------------------------------------------------------------
68 template <typename... Is>
69 requires((einstein_notation::index<Is> && ...) ||
70 (integral<Is> && ...))
71 auto constexpr operator()(Is const... is) const -> decltype(auto) {
72 return at(is...);
73 }
74 //----------------------------------------------------------------------------
75 constexpr auto operator()(integral_range auto const& indices) const -> const
76 auto& {
78 }
79 //----------------------------------------------------------------------------
80 constexpr auto operator()(integral_range auto const& indices) -> auto& {
82 }
83 //----------------------------------------------------------------------------
84 template <convertible_to<ValueType>... Ts>
86 sizeof...(Ts)) explicit constexpr tensor(Ts const&... ts)
87 : array_parent_type{ts...} {}
88 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
89
90 explicit constexpr tensor(tag::zeros_t zeros) requires
91 is_arithmetic<ValueType> : array_parent_type{zeros} {}
92 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
93
94 explicit constexpr tensor(tag::ones_t ones) requires is_arithmetic<ValueType>
96 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97 template <typename FillReal>
98 requires is_arithmetic<ValueType>
99 explicit constexpr tensor(tag::fill<FillReal> f) : array_parent_type{f} {}
100 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101 template <typename RandomReal, typename Engine>
102 requires is_arithmetic<ValueType>
104 : array_parent_type{std::move(rand)} {}
105 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
106 template <arithmetic RandomReal, typename Engine>
108 : array_parent_type{rand} {}
109 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
110 template <arithmetic RandomReal, typename Engine>
111 requires is_arithmetic<ValueType>
113 : array_parent_type{std::move(rand)} {}
114 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115 template <arithmetic RandomReal, typename Engine>
116 requires is_arithmetic<ValueType>
118 : array_parent_type{rand} {}
119 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120 template <static_tensor OtherTensor>
121 requires(same_dimensions<this_type, OtherTensor>())
122 explicit constexpr tensor(OtherTensor&& other)
123 : tensor_parent_type{std::forward<OtherTensor>(other)} {}
124 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125 template <static_tensor OtherTensor>
126 requires(same_dimensions<this_type, OtherTensor>())
127 constexpr auto operator=(OtherTensor&& other) -> tensor& {
128 // Check if the same matrix gets assigned as its transposed version. If yes
129 // just swap components.
130 if constexpr (transposed_tensor<OtherTensor>) {
131 if (this == &other.internal_tensor()) {
132 for (std::size_t col = 0; col < dimension(1) - 1; ++col) {
133 for (std::size_t row = col + 1; row < dimension(0); ++row) {
134 std::swap(at(row, col), at(col, row));
135 }
136 }
137 return *this;
138 }
139 }
140 this->assign(std::forward<OtherTensor>(other));
141 return *this;
142 }
143 //----------------------------------------------------------------------------
144 static constexpr auto zeros() { return this_type{tag::fill<ValueType>{0}}; }
145 //----------------------------------------------------------------------------
146 static constexpr auto ones() { return this_type{tag::fill<ValueType>{1}}; }
147 //----------------------------------------------------------------------------
148 static constexpr auto fill(ValueType const& t) {
150 }
151 //----------------------------------------------------------------------------
152 template <typename RandEng = std::mt19937_64>
153 static constexpr auto randu(ValueType min = 0, ValueType max = 1,
154 RandEng&& eng = RandEng{std::random_device{}()}) {
155 return this_type{random::uniform{min, max, std::forward<RandEng>(eng)}};
156 }
157 //----------------------------------------------------------------------------
158 template <typename RandEng = std::mt19937_64>
159 static constexpr auto randn(ValueType mean = 0, ValueType stddev = 1,
160 RandEng&& eng = RandEng{std::random_device{}()}) {
161 return this_type{random::normal<ValueType>{eng, mean, stddev}};
162 }
163 //----------------------------------------------------------------------------
164 template <typename OtherT>
165 auto operator<(tensor<OtherT, Dims...> const& other) const {
166 return this->internal_container() < other.internal_container();
167 }
168 //============================================================================
169 template <typename F>
170 auto unary_operation(F&& f) -> auto& {
171 array_parent_type::unary_operation(std::forward<F>(f));
172 return *this;
173 }
174 //----------------------------------------------------------------------------
175 template <typename F, typename OtherTensor, typename OtherT>
178 -> decltype(auto) {
179 tensor_parent_type::binary_operation(std::forward<F>(f), other);
180 return *this;
181 }
182};
183template <std::size_t... Dimensions>
184using Tensor = tensor<real_number, Dimensions...>;
185
202//==============================================================================
203//namespace reflection {
204//template <typename ValueType, std::size_t... Dims>
205//TATOOINE_MAKE_TEMPLATED_ADT_REFLECTABLE(
206// (tensor<ValueType, Dims...>),
207// TATOOINE_REFLECTION_INSERT_METHOD(data, data()))
208//} // namespace reflection
209//==============================================================================
210} // namespace tatooine
211//==============================================================================
214//==============================================================================
215#include <tatooine/mat.h>
216#include <tatooine/vec.h>
217// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
218#include <tatooine/tensor_cast.h>
222//==============================================================================
224#include <tatooine/diag_tensor.h>
225#include <tatooine/rank.h>
226#include <tatooine/tensor_io.h>
228//==============================================================================
229#endif
Definition: static_multidim_array.h:19
constexpr auto internal_container() -> auto &
Definition: static_multidim_array.h:253
constexpr void unary_operation(F &&f)
Definition: static_multidim_array.h:268
Definition: concepts.h:91
Definition: concepts.h:21
Definition: tensor_concepts.h:93
Definition: algorithm.h:6
constexpr auto max(A &&a, B &&b)
Definition: math.h:20
constexpr auto min(A &&a, B &&b)
Definition: math.h:15
Definition: base_tensor.h:23
static auto constexpr rank()
Definition: base_tensor.h:41
auto constexpr assign(Other &&other) -> void
Definition: base_tensor.h:82
ValueType value_type
Definition: base_tensor.h:24
static auto constexpr num_components()
Definition: base_tensor.h:43
static auto constexpr indices()
Definition: base_tensor.h:56
auto constexpr dimension() const
Definition: contracted_dynamic_tensor.h:36
Definition: binary_operation.h:10
Definition: random.h:97
Definition: random.h:15
Definition: tags.h:112
Definition: tags.h:96
Definition: tags.h:105
Definition: tags.h:102
Definition: tensor.h:17
constexpr auto operator()(integral_range auto const &indices) const -> const auto &
Definition: tensor.h:75
~tensor()=default
auto constexpr at(integral auto const ... is) const -> decltype(auto)
Definition: tensor.h:41
static constexpr auto zeros()
Definition: tensor.h:144
auto operator<(tensor< OtherT, Dims... > const &other) const
Definition: tensor.h:165
static constexpr auto randn(ValueType mean=0, ValueType stddev=1, RandEng &&eng=RandEng{std::random_device{}()})
Definition: tensor.h:159
static auto constexpr dimension(std::size_t const i)
Definition: base_tensor.h:49
constexpr auto operator=(tensor const &) -> tensor &=default
auto unary_operation(F &&f) -> auto &
Definition: tensor.h:170
auto constexpr at(einstein_notation::index auto const ... is) -> decltype(auto)
Definition: tensor.h:44
static constexpr auto fill(ValueType const &t)
Definition: tensor.h:148
auto binary_operation(F &&f, base_tensor< OtherTensor, OtherT, Dims... > const &other) -> decltype(auto)
Definition: tensor.h:176
static constexpr auto ones()
Definition: tensor.h:146
constexpr tensor(tensor const &)=default
constexpr auto at(integral_range auto const &indices) const -> auto const &
Definition: tensor.h:53
constexpr auto at(integral_range auto const &indices) -> auto &
Definition: tensor.h:57
constexpr tensor(Ts const &... ts)
Definition: tensor.h:86
constexpr tensor(tag::zeros_t zeros)
Definition: tensor.h:90
constexpr auto operator()(integral_range auto const &indices) -> auto &
Definition: tensor.h:80
auto constexpr at(integral auto const ... is) -> decltype(auto)
Definition: tensor.h:38
constexpr tensor(random::normal< RandomReal, Engine > &rand)
Definition: tensor.h:117
tensor< ValueType, Dims... > this_type
Definition: tensor.h:19
constexpr tensor(random::normal< RandomReal, Engine > &&rand)
Definition: tensor.h:112
constexpr tensor()=default
constexpr tensor(tag::fill< FillReal > f)
Definition: tensor.h:99
auto constexpr at(einstein_notation::index auto const ... is) const -> decltype(auto)
Definition: tensor.h:48
constexpr tensor(random::uniform< RandomReal, Engine > &&rand)
Definition: tensor.h:103
constexpr tensor(OtherTensor &&other)
Definition: tensor.h:122
constexpr auto operator=(tensor &&other) noexcept -> tensor &=default
static constexpr auto randu(ValueType min=0, ValueType max=1, RandEng &&eng=RandEng{std::random_device{}()})
Definition: tensor.h:153
typename tensor_parent_type::value_type value_type
Definition: tensor.h:21
constexpr tensor(tag::ones_t ones)
Definition: tensor.h:94
constexpr tensor(random::uniform< RandomReal, Engine > &rand)
Definition: tensor.h:107
constexpr tensor(tensor &&other) noexcept=default
type_list_at< this_type, I > at
Definition: type_list.h:269
Definition: index_order.h:17