Blender V2.61 - r43446

tnt_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_ARRAY2D_UTILS_H
00025 #define TNT_ARRAY2D_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 Array2D<T> &A)
00036 {
00037     int M=A.dim1();
00038     int N=A.dim2();
00039 
00040     s << M << " " << N << "\n";
00041 
00042     for (int i=0; i<M; i++)
00043     {
00044         for (int j=0; j<N; j++)
00045         {
00046             s << A[i][j] << " ";
00047         }
00048         s << "\n";
00049     }
00050 
00051 
00052     return s;
00053 }
00054 
00055 template <class T>
00056 std::istream& operator>>(std::istream &s, Array2D<T> &A)
00057 {
00058 
00059     int M, N;
00060 
00061     s >> M >> N;
00062 
00063     Array2D<T> B(M,N);
00064 
00065     for (int i=0; i<M; i++)
00066         for (int j=0; j<N; j++)
00067         {
00068             s >>  B[i][j];
00069         }
00070 
00071     A = B;
00072     return s;
00073 }
00074 
00075 
00076 template <class T>
00077 Array2D<T> operator+(const Array2D<T> &A, const Array2D<T> &B)
00078 {
00079     int m = A.dim1();
00080     int n = A.dim2();
00081 
00082     if (B.dim1() != m ||  B.dim2() != n )
00083         return Array2D<T>();
00084 
00085     else
00086     {
00087         Array2D<T> C(m,n);
00088 
00089         for (int i=0; i<m; i++)
00090         {
00091             for (int j=0; j<n; j++)
00092                 C[i][j] = A[i][j] + B[i][j];
00093         }
00094         return C;
00095     }
00096 }
00097 
00098 template <class T>
00099 Array2D<T> operator-(const Array2D<T> &A, const Array2D<T> &B)
00100 {
00101     int m = A.dim1();
00102     int n = A.dim2();
00103 
00104     if (B.dim1() != m ||  B.dim2() != n )
00105         return Array2D<T>();
00106 
00107     else
00108     {
00109         Array2D<T> C(m,n);
00110 
00111         for (int i=0; i<m; i++)
00112         {
00113             for (int j=0; j<n; j++)
00114                 C[i][j] = A[i][j] - B[i][j];
00115         }
00116         return C;
00117     }
00118 }
00119 
00120 
00121 template <class T>
00122 Array2D<T> operator*(const Array2D<T> &A, const Array2D<T> &B)
00123 {
00124     int m = A.dim1();
00125     int n = A.dim2();
00126 
00127     if (B.dim1() != m ||  B.dim2() != n )
00128         return Array2D<T>();
00129 
00130     else
00131     {
00132         Array2D<T> C(m,n);
00133 
00134         for (int i=0; i<m; i++)
00135         {
00136             for (int j=0; j<n; j++)
00137                 C[i][j] = A[i][j] * B[i][j];
00138         }
00139         return C;
00140     }
00141 }
00142 
00143 
00144 
00145 
00146 template <class T>
00147 Array2D<T> operator/(const Array2D<T> &A, const Array2D<T> &B)
00148 {
00149     int m = A.dim1();
00150     int n = A.dim2();
00151 
00152     if (B.dim1() != m ||  B.dim2() != n )
00153         return Array2D<T>();
00154 
00155     else
00156     {
00157         Array2D<T> C(m,n);
00158 
00159         for (int i=0; i<m; i++)
00160         {
00161             for (int j=0; j<n; j++)
00162                 C[i][j] = A[i][j] / B[i][j];
00163         }
00164         return C;
00165     }
00166 }
00167 
00168 
00169 
00170 
00171 
00172 template <class T>
00173 Array2D<T>&  operator+=(Array2D<T> &A, const Array2D<T> &B)
00174 {
00175     int m = A.dim1();
00176     int n = A.dim2();
00177 
00178     if (B.dim1() == m ||  B.dim2() == n )
00179     {
00180         for (int i=0; i<m; i++)
00181         {
00182             for (int j=0; j<n; j++)
00183                 A[i][j] += B[i][j];
00184         }
00185     }
00186     return A;
00187 }
00188 
00189 
00190 
00191 template <class T>
00192 Array2D<T>&  operator-=(Array2D<T> &A, const Array2D<T> &B)
00193 {
00194     int m = A.dim1();
00195     int n = A.dim2();
00196 
00197     if (B.dim1() == m ||  B.dim2() == n )
00198     {
00199         for (int i=0; i<m; i++)
00200         {
00201             for (int j=0; j<n; j++)
00202                 A[i][j] -= B[i][j];
00203         }
00204     }
00205     return A;
00206 }
00207 
00208 
00209 
00210 template <class T>
00211 Array2D<T>&  operator*=(Array2D<T> &A, const Array2D<T> &B)
00212 {
00213     int m = A.dim1();
00214     int n = A.dim2();
00215 
00216     if (B.dim1() == m ||  B.dim2() == n )
00217     {
00218         for (int i=0; i<m; i++)
00219         {
00220             for (int j=0; j<n; j++)
00221                 A[i][j] *= B[i][j];
00222         }
00223     }
00224     return A;
00225 }
00226 
00227 
00228 
00229 
00230 
00231 template <class T>
00232 Array2D<T>&  operator/=(Array2D<T> &A, const Array2D<T> &B)
00233 {
00234     int m = A.dim1();
00235     int n = A.dim2();
00236 
00237     if (B.dim1() == m ||  B.dim2() == n )
00238     {
00239         for (int i=0; i<m; i++)
00240         {
00241             for (int j=0; j<n; j++)
00242                 A[i][j] /= B[i][j];
00243         }
00244     }
00245     return A;
00246 }
00247 
00261 template <class T>
00262 Array2D<T> matmult(const Array2D<T> &A, const Array2D<T> &B)
00263 {
00264     if (A.dim2() != B.dim1())
00265         return Array2D<T>();
00266 
00267     int M = A.dim1();
00268     int N = A.dim2();
00269     int K = B.dim2();
00270 
00271     Array2D<T> C(M,K);
00272 
00273     for (int i=0; i<M; i++)
00274         for (int j=0; j<K; j++)
00275         {
00276             T sum = 0;
00277 
00278             for (int k=0; k<N; k++)
00279                 sum += A[i][k] * B [k][j];
00280 
00281             C[i][j] = sum;
00282         }
00283 
00284     return C;
00285 
00286 }
00287 
00288 } // namespace TNT
00289 
00290 #endif