PSCF v1.3.2
AmIteratorDArray.tpp
1#ifndef PSCF_AM_ITERATOR_DARRAY_TPP
2#define PSCF_AM_ITERATOR_DARRAY_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "NanException.h"
12#include <util/global.h>
13
14namespace Pscf
15{
16
17 using namespace Util;
18
19 /*
20 * Vector assignment, a = b .
21 */
22 template <typename Iterator>
23 void AmIteratorDArray<Iterator>::setEqual(DArray<double>& a,
24 DArray<double> const & b)
25 { a = b; }
26
27 /*
28 * Compute and return the inner product of two vectors
29 */
30 template <typename Iterator>
31 double
32 AmIteratorDArray<Iterator>::dotProduct(DArray<double> const & a,
33 DArray<double> const & b)
34 {
35 const int n = a.capacity();
36 UTIL_CHECK(n == b.capacity());
37 double product = 0.0;
38 for (int i = 0; i < n; i++) {
39 // if either value is NaN, throw NanException
40 if (std::isnan(a[i]) || std::isnan(b[i])) {
41 throw NanException("AmIteratorDArray<Iterator>::dotProduct",
42 __FILE__,__LINE__,0);
43 }
44 product += a[i] * b[i];
45 }
46 return product;
47 }
48
49 /*
50 * Compute and return the maximum magnitude element of a vector.
51 */
52 template <typename Iterator>
53 double AmIteratorDArray<Iterator>::maxAbs(DArray<double> const & a)
54 {
55 const int n = a.capacity();
56 double max = 0.0;
57 double value;
58 for (int i = 0; i < n; i++) {
59 value = a[i];
60 if (std::isnan(value)) { // if value is NaN, throw NanException
61 throw NanException("AmIteratorDArray<Iterator>::dotProduct",
62 __FILE__,__LINE__,0);
63 }
64 if (fabs(value) > max)
65 max = fabs(value);
66 }
67 return max;
68 }
69
70 /*
71 * Compute the vector difference a = b - c .
72 */
73 template <typename Iterator>
74 void AmIteratorDArray<Iterator>::subVV(DArray<double>& a,
75 DArray<double> const & b,
76 DArray<double> const & c)
77 {
78 const int n = a.capacity();
79 UTIL_CHECK(n == b.capacity());
80 UTIL_CHECK(n == c.capacity());
81 for (int i = 0; i < n; i++) {
82 a[i] = b[i] - c[i];
83 }
84 }
85
86 /*
87 * Composite a += b*c for vectors a and b, scalar c .
88 */
89 template <typename Iterator>
90 void AmIteratorDArray<Iterator>::addEqVc(DArray<double>& a,
91 DArray<double> const & b,
92 double c)
93 {
94 const int n = a.capacity();
95 UTIL_CHECK(n == b.capacity());
96 for (int i = 0; i < n; i++) {
97 a[i] += c*b[i];
98 }
99 }
100
101}
102#endif
Exception thrown when not-a-number (NaN) is encountered.
int capacity() const
Return allocated size.
Definition Array.h:159
Dynamically allocatable contiguous array template.
Definition DArray.h:32
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
double max(Array< double > const &in)
Get maximum of array elements .
Definition Reduce.cpp:20
PSCF package top-level namespace.
float product(float a, float b)
Product for float Data.
Definition product.h:22