MADNESS 0.10.1
vector.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#ifndef MADNESS_WORLD_VECTOR_H__INCLUDED
33#define MADNESS_WORLD_VECTOR_H__INCLUDED
34
35/**
36 \file vector.h
37 \brief Implement the \c madness:Vector class, an extension of \c std::array
38 that supports some mathematical operations.
39 \ingroup containers
40*/
41
47#include <vector>
48#include <algorithm>
49#include <iostream>
50#include <math.h>
51
52namespace madness {
53
54 /// A simple, fixed dimension vector.
55
56 /// This class eliminates memory allocation cost, is just POD (it can be
57 /// copied easily and allocated on the stack), and the known
58 /// dimension permits aggressive compiler optimizations.
59 ///
60 /// Provides additional mathematical and I/O operations.
61 /// \tparam T The type of data stored in the vector.
62 /// \tparam N The size of the vector.
63 template <typename T, std::size_t N>
64 class Vector {
65 public:
66 using arrayT = std::array<T,N>; ///< The underlying array type.
67
68 template <typename Q,std::size_t M>
69 friend class Vector;
70
71 private:
72 arrayT data_; ///< The underlying array.
73
74 public:
75 // type defs... these are just wrappers to the underlying array types
76 using value_type = typename arrayT::value_type; ///< The data value type.
77 using iterator = typename arrayT::iterator; ///< Iterator type.
78 using const_iterator = typename arrayT::const_iterator; ///< Const iterator type.
79 using reverse_iterator = typename arrayT::reverse_iterator; ///< Reverse iterator type.
80 using const_reverse_iterator = typename arrayT::const_reverse_iterator; ///< Const reverse iterator type.
81 using reference = typename arrayT::reference; ///< Reference type.
82 using const_reference = typename arrayT::const_reference; ///< Const reference type.
83 using size_type = typename arrayT::size_type; ///< Size type.
84 using difference_type = typename arrayT::difference_type; ///< Difference type.
85
86 /// The size of the \c Vector.
87 static const size_type static_size = N;
88
89 /// Default constructor; does not initialize vector contents.
90 Vector() = default;
91
92 /// Initialize all elements to value \c t.
93
94 /// \tparam Q The type of \c t.
95 /// \param[in] t The value used to initialized the \c Vector.
96 template <typename Q>
97 explicit Vector(Q t) {
98 fill(t);
99 }
100
101 /// Construct from a C-style array of the same dimension.
102
103 /// \tparam Q The type of data in \c t.
104 /// \param[in] t The C-style array.
105 template <typename Q>
106 explicit Vector(const Q (&t)[N]) {
107 std::copy(t, t + N, data_.begin());
108 }
109
110 /// Construct from an STL vector of equal or greater length.
111
112 /// \tparam Q Type of data stored in the \c std::vector.
113 /// \tparam A Allocator type for the \c std::vector.
114 /// \param[in] t The \c std::vector.
115 template <typename Q, typename A>
116 explicit Vector(const std::vector<Q, A>& t) {
117 operator=(t);
118 }
119
120 /// Construct from a \c std::array of equal length.
121
122 /// \tparam Q Type of data stored in the original \c std::array.
123 /// \param[in] t The \c std::array.
124 template <typename Q>
125 explicit Vector(const std::array<Q, N>& t) {
126 data_ = t;
127 }
128
129 /// Copy constructor is deep (because \c Vector is POD).
130
131 /// \param[in] other The \c Vector to copy.
132 Vector(const Vector<T,N>& other) {
133MADNESS_PRAGMA_GCC(diagnostic push)
134MADNESS_PRAGMA_GCC(diagnostic ignored "-Wuninitialized")
135MADNESS_PRAGMA_GCC(diagnostic ignored "-Wmaybe-uninitialized")
136 data_ = other.data_;
137MADNESS_PRAGMA_GCC(diagnostic pop)
138 }
139
140 /// Copy constructor is deep (because \c Vector is POD).
141
142 /// \tparam Q Type of the \c Vector to copy.
143 /// \param[in] other The \c Vector to copy.
144 template <typename Q>
145 Vector(const Vector<Q,N>& other) {
146 data_ = other.data_;
147 }
148
149 /// List initialization constructor (deep copy because \c Vector is POD).
150
151 /// This constructor allows initialization using, e.g.,
152 /// \code
153 /// Vector<double, 3> v{ 1.5, 2.4, -1.9 };
154 /// \endcode
155 /// \throw MadnessException if the list does not contain exactly \c N
156 /// elements.
157 /// \param[in] list The initializer list; elements are copied to the
158 /// \c Vector.
159 Vector(const std::initializer_list<T>& list) :
160 data_()
161 {
162 MADNESS_ASSERT(list.size() == N);
163 std::copy(list.begin(), list.end(), data_.begin());
164 }
165
166 /// Assignment is deep (because a \c Vector is POD).
167
168 /// \param[in] other The \c Vector to copy.
169 /// \return This \c Vector.
171 data_ = other.data_;
172 return *this;
173 }
174
175 /// Assignment is deep (because \c Vector is POD).
176
177 /// \tparam Q The type of the \c Vector to copy.
178 /// \param[in] other The \c Vector to copy.
179 /// \return This \c Vector.
180 template <typename Q>
182 data_ = other.data_;
183 return *this;
184 }
185
186 /// Assignment is deep (because \c Vector is POD).
187
188 /// Make sure the size of \c other is at least \c N.
189 /// \tparam Q The type of data in the \c std::vector.
190 /// \tparam A The allocator type for the \c std::vector.
191 /// \param[in] other The \c std::vector to copy.
192 /// \return This \c Vector.
193 template <typename Q, typename A>
194 Vector<T,N>& operator=(const std::vector<Q, A>& other) {
195 MADNESS_ASSERT(other.size() >= N);
196 std::copy(other.begin(), other.begin() + N, data_.begin());
197 return *this;
198 }
199
200 /// List initialization assignment (deep copy because \c Vector is POD).
201
202 /// This assignment operator allows initialization using, e.g.,
203 /// \code
204 /// v = { 1.5, 2.4, -1.9 };
205 /// \endcode
206 /// \throw MadnessException if the list does not contain exactly \c N
207 /// elements.
208 /// \param[in] list The initializer list; elements are copied to the
209 /// \c Vector.
210 Vector<T,N>& operator=(const std::initializer_list<T>& list) {
211 MADNESS_ASSERT(list.size() == N);
212 std::copy(list.begin(), list.end(), data_.begin());
213 return *this;
214 }
215
216 /// Fill from a scalar value.
217
218 /// \param[in] t The scalar to use for filling.
219 /// \return This \c Vector.
221 fill(t);
222 return *this;
223 }
224
225 /// Type conversion to a \c std::array.
226
227 /// \return The underlying \c std::array.
228 operator std::array<T,N> () { return data_; }
229
230 // iterator support
231 /// Iterator starting at the first element.
232
233 /// \return Iterator to the starting element.
234 iterator begin() { return data_.begin(); }
235
236 /// Const iterator starting at the first element.
237
238 /// \return Const iterator to the starting element.
239 const_iterator begin() const { return data_.begin(); }
240
241 /// Iterator to the end (past the last element).
242
243 /// \return Iterator to the end.
244 iterator end() { return data_.end(); }
245
246 /// Const iterator to the end (past the last element).
247
248 /// \return Const iterator to the end.
249 const_iterator end() const { return data_.end(); }
250
251 // reverse iterator support
252 /// Reverse iterator starting at the last element.
253
254 /// \return Reverse iterator to the last element.
255 reverse_iterator rbegin() { return data_.rbegin(); }
256
257 /// Const reverse iterator starting at the last element.
258
259 /// \return Const reverse iterator to the last element.
260 const_reverse_iterator rbegin() const { return data_.rbegin(); }
261
262 /// Reverse iterator to the beginning (before the first element).
263
264 /// \return Reverse iterator to the beginning.
265 reverse_iterator rend() { return data_.rend(); }
266
267 /// Const reverse iterator to the beginning (before the first element).
268
269 /// \return Const reverse iterator to the beginning.
270 const_reverse_iterator rend() const { return data_.rend(); }
271
272 // capacity
273 /// Accessor for the number of elements in the \c Vector.
274
275 /// \return The number of elements.
276 size_type size() const { return data_.size(); }
277
278 /// Check if the \c Vector is empty.
279
280 /// \return True if the \c Vector is empty; false otherwise. This
281 /// should be false unless `N == 0`.
282 bool empty() const { return data_.empty(); }
283
284 /// Get the maximum size of the \c Vector.
285
286 /// \return The maximum size, \c N.
287 size_type max_size() const { return data_.max_size(); }
288
289 // element access
290 /// Access element \c i of the \c Vector.
291
292 /// Bounds checking is not performed.
293 /// \param[in] i The index.
294 /// \return A reference to element \c i.
296
297 /// Access element \c i of the \c Vector.
298
299 /// Bounds checking is not performed.
300 /// \param[in] i The index.
301 /// \return A const reference to element \c i.
303
304 /// Access element \c i of the \c Vector with bounds checking.
305
306 /// \param[in] i The index.
307 /// \return A reference to element \c i.
308 reference at(size_type i) { return data_.at(i); }
309
310 /// Access element \c i of the \c Vector with bounds checking.
311
312 /// \param[in] i The index.
313 /// \return A const reference to element \c i.
314 const_reference at(size_type i) const { return data_.at(i); }
315
316 /// Access the first element.
317
318 /// \return A reference to the first element.
319 reference front() { return data_.front(); }
320
321 /// Access the first element.
322
323 /// \return A const reference to the first element.
324 const_reference front() const { return data_.front(); }
325
326 /// Access the last element.
327
328 /// \return A reference to the last element.
329 reference back() { return data_.back(); }
330
331 /// Access the last element.
332
333 /// \return A const reference to the last element.
334 const_reference back() const { return data_.back(); }
335
336 /// Direct access to the underlying array.
337
338 /// \return Pointer to the underlying array.
339 T* data() { return data_.data(); }
340
341 /// Direct access to the underlying array.
342
343 /// \return Const pointer to the underlying array.
344 const T* data() const { return data_.data(); }
345
346 // modifiers
347 /// Swap the contents with another \c Vector.
348
349 /// \param[in] other The other vector.
350 void swap(Vector<T, N>& other) { data_.swap(other.data_); }
351
352 /// Fill the \c Vector with the specified value.
353
354 /// \param[in] t The value used to fill the \c Vector.
355 void fill(const T& t) {
356 data_.fill(t);
357 }
358
359 /// In-place, element-wise multiplcation by a scalar.
360
361 /// \tparam Q Type of the scalar.
362 /// \param[in] q The scalar.
363 /// \return A reference to this for chaining operations.
364 /// \todo Do we want a similar division operation?
365 template <typename Q>
367 for(size_type i = 0; i < N; ++i)
368 data_[i] *= q;
369 return *this;
370 }
371
372 /// In-place, element-wise addition of another \c Vector.
373
374 /// \tparam Q Type stored in the other \c Vector.
375 /// \param[in] q The other \c Vector.
376 /// \return A reference to this for chaining operations.
377 template <typename Q>
379 for(size_type i = 0; i < N; ++i)
380 data_[i] += q[i];
381 return *this;
382 }
383
384 /// In-place, element-wise subtraction of another \c Vector.
385
386 /// \tparam Q Type stored in the other \c Vector.
387 /// \param[in] q The other \c Vector.
388 /// \returns A reference to this for chaining operations.
389 template <typename Q>
391 for(size_type i = 0; i < N; ++i)
392 data_[i] -= q[i];
393 return *this;
394 }
395
396 /// Calculate the 2-norm of the vector elements.
397
398 /// \return The 2-norm.
399 /// \todo Is there a reason this is "normf" and not "norm2"?
400 T normf() const {
401 T d = 0.;
402 for(std::size_t i=0; i<N; ++i)
403 d += (data_[i])*(data_[i]);
404 return sqrt(d);
405 }
406
407 /// Support for MADNESS serialization.
408
409 /// \tparam Archive The archive type.
410 /// \param[in,out] ar The archive.
411 template <typename Archive>
412 void serialize(Archive& ar) {
413 ar & data_;
414 }
415
416 /// Support for MADNESS hashing.
417
418 /// \return The hash.
419 hashT hash() const {
420 return hash_value(data_);
421 }
422
423 // comparisons
424 /// Check if each element is equal to its partner in the other \c Vector.
425
426 /// \param[in] l One \c Vector.
427 /// \param[in] r The other \c Vector.
428 /// \return True if each element is equal to its partner; false otherwise.
429 friend bool operator==(const Vector<T, N>& l, const Vector<T, N>& r) {
430 return l.data_ == r.data_;
431 }
432
433 /// Check if any element is not equal to its partner in the other \c Vector.
434
435 /// \param[in] l One \c Vector.
436 /// \param[in] r The other \c Vector.
437 /// \return True if any element is not equal to its partner; false otherwise.
438 friend bool operator!=(const Vector<T, N>& l, const Vector<T, N>& r) {
439 return l.data_ != r.data_;
440 }
441
442 /// Compare \c l and \c r lexicographically.
443
444 /// \param[in] l One \c Vector.
445 /// \param[in] r The other \c Vector.
446 /// \return True if the contents of \c l are lexicographically less than the contents of \c r; false otherwise.
447 friend bool operator<(const Vector<T, N>& l, const Vector<T, N>& r) {
448 return l.data_ < r.data_;
449 }
450
451 /// Compare \c l and \c r lexicographically.
452
453 /// \param[in] l One \c Vector.
454 /// \param[in] r The other \c Vector.
455 /// \return True if the contents of \c l are lexicographically greater than the contents of \c r; false otherwise.
456 friend bool operator>(const Vector<T, N>& l, const Vector<T, N>& r) {
457 return l.data_ > r.data_;
458 }
459
460 /// Compare \c l and \c r lexicographically.
461
462 /// \param[in] l One \c Vector.
463 /// \param[in] r The other \c Vector.
464 /// \return True if the contents of \c l are lexicographically less than or equal to the contents of \c r; false otherwise.
465 friend bool operator<=(const Vector<T, N>& l, const Vector<T, N>& r) {
466 return l.data_ <= r.data_;
467 }
468
469 /// Compare \c l and \c r lexicographically.
470
471 /// \param[in] l One \c Vector.
472 /// \param[in] r The other \c Vector.
473 /// \return True if the contents of \c l are lexicographically greater than or equal to the contents of \c r; false otherwise.
474 friend bool operator>=(const Vector<T, N>& l, const Vector<T, N>& r) {
475 return l.data_ >= r.data_;
476 }
477
478 /// Output a \c Vector to stream.
479
480 /// \param[in] s The output stream.
481 /// \param[in] v The \c Vector to output.
482 /// \return The output stream.
483 friend std::ostream& operator<<(std::ostream& s, const Vector<T,N>& v) {
484 using madness::operators::operator<<;
485 s << v.data_;
486 return s;
487 }
488 }; // class Vector
489
490 /// Swap the contents of two `Vector`s.
491
492 /// \tparam T The type of data stored in the `Vector`s.
493 /// \tparam N The size of the `Vector`s.
494 /// \param[in,out] l One \c Vector.
495 /// \param[in,out] r The other \c Vector.
496 template <typename T, std::size_t N>
498 l.swap(r);
499 }
500
501
502 // Arithmetic operators
503
504 /// Scale a \c Vector.
505
506 /// Multiply each \c Vector element by the scalar value \c r.
507 /// \tparam T The left-hand \c Vector element type.
508 /// \tparam N The \c Vector size.
509 /// \tparam U The right-hand scalar type.
510 /// \param[in] l The left-hand \c Vector.
511 /// \param[in] r The right-hand scalar value to be multiplied by
512 /// the \c Vector elements.
513 /// \return A new \c Vector, \c c, where `c[i]==(l[i]*r)`.
514 template <typename T, std::size_t N, typename U>
516 // coordinate passed by value to allow compiler optimization
517 for (std::size_t i = 0; i < N; ++i)
518 l[i] *= r;
519 return l;
520 }
521
522
523 /// Scale a \c Vector.
524
525 /// Multiply the scalar value \c l by each \c Vector element.
526 /// \tparam T The left-hand scalar type.
527 /// \tparam U The right-hand \c Vector element type.
528 /// \tparam N The \c Vector size.
529 /// \param[in] l The left-hand scalar value to be multiplied by
530 /// the \c Vector elements.
531 /// \param[in] r The right-hand \c Vector.
532 /// \return A new \c Vector, \c c, where `c[i]==(l*r[i])`.
533 template <typename T, typename U, std::size_t N>
535 // coordinate passed by value to allow compiler optimization
536 for (std::size_t i = 0; i < N; ++i)
537 r[i] *= l;
538 return r;
539 }
540
541 /// Multiply (element-wise) two `Vector`s.
542
543 /// Do an element-wise multiplication of \c l and \c r and return the
544 /// result in a new \c Vector.
545 /// \tparam T The left-hand \c Vector element type.
546 /// \tparam N The \c Vector size.
547 /// \tparam U The right-hand \c Vector element type.
548 /// \param[in] l The left-hand \c Vector.
549 /// \param[in] r The right-hand \c Vector.
550 /// \return A new \c Vector, \c c, where `c[i]==(l[i]*r[i])`.
551 template <typename T, std::size_t N, typename U>
553 // coordinate r passed by value to allow compiler optimization
554 for (std::size_t i = 0; i < N; ++i)
555 l[i] *= r[i];
556 return l;
557 }
558
559 /// Add a scalar to a \c Vector.
560
561 /// Add the scalar value \c r to each \c Vector element.
562 /// \tparam T The left-hand \c Vector element type.
563 /// \tparam N The \c Vector size.
564 /// \tparam U The right-hand scalar type.
565 /// \param[in] l The left-hand \c Vector.
566 /// \param[in] r The right-hand scalar value to be added to the \c Vector.
567 /// \return A new \c Vector, \c c, where `c[i]==(l[i]+r)`.
568 template <typename T, std::size_t N, typename U>
570 // coordinate passed by value to allow compiler optimization
571 for (std::size_t i = 0; i < N; ++i)
572 l[i] += r;
573 return l;
574 }
575
576 /// Add (element-wise) two `Vector`s.
577
578 /// Do an element-wise addition of \c l and \c r and return the
579 /// result in a new \c Vector.
580 /// \tparam T The left-hand \c Vector element type.
581 /// \tparam N The \c Vector size.
582 /// \tparam U The right-hand \c Vector element type.
583 /// \param[in] l The left-hand \c Vector.
584 /// \param[in] r The right-hand \c Vector.
585 /// \return A new \c Vector, \c c, where `c[i]==(l[i]+r[i])`.
586 template <typename T, std::size_t N, typename U>
588 // l passed by value to allow compiler optimization
589 for (std::size_t i = 0; i < N; ++i)
590 l[i] += r[i];
591 return l;
592 }
593
594 /// Subtract a scalar from a \c Vector.
595
596 /// Subtract the scalar value \c r from the \c Vector elements `l[i]`.
597 /// \tparam T The left-hand \c Vector element type.
598 /// \tparam N The \c Vector size.
599 /// \tparam U The right-hand scalar type.
600 /// \param[in] l The left-hand \c Vector.
601 /// \param[in] r The right-hand scalar value to be added to the \c Vector.
602 /// \return A new \c Vector, \c c, where `c[i]==(l[i]-r)`.
603 template <typename T, std::size_t N, typename U>
605 // l passed by value to allow compiler optimization
606 for (std::size_t i = 0; i < N; ++i)
607 l[i] -= r;
608 return l;
609 }
610
611 /// Subtract (element-wise) two `Vector`s.
612
613 /// Do an element-wise subtraction of \c l and \c r and return the
614 /// result in a new \c Vector.
615 /// \tparam T The left-hand \c Vector element type.
616 /// \tparam N The \c Vector size.
617 /// \tparam U The right-hand \c Vector element type.
618 /// \param[in] l The left-hand \c Vector.
619 /// \param[in] r The right-hand \c Vector.
620 /// \return A new \c Vector, \c c, where `c[i]==(l[i]-r[i])`.
621 template <typename T, std::size_t N, typename U>
623 // l passed by value to allow compiler optimization
624 for (std::size_t i = 0; i < N; ++i)
625 l[i] -= r[i];
626 return l;
627 }
628
629
630 /// compute the inner product two `Vector`s.
631
632 /// Do an inner product of \c l and \c r and return the
633 /// result in a new \c Vector.
634 /// \tparam T The \c Vector element type.
635 /// \tparam N The \c Vector size.
636 /// \param[in] l The left-hand \c Vector.
637 /// \param[in] r The right-hand \c Vector.
638 /// \return the inner product, where `result==\sum_i(l[i]*r[i])`.
639 template <typename T, std::size_t N>
640 T inner(const Vector<T,N>& l, const Vector<T,N>& r) {
641 T result=0.0;
642 for (std::size_t i = 0; i < N; ++i)
643 result+=l[i]*r[i];
644 return result;
645 }
646
647 /// compute the cross product two `Vector`s of dimension 3
648
649 /// Do a cross product of \c l and \c r and return the result in a new \c Vector.
650 /// \tparam T The \c Vector element type.
651 /// \tparam N The \c Vector size.
652 /// \param[in] l The left-hand \c Vector.
653 /// \param[in] r The right-hand \c Vector.
654 /// \return the cross product
655 template <typename T, std::size_t N>
656 typename std::enable_if<N==3, Vector<T,N> >::type
657 cross(const Vector<T,N>& l, const Vector<T,N>& r) {
658 Vector<T,N> result;
659 result[0]=l[1]*r[2] - r[1]*l[2];
660 result[1]=l[2]*r[0] - r[2]*l[0];
661 result[2]=l[0]*r[1] - r[0]*l[1];
662 return result;
663 }
664
665
666 /// Factory function for creating a \c madness::Vector.
667
668 /// Variadic templates are used to create factories that mimic
669 /// \code
670 /// inline madness::Vector<T, N> vec(T t1, ..., T tN) {
671 /// std::Vector<T, N> ret;
672 /// ret[0] = t1;
673 /// ...
674 /// ret[N-1] = tN;
675 /// return ret;
676 /// }
677 /// \endcode
678 ///
679 /// This function counts the number of arguments passed in through the
680 /// argument pack, creates a \c std::array of the appropriate size,
681 /// forwards the arguments to the `std::array`'s constructor, and passes
682 /// the \c std::array to \c madness::Vector.
683 ///
684 /// \deprecated This function has been replaced by the list-initialization
685 /// constructor and assignment operator. Rather than
686 /// \code
687 /// Vector<double, 3> v = vec(1.4, 2.5, 4.0);
688 /// \endcode
689 /// use
690 /// \code
691 /// Vector<double, 3> v{ 1.4, 2.5, 4.0 };
692 /// \endcode
693 /// or
694 /// \code
695 /// Vector<double, 3> v = { 1.4, 2.5, 4.0 };
696 /// \endcode
697 ///
698 /// \note The first argument is separated from the pack to prevent 0-size
699 /// arrays and also so that the caller doesn't have to explicitly
700 /// specify \c T. It is assumed that all arguments are of type \c T or
701 /// are convertible to type \c T.
702 ///
703 /// \tparam T The data type for the array.
704 /// \tparam Ts The argument pack; that is, the list of arguments. The
705 /// size of the resulting \c Vector is directly determined from the
706 /// size of the argument pack.
707 /// \param[in] t The first argument.
708 /// \param[in] ts The rest of the arguments.
709 /// \return The \c Vector with the arguments put into it.
710 template<typename T, typename... Ts>
711 inline Vector<T, sizeof...(Ts) + 1> vec(T t, Ts... ts) {
712 return Vector<T, sizeof...(Ts) + 1> {
713 std::array<T, sizeof...(Ts) + 1>
714 {{ t, static_cast<T>(ts)... }}
715 };
716 }
717
718
719 /// Construct a unit-`Vector` that has the same direction as \c r.
720
721 /// \tparam T The type of data stored in the \c Vector.
722 /// \tparam N The size of the \c Vector.
723 /// \param[in] r The \c Vector.
724 /// \param[in] eps A precision. If the norm of \c r is less than \c eps,
725 /// the zero vector is returned.
726 /// \return The desired unit-`Vector` (unless \c r is numerically the zero
727 /// \c Vector).
728 template<typename T, std::size_t N>
729 Vector<T,N> unitvec(const Vector<T,N>& r, const double eps=1.e-6) {
730 const double norm=r.normf();
731 if(norm < eps)
732 return Vector<T,N>(0.0);
733 return r * (1.0/norm);
734 }
735
736} // namespace madness
737
738#endif // MADNESS_WORLD_VECTOR_H__INCLUDED
double q(double t)
Definition DKops.h:18
Interface templates for the archives (serialization).
Supplements to the std::array class, such as I/O operations, for convenience.
A simple, fixed dimension vector.
Definition vector.h:64
Vector< T, N > & operator=(const T &t)
Fill from a scalar value.
Definition vector.h:220
friend bool operator!=(const Vector< T, N > &l, const Vector< T, N > &r)
Check if any element is not equal to its partner in the other Vector.
Definition vector.h:438
typename arrayT::reverse_iterator reverse_iterator
Reverse iterator type.
Definition vector.h:79
typename arrayT::iterator iterator
Iterator type.
Definition vector.h:77
typename arrayT::const_reference const_reference
Const reference type.
Definition vector.h:82
bool empty() const
Check if the Vector is empty.
Definition vector.h:282
typename arrayT::value_type value_type
The data value type.
Definition vector.h:76
const_reference front() const
Access the first element.
Definition vector.h:324
Vector< T, N > & operator*=(Q q)
In-place, element-wise multiplcation by a scalar.
Definition vector.h:366
Vector(Q t)
Initialize all elements to value t.
Definition vector.h:97
T normf() const
Calculate the 2-norm of the vector elements.
Definition vector.h:400
const_reference back() const
Access the last element.
Definition vector.h:334
friend bool operator<=(const Vector< T, N > &l, const Vector< T, N > &r)
Compare l and r lexicographically.
Definition vector.h:465
Vector< T, N > & operator=(const std::initializer_list< T > &list)
List initialization assignment (deep copy because Vector is POD).
Definition vector.h:210
reverse_iterator rbegin()
Reverse iterator starting at the last element.
Definition vector.h:255
friend bool operator<(const Vector< T, N > &l, const Vector< T, N > &r)
Compare l and r lexicographically.
Definition vector.h:447
Vector(const std::initializer_list< T > &list)
List initialization constructor (deep copy because Vector is POD).
Definition vector.h:159
iterator begin()
Iterator starting at the first element.
Definition vector.h:234
const_reference at(size_type i) const
Access element i of the Vector with bounds checking.
Definition vector.h:314
Vector< T, N > & operator=(const Vector< T, N > &other)
Assignment is deep (because a Vector is POD).
Definition vector.h:170
Vector(const Q(&t)[N])
Construct from a C-style array of the same dimension.
Definition vector.h:106
Vector(const std::vector< Q, A > &t)
Construct from an STL vector of equal or greater length.
Definition vector.h:116
iterator end()
Iterator to the end (past the last element).
Definition vector.h:244
void fill(const T &t)
Fill the Vector with the specified value.
Definition vector.h:355
reference at(size_type i)
Access element i of the Vector with bounds checking.
Definition vector.h:308
reference front()
Access the first element.
Definition vector.h:319
reverse_iterator rend()
Reverse iterator to the beginning (before the first element).
Definition vector.h:265
Vector(const Vector< T, N > &other)
Copy constructor is deep (because Vector is POD).
Definition vector.h:132
const_reference operator[](size_type i) const
Access element i of the Vector.
Definition vector.h:302
Vector< T, N > & operator=(const Vector< Q, N > &other)
Assignment is deep (because Vector is POD).
Definition vector.h:181
const_iterator begin() const
Const iterator starting at the first element.
Definition vector.h:239
size_type size() const
Accessor for the number of elements in the Vector.
Definition vector.h:276
void swap(Vector< T, N > &other)
Swap the contents with another Vector.
Definition vector.h:350
size_type max_size() const
Get the maximum size of the Vector.
Definition vector.h:287
typename arrayT::const_reverse_iterator const_reverse_iterator
Const reverse iterator type.
Definition vector.h:80
Vector< T, N > & operator-=(const Vector< Q, N > &q)
In-place, element-wise subtraction of another Vector.
Definition vector.h:390
void serialize(Archive &ar)
Support for MADNESS serialization.
Definition vector.h:412
const_iterator end() const
Const iterator to the end (past the last element).
Definition vector.h:249
Vector()=default
Default constructor; does not initialize vector contents.
friend bool operator>(const Vector< T, N > &l, const Vector< T, N > &r)
Compare l and r lexicographically.
Definition vector.h:456
friend bool operator==(const Vector< T, N > &l, const Vector< T, N > &r)
Check if each element is equal to its partner in the other Vector.
Definition vector.h:429
const_reverse_iterator rbegin() const
Const reverse iterator starting at the last element.
Definition vector.h:260
typename arrayT::const_iterator const_iterator
Const iterator type.
Definition vector.h:78
T * data()
Direct access to the underlying array.
Definition vector.h:339
Vector< T, N > & operator=(const std::vector< Q, A > &other)
Assignment is deep (because Vector is POD).
Definition vector.h:194
Vector(const Vector< Q, N > &other)
Copy constructor is deep (because Vector is POD).
Definition vector.h:145
hashT hash() const
Support for MADNESS hashing.
Definition vector.h:419
typename arrayT::difference_type difference_type
Difference type.
Definition vector.h:84
typename arrayT::reference reference
Reference type.
Definition vector.h:81
friend std::ostream & operator<<(std::ostream &s, const Vector< T, N > &v)
Output a Vector to stream.
Definition vector.h:483
reference back()
Access the last element.
Definition vector.h:329
reference operator[](size_type i)
Access element i of the Vector.
Definition vector.h:295
static const size_type static_size
The size of the Vector.
Definition vector.h:87
arrayT data_
The underlying array.
Definition vector.h:72
const_reverse_iterator rend() const
Const reverse iterator to the beginning (before the first element).
Definition vector.h:270
std::array< T, N > arrayT
The underlying array type.
Definition vector.h:66
Vector(const std::array< Q, N > &t)
Construct from a std::array of equal length.
Definition vector.h:125
Vector< T, N > & operator+=(const Vector< Q, N > &q)
In-place, element-wise addition of another Vector.
Definition vector.h:378
friend bool operator>=(const Vector< T, N > &l, const Vector< T, N > &r)
Compare l and r lexicographically.
Definition vector.h:474
const T * data() const
Direct access to the underlying array.
Definition vector.h:344
typename arrayT::size_type size_type
Size type.
Definition vector.h:83
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
static const double v
Definition hatom_sf_dirac.cc:20
Macros and tools pertaining to the configuration of MADNESS.
#define MADNESS_PRAGMA_GCC(x)
Definition madness_config.h:205
Defines madness::MadnessException for exception handling.
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
std::vector< Function< TENSOR_RESULT_TYPE(T, R), NDIM > > cross(const std::vector< Function< T, NDIM > > &f, const std::vector< Function< R, NDIM > > &g, bool do_refine=false, bool fence=true)
shorthand cross operator
Definition vmra.h:2070
Vector< T, N > unitvec(const Vector< T, N > &r, const double eps=1.e-6)
Construct a unit-Vector that has the same direction as r.
Definition vector.h:729
std::vector< CCPairFunction< T, NDIM > > operator*(const double fac, const std::vector< CCPairFunction< T, NDIM > > &arg)
Definition ccpairfunction.h:1084
std::vector< CCPairFunction< T, NDIM > > operator-(const std::vector< CCPairFunction< T, NDIM > > c1, const std::vector< CCPairFunction< T, NDIM > > &c2)
Definition ccpairfunction.h:1055
static double pop(std::vector< double > &v)
Definition SCF.cc:113
std::size_t hashT
The hash value type.
Definition worldhash.h:145
double inner(response_space &a, response_space &b)
Definition response_functions.h:442
std::string type(const PairType &n)
Definition PNOParameters.h:18
std::vector< CCPairFunction< T, NDIM > > operator+(const std::vector< CCPairFunction< T, NDIM > > c1, const std::vector< CCPairFunction< T, NDIM > > &c2)
Definition ccpairfunction.h:1047
madness::hashT hash_value(const std::array< T, N > &a)
Hash std::array with madness hash.
Definition array_addons.h:78
void swap(Vector< T, N > &l, Vector< T, N > &r)
Swap the contents of two Vectors.
Definition vector.h:497
Vector< T, sizeof...(Ts)+1 > vec(T t, Ts... ts)
Factory function for creating a madness::Vector.
Definition vector.h:711
static const double d
Definition nonlinschro.cc:121
double Q(double a)
Definition relops.cc:20
double norm(const T i1)
Definition test_cloud.cc:72
#define N
Definition testconv.cc:37
Defines hash functions for use in distributed containers.