Advanced Multi-Physics (AMP)
On-Line Documentation
ArrayHelpers.h
Go to the documentation of this file.
1#ifndef included_AMP_arrayHelpers
2#define included_AMP_arrayHelpers
3
4#include <array>
5#include <cmath>
6#include <ostream>
7
8
9namespace AMP {
10
11
12/****************************************************************
13 * Output array *
14 ****************************************************************/
15template<std::size_t N>
16inline std::ostream &operator<<( std::ostream &out, const std::array<double, N> &x )
17{
18 out << "(" << x[0];
19 for ( size_t i = 1; i < N; i++ )
20 out << "," << x[i];
21 out << ")";
22 return out;
23}
24
25
26/****************************************************************
27 * Scalar-array arithmetic operations *
28 ****************************************************************/
29template<std::size_t N>
30constexpr std::array<double, N> operator*( double a, const std::array<double, N> &b )
31{
32 if constexpr ( N == 1 ) {
33 return { a * b[0] };
34 } else if constexpr ( N == 2 ) {
35 return { a * b[0], a * b[1] };
36 } else if constexpr ( N == 3 ) {
37 return { a * b[0], a * b[1], a * b[2] };
38 } else {
39 auto c = b;
40 for ( size_t i = 0; i < N; i++ )
41 c[i] *= a;
42 return c;
43 }
44}
45template<std::size_t N>
46constexpr std::array<double, N> operator*( const std::array<double, N> &b, double a )
47{
48 if constexpr ( N == 1 ) {
49 return { a * b[0] };
50 } else if constexpr ( N == 2 ) {
51 return { a * b[0], a * b[1] };
52 } else if constexpr ( N == 3 ) {
53 return { a * b[0], a * b[1], a * b[2] };
54 } else {
55 auto c = b;
56 for ( size_t i = 0; i < N; i++ )
57 c[i] *= a;
58 return c;
59 }
60}
61template<std::size_t N>
62constexpr std::array<double, N> operator+( const std::array<double, N> &a, double b )
63{
64 if constexpr ( N == 1 ) {
65 return { a[0] + b };
66 } else if constexpr ( N == 2 ) {
67 return { a[0] + b, a[1] + b };
68 } else if constexpr ( N == 3 ) {
69 return { a[0] + b, a[1] + b, a[2] + b };
70 } else {
71 auto c = a;
72 for ( size_t i = 0; i < N; i++ )
73 c[i] += b;
74 return c;
75 }
76}
77template<std::size_t N>
78constexpr std::array<double, N> operator-( const std::array<double, N> &a, double b )
79{
80 if constexpr ( N == 1 ) {
81 return { a[0] - b };
82 } else if constexpr ( N == 2 ) {
83 return { a[0] - b, a[1] - b };
84 } else if constexpr ( N == 3 ) {
85 return { a[0] - b, a[1] - b, a[2] - b };
86 } else {
87 auto c = a;
88 for ( size_t i = 0; i < N; i++ )
89 c[i] -= b;
90 return c;
91 }
92}
93template<std::size_t N>
94constexpr std::array<double, N> operator-( const std::array<double, N> &a )
95{
96 if constexpr ( N == 1 ) {
97 return { -a[0] };
98 } else if constexpr ( N == 2 ) {
99 return { -a[0], -a[1] };
100 } else if constexpr ( N == 3 ) {
101 return { -a[0], -a[1], -a[2] };
102 } else {
103 std::array<double, N> c = { 0 };
104 for ( size_t i = 0; i < N; i++ )
105 c[i] = -a[i];
106 return c;
107 }
108}
109
110
111/****************************************************************
112 * array-array arithmetic operations *
113 ****************************************************************/
114template<std::size_t N>
115constexpr std::array<double, N> operator+( const std::array<double, N> &x,
116 const std::array<double, N> &y )
117{
118 if constexpr ( N == 1 ) {
119 return { x[0] + y[0] };
120 } else if constexpr ( N == 2 ) {
121 return { x[0] + y[0], x[1] + y[1] };
122 } else if constexpr ( N == 3 ) {
123 return { x[0] + y[0], x[1] + y[1], x[2] + y[2] };
124 } else {
125 auto z = x;
126 for ( size_t i = 0; i < N; i++ )
127 z[i] += y[i];
128 return z;
129 }
130}
131template<std::size_t N>
132constexpr std::array<double, N> operator-( const std::array<double, N> &x,
133 const std::array<double, N> &y )
134{
135 if constexpr ( N == 1 ) {
136 return { x[0] - y[0] };
137 } else if constexpr ( N == 2 ) {
138 return { x[0] - y[0], x[1] - y[1] };
139 } else if constexpr ( N == 3 ) {
140 return { x[0] - y[0], x[1] - y[1], x[2] - y[2] };
141 } else {
142 auto z = x;
143 for ( size_t i = 0; i < N; i++ )
144 z[i] -= y[i];
145 return z;
146 }
147}
148
149
150/****************************************************************
151 * dot/cross products *
152 ****************************************************************/
153template<std::size_t N>
154constexpr double dot( const std::array<double, N> &x, const std::array<double, N> &y )
155{
156 if constexpr ( N == 1 ) {
157 return x[0] * y[0];
158 } else if constexpr ( N == 2 ) {
159 return x[0] * y[0] + x[1] * y[1];
160 } else if constexpr ( N == 3 ) {
161 return x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
162 } else {
163 double d = 0;
164 for ( size_t i = 0; i < N; i++ )
165 d += x[i] * y[i];
166 return d;
167 }
168}
169constexpr double cross( const std::array<double, 2> &x, const std::array<double, 2> &y )
170{
171 return x[0] * y[1] - x[1] * y[0];
172}
173constexpr std::array<double, 3> cross( const std::array<double, 3> &x,
174 const std::array<double, 3> &y )
175{
176 return { x[1] * y[2] - x[2] * y[1], x[2] * y[0] - x[0] * y[2], x[0] * y[1] - x[1] * y[0] };
177}
178template<std::size_t N>
179constexpr double norm( const std::array<double, N> &x )
180{
181 if constexpr ( N == 1 ) {
182 return x[0] * x[0];
183 } else if constexpr ( N == 2 ) {
184 return x[0] * x[0] + x[1] * x[1];
185 } else if constexpr ( N == 3 ) {
186 return x[0] * x[0] + x[1] * x[1] + x[2] * x[2];
187 } else {
188 double n = 0;
189 for ( size_t i = 0; i < N; i++ )
190 n += x[i] * x[i];
191 return n;
192 }
193}
194template<std::size_t N>
195constexpr std::array<double, N> normalize( const std::array<double, N> &x )
196{
197 double tmp = 1.0 / std::sqrt( dot( x, x ) );
198 auto y = x;
199 for ( size_t i = 0; i < N; i++ )
200 y[i] *= tmp;
201 return y;
202}
203
204} // namespace AMP
205
206
207#endif
Array< TYPE, FUN, Allocator > operator-(const Array< TYPE, FUN, Allocator > &a, const Array< TYPE, FUN, Allocator > &b)
Definition Array.h:889
constexpr double cross(const std::array< double, 2 > &x, const std::array< double, 2 > &y)
constexpr std::array< double, N > normalize(const std::array< double, N > &x)
constexpr double dot(const std::array< double, N > &x, const std::array< double, N > &y)
std::ostream & operator<<(std::ostream &out, const AMP::ArraySize &s)
Definition Array.h:861
constexpr double norm(const std::array< double, N > &x)
Array< TYPE, FUN, Allocator > operator*(const Array< TYPE, FUN, Allocator > &a, const Array< TYPE, FUN, Allocator > &b)
Definition Array.h:897
Array< TYPE, FUN, Allocator > operator+(const Array< TYPE, FUN, Allocator > &a, const Array< TYPE, FUN, Allocator > &b)
Definition Array.h:881



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:41.
Comments on this page