Tatooine
vec.h
Go to the documentation of this file.
1#ifndef TATOOINE_VEC_H
2#define TATOOINE_VEC_H
3//==============================================================================
4#include <tatooine/tensor.h>
5//==============================================================================
6#include <tatooine/random.h>
7#include <tatooine/tags.h>
8//==============================================================================
9namespace tatooine {
10//==============================================================================
11template <arithmetic_or_complex ValueType, std::size_t N>
12struct vec : tensor<ValueType, N> {
15 using typename parent_type::value_type;
16 using parent_type::at;
17 using parent_type::operator();
19 //============================================================================
20 using iterator =
22 //============================================================================
25 //============================================================================
26 static auto constexpr zeros() { return this_type{tag::fill<ValueType>{0}}; }
27 //----------------------------------------------------------------------------
28 static auto constexpr ones() { return this_type{tag::fill<ValueType>{1}}; }
29 //----------------------------------------------------------------------------
30 static auto constexpr fill(ValueType const& t) {
32 }
33 //----------------------------------------------------------------------------
34 template <typename RandEng = std::mt19937_64>
35 static auto constexpr randu(ValueType min = 0, ValueType max = 1,
36 RandEng&& eng = RandEng{std::random_device{}()}) {
37 return this_type{random::uniform{min, max, std::forward<RandEng>(eng)}};
38 }
39 //----------------------------------------------------------------------------
40 template <typename RandEng = std::mt19937_64>
41 static auto constexpr randn(ValueType mean = 0, ValueType stddev = 1,
42 RandEng&& eng = RandEng{std::random_device{}()}) {
43 return this_type{random::normal<ValueType>{eng, mean, stddev}};
44 }
45 //----------------------------------------------------------------------------
46 constexpr vec() = default;
47 constexpr vec(vec const&) = default;
48 constexpr vec(vec&& other) noexcept = default;
49 //----------------------------------------------------------------------------
50 constexpr vec(convertible_to<ValueType> auto&&... ts) requires(
51 parent_type::dimension(0) == sizeof...(ts))
52 : parent_type{std::forward<decltype(ts)>(ts)...} {}
53 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54 template <typename OtherTensor, typename OtherReal>
55 explicit constexpr vec(base_tensor<OtherTensor, OtherReal, N> const& other)
56 : parent_type{other} {}
57 //----------------------------------------------------------------------------
58 template <typename FillReal>
60 explicit constexpr vec(tag::fill<FillReal> const f) : parent_type{f} {}
61 explicit constexpr vec(tag::ones_t const ones) : parent_type{ones} {}
62 explicit constexpr vec(tag::zeros_t const zeros) : parent_type{zeros} {}
63 template <typename RandomReal, typename Engine>
64 requires is_arithmetic<ValueType>
65 explicit constexpr vec(random::uniform<RandomReal, Engine>&& rand)
66 : parent_type{std::move(rand)} {}
67 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68 template <arithmetic RandomReal, typename Engine>
69 explicit constexpr vec(random::uniform<RandomReal, Engine>& rand)
70 : parent_type{rand} {}
71 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72 template <arithmetic RandomReal, typename Engine>
73 requires is_arithmetic<ValueType>
74 explicit constexpr vec(random::normal<RandomReal, Engine>&& rand)
75 : parent_type{std::move(rand)} {}
76 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77 template <arithmetic RandomReal, typename Engine>
78 requires is_arithmetic<ValueType>
79 explicit constexpr vec(random::normal<RandomReal, Engine>& rand)
80 : parent_type{rand} {}
81 //----------------------------------------------------------------------------
82 auto constexpr operator=(vec const&) -> vec& = default;
83 auto constexpr operator=(vec&& other) noexcept -> vec& = default;
84 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
85 template <static_tensor Other>
86 requires (same_dimensions<this_type, Other>()) &&
89 auto constexpr operator=(Other&& other) -> vec& {
90 this->assign(std::forward<decltype(other)>(other));
91 return *this;
92 }
93 //----------------------------------------------------------------------------
94 ~vec() = default;
95 //----------------------------------------------------------------------------
96 auto begin() const { return this->internal_container().begin(); }
97 auto begin() { return this->internal_container().begin(); }
98 auto end() const { return this->internal_container().end(); }
99 auto end() { return this->internal_container().end(); }
100 auto size() const { return parent_type::size().front(); }
101 //----------------------------------------------------------------------------
102 auto constexpr x() const -> auto const& requires(N >= 1) { return at(0); }
103 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104 auto constexpr x() -> auto& requires(N >= 1) { return at(0); }
105 //----------------------------------------------------------------------------
106 auto constexpr y() const -> auto const& requires(N >= 2) { return at(1); }
107 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108 auto constexpr xy() const requires(N >= 2) {
109 return vec<ValueType, 2>{at(0), at(1)};
110 }
111 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112 auto constexpr yx() const requires(N >= 2) {
113 return vec<ValueType, 2>{at(1), at(0)};
114 }
115 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116 auto constexpr y() -> auto& requires(N >= 2) { return at(1); }
117 //----------------------------------------------------------------------------
118 auto constexpr xyz() const requires(N >= 3) {
119 return vec<ValueType, 3>{at(0), at(1), at(2)};
120 }
121 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
122 auto constexpr z() const -> auto const& requires(N >= 3) { return at(2); }
123 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124 auto constexpr z() -> auto& requires(N >= 3) { return at(2); }
125 //----------------------------------------------------------------------------
126 auto constexpr xyzw() const requires(N >= 3) {
127 return vec<ValueType, 4>{at(0), at(1), at(2), at(3)};
128 }
129 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
130 auto constexpr w() const -> auto const& requires(N >= 4) { return at(3); }
131 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
132 auto constexpr w() -> auto& requires(N >= 4) { return at(3); }
133};
134//==============================================================================
135// type traits
136//==============================================================================
137template <typename ValueType, std::size_t N>
138auto begin(vec<ValueType, N> const& v) {
139 return v.begin();
140}
141//------------------------------------------------------------------------------
142template <typename ValueType, std::size_t N>
143auto end(vec<ValueType, N> const& v) {
144 return v.end();
145}
146//------------------------------------------------------------------------------
147template <typename ValueType, std::size_t N>
148auto size(vec<ValueType, N> const& v) {
149 return v.size();
150}
151//==============================================================================
152template <typename... Ts>
153vec(const Ts&...) -> vec<common_type<Ts...>, sizeof...(Ts)>;
154//------------------------------------------------------------------------------
155template <typename V, typename ValueType, std::size_t N>
157//==============================================================================
158//namespace reflection {
159//template <typename ValueType, std::size_t N>
160//TATOOINE_MAKE_TEMPLATED_ADT_REFLECTABLE(
161// (vec<ValueType, N>), TATOOINE_REFLECTION_INSERT_METHOD(data, data()))
162//} // namespace reflection
163//==============================================================================
164} // namespace tatooine
165//==============================================================================
167//==============================================================================
168#endif
typename container_type::const_iterator const_iterator
Definition: cache.h:19
Definition: netcdf.h:348
constexpr auto internal_container() -> auto &
Definition: static_multidim_array.h:253
Definition: concepts.h:33
Definition: concepts.h:39
Definition: algorithm.h:6
typename common_type_impl< Ts... >::type common_type
Definition: common_type.h:23
auto begin(Range &&range)
Definition: iterator_facade.h:318
typename value_type_impl< T >::type value_type
Definition: type_traits.h:280
auto end(Range &&range)
Definition: iterator_facade.h:322
constexpr auto max(A &&a, B &&b)
Definition: math.h:20
auto size(vec< ValueType, N > const &v)
Definition: vec.h:148
constexpr auto min(A &&a, B &&b)
Definition: math.h:15
Definition: base_tensor.h:23
auto constexpr assign(Other &&other) -> void
Definition: base_tensor.h:82
Definition: linspace.h:190
auto constexpr dimension() const
Definition: contracted_dynamic_tensor.h:36
Definition: random.h:97
Definition: random.h:15
Definition: tags.h:96
Definition: tags.h:105
Definition: tags.h:102
Definition: tensor.h:17
auto constexpr at(integral auto const ... is) -> decltype(auto)
Definition: tensor.h:38
typename tensor_parent_type::value_type value_type
Definition: tensor.h:21
type_list_at< this_type, I > at
Definition: type_list.h:269
Definition: vec.h:12
static auto constexpr ones()
Definition: vec.h:28
auto constexpr xyz() const
Definition: vec.h:118
auto constexpr xy() const
Definition: vec.h:108
auto begin() const
Definition: vec.h:96
constexpr vec(base_tensor< OtherTensor, OtherReal, N > const &other)
Definition: vec.h:55
constexpr vec(tag::zeros_t const zeros)
Definition: vec.h:62
auto constexpr xyzw() const
Definition: vec.h:126
constexpr vec(convertible_to< ValueType > auto &&... ts)
Definition: vec.h:50
vec< ValueType, N > this_type
Definition: vec.h:13
auto constexpr x() -> auto &requires(N >=1)
Definition: vec.h:104
auto constexpr x() const -> auto const &requires(N >=1)
Definition: vec.h:102
auto constexpr operator=(Other &&other) -> vec &
Definition: vec.h:89
constexpr vec(random::normal< RandomReal, Engine > &&rand)
Definition: vec.h:74
constexpr vec(tag::ones_t const ones)
Definition: vec.h:61
auto constexpr z() const -> auto const &requires(N >=3)
Definition: vec.h:122
auto constexpr z() -> auto &requires(N >=3)
Definition: vec.h:124
auto size() const
Definition: vec.h:100
static auto constexpr randn(ValueType mean=0, ValueType stddev=1, RandEng &&eng=RandEng{std::random_device{}()})
Definition: vec.h:41
auto constexpr y() const -> auto const &requires(N >=2)
Definition: vec.h:106
constexpr vec(tag::fill< FillReal > const f)
Definition: vec.h:60
constexpr vec(vec const &)=default
auto begin()
Definition: vec.h:97
constexpr vec(random::normal< RandomReal, Engine > &rand)
Definition: vec.h:79
auto constexpr operator=(vec const &) -> vec &=default
typename parent_type::array_parent_type::container_type::iterator iterator
Definition: vec.h:21
typename parent_type::array_parent_type::container_type::const_iterator const_iterator
Definition: vec.h:24
auto constexpr w() const -> auto const &requires(N >=4)
Definition: vec.h:130
auto constexpr operator=(vec &&other) noexcept -> vec &=default
constexpr vec(random::uniform< RandomReal, Engine > &rand)
Definition: vec.h:69
~vec()=default
auto end() const
Definition: vec.h:98
static auto constexpr randu(ValueType min=0, ValueType max=1, RandEng &&eng=RandEng{std::random_device{}()})
Definition: vec.h:35
constexpr vec()=default
auto constexpr yx() const
Definition: vec.h:112
auto constexpr y() -> auto &requires(N >=2)
Definition: vec.h:116
static auto constexpr zeros()
Definition: vec.h:26
auto constexpr w() -> auto &requires(N >=4)
Definition: vec.h:132
constexpr vec(random::uniform< RandomReal, Engine > &&rand)
Definition: vec.h:65
auto end()
Definition: vec.h:99
constexpr vec(vec &&other) noexcept=default
static auto constexpr fill(ValueType const &t)
Definition: vec.h:30