Blender  V2.59
tnt_fortran_array2d_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_ARRAY2D_UTILS_H
00025 #define TNT_FORTRAN_ARRAY2D_UTILS_H
00026 
00027 #include <iostream>
00028 
00029 namespace TNT
00030 {
00031 
00032 
00033 template <class T>
00034 std::ostream& operator<<(std::ostream &s, const Fortran_Array2D<T> &A)
00035 {
00036     int M=A.dim1();
00037     int N=A.dim2();
00038 
00039     s << M << " " << N << "\n";
00040 
00041     for (int i=1; i<=M; i++)
00042     {
00043         for (int j=1; j<=N; j++)
00044         {
00045             s << A(i,j) << " ";
00046         }
00047         s << "\n";
00048     }
00049 
00050 
00051     return s;
00052 }
00053 
00054 template <class T>
00055 std::istream& operator>>(std::istream &s, Fortran_Array2D<T> &A)
00056 {
00057 
00058     int M, N;
00059 
00060     s >> M >> N;
00061 
00062         Fortran_Array2D<T> B(M,N);
00063 
00064     for (int i=1; i<=M; i++)
00065         for (int j=1; j<=N; j++)
00066         {
00067             s >>  B(i,j);
00068         }
00069 
00070         A = B;
00071     return s;
00072 }
00073 
00074 
00075 
00076 
00077 template <class T>
00078 Fortran_Array2D<T> operator+(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
00079 {
00080         int m = A.dim1();
00081         int n = A.dim2();
00082 
00083         if (B.dim1() != m ||  B.dim2() != n )
00084                 return Fortran_Array2D<T>();
00085 
00086         else
00087         {
00088                 Fortran_Array2D<T> C(m,n);
00089 
00090                 for (int i=1; i<=m; i++)
00091                 {
00092                         for (int j=1; j<=n; j++)
00093                                 C(i,j) = A(i,j) + B(i,j);
00094                 }
00095                 return C;
00096         }
00097 }
00098 
00099 template <class T>
00100 Fortran_Array2D<T> operator-(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
00101 {
00102         int m = A.dim1();
00103         int n = A.dim2();
00104 
00105         if (B.dim1() != m ||  B.dim2() != n )
00106                 return Fortran_Array2D<T>();
00107 
00108         else
00109         {
00110                 Fortran_Array2D<T> C(m,n);
00111 
00112                 for (int i=1; i<=m; i++)
00113                 {
00114                         for (int j=1; j<=n; j++)
00115                                 C(i,j) = A(i,j) - B(i,j);
00116                 }
00117                 return C;
00118         }
00119 }
00120 
00121 
00122 template <class T>
00123 Fortran_Array2D<T> operator*(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
00124 {
00125         int m = A.dim1();
00126         int n = A.dim2();
00127 
00128         if (B.dim1() != m ||  B.dim2() != n )
00129                 return Fortran_Array2D<T>();
00130 
00131         else
00132         {
00133                 Fortran_Array2D<T> C(m,n);
00134 
00135                 for (int i=1; i<=m; i++)
00136                 {
00137                         for (int j=1; j<=n; j++)
00138                                 C(i,j) = A(i,j) * B(i,j);
00139                 }
00140                 return C;
00141         }
00142 }
00143 
00144 
00145 template <class T>
00146 Fortran_Array2D<T> operator/(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
00147 {
00148         int m = A.dim1();
00149         int n = A.dim2();
00150 
00151         if (B.dim1() != m ||  B.dim2() != n )
00152                 return Fortran_Array2D<T>();
00153 
00154         else
00155         {
00156                 Fortran_Array2D<T> C(m,n);
00157 
00158                 for (int i=1; i<=m; i++)
00159                 {
00160                         for (int j=1; j<=n; j++)
00161                                 C(i,j) = A(i,j) / B(i,j);
00162                 }
00163                 return C;
00164         }
00165 }
00166 
00167 
00168 
00169 template <class T>
00170 Fortran_Array2D<T>&  operator+=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
00171 {
00172         int m = A.dim1();
00173         int n = A.dim2();
00174 
00175         if (B.dim1() == m ||  B.dim2() == n )
00176         {
00177                 for (int i=1; i<=m; i++)
00178                 {
00179                         for (int j=1; j<=n; j++)
00180                                 A(i,j) += B(i,j);
00181                 }
00182         }
00183         return A;
00184 }
00185 
00186 template <class T>
00187 Fortran_Array2D<T>&  operator-=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
00188 {
00189         int m = A.dim1();
00190         int n = A.dim2();
00191 
00192         if (B.dim1() == m ||  B.dim2() == n )
00193         {
00194                 for (int i=1; i<=m; i++)
00195                 {
00196                         for (int j=1; j<=n; j++)
00197                                 A(i,j) -= B(i,j);
00198                 }
00199         }
00200         return A;
00201 }
00202 
00203 template <class T>
00204 Fortran_Array2D<T>&  operator*=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
00205 {
00206         int m = A.dim1();
00207         int n = A.dim2();
00208 
00209         if (B.dim1() == m ||  B.dim2() == n )
00210         {
00211                 for (int i=1; i<=m; i++)
00212                 {
00213                         for (int j=1; j<=n; j++)
00214                                 A(i,j) *= B(i,j);
00215                 }
00216         }
00217         return A;
00218 }
00219 
00220 template <class T>
00221 Fortran_Array2D<T>&  operator/=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
00222 {
00223         int m = A.dim1();
00224         int n = A.dim2();
00225 
00226         if (B.dim1() == m ||  B.dim2() == n )
00227         {
00228                 for (int i=1; i<=m; i++)
00229                 {
00230                         for (int j=1; j<=n; j++)
00231                                 A(i,j) /= B(i,j);
00232                 }
00233         }
00234         return A;
00235 }
00236 
00237 } // namespace TNT
00238 
00239 #endif