blob: edd3fd114f6f831316d1935ca21ccc3fa7c56485 (
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
// $Header$
//
// *** class jtutil::norm::
//
#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include "util.hh"
//*****************************************************************************
// specify data point
namespace jtutil
{
template <typename fp_t>
void norm<fp_t>::data(fp_t x)
{
++N_;
sum_ += x;
sum2_ += x*x;
infinity_norm_ = jtutil::max(infinity_norm_, jtutil::abs<fp_t>(x));
}
} // namespace jtutil::
//******************************************************************************
// get norms etc
namespace jtutil
{
template<typename fp_t>
fp_t norm<fp_t>::mean() const { return sum_/fp_t(N_); }
template<typename fp_t>
fp_t norm<fp_t>::two_norm() const { return sqrt(sum2_); }
template<typename fp_t>
fp_t norm<fp_t>::rms_norm() const
{ assert(is_nonempty()); return sqrt(sum2_/fp_t(N_)); }
template<typename fp_t>
fp_t norm<fp_t>::infinity_norm() const { return infinity_norm_; }
} // namespace jtutil::
//******************************************************************************
//
// ***** template instantiations *****
//
template class jtutil::norm<float>;
template class jtutil::norm<double>;
|