1#ifndef included_AMP_arrayHelpers
2#define included_AMP_arrayHelpers
15template<std::
size_t N>
16inline std::ostream &
operator<<( std::ostream &out,
const std::array<double, N> &x )
19 for (
size_t i = 1; i < N; i++ )
29template<std::
size_t N>
30constexpr std::array<double, N>
operator*(
double a,
const std::array<double, N> &b )
32 if constexpr ( N == 1 ) {
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] };
40 for (
size_t i = 0; i < N; i++ )
45template<std::
size_t N>
46constexpr std::array<double, N>
operator*(
const std::array<double, N> &b,
double a )
48 if constexpr ( N == 1 ) {
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] };
56 for (
size_t i = 0; i < N; i++ )
61template<std::
size_t N>
62constexpr std::array<double, N>
operator+(
const std::array<double, N> &a,
double b )
64 if constexpr ( N == 1 ) {
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 };
72 for (
size_t i = 0; i < N; i++ )
77template<std::
size_t N>
78constexpr std::array<double, N>
operator-(
const std::array<double, N> &a,
double b )
80 if constexpr ( N == 1 ) {
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 };
88 for (
size_t i = 0; i < N; i++ )
93template<std::
size_t N>
94constexpr std::array<double, N>
operator-(
const std::array<double, N> &a )
96 if constexpr ( N == 1 ) {
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] };
103 std::array<double, N> c = { 0 };
104 for (
size_t i = 0; i < N; i++ )
114template<std::
size_t N>
115constexpr std::array<double, N>
operator+(
const std::array<double, N> &x,
116 const std::array<double, N> &y )
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] };
126 for (
size_t i = 0; i < N; i++ )
131template<std::
size_t N>
132constexpr std::array<double, N>
operator-(
const std::array<double, N> &x,
133 const std::array<double, N> &y )
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] };
143 for (
size_t i = 0; i < N; i++ )
153template<std::
size_t N>
154constexpr double dot(
const std::array<double, N> &x,
const std::array<double, N> &y )
156 if constexpr ( N == 1 ) {
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];
164 for (
size_t i = 0; i < N; i++ )
169constexpr double cross(
const std::array<double, 2> &x,
const std::array<double, 2> &y )
171 return x[0] * y[1] - x[1] * y[0];
173constexpr std::array<double, 3>
cross(
const std::array<double, 3> &x,
174 const std::array<double, 3> &y )
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] };
178template<std::
size_t N>
179constexpr double norm(
const std::array<double, N> &x )
181 if constexpr ( N == 1 ) {
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];
189 for (
size_t i = 0; i < N; i++ )
194template<std::
size_t N>
195constexpr std::array<double, N>
normalize(
const std::array<double, N> &x )
197 double tmp = 1.0 / std::sqrt(
dot( x, x ) );
199 for (
size_t i = 0; i < N; i++ )
Array< TYPE, FUN, Allocator > operator-(const Array< TYPE, FUN, Allocator > &a, const Array< TYPE, FUN, Allocator > &b)
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)
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)
Array< TYPE, FUN, Allocator > operator+(const Array< TYPE, FUN, Allocator > &a, const Array< TYPE, FUN, Allocator > &b)