1 | /* |
---|
2 | * |
---|
3 | * Template Numerical Toolkit (TNT) |
---|
4 | * |
---|
5 | * Mathematical and Computational Sciences Division |
---|
6 | * National Institute of Technology, |
---|
7 | * Gaithersburg, MD USA |
---|
8 | * |
---|
9 | * |
---|
10 | * This software was developed at the National Institute of Standards and |
---|
11 | * Technology (NIST) by employees of the Federal Government in the course |
---|
12 | * of their official duties. Pursuant to title 17 Section 105 of the |
---|
13 | * United States Code, this software is not subject to copyright protection |
---|
14 | * and is in the public domain. NIST assumes no responsibility whatsoever for |
---|
15 | * its use by other parties, and makes no guarantees, expressed or implied, |
---|
16 | * about its quality, reliability, or any other characteristic. |
---|
17 | * |
---|
18 | */ |
---|
19 | |
---|
20 | |
---|
21 | #ifndef TNT_FORTRAN_ARRAY2D_UTILS_H |
---|
22 | #define TNT_FORTRAN_ARRAY2D_UTILS_H |
---|
23 | |
---|
24 | #include <iostream> |
---|
25 | |
---|
26 | namespace TNT |
---|
27 | { |
---|
28 | |
---|
29 | |
---|
30 | template <class T> |
---|
31 | std::ostream& operator<<(std::ostream &s, const Fortran_Array2D<T> &A) |
---|
32 | { |
---|
33 | int M=A.dim1(); |
---|
34 | int N=A.dim2(); |
---|
35 | |
---|
36 | s << M << " " << N << "\n"; |
---|
37 | |
---|
38 | for (int i=1; i<=M; i++) |
---|
39 | { |
---|
40 | for (int j=1; j<=N; j++) |
---|
41 | { |
---|
42 | s << A(i,j) << " "; |
---|
43 | } |
---|
44 | s << "\n"; |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | return s; |
---|
49 | } |
---|
50 | |
---|
51 | template <class T> |
---|
52 | std::istream& operator>>(std::istream &s, Fortran_Array2D<T> &A) |
---|
53 | { |
---|
54 | |
---|
55 | int M, N; |
---|
56 | |
---|
57 | s >> M >> N; |
---|
58 | |
---|
59 | Fortran_Array2D<T> B(M,N); |
---|
60 | |
---|
61 | for (int i=1; i<=M; i++) |
---|
62 | for (int j=1; j<=N; j++) |
---|
63 | { |
---|
64 | s >> B(i,j); |
---|
65 | } |
---|
66 | |
---|
67 | A = B; |
---|
68 | return s; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | template <class T> |
---|
75 | Fortran_Array2D<T> operator+(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B) |
---|
76 | { |
---|
77 | int m = A.dim1(); |
---|
78 | int n = A.dim2(); |
---|
79 | |
---|
80 | if (B.dim1() != m || B.dim2() != n ) |
---|
81 | return Fortran_Array2D<T>(); |
---|
82 | |
---|
83 | else |
---|
84 | { |
---|
85 | Fortran_Array2D<T> C(m,n); |
---|
86 | |
---|
87 | for (int i=1; i<=m; i++) |
---|
88 | { |
---|
89 | for (int j=1; j<=n; j++) |
---|
90 | C(i,j) = A(i,j) + B(i,j); |
---|
91 | } |
---|
92 | return C; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | template <class T> |
---|
97 | Fortran_Array2D<T> operator-(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B) |
---|
98 | { |
---|
99 | int m = A.dim1(); |
---|
100 | int n = A.dim2(); |
---|
101 | |
---|
102 | if (B.dim1() != m || B.dim2() != n ) |
---|
103 | return Fortran_Array2D<T>(); |
---|
104 | |
---|
105 | else |
---|
106 | { |
---|
107 | Fortran_Array2D<T> C(m,n); |
---|
108 | |
---|
109 | for (int i=1; i<=m; i++) |
---|
110 | { |
---|
111 | for (int j=1; j<=n; j++) |
---|
112 | C(i,j) = A(i,j) - B(i,j); |
---|
113 | } |
---|
114 | return C; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | template <class T> |
---|
120 | Fortran_Array2D<T> operator*(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B) |
---|
121 | { |
---|
122 | int m = A.dim1(); |
---|
123 | int n = A.dim2(); |
---|
124 | |
---|
125 | if (B.dim1() != m || B.dim2() != n ) |
---|
126 | return Fortran_Array2D<T>(); |
---|
127 | |
---|
128 | else |
---|
129 | { |
---|
130 | Fortran_Array2D<T> C(m,n); |
---|
131 | |
---|
132 | for (int i=1; i<=m; i++) |
---|
133 | { |
---|
134 | for (int j=1; j<=n; j++) |
---|
135 | C(i,j) = A(i,j) * B(i,j); |
---|
136 | } |
---|
137 | return C; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | template <class T> |
---|
143 | Fortran_Array2D<T> operator/(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B) |
---|
144 | { |
---|
145 | int m = A.dim1(); |
---|
146 | int n = A.dim2(); |
---|
147 | |
---|
148 | if (B.dim1() != m || B.dim2() != n ) |
---|
149 | return Fortran_Array2D<T>(); |
---|
150 | |
---|
151 | else |
---|
152 | { |
---|
153 | Fortran_Array2D<T> C(m,n); |
---|
154 | |
---|
155 | for (int i=1; i<=m; i++) |
---|
156 | { |
---|
157 | for (int j=1; j<=n; j++) |
---|
158 | C(i,j) = A(i,j) / B(i,j); |
---|
159 | } |
---|
160 | return C; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | template <class T> |
---|
167 | Fortran_Array2D<T>& operator+=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B) |
---|
168 | { |
---|
169 | int m = A.dim1(); |
---|
170 | int n = A.dim2(); |
---|
171 | |
---|
172 | if (B.dim1() == m || B.dim2() == n ) |
---|
173 | { |
---|
174 | for (int i=1; i<=m; i++) |
---|
175 | { |
---|
176 | for (int j=1; j<=n; j++) |
---|
177 | A(i,j) += B(i,j); |
---|
178 | } |
---|
179 | } |
---|
180 | return A; |
---|
181 | } |
---|
182 | |
---|
183 | template <class T> |
---|
184 | Fortran_Array2D<T>& operator-=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B) |
---|
185 | { |
---|
186 | int m = A.dim1(); |
---|
187 | int n = A.dim2(); |
---|
188 | |
---|
189 | if (B.dim1() == m || B.dim2() == n ) |
---|
190 | { |
---|
191 | for (int i=1; i<=m; i++) |
---|
192 | { |
---|
193 | for (int j=1; j<=n; j++) |
---|
194 | A(i,j) -= B(i,j); |
---|
195 | } |
---|
196 | } |
---|
197 | return A; |
---|
198 | } |
---|
199 | |
---|
200 | template <class T> |
---|
201 | Fortran_Array2D<T>& operator*=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B) |
---|
202 | { |
---|
203 | int m = A.dim1(); |
---|
204 | int n = A.dim2(); |
---|
205 | |
---|
206 | if (B.dim1() == m || B.dim2() == n ) |
---|
207 | { |
---|
208 | for (int i=1; i<=m; i++) |
---|
209 | { |
---|
210 | for (int j=1; j<=n; j++) |
---|
211 | A(i,j) *= B(i,j); |
---|
212 | } |
---|
213 | } |
---|
214 | return A; |
---|
215 | } |
---|
216 | |
---|
217 | template <class T> |
---|
218 | Fortran_Array2D<T>& operator/=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B) |
---|
219 | { |
---|
220 | int m = A.dim1(); |
---|
221 | int n = A.dim2(); |
---|
222 | |
---|
223 | if (B.dim1() == m || B.dim2() == n ) |
---|
224 | { |
---|
225 | for (int i=1; i<=m; i++) |
---|
226 | { |
---|
227 | for (int j=1; j<=n; j++) |
---|
228 | A(i,j) /= B(i,j); |
---|
229 | } |
---|
230 | } |
---|
231 | return A; |
---|
232 | } |
---|
233 | |
---|
234 | } // namespace TNT |
---|
235 | |
---|
236 | #endif |
---|