comparison liboctave/numeric/oct-norm.cc @ 30119:b0af792466dc

maint: Add "private:" access specifier to classes in oct-norm.cc. * oct-norm.cc: Add "private:" access specifier for private member variables in norm_accumulator_XXX classes.
author Rik <rik@octave.org>
date Thu, 02 Sep 2021 20:48:47 -0700
parents de8ba056e8e2
children f3f3e3793fb5
comparison
equal deleted inserted replaced
30118:62af50666f62 30119:b0af792466dc
72 72
73 // norm accumulator for the p-norm 73 // norm accumulator for the p-norm
74 template <typename R> 74 template <typename R>
75 class norm_accumulator_p 75 class norm_accumulator_p
76 { 76 {
77 R m_p,m_scl,m_sum;
78 public: 77 public:
79 norm_accumulator_p () { } // we need this one for Array 78 norm_accumulator_p () { } // we need this one for Array
80 norm_accumulator_p (R pp) : m_p(pp), m_scl(0), m_sum(1) { } 79 norm_accumulator_p (R pp) : m_p(pp), m_scl(0), m_sum(1) { }
81 80
82 template <typename U> 81 template <typename U>
93 m_scl = t; 92 m_scl = t;
94 } 93 }
95 else if (t != 0) 94 else if (t != 0)
96 m_sum += std::pow (t/m_scl, m_p); 95 m_sum += std::pow (t/m_scl, m_p);
97 } 96 }
97
98 operator R () { return m_scl * std::pow (m_sum, 1/m_p); } 98 operator R () { return m_scl * std::pow (m_sum, 1/m_p); }
99
100 private:
101 R m_p, m_scl, m_sum;
99 }; 102 };
100 103
101 // norm accumulator for the minus p-pseudonorm 104 // norm accumulator for the minus p-pseudonorm
102 template <typename R> 105 template <typename R>
103 class norm_accumulator_mp 106 class norm_accumulator_mp
104 { 107 {
105 R m_p,m_scl,m_sum;
106 public: 108 public:
107 norm_accumulator_mp () { } // we need this one for Array 109 norm_accumulator_mp () { } // we need this one for Array
108 norm_accumulator_mp (R pp) : m_p(pp), m_scl(0), m_sum(1) { } 110 norm_accumulator_mp (R pp) : m_p(pp), m_scl(0), m_sum(1) { }
109 111
110 template <typename U> 112 template <typename U>
121 m_scl = t; 123 m_scl = t;
122 } 124 }
123 else if (t != 0) 125 else if (t != 0)
124 m_sum += std::pow (t/m_scl, m_p); 126 m_sum += std::pow (t/m_scl, m_p);
125 } 127 }
128
126 operator R () { return m_scl * std::pow (m_sum, -1/m_p); } 129 operator R () { return m_scl * std::pow (m_sum, -1/m_p); }
130
131 private:
132 R m_p, m_scl, m_sum;
127 }; 133 };
128 134
129 // norm accumulator for the 2-norm (euclidean) 135 // norm accumulator for the 2-norm (euclidean)
130 template <typename R> 136 template <typename R>
131 class norm_accumulator_2 137 class norm_accumulator_2
132 { 138 {
133 R m_scl,m_sum;
134 static R pow2 (R x) { return x*x; }
135 public: 139 public:
136 norm_accumulator_2 () : m_scl(0), m_sum(1) { } 140 norm_accumulator_2 () : m_scl(0), m_sum(1) { }
137 141
138 void accum (R val) 142 void accum (R val)
139 { 143 {
155 accum (val.real ()); 159 accum (val.real ());
156 accum (val.imag ()); 160 accum (val.imag ());
157 } 161 }
158 162
159 operator R () { return m_scl * std::sqrt (m_sum); } 163 operator R () { return m_scl * std::sqrt (m_sum); }
164
165 private:
166 static inline R pow2 (R x) { return x*x; }
167
168 //--------
169
170 R m_scl, m_sum;
160 }; 171 };
161 172
162 // norm accumulator for the 1-norm (city metric) 173 // norm accumulator for the 1-norm (city metric)
163 template <typename R> 174 template <typename R>
164 class norm_accumulator_1 175 class norm_accumulator_1
165 { 176 {
166 R m_sum;
167 public: 177 public:
168 norm_accumulator_1 () : m_sum (0) { } 178 norm_accumulator_1 () : m_sum (0) { }
169 template <typename U> 179 template <typename U>
170 void accum (U val) 180 void accum (U val)
171 { 181 {
172 m_sum += std::abs (val); 182 m_sum += std::abs (val);
173 } 183 }
184
174 operator R () { return m_sum; } 185 operator R () { return m_sum; }
186
187 private:
188 R m_sum;
175 }; 189 };
176 190
177 // norm accumulator for the inf-norm (max metric) 191 // norm accumulator for the inf-norm (max metric)
178 template <typename R> 192 template <typename R>
179 class norm_accumulator_inf 193 class norm_accumulator_inf
180 { 194 {
181 R m_max;
182 public: 195 public:
183 norm_accumulator_inf () : m_max (0) { } 196 norm_accumulator_inf () : m_max (0) { }
184 template <typename U> 197 template <typename U>
185 void accum (U val) 198 void accum (U val)
186 { 199 {
187 if (math::isnan (val)) 200 if (math::isnan (val))
188 m_max = numeric_limits<R>::NaN (); 201 m_max = numeric_limits<R>::NaN ();
189 else 202 else
190 m_max = std::max (m_max, std::abs (val)); 203 m_max = std::max (m_max, std::abs (val));
191 } 204 }
205
192 operator R () { return m_max; } 206 operator R () { return m_max; }
207
208 private:
209 R m_max;
193 }; 210 };
194 211
195 // norm accumulator for the -inf pseudonorm (m_min abs value) 212 // norm accumulator for the -inf pseudonorm (min abs value)
196 template <typename R> 213 template <typename R>
197 class norm_accumulator_minf 214 class norm_accumulator_minf
198 { 215 {
199 R m_min;
200 public: 216 public:
201 norm_accumulator_minf () : m_min (numeric_limits<R>::Inf ()) { } 217 norm_accumulator_minf () : m_min (numeric_limits<R>::Inf ()) { }
202 template <typename U> 218 template <typename U>
203 void accum (U val) 219 void accum (U val)
204 { 220 {
205 if (math::isnan (val)) 221 if (math::isnan (val))
206 m_min = numeric_limits<R>::NaN (); 222 m_min = numeric_limits<R>::NaN ();
207 else 223 else
208 m_min = std::min (m_min, std::abs (val)); 224 m_min = std::min (m_min, std::abs (val));
209 } 225 }
226
210 operator R () { return m_min; } 227 operator R () { return m_min; }
228
229 private:
230 R m_min;
211 }; 231 };
212 232
213 // norm accumulator for the 0-pseudonorm (hamming distance) 233 // norm accumulator for the 0-pseudonorm (hamming distance)
214 template <typename R> 234 template <typename R>
215 class norm_accumulator_0 235 class norm_accumulator_0
216 { 236 {
217 unsigned int m_num;
218 public: 237 public:
219 norm_accumulator_0 () : m_num (0) { } 238 norm_accumulator_0 () : m_num (0) { }
220 template <typename U> 239 template <typename U>
221 void accum (U val) 240 void accum (U val)
222 { 241 {
223 if (val != static_cast<U> (0)) ++m_num; 242 if (val != static_cast<U> (0)) ++m_num;
224 } 243 }
244
225 operator R () { return m_num; } 245 operator R () { return m_num; }
246
247 private:
248 unsigned int m_num;
226 }; 249 };
227 250
228 // OK, we're armed :) Now let's go for the fun 251 // OK, we're armed :) Now let's go for the fun
229 252
230 template <typename T, typename R, typename ACC> 253 template <typename T, typename R, typename ACC>