Molecular Dynamics Simulation  1.0
Namespaces | Classes | Functions
ArrayUtils Namespace Reference

Collection of utility functions and operators for iterable data containers. More...

Namespaces

 is_container_impl
 Type trait to check if a given type is a container.
 

Classes

struct  is_container
 

Functions

template<class Container >
std::string to_string (const Container &container, const std::string &delimiter=", ", const std::array< std::string, 2 > &surround={"[", "]"})
 
template<class Container , class F >
Container elementWisePairOp (const Container &lhs, const Container &rhs, F binaryFunction)
 
template<class Scalar , class Container , class F >
Container elementWiseScalarOp (const Scalar &lhs, const Container &rhs, F binaryFunction)
 
template<class Container >
auto L2Norm (const Container &c)
 
template<>
auto L2Norm< std::array< double, 3 > > (const std::array< double, 3 > &c)
 
template<class Container >
auto L2NormSquared (const Container &c)
 
template<>
auto L2NormSquared< std::array< double, 3 > > (const std::array< double, 3 > &c)
 

Detailed Description

Collection of utility functions and operators for iterable data containers.

Collection of utility functions and operators for iterable data containers like std::array, std::vector, etc.

Function Documentation

◆ elementWisePairOp()

template<class Container , class F >
Container ArrayUtils::elementWisePairOp ( const Container &  lhs,
const Container &  rhs,
binaryFunction 
)
inline

Applies an element wise binary function F to two containers.

If the containers differ in size the F is only applied to as many elements as are in the smaller container.

Template Parameters
ContainerType for both containers.
FType of binary function.
Parameters
lhs
rhs
binaryFunction
Returns
Element wise F(lhs, rhs).

Definition at line 127 of file ArrayUtils.h.

127  {
128  Container ret = lhs;
129  auto retIter = std::begin(ret);
130  auto lhsIter = std::cbegin(lhs);
131  const auto lhsEnd = std::cend(lhs);
132  auto rhsIter = std::cbegin(rhs);
133  const auto rhsEnd = std::cend(rhs);
134 
135  for (; lhsIter != lhsEnd and rhsIter != rhsEnd; ++lhsIter, ++rhsIter, ++retIter) {
136  *retIter = binaryFunction(*lhsIter, *rhsIter);
137  }
138 
139  return ret;
140 }

◆ elementWiseScalarOp()

template<class Scalar , class Container , class F >
Container ArrayUtils::elementWiseScalarOp ( const Scalar &  lhs,
const Container &  rhs,
binaryFunction 
)
inline

Applies a binary function F to with a scalar to every element in a container.

Template Parameters
ScalarType of scalar value.
ContainerType of the container.
F
Parameters
lhs
rhs
binaryFunction
Returns
Element wise F(lhs, rhs).

Definition at line 154 of file ArrayUtils.h.

154  {
155  Container ret = rhs;
156  auto retIter = std::begin(ret);
157  auto rhsIter = std::cbegin(rhs);
158  const auto rhsEnd = std::cend(rhs);
159 
160  for (; rhsIter != rhsEnd; ++rhsIter, ++retIter) {
161  *retIter = binaryFunction(lhs, *rhsIter);
162  }
163 
164  return ret;
165 }

◆ L2Norm()

template<class Container >
auto ArrayUtils::L2Norm ( const Container &  c)

Calculates the L2 norm for a given container.

Template Parameters
Container
Parameters
c
Returns
sqrt(sum_i(c[i]*c[i])).

Definition at line 174 of file ArrayUtils.h.

174  {
175  return std::sqrt(std::accumulate(std::cbegin(c), std::cend(c), 0.0, [](auto a, auto b) { return a + b * b; }));
176 }

◆ L2Norm< std::array< double, 3 > >()

template<>
auto ArrayUtils::L2Norm< std::array< double, 3 > > ( const std::array< double, 3 > &  c)
inline

Specialization of L2Norm for std::array<double, 3>.

Returns
sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]).

Definition at line 183 of file ArrayUtils.h.

183  {
184  return std::sqrt(c[0] * c[0] + c[1] * c[1] + c[2] * c[2]);
185 }

◆ L2NormSquared()

template<class Container >
auto ArrayUtils::L2NormSquared ( const Container &  c)

Calculates the L2 norm for a given container.

Template Parameters
Container
Parameters
c
Returns
sqrt(sum_i(c[i]*c[i])).

Definition at line 194 of file ArrayUtils.h.

194  {
195  return std::accumulate(std::cbegin(c), std::cend(c), 0.0, [](auto a, auto b) { return a + b * b; });
196 }

◆ L2NormSquared< std::array< double, 3 > >()

template<>
auto ArrayUtils::L2NormSquared< std::array< double, 3 > > ( const std::array< double, 3 > &  c)
inline

Specialization of L2Norm for std::array<double, 3>.

Returns
c[0]*c[0] + c[1]*c[1] + c[2]*c[2].

Definition at line 203 of file ArrayUtils.h.

203  {
204  return c[0] * c[0] + c[1] * c[1] + c[2] * c[2];
205 }

◆ to_string()

template<class Container >
std::string ArrayUtils::to_string ( const Container &  container,
const std::string &  delimiter = ", ",
const std::array< std::string, 2 > &  surround = {"[", "]"} 
)

Generates a string representation of a container which fulfills the Container requirement (provide cbegin and cend).

Template Parameters
ContainerType of Container.
Parameters
containerContainer to be converted to string.
delimiterString that is put between items.
surroundStrings to be put before and after the listing (e.g. brackets).
Returns
String representation of container.

Definition at line 97 of file ArrayUtils.h.

98  {"[", "]"}) {
99  auto iter = std::cbegin(container);
100  const auto end = std::cend(container);
101  if (iter == end) {
102  return surround[0] + surround[1];
103  }
104  std::ostringstream strStream;
105  strStream << surround[0] << *iter;
106  for (++iter; iter != end; ++iter) {
107  strStream << delimiter << *iter;
108  }
109  strStream << surround[1];
110  return strStream.str();
111 }