comparison liboctave/numeric/oct-norm.cc @ 19895:19755f4fc851

maint: Cleanup C++ code to follow Octave coding conventions. Try to wrap long lines to < 80 characters. Use GNU style and don't indent first brace of function definition. "case" statement is aligned flush left with brace of switch stmt. Remove trailing '\' line continuation from the end of #define macros. Use 2 spaces for indent. * files-dock-widget.cc, history-dock-widget.cc, main-window.cc, octave-cmd.cc, octave-dock-widget.cc, octave-gui.cc, resource-manager.cc, settings-dialog.cc, shortcut-manager.cc, welcome-wizard.cc, workspace-view.cc, cellfun.cc, data.cc, debug.cc, debug.h, dirfns.cc, error.h, file-io.cc, gl-render.cc, gl-render.h, gl2ps-renderer.h, graphics.cc, graphics.in.h, help.cc, input.cc, load-path.cc, load-path.h, lookup.cc, lu.cc, oct-stream.cc, octave-default-image.h, ordschur.cc, pr-output.cc, qz.cc, strfns.cc, symtab.cc, symtab.h, sysdep.cc, variables.cc, zfstream.h, __fltk_uigetfile__.cc, __init_fltk__.cc, __magick_read__.cc, __osmesa_print__.cc, audiodevinfo.cc, ov-classdef.cc, ov-classdef.h, ov-fcn.h, ov-float.cc, ov-flt-complex.cc, ov-java.cc, ov-range.cc, ov-re-mat.cc, ov-usr-fcn.h, ov.cc, op-int.h, options-usage.h, pt-eval.cc, Array-C.cc, Array-fC.cc, Array.cc, Array.h, PermMatrix.cc, Sparse.cc, chMatrix.h, dSparse.cc, dim-vector.h, bsxfun-decl.h, bsxfun-defs.cc, oct-norm.cc, Sparse-op-defs.h, oct-inttypes.cc, oct-inttypes.h, main.in.cc, mkoctfile.in.cc: Cleanup C++ code to follow Octave coding conventions.
author Rik <rik@octave.org>
date Wed, 25 Feb 2015 11:55:49 -0800
parents 4197fc428c7d
children 277b12eed117
comparison
equal deleted inserted replaced
19894:e8ccfc5d892b 19895:19755f4fc851
75 norm_accumulator_p () {} // we need this one for Array 75 norm_accumulator_p () {} // we need this one for Array
76 norm_accumulator_p (R pp) : p(pp), scl(0), sum(1) {} 76 norm_accumulator_p (R pp) : p(pp), scl(0), sum(1) {}
77 77
78 template<class U> 78 template<class U>
79 void accum (U val) 79 void accum (U val)
80 { 80 {
81 octave_quit (); 81 octave_quit ();
82 R t = std::abs (val); 82 R t = std::abs (val);
83 if (scl == t) // we need this to handle Infs properly 83 if (scl == t) // we need this to handle Infs properly
84 sum += 1;
85 else if (scl < t)
86 {
87 sum *= std::pow (scl/t, p);
84 sum += 1; 88 sum += 1;
85 else if (scl < t) 89 scl = t;
86 { 90 }
87 sum *= std::pow (scl/t, p); 91 else if (t != 0)
88 sum += 1; 92 sum += std::pow (t/scl, p);
89 scl = t; 93 }
90 }
91 else if (t != 0)
92 sum += std::pow (t/scl, p);
93 }
94 operator R () { return scl * std::pow (sum, 1/p); } 94 operator R () { return scl * std::pow (sum, 1/p); }
95 }; 95 };
96 96
97 // norm accumulator for the minus p-pseudonorm 97 // norm accumulator for the minus p-pseudonorm
98 template <class R> 98 template <class R>
103 norm_accumulator_mp () {} // we need this one for Array 103 norm_accumulator_mp () {} // we need this one for Array
104 norm_accumulator_mp (R pp) : p(pp), scl(0), sum(1) {} 104 norm_accumulator_mp (R pp) : p(pp), scl(0), sum(1) {}
105 105
106 template<class U> 106 template<class U>
107 void accum (U val) 107 void accum (U val)
108 { 108 {
109 octave_quit (); 109 octave_quit ();
110 R t = 1 / std::abs (val); 110 R t = 1 / std::abs (val);
111 if (scl == t) 111 if (scl == t)
112 sum += 1;
113 else if (scl < t)
114 {
115 sum *= std::pow (scl/t, p);
112 sum += 1; 116 sum += 1;
113 else if (scl < t) 117 scl = t;
114 { 118 }
115 sum *= std::pow (scl/t, p); 119 else if (t != 0)
116 sum += 1; 120 sum += std::pow (t/scl, p);
117 scl = t; 121 }
118 }
119 else if (t != 0)
120 sum += std::pow (t/scl, p);
121 }
122 operator R () { return scl * std::pow (sum, -1/p); } 122 operator R () { return scl * std::pow (sum, -1/p); }
123 }; 123 };
124 124
125 // norm accumulator for the 2-norm (euclidean) 125 // norm accumulator for the 2-norm (euclidean)
126 template <class R> 126 template <class R>
130 static R pow2 (R x) { return x*x; } 130 static R pow2 (R x) { return x*x; }
131 public: 131 public:
132 norm_accumulator_2 () : scl(0), sum(1) {} 132 norm_accumulator_2 () : scl(0), sum(1) {}
133 133
134 void accum (R val) 134 void accum (R val)
135 { 135 {
136 R t = std::abs (val); 136 R t = std::abs (val);
137 if (scl == t) 137 if (scl == t)
138 sum += 1;
139 else if (scl < t)
140 {
141 sum *= pow2 (scl/t);
138 sum += 1; 142 sum += 1;
139 else if (scl < t) 143 scl = t;
140 { 144 }
141 sum *= pow2 (scl/t); 145 else if (t != 0)
142 sum += 1; 146 sum += pow2 (t/scl);
143 scl = t; 147 }
144 }
145 else if (t != 0)
146 sum += pow2 (t/scl);
147 }
148 148
149 void accum (std::complex<R> val) 149 void accum (std::complex<R> val)
150 { 150 {
151 accum (val.real ()); 151 accum (val.real ());
152 accum (val.imag ()); 152 accum (val.imag ());
153 } 153 }
154 154
155 operator R () { return scl * std::sqrt (sum); } 155 operator R () { return scl * std::sqrt (sum); }
156 }; 156 };
157 157
158 // norm accumulator for the 1-norm (city metric) 158 // norm accumulator for the 1-norm (city metric)
162 R sum; 162 R sum;
163 public: 163 public:
164 norm_accumulator_1 () : sum (0) {} 164 norm_accumulator_1 () : sum (0) {}
165 template<class U> 165 template<class U>
166 void accum (U val) 166 void accum (U val)
167 { 167 {
168 sum += std::abs (val); 168 sum += std::abs (val);
169 } 169 }
170 operator R () { return sum; } 170 operator R () { return sum; }
171 }; 171 };
172 172
173 // norm accumulator for the inf-norm (max metric) 173 // norm accumulator for the inf-norm (max metric)
174 template <class R> 174 template <class R>
177 R max; 177 R max;
178 public: 178 public:
179 norm_accumulator_inf () : max (0) {} 179 norm_accumulator_inf () : max (0) {}
180 template<class U> 180 template<class U>
181 void accum (U val) 181 void accum (U val)
182 { 182 {
183 max = std::max (max, std::abs (val)); 183 max = std::max (max, std::abs (val));
184 } 184 }
185 operator R () { return max; } 185 operator R () { return max; }
186 }; 186 };
187 187
188 // norm accumulator for the -inf pseudonorm (min abs value) 188 // norm accumulator for the -inf pseudonorm (min abs value)
189 template <class R> 189 template <class R>
192 R min; 192 R min;
193 public: 193 public:
194 norm_accumulator_minf () : min (octave_Inf) {} 194 norm_accumulator_minf () : min (octave_Inf) {}
195 template<class U> 195 template<class U>
196 void accum (U val) 196 void accum (U val)
197 { 197 {
198 min = std::min (min, std::abs (val)); 198 min = std::min (min, std::abs (val));
199 } 199 }
200 operator R () { return min; } 200 operator R () { return min; }
201 }; 201 };
202 202
203 // norm accumulator for the 0-pseudonorm (hamming distance) 203 // norm accumulator for the 0-pseudonorm (hamming distance)
204 template <class R> 204 template <class R>
207 unsigned int num; 207 unsigned int num;
208 public: 208 public:
209 norm_accumulator_0 () : num (0) {} 209 norm_accumulator_0 () : num (0) {}
210 template<class U> 210 template<class U>
211 void accum (U val) 211 void accum (U val)
212 { 212 {
213 if (val != static_cast<U> (0)) ++num; 213 if (val != static_cast<U> (0)) ++num;
214 } 214 }
215 operator R () { return num; } 215 operator R () { return num; }
216 }; 216 };
217 217
218 218
219 // OK, we're armed :) Now let's go for the fun 219 // OK, we're armed :) Now let's go for the fun