blob: 5c9b699ff1d321ec5e8154efed2dec7abd2f4938 (
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
// norm.cc -- compute norms
// $Header$
//
// jtutil::norm::data
// jtutil::norm::mean
// jtutil::norm::two_norm
// jtutil::norm::rms_norm
//
// ***** template instantiations *****
//
#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include "util.hh"
// everything in this file is inside these namespaces
namespace AHFinderDirect
{
namespace jtutil
{
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//
// This function constructs a norm object.
//
template <typename fp_t>
norm<fp_t>::norm()
: N_(0L),
sum_(0.0), sum2_(0.0),
max_abs_value_(0.0), min_abs_value_(0.0),
max_value_(0.0), min_value_(0.0)
{ }
//*****************************************************************************
//
// This function resets a norm object to its initial state
//
template <typename fp_t>
void norm<fp_t>::reset()
{
N_ = 0L;
sum_ = 0.0;
sum2_ = 0.0;
max_abs_value_ = 0.0;
min_abs_value_ = 0.0;
max_value_ = 0.0;
min_value_ = 0.0;
}
//*****************************************************************************
//
// This function updates the norms with a new data point x .
//
template <typename fp_t>
void norm<fp_t>::data(fp_t x)
{
sum_ += x;
sum2_ += x*x;
const fp_t abs_x = jtutil::abs<fp_t>(x);
max_abs_value_ = jtutil::max(max_abs_value_, abs_x);
min_abs_value_ = (N_ == 0) ? abs_x : jtutil::min(min_abs_value_, abs_x);
min_value_ = (N_ == 0) ? x : jtutil::min(min_value_, x);
max_value_ = (N_ == 0) ? x : jtutil::max(max_value_, x);
++N_;
}
//******************************************************************************
//
// these functions compute the corresponding norms
//
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 instantiations *****
//
template class jtutil::norm<float>;
template class jtutil::norm<double>;
//******************************************************************************
//******************************************************************************
//******************************************************************************
} // namespace jtutil
} // namespace AHFinderDirect
|