KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
amatrix_interface.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Pooyan Dadvand
11 //
12 
13 #if !defined(KRATOS_AMATRIX_INTERFACE_H_INCLUDED )
14 #define KRATOS_AMATRIX_INTERFACE_H_INCLUDED
15 
16 
17 
18 // System includes
19 #include <string>
20 #include <iostream>
21 
22 
23 // External includes
24 #include "amatrix.h"
25 
26 // Project includes
27 #include "includes/define.h"
28 #include "includes/checks.h"
29 
30 
31 namespace Kratos
32 {
33 
34 namespace Internals {
35 
36 // This class is a modified copy of the AMatrix::Matrix to cover the backward compatibility with ublas in current Kratos implementation.
37 // The idea is to go step by step toward the original Matrix class and eliminate the differences. So please do not add new API to it.
38 template <typename TDataType, std::size_t TSize1, std::size_t TSize2>
39 class Matrix : public AMatrix::MatrixExpression<Matrix<TDataType, TSize1, TSize2>,
40  AMatrix::row_major_access>,
41  public AMatrix::MatrixStorage<TDataType, TSize1, TSize2> {
42  public:
43  using data_type = TDataType;
44  using base_type = AMatrix::MatrixStorage<TDataType, TSize1, TSize2>;
45  using base_type::at;
46  using base_type::data;
47  using base_type::size;
48  using base_type::size1;
49  using base_type::size2;
50  using base_type::operator();
51 
52  using iterator = AMatrix::RandomAccessIterator<TDataType>;
53  using const_iterator = AMatrix::RandomAccessIterator<const TDataType>;
54 
55  //ublas compatibility definitions
56  using value_type = TDataType;
57  using size_type = std::size_t;
58  using difference_type = std::size_t;
59  using const_reference = const TDataType&;
60  using reference = TDataType&;
61  using const_pointer = TDataType*;
62  using pointer = TDataType*;
63 
64 
65  Matrix(): base_type(0, 0) {}
66 
67  explicit Matrix(std::size_t TheSize1, std::size_t TheSize2)
68  : base_type(TheSize1, TheSize2) {}
69 
70  explicit Matrix(std::size_t TheSize)
71  : base_type(TheSize) {}
72 
73  Matrix(Matrix const& Other) : base_type(Other) {}
74 
75  Matrix(Matrix&& Other) : base_type(Other) {}
76 
77  template <typename TExpressionType, std::size_t TCategory>
78  Matrix(AMatrix::MatrixExpression<TExpressionType, TCategory> const& Other)
79  : base_type(Other) {}
80 
81  template <typename TExpressionType>
82  Matrix(AMatrix::MatrixExpression<TExpressionType, AMatrix::row_major_access> const& Other)
83  : base_type(Other) {}
84 
85  // template <typename TOtherMatrixType>
86  // explicit Matrix(TOtherMatrixType const& Other) : base_type(Other) {}
87 
88  explicit Matrix(std::initializer_list<TDataType> InitialValues)
89  : base_type(InitialValues) {}
90 
91  template <typename TExpressionType, std::size_t TCategory>
93  AMatrix::MatrixExpression<TExpressionType, TCategory> const& Other) {
94  KRATOS_DEBUG_ERROR_IF(Other.expression().check_aliasing(data(), data()+size())) << "Aliasing found in assigning Matrix";
95  base_type::operator=(Other.expression());
96  return *this;
97  }
98 
99  template <typename TExpressionType>
101  AMatrix::MatrixExpression<TExpressionType, AMatrix::row_major_access> const& Other) {
102  base_type::operator=(Other.expression());
103  return *this;
104  }
105 
106  //template <typename TOtherMatrixType>
107  //Matrix& operator=(TOtherMatrixType const& Other) {
108  // base_type::operator=(Other);
109  // return *this;
110  //}
111 
112  Matrix& operator=(Matrix const& Other) {
113  base_type::operator=(Other);
114  return *this;
115  }
116 
117  Matrix& operator=(Matrix&& Other) {
118  base_type::operator=(Other);
119  return *this;
120  }
121 
122  TDataType& operator()(std::size_t i) { return at(i); }
123 
124  TDataType const& operator()(std::size_t i) const { return at(i); }
125 
126  friend bool operator==(Matrix const& First, Matrix const& Second) {
127 
128  if( First.size1() != Second.size1() ||
129  First.size2() != Second.size2()) {
130  return false;
131  }
132 
133  for (std::size_t i = 0; i < First.size(); i++)
134  if (First._data[i] != Second._data[i])
135  return false;
136  return true;
137  }
138 
139  template <typename TExpressionType, std::size_t TCategory>
141  AMatrix::MatrixExpression<TExpressionType, TCategory> const& Other) {
143  this->expression().size1() != Other.expression().size1() || this->expression().size2() != Other.expression().size2())
144  << "Size mismatch in Matrix operator+=" << std::endl
145  << "LHS has size (" << this->expression().size1() << "," << this->expression().size2() <<"), RHS has size ("
146  << Other.expression().size1() << "," << Other.expression().size2() << ")." << std::endl;
147 
148  KRATOS_DEBUG_ERROR_IF(Other.expression().check_aliasing(data(), data()+size())) << "Aliasing found in += operator";
149 
150  for (std::size_t i = 0; i < size1(); i++)
151  for (std::size_t j = 0; j < size2(); j++)
152  at(i, j) += Other.expression()(i, j);
153 
154  return *this;
155  }
156 
157  template <typename TExpressionType>
159  AMatrix::MatrixExpression<TExpressionType, AMatrix::row_major_access> const& Other) {
161  this->expression().size1() != Other.expression().size1() || this->expression().size2() != Other.expression().size2())
162  << "Size mismatch in Matrix operator+=" << std::endl
163  << "LHS has size (" << this->expression().size1() << "," << this->expression().size2() <<"), RHS has size ("
164  << Other.expression().size1() << "," << Other.expression().size2() << ")." << std::endl;
165 
166  for (std::size_t i = 0; i < size(); i++)
167  at(i) += Other.expression()[i];
168 
169  return *this;
170  }
171 
172  template <typename TExpressionType, std::size_t TCategory>
174  AMatrix::MatrixExpression<TExpressionType, TCategory> const& Other) {
176  this->expression().size1() != Other.expression().size1() || this->expression().size2() != Other.expression().size2())
177  << "Size mismatch in Matrix operator-=" << std::endl
178  << "LHS has size (" << this->expression().size1() << "," << this->expression().size2() <<"), RHS has size ("
179  << Other.expression().size1() << "," << Other.expression().size2() << ")." << std::endl;
180 
181  KRATOS_DEBUG_ERROR_IF(Other.expression().check_aliasing(data(), data()+size())) << "Aliasing found in -= operator";
182 
183  for (std::size_t i = 0; i < size1(); i++)
184  for (std::size_t j = 0; j < size2(); j++)
185  at(i, j) -= Other.expression()(i, j);
186 
187  return *this;
188  }
189 
190  template <typename TExpressionType>
192  AMatrix::MatrixExpression<TExpressionType, AMatrix::row_major_access> const& Other) {
194  this->expression().size1() != Other.expression().size1() || this->expression().size2() != Other.expression().size2())
195  << "Size mismatch in Matrix operator-=" << std::endl
196  << "LHS has size (" << this->expression().size1() << "," << this->expression().size2() <<"), RHS has size ("
197  << Other.expression().size1() << "," << Other.expression().size2() << ")." << std::endl;
198 
199  for (std::size_t i = 0; i < size(); i++)
200  at(i) -= Other.expression()[i];
201 
202  return *this;
203  }
204 
206  for (std::size_t i = 0; i < size(); i++)
207  at(i) *= TheValue;
208 
209  return *this;
210  }
211 
213  auto inverse_of_value = 1.00 / TheValue;
214  for (std::size_t i = 0; i < size(); i++)
215  at(i) *= inverse_of_value;
216 
217  return *this;
218  }
219 
220  AMatrix::MatrixUnaryMinusExpression<Matrix> operator-() const {
221  return AMatrix::MatrixUnaryMinusExpression<Matrix>(*this);
222  }
223 
224  void resize(std::size_t NewSize1, std::size_t NewSize2, bool preserve = 0){
225  KRATOS_DEBUG_ERROR_IF(preserve) << "The preserve is not supported anymore" << std::endl;
226 
227  base_type::resize(NewSize1,NewSize2);
228  }
229 
230  void resize(std::size_t NewSize, bool preserve = 0){
231  KRATOS_DEBUG_ERROR_IF(preserve) << "The preserve is not supported anymore" << std::endl;
232  base_type::resize(NewSize);
233  }
234 
235  void resize(std::size_t NewSize, TDataType const& TheValue){
236  base_type::resize(NewSize);
237  for (std::size_t i = 0; i < size(); i++)
238  at(i) = TheValue;
239  }
240 
241  iterator begin() { return iterator(data()); }
242 
243  iterator end() { return iterator(data() + size()); }
244 
245  const_iterator begin() const { return const_iterator(data()); }
246 
247  const_iterator end() const { return const_iterator(data() + size()); }
248 
249  template <typename TExpressionType>
251  AMatrix::MatrixExpression<TExpressionType, AMatrix::row_major_access> const& Other)
252  const {
253  data_type result = data_type();
254  for (std::size_t i = 0; i < size(); ++i) {
255  result += at(i) * Other.expression()[i];
256  }
257  return result;
258  }
259 
260  data_type squared_norm() const { return dot(*this); }
261 
262  data_type norm() const { return std::sqrt(dot(*this)); }
263 
264  void normalize() {
265  auto the_norm = norm();
266  if (the_norm > std::numeric_limits<data_type>::epsilon()) {
267  const auto norm_inverse = 1.0 / the_norm;
268  for (std::size_t i = 0; i < size(); ++i) {
269  at(i) *= norm_inverse;
270  }
271  }
272  }
273 
274  Matrix& noalias() { return *this; }
275 
276  AMatrix::TransposeMatrix<const Matrix<TDataType, TSize1, TSize2>> transpose() const {
277  return AMatrix::TransposeMatrix<const Matrix<TDataType, TSize1, TSize2>>(*this);
278  }
279 
280  AMatrix::TransposeMatrix<Matrix<TDataType, TSize1, TSize2>> transpose() {
281  return AMatrix::TransposeMatrix<Matrix<TDataType, TSize1, TSize2>>(*this);
282  }
283 
284  void clear(){
285  for (std::size_t i = 0; i < size(); i++)
286  at(i) = TDataType();
287  }
288 
289  void swap(Matrix& Other){
290  base_type::swap(Other);
291  }
292 
293  bool check_aliasing(const data_type* From, const data_type* To) const {
294  const data_type* const end_pointer = data() + size();
295  bool check1 = ((From <= data()) && (data() < To));
296  bool check2 = ((From < end_pointer) && (end_pointer < To)); // I'm not sure if should be =< To. Pooyan.
297  bool check3 = ((From > data()) && (To <= end_pointer));
298 
299  return (check1 || check2 || check3);
300  }
301 
302  void fill(data_type const& value) {
303  for (std::size_t i = 0; i < size(); i++)
304  at(i) = value;
305 
306  }
307 
308  void fill_identity() {
309  KRATOS_ERROR_IF(size1() != size2()) << "fill_identity is only supported for square matrices." << std::endl;
310  fill(0.00);
311  const std::size_t next_diagonal = size1() + 1;
312  for (std::size_t i = 0; i < size(); i += next_diagonal)
313  at(i) = 1.00;
314 
315  }
316 
317 };
318 
319 template <typename TDataType, std::size_t TSize1, std::size_t TSize2>
321  Matrix<TDataType, TSize1, TSize2> const& Second) {
322  return !(First == Second);
323 }
324 
328 template <typename TExpressionType, std::size_t TCategory>
329 inline std::ostream& operator<<(std::ostream& rOStream,
330  AMatrix::MatrixExpression<TExpressionType, TCategory> const& TheMatrix) {
331  TExpressionType const& the_expression = TheMatrix.expression();
332  if ((the_expression.size1() == 1) || (the_expression.size2() == 1)) { // writing in vector format
333  const std::size_t size = the_expression.size();
334  rOStream << "[" << size << "](";
335  if (size > 0)
336  rOStream << the_expression[0];
337  for (std::size_t i = 1; i < size; i++) {
338  rOStream << "," << the_expression[i];
339  }
340  rOStream << ")";
341  }
342  else // writing in matrix format
343  {
344  const std::size_t size2 = the_expression.size2();
345  rOStream << "[" << the_expression.size1() << "," << the_expression.size2() << "](";
346  if (the_expression.size1() > 0) {
347  rOStream << "(";
348  if (size2 > 0)
349  rOStream << the_expression(0, 0);
350  for (std::size_t j = 1; j < size2; j++) {
351  rOStream << "," << the_expression(0, j);
352  }
353  rOStream << ")";
354  }
355  for (std::size_t i = 1; i < the_expression.size1(); i++) {
356  rOStream << ",(";
357  if (size2 > 0)
358  rOStream << the_expression(i, 0);
359  for (std::size_t j = 1; j < size2; j++) {
360  rOStream << "," << the_expression(i, j);
361  }
362  rOStream << ")";
363  }
364  rOStream << ")";
365  }
366  return rOStream;
367 }
368 
372 template <typename TDataType, std::size_t TSize1, std::size_t TSize2>
373 inline std::istream& operator>>(std::istream& rIStream,
375 
376  std::size_t size1;
377  std::size_t size2;
378 
379  char c;
380 
381  rIStream >> c; // skipping the '['
382  KRATOS_DEBUG_CHECK(c == '[');
383 
384  rIStream >> size1;
385 
386  rIStream >> c; // skipping the ','
387  KRATOS_DEBUG_CHECK(c == ',');
388 
389  rIStream >> size2;
390 
391  rIStream >> c; // skipping the ']'
392  KRATOS_DEBUG_CHECK(c == ']');
393 
394  TheMatrix.resize(size1, size2);
395 
396  rIStream >> c; // skipping the '('
397  KRATOS_DEBUG_CHECK(c == '(');
398 
399  for (std::size_t i = 0; i < size1; i++) {
400  if (i > 0) {
401  rIStream >> c; // skipping the row ','
402  KRATOS_DEBUG_CHECK(c == ',');
403  }
404  rIStream >> c; // skipping the row '('
405  KRATOS_DEBUG_CHECK(c == '(');
406 
407  for (std::size_t j = 0; j < size2; j++) {
408  if (j > 0) {
409  rIStream >> c; // skipping the ','
410  KRATOS_DEBUG_CHECK(c == ',');
411  }
412  rIStream >> TheMatrix(i, j);
413  }
414 
415  rIStream >> c; // skipping the row ')'
416  KRATOS_DEBUG_CHECK(c == ')');
417  }
418 
419  rIStream >> c; // skipping the final ')'
420  KRATOS_DEBUG_CHECK(c == ')');
421 
422  return rIStream;
423 }
424 
428 template <typename TDataType, std::size_t TSize1>
429 inline std::istream& operator>>(std::istream& rIStream,
430  Matrix<TDataType, TSize1, 1>& TheMatrix) {
431 
432  std::size_t size1;
433 
434 
435  char c;
436 
437  rIStream >> c; // skipping the '['
438  KRATOS_DEBUG_CHECK(c == '[');
439 
440  rIStream >> size1;
441 
442  rIStream >> c; // skipping the ']'
443  KRATOS_DEBUG_CHECK(c == ']');
444 
445  TheMatrix.resize(size1);
446 
447  rIStream >> c; // skipping the '('
448  KRATOS_DEBUG_CHECK(c == '(');
449 
450  for (std::size_t i = 0; i < size1; i++) {
451  if (i > 0) {
452  rIStream >> c; // skipping the ','
453  KRATOS_DEBUG_CHECK(c == ',');
454  }
455  rIStream >> TheMatrix[i];
456  }
457 
458  rIStream >> c; // skipping the ')'
459  KRATOS_DEBUG_CHECK(c == ')');
460 
461  return rIStream;
462 }
463 
464 
465 } // namespace Internals
466 
469 
471 
473 
475 
477 
478 template <typename TDataType, std::size_t TSize> using BoundedVector=Internals::Matrix<TDataType,TSize, 1>;
479 
480 template <typename TDataType, std::size_t TSize1, std::size_t TSize2> using BoundedMatrix=Internals::Matrix<TDataType,TSize1, TSize2>;
481 
482 template <typename TDataType, std::size_t TSize> using BoundedVector=Internals::Matrix<TDataType,TSize, 1>;
483 
484 template <typename T> T& noalias(T& TheMatrix){return TheMatrix;}
485 
486 template <typename T> AMatrix::TransposeMatrix<const T> trans(const T& TheMatrix){ return AMatrix::TransposeMatrix<const T>(TheMatrix);}
487 
488 template <typename T> AMatrix::TransposeMatrix<T> trans(T& TheMatrix){ return AMatrix::TransposeMatrix<T>(TheMatrix); }
489 
490 template <typename TExpressionType> using vector_expression = AMatrix::MatrixExpression<TExpressionType,AMatrix::row_major_access>;
491 
492 template <typename TExpressionType> using MatrixRow = AMatrix::MatrixRow<TExpressionType>;
493 
494 
495 template <typename TDataType>
497  : public AMatrix::MatrixExpression<KratosZeroMatrix<TDataType>, AMatrix::row_major_access> {
498  std::size_t _size1;
499  std::size_t _size2;
500 
501  public:
502  using data_type = TDataType;
503 
504  KratosZeroMatrix() = delete;
505 
506  KratosZeroMatrix(std::size_t Size) = delete;
507 
508  KratosZeroMatrix(std::size_t Size1, std::size_t Size2)
509  : _size1(Size1), _size2(Size2) {}
510 
511  inline TDataType operator()(std::size_t i, std::size_t j) const {
512  return TDataType();
513  }
514 
515  inline TDataType operator[](std::size_t i) const { return TDataType(); }
516 
517  inline std::size_t size1() const { return _size1; }
518  inline std::size_t size2() const { return _size2; }
519 
520  inline std::size_t size() const { return _size1 * _size2; }
521 
522  bool check_aliasing(const data_type* From, const data_type* To) const {
523  return false;
524  }
525 };
526 
527 
528 template <typename TDataType>
530  : public AMatrix::MatrixExpression<KratosZeroVector<TDataType>, AMatrix::row_major_access> {
531  std::size_t _size1;
532 
533  public:
534  using data_type = TDataType;
535 
536  KratosZeroVector() = delete;
537 
538  KratosZeroVector(std::size_t Size)
539  : _size1(Size) {}
540 
541  KratosZeroVector(std::size_t Size1, std::size_t Size2) = delete;
542 
543  inline TDataType operator()(std::size_t i, std::size_t j) const {
544  return TDataType();
545  }
546 
547  inline TDataType operator[](std::size_t i) const { return TDataType(); }
548 
549  inline std::size_t size1() const { return _size1; }
550  inline std::size_t size2() const { return 1; }
551 
552  inline std::size_t size() const { return _size1; }
553 
554  bool check_aliasing(const data_type* From, const data_type* To) const {
555  return false;
556  }
557 };
558 
560 
562 
563 
564 using IdentityMatrix = AMatrix::IdentityMatrix<double>;
565 
566 template <typename TExpression1Type, typename TExpression2Type,
567  std::size_t TCategory1, std::size_t TCategory2>
568 AMatrix::MatrixProductExpression<TExpression1Type, TExpression2Type> prod(
569  AMatrix::MatrixExpression<TExpression1Type, TCategory1> const& First,
570  AMatrix::MatrixExpression<TExpression2Type, TCategory2> const& Second) {
571  KRATOS_DEBUG_ERROR_IF(First.expression().size2() != Second.expression().size1())
572  << "Size mismatch in AMatrix prod." << std::endl
573  << "Argument sizes are (" << First.expression().size1() << "," << First.expression().size2() << ") and ("
574  << Second.expression().size1() << "," << Second.expression().size2() << ")." << std::endl;
575 
576  return AMatrix::MatrixProductExpression<TExpression1Type, TExpression2Type>(
577  First.expression(), Second.expression());
578 }
579 
580 template <typename TExpression1Type, typename TExpression2Type,
581  std::size_t TCategory1, std::size_t TCategory2>
582 AMatrix::VectorOuterProductExpression<TExpression1Type, TExpression2Type> outer_prod(
583  AMatrix::MatrixExpression<TExpression1Type, TCategory1> const& First,
584  AMatrix::MatrixExpression<TExpression2Type, TCategory2> const& Second) {
585 
586  return AMatrix::VectorOuterProductExpression<TExpression1Type, TExpression2Type>(
587  First.expression(), Second.expression());
588 }
589 
590 template <typename TExpression1Type, typename TExpression2Type,
591  std::size_t TCategory1, std::size_t TCategory2>
592 typename TExpression1Type::data_type inner_prod(
593  AMatrix::MatrixExpression<TExpression1Type, TCategory1> const& First,
594  AMatrix::MatrixExpression<TExpression2Type, TCategory2> const& Second) {
595  KRATOS_DEBUG_ERROR_IF(First.expression().size() != Second.expression().size())
596  << "Size mismatch in AMatrix inner_prod." << std::endl
597  << "Argument sizes are " << First.expression().size() << " and "
598  << Second.expression().size() << "." << std::endl
599  << "Both vectors should have the same size." << std::endl;
600 
601  using data_type = typename TExpression1Type::data_type;
602  auto& the_expression1 = First.expression();
603  auto& the_expression2 = Second.expression();
604  data_type result = data_type();
605  for (std::size_t i = 0; i < the_expression1.size(); ++i) {
606  result += the_expression1[i] * the_expression2[i];
607  }
608  return result;
609 
610 }
611 
612  template <typename TExpressionType, std::size_t TCategory>
613  typename TExpressionType::data_type norm_1(
614  AMatrix::MatrixExpression<TExpressionType, TCategory> const& TheExpression) {
615  using data_type = typename TExpressionType::data_type;
616  auto& the_expression = TheExpression.expression();
617  data_type result = data_type();
618  for (std::size_t i = 0; i < the_expression.size(); ++i) {
619  result += std::abs(the_expression[i]);
620  }
621  return result;
622  }
623 
624 template <typename TExpressionType, std::size_t TCategory>
625  typename TExpressionType::data_type norm_2(
626  AMatrix::MatrixExpression<TExpressionType, TCategory> const& TheExpression) {
627  using data_type = typename TExpressionType::data_type;
628  auto& the_expression = TheExpression.expression();
629  data_type result = data_type();
630  for (std::size_t i = 0; i < the_expression.size(); ++i) {
631  result += the_expression[i] * the_expression[i];
632  }
633  return std::sqrt(result);
634 }
635 
636  template <typename TExpressionType, std::size_t TCategory>
637  AMatrix::MatrixColumn<const TExpressionType> column(
638  AMatrix::MatrixExpression<TExpressionType, TCategory> const& TheExpression, std::size_t ColumnIndex) {
639  return AMatrix::MatrixColumn<const TExpressionType>(TheExpression.expression(), ColumnIndex);
640  }
641 
642  template <typename TExpressionType, std::size_t TCategory>
643  AMatrix::MatrixColumn<TExpressionType> column(
644  AMatrix::MatrixExpression<TExpressionType, TCategory>& TheExpression, std::size_t ColumnIndex) {
645  return AMatrix::MatrixColumn<TExpressionType>(TheExpression.expression(), ColumnIndex);
646  }
647 
648 template <typename TExpressionType, std::size_t TCategory>
649  AMatrix::MatrixRow<const TExpressionType> row(
650  AMatrix::MatrixExpression<TExpressionType, TCategory> const& TheExpression, std::size_t RowIndex) {
651  return AMatrix::MatrixRow<const TExpressionType>(TheExpression.expression(), RowIndex);
652 }
653 
654 
655  template <typename TExpressionType, std::size_t TCategory>
656  AMatrix::MatrixRow<TExpressionType> row(
657  AMatrix::MatrixExpression<TExpressionType, TCategory>& TheExpression, std::size_t RowIndex) {
658  return AMatrix::MatrixRow<TExpressionType>(TheExpression.expression(), RowIndex);
659  }
660 
661 template <typename TExpressionType, std::size_t TCategory>
662  AMatrix::SubVector<const TExpressionType> subrange(
663  AMatrix::MatrixExpression<TExpressionType, TCategory> const& TheExpression, std::size_t From, std::size_t To) {
664  return AMatrix::SubVector<const TExpressionType>(TheExpression.expression(), From,To - From);
665 }
666 
667 template <typename TExpressionType, std::size_t TCategory>
668  AMatrix::SubVector<TExpressionType> subrange(
669  AMatrix::MatrixExpression<TExpressionType, TCategory>& TheExpression, std::size_t From, std::size_t To) {
670  return AMatrix::SubVector<TExpressionType>(TheExpression.expression(), From,To - From);
671 }
672 
673 
674  template <typename TExpressionType, std::size_t TCategory>
675  typename TExpressionType::data_type sum(
676  AMatrix::MatrixExpression<TExpressionType, TCategory> const& TheExpression) {
677  using data_type = typename TExpressionType::data_type;
678  auto& the_expression = TheExpression.expression();
679  data_type result = data_type();
680  for (std::size_t i = 0; i < the_expression.size(); ++i) {
681  result += the_expression[i];
682  }
683  return result;
684  }
685 
686 template <typename TExpressionType, std::size_t TCategory>
687  typename TExpressionType::data_type norm_frobenius(
688  AMatrix::MatrixExpression<TExpressionType, TCategory> const& TheExpression) {
689  using data_type = typename TExpressionType::data_type;
690  auto& the_expression = TheExpression.expression();
691  data_type result = data_type();
692  for (std::size_t i = 0; i < the_expression.size(); ++i) {
693  result += the_expression[i] * the_expression[i];
694  }
695  return std::sqrt(result);
696 }
697 
698 
699  template <typename TDataType>
701  : public AMatrix::MatrixExpression<scalar_matrix<TDataType>, AMatrix::row_major_access> {
702  std::size_t _size1;
703  std::size_t _size2;
704  const TDataType _value;
705 
706  public:
707  using data_type = TDataType;
708 
709  scalar_matrix() = delete;
710 
711  scalar_matrix(std::size_t Size1, std::size_t Size2, TDataType const& Value)
712  : _size1(Size1), _size2(Size2), _value(Value) {}
713 
714  inline TDataType operator()(std::size_t i, std::size_t j) const {
715  return _value;
716  }
717 
718  inline TDataType operator[](std::size_t i) const { return _value; }
719 
720  inline std::size_t size1() const { return _size1; }
721  inline std::size_t size2() const { return _size2; }
722 
723  bool check_aliasing(const data_type* From, const data_type* To) const {
724  return false;
725  }
726  };
727 
729 
730 
731  template <typename TExpressionType, typename TIndicesVectorType>
732  class PermutationMatrix : public AMatrix::MatrixExpression<PermutationMatrix<TExpressionType, TIndicesVectorType>, AMatrix::unordered_access> {
733  TExpressionType& _original_expression;
734  TIndicesVectorType const& _permutation_indices_i;
735  TIndicesVectorType const& _permutation_indices_j;
736  public:
737  using data_type = typename TExpressionType::data_type;
738  using value_type = typename TExpressionType::data_type;
739  PermutationMatrix() = delete;
740 
741  PermutationMatrix(TExpressionType& Original, TIndicesVectorType const& PermutaionIndices)
742  : _original_expression(Original),
743  _permutation_indices_i(PermutaionIndices),
744  _permutation_indices_j(PermutaionIndices) {}
745 
746  PermutationMatrix(TExpressionType& Original, TIndicesVectorType const& PermutaionIndicesI, TIndicesVectorType const& PermutaionIndicesJ)
747  : _original_expression(Original),
748  _permutation_indices_i(PermutaionIndicesI),
749  _permutation_indices_j(PermutaionIndicesJ) {}
750 
751  inline data_type const& operator()(std::size_t i, std::size_t j) const {
752  return _original_expression(_permutation_indices_i[i], _permutation_indices_j[j]);
753  }
754 
755  inline data_type& operator()(std::size_t i, std::size_t j) {
756  return _original_expression(_permutation_indices_i[i], _permutation_indices_j[j]);
757  }
758 
759  inline std::size_t size() const { return size1() * size2(); }
760  inline std::size_t size1() const { return _permutation_indices_i.size(); }
761  inline std::size_t size2() const { return _permutation_indices_j.size(); }
762 
763  bool check_aliasing(const data_type* From, const data_type* To) const {
764  return _original_expression.check_aliasing(From, To);
765  }
766 
767  };
768 
769 
770 
771 
775 
779 
783 
784 
788 
789 
793 
794 } // namespace Kratos.
795 
796 #endif // KRATOS_AMATRIX_INTERFACE_H_INCLUDED defined
797 
798 
799 
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
Definition: amatrix_interface.h:41
TDataType & operator()(std::size_t i)
Definition: amatrix_interface.h:122
void resize(std::size_t NewSize, TDataType const &TheValue)
Definition: amatrix_interface.h:235
void swap(Matrix &Other)
Definition: amatrix_interface.h:289
AMatrix::TransposeMatrix< Matrix< TDataType, TSize1, TSize2 > > transpose()
Definition: amatrix_interface.h:280
Matrix()
Definition: amatrix_interface.h:65
TDataType data_type
Definition: amatrix_interface.h:43
data_type dot(AMatrix::MatrixExpression< TExpressionType, AMatrix::row_major_access > const &Other) const
Definition: amatrix_interface.h:250
data_type norm() const
Definition: amatrix_interface.h:262
AMatrix::TransposeMatrix< const Matrix< TDataType, TSize1, TSize2 > > transpose() const
Definition: amatrix_interface.h:276
void resize(std::size_t NewSize1, std::size_t NewSize2, bool preserve=0)
Definition: amatrix_interface.h:224
Matrix & operator-=(AMatrix::MatrixExpression< TExpressionType, TCategory > const &Other)
Definition: amatrix_interface.h:173
Matrix(std::size_t TheSize)
Definition: amatrix_interface.h:70
Matrix & operator+=(AMatrix::MatrixExpression< TExpressionType, AMatrix::row_major_access > const &Other)
Definition: amatrix_interface.h:158
Matrix & operator=(AMatrix::MatrixExpression< TExpressionType, TCategory > const &Other)
Definition: amatrix_interface.h:92
TDataType * pointer
Definition: amatrix_interface.h:62
bool check_aliasing(const data_type *From, const data_type *To) const
Definition: amatrix_interface.h:293
std::size_t difference_type
Definition: amatrix_interface.h:58
Matrix & operator=(AMatrix::MatrixExpression< TExpressionType, AMatrix::row_major_access > const &Other)
Definition: amatrix_interface.h:100
Matrix & operator*=(data_type TheValue)
Definition: amatrix_interface.h:205
iterator end()
Definition: amatrix_interface.h:243
Matrix(Matrix const &Other)
Definition: amatrix_interface.h:73
void fill(data_type const &value)
Definition: amatrix_interface.h:302
TDataType * const_pointer
Definition: amatrix_interface.h:61
Matrix & noalias()
Definition: amatrix_interface.h:274
AMatrix::RandomAccessIterator< TDataType > iterator
Definition: amatrix_interface.h:52
Matrix(AMatrix::MatrixExpression< TExpressionType, TCategory > const &Other)
Definition: amatrix_interface.h:78
void fill_identity()
Definition: amatrix_interface.h:308
Matrix(std::size_t TheSize1, std::size_t TheSize2)
Definition: amatrix_interface.h:67
TDataType & reference
Definition: amatrix_interface.h:60
const_iterator end() const
Definition: amatrix_interface.h:247
Matrix(AMatrix::MatrixExpression< TExpressionType, AMatrix::row_major_access > const &Other)
Definition: amatrix_interface.h:82
void normalize()
Definition: amatrix_interface.h:264
void clear()
Definition: amatrix_interface.h:284
void resize(std::size_t NewSize, bool preserve=0)
Definition: amatrix_interface.h:230
TDataType const & operator()(std::size_t i) const
Definition: amatrix_interface.h:124
iterator begin()
Definition: amatrix_interface.h:241
Matrix & operator+=(AMatrix::MatrixExpression< TExpressionType, TCategory > const &Other)
Definition: amatrix_interface.h:140
AMatrix::MatrixUnaryMinusExpression< Matrix > operator-() const
Definition: amatrix_interface.h:220
AMatrix::MatrixStorage< TDataType, TSize1, TSize2 > base_type
Definition: amatrix_interface.h:44
Matrix & operator/=(data_type TheValue)
Definition: amatrix_interface.h:212
const_iterator begin() const
Definition: amatrix_interface.h:245
data_type squared_norm() const
Definition: amatrix_interface.h:260
TDataType value_type
Definition: amatrix_interface.h:56
AMatrix::RandomAccessIterator< const TDataType > const_iterator
Definition: amatrix_interface.h:53
const TDataType & const_reference
Definition: amatrix_interface.h:59
Matrix & operator=(Matrix const &Other)
Definition: amatrix_interface.h:112
friend bool operator==(Matrix const &First, Matrix const &Second)
Definition: amatrix_interface.h:126
Matrix & operator=(Matrix &&Other)
Definition: amatrix_interface.h:117
Matrix(std::initializer_list< TDataType > InitialValues)
Definition: amatrix_interface.h:88
Matrix & operator-=(AMatrix::MatrixExpression< TExpressionType, AMatrix::row_major_access > const &Other)
Definition: amatrix_interface.h:191
std::size_t size_type
Definition: amatrix_interface.h:57
Matrix(Matrix &&Other)
Definition: amatrix_interface.h:75
Definition: amatrix_interface.h:497
TDataType operator[](std::size_t i) const
Definition: amatrix_interface.h:515
std::size_t size1() const
Definition: amatrix_interface.h:517
std::size_t size2() const
Definition: amatrix_interface.h:518
bool check_aliasing(const data_type *From, const data_type *To) const
Definition: amatrix_interface.h:522
TDataType data_type
Definition: amatrix_interface.h:502
std::size_t size() const
Definition: amatrix_interface.h:520
KratosZeroMatrix(std::size_t Size1, std::size_t Size2)
Definition: amatrix_interface.h:508
TDataType operator()(std::size_t i, std::size_t j) const
Definition: amatrix_interface.h:511
KratosZeroMatrix(std::size_t Size)=delete
Definition: amatrix_interface.h:530
bool check_aliasing(const data_type *From, const data_type *To) const
Definition: amatrix_interface.h:554
TDataType data_type
Definition: amatrix_interface.h:534
std::size_t size1() const
Definition: amatrix_interface.h:549
TDataType operator[](std::size_t i) const
Definition: amatrix_interface.h:547
TDataType operator()(std::size_t i, std::size_t j) const
Definition: amatrix_interface.h:543
KratosZeroVector(std::size_t Size)
Definition: amatrix_interface.h:538
KratosZeroVector(std::size_t Size1, std::size_t Size2)=delete
std::size_t size2() const
Definition: amatrix_interface.h:550
std::size_t size() const
Definition: amatrix_interface.h:552
Definition: amatrix_interface.h:732
std::size_t size1() const
Definition: amatrix_interface.h:760
data_type & operator()(std::size_t i, std::size_t j)
Definition: amatrix_interface.h:755
typename TExpressionType::data_type data_type
Definition: amatrix_interface.h:737
data_type const & operator()(std::size_t i, std::size_t j) const
Definition: amatrix_interface.h:751
PermutationMatrix(TExpressionType &Original, TIndicesVectorType const &PermutaionIndices)
Definition: amatrix_interface.h:741
std::size_t size2() const
Definition: amatrix_interface.h:761
bool check_aliasing(const data_type *From, const data_type *To) const
Definition: amatrix_interface.h:763
typename TExpressionType::data_type value_type
Definition: amatrix_interface.h:738
std::size_t size() const
Definition: amatrix_interface.h:759
PermutationMatrix(TExpressionType &Original, TIndicesVectorType const &PermutaionIndicesI, TIndicesVectorType const &PermutaionIndicesJ)
Definition: amatrix_interface.h:746
Definition: amatrix_interface.h:701
TDataType operator[](std::size_t i) const
Definition: amatrix_interface.h:718
std::size_t size2() const
Definition: amatrix_interface.h:721
TDataType data_type
Definition: amatrix_interface.h:707
std::size_t size1() const
Definition: amatrix_interface.h:720
scalar_matrix(std::size_t Size1, std::size_t Size2, TDataType const &Value)
Definition: amatrix_interface.h:711
TDataType operator()(std::size_t i, std::size_t j) const
Definition: amatrix_interface.h:714
bool check_aliasing(const data_type *From, const data_type *To) const
Definition: amatrix_interface.h:723
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
#define KRATOS_DEBUG_CHECK(IsTrue)
Definition: checks.h:214
std::istream & operator>>(std::istream &rIStream, Matrix< TDataType, TSize1, TSize2 > &TheMatrix)
Definition: amatrix_interface.h:373
std::ostream & operator<<(std::ostream &rOStream, AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheMatrix)
Definition: amatrix_interface.h:329
bool operator!=(Matrix< TDataType, TSize1, TSize2 > const &First, Matrix< TDataType, TSize1, TSize2 > const &Second)
Definition: amatrix_interface.h:320
TSpaceType::IndexType Size1(TSpaceType &dummy, typename TSpaceType::MatrixType const &rM)
Definition: add_strategies_to_python.cpp:117
TSpaceType::IndexType Size2(TSpaceType &dummy, typename TSpaceType::MatrixType const &rM)
Definition: add_strategies_to_python.cpp:123
TSpaceType::IndexType Size(TSpaceType &dummy, typename TSpaceType::VectorType const &rV)
Definition: add_strategies_to_python.cpp:111
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
TExpressionType::data_type norm_2(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression)
Definition: amatrix_interface.h:625
Kratos::PeriodicInterfaceProcess Process operator(std::istream &rIStream, PeriodicInterfaceProcess &rThis)
input stream function
AMatrix::IdentityMatrix< double > IdentityMatrix
Definition: amatrix_interface.h:564
TExpressionType::data_type norm_1(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression)
Definition: amatrix_interface.h:613
AMatrix::MatrixRow< TExpressionType > MatrixRow
Definition: amatrix_interface.h:492
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
TExpression1Type::data_type inner_prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:592
TExpressionType::data_type sum(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression)
Definition: amatrix_interface.h:675
AMatrix::VectorOuterProductExpression< TExpression1Type, TExpression2Type > outer_prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:582
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
AMatrix::MatrixRow< const TExpressionType > row(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression, std::size_t RowIndex)
Definition: amatrix_interface.h:649
AMatrix::MatrixExpression< TExpressionType, AMatrix::row_major_access > vector_expression
Definition: amatrix_interface.h:490
AMatrix::TransposeMatrix< const T > trans(const T &TheMatrix)
Definition: amatrix_interface.h:486
AMatrix::MatrixColumn< const TExpressionType > column(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression, std::size_t ColumnIndex)
Definition: amatrix_interface.h:637
TExpressionType::data_type norm_frobenius(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression)
Definition: amatrix_interface.h:687
AMatrix::SubVector< const TExpressionType > subrange(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression, std::size_t From, std::size_t To)
Definition: amatrix_interface.h:662
c
Definition: generate_weakly_compressible_navier_stokes_element.py:108
data
Definition: mesh_to_mdpa_converter.py:59
int j
Definition: quadrature.py:648
integer i
Definition: TensorModule.f:17