// $Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetLib/src/vect.hh,v 1.20 2003/11/05 16:18:39 schnetter Exp $ #ifndef VECT_HH #define VECT_HH #include #include #include using namespace std; // Forward definition template class vect; // Input/Output template istream& operator>> (istream& is, vect& a); template ostream& operator<< (ostream& os, const vect& a); /** * A short vector with a size that is specified at compile time. */ template class vect { // Fields /** Vector elements. */ T elt[D]; public: // Constructors /** Explicit empty constructor. */ explicit vect () { } /** Copy constructor. */ vect (const vect& a) { for (int d=0; d explicit vect (const vect& a) { for (int d=0; d=0 && d=0 && d vect operator[] (const vect& a) const { vect r; // (*this)[] performs index checking for (int d=0; d=0 && d operator! () const { vect r; for (int d=0; d operator&& (const T x) const { vect r; for (int d=0; d operator|| (const T x) const { vect r; for (int d=0; d operator&& (const vect& a) const { vect r; for (int d=0; d operator|| (const vect& a) const { vect r; for (int d=0; d operator== (const vect& a) const { vect r; for (int d=0; d operator!= (const vect& a) const { vect r; for (int d=0; d operator< (const vect& a) const { vect r; for (int d=0; d operator<= (const vect& a) const { vect r; for (int d=0; d operator> (const vect& a) const { vect r; for (int d=0; da[d]; return r; } vect operator>= (const vect& a) const { vect r; for (int d=0; d=a[d]; return r; } /** This corresponds to the ?: operator. Return a vector with the elements set to either a[i] or b[i], depending on whether (*this)[i] is true or not. */ template vect ifthen (const vect& a, const vect& b) const { vect r; for (int d=0; d inline vect abs (const vect& a) { vect r; for (int d=0; d inline vect max (const vect& a, const vect& b) { vect r; for (int d=0; d inline vect min (const vect& a, const vect& b) { vect r; for (int d=0; d inline bool any (const vect& a) { bool r(false); for (int d=0; d inline bool all (const vect& a) { bool r(true); for (int d=0; d inline int count (const vect& a) { return D; } /** Return the dot product of two vectors. */ template inline T dot (const vect& a, const vect& b) { T r(0); for (int d=0; d inline T hypot (const vect& a) { return sqrt(dot(a,a)); } /** Return the maximum element. */ template inline T maxval (const vect& a) { assert (D>0); T r(a[0]); for (int d=1; d inline T minval (const vect& a) { assert (D>0); T r(a[0]); for (int d=1; d inline int maxloc (const vect& a) { assert (D>0); int r(0); for (int d=1; da[r]) r=d; return r; } /** Return the index of the first minimum element. */ template inline int minloc (const vect& a) { assert (D>0); int r(0); for (int d=1; d inline T prod (const vect& a) { T r(1); for (int d=0; d inline int size (const vect& a) { return D; } /** Return the sum of the elements. */ template inline T sum (const vect& a) { T r(0); for (int d=0; d inline vect map (U (* const func)(T x), const vect& a) { vect r; for (int d=0; d inline vect zip (U (* const func)(S x, T y), const vect& a, const vect& b) { vect r; for (int d=0; d inline U fold (U (* const func)(U val, T x), U val, const vect& a) { for (int d=0; d inline U fold1 (U (* const func)(U val, T x), const vect& a) { assert (D>=1); U val = a[0]; for (int d=1; d inline vect scan0 (U (* const func)(U val, T x), U val, const vect& a) { vect r; for (int d=0; d inline vect scan1 (U (* const func)(U val, T x), U val, const vect& a) { vect r; for (int d=0; d inline istream& operator>> (istream& is, vect& a) { a.input(is); return is; } // Output /** Write a vector formatted to a stream. */ template inline ostream& operator<< (ostream& os, const vect& a) { a.output(os); return os; } #endif // VECT_HH