fn_norm.hpp
Go to the documentation of this file.
1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12 
13 
16 
17 
18 
19 template<typename T1>
21 inline
22 typename T1::pod_type
24  {
26 
27  typedef typename T1::pod_type T;
28 
29  T acc = T(0);
30 
32  {
33  typename Proxy<T1>::ea_type P = A.get_ea();
34 
35  const uword N = A.get_n_elem();
36 
37  uword i,j;
38 
39  for(i=0, j=1; j<N; i+=2, j+=2)
40  {
41  acc += std::abs(P[i]);
42  acc += std::abs(P[j]);
43  }
44 
45  if(i < N)
46  {
47  acc += std::abs(P[i]);
48  }
49  }
50  else
51  {
52  const uword n_rows = A.get_n_rows();
53  const uword n_cols = A.get_n_cols();
54 
55  for(uword col=0; col<n_cols; ++col)
56  {
57  uword i,j;
58 
59  for(i=0, j=1; j<n_rows; i+=2, j+=2)
60  {
61  acc += std::abs(A.at(i,col));
62  acc += std::abs(A.at(j,col));
63  }
64 
65  if(i < n_rows)
66  {
67  acc += std::abs(A.at(i,col));
68  }
69  }
70  }
71 
72  return acc;
73  }
74 
75 
76 
77 template<typename T1>
79 inline
80 typename T1::pod_type
82  {
85 
86  typedef typename T1::pod_type T;
87 
88  T acc = T(0);
89 
91  {
92  typename Proxy<T1>::ea_type P = A.get_ea();
93 
94  const uword N = A.get_n_elem();
95 
96  uword i,j;
97 
98  for(i=0, j=1; j<N; i+=2, j+=2)
99  {
100  const T tmp_i = P[i];
101  const T tmp_j = P[j];
102 
103  acc += tmp_i * tmp_i;
104  acc += tmp_j * tmp_j;
105  }
106 
107  if(i < N)
108  {
109  const T tmp_i = P[i];
110 
111  acc += tmp_i * tmp_i;
112  }
113  }
114  else
115  {
116  const uword n_rows = A.get_n_rows();
117  const uword n_cols = A.get_n_cols();
118 
119  for(uword col=0; col<n_cols; ++col)
120  {
121  uword i,j;
122 
123  for(i=0, j=1; j<n_rows; i+=2, j+=2)
124  {
125  const T tmp_i = A.at(i,col);
126  const T tmp_j = A.at(j,col);
127 
128  acc += tmp_i * tmp_i;
129  acc += tmp_j * tmp_j;
130  }
131 
132  if(i < n_rows)
133  {
134  const T tmp_i = A.at(i,col);
135 
136  acc += tmp_i * tmp_i;
137  }
138  }
139  }
140 
141  return std::sqrt(acc);
142  }
143 
144 
145 
146 template<typename T1>
147 arma_hot
148 inline
149 typename T1::pod_type
151  {
153  arma_ignore(junk);
154 
155  typedef typename T1::pod_type T;
156 
157  T acc = T(0);
158 
159  if(Proxy<T1>::prefer_at_accessor == false)
160  {
161  typename Proxy<T1>::ea_type P = A.get_ea();
162 
163  const uword N = A.get_n_elem();
164 
165  for(uword i=0; i<N; ++i)
166  {
167  const T tmp = std::abs(P[i]);
168  acc += tmp*tmp;
169  }
170  }
171  else
172  {
173  const uword n_rows = A.get_n_rows();
174  const uword n_cols = A.get_n_cols();
175 
176  for(uword col=0; col<n_cols; ++col)
177  for(uword row=0; row<n_rows; ++row)
178  {
179  const T tmp = std::abs(A.at(row,col));
180  acc += tmp*tmp;
181  }
182  }
183 
184  return std::sqrt(acc);
185  }
186 
187 
188 
189 template<typename T1>
190 arma_hot
191 inline
192 typename T1::pod_type
193 arma_vec_norm_k(const Proxy<T1>& A, const int k)
194  {
196 
197  typedef typename T1::pod_type T;
198 
199  T acc = T(0);
200 
201  if(Proxy<T1>::prefer_at_accessor == false)
202  {
203  typename Proxy<T1>::ea_type P = A.get_ea();
204 
205  const uword N = A.get_n_elem();
206 
207  uword i,j;
208 
209  for(i=0, j=1; j<N; i+=2, j+=2)
210  {
211  acc += std::pow(std::abs(P[i]), k);
212  acc += std::pow(std::abs(P[j]), k);
213  }
214 
215  if(i < N)
216  {
217  acc += std::pow(std::abs(P[i]), k);
218  }
219  }
220  else
221  {
222  const uword n_rows = A.get_n_rows();
223  const uword n_cols = A.get_n_cols();
224 
225  for(uword col=0; col<n_cols; ++col)
226  for(uword row=0; row<n_rows; ++row)
227  {
228  acc += std::pow(std::abs(A.at(row,col)), k);
229  }
230  }
231 
232  return std::pow(acc, T(1)/T(k));
233  }
234 
235 
236 
237 template<typename T1>
238 arma_hot
239 inline
240 typename T1::pod_type
242  {
244 
245  typedef typename T1::pod_type T;
246  typedef typename Proxy<T1>::ea_type ea_type;
247 
248  ea_type P = A.get_ea();
249  const uword N = A.get_n_elem();
250 
251  T max_val = (N != 1) ? priv::most_neg<T>() : std::abs(P[0]);
252 
253  uword i,j;
254 
255  for(i=0, j=1; j<N; i+=2, j+=2)
256  {
257  const T tmp_i = std::abs(P[i]);
258  const T tmp_j = std::abs(P[j]);
259 
260  if(max_val < tmp_i) { max_val = tmp_i; }
261  if(max_val < tmp_j) { max_val = tmp_j; }
262  }
263 
264  if(i < N)
265  {
266  const T tmp_i = std::abs(P[i]);
267 
268  if(max_val < tmp_i) { max_val = tmp_i; }
269  }
270 
271  return max_val;
272  }
273 
274 
275 
276 template<typename T1>
277 arma_hot
278 inline
279 typename T1::pod_type
281  {
283 
284  typedef typename T1::pod_type T;
285  typedef typename Proxy<T1>::ea_type ea_type;
286 
287  ea_type P = A.get_ea();
288  const uword N = A.get_n_elem();
289 
290  T min_val = (N != 1) ? priv::most_pos<T>() : std::abs(P[0]);
291 
292  uword i,j;
293 
294  for(i=0, j=1; j<N; i+=2, j+=2)
295  {
296  const T tmp_i = std::abs(P[i]);
297  const T tmp_j = std::abs(P[j]);
298 
299  if(min_val > tmp_i) { min_val = tmp_i; }
300  if(min_val > tmp_j) { min_val = tmp_j; }
301  }
302 
303  if(i < N)
304  {
305  const T tmp_i = std::abs(P[i]);
306 
307  if(min_val > tmp_i) { min_val = tmp_i; }
308  }
309 
310  return min_val;
311  }
312 
313 
314 
315 template<typename T1>
316 inline
317 typename T1::pod_type
319  {
321 
322  typedef typename T1::elem_type eT;
323  typedef typename T1::pod_type T;
324 
326  const Mat<eT>& X = tmp.M;
327 
328  // TODO: this can be sped up with a dedicated implementation
329  return as_scalar( max( sum(abs(X)), 1) );
330  }
331 
332 
333 
334 template<typename T1>
335 inline
336 typename T1::pod_type
338  {
340 
341  typedef typename T1::elem_type eT;
342  typedef typename T1::pod_type T;
343 
345  const Mat<eT>& X = tmp.M;
346 
347  Col<T> S;
348  svd(S, X);
349 
350  return (S.n_elem > 0) ? max(S) : T(0);
351  }
352 
353 
354 
355 template<typename T1>
356 inline
357 typename T1::pod_type
359  {
361 
362  typedef typename T1::elem_type eT;
363  typedef typename T1::pod_type T;
364 
366  const Mat<eT>& X = tmp.M;
367 
368  // TODO: this can be sped up with a dedicated implementation
369  return as_scalar( max( sum(abs(X),1) ) );
370  }
371 
372 
373 
374 template<typename T1>
375 inline
377 typename T1::pod_type
378 norm
379  (
381  const uword k,
383  )
384  {
386  arma_ignore(junk);
387 
388  typedef typename T1::elem_type eT;
389  typedef typename T1::pod_type T;
390 
391  const Proxy<T1> A(X.get_ref());
392 
393  if(A.get_n_elem() == 0)
394  {
395  return T(0);
396  }
397 
398  const bool is_vec = (A.get_n_rows() == 1) || (A.get_n_cols() == 1);
399 
400  if(is_vec == true)
401  {
402  switch(k)
403  {
404  case 1:
405  return arma_vec_norm_1(A);
406  break;
407 
408  case 2:
409  return arma_vec_norm_2(A);
410  break;
411 
412  default:
413  {
414  arma_debug_check( (k == 0), "norm(): k must be greater than zero" );
415  return arma_vec_norm_k(A, int(k));
416  }
417  }
418  }
419  else
420  {
421  switch(k)
422  {
423  case 1:
424  return arma_mat_norm_1(A);
425  break;
426 
427  case 2:
428  return arma_mat_norm_2(A);
429  break;
430 
431  default:
432  arma_stop("norm(): unsupported matrix norm type");
433  return T(0);
434  }
435  }
436  }
437 
438 
439 
440 template<typename T1>
441 inline
443 typename T1::pod_type
444 norm
445  (
447  const char* method,
449  )
450  {
452  arma_ignore(junk);
453 
454  typedef typename T1::elem_type eT;
455  typedef typename T1::pod_type T;
456 
457  const Proxy<T1> A(X.get_ref());
458 
459  if(A.get_n_elem() == 0)
460  {
461  return T(0);
462  }
463 
464  const char sig = method[0];
465  const bool is_vec = (A.get_n_rows() == 1) || (A.get_n_cols() == 1);
466 
467  if(is_vec == true)
468  {
469  if( (sig == 'i') || (sig == 'I') || (sig == '+') ) // max norm
470  {
471  return arma_vec_norm_max(A);
472  }
473  else
474  if(sig == '-') // min norm
475  {
476  return arma_vec_norm_min(A);
477  }
478  else
479  if( (sig == 'f') || (sig == 'F') )
480  {
481  return arma_vec_norm_2(A);
482  }
483  else
484  {
485  arma_stop("norm(): unsupported vector norm type");
486  return T(0);
487  }
488  }
489  else
490  {
491  if( (sig == 'i') || (sig == 'I') || (sig == '+') ) // inf norm
492  {
493  return arma_mat_norm_inf(A);
494  }
495  else
496  if( (sig == 'f') || (sig == 'F') )
497  {
498  return arma_vec_norm_2(A);
499  }
500  else
501  {
502  arma_stop("norm(): unsupported matrix norm type");
503  return T(0);
504  }
505  }
506  }
507 
508 
509 
arma_inline const Op< T1, op_sum > sum(const Base< typename T1::elem_type, T1 > &X, const uword dim=0)
Delayed sum of elements of a matrix along a specified dimension (either rows or columns). The result is stored in a dense matrix that has either one column or one row. For dim = 0, find the sum of each column. For dim = 1, find the sum of each row. The default is dim = 0. NOTE: this function works differently than in Matlab/Octave.
Definition: fn_sum.hpp:29
arma_hot T1::pod_type arma_vec_norm_1(const Proxy< T1 > &A)
Definition: fn_norm.hpp:23
T1::pod_type arma_mat_norm_inf(const Proxy< T1 > &A)
Definition: fn_norm.hpp:358
arma_inline const derived & get_ref() const
Definition: Base_meat.hpp:22
arma_inline const eOp< T1, eop_sqrt > sqrt(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_elem.hpp:403
arma_hot T1::pod_type arma_vec_norm_k(const Proxy< T1 > &A, const int k)
Definition: fn_norm.hpp:193
const uword n_elem
number of elements in the matrix (read-only)
Definition: Mat_bones.hpp:31
arma_inline const Op< T1, op_max > max(const Base< typename T1::elem_type, T1 > &X, const uword dim=0)
Delayed &#39;maximum values&#39; operation. The dimension, along which the maxima are found, is set via &#39;dim&#39;. For dim = 0, the maximum value of each column is found (i.e. searches by traversing across rows). For dim = 1, the maximum value of each row is found (i.e. searches by traversing across columns). The default is dim = 0.
Definition: fn_max.hpp:28
arma_hot T1::pod_type arma_vec_norm_min(const Proxy< T1 > &A)
Definition: fn_norm.hpp:280
arma_warn_unused T1::pod_type norm(const Base< typename T1::elem_type, T1 > &X, const uword k, const typename arma_float_or_cx_only< typename T1::elem_type >::result *junk=0)
Definition: fn_norm.hpp:379
T1::pod_type arma_mat_norm_1(const Proxy< T1 > &A)
Definition: fn_norm.hpp:318
u32 uword
Definition: typedef.hpp:85
Class for column vectors (matrices with only one column)
Definition: Col_bones.hpp:20
#define arma_debug_check
Definition: debug.hpp:1084
arma_hot T1::pod_type arma_vec_norm_max(const Proxy< T1 > &A)
Definition: fn_norm.hpp:241
const Mat< eT > M
Definition: unwrap.hpp:32
#define arma_ignore(variable)
arma_inline arma_warn_unused T1::elem_type as_scalar(const Glue< T1, T2, glue_times > &X, const typename arma_not_cx< typename T1::elem_type >::result *junk=0)
void arma_cold arma_stop(const T1 &x)
print a message to get_stream_err1() and/or throw a logic_error exception
Definition: debug.hpp:98
arma_inline const eOp< T1, eop_abs > abs(const Base< typename T1::elem_type, T1 > &X, const typename arma_not_cx< typename T1::elem_type >::result *junk=0)
Definition: fn_elem.hpp:317
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
arma_inline const eOp< T1, eop_pow > pow(const Base< typename T1::elem_type, T1 > &A, const typename T1::elem_type exponent)
Definition: fn_elem.hpp:520
#define arma_warn_unused
bool svd(Col< typename T1::pod_type > &S, const Base< typename T1::elem_type, T1 > &X, const typename arma_blas_type_only< typename T1::elem_type >::result *junk=0)
Definition: fn_svd.hpp:23
Dense matrix class.
arma_hot T1::pod_type arma_vec_norm_2(const Proxy< T1 > &A, const typename arma_not_cx< typename T1::elem_type >::result *junk=0)
Definition: fn_norm.hpp:81
#define arma_hot
T1::pod_type arma_mat_norm_2(const Proxy< T1 > &A)
Definition: fn_norm.hpp:337


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:57