[f2d6445] | 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 | #ifndef TNT_FORTRAN_ARRAY1D_UTILS_H |
---|
| 21 | #define TNT_FORTRAN_ARRAY1D_UTILS_H |
---|
| 22 | |
---|
| 23 | #include <iostream> |
---|
| 24 | |
---|
| 25 | namespace TNT |
---|
| 26 | { |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | Write an array to a character outstream. Output format is one that can |
---|
| 31 | be read back in via the in-stream operator: one integer |
---|
| 32 | denoting the array dimension (n), followed by n elements, |
---|
| 33 | one per line. |
---|
| 34 | |
---|
| 35 | */ |
---|
| 36 | template <class T> |
---|
| 37 | std::ostream& operator<<(std::ostream &s, const Fortran_Array1D<T> &A) |
---|
| 38 | { |
---|
| 39 | int N=A.dim1(); |
---|
| 40 | |
---|
| 41 | s << N << "\n"; |
---|
| 42 | for (int j=1; j<=N; j++) |
---|
| 43 | { |
---|
| 44 | s << A(j) << "\n"; |
---|
| 45 | } |
---|
| 46 | s << "\n"; |
---|
| 47 | |
---|
| 48 | return s; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | Read an array from a character stream. Input format |
---|
| 53 | is one integer, denoting the dimension (n), followed |
---|
| 54 | by n whitespace-separated elments. Newlines are ignored |
---|
| 55 | |
---|
| 56 | <p> |
---|
| 57 | Note: the array being read into references new memory |
---|
| 58 | storage. If the intent is to fill an existing conformant |
---|
| 59 | array, use <code> cin >> B; A.inject(B) ); </code> |
---|
| 60 | instead or read the elements in one-a-time by hand. |
---|
| 61 | |
---|
| 62 | @param s the charater to read from (typically <code>std::in</code>) |
---|
| 63 | @param A the array to read into. |
---|
| 64 | */ |
---|
| 65 | template <class T> |
---|
| 66 | std::istream& operator>>(std::istream &s, Fortran_Array1D<T> &A) |
---|
| 67 | { |
---|
| 68 | int N; |
---|
| 69 | s >> N; |
---|
| 70 | |
---|
| 71 | Fortran_Array1D<T> B(N); |
---|
| 72 | for (int i=1; i<=N; i++) |
---|
| 73 | s >> B(i); |
---|
| 74 | A = B; |
---|
| 75 | return s; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | template <class T> |
---|
| 80 | Fortran_Array1D<T> operator+(const Fortran_Array1D<T> &A, const Fortran_Array1D<T> &B) |
---|
| 81 | { |
---|
| 82 | int n = A.dim1(); |
---|
| 83 | |
---|
| 84 | if (B.dim1() != n ) |
---|
| 85 | return Fortran_Array1D<T>(); |
---|
| 86 | |
---|
| 87 | else |
---|
| 88 | { |
---|
| 89 | Fortran_Array1D<T> C(n); |
---|
| 90 | |
---|
| 91 | for (int i=1; i<=n; i++) |
---|
| 92 | { |
---|
| 93 | C(i) = A(i) + B(i); |
---|
| 94 | } |
---|
| 95 | return C; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | template <class T> |
---|
| 102 | Fortran_Array1D<T> operator-(const Fortran_Array1D<T> &A, const Fortran_Array1D<T> &B) |
---|
| 103 | { |
---|
| 104 | int n = A.dim1(); |
---|
| 105 | |
---|
| 106 | if (B.dim1() != n ) |
---|
| 107 | return Fortran_Array1D<T>(); |
---|
| 108 | |
---|
| 109 | else |
---|
| 110 | { |
---|
| 111 | Fortran_Array1D<T> C(n); |
---|
| 112 | |
---|
| 113 | for (int i=1; i<=n; i++) |
---|
| 114 | { |
---|
| 115 | C(i) = A(i) - B(i); |
---|
| 116 | } |
---|
| 117 | return C; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | template <class T> |
---|
| 123 | Fortran_Array1D<T> operator*(const Fortran_Array1D<T> &A, const Fortran_Array1D<T> &B) |
---|
| 124 | { |
---|
| 125 | int n = A.dim1(); |
---|
| 126 | |
---|
| 127 | if (B.dim1() != n ) |
---|
| 128 | return Fortran_Array1D<T>(); |
---|
| 129 | |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | Fortran_Array1D<T> C(n); |
---|
| 133 | |
---|
| 134 | for (int i=1; i<=n; i++) |
---|
| 135 | { |
---|
| 136 | C(i) = A(i) * B(i); |
---|
| 137 | } |
---|
| 138 | return C; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | template <class T> |
---|
| 144 | Fortran_Array1D<T> operator/(const Fortran_Array1D<T> &A, const Fortran_Array1D<T> &B) |
---|
| 145 | { |
---|
| 146 | int n = A.dim1(); |
---|
| 147 | |
---|
| 148 | if (B.dim1() != n ) |
---|
| 149 | return Fortran_Array1D<T>(); |
---|
| 150 | |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | Fortran_Array1D<T> C(n); |
---|
| 154 | |
---|
| 155 | for (int i=1; i<=n; i++) |
---|
| 156 | { |
---|
| 157 | C(i) = A(i) / B(i); |
---|
| 158 | } |
---|
| 159 | return C; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | template <class T> |
---|
| 172 | Fortran_Array1D<T>& operator+=(Fortran_Array1D<T> &A, const Fortran_Array1D<T> &B) |
---|
| 173 | { |
---|
| 174 | int n = A.dim1(); |
---|
| 175 | |
---|
| 176 | if (B.dim1() == n) |
---|
| 177 | { |
---|
| 178 | for (int i=1; i<=n; i++) |
---|
| 179 | { |
---|
| 180 | A(i) += B(i); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | return A; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | template <class T> |
---|
| 190 | Fortran_Array1D<T>& operator-=(Fortran_Array1D<T> &A, const Fortran_Array1D<T> &B) |
---|
| 191 | { |
---|
| 192 | int n = A.dim1(); |
---|
| 193 | |
---|
| 194 | if (B.dim1() == n) |
---|
| 195 | { |
---|
| 196 | for (int i=1; i<=n; i++) |
---|
| 197 | { |
---|
| 198 | A(i) -= B(i); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | return A; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | template <class T> |
---|
| 207 | Fortran_Array1D<T>& operator*=(Fortran_Array1D<T> &A, const Fortran_Array1D<T> &B) |
---|
| 208 | { |
---|
| 209 | int n = A.dim1(); |
---|
| 210 | |
---|
| 211 | if (B.dim1() == n) |
---|
| 212 | { |
---|
| 213 | for (int i=1; i<=n; i++) |
---|
| 214 | { |
---|
| 215 | A(i) *= B(i); |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | return A; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | |
---|
| 224 | template <class T> |
---|
| 225 | Fortran_Array1D<T>& operator/=(Fortran_Array1D<T> &A, const Fortran_Array1D<T> &B) |
---|
| 226 | { |
---|
| 227 | int n = A.dim1(); |
---|
| 228 | |
---|
| 229 | if (B.dim1() == n) |
---|
| 230 | { |
---|
| 231 | for (int i=1; i<=n; i++) |
---|
| 232 | { |
---|
| 233 | A(i) /= B(i); |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | return A; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | } // namespace TNT |
---|
| 241 | |
---|
| 242 | #endif |
---|