ExaFMM 1
Fast-multipole Method for exascale systems
include/vec.h
Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2011 by Rio Yokota, Simon Layton, Lorena Barba
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 #ifndef vec_h
00023 #define vec_h
00024 #define for_i for( int i=0; i!=N; ++i )
00025 //! Custom vector type for small vectors
00026 template<int N, typename T>
00027 class vec {
00028 private:
00029   T a[N];
00030 public:
00031   vec(){}                                                          // Default constructor
00032   vec(const T &b) {                                                // Copy constructor (scalar)
00033     for_i a[i] = b;
00034   }
00035   vec(const vec &b) {                                              // Copy constructor (vector)
00036     for_i a[i] = b[i];
00037   }
00038   ~vec(){}                                                         // Destructor
00039   const vec &operator=(const T b) {                                // Scalar assignment
00040     for_i a[i] = b;
00041     return *this;
00042   }
00043   const vec &operator+=(const T b) {                               // Scalar compound assignment (add)
00044     for_i a[i] += b;
00045     return *this;
00046   }
00047   const vec &operator-=(const T b) {                               // Scalar compound assignment (subtract)
00048     for_i a[i] -= b;
00049     return *this;
00050   }
00051   const vec &operator*=(const T b) {                               // Scalar compound assignment (multiply)
00052     for_i a[i] *= b;
00053     return *this;
00054   }
00055   const vec &operator/=(const T b) {                               // Scalar compound assignment (divide)
00056     for_i a[i] /= b;
00057     return *this;
00058   }
00059   const vec &operator=(const vec &b) {                             // Vector assignment
00060     for_i a[i] = b[i];
00061     return *this;
00062   }
00063   const vec &operator+=(const vec &b) {                            // Vector compound assignment (add)
00064     for_i a[i] += b[i];
00065     return *this;
00066   }
00067   const vec &operator-=(const vec &b) {                            // Vector compound assignment (subtract)
00068     for_i a[i] -= b[i];
00069     return *this;
00070   }
00071   const vec &operator*=(const vec &b) {                            // Vector compound assignment (multiply)
00072     for_i a[i] *= b[i];
00073     return *this;
00074   }
00075   const vec &operator/=(const vec &b) {                            // Vector compound assignment (divide)
00076     for_i a[i] /= b[i];
00077     return *this;
00078   }
00079   vec operator+(const vec &b) const {                              // Vector arithmetic (add)
00080     vec c;
00081     for_i c[i] = a[i] + b[i];
00082     return c;
00083   }
00084   vec operator-(const vec &b) const {                              // Vector arithmetic (subtract)
00085     vec c;
00086     for_i c[i] = a[i] - b[i];
00087     return c;
00088   }
00089   vec operator*(const vec &b) const {                              // Vector arithmetic (multiply)
00090     vec c;
00091     for_i c[i] = a[i] * b[i];
00092     return c;
00093   }
00094   vec operator/(const vec &b) const {                              // Vector arithmetic (divide)
00095     vec c;
00096     for_i c[i] = a[i] / b[i];
00097     return c;
00098   }
00099   T &operator[](int i) {                                           // Indexing (lvalue)
00100     return a[i];
00101   }
00102   const T &operator[](int i) const {                               // Indexing (rvalue)
00103     return a[i];
00104   }
00105   operator       T* ()       {return a;}                           // Type-casting (lvalue)
00106   operator const T* () const {return a;}                           // Type-casting (rvalue)
00107   friend std::ostream &operator<<(std::ostream &s, const vec &a) { // Component-wise output stream
00108     for_i s<<a[i]<<' ';
00109     return s;
00110   }
00111   friend T norm(const vec &b) {                                    // L2 norm squared
00112     T c=0;
00113     for_i c+=b[i]*b[i];
00114     return c;
00115   }
00116 };
00117 
00118 #undef for_i
00119 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines