Tatooine
mat.h
Go to the documentation of this file.
1#ifndef TATOOINE_MAT_H
2#define TATOOINE_MAT_H
3//==============================================================================
4#include <tatooine/hdf5.h>
5#include <tatooine/real.h>
6#include <tatooine/tensor.h>
7//==============================================================================
8#include <tatooine/random.h>
9#include <tatooine/tags.h>
10//==============================================================================
11namespace tatooine {
12//==============================================================================
13template <arithmetic_or_complex ValueType, std::size_t M, std::size_t N>
14struct mat : tensor<ValueType, M, N> {
15 //============================================================================
16 // static methods
17 //============================================================================
18 static auto constexpr num_rows() { return M; }
19 static auto constexpr num_columns() { return N; }
20
21 //============================================================================
22 // typedefs
23 //============================================================================
26
27 //============================================================================
28 // inherited methods
29 //============================================================================
30 using parent_type::operator();
31 using parent_type::at;
32
33 //============================================================================
34 // factories
35 //============================================================================
36 static auto constexpr zeros() { return this_type{tag::fill<ValueType>{0}}; }
37 //----------------------------------------------------------------------------
38 static auto constexpr ones() { return this_type{tag::fill<ValueType>{1}}; }
39 //----------------------------------------------------------------------------
40 static auto constexpr fill(ValueType const& v) { return this_type{tag::fill<ValueType>{v}}; }
41 //----------------------------------------------------------------------------
42 static auto constexpr nans() requires floating_point<ValueType> {
43 return this_type{tag::fill<ValueType>{ValueType{} / ValueType{}}};
44 }
45 //----------------------------------------------------------------------------
46 static auto constexpr eye() { return this_type{tag::eye}; }
47 //----------------------------------------------------------------------------
48 template <typename RandEng = std::mt19937_64>
49 static auto constexpr randu(ValueType min = 0, ValueType max = 1,
50 RandEng&& eng = RandEng{std::random_device{}()}) {
51 return this_type{random::uniform{min, max, std::forward<RandEng>(eng)}};
52 }
53 //----------------------------------------------------------------------------
54 template <typename RandEng = std::mt19937_64>
55 static auto constexpr randn(ValueType mean = 0, ValueType stddev = 1,
56 RandEng&& eng = RandEng{std::random_device{}()}) {
57 return this_type{random::normal<ValueType>{eng, mean, stddev}};
58 }
59 //----------------------------------------------------------------------------
60 static auto constexpr vander(fixed_size_vec<N> auto const& v) {
61 auto V = this_type{};
62 auto factor_up_row = [row = std::size_t(0), &V](auto x) mutable {
63 V(row, 0) = 1;
64 for (std::size_t col = 1; col < N; ++col) {
65 V(row, col) = V(row, col - 1) * x;
66 }
67 ++row;
68 };
69 for (std::size_t i = 0; i < N; ++i) {
70 factor_up_row(v(i));
71 }
72 return V;
73 }
74 //----------------------------------------------------------------------------
75 static auto constexpr vander(convertible_to<ValueType> auto&&... xs) {
76 static_assert(sizeof...(xs) == num_columns());
77 auto V = this_type{};
78 auto factor_up_row = [row = std::size_t(0), &V](auto x) mutable {
79 V(row, 0) = 1;
80 for (std::size_t col = 1; col < N; ++col) {
81 V(row, col) = V(row, col - 1) * x;
82 }
83 ++row;
84 };
85 (factor_up_row(xs), ...);
86 return V;
87 }
88
89 //============================================================================
90 // constructors
91 //============================================================================
92 constexpr mat() = default;
93 //----------------------------------------------------------------------------
94 constexpr mat(mat const&) = default;
95 //----------------------------------------------------------------------------
96 constexpr mat(mat&& other) noexcept = default;
97 //----------------------------------------------------------------------------
99 template <static_tensor Other>
100 requires(same_dimensions<this_type, Other>()) explicit constexpr mat(
101 Other&& other)
102 : parent_type{std::forward<Other>(other)} {}
103 //----------------------------------------------------------------------------
104 template <typename... Rows>
105 explicit constexpr mat(Rows(&&... rows)[parent_type::dimension(1)]) {
106 static_assert(((is_arithmetic<Rows> || is_complex<Rows>)&&...));
107 static_assert(
108 sizeof...(rows) == parent_type::dimension(0),
109 "number of given rows does not match specified number of rows");
110
111 // lambda inserting row into data block
112 auto insert_row = [r = std::size_t(0), this](auto const& row) mutable {
113 for (std::size_t c = 0; c < parent_type::dimension(1); ++c) {
114 at(r, c) = static_cast<ValueType>(row[c]);
115 }
116 ++r;
117 };
118
119 for_each(insert_row, rows...);
120 }
121 //----------------------------------------------------------------------------
123 explicit constexpr mat(tag::eye_t /*flag*/) : parent_type{tag::zeros} {
124 for (std::size_t i = 0; i < std::min(M, N); ++i) {
125 at(i, i) = 1;
126 }
127 }
128 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129 template <convertible_to<ValueType> Fill>
130 explicit constexpr mat(tag::fill<Fill> const f) : parent_type{f} {}
131 explicit constexpr mat(tag::ones_t const ones) : parent_type{ones} {}
132 explicit constexpr mat(tag::zeros_t const zeros) : parent_type{zeros} {}
133 template <typename RandomReal, typename Engine>
134 requires is_arithmetic<ValueType>
135 explicit constexpr mat(random::uniform<RandomReal, Engine>&& rand)
136 : parent_type{std::move(rand)} {}
137 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
138 template <arithmetic RandomReal, typename Engine>
139 explicit constexpr mat(random::uniform<RandomReal, Engine>& rand)
140 : parent_type{rand} {}
141 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
142 template <arithmetic RandomReal, typename Engine>
143 requires is_arithmetic<ValueType>
144 explicit constexpr mat(random::normal<RandomReal, Engine>&& rand)
145 : parent_type{std::move(rand)} {}
146 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
147 template <arithmetic RandomReal, typename Engine>
148 requires is_arithmetic<ValueType>
149 explicit constexpr mat(random::normal<RandomReal, Engine>& rand)
150 : parent_type{rand} {}
151 //============================================================================
152 // assign operators
153 //============================================================================
154 auto constexpr operator=(mat const&) -> mat& = default;
155 //----------------------------------------------------------------------------
156 auto constexpr operator=(mat&& other) noexcept -> mat& = default;
157 //----------------------------------------------------------------------------
158 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159 template <static_tensor Other>
160 requires(same_dimensions<this_type, Other>())
161 auto constexpr operator=(Other const& other) noexcept -> mat& {
163 return *this;
164 }
165 //============================================================================
166 // destructor
167 //============================================================================
168 ~mat() = default;
169 //============================================================================
170 // methods
171 //============================================================================
172 auto constexpr row(std::size_t i) { return this->template slice<0>(i); }
173 auto constexpr row(std::size_t i) const { return this->template slice<0>(i); }
174 //------------------------------------------------------------------------------
175 auto constexpr col(std::size_t i) { return this->template slice<1>(i); }
176 auto constexpr col(std::size_t i) const { return this->template slice<1>(i); }
177};
178//==============================================================================
179// deduction guides
180//==============================================================================
181template <std::size_t C, typename... Rows>
182mat(Rows const(&&... rows)[C]) -> mat<common_type<Rows...>, sizeof...(Rows), C>;
183//------------------------------------------------------------------------------
184template <typename Mat, typename ValueType, std::size_t M, std::size_t N>
186//==============================================================================
187//namespace reflection {
188//template <typename ValueType, std::size_t M, std::size_t N>
189//TATOOINE_MAKE_TEMPLATED_ADT_REFLECTABLE(
190// (mat<ValueType, M, N>), TATOOINE_REFLECTION_INSERT_METHOD(data, data()))
191//} // namespace reflection
192//==============================================================================
193} // namespace tatooine
194//==============================================================================
196//==============================================================================
197#endif
Definition: concepts.h:39
Definition: tensor_concepts.h:33
Definition: concepts.h:30
static constexpr eye_t eye
Definition: tags.h:85
Definition: algorithm.h:6
typename common_type_impl< Ts... >::type common_type
Definition: common_type.h:23
constexpr auto max(A &&a, B &&b)
Definition: math.h:20
constexpr auto min(A &&a, B &&b)
Definition: math.h:15
constexpr void for_each(F &&f, Ts &&... ts)
Definition: utility.h:39
Definition: base_tensor.h:23
auto constexpr dimension() const
Definition: contracted_dynamic_tensor.h:36
Definition: mat.h:14
static auto constexpr randn(ValueType mean=0, ValueType stddev=1, RandEng &&eng=RandEng{std::random_device{}()})
Definition: mat.h:55
constexpr mat()=default
mat< ValueType, M, N > this_type
Definition: mat.h:24
constexpr mat(tag::eye_t)
Constructs an identity matrix.
Definition: mat.h:123
static auto constexpr num_columns()
Definition: mat.h:19
static auto constexpr num_rows()
Definition: mat.h:18
auto constexpr operator=(mat const &) -> mat &=default
auto constexpr row(std::size_t i)
Definition: mat.h:172
~mat()=default
static auto constexpr fill(ValueType const &v)
Definition: mat.h:40
auto constexpr col(std::size_t i) const
Definition: mat.h:176
constexpr mat(random::normal< RandomReal, Engine > &rand)
Definition: mat.h:149
static auto constexpr vander(fixed_size_vec< N > auto const &v)
Definition: mat.h:60
auto constexpr col(std::size_t i)
Definition: mat.h:175
static auto constexpr randu(ValueType min=0, ValueType max=1, RandEng &&eng=RandEng{std::random_device{}()})
Definition: mat.h:49
static auto constexpr ones()
Definition: mat.h:38
constexpr mat(Rows(&&... rows)[parent_type::dimension(1)])
Definition: mat.h:105
constexpr mat(Other &&other)
Copies any other tensor with same dimensions.
Definition: mat.h:100
constexpr mat(mat &&other) noexcept=default
constexpr mat(random::uniform< RandomReal, Engine > &&rand)
Definition: mat.h:135
static auto constexpr vander(convertible_to< ValueType > auto &&... xs)
Definition: mat.h:75
constexpr mat(tag::fill< Fill > const f)
Definition: mat.h:130
static auto constexpr zeros()
Definition: mat.h:36
constexpr mat(random::uniform< RandomReal, Engine > &rand)
Definition: mat.h:139
auto constexpr operator=(mat &&other) noexcept -> mat &=default
constexpr mat(random::normal< RandomReal, Engine > &&rand)
Definition: mat.h:144
constexpr mat(tag::zeros_t const zeros)
Definition: mat.h:132
constexpr mat(tag::ones_t const ones)
Definition: mat.h:131
static auto constexpr nans()
Definition: mat.h:42
static auto constexpr eye()
Definition: mat.h:46
auto constexpr row(std::size_t i) const
Definition: mat.h:173
constexpr mat(mat const &)=default
Definition: random.h:97
Definition: random.h:15
Definition: tags.h:84
Definition: tags.h:96
Definition: tags.h:105
Definition: tags.h:102
Definition: tensor.h:17
constexpr auto operator=(tensor const &) -> tensor &=default
auto constexpr at(integral auto const ... is) -> decltype(auto)
Definition: tensor.h:38
type_list_at< this_type, I > at
Definition: type_list.h:269