aboutsummaryrefslogtreecommitdiff
path: root/src/jtutil/norm.cc
blob: 2e20327d71a336bea36330c2f92cec6f8e0d2883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// norm.cc -- compute norms
// $Id$

//
// *** class jtutil::norm::
//

#include <cmath>
#include <algorithm>
#include <assert.h>

#include "util.hh"

//*****************************************************************************

// specify data point
namespace jtutil
	  {
template <typename fp>
  void norm<fp>::data(fp x)
{
++N_;
sum_  += x;
sum2_ += x*x;
infinity_norm_ = std::max(infinity_norm_, jtutil::abs<fp>(x));
}
	  }	// namespace jtutil::

//******************************************************************************

// get norms etc
namespace jtutil
	  {
template<typename fp>
  fp norm<fp>::mean() const { return sum_/fp(N_); }
template<typename fp>
  fp norm<fp>::two_norm() const { return std::sqrt(sum2_); }
template<typename fp>
  fp norm<fp>::rms_norm() const
	{ assert(is_nonempty()); return std::sqrt(sum2_/fp(N_)); }
template<typename fp>
  fp norm<fp>::infinity_norm() const { return infinity_norm_; }
	  }	// namespace jtutil::

//******************************************************************************

//
// ***** template instantiations *****
//

template class jtutil::norm<float>;
template class jtutil::norm<double>;