PEXSI
 All Classes Namespaces Files Functions Variables Typedefs Pages
tinyvec.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012 The Regents of the University of California,
3  through Lawrence Berkeley National Laboratory.
4 
5  Authors: Lexing Ying, Mathias Jacquelin and Lin Lin
6 
7  This file is part of PEXSI. All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions are met:
11 
12  (1) Redistributions of source code must retain the above copyright notice, this
13  list of conditions and the following disclaimer.
14  (2) Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17  (3) Neither the name of the University of California, Lawrence Berkeley
18  National Laboratory, U.S. Dept. of Energy nor the names of its contributors may
19  be used to endorse or promote products derived from this software without
20  specific prior written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33  You are under no obligation whatsoever to provide any bug fixes, patches, or
34  upgrades to the features, functionality or performance of the source code
35  ("Enhancements") to anyone; however, if you choose to make your Enhancements
36  available either publicly, or directly to Lawrence Berkeley National
37  Laboratory, without imposing a separate written license agreement for such
38  Enhancements, then you hereby grant the following license: a non-exclusive,
39  royalty-free perpetual license to install, use, modify, prepare derivative
40  works, incorporate into other computer software, distribute, and sublicense
41  such enhancements or derivative works thereof, in binary and source code form.
42 */
46 #ifndef _PEXSI_TINYVEC_HPP_
47 #define _PEXSI_TINYVEC_HPP_
48 
49 #include "environment.hpp"
50 
51 namespace PEXSI{
52 
59  template <class F>
60  class Vec3T {
61  private:
62  F v_[3];
63  public:
64  enum{ X=0, Y=1, Z=2 };
65  //------------CONSTRUCTOR AND DESTRUCTOR
66  Vec3T() { v_[0]=F(0); v_[1]=F(0); v_[2]=F(0); }
67  Vec3T(const F* f) { v_[0]=f[0]; v_[1]=f[1]; v_[2]=f[2]; }
68  Vec3T(const F a, const F b, const F c) { v_[0]=a; v_[1]=b; v_[2]=c; }
69  Vec3T(const Vec3T& c){ v_[0]=c.v_[0]; v_[1]=c.v_[1]; v_[2]=c.v_[2]; }
70  ~Vec3T() {}
71  //------------POINTER and ACCESS
72  operator F*() { return &v_[0]; }
73  operator const F*() const { return &v_[0]; }
74  F* Data() { return &v_[0]; } //access array
75  F& operator()(Int i);
76  const F& operator()(Int i) const;
77  F& operator[](Int i);
78  const F& operator[](Int i) const;
79  //------------ASSIGN
80  Vec3T& operator= ( const Vec3T& c ) { v_[0] =c.v_[0]; v_[1] =c.v_[1]; v_[2] =c.v_[2]; return *this; }
81  Vec3T& operator+=( const Vec3T& c ) { v_[0]+=c.v_[0]; v_[1]+=c.v_[1]; v_[2]+=c.v_[2]; return *this; }
82  Vec3T& operator-=( const Vec3T& c ) { v_[0]-=c.v_[0]; v_[1]-=c.v_[1]; v_[2]-=c.v_[2]; return *this; }
83  Vec3T& operator*=( const F& s ) { v_[0]*=s; v_[1]*=s; v_[2]*=s; return *this; }
84  Vec3T& operator/=( const F& s ) { v_[0]/=s; v_[1]/=s; v_[2]/=s; return *this; }
85  //-----------LENGTH
86  F l1( void ) const { F sum=F(0); for(Int i=0; i<3; i++) sum=sum+std::abs(v_[i]); return sum; }
87  F linfty( void ) const { F cur=F(0); for(Int i=0; i<3; i++) cur=std::max(cur,std::abs(v_[i])); return cur; }
88  F l2( void ) const { F sum=F(0); for(Int i=0; i<3; i++) sum=sum+v_[i]*v_[i]; return sqrt(sum); }
89  };
90 
91  // *********************************************************************
92  // Most commonly used Vec3T thypes
93  // *********************************************************************
94  typedef Vec3T<Real> Point3;
95  typedef Vec3T<Int> Index3;
96 
97  // *********************************************************************
98  // Compare
99  // *********************************************************************
100  template <class F> inline bool operator==(const Vec3T<F>& a, const Vec3T<F>& b) {
101  return (a[0]==b[0] && a[1]==b[1] && a[2]==b[2]);
102  }
103  template <class F> inline bool operator!=(const Vec3T<F>& a, const Vec3T<F>& b) {
104  return !(a==b);
105  }
106  template <class F> inline bool operator> (const Vec3T<F>& a, const Vec3T<F>& b) {
107  for(Int i=0; i<3; i++) {
108  if( a[i]>b[i]) return true;
109  else if(a[i]<b[i]) return false;
110  }
111  return false;
112  }
113  template <class F> inline bool operator< (const Vec3T<F>& a, const Vec3T<F>& b) {
114  for(Int i=0; i<3; i++) {
115  if( a[i]<b[i]) return true;
116  else if(a[i]>b[i]) return false;
117  }
118  return false;
119  }
120  template <class F> inline bool operator>=(const Vec3T<F>& a, const Vec3T<F>& b) {
121  for(Int i=0; i<3; i++) {
122  if( a[i]>b[i]) return true;
123  else if(a[i]<b[i]) return false;
124  }
125  return true;
126  }
127  template <class F> inline bool operator<=(const Vec3T<F>& a, const Vec3T<F>& b) {
128  for(Int i=0; i<3; i++) {
129  if( a[i]<b[i]) return true;
130  else if(a[i]>b[i]) return false;
131  }
132  return true;
133  }
134 
135  // *********************************************************************
136  // Numerical operations
137  // *********************************************************************
138  template <class F> inline Vec3T<F> operator- (const Vec3T<F>& a) {
139  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = -a[i]; return r;
140  }
141  template <class F> inline Vec3T<F> operator+ (const Vec3T<F>& a, const Vec3T<F>& b) {
142  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = a[i]+b[i]; return r;
143  }
144  template <class F> inline Vec3T<F> operator- (const Vec3T<F>& a, const Vec3T<F>& b) {
145  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = a[i]-b[i]; return r;
146  }
147  template <class F> inline Vec3T<F> operator* (F scl, const Vec3T<F>& a) {
148  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = scl*a[i]; return r;
149  }
150  template <class F> inline Vec3T<F> operator* (const Vec3T<F>& a, F scl) {
151  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = scl*a[i]; return r;
152  }
153  template <class F> inline Vec3T<F> operator/ (const Vec3T<F>& a, F scl) {
154  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = a[i]/scl; return r;
155  }
156  template <class F> inline F operator* (const Vec3T<F>& a, const Vec3T<F>& b) {
157  F sum=F(0); for(Int i=0; i<3; i++) sum=sum+a(i)*b(i); return sum;
158  }
159  template <class F> inline F dot (const Vec3T<F>& a, const Vec3T<F>& b) {
160  return a*b;
161  }
162  template <class F> inline Vec3T<F> operator^ (const Vec3T<F>& a, const Vec3T<F>& b) {
163  return Vec3T<F>(a(1)*b(2)-a(2)*b(1), a(2)*b(0)-a(0)*b(2), a(0)*b(1)-a(1)*b(0));
164  }
165  template <class F> inline Vec3T<F> cross (const Vec3T<F>& a, const Vec3T<F>& b) {
166  return a^b;
167  }
168 
169  // *********************************************************************
170  // Element wise numerical operations
171  // *********************************************************************
172  template <class F> inline Vec3T<F> ewmin(const Vec3T<F>& a, const Vec3T<F>& b) {
173  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = std::min(a[i], b[i]); return r;
174  }
175  template <class F> inline Vec3T<F> ewmax(const Vec3T<F>& a, const Vec3T<F>& b) {
176  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = std::max(a[i], b[i]); return r;
177  }
178  template <class F> inline Vec3T<F> ewabs(const Vec3T<F>& a) {
179  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = std::abs(a[i]); return r;
180  }
181  template <class F> inline Vec3T<F> ewmul(const Vec3T<F>&a, const Vec3T<F>& b) {
182  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = a[i]*b[i]; return r;
183  }
184  template <class F> inline Vec3T<F> ewdiv(const Vec3T<F>&a, const Vec3T<F>& b) {
185  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = a[i]/b[i]; return r;
186  }
187  template <class F> inline Vec3T<F> ewrnd(const Vec3T<F>&a) { //round
188  Vec3T<F> r; for(Int i=0; i<3; i++) r[i] = round(a[i]); return r;
189  }
190 
191  // *********************************************************************
192  // Accumulative boolean operations
193  // *********************************************************************
194  template <class F> inline bool allequ(const Vec3T<F>& a, const Vec3T<F>& b) {
195  bool res = true; for(Int i=0; i<3; i++) res = res && (a(i)==b(i)); return res;
196  }
197  template <class F> inline bool allneq(const Vec3T<F>& a, const Vec3T<F>& b) {
198  return !(a==b);
199  }
200  template <class F> inline bool allgtt(const Vec3T<F>& a, const Vec3T<F>& b) {
201  bool res = true; for(Int i=0; i<3; i++) res = res && (a(i)> b(i)); return res;
202  }
203  template <class F> inline bool alllst(const Vec3T<F>& a, const Vec3T<F>& b) {
204  bool res = true; for(Int i=0; i<3; i++) res = res && (a(i)< b(i)); return res;
205  }
206  template <class F> inline bool allgoe(const Vec3T<F>& a, const Vec3T<F>& b) {
207  bool res = true; for(Int i=0; i<3; i++) res = res && (a(i)>=b(i)); return res;
208  }
209  template <class F> inline bool allloe(const Vec3T<F>& a, const Vec3T<F>& b) {
210  bool res = true; for(Int i=0; i<3; i++) res = res && (a(i)<=b(i)); return res;
211  }
212 
213 
214  // *********************************************************************
215  // Input and output
216  // *********************************************************************
217  template <class F> std::istream& operator>>(std::istream& is, Vec3T<F>& a) {
218  for(Int i=0; i<3; i++) is>>a[i]; return is;
219  }
220  template <class F> std::ostream& operator<<(std::ostream& os, const Vec3T<F>& a) {
221  for(Int i=0; i<3; i++) os<<a[i]<<" "; return os;
222  }
223 
224 
225 
226 } // namespace PEXSI
227 
228 #include "tinyvec_impl.hpp"
229 
230 #endif // _PEXSI_TINYVEC_HPP_
Environmental variables.
Tiny vectors of dimension 3.
Definition: tinyvec.hpp:60
Implementation of tiny vectors.