MADNESS  version 0.9
array.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 #ifndef MADNESS_WORLD_ARRAY_H__INCLUDED
36 #define MADNESS_WORLD_ARRAY_H__INCLUDED
37 
38 #include <madness/madness_config.h>
39 #include <madness/world/worldexc.h>
41 #include <madness/world/stdarray.h>
42 #include <vector>
43 #include <algorithm>
44 #include <iostream>
45 
46 namespace madness {
47 
49  template <typename T, std::size_t N>
50  std::ostream& operator<<(std::ostream& s, const std::array<T,N>& a) {
51  s << "[";
52  for(std::size_t i=0; i<N; ++i) {
53  s << a[i];
54  if (i != (N-1)) s << ",";
55  }
56  s << "]";
57  return s;
58  }
59 
61  template <typename T, std::size_t N>
63  // Use this version of range for potential optimization.
64  return madness::hash_range(a.data(), N);
65  }
66 
67  // Serialize std::array objects
68  namespace archive {
69 
70  template <class Archive, class T>
71  struct ArchiveStoreImpl;
72  template <class Archive, class T>
73  struct ArchiveLoadImpl;
74 
75  template <class Archive, typename T, std::size_t N>
76  struct ArchiveStoreImpl<Archive, std::array<T,N> > {
77  static void store(const Archive& ar, const std::array<T,N>& a) {
78  for(typename std::array<T,N>::const_iterator it = a.begin(); it != a.end(); ++it)
79  ar & (*it);
80  }
81  };
82 
83  template <class Archive, typename T, std::size_t N>
84  struct ArchiveLoadImpl<Archive, std::array<T,N> > {
85  static void load(const Archive& ar, std::array<T,N>& a) {
86  for(typename std::array<T,N>::iterator it = a.begin(); it != a.end(); ++it)
87  ar & (*it);
88  }
89  };
90 
91  } // namespace archive
92 
94 
98  template <typename T, std::size_t N>
99  class Vector {
100  public:
102 
103  private:
104  arrayT data_;
105 
106  public:
107  // type defs
108  typedef typename arrayT::value_type value_type;
109  typedef typename arrayT::iterator iterator;
113  typedef typename arrayT::reference reference;
115  typedef typename arrayT::size_type size_type;
117 
119  static const size_type static_size = N;
120 
122  Vector() {}
123 
125  template <typename Q>
126  explicit Vector(Q t) {
127  fill(t);
128  }
129 
131  template <typename Q>
132  explicit Vector(const Q (&t)[N]) {
133  std::copy(t, t + N, data_.begin());
134  }
135 
137  template <typename Q, typename A>
138  explicit Vector(const std::vector<Q, A>& t) {
139  operator=(t);
140  }
141 
142  template <typename Q>
143  explicit Vector(const std::array<Q, N>& t) {
144  data_ = t;
145  }
146 
148  Vector(const Vector<T,N>& other) {
149  data_ = other.data_;
150  }
151 
153  template <typename Q>
154  Vector(const Vector<Q,N>& other) {
155  data_ = other.data_;
156  }
157 
160  data_ = other.data_;
161  return *this;
162  }
163 
165  template <typename Q>
167  data_ = other.data_;
168  return *this;
169  }
170 
172  template <typename Q, typename A>
173  Vector<T,N>& operator=(const std::vector<Q, A>& other) {
174  MADNESS_ASSERT(other.size() >= N);
175  std::copy(other.begin(), other.begin() + N, data_.begin());
176  return *this;
177  }
178 
180  Vector<T,N>& operator=(const T& t) {
181  fill(t);
182  return *this;
183  }
184 
185  // type conversion
186  operator std::array<T,N> () { return data_; }
187 
188  // iterator support
189  iterator begin() { return data_.begin(); }
190  const_iterator begin() const { return data_.begin(); }
191  iterator end() { return data_.end(); }
192  const_iterator end() const { return data_.end(); }
193 
194  // reverse iterator support
195  reverse_iterator rbegin() { return data_.rbegin(); }
196  const_reverse_iterator rbegin() const { return data_.rbegin(); }
197  reverse_iterator rend() { return data_.rend(); }
198  const_reverse_iterator rend() const { return data_.rend(); }
199 
200  // capacity
201  size_type size() const { return data_.size(); }
202  bool empty() const { return data_.empty(); }
203  size_type max_size() const { return data_.max_size(); }
204 
205  // element access
206  reference operator[](size_type i) { return data_[i]; }
207  const_reference operator[](size_type i) const { return data_[i]; }
208  reference at(size_type i) { return data_.at(i); }
209  const_reference at(size_type i) const { return data_.at(i); }
210  reference front() { return data_.front(); }
211  const_reference front() const { return data_.front(); }
212  reference back() { return data_.back(); }
213  const_reference back() const { return data_.back(); }
214  const T* data() const { return data_.data(); }
215  T* c_array() { return data_.data(); }
216 
217  // modifiers
218  void swap(Vector<T, N>& other) { data_.swap(other.data_); }
219  void fill(const T& t) {
220 #ifdef MADNESS_ARRAY_HAS_FILL
221  data_.fill(t);
222  #else
223  data_.assign(t);
224  #endif
225  }
226 
228 
230  template <typename Q>
232  for(size_type i = 0; i < N; ++i)
233  data_[i] *= q;
234  return *this;
235  }
236 
238 
240  template <typename Q>
242  for(size_type i = 0; i < N; ++i)
243  data_[i] += q[i];
244  return *this;
245  }
246 
248 
250  template <typename Q>
252  for(size_type i = 0; i < N; ++i)
253  data_[i] -= q[i];
254  return *this;
255  }
256 
258  T normf() const {
259  T d=0;
260  for (std::size_t i=0; i<N; ++i) d+=(data_[i])*(data_[i]);
261  return sqrt(d);
262  }
263 
265  template <typename Archive>
266  void serialize(Archive& ar) {
267  ar & data_;
268  }
269 
271  hashT hash() const {
272  return hash_value(data_);
273  }
274 
275  // comparisons
276  friend bool operator==(const Vector<T, N>& l, const Vector<T, N>& r) {
277  return l.data_ == r.data_;
278  }
279 
280  friend bool operator!=(const Vector<T, N>& l, const Vector<T, N>& r) {
281  return l.data_ != r.data_;
282  }
283 
284  friend bool operator<(const Vector<T, N>& l, const Vector<T, N>& r) {
285  return l.data_ < r.data_;
286  }
287 
288  friend bool operator>(const Vector<T, N>& l, const Vector<T, N>& r) {
289  return l.data_ > r.data_;
290  }
291 
292  friend bool operator<=(const Vector<T, N>& l, const Vector<T, N>& r) {
293  return l.data_ <= r.data_;
294  }
295 
296  friend bool operator>=(const Vector<T, N>& l, const Vector<T, N>& r) {
297  return l.data_ >= r.data_;
298  }
299 
301  friend std::ostream& operator<<(std::ostream& s, const Vector<T,N>& v) {
302  s << v.data_;
303  return s;
304  }
305  }; // class Vector
306 
307  template <typename T, std::size_t N>
308  void swap(Vector<T,N>& l, Vector<T,N>& r) {
309  l.swap(r);
310  }
311 
312  // Arithmetic operators
313 
314 
316 
325  template <typename T, std::size_t N, typename U>
327  // coordinate passed by value to allow compiler optimization
328  for (std::size_t i = 0; i < N; ++i)
329  l[i] *= r;
330  return l;
331  }
332 
333 
335 
344  template <typename T, typename U, std::size_t N>
346  // coordinate passed by value to allow compiler optimization
347  for (std::size_t i = 0; i < N; ++i)
348  r[i] *= l;
349  return r;
350  }
351 
353 
362  template <typename T, std::size_t N, typename U>
364  // coordinate r passed by value to allow compiler optimization
365  for (std::size_t i = 0; i < N; ++i)
366  l[i] *= r[i];
367  return l;
368  }
369 
371 
379  template <typename T, std::size_t N, typename U>
381  // coordinate passed by value to allow compiler optimization
382  for (std::size_t i = 0; i < N; ++i)
383  l[i] += r;
384  return l;
385  }
386 
388 
397  template <typename T, std::size_t N, typename U>
399  // coordinate r passed by value to allow compiler optimization
400  for (std::size_t i = 0; i < N; ++i)
401  l[i] += r[i];
402  return l;
403  }
404 
406 
414  template <typename T, std::size_t N, typename U>
416  // coordinate passed by value to allow compiler optimization
417  for (std::size_t i = 0; i < N; ++i)
418  l[i] -= r;
419  return l;
420  }
421 
423 
432  template <typename T, std::size_t N, typename U>
434  // coordinate r passed by value to allow compiler optimization
435  for (std::size_t i = 0; i < N; ++i)
436  l[i] -= r[i];
437  return l;
438  }
439 
441 
446  template <typename T, std::size_t N>
448  T norm2 = 0.0;
449  for (std::size_t i = 0; i < N; ++i)
450  norm2 += v[i] * v[i];
451  return sqrt(norm2);
452  }
453 
455  template <typename T>
457  Vector<T,1> r; r[0] = x;
458  return r;
459  }
460 
462  template <typename T>
463  Vector<T,2> vec(T x, T y) {
464  Vector<T,2> r; r[0] = x; r[1] = y;
465  return r;
466  }
467 
469  template <typename T>
470  Vector<T,3> vec(T x, T y, T z) {
471  Vector<T,3> r; r[0] = x; r[1] = y; r[2] = z;
472  return r;
473  }
474 
476  template <typename T>
477  Vector<T,4> vec(T x, T y, T z, T xx) {
478  Vector<T,4> r; r[0] = x; r[1] = y; r[2] = z; r[3] = xx;
479  return r;
480  }
481 
483  template <typename T>
484  Vector<T,5> vec(T x, T y, T z, T xx, T yy) {
485  Vector<T,5> r; r[0] = x; r[1] = y; r[2] = z; r[3] = xx; r[4] = yy;
486  return r;
487  }
488 
490  template <typename T>
491  Vector<T,6> vec(T x, T y, T z, T xx, T yy, T zz) {
492  Vector<T,6> r; r[0] = x; r[1] = y; r[2] = z; r[3] = xx; r[4] = yy; r[5] = zz;
493  return r;
494  }
495 
496 
498  template <typename T, std::size_t N>
499  class Stack {
500  private:
501  std::array<T,N> t;
502  std::size_t n;
503 
504  public:
505  Stack() : n(0) {}
506 
507  void push(const T& value) {
508  MADNESS_ASSERT(n < N);
509  t[n++] = value;
510  }
511 
512  T& pop() {
513  MADNESS_ASSERT(n > 0);
514  return t[--n];
515  }
516 
517  T& front() {
518  MADNESS_ASSERT(n > 0);
519  return t[n-1];
520  }
521 
522  T& top() {
523  return front();
524  }
525 
526  std::size_t size() const {
527  return n;
528  }
529 
530  bool empty() const {
531  return n==0;
532  }
533 
534  void clear() {
535  n = 0;
536  }
537 
538  void reset() {
539  clear();
540  }
541 
542  }; // class Stack
543 
545  template<typename T, std::size_t NDIM>
546  Vector<T,NDIM> n12(const Vector<T,NDIM>& r, const double eps=1.e-6) {
547  const double norm=r.normf();
548  if (norm<1.e-6) return Vector<T,NDIM>(0.0);
549  return r*(1.0/norm);
550  }
551 
552 
554  template <typename T>
555  inline std::array<T,1> array_factory(const T& v0) {
556  std::array<T,1> v;
557  v[0] = v0;
558  return v;
559  }
560 
562  template <typename T>
563  inline std::array<T,2> array_factory(const T& v0, const T& v1) {
564  std::array<T,2> v;
565  v[0] = v0;
566  v[1] = v1;
567  return v;
568  }
569 
571  template <typename T>
572  inline std::array<T,3> array_factory(const T& v0, const T& v1,
573  const T& v2) {
574  std::array<T,3> v;
575  v[0] = v0;
576  v[1] = v1;
577  v[2] = v2;
578  return v;
579  }
580 
582  template <typename T>
583  inline std::array<T,4> array_factory(const T& v0, const T& v1,
584  const T& v2, const T& v3) {
585  std::array<T,4> v;
586  v[0] = v0;
587  v[1] = v1;
588  v[2] = v2;
589  v[3] = v3;
590  return v;
591  }
592 
594  template <typename T>
595  inline std::array<T,5> array_factory(const T& v0, const T& v1,
596  const T& v2, const T& v3,
597  const T& v4) {
598  std::array<T,5> v;
599  v[0] = v0;
600  v[1] = v1;
601  v[2] = v2;
602  v[3] = v3;
603  v[4] = v4;
604  return v;
605  }
606 
608  template <typename T>
609  inline std::array<T,6> array_factory(const T& v0, const T& v1,
610  const T& v2, const T& v3,
611  const T& v4, const T& v5) {
612  std::array<T,6> v;
613  v[0] = v0;
614  v[1] = v1;
615  v[2] = v2;
616  v[3] = v3;
617  v[4] = v4;
618  v[5] = v5;
619  return v;
620  }
621 } // namespace madness
622 
623 #endif // MADNESS_WORLD_ARRAY_H__INCLUDED
void assign(const T &value)
Definition: stdarray_bits.h:83
const_reverse_iterator rend() const
Definition: array.h:198
Vector< T, N > & operator=(const std::vector< Q, A > &other)
Assignment is deep (since a vector is POD)
Definition: array.h:173
Vector(const std::vector< Q, A > &t)
Construct from an STL vector of equal or greater length.
Definition: array.h:138
Function< TENSOR_RESULT_TYPE(L, R), NDIM > operator+(const Function< L, NDIM > &left, const Function< R, NDIM > &right)
Adds two functions with the new result being of type TensorResultType
Definition: mra.h:1759
void clear()
Definition: array.h:534
Vector< T, NDIM > n12(const Vector< T, NDIM > &r, const double eps=1.e-6)
helper function unit vector in direction r
Definition: array.h:546
T norm(Vector< T, N > v)
Compute norm of a Vector.
Definition: array.h:447
void swap(array< T, N > &y)
Definition: stdarray_bits.h:65
std::array< T, N > arrayT
Definition: array.h:101
static size_type max_size()
Definition: stdarray_bits.h:61
reference at(size_type i)
Definition: stdarray_bits.h:49
A simple, fixed dimension Coordinate.
Definition: array.h:99
arrayT::size_type size_type
Definition: array.h:115
std::size_t size() const
Definition: array.h:526
std::reverse_iterator< iterator > reverse_iterator
Definition: stdarray_bits.h:32
Defines hash functions for use in distributed containers.
void swap(Vector< T, N > &other)
Definition: array.h:218
static void store(const Archive &ar, const std::array< T, N > &a)
Definition: array.h:77
void push(const T &value)
Definition: array.h:507
Vector< T, N > & operator=(const T &t)
Fill from scalar value.
Definition: array.h:180
T * c_array()
Definition: array.h:215
Definition: mpreal.h:3066
reference back()
Definition: stdarray_bits.h:55
reference back()
Definition: array.h:212
reverse_iterator rbegin()
Definition: stdarray_bits.h:35
iterator begin()
Definition: array.h:189
Vector(const std::array< Q, N > &t)
Definition: array.h:143
Default store of a thingy via serialize(ar,t)
Definition: archive.h:708
arrayT::difference_type difference_type
Definition: array.h:116
arrayT::iterator iterator
Definition: array.h:109
const_iterator end() const
Definition: array.h:192
Array idential to C++0X arrays.
Definition: stdarray_bits.h:11
const int * const_iterator
Definition: stdarray_bits.h:19
static bool empty()
Definition: stdarray_bits.h:60
bool empty() const
Definition: array.h:530
static const size_type static_size
The size of the vector.
Definition: array.h:119
Vector(const Vector< T, N > &other)
Copy constructor is deep (since a coordinate is POD)
Definition: array.h:148
std::array< T, 1 > array_factory(const T &v0)
Returns a Vector initialized from the arguments.
Definition: array.h:555
const_reference at(size_type i) const
Definition: array.h:209
void fill(const T &t)
Definition: array.h:219
T & top()
Definition: array.h:522
int * iterator
Definition: stdarray_bits.h:18
size_type max_size() const
Definition: array.h:203
friend bool operator!=(const Vector< T, N > &l, const Vector< T, N > &r)
Definition: array.h:280
A simple, fixed-size, stack.
Definition: array.h:499
void reset()
Definition: array.h:538
Vector< T, N > & operator-=(const Vector< Q, N > &q)
In-place element-wise subtraction of another vector.
Definition: array.h:251
const_reference operator[](size_type i) const
Definition: array.h:207
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
std::ptrdiff_t difference_type
Definition: stdarray_bits.h:23
Vector< T, 1 > vec(T x)
Your friendly neighborhood factory function.
Definition: array.h:456
const_iterator begin() const
Definition: array.h:190
reference at(size_type i)
Definition: array.h:208
Function< T, NDIM > copy(const Function< T, NDIM > &f, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool fence=true)
Create a new copy of the function with different distribution and optional fence. ...
Definition: mra.h:1835
Vector< T, N > & operator=(const Vector< Q, N > &other)
Assignment is deep (since a vector is POD)
Definition: array.h:166
static size_type size()
Definition: stdarray_bits.h:59
bool empty() const
Definition: array.h:202
friend bool operator==(const Vector< T, N > &l, const Vector< T, N > &r)
Definition: array.h:276
Stack()
Definition: array.h:505
FLOAT a(int j, FLOAT z)
Definition: y1.cc:86
int & reference
Definition: stdarray_bits.h:20
Default load of a thingy via serialize(ar,t)
Definition: archive.h:718
reference front()
Definition: array.h:210
T & pop()
Definition: array.h:512
T normf() const
return the 2-norm of the vector elements
Definition: array.h:258
void serialize(Archive &ar)
Support for MADNESS serialization.
Definition: array.h:266
void hash_range(hashT &seed, It first, It last)
Combine the hash values of an iterator range.
Definition: worldhash.h:271
Vector< T, N > & operator=(const Vector< T, N > &other)
Assignment is deep (since a vector is POD)
Definition: array.h:159
madness::hashT hash_value(const std::array< T, N > &a)
Hash std::array with madness::Hash.
Definition: array.h:62
const T * data() const
Definition: array.h:214
reverse_iterator rend()
Definition: array.h:197
const int & const_reference
Definition: stdarray_bits.h:21
arrayT::const_iterator const_iterator
Definition: array.h:110
tensorT sqrt(const tensorT &s, double tol=1e-8)
Computes matrix square root (not used any more?)
Definition: DFcode/moldft.cc:446
reference front()
Definition: stdarray_bits.h:53
double norm2(World &world, const std::vector< Function< T, NDIM > > &v)
Computes the 2-norm of a vector of functions.
Definition: vmra.h:325
std::size_t hashT
The hash value type.
Definition: worldhash.h:148
arrayT::const_reference const_reference
Definition: array.h:114
hashT hash() const
Support for MADNESS hashing.
Definition: array.h:271
Vector(Q t)
Initialize all elements to value t.
Definition: array.h:126
Vector()
Default constructor does not initialize vector contents.
Definition: array.h:122
Vector(const Vector< Q, N > &other)
Copy constructor is deep (since a coordinate is POD)
Definition: array.h:154
arrayT::value_type value_type
Definition: array.h:108
iterator begin()
Definition: stdarray_bits.h:26
const T * data() const
Definition: stdarray_bits.h:70
const_reverse_iterator rbegin() const
Definition: array.h:196
const_reference front() const
Definition: array.h:211
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: stdarray_bits.h:33
Vector< T, N > & operator*=(Q q)
In-place element-wise multiplcation by a scalar.
Definition: array.h:231
Vector< T, N > & operator+=(const Vector< Q, N > &q)
In-place element-wise addition of another vector.
Definition: array.h:241
Function< TENSOR_RESULT_TYPE(Q, T), NDIM > operator*(const Function< T, NDIM > &f, const Q alpha)
Returns new function equal to f(x)*alpha.
Definition: mra.h:1553
arrayT::const_reverse_iterator const_reverse_iterator
Definition: array.h:112
friend bool operator>(const Vector< T, N > &l, const Vector< T, N > &r)
Definition: array.h:288
friend bool operator>=(const Vector< T, N > &l, const Vector< T, N > &r)
Definition: array.h:296
reverse_iterator rbegin()
Definition: array.h:195
static void load(const Archive &ar, std::array< T, N > &a)
Definition: array.h:85
reference operator[](size_type i)
Definition: array.h:206
T & front()
Definition: array.h:517
void swap(Vector< T, N > &l, Vector< T, N > &r)
Definition: array.h:308
Function< TENSOR_RESULT_TYPE(L, R), NDIM > operator-(const Function< L, NDIM > &left, const Function< R, NDIM > &right)
Subtracts two functions with the new result being of type TensorResultType
Definition: mra.h:1789
int value_type
Definition: stdarray_bits.h:17
arrayT::reverse_iterator reverse_iterator
Definition: array.h:111
Implements MadnessException.
const_reference back() const
Definition: array.h:213
Vector(const Q(&t)[N])
Construct from a C-style array of the same dimension.
Definition: array.h:132
size_type size() const
Definition: array.h:201
reverse_iterator rend()
Definition: stdarray_bits.h:39
arrayT::reference reference
Definition: array.h:113
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
iterator end()
Definition: array.h:191
std::size_t size_type
Definition: stdarray_bits.h:22
iterator end()
Definition: stdarray_bits.h:28