fn_approx_equal.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_approx_equal
  16. //! @{
  17. template<typename eT>
  18. arma_inline
  19. bool
  20. internal_approx_equal_abs_diff(const eT& x, const eT& y, const typename get_pod_type<eT>::result tol)
  21. {
  22. typedef typename get_pod_type<eT>::result T;
  23. if(x != y)
  24. {
  25. if(is_real<T>::value) // also true for eT = std::complex<float> or eT = std::complex<double>
  26. {
  27. if( arma_isnan(x) || arma_isnan(y) || (eop_aux::arma_abs(x - y) > tol) ) { return false; }
  28. }
  29. else
  30. {
  31. if( eop_aux::arma_abs( ( cond_rel< is_cx<eT>::no >::gt(x, y) ) ? (x-y) : (y-x) ) > tol ) { return false; }
  32. }
  33. }
  34. return true;
  35. }
  36. template<typename eT>
  37. arma_inline
  38. bool
  39. internal_approx_equal_rel_diff(const eT& a, const eT& b, const typename get_pod_type<eT>::result tol)
  40. {
  41. typedef typename get_pod_type<eT>::result T;
  42. if(a != b)
  43. {
  44. if(is_real<T>::value) // also true for eT = std::complex<float> or eT = std::complex<double>
  45. {
  46. if( arma_isnan(a) || arma_isnan(b) ) { return false; }
  47. const T abs_a = eop_aux::arma_abs(a);
  48. const T abs_b = eop_aux::arma_abs(b);
  49. const T max_c = (std::max)(abs_a,abs_b);
  50. const T abs_d = eop_aux::arma_abs(a - b);
  51. if(max_c >= T(1))
  52. {
  53. if( abs_d > (tol * max_c) ) { return false; }
  54. }
  55. else
  56. {
  57. if( (abs_d / max_c) > tol ) { return false; }
  58. }
  59. }
  60. else
  61. {
  62. const T abs_a = eop_aux::arma_abs(a);
  63. const T abs_b = eop_aux::arma_abs(b);
  64. const T max_c = (std::max)(abs_a,abs_b);
  65. const T abs_d = eop_aux::arma_abs( ( cond_rel< is_cx<eT>::no >::gt(a, b) ) ? (a-b) : (b-a) );
  66. if( abs_d > (tol * max_c) ) { return false; }
  67. }
  68. }
  69. return true;
  70. }
  71. template<bool use_abs_diff, bool use_rel_diff, typename T1, typename T2>
  72. inline
  73. bool
  74. internal_approx_equal_worker
  75. (
  76. const Base<typename T1::elem_type,T1>& A,
  77. const Base<typename T1::elem_type,T2>& B,
  78. const typename T1::pod_type abs_tol,
  79. const typename T1::pod_type rel_tol
  80. )
  81. {
  82. arma_extra_debug_sigprint();
  83. typedef typename T1::elem_type eT;
  84. typedef typename T1::pod_type T;
  85. arma_debug_check( ((use_abs_diff == false) && (use_rel_diff == false)), "internal_approx_equal_worker(): both 'use_abs_diff' and 'use_rel_diff' are false" );
  86. if(use_abs_diff) { arma_debug_check( cond_rel< is_signed<T>::value >::lt(abs_tol, T(0)), "approx_equal(): argument 'abs_tol' must be >= 0" ); }
  87. if(use_rel_diff) { arma_debug_check( cond_rel< is_signed<T>::value >::lt(rel_tol, T(0)), "approx_equal(): argument 'rel_tol' must be >= 0" ); }
  88. const Proxy<T1> PA(A.get_ref());
  89. const Proxy<T2> PB(B.get_ref());
  90. if( (PA.get_n_rows() != PB.get_n_rows()) || (PA.get_n_cols() != PB.get_n_cols()) ) { return false; }
  91. if( (Proxy<T1>::use_at == false) && (Proxy<T2>::use_at == false) )
  92. {
  93. const uword N = PA.get_n_elem();
  94. const typename Proxy<T1>::ea_type PA_ea = PA.get_ea();
  95. const typename Proxy<T2>::ea_type PB_ea = PB.get_ea();
  96. for(uword i=0; i<N; ++i)
  97. {
  98. const eT x = PA_ea[i];
  99. const eT y = PB_ea[i];
  100. const bool state_abs_diff = (use_abs_diff) ? internal_approx_equal_abs_diff(x, y, abs_tol) : true;
  101. const bool state_rel_diff = (use_rel_diff) ? internal_approx_equal_rel_diff(x, y, rel_tol) : true;
  102. if(use_abs_diff && use_rel_diff)
  103. {
  104. if((state_abs_diff == false) && (state_rel_diff == false)) { return false; }
  105. }
  106. else
  107. if(use_abs_diff)
  108. {
  109. if(state_abs_diff == false) { return false; }
  110. }
  111. else
  112. if(use_rel_diff)
  113. {
  114. if(state_rel_diff == false) { return false; }
  115. }
  116. }
  117. }
  118. else
  119. {
  120. const uword n_rows = PA.get_n_rows();
  121. const uword n_cols = PA.get_n_cols();
  122. for(uword col=0; col < n_cols; ++col)
  123. for(uword row=0; row < n_rows; ++row)
  124. {
  125. const eT x = PA.at(row,col);
  126. const eT y = PB.at(row,col);
  127. const bool state_abs_diff = (use_abs_diff) ? internal_approx_equal_abs_diff(x, y, abs_tol) : true;
  128. const bool state_rel_diff = (use_rel_diff) ? internal_approx_equal_rel_diff(x, y, rel_tol) : true;
  129. if(use_abs_diff && use_rel_diff)
  130. {
  131. if((state_abs_diff == false) && (state_rel_diff == false)) { return false; }
  132. }
  133. else
  134. if(use_abs_diff)
  135. {
  136. if(state_abs_diff == false) { return false; }
  137. }
  138. else
  139. if(use_rel_diff)
  140. {
  141. if(state_rel_diff == false) { return false; }
  142. }
  143. }
  144. }
  145. return true;
  146. }
  147. template<bool use_abs_diff, bool use_rel_diff, typename T1, typename T2>
  148. inline
  149. bool
  150. internal_approx_equal_worker
  151. (
  152. const BaseCube<typename T1::elem_type,T1>& A,
  153. const BaseCube<typename T1::elem_type,T2>& B,
  154. const typename T1::pod_type abs_tol,
  155. const typename T1::pod_type rel_tol
  156. )
  157. {
  158. arma_extra_debug_sigprint();
  159. typedef typename T1::elem_type eT;
  160. typedef typename T1::pod_type T;
  161. arma_debug_check( ((use_abs_diff == false) && (use_rel_diff == false)), "internal_approx_equal_worker(): both 'use_abs_diff' and 'use_rel_diff' are false" );
  162. if(use_abs_diff) { arma_debug_check( cond_rel< is_signed<T>::value >::lt(abs_tol, T(0)), "approx_equal(): argument 'abs_tol' must be >= 0" ); }
  163. if(use_rel_diff) { arma_debug_check( cond_rel< is_signed<T>::value >::lt(rel_tol, T(0)), "approx_equal(): argument 'rel_tol' must be >= 0" ); }
  164. const ProxyCube<T1> PA(A.get_ref());
  165. const ProxyCube<T2> PB(B.get_ref());
  166. if( (PA.get_n_rows() != PB.get_n_rows()) || (PA.get_n_cols() != PB.get_n_cols()) || (PA.get_n_slices() != PB.get_n_slices()) ) { return false; }
  167. if( (ProxyCube<T1>::use_at == false) && (ProxyCube<T2>::use_at == false) )
  168. {
  169. const uword N = PA.get_n_elem();
  170. const typename ProxyCube<T1>::ea_type PA_ea = PA.get_ea();
  171. const typename ProxyCube<T2>::ea_type PB_ea = PB.get_ea();
  172. for(uword i=0; i<N; ++i)
  173. {
  174. const eT x = PA_ea[i];
  175. const eT y = PB_ea[i];
  176. const bool state_abs_diff = (use_abs_diff) ? internal_approx_equal_abs_diff(x, y, abs_tol) : true;
  177. const bool state_rel_diff = (use_rel_diff) ? internal_approx_equal_rel_diff(x, y, rel_tol) : true;
  178. if(use_abs_diff && use_rel_diff)
  179. {
  180. if((state_abs_diff == false) && (state_rel_diff == false)) { return false; }
  181. }
  182. else
  183. if(use_abs_diff)
  184. {
  185. if(state_abs_diff == false) { return false; }
  186. }
  187. else
  188. if(use_rel_diff)
  189. {
  190. if(state_rel_diff == false) { return false; }
  191. }
  192. }
  193. }
  194. else
  195. {
  196. const uword n_rows = PA.get_n_rows();
  197. const uword n_cols = PA.get_n_cols();
  198. const uword n_slices = PA.get_n_slices();
  199. for(uword slice=0; slice < n_slices; ++slice)
  200. for(uword col=0; col < n_cols; ++col )
  201. for(uword row=0; row < n_rows; ++row )
  202. {
  203. const eT x = PA.at(row,col,slice);
  204. const eT y = PB.at(row,col,slice);
  205. const bool state_abs_diff = (use_abs_diff) ? internal_approx_equal_abs_diff(x, y, abs_tol) : true;
  206. const bool state_rel_diff = (use_rel_diff) ? internal_approx_equal_rel_diff(x, y, rel_tol) : true;
  207. if(use_abs_diff && use_rel_diff)
  208. {
  209. if((state_abs_diff == false) && (state_rel_diff == false)) { return false; }
  210. }
  211. else
  212. if(use_abs_diff)
  213. {
  214. if(state_abs_diff == false) { return false; }
  215. }
  216. else
  217. if(use_rel_diff)
  218. {
  219. if(state_rel_diff == false) { return false; }
  220. }
  221. }
  222. }
  223. return true;
  224. }
  225. template<typename T1, typename T2>
  226. inline
  227. bool
  228. internal_approx_equal_handler(const T1& A, const T2& B, const char* method, const typename T1::pod_type abs_tol, const typename T1::pod_type rel_tol)
  229. {
  230. arma_extra_debug_sigprint();
  231. typedef typename T1::pod_type T;
  232. const char sig = (method != NULL) ? method[0] : char(0);
  233. arma_debug_check( ((sig != 'a') && (sig != 'r') && (sig != 'b')), "approx_equal(): argument 'method' must be \"absdiff\" or \"reldiff\" or \"both\"" );
  234. bool status = false;
  235. if(sig == 'a')
  236. {
  237. status = internal_approx_equal_worker<true,false>(A, B, abs_tol, T(0));
  238. }
  239. else
  240. if(sig == 'r')
  241. {
  242. status = internal_approx_equal_worker<false,true>(A, B, T(0), rel_tol);
  243. }
  244. else
  245. if(sig == 'b')
  246. {
  247. status = internal_approx_equal_worker<true,true>(A, B, abs_tol, rel_tol);
  248. }
  249. return status;
  250. }
  251. template<typename T1, typename T2>
  252. inline
  253. bool
  254. internal_approx_equal_handler(const T1& A, const T2& B, const char* method, const typename T1::pod_type tol)
  255. {
  256. arma_extra_debug_sigprint();
  257. typedef typename T1::pod_type T;
  258. const char sig = (method != NULL) ? method[0] : char(0);
  259. arma_debug_check( ((sig != 'a') && (sig != 'r') && (sig != 'b')), "approx_equal(): argument 'method' must be \"absdiff\" or \"reldiff\" or \"both\"" );
  260. arma_debug_check( (sig == 'b'), "approx_equal(): argument 'method' is \"both\", but only one 'tol' argument has been given" );
  261. bool status = false;
  262. if(sig == 'a')
  263. {
  264. status = internal_approx_equal_worker<true,false>(A, B, tol, T(0));
  265. }
  266. else
  267. if(sig == 'r')
  268. {
  269. status = internal_approx_equal_worker<false,true>(A, B, T(0), tol);
  270. }
  271. return status;
  272. }
  273. template<typename T1, typename T2>
  274. arma_warn_unused
  275. inline
  276. bool
  277. approx_equal(const Base<typename T1::elem_type,T1>& A, const Base<typename T1::elem_type,T2>& B, const char* method, const typename T1::pod_type tol)
  278. {
  279. arma_extra_debug_sigprint();
  280. return internal_approx_equal_handler(A.get_ref(), B.get_ref(), method, tol);
  281. }
  282. template<typename T1, typename T2>
  283. arma_warn_unused
  284. inline
  285. bool
  286. approx_equal(const BaseCube<typename T1::elem_type,T1>& A, const BaseCube<typename T1::elem_type,T2>& B, const char* method, const typename T1::pod_type tol)
  287. {
  288. arma_extra_debug_sigprint();
  289. return internal_approx_equal_handler(A.get_ref(), B.get_ref(), method, tol);
  290. }
  291. template<typename T1, typename T2>
  292. arma_warn_unused
  293. inline
  294. bool
  295. approx_equal(const Base<typename T1::elem_type,T1>& A, const Base<typename T1::elem_type,T2>& B, const char* method, const typename T1::pod_type abs_tol, const typename T1::pod_type rel_tol)
  296. {
  297. arma_extra_debug_sigprint();
  298. return internal_approx_equal_handler(A.get_ref(), B.get_ref(), method, abs_tol, rel_tol);
  299. }
  300. template<typename T1, typename T2>
  301. arma_warn_unused
  302. inline
  303. bool
  304. approx_equal(const BaseCube<typename T1::elem_type,T1>& A, const BaseCube<typename T1::elem_type,T2>& B, const char* method, const typename T1::pod_type abs_tol, const typename T1::pod_type rel_tol)
  305. {
  306. arma_extra_debug_sigprint();
  307. return internal_approx_equal_handler(A.get_ref(), B.get_ref(), method, abs_tol, rel_tol);
  308. }
  309. template<typename T1, typename T2>
  310. arma_warn_unused
  311. inline
  312. bool
  313. approx_equal(const SpBase<typename T1::elem_type,T1>& A, const SpBase<typename T1::elem_type,T2>& B, const char* method, const typename T1::pod_type tol)
  314. {
  315. arma_extra_debug_sigprint();
  316. typedef typename T1::elem_type eT;
  317. typedef typename T1::pod_type T;
  318. const char sig = (method != NULL) ? method[0] : char(0);
  319. arma_debug_check( ((sig != 'a') && (sig != 'r') && (sig != 'b')), "approx_equal(): argument 'method' must be \"absdiff\" or \"reldiff\" or \"both\"" );
  320. arma_debug_check( (sig == 'b'), "approx_equal(): argument 'method' is \"both\", but only one 'tol' argument has been given" );
  321. arma_debug_check( (sig == 'r'), "approx_equal(): only the \"absdiff\" method is currently implemented for sparse matrices" );
  322. arma_debug_check( cond_rel< is_signed<T>::value >::lt(tol, T(0)), "approx_equal(): argument 'tol' must be >= 0" );
  323. const unwrap_spmat<T1> UA(A.get_ref());
  324. const unwrap_spmat<T2> UB(B.get_ref());
  325. if( (UA.M.n_rows != UB.M.n_rows) || (UA.M.n_cols != UB.M.n_cols) ) { return false; }
  326. const SpMat<eT> C = UA.M - UB.M;
  327. typename SpMat<eT>::const_iterator it = C.begin();
  328. typename SpMat<eT>::const_iterator it_end = C.end();
  329. while(it != it_end)
  330. {
  331. const eT val = (*it);
  332. if( arma_isnan(val) || (eop_aux::arma_abs(val) > tol) ) { return false; }
  333. ++it;
  334. }
  335. return true;
  336. }
  337. template<typename T1, typename T2>
  338. arma_warn_unused
  339. inline
  340. bool
  341. approx_equal(const SpBase<typename T1::elem_type,T1>& A, const SpBase<typename T1::elem_type,T2>& B, const char* method, const typename T1::pod_type abs_tol, const typename T1::pod_type rel_tol)
  342. {
  343. arma_extra_debug_sigprint();
  344. typedef typename T1::pod_type T;
  345. const char sig = (method != NULL) ? method[0] : char(0);
  346. arma_debug_check( ((sig != 'a') && (sig != 'r') && (sig != 'b')), "approx_equal(): argument 'method' must be \"absdiff\" or \"reldiff\" or \"both\"" );
  347. arma_debug_check( ((sig == 'r') || (sig == 'b')), "approx_equal(): only the \"absdiff\" method is currently implemented for sparse matrices" );
  348. arma_debug_check( cond_rel< is_signed<T>::value >::lt(abs_tol, T(0)), "approx_equal(): argument 'abs_tol' must be >= 0" );
  349. arma_debug_check( cond_rel< is_signed<T>::value >::lt(rel_tol, T(0)), "approx_equal(): argument 'rel_tol' must be >= 0" );
  350. return approx_equal(A.get_ref(), B.get_ref(), "abs", abs_tol);
  351. }
  352. //! @}