BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
algo Namespace Reference

Some additions to standard library algorithms. More...

Functions

bool almostEqual (double a, double b)
 Returns true if two doubles agree within machine epsilon. More...
 
template<class T >
std::vector< T > concat (const std::vector< T > &v1, const std::vector< T > &v2)
 Returns the concatenation of two std::vectors. More...
 
template<typename Evaluator , typename Iterator >
double max_value (const Iterator &begin, const Iterator &end, const Evaluator &evaluate)
 Returns the maximum value of function evaluate as applied to the elements of an iterator range. More...
 
template<typename Evaluator , typename Iterator >
double min_value (const Iterator &begin, const Iterator &end, const Evaluator &evaluate)
 Returns the minimum value of function evaluate as applied to the elements of an iterator range. More...
 

Detailed Description

Some additions to standard library algorithms.

Function Documentation

◆ almostEqual()

bool algo::almostEqual ( double  a,
double  b 
)
inline

Returns true if two doubles agree within machine epsilon.

Definition at line 34 of file Algorithms.h.

35 {
36  constexpr double eps = std::numeric_limits<double>::epsilon();
37  return std::abs(a - b) <= eps * std::max(eps, (std::abs(a) + std::abs(b)) / 2);
38 }

Referenced by VerticalLine::contains(), HorizontalLine::contains(), CustomBinAxis::equals(), FixedBinAxis::equals(), VariableBinAxis::equals(), ConstKBinAxis::equals(), pyfmt::isHexagonal(), and pyfmt::isSquare().

◆ concat()

template<class T >
std::vector< T > algo::concat ( const std::vector< T > &  v1,
const std::vector< T > &  v2 
)

Returns the concatenation of two std::vectors.

Definition at line 79 of file Algorithms.h.

80 {
81  std::vector<T> v = v1;
82  v.insert(v.end(), v2.begin(), v2.end());
83  return v;
84 }

Referenced by nodeMetaUnion().

◆ max_value()

template<typename Evaluator , typename Iterator >
double algo::max_value ( const Iterator &  begin,
const Iterator &  end,
const Evaluator &  evaluate 
)

Returns the maximum value of function evaluate as applied to the elements of an iterator range.

Definition at line 69 of file Algorithms.h.

70 {
71  ASSERT(begin != end);
72  double ret = evaluate(*begin);
73  Iterator it = begin;
74  while (++it != end)
75  ret = std::max(ret, evaluate(*it));
76  return ret;
77 }
#define ASSERT(condition)
Definition: Assert.h:31

References ASSERT.

Referenced by FormFactorWeighted::topZ(), and IBornFF::TopZ().

◆ min_value()

template<typename Evaluator , typename Iterator >
double algo::min_value ( const Iterator &  begin,
const Iterator &  end,
const Evaluator &  evaluate 
)

Returns the minimum value of function evaluate as applied to the elements of an iterator range.

Definition at line 58 of file Algorithms.h.

59 {
60  ASSERT(begin != end);
61  double ret = evaluate(*begin);
62  Iterator it = begin;
63  while (++it != end)
64  ret = std::min(ret, evaluate(*it));
65  return ret;
66 }

References ASSERT.

Referenced by FormFactorWeighted::bottomZ(), IBornFF::BottomZ(), and TestMinimizer::minimize_scalar().