LIBINT  2.1.0-stable
vectorn.h
1 /*
2  * This file is a part of Libint.
3  * Copyright (C) 2004-2014 Edward F. Valeev
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see http://www.gnu.org/licenses/.
17  *
18  */
19 
20 #ifndef _libint2_src_bin_libint_boostutil_h_
21 #define _libint2_src_bin_libint_boostutil_h_
22 
23 #include <boost/tuple/tuple.hpp>
24 
25 namespace libint2 {
26 
28  template <typename T, int N>
29  class VectorN {
30  public:
32  VectorN() { for(int i=0; i<N; ++i) data_[i] = T(); }
33  VectorN(const VectorN& a) { for(int i=0; i<N; ++i) data_[i] = a.data_[i]; }
34 
35  VectorN& operator+=(const VectorN& a) {
36  for(int i=0; i<N; ++i)
37  data_[i] += a.data_[i];
38  return *this;
39  }
40  VectorN& operator-=(const VectorN& a) {
41  for(int i=0; i<N; ++i)
42  data_[i] -= a.data_[i];
43  return *this;
44  }
45 
47  T norm1() const { T result(0); for(int i=0; i<N; ++i) result += abs(data_[i]); return result; }
48 
49  T& operator[](int i) {
50  assert(i>=0 && i<N);
51  return data_[i];
52  }
53  const T& operator[](int i) const {
54  assert(i>=0 && i<N);
55  return data_[i];
56  }
57  private:
58  T data_[N];
59  };
60 
61  template<typename T, int N>
62  VectorN<T,N> operator+(const VectorN<T,N>& a,
63  const VectorN<T,N>& b) {
64  VectorN<T,N> result(a);
65  result += b;
66  return result;
67  }
68  template<typename T, int N>
69  VectorN<T,N> operator-(const VectorN<T,N>& a,
70  const VectorN<T,N>& b) {
71  VectorN<T,N> result(a);
72  result -= b;
73  return result;
74  }
75 
76  template <typename T, int N>
77  inline VectorN<T,N> unit_vector(int i) {
78  assert(i >= 0 && i < N);
79  VectorN<T,N> result;
80  result[i] += 1;
81  return result;
82  }
83 
84  // useful typedefs
85  typedef VectorN<int,3> IntVec3;
86  inline IntVec3 unit_intvec3(int i) {
87  return unit_vector<int,3>(i);
88  }
90  inline bool ltzero(const IntVec3& a) {
91  for(int xyz=0; xyz<3; ++xyz)
92  if (a[xyz] < 0)
93  return true;
94  return false;
95  }
96 
97 }
98 
99 #endif /* header guard */
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
bool ltzero(const IntVec3 &a)
return true if has elements < 0
Definition: vectorn.h:90
T norm1() const
1-norm
Definition: vectorn.h:47
vector of N elements of type T
Definition: vectorn.h:29
VectorN()
Default is vector of zeroes.
Definition: vectorn.h:32