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