fn_norm.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup fn_norm
  16. //! @{
  17. template<typename T1>
  18. inline
  19. arma_warn_unused
  20. typename enable_if2< is_arma_type<T1>::value, typename T1::pod_type >::result
  21. norm
  22. (
  23. const T1& X,
  24. const uword k = uword(2),
  25. const typename arma_real_or_cx_only<typename T1::elem_type>::result* junk = 0
  26. )
  27. {
  28. arma_extra_debug_sigprint();
  29. arma_ignore(junk);
  30. typedef typename T1::pod_type T;
  31. const Proxy<T1> P(X);
  32. if(P.get_n_elem() == 0)
  33. {
  34. return T(0);
  35. }
  36. const bool is_vec = (T1::is_xvec) || (T1::is_row) || (T1::is_col) || (P.get_n_rows() == 1) || (P.get_n_cols() == 1);
  37. if(is_vec)
  38. {
  39. switch(k)
  40. {
  41. case 1:
  42. return op_norm::vec_norm_1(P);
  43. break;
  44. case 2:
  45. return op_norm::vec_norm_2(P);
  46. break;
  47. default:
  48. {
  49. arma_debug_check( (k == 0), "norm(): k must be greater than zero" );
  50. return op_norm::vec_norm_k(P, int(k));
  51. }
  52. }
  53. }
  54. else
  55. {
  56. switch(k)
  57. {
  58. case 1:
  59. return op_norm::mat_norm_1(P);
  60. break;
  61. case 2:
  62. return op_norm::mat_norm_2(P);
  63. break;
  64. default:
  65. arma_stop_logic_error("norm(): unsupported matrix norm type");
  66. return T(0);
  67. }
  68. }
  69. return T(0); // prevent erroneous compiler warnings
  70. }
  71. template<typename T1>
  72. inline
  73. arma_warn_unused
  74. typename enable_if2< is_arma_type<T1>::value, typename T1::pod_type >::result
  75. norm
  76. (
  77. const T1& X,
  78. const char* method,
  79. const typename arma_real_or_cx_only<typename T1::elem_type>::result* junk = 0
  80. )
  81. {
  82. arma_extra_debug_sigprint();
  83. arma_ignore(junk);
  84. typedef typename T1::pod_type T;
  85. const Proxy<T1> P(X);
  86. if(P.get_n_elem() == 0)
  87. {
  88. return T(0);
  89. }
  90. const char sig = (method != NULL) ? method[0] : char(0);
  91. const bool is_vec = (T1::is_xvec) || (T1::is_row) || (T1::is_col) || (P.get_n_rows() == 1) || (P.get_n_cols() == 1);
  92. if(is_vec)
  93. {
  94. if( (sig == 'i') || (sig == 'I') || (sig == '+') ) // max norm
  95. {
  96. return op_norm::vec_norm_max(P);
  97. }
  98. else
  99. if(sig == '-') // min norm
  100. {
  101. return op_norm::vec_norm_min(P);
  102. }
  103. else
  104. if( (sig == 'f') || (sig == 'F') )
  105. {
  106. return op_norm::vec_norm_2(P);
  107. }
  108. else
  109. {
  110. arma_stop_logic_error("norm(): unsupported vector norm type");
  111. return T(0);
  112. }
  113. }
  114. else
  115. {
  116. if( (sig == 'i') || (sig == 'I') || (sig == '+') ) // inf norm
  117. {
  118. return op_norm::mat_norm_inf(P);
  119. }
  120. else
  121. if( (sig == 'f') || (sig == 'F') )
  122. {
  123. return op_norm::vec_norm_2(P);
  124. }
  125. else
  126. {
  127. arma_stop_logic_error("norm(): unsupported matrix norm type");
  128. return T(0);
  129. }
  130. }
  131. }
  132. //
  133. // norms for sparse matrices
  134. template<typename T1>
  135. inline
  136. arma_warn_unused
  137. typename enable_if2< is_arma_sparse_type<T1>::value, typename T1::pod_type >::result
  138. norm
  139. (
  140. const T1& X,
  141. const uword k = uword(2),
  142. const typename arma_real_or_cx_only<typename T1::elem_type>::result* junk = 0
  143. )
  144. {
  145. arma_extra_debug_sigprint();
  146. arma_ignore(junk);
  147. typedef typename T1::elem_type eT;
  148. typedef typename T1::pod_type T;
  149. const SpProxy<T1> P(X);
  150. if(P.get_n_nonzero() == 0)
  151. {
  152. return T(0);
  153. }
  154. const bool is_vec = (P.get_n_rows() == 1) || (P.get_n_cols() == 1);
  155. if(is_vec)
  156. {
  157. const unwrap_spmat<typename SpProxy<T1>::stored_type> tmp(P.Q);
  158. const SpMat<eT>& A = tmp.M;
  159. // create a fake dense vector to allow reuse of code for dense vectors
  160. Col<eT> fake_vector( access::rwp(A.values), A.n_nonzero, false );
  161. const Proxy< Col<eT> > P_fake_vector(fake_vector);
  162. switch(k)
  163. {
  164. case 1:
  165. return op_norm::vec_norm_1(P_fake_vector);
  166. break;
  167. case 2:
  168. return op_norm::vec_norm_2(P_fake_vector);
  169. break;
  170. default:
  171. {
  172. arma_debug_check( (k == 0), "norm(): k must be greater than zero" );
  173. return op_norm::vec_norm_k(P_fake_vector, int(k));
  174. }
  175. }
  176. }
  177. else
  178. {
  179. switch(k)
  180. {
  181. case 1:
  182. return op_norm::mat_norm_1(P);
  183. break;
  184. case 2:
  185. return op_norm::mat_norm_2(P);
  186. break;
  187. default:
  188. arma_stop_logic_error("norm(): unsupported or unimplemented norm type for sparse matrices");
  189. return T(0);
  190. }
  191. }
  192. }
  193. template<typename T1>
  194. inline
  195. arma_warn_unused
  196. typename enable_if2< is_arma_sparse_type<T1>::value, typename T1::pod_type >::result
  197. norm
  198. (
  199. const T1& X,
  200. const char* method,
  201. const typename arma_real_or_cx_only<typename T1::elem_type>::result* junk = 0
  202. )
  203. {
  204. arma_extra_debug_sigprint();
  205. arma_ignore(junk);
  206. typedef typename T1::elem_type eT;
  207. typedef typename T1::pod_type T;
  208. const SpProxy<T1> P(X);
  209. if(P.get_n_nonzero() == 0)
  210. {
  211. return T(0);
  212. }
  213. const unwrap_spmat<typename SpProxy<T1>::stored_type> tmp(P.Q);
  214. const SpMat<eT>& A = tmp.M;
  215. // create a fake dense vector to allow reuse of code for dense vectors
  216. Col<eT> fake_vector( access::rwp(A.values), A.n_nonzero, false );
  217. const Proxy< Col<eT> > P_fake_vector(fake_vector);
  218. const char sig = (method != NULL) ? method[0] : char(0);
  219. const bool is_vec = (P.get_n_rows() == 1) || (P.get_n_cols() == 1); // TODO: (T1::is_row) || (T1::is_col) || ...
  220. if(is_vec)
  221. {
  222. if( (sig == 'i') || (sig == 'I') || (sig == '+') ) // max norm
  223. {
  224. return op_norm::vec_norm_max(P_fake_vector);
  225. }
  226. else
  227. if(sig == '-') // min norm
  228. {
  229. const T val = op_norm::vec_norm_min(P_fake_vector);
  230. if( P.get_n_nonzero() < P.get_n_elem() )
  231. {
  232. return (std::min)(T(0), val);
  233. }
  234. else
  235. {
  236. return val;
  237. }
  238. }
  239. else
  240. if( (sig == 'f') || (sig == 'F') )
  241. {
  242. return op_norm::vec_norm_2(P_fake_vector);
  243. }
  244. else
  245. {
  246. arma_stop_logic_error("norm(): unsupported vector norm type");
  247. return T(0);
  248. }
  249. }
  250. else
  251. {
  252. if( (sig == 'i') || (sig == 'I') || (sig == '+') ) // inf norm
  253. {
  254. return op_norm::mat_norm_inf(P);
  255. }
  256. else
  257. if( (sig == 'f') || (sig == 'F') )
  258. {
  259. return op_norm::vec_norm_2(P_fake_vector);
  260. }
  261. else
  262. {
  263. arma_stop_logic_error("norm(): unsupported matrix norm type");
  264. return T(0);
  265. }
  266. }
  267. }
  268. //! @}