www.cemf.ir
array2D.hpp
Go to the documentation of this file.
1 
2 #ifndef __array2D_hpp__
3 #define __array2D_hpp__
4 
5 #include "iOstream.hpp"
6 
7 namespace pFlow
8 {
9 
10 template<typename T, size_t nRow, size_t nCol>
11 struct array2D
12 {
13 
14  T elements_[nRow][nCol];
15 
16  inline
17  T& operator()(size_t i, size_t j) noexcept
18  {
19  return elements_[i][j];
20  }
21 
22  inline
23  const T& operator()(size_t i, size_t j)const noexcept
24  {
25  return elements_[i][j];
26  }
27 
28  constexpr size_t size()const noexcept
29  {
30  return nRow*nCol;
31  }
32 
33 
34  constexpr size_t nCols()const noexcept
35  {
36  return nCol;
37  }
38 
39  constexpr size_t nRows()const noexcept
40  {
41  return nRow;
42  }
43 
44 };
45 
46 template<typename T, size_t nRow, size_t nCol>
48  const array2D<T, nRow, nCol>& arr1,
49  const array2D<T, nRow, nCol>& arr2)
50 {
52 
53  for(size_t i=0; i<nRow; i++)
54  for(size_t j=0; j<nCol; j++)
55  res(i,j)= arr1(i,j)+arr2(i,j);
56  return res;
57 }
58 
59 template<typename T, size_t nRow, size_t nCol>
61  const array2D<T, nRow, nCol>& arr1,
62  const array2D<T, nRow, nCol>& arr2)
63 {
65 
66  for(size_t i=0; i<nRow; i++)
67  for(size_t j=0; j<nCol; j++)
68  res(i,j)= arr1(i,j)-arr2(i,j);
69  return res;
70 }
71 
72 template<typename T, size_t nRow, size_t nCol>
74  const array2D<T, nRow, nCol>& arr1,
75  const array2D<T, nRow, nCol>& arr2)
76 {
78 
79  for(size_t i=0; i<nRow; i++)
80  for(size_t j=0; j<nCol; j++)
81  res(i,j)= arr1(i,j)*arr2(i,j);
82  return res;
83 }
84 
85 template<typename T, size_t nRow, size_t nCol>
87  const T& s,
88  const array2D<T, nRow, nCol>& arr2)
89 {
91 
92  for(size_t i=0; i<nRow; i++)
93  for(size_t j=0; j<nCol; j++)
94  res(i,j)= s*arr2(i,j);
95  return res;
96 }
97 
98 template<typename T, size_t nRow, size_t nCol>
100  const array2D<T, nRow, nCol>& arr1,
101  const array2D<T, nRow, nCol>& arr2)
102 {
104 
105  for(size_t i=0; i<nRow; i++)
106  for(size_t j=0; j<nCol; j++)
107  res(i,j)= arr1(i,j)/arr2(i,j);
108  return res;
109 }
110 
111 template<typename T, size_t nRow, size_t nCol>
113  const T& val,
114  const array2D<T, nRow, nCol>& arr2)
115 {
117 
118  for(size_t i=0; i<nRow; i++)
119  for(size_t j=0; j<nCol; j++)
120  res(i,j)= val/arr2(i,j);
121  return res;
122 }
123 
124 template <typename T, size_t nRow, size_t nInner, size_t nCol >
125 array2D<T, nRow, nCol>
127  const array2D<T, nRow, nInner>& A,
128  const array2D<T, nInner, nCol>& B)
129 {
131 
132  for (size_t row = 0; row < nRow; row++)
133  {
134  for (size_t col = 0; col < nCol; col++)
135  {
136  T sum = 0;
137  for (size_t inner = 0; inner < nInner; inner++)
138  {
139  sum += A(row,inner) * B(inner,col);
140  }
141  C(row,col) = sum;
142  }
143  }
144 
145  return C;
146 }
147 
148 template<typename T, size_t nRow, size_t nCol>
149 array2D<T, nCol, nRow>
151 {
153 
154  for(size_t i=0; i<nRow; i++)
155  {
156  for(size_t j=0; j<nCol; j++)
157  {
158  tArr(j,i) = arr(i,j);
159  }
160  }
161  return tArr;
162 }
163 
164 template<typename T, size_t nRow, size_t nCol>
166 {
167  os<<'[';
168  for(size_t i=0; i<nRow; i++)
169  {
170  os<<'[';
171  for(size_t j=0; j<nCol-1; j++)
172  {
173  os<<arr(i,j)<<' ';
174  }
175 
176  if(i < nRow-1)
177  os<<arr(i,nCol-1)<<"]\n";
178  else
179  os<<arr(i,nCol-1)<<"]";
180  }
181  os<<']';
182 
183  return os;
184 }
185 
186 }
187 
188 #endif //__array2D_hpp__
pFlow::array2D::nCols
constexpr size_t nCols() const noexcept
Definition: array2D.hpp:34
pFlow::array2D::nRows
constexpr size_t nRows() const noexcept
Definition: array2D.hpp:39
pFlow::array2D::size
constexpr size_t size() const noexcept
Definition: array2D.hpp:28
pFlow::array2D::elements_
T elements_[nRow][nCol]
Definition: array2D.hpp:14
pFlow::transpose
array2D< T, nCol, nRow > transpose(const array2D< T, nRow, nCol > &arr)
Definition: array2D.hpp:150
pFlow::array2D
Definition: array2D.hpp:11
pFlow::operator/
fileSystem operator/(const fileSystem &fs1, const fileSystem &fs2)
Definition: fileSystem.cpp:280
pFlow::operator-
array2D< T, nRow, nCol > operator-(const array2D< T, nRow, nCol > &arr1, const array2D< T, nRow, nCol > &arr2)
Definition: array2D.hpp:60
pFlow
Definition: demGeometry.hpp:27
pFlow::array2D::operator()
const T & operator()(size_t i, size_t j) const noexcept
Definition: array2D.hpp:23
pFlow::MatMul
array2D< T, nRow, nCol > MatMul(const array2D< T, nRow, nInner > &A, const array2D< T, nInner, nCol > &B)
Definition: array2D.hpp:126
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::sum
T sum(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:197
pFlow::operator*
array2D< T, nRow, nCol > operator*(const array2D< T, nRow, nCol > &arr1, const array2D< T, nRow, nCol > &arr2)
Definition: array2D.hpp:73
iOstream.hpp
pFlow::array2D::operator()
T & operator()(size_t i, size_t j) noexcept
Definition: array2D.hpp:17
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::operator+
message operator+(message::EVENT evnt1, message::EVENT evnt2)
Definition: message.hpp:216