STIR 6.4.0
more_algorithms.inl
Go to the documentation of this file.
1//
2//
3/*
4 Copyright (C) 2005- 2005, Hammersmith Imanet Ltd
5 This file is part of STIR.
6
7 SPDX-License-Identifier: Apache-2.0
8
9 See STIR/LICENSE.txt for details
10*/
20// not nice to have a dependency in buildblock on numerics/norm, but
21// we'll bother with that when necessary.
22#include "stir/numerics/norm.h"
23
24START_NAMESPACE_STIR
25
26template <class iterT>
27iterT
28abs_max_element(iterT start, iterT end)
29{
30 if (start == end)
31 return start;
32 iterT current_max_iter = start;
33 double current_max = norm_squared(*start);
34 iterT iter = start;
35 ++iter;
36
37 while (iter != end)
38 {
39 const double n = norm_squared(*iter);
40 if (n > current_max)
41 {
42 current_max = n;
43 current_max_iter = iter;
44 }
45 ++iter;
46 }
47 return current_max_iter;
48}
49
50template <class IterT, class elemT>
51inline elemT
52sum(IterT start, IterT end, elemT init)
53{
54 elemT tmp = init;
55 for (IterT iter = start; iter != end; ++iter)
56 tmp += *iter;
57 return tmp;
58}
59
60template <class IterT>
61inline typename std::iterator_traits<IterT>::value_type
62sum(IterT start, IterT end)
63{
64 if (start == end)
65 {
66 typename std::iterator_traits<IterT>::value_type tmp;
67 tmp *= 0;
68 return tmp;
69 }
70 return sum(start + 1, end, *start);
71}
72
73template <class IterT>
74inline typename std::iterator_traits<IterT>::value_type
75average(IterT start, IterT end)
76{
77 typename std::iterator_traits<IterT>::value_type tmp = sum(start, end);
78 tmp /= (end - start);
79 return tmp;
80}
81
82END_NAMESPACE_STIR
double norm_squared(const BasicCoordinate< num_dimensions, coordT > &p1)
compute (inner_product(p1,p1))
Definition BasicCoordinate.inl:415
iterT abs_max_element(iterT start, iterT end)
Like std::max_element, but comparing after taking absolute value.
Definition more_algorithms.inl:28
elemT sum(IterT start, IterT end, elemT init)
Compute the sum of a sequence using operator+=(), using an initial value.
Definition more_algorithms.inl:52
std::iterator_traits< IterT >::value_type average(IterT start, IterT end)
Compute the average of a sequence using sum(start,end).
Definition more_algorithms.inl:75
Declaration of the stir::norm(), stir::norm_squared() functions and stir::NormSquared unary function.