Blender V2.61 - r43446

tnt_fortran_array3d_utils.h

Go to the documentation of this file.
00001 
00004 /*
00005 *
00006 * Template Numerical Toolkit (TNT)
00007 *
00008 * Mathematical and Computational Sciences Division
00009 * National Institute of Technology,
00010 * Gaithersburg, MD USA
00011 *
00012 *
00013 * This software was developed at the National Institute of Standards and
00014 * Technology (NIST) by employees of the Federal Government in the course
00015 * of their official duties. Pursuant to title 17 Section 105 of the
00016 * United States Code, this software is not subject to copyright protection
00017 * and is in the public domain. NIST assumes no responsibility whatsoever for
00018 * its use by other parties, and makes no guarantees, expressed or implied,
00019 * about its quality, reliability, or any other characteristic.
00020 *
00021 */
00022 
00023 
00024 #ifndef TNT_FORTRAN_ARRAY3D_UTILS_H
00025 #define TNT_FORTRAN_ARRAY3D_UTILS_H
00026 
00027 #include <cstdlib>
00028 #include <cassert>
00029 
00030 namespace TNT
00031 {
00032 
00033 
00034 template <class T>
00035 std::ostream& operator<<(std::ostream &s, const Fortran_Array3D<T> &A)
00036 {
00037     int M=A.dim1();
00038     int N=A.dim2();
00039     int K=A.dim3();
00040 
00041     s << M << " " << N << " " << K << "\n";
00042 
00043     for (int i=1; i<=M; i++)
00044     {
00045         for (int j=1; j<=N; j++)
00046         {
00047             for (int k=1; k<=K; k++)
00048                 s << A(i,j,k) << " ";
00049             s << "\n";
00050         }
00051         s << "\n";
00052     }
00053 
00054 
00055     return s;
00056 }
00057 
00058 template <class T>
00059 std::istream& operator>>(std::istream &s, Fortran_Array3D<T> &A)
00060 {
00061 
00062     int M, N, K;
00063 
00064     s >> M >> N >> K;
00065 
00066     Fortran_Array3D<T> B(M,N,K);
00067 
00068     for (int i=1; i<=M; i++)
00069         for (int j=1; j<=N; j++)
00070             for (int k=1; k<=K; k++)
00071                 s >>  B(i,j,k);
00072 
00073     A = B;
00074     return s;
00075 }
00076 
00077 
00078 template <class T>
00079 Fortran_Array3D<T> operator+(const Fortran_Array3D<T> &A, const Fortran_Array3D<T> &B)
00080 {
00081     int m = A.dim1();
00082     int n = A.dim2();
00083     int p = A.dim3();
00084 
00085     if (B.dim1() != m ||  B.dim2() != n || B.dim3() != p )
00086         return Fortran_Array3D<T>();
00087 
00088     else
00089     {
00090         Fortran_Array3D<T> C(m,n,p);
00091 
00092         for (int i=1; i<=m; i++)
00093             for (int j=1; j<=n; j++)
00094                 for (int k=1; k<=p; k++)
00095                 C(i,j,k) = A(i,j,k)+ B(i,j,k);
00096 
00097         return C;
00098     }
00099 }
00100 
00101 
00102 template <class T>
00103 Fortran_Array3D<T> operator-(const Fortran_Array3D<T> &A, const Fortran_Array3D<T> &B)
00104 {
00105     int m = A.dim1();
00106     int n = A.dim2();
00107     int p = A.dim3();
00108 
00109     if (B.dim1() != m ||  B.dim2() != n || B.dim3() != p )
00110         return Fortran_Array3D<T>();
00111 
00112     else
00113     {
00114         Fortran_Array3D<T> C(m,n,p);
00115 
00116         for (int i=1; i<=m; i++)
00117             for (int j=1; j<=n; j++)
00118                 for (int k=1; k<=p; k++)
00119                 C(i,j,k) = A(i,j,k)- B(i,j,k);
00120 
00121         return C;
00122     }
00123 }
00124 
00125 
00126 template <class T>
00127 Fortran_Array3D<T> operator*(const Fortran_Array3D<T> &A, const Fortran_Array3D<T> &B)
00128 {
00129     int m = A.dim1();
00130     int n = A.dim2();
00131     int p = A.dim3();
00132 
00133     if (B.dim1() != m ||  B.dim2() != n || B.dim3() != p )
00134         return Fortran_Array3D<T>();
00135 
00136     else
00137     {
00138         Fortran_Array3D<T> C(m,n,p);
00139 
00140         for (int i=1; i<=m; i++)
00141             for (int j=1; j<=n; j++)
00142                 for (int k=1; k<=p; k++)
00143                 C(i,j,k) = A(i,j,k)* B(i,j,k);
00144 
00145         return C;
00146     }
00147 }
00148 
00149 
00150 template <class T>
00151 Fortran_Array3D<T> operator/(const Fortran_Array3D<T> &A, const Fortran_Array3D<T> &B)
00152 {
00153     int m = A.dim1();
00154     int n = A.dim2();
00155     int p = A.dim3();
00156 
00157     if (B.dim1() != m ||  B.dim2() != n || B.dim3() != p )
00158         return Fortran_Array3D<T>();
00159 
00160     else
00161     {
00162         Fortran_Array3D<T> C(m,n,p);
00163 
00164         for (int i=1; i<=m; i++)
00165             for (int j=1; j<=n; j++)
00166                 for (int k=1; k<=p; k++)
00167                 C(i,j,k) = A(i,j,k)/ B(i,j,k);
00168 
00169         return C;
00170     }
00171 }
00172 
00173 
00174 template <class T>
00175 Fortran_Array3D<T>& operator+=(Fortran_Array3D<T> &A, const Fortran_Array3D<T> &B)
00176 {
00177     int m = A.dim1();
00178     int n = A.dim2();
00179     int p = A.dim3();
00180 
00181     if (B.dim1() == m &&  B.dim2() == n && B.dim3() == p )
00182     {
00183         for (int i=1; i<=m; i++)
00184             for (int j=1; j<=n; j++)
00185                 for (int k=1; k<=p; k++)
00186                     A(i,j,k) += B(i,j,k);
00187     }
00188 
00189     return A;
00190 }
00191 
00192 
00193 template <class T>
00194 Fortran_Array3D<T>& operator-=(Fortran_Array3D<T> &A, const Fortran_Array3D<T> &B)
00195 {
00196     int m = A.dim1();
00197     int n = A.dim2();
00198     int p = A.dim3();
00199 
00200     if (B.dim1() == m &&  B.dim2() == n && B.dim3() == p )
00201     {
00202         for (int i=1; i<=m; i++)
00203             for (int j=1; j<=n; j++)
00204                 for (int k=1; k<=p; k++)
00205                     A(i,j,k) -= B(i,j,k);
00206     }
00207 
00208     return A;
00209 }
00210 
00211 
00212 template <class T>
00213 Fortran_Array3D<T>& operator*=(Fortran_Array3D<T> &A, const Fortran_Array3D<T> &B)
00214 {
00215     int m = A.dim1();
00216     int n = A.dim2();
00217     int p = A.dim3();
00218 
00219     if (B.dim1() == m &&  B.dim2() == n && B.dim3() == p )
00220     {
00221         for (int i=1; i<=m; i++)
00222             for (int j=1; j<=n; j++)
00223                 for (int k=1; k<=p; k++)
00224                     A(i,j,k) *= B(i,j,k);
00225     }
00226 
00227     return A;
00228 }
00229 
00230 
00231 template <class T>
00232 Fortran_Array3D<T>& operator/=(Fortran_Array3D<T> &A, const Fortran_Array3D<T> &B)
00233 {
00234     int m = A.dim1();
00235     int n = A.dim2();
00236     int p = A.dim3();
00237 
00238     if (B.dim1() == m &&  B.dim2() == n && B.dim3() == p )
00239     {
00240         for (int i=1; i<=m; i++)
00241             for (int j=1; j<=n; j++)
00242                 for (int k=1; k<=p; k++)
00243                     A(i,j,k) /= B(i,j,k);
00244     }
00245 
00246     return A;
00247 }
00248 
00249 
00250 } // namespace TNT
00251 
00252 #endif