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