Expression Templates Library (ETL)
strictly_lower.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/strictly_lower_exception.hpp" // The exception
17 #include "etl/adapters/strictly_lower_reference.hpp" // The reference proxy
18 #include "etl/concepts.hpp"
19 
20 namespace etl {
21 
27 template <adaptable Matrix>
28 struct strictly_lower_matrix final : adapter<Matrix>, iterable<const strictly_lower_matrix<Matrix>> {
29  using matrix_t = Matrix;
30  using expr_t = matrix_t;
32 
33  static constexpr size_t n_dimensions = etl_traits<matrix_t>::dimensions();
35  static constexpr size_t alignment = matrix_t::alignment;
36 
39  using const_memory_type = const value_type*;
40 
43 
45 
49  template <typename V = default_vec>
50  using vec_type = typename V::template vec_type<value_type>;
51 
52  using base_type::value;
53 
54 public:
61  //Nothing else to init
62  }
63 
72  //Nothing else to init
73  }
74 
79  explicit strictly_lower_matrix(size_t dim) noexcept : base_type(dim) {
80  //Nothing else to init
81  }
82 
87  strictly_lower_matrix(const strictly_lower_matrix& rhs) = default;
88 
95 
100  strictly_lower_matrix(strictly_lower_matrix&& rhs) noexcept = default;
101 
107  strictly_lower_matrix& operator=(strictly_lower_matrix&& rhs) noexcept = default;
108 
114  template <convertible_expr<value_type> E>
115  strictly_lower_matrix& operator=(E&& e) noexcept(false) {
116  // Make sure the other matrix is strictly lower triangular
118  throw strictly_lower_exception();
119  }
120 
121  // Perform the real assign
122 
123  validate_assign(*this, e);
124 
125  // Avoid aliasing issues
126  if constexpr (!decay_traits<E>::is_linear) {
127  if (e.alias(*this)) {
128  // Create a temporary to hold the result
129  this_type tmp(*this);
130 
131  // Assign the expression to the temporary
132  tmp = e;
133 
134  // Assign the temporary to this matrix
135  *this = tmp;
136  } else {
137  e.assign_to(*this);
138  }
139  } else {
140  // Direct assignment of the expression into this matrix
141  e.assign_to(*this);
142  }
143 
144  return *this;
145  }
146 
152  template <etl_expr R>
154  // Make sure the other matrix is strictly lower triangular
155  if (!is_strictly_lower_triangular(rhs)) {
156  throw strictly_lower_exception();
157  }
158 
159  validate_expression(*this, rhs);
160  rhs.assign_add_to(*this);
161  return *this;
162  }
163 
169  template <etl_expr R>
171  // Make sure the other matrix is strictly lower triangular
172  if (!is_strictly_lower_triangular(rhs)) {
173  throw strictly_lower_exception();
174  }
175 
176  validate_expression(*this, rhs);
177  rhs.assign_sub_to(*this);
178  return *this;
179  }
180 
188  return *this;
189  }
190 
196  template <etl_expr R>
198  // Make sure the other matrix is strictly lower triangular
199  if (!is_strictly_lower_triangular(rhs)) {
200  throw strictly_lower_exception();
201  }
202 
203  validate_expression(*this, rhs);
204  rhs.assign_mul_to(*this);
205  return *this;
206  }
207 
215  return *this;
216  }
217 
223  template <etl_expr R>
225  // Make sure the other matrix is strictly lower triangular
226  if (!is_strictly_lower_triangular(rhs)) {
227  throw strictly_lower_exception();
228  }
229 
230  validate_expression(*this, rhs);
231  rhs.assign_mul_to(*this);
232  return *this;
233  }
234 
242  return *this;
243  }
244 
250  template <etl_expr R>
252  // Make sure the other matrix is strictly lower triangular
253  if (!is_strictly_lower_triangular(rhs)) {
254  throw strictly_lower_exception();
255  }
256 
257  validate_expression(*this, rhs);
258  rhs.assign_div_to(*this);
259  return *this;
260  }
261 
269  return *this;
270  }
271 
277  template <etl_expr R>
279  // Make sure the other matrix is strictly lower triangular
280  if (!is_strictly_lower_triangular(rhs)) {
281  throw strictly_lower_exception();
282  }
283 
284  validate_expression(*this, rhs);
285  rhs.assign_mod_to(*this);
286  return *this;
287  }
288 
298  return {value, i, j};
299  }
300 
301  using base_type::operator();
302 };
303 
307 template <typename Matrix>
308 struct etl_traits<strictly_lower_matrix<Matrix>> : wrapper_traits<strictly_lower_matrix<Matrix>> {};
309 
310 } //end of namespace etl
CRTP class to inject iterators functions.
Definition: iterable.hpp:23
void assign_to(L &&lhs) const
Assign to the given left-hand-side expression.
Definition: adapter.hpp:279
Contains base class for adapters.
strictly_lower_matrix & operator%=(const R &rhs)
Modulo each element by the value of the elements in the right hand side expression.
Definition: strictly_lower.hpp:278
strictly_lower_matrix & operator*=(const value_type &rhs) noexcept
Multiply each element by the right hand side scalar.
Definition: strictly_lower.hpp:186
adapter< Matrix > base_type
The base type.
Definition: strictly_lower.hpp:44
static constexpr order storage_order
The storage order.
Definition: strictly_lower.hpp:34
typename V::template vec_type< value_type > vec_type
The vectorization type for V.
Definition: adapter.hpp:33
strictly_lower_matrix & operator>>=(const R &rhs)
Multiply each element by the value of the elements in the right hand side expression.
Definition: strictly_lower.hpp:224
strictly_lower_detail::strictly_lower_reference< matrix_t > operator()(size_t i, size_t j) noexcept
Access the (i, j) element of the 2D matrix.
Definition: strictly_lower.hpp:297
order
Storage order of a matrix.
Definition: order.hpp:15
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: adapter.hpp:324
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: adapter.hpp:315
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
strictly_lower_matrix(size_t dim) noexcept
Construct a new strictly lower triangular matrix and fill it with zeros.
Definition: strictly_lower.hpp:79
strictly_lower_matrix & operator+=(const R &rhs)
Multiply each element by the value of the elements in the right hand side expression.
Definition: strictly_lower.hpp:153
A proxy representing a reference to a mutable element of a strictly lower triangular matrix...
Definition: strictly_lower_reference.hpp:24
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
strictly_lower_matrix(value_type value) noexcept
Construct a new strictly lower triangular matrix and fill it witht the given value.
Definition: strictly_lower.hpp:71
A strictly lower triangular matrix adapter.
Definition: strictly_lower.hpp:28
strictly_lower_matrix & operator=(E &&e) noexcept(false)
Assign the values of the ETL expression to the strictly lower triangular matrix.
Definition: strictly_lower.hpp:115
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
value_type * memory_type
The memory type.
Definition: adapter.hpp:26
Exception that is thrown when an operation is made to a strictly lower triangular matrix that would r...
Definition: strictly_lower_exception.hpp:24
A base class for adapters.
Definition: adapter.hpp:21
matrix_t value
The adapted matrix.
Definition: adapter.hpp:36
strictly_lower_matrix & operator=(const strictly_lower_matrix &rhs)=default
Assign to the matrix by copy.
strictly_lower_matrix & operator*=(const R &rhs)
Multiply each element by the value of the elements in the right hand side expression.
Definition: strictly_lower.hpp:197
strictly_lower_matrix & operator%=(const value_type &rhs) noexcept
Modulo each element by the right hand side scalar.
Definition: strictly_lower.hpp:267
strictly_lower_matrix() noexcept
Construct a new strictly lower triangular matrix and fill it with zeros.
Definition: strictly_lower.hpp:60
Represents a scalar value.
Definition: concepts_base.hpp:19
strictly_lower_matrix & operator-=(const R &rhs)
Multiply each element by the value of the elements in the right hand side expression.
Definition: strictly_lower.hpp:170
matrix_t expr_t
The wrapped expression type.
Definition: adapter.hpp:23
etl::iterator< const this_type > const_iterator
The const iterator type.
Definition: dyn_matrix_view.hpp:37
typename matrix_t::const_iterator iterator
The type of const iterator.
Definition: strictly_lower.hpp:41
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: adapter.hpp:306
Matrix matrix_t
The adapted matrix type.
Definition: strictly_lower.hpp:29
Contains strictly lower triangular matrix exception implementation.
static constexpr size_t alignment
The memory alignment.
Definition: strictly_lower.hpp:35
Contains strictly lower triangular matrix reference proxy implementation.
strictly_lower_matrix & operator/=(const R &rhs)
Modulo each element by the value of the elements in the right hand side expression.
Definition: strictly_lower.hpp:251
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
strictly_lower_matrix & operator/=(const value_type &rhs) noexcept
Divide each element by the right hand side scalar.
Definition: strictly_lower.hpp:240
bool is_strictly_lower_triangular(E &&expr)
Indicates if the given expression is a strictly lower triangular matrix or not.
Definition: adapters.hpp:184
typename matrix_t::const_iterator const_iterator
The type of const iterator.
Definition: strictly_lower.hpp:42
Traits for wrapper expressions.
Definition: wrapper_traits.hpp:21
Matrix matrix_t
The adapted matrix type.
Definition: adapter.hpp:22
strictly_lower_matrix & operator>>=(const value_type &rhs) noexcept
Multiply each element by the right hand side scalar.
Definition: strictly_lower.hpp:213
static constexpr size_t n_dimensions
The number of dimensions.
Definition: strictly_lower.hpp:33