Vektor.h 7.8 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4
// -*- C++ -*-
/***************************************************************************
 *
 * The IPPL Framework
5
 *
gsell's avatar
gsell committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Visit http://people.web.psi.ch/adelmann/ for more details
 *
 ***************************************************************************/

#ifndef VEKTOR_H
#define VEKTOR_H

// include files
#include "Utility/PAssert.h"
#include "Message/Message.h"
#include "PETE/IpplExpressions.h"
#include "AppTypes/TSVMeta.h"

20
#include <cmath>
gsell's avatar
gsell committed
21
#include <iostream>
22
#include <iomanip>
gsell's avatar
gsell committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

//////////////////////////////////////////////////////////////////////
//
// Definition of class Vektor.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned D>
class Vektor
{
public:

  typedef T Element_t;
  enum { ElemDim = 1 };
  enum { Size = D };

  // Default Constructor initializes to zero.
40
  Vektor() {
gsell's avatar
gsell committed
41 42 43
    TSV_MetaAssignScalar<Vektor<T,D>,T,OpAssign>::apply(*this,T(0));
  }

44
  // Copy Constructor
gsell's avatar
gsell committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  Vektor(const Vektor<T,D> &rhs) {
    TSV_MetaAssign< Vektor<T,D> , Vektor<T,D> ,OpAssign >::apply(*this,rhs);
  }

  // Templated Vektor constructor.
  template<class T1, unsigned D1>
  Vektor(const Vektor<T1,D1> &rhs) {
    for (unsigned d=0; d<D; ++d)
      X[d] = (d < D1) ? rhs[d] : T1(0);
  }

  // Constructor from a single T
  Vektor(const T& x00) {
    TSV_MetaAssignScalar<Vektor<T,D>,T,OpAssign>::apply(*this,x00);
  }

  // Constructors for fixed dimension
  Vektor(const T& x00, const T& x01) {
    PInsist(D==2, "Number of arguments does not match Vektor dimension!!");
    X[0] = x00;
    X[1] = x01;
  }
adelmann's avatar
adelmann committed
67

gsell's avatar
gsell committed
68 69 70 71 72 73 74
  Vektor(const T& x00, const T& x01, const T& x02) {
    PInsist(D==3, "Number of arguments does not match Vektor dimension!!");
    X[0] = x00;
    X[1] = x01;
    X[2] = x02;
  }

adelmann's avatar
adelmann committed
75 76 77 78 79 80 81 82
  Vektor(const T& x00, const T& x01, const T& x02, const T& x03) {
    PInsist(D==4, "Number of arguments does not match Vektor dimension!!");
    X[0] = x00;
    X[1] = x01;
    X[2] = x02;
    X[3] = x03;
  }

83
  // Destructor
gsell's avatar
gsell committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  ~Vektor() { }

  // Assignment Operators
  const Vektor<T,D>& operator=(const Vektor<T,D> &rhs)
  {
    TSV_MetaAssign< Vektor<T,D> , Vektor<T,D> ,OpAssign> :: apply(*this,rhs);
    return *this;
  }
  template<class T1>
  const Vektor<T,D>& operator=(const Vektor<T1,D> &rhs)
  {
    TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> ,OpAssign> :: apply(*this,rhs);
    return *this;
  }
  const Vektor<T,D>& operator=(const T& rhs)
  {
    TSV_MetaAssignScalar< Vektor<T,D> , T ,OpAssign > :: apply(*this,rhs);
    return *this;
  }

  // Accumulation Operators
  template<class T1>
  Vektor<T,D>& operator+=(const Vektor<T1,D> &rhs)
  {
    TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> , OpAddAssign > :: apply(*this,rhs);
    return *this;
  }
  Vektor<T,D>& operator+=(const T& rhs)
  {
    TSV_MetaAssignScalar< Vektor<T,D> , T , OpAddAssign > :: apply(*this,rhs);
    return *this;
  }

  template<class T1>
  Vektor<T,D>& operator-=(const Vektor<T1,D> &rhs)
  {
    TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> , OpSubtractAssign > :: apply(*this,rhs);
    return *this;
  }
  Vektor<T,D>& operator-=(const T& rhs)
  {
    TSV_MetaAssignScalar< Vektor<T,D> , T , OpSubtractAssign > :: apply(*this,rhs);
    return *this;
  }

  template<class T1>
  Vektor<T,D>& operator*=(const Vektor<T1,D> &rhs)
  {
    TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> , OpMultipplyAssign > :: apply(*this,rhs);
    return *this;
  }
  Vektor<T,D>& operator*=(const T& rhs)
  {
    TSV_MetaAssignScalar< Vektor<T,D> , T , OpMultipplyAssign > :: apply(*this,rhs);
    return *this;
  }

  template<class T1>
  Vektor<T,D>& operator/=(const Vektor<T1,D> &rhs)
  {
144
    TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> , OpDivideAssign > ::
gsell's avatar
gsell committed
145 146 147 148 149
      apply(*this,rhs);
    return *this;
  }
  Vektor<T,D>& operator/=(const T& rhs)
  {
150
    TSV_MetaAssignScalar< Vektor<T,D> , T , OpDivideAssign > ::
gsell's avatar
gsell committed
151 152 153 154 155
      apply(*this,rhs);
    return *this;
  }

  // Get and Set Operations
156
  Element_t& operator[](unsigned int i);
157

158
  Element_t operator[](unsigned int i) const;
159

160
  Element_t& operator()(unsigned int i);
161

162
  Element_t operator()( unsigned int i) const;
gsell's avatar
gsell committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

  // Comparison operators.
  bool operator==(const Vektor<T,D>& that) const {
    return TSV_MetaCompareArrays<T,T,D>::apply(X,that.X);
  }
  bool operator!=(const Vektor<T,D>& that) const {
    return !(*this == that);
  }

  //----------------------------------------------------------------------
  // parallel communication
  Message& putMessage(Message& m) const {
    m.setCopy(true);
    ::putMessage(m, X, X + D);
    return m;
  }

  Message& getMessage(Message& m) {
    ::getMessage(m, X, X + D);
    return m;
  }

private:

  // Just store D elements of type T.
  T X[D];

};

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
template<class T, unsigned D>
typename Vektor<T,D>::Element_t& Vektor<T,D>::operator[](unsigned int i)
{
  PAssert (i<D);
  return X[i];
}

template<class T, unsigned D>
typename Vektor<T,D>::Element_t Vektor<T,D>::operator[](unsigned int i) const
{
  PAssert (i<D);
  return X[i];
}

template<class T, unsigned D>
typename Vektor<T,D>::Element_t& Vektor<T,D>::operator()(unsigned int i)
{
  PAssert (i<D);
  return X[i];
}

template<class T, unsigned D>
typename Vektor<T,D>::Element_t Vektor<T,D>::operator()( unsigned int i) const
{
  PAssert (i<D);
  return X[i];
}

gsell's avatar
gsell committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
//////////////////////////////////////////////////////////////////////
//
// Unary Operators
//
//////////////////////////////////////////////////////////////////////

//----------------------------------------------------------------------
// unary operator-
template<class T, unsigned D>
inline Vektor<T,D> operator-(const Vektor<T,D> &op)
{
  return TSV_MetaUnary< Vektor<T,D> , OpUnaryMinus > :: apply(op);
}

//----------------------------------------------------------------------
// unary operator+
template<class T, unsigned D>
inline const Vektor<T,D> &operator+(const Vektor<T,D> &op)
{
  return op;
}

//////////////////////////////////////////////////////////////////////
//
// Binary Operators
//
//////////////////////////////////////////////////////////////////////

//
// Elementwise operators.
//

TSV_ELEMENTWISE_OPERATOR(Vektor,operator+,OpAdd)
TSV_ELEMENTWISE_OPERATOR(Vektor,operator-,OpSubtract)
TSV_ELEMENTWISE_OPERATOR(Vektor,operator*,OpMultipply)
TSV_ELEMENTWISE_OPERATOR(Vektor,operator/,OpDivide)
TSV_ELEMENTWISE_OPERATOR(Vektor,Min,FnMin)
TSV_ELEMENTWISE_OPERATOR(Vektor,Max,FnMax)

//----------------------------------------------------------------------
// dot product
//----------------------------------------------------------------------

template < class T1, class T2, unsigned D >
inline typename PETEBinaryReturn<T1,T2,OpMultipply>::type
265
dot(const Vektor<T1,D> &lhs, const Vektor<T2,D> &rhs)
gsell's avatar
gsell committed
266 267 268 269 270 271 272 273 274 275
{
  return TSV_MetaDot< Vektor<T1,D> , Vektor<T2,D> > :: apply(lhs,rhs);
}

//----------------------------------------------------------------------
// cross product
//----------------------------------------------------------------------

template < class T1, class T2, unsigned D >
inline Vektor<typename PETEBinaryReturn<T1,T2,OpMultipply>::type,D>
276
cross(const Vektor<T1,D> &lhs, const Vektor<T2,D> &rhs)
gsell's avatar
gsell committed
277 278 279 280
{
  return TSV_MetaCross< Vektor<T1,D> , Vektor<T2,D> > :: apply(lhs,rhs);
}

281 282 283 284 285 286 287 288 289 290 291
//----------------------------------------------------------------------
// euclidean norm
//----------------------------------------------------------------------
template < class T, unsigned D >
inline double
euclidean_norm(const Vektor<T, D> &a)
{
  return std::sqrt(dot(a, a));
}


gsell's avatar
gsell committed
292 293 294
//----------------------------------------------------------------------
// I/O
template<class T, unsigned D>
295
inline std::ostream& operator<<(std::ostream& out, const Vektor<T,D>& rhs)
gsell's avatar
gsell committed
296
{
297 298
  std::streamsize sw = out.width();
  out << std::setw(1);
gsell's avatar
gsell committed
299 300 301
  if (D >= 1) {
    out << "( ";
    for (unsigned int i=0; i<D - 1; i++)
302 303
      out << std::setw(sw) << rhs[i] << " , ";
    out << std::setw(sw) << rhs[D - 1] << " )";
gsell's avatar
gsell committed
304
  } else {
305
    out << "( " << std::setw(sw) << rhs[0] << " )";
gsell's avatar
gsell committed
306 307 308 309 310
  }

  return out;
}

311
#endif // VEKTOR_H