Expression Templates Library (ETL)
uni_upper.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 #include "etl/adapters/adapter.hpp" // The adapter base class
16 #include "etl/adapters/uni_upper_exception.hpp" // The exception
17 #include "etl/adapters/uni_upper_reference.hpp" // The reference proxy
18 
19 namespace etl {
20 
26 template <adaptable Matrix>
27 struct uni_upper_matrix final : adapter<Matrix>, iterable<const uni_upper_matrix<Matrix>> {
28  using matrix_t = Matrix;
29  using expr_t = matrix_t;
31 
32  static constexpr size_t n_dimensions = etl_traits<matrix_t>::dimensions();
34  static constexpr size_t alignment = matrix_t::alignment;
35 
38  using const_memory_type = const value_type*;
39 
42 
44 
48  template <typename V = default_vec>
49  using vec_type = typename V::template vec_type<value_type>;
50 
51  using base_type::value;
52 
53 public:
59  uni_upper_matrix() noexcept : base_type() {
60  // Fill the diagonal
61  for (size_t i = 0; i < etl::dim<0>(value); ++i) {
62  value(i, i) = value_type(1);
63  }
64  }
65 
70  explicit uni_upper_matrix(size_t dim) noexcept : base_type(dim) {
71  // Fill the diagonal
72  for (size_t i = 0; i < etl::dim<0>(value); ++i) {
73  value(i, i) = value_type(1);
74  }
75  }
76 
81  uni_upper_matrix(const uni_upper_matrix& rhs) = default;
82 
88  uni_upper_matrix& operator=(const uni_upper_matrix& rhs) = default;
89 
94  uni_upper_matrix(uni_upper_matrix&& rhs) noexcept = default;
95 
101  uni_upper_matrix& operator=(uni_upper_matrix&& rhs) noexcept = default;
102 
108  template <convertible_expr<value_type> E>
109  uni_upper_matrix& operator=(E&& e) noexcept(false) {
110  // Make sure the other matrix is uni upper triangular
111  if (!is_uni_upper_triangular(e)) {
112  throw uni_upper_exception();
113  }
114 
115  // Perform the real assign
116 
117  validate_assign(*this, e);
118 
119  // Avoid aliasing issues
120  if constexpr (!decay_traits<E>::is_linear) {
121  if (e.alias(*this)) {
122  // Create a temporary to hold the result
123  this_type tmp(*this);
124 
125  // Assign the expression to the temporary
126  tmp = e;
127 
128  // Assign the temporary to this matrix
129  *this = tmp;
130  } else {
131  e.assign_to(*this);
132  }
133  } else {
134  // Direct assignment of the expression into this matrix
135  e.assign_to(*this);
136  }
137 
138  return *this;
139  }
140 
146  template <etl_expr R>
147  uni_upper_matrix& operator+=(const R & rhs) {
148  // Make sure the other matrix is uni upper triangular
149  if (!is_uni_upper_triangular(rhs)) {
150  throw uni_upper_exception();
151  }
152 
153  validate_expression(*this, rhs);
154  rhs.assign_add_to(*this);
155  return *this;
156  }
157 
163  template <etl_expr R>
164  uni_upper_matrix& operator-=(const R & rhs) {
165  // Make sure the other matrix is uni upper triangular
166  if (!is_uni_upper_triangular(rhs)) {
167  throw uni_upper_exception();
168  }
169 
170  validate_expression(*this, rhs);
171  rhs.assign_sub_to(*this);
172  return *this;
173  }
174 
180  template <typename R>
181  uni_upper_matrix& operator*=(const R & rhs) {
182  // Make sure the other matrix is uni upper triangular
183  if (!is_uni_upper_triangular(rhs)) {
184  throw uni_upper_exception();
185  }
186 
187  validate_expression(*this, rhs);
188  rhs.assign_mul_to(*this);
189  return *this;
190  }
191 
197  template <etl_expr R>
198  uni_upper_matrix& operator>>=(const R & rhs) {
199  // Make sure the other matrix is uni upper triangular
200  if (!is_uni_upper_triangular(rhs)) {
201  throw uni_upper_exception();
202  }
203 
204  validate_expression(*this, rhs);
205  rhs.assign_mul_to(*this);
206  return *this;
207  }
208 
214  template <etl_expr R>
215  uni_upper_matrix& operator/=(const R & rhs) {
216  // Make sure the other matrix is uni upper triangular
217  if (!is_uni_upper_triangular(rhs)) {
218  throw uni_upper_exception();
219  }
220 
221  validate_expression(*this, rhs);
222  rhs.assign_div_to(*this);
223  return *this;
224  }
225 
235  return {value, i, j};
236  }
237 
238  using base_type::operator();
239 };
240 
244 template <typename Matrix>
245 struct etl_traits<uni_upper_matrix<Matrix>> : wrapper_traits<uni_upper_matrix<Matrix>> {};
246 
247 } //end of namespace etl
uni_upper_matrix() noexcept
Construct a new uni upper triangular matrix and fill it with zeros.
Definition: uni_upper.hpp:59
CRTP class to inject iterators functions.
Definition: iterable.hpp:23
static constexpr size_t alignment
The memory alignment.
Definition: uni_upper.hpp:34
adapter< Matrix > base_type
The base type.
Definition: uni_upper.hpp:43
void assign_to(L &&lhs) const
Assign to the given left-hand-side expression.
Definition: adapter.hpp:279
Contains base class for adapters.
typename V::template vec_type< value_type > vec_type
The vectorization type for V.
Definition: adapter.hpp:33
order
Storage order of a matrix.
Definition: order.hpp:15
A proxy representing a reference to a mutable element of a uni upper triangular matrix.
Definition: uni_upper_reference.hpp:24
Contains uni upper triangular matrix exception implementation.
uni_upper_matrix & operator-=(const R &rhs)
Multiply each element by the value of the elements in the right hand side expression.
Definition: uni_upper.hpp:164
uni_upper_matrix & operator*=(const R &rhs)
Multiply each element by the value of the elements in the right hand side expression.
Definition: uni_upper.hpp:181
const value_type * const_memory_type
The const memory type.
Definition: adapter.hpp:27
value_t< matrix_t > value_type
The value type.
Definition: adapter.hpp:25
value_t< matrix_t > value_type
The value type.
Definition: uni_upper.hpp:36
uni_upper_matrix & operator=(E &&e) noexcept(false)
Assign the values of the ETL expression to the uni upper triangular matrix.
Definition: uni_upper.hpp:109
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
static constexpr size_t dimensions()
Return the number of dimensions of the expression.
Definition: traits_base.hpp:31
auto dim(E &&value, size_t i) -> detail::identity_helper< E, dim_view< detail::build_identity_type< E >, D >>
Return a view representing the ith Dth dimension.
Definition: view_expression_builder.hpp:25
A uni upper triangular matrix adapter.
Definition: uni_upper.hpp:27
value_type * memory_type
The memory type.
Definition: adapter.hpp:26
Matrix matrix_t
The adapted matrix type.
Definition: uni_upper.hpp:28
A base class for adapters.
Definition: adapter.hpp:21
static constexpr order storage_order
The storage order.
Definition: uni_upper.hpp:33
matrix_t value
The adapted matrix.
Definition: adapter.hpp:36
Contains uni upper triangular matrix reference proxy implementation.
uni_upper_detail::uni_upper_reference< matrix_t > operator()(size_t i, size_t j) noexcept
Access the (i, j) element of the 2D matrix.
Definition: uni_upper.hpp:234
matrix_t expr_t
The wrapped expression type.
Definition: adapter.hpp:23
uni_upper_matrix & operator>>=(const R &rhs)
Multiply each element by the value of the elements in the right hand side expression.
Definition: uni_upper.hpp:198
etl::iterator< const this_type > const_iterator
The const iterator type.
Definition: dyn_matrix_view.hpp:37
uni_upper_matrix & operator+=(const R &rhs)
Multiply each element by the value of the elements in the right hand side expression.
Definition: uni_upper.hpp:147
static constexpr size_t n_dimensions
The number of dimensions.
Definition: uni_upper.hpp:32
typename matrix_t::const_iterator const_iterator
The type of const iterator.
Definition: uni_upper.hpp:41
bool is_uni_upper_triangular(E &&expr)
Indicates if the given expression is a strictly upper triangular matrix or not.
Definition: adapters.hpp:250
Exception that is thrown when an operation is made to a uni upper triangular matrix that would render...
Definition: uni_upper_exception.hpp:24
typename matrix_t::const_iterator iterator
The type of const iterator.
Definition: uni_upper.hpp:40
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
Traits for wrapper expressions.
Definition: wrapper_traits.hpp:21
Matrix matrix_t
The adapted matrix type.
Definition: adapter.hpp:22
uni_upper_matrix & operator/=(const R &rhs)
Modulo each element by the value of the elements in the right hand side expression.
Definition: uni_upper.hpp:215
uni_upper_matrix & operator=(const uni_upper_matrix &rhs)=default
Assign to the matrix by copy.
uni_upper_matrix(size_t dim) noexcept
Construct a new uni upper triangular matrix and fill it with zeros.
Definition: uni_upper.hpp:70