BaseCube_meat.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 BaseCube
  16. //! @{
  17. template<typename elem_type, typename derived>
  18. arma_inline
  19. const derived&
  20. BaseCube<elem_type,derived>::get_ref() const
  21. {
  22. return static_cast<const derived&>(*this);
  23. }
  24. template<typename elem_type, typename derived>
  25. arma_cold
  26. inline
  27. void
  28. BaseCube<elem_type,derived>::print(const std::string extra_text) const
  29. {
  30. const unwrap_cube<derived> tmp( (*this).get_ref() );
  31. tmp.M.impl_print(extra_text);
  32. }
  33. template<typename elem_type, typename derived>
  34. arma_cold
  35. inline
  36. void
  37. BaseCube<elem_type,derived>::print(std::ostream& user_stream, const std::string extra_text) const
  38. {
  39. const unwrap_cube<derived> tmp( (*this).get_ref() );
  40. tmp.M.impl_print(user_stream, extra_text);
  41. }
  42. template<typename elem_type, typename derived>
  43. arma_cold
  44. inline
  45. void
  46. BaseCube<elem_type,derived>::raw_print(const std::string extra_text) const
  47. {
  48. const unwrap_cube<derived> tmp( (*this).get_ref() );
  49. tmp.M.impl_raw_print(extra_text);
  50. }
  51. template<typename elem_type, typename derived>
  52. arma_cold
  53. inline
  54. void
  55. BaseCube<elem_type,derived>::raw_print(std::ostream& user_stream, const std::string extra_text) const
  56. {
  57. const unwrap_cube<derived> tmp( (*this).get_ref() );
  58. tmp.M.impl_raw_print(user_stream, extra_text);
  59. }
  60. template<typename elem_type, typename derived>
  61. inline
  62. arma_warn_unused
  63. elem_type
  64. BaseCube<elem_type,derived>::min() const
  65. {
  66. return op_min::min( (*this).get_ref() );
  67. }
  68. template<typename elem_type, typename derived>
  69. inline
  70. arma_warn_unused
  71. elem_type
  72. BaseCube<elem_type,derived>::max() const
  73. {
  74. return op_max::max( (*this).get_ref() );
  75. }
  76. template<typename elem_type, typename derived>
  77. inline
  78. arma_warn_unused
  79. uword
  80. BaseCube<elem_type,derived>::index_min() const
  81. {
  82. const ProxyCube<derived> P( (*this).get_ref() );
  83. uword index = 0;
  84. if(P.get_n_elem() == 0)
  85. {
  86. arma_debug_check(true, "index_min(): object has no elements");
  87. }
  88. else
  89. {
  90. op_min::min_with_index(P, index);
  91. }
  92. return index;
  93. }
  94. template<typename elem_type, typename derived>
  95. inline
  96. arma_warn_unused
  97. uword
  98. BaseCube<elem_type,derived>::index_max() const
  99. {
  100. const ProxyCube<derived> P( (*this).get_ref() );
  101. uword index = 0;
  102. if(P.get_n_elem() == 0)
  103. {
  104. arma_debug_check(true, "index_max(): object has no elements");
  105. }
  106. else
  107. {
  108. op_max::max_with_index(P, index);
  109. }
  110. return index;
  111. }
  112. template<typename elem_type, typename derived>
  113. inline
  114. arma_warn_unused
  115. bool
  116. BaseCube<elem_type,derived>::is_zero(const typename get_pod_type<elem_type>::result tol) const
  117. {
  118. arma_extra_debug_sigprint();
  119. typedef typename get_pod_type<elem_type>::result T;
  120. arma_debug_check( (tol < T(0)), "is_zero(): parameter 'tol' must be >= 0" );
  121. if(ProxyCube<derived>::use_at || is_Cube<typename ProxyCube<derived>::stored_type>::value)
  122. {
  123. const unwrap_cube<derived> U( (*this).get_ref() );
  124. return arrayops::is_zero( U.M.memptr(), U.M.n_elem, tol );
  125. }
  126. const ProxyCube<derived> P( (*this).get_ref() );
  127. const uword n_elem = P.get_n_elem();
  128. if(n_elem == 0) { return false; }
  129. const typename ProxyCube<derived>::ea_type Pea = P.get_ea();
  130. if(is_cx<elem_type>::yes)
  131. {
  132. for(uword i=0; i<n_elem; ++i)
  133. {
  134. const elem_type val = Pea[i];
  135. const T val_real = access::tmp_real(val);
  136. const T val_imag = access::tmp_imag(val);
  137. if(eop_aux::arma_abs(val_real) > tol) { return false; }
  138. if(eop_aux::arma_abs(val_imag) > tol) { return false; }
  139. }
  140. }
  141. else // not complex
  142. {
  143. for(uword i=0; i < n_elem; ++i)
  144. {
  145. if(eop_aux::arma_abs(Pea[i]) > tol) { return false; }
  146. }
  147. }
  148. return true;
  149. }
  150. template<typename elem_type, typename derived>
  151. inline
  152. arma_warn_unused
  153. bool
  154. BaseCube<elem_type,derived>::is_empty() const
  155. {
  156. arma_extra_debug_sigprint();
  157. const ProxyCube<derived> P( (*this).get_ref() );
  158. return (P.get_n_elem() == uword(0));
  159. }
  160. template<typename elem_type, typename derived>
  161. inline
  162. arma_warn_unused
  163. bool
  164. BaseCube<elem_type,derived>::is_finite() const
  165. {
  166. arma_extra_debug_sigprint();
  167. const ProxyCube<derived> P( (*this).get_ref() );
  168. if(is_Cube<typename ProxyCube<derived>::stored_type>::value)
  169. {
  170. const unwrap_cube<typename ProxyCube<derived>::stored_type> U(P.Q);
  171. return arrayops::is_finite( U.M.memptr(), U.M.n_elem );
  172. }
  173. const uword n_r = P.get_n_rows();
  174. const uword n_c = P.get_n_cols();
  175. const uword n_s = P.get_n_slices();
  176. for(uword s=0; s<n_s; ++s)
  177. for(uword c=0; c<n_c; ++c)
  178. for(uword r=0; r<n_r; ++r)
  179. {
  180. if( arma_isfinite(P.at(r,c,s)) == false ) { return false; }
  181. }
  182. return true;
  183. }
  184. template<typename elem_type, typename derived>
  185. inline
  186. arma_warn_unused
  187. bool
  188. BaseCube<elem_type,derived>::has_inf() const
  189. {
  190. arma_extra_debug_sigprint();
  191. const ProxyCube<derived> P( (*this).get_ref() );
  192. if(is_Cube<typename ProxyCube<derived>::stored_type>::value)
  193. {
  194. const unwrap_cube<typename ProxyCube<derived>::stored_type> U(P.Q);
  195. return arrayops::has_inf( U.M.memptr(), U.M.n_elem );
  196. }
  197. const uword n_r = P.get_n_rows();
  198. const uword n_c = P.get_n_cols();
  199. const uword n_s = P.get_n_slices();
  200. for(uword s=0; s<n_s; ++s)
  201. for(uword c=0; c<n_c; ++c)
  202. for(uword r=0; r<n_r; ++r)
  203. {
  204. if(arma_isinf(P.at(r,c,s))) { return true; }
  205. }
  206. return false;
  207. }
  208. template<typename elem_type, typename derived>
  209. inline
  210. arma_warn_unused
  211. bool
  212. BaseCube<elem_type,derived>::has_nan() const
  213. {
  214. arma_extra_debug_sigprint();
  215. const ProxyCube<derived> P( (*this).get_ref() );
  216. if(is_Cube<typename ProxyCube<derived>::stored_type>::value)
  217. {
  218. const unwrap_cube<typename ProxyCube<derived>::stored_type> U(P.Q);
  219. return arrayops::has_nan( U.M.memptr(), U.M.n_elem );
  220. }
  221. const uword n_r = P.get_n_rows();
  222. const uword n_c = P.get_n_cols();
  223. const uword n_s = P.get_n_slices();
  224. for(uword s=0; s<n_s; ++s)
  225. for(uword c=0; c<n_c; ++c)
  226. for(uword r=0; r<n_r; ++r)
  227. {
  228. if(arma_isnan(P.at(r,c,s))) { return true; }
  229. }
  230. return false;
  231. }
  232. //
  233. // extra functions defined in BaseCube_eval_Cube
  234. template<typename elem_type, typename derived>
  235. arma_inline
  236. const derived&
  237. BaseCube_eval_Cube<elem_type, derived>::eval() const
  238. {
  239. arma_extra_debug_sigprint();
  240. return static_cast<const derived&>(*this);
  241. }
  242. //
  243. // extra functions defined in BaseCube_eval_expr
  244. template<typename elem_type, typename derived>
  245. arma_inline
  246. Cube<elem_type>
  247. BaseCube_eval_expr<elem_type, derived>::eval() const
  248. {
  249. arma_extra_debug_sigprint();
  250. return Cube<elem_type>( static_cast<const derived&>(*this) );
  251. }
  252. //! @}