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