Essentia  2.1-beta5-dev
tnt_array3d_utils.h
Go to the documentation of this file.
1 #ifndef TNT_ARRAY3D_UTILS_H
2 #define TNT_ARRAY3D_UTILS_H
3 
4 #include <cstdlib>
5 #include <cassert>
6 
7 namespace TNT
8 {
9 
10 
11 template <class T>
12 std::ostream& operator<<(std::ostream &s, const Array3D<T> &A)
13 {
14  int M=A.dim1();
15  int N=A.dim2();
16  int K=A.dim3();
17 
18  s << M << " " << N << " " << K << "\n";
19 
20  for (int i=0; i<M; i++)
21  {
22  for (int j=0; j<N; j++)
23  {
24  for (int k=0; k<K; k++)
25  s << A[i][j][k] << " ";
26  s << "\n";
27  }
28  s << "\n";
29  }
30 
31 
32  return s;
33 }
34 
35 template <class T>
36 std::istream& operator>>(std::istream &s, Array3D<T> &A)
37 {
38 
39  int M, N, K;
40 
41  s >> M >> N >> K;
42 
43  Array3D<T> B(M,N,K);
44 
45  for (int i=0; i<M; i++)
46  for (int j=0; j<N; j++)
47  for (int k=0; k<K; k++)
48  s >> B[i][j][k];
49 
50  A = B;
51  return s;
52 }
53 
54 
55 
56 template <class T>
58 {
59  int m = A.dim1();
60  int n = A.dim2();
61  int p = A.dim3();
62 
63  if (B.dim1() != m || B.dim2() != n || B.dim3() != p )
64  return Array3D<T>();
65 
66  else
67  {
68  Array3D<T> C(m,n,p);
69 
70  for (int i=0; i<m; i++)
71  for (int j=0; j<n; j++)
72  for (int k=0; k<p; k++)
73  C[i][j][k] = A[i][j][k] + B[i][j][k];
74 
75  return C;
76  }
77 }
78 
79 
80 template <class T>
82 {
83  int m = A.dim1();
84  int n = A.dim2();
85  int p = A.dim3();
86 
87  if (B.dim1() != m || B.dim2() != n || B.dim3() != p )
88  return Array3D<T>();
89 
90  else
91  {
92  Array3D<T> C(m,n,p);
93 
94  for (int i=0; i<m; i++)
95  for (int j=0; j<n; j++)
96  for (int k=0; k<p; k++)
97  C[i][j][k] = A[i][j][k] - B[i][j][k];
98 
99  return C;
100  }
101 }
102 
103 
104 
105 
106 template <class T>
108 {
109  int m = A.dim1();
110  int n = A.dim2();
111  int p = A.dim3();
112 
113  if (B.dim1() != m || B.dim2() != n || B.dim3() != p )
114  return Array3D<T>();
115 
116  else
117  {
118  Array3D<T> C(m,n,p);
119 
120  for (int i=0; i<m; i++)
121  for (int j=0; j<n; j++)
122  for (int k=0; k<p; k++)
123  C[i][j][k] = A[i][j][k] * B[i][j][k];
124 
125  return C;
126  }
127 }
128 
129 
130 template <class T>
132 {
133  int m = A.dim1();
134  int n = A.dim2();
135  int p = A.dim3();
136 
137  if (B.dim1() != m || B.dim2() != n || B.dim3() != p )
138  return Array3D<T>();
139 
140  else
141  {
142  Array3D<T> C(m,n,p);
143 
144  for (int i=0; i<m; i++)
145  for (int j=0; j<n; j++)
146  for (int k=0; k<p; k++)
147  C[i][j][k] = A[i][j][k] / B[i][j][k];
148 
149  return C;
150  }
151 }
152 
153 
154 
155 template <class T>
157 {
158  int m = A.dim1();
159  int n = A.dim2();
160  int p = A.dim3();
161 
162  if (B.dim1() == m && B.dim2() == n && B.dim3() == p )
163  {
164  for (int i=0; i<m; i++)
165  for (int j=0; j<n; j++)
166  for (int k=0; k<p; k++)
167  A[i][j][k] += B[i][j][k];
168  }
169 
170  return A;
171 }
172 
173 template <class T>
175 {
176  int m = A.dim1();
177  int n = A.dim2();
178  int p = A.dim3();
179 
180  if (B.dim1() == m && B.dim2() == n && B.dim3() == p )
181  {
182  for (int i=0; i<m; i++)
183  for (int j=0; j<n; j++)
184  for (int k=0; k<p; k++)
185  A[i][j][k] -= B[i][j][k];
186  }
187 
188  return A;
189 }
190 
191 template <class T>
193 {
194  int m = A.dim1();
195  int n = A.dim2();
196  int p = A.dim3();
197 
198  if (B.dim1() == m && B.dim2() == n && B.dim3() == p )
199  {
200  for (int i=0; i<m; i++)
201  for (int j=0; j<n; j++)
202  for (int k=0; k<p; k++)
203  A[i][j][k] *= B[i][j][k];
204  }
205 
206  return A;
207 }
208 
209 
210 template <class T>
212 {
213  int m = A.dim1();
214  int n = A.dim2();
215  int p = A.dim3();
216 
217  if (B.dim1() == m && B.dim2() == n && B.dim3() == p )
218  {
219  for (int i=0; i<m; i++)
220  for (int j=0; j<n; j++)
221  for (int k=0; k<p; k++)
222  A[i][j][k] /= B[i][j][k];
223  }
224 
225  return A;
226 }
227 
228 
229 
230 
231 
232 } // namespace TNT
233 
234 #endif
int dim2() const
Definition: tnt_array3d.h:236
Definition: tnt_array1d.h:35
Array1D< T > operator/(const Array1D< T > &A, const Array1D< T > &B)
Definition: tnt_array1d_utils.h:128
Array1D< T > operator+(const Array1D< T > &A, const Array1D< T > &B)
Definition: tnt_array1d_utils.h:64
Array1D< T > & operator+=(Array1D< T > &A, const Array1D< T > &B)
Definition: tnt_array1d_utils.h:156
Array1D< T > & operator*=(Array1D< T > &A, const Array1D< T > &B)
Definition: tnt_array1d_utils.h:191
int dim1() const
Definition: tnt_array3d.h:233
Array1D< T > & operator-=(Array1D< T > &A, const Array1D< T > &B)
Definition: tnt_array1d_utils.h:174
Array1D< T > & operator/=(Array1D< T > &A, const Array1D< T > &B)
Definition: tnt_array1d_utils.h:209
Array1D< T > operator*(const Array1D< T > &A, const Array1D< T > &B)
Definition: tnt_array1d_utils.h:107
Array1D< T > operator-(const Array1D< T > &A, const Array1D< T > &B)
Definition: tnt_array1d_utils.h:86
std::istream & operator>>(std::istream &s, Array1D< T > &A)
Definition: tnt_array1d_utils.h:49
Definition: tnt_array3d.h:38
int dim3() const
Definition: tnt_array3d.h:239