SpBase_meat.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 SpBase
  16. //! @{
  17. template<typename elem_type, typename derived>
  18. arma_inline
  19. const derived&
  20. SpBase<elem_type,derived>::get_ref() const
  21. {
  22. return static_cast<const derived&>(*this);
  23. }
  24. template<typename elem_type, typename derived>
  25. arma_inline
  26. bool
  27. SpBase<elem_type,derived>::is_alias(const SpMat<elem_type>& X) const
  28. {
  29. return (*this).get_ref().is_alias(X);
  30. }
  31. template<typename elem_type, typename derived>
  32. inline
  33. const SpOp<derived, spop_htrans>
  34. SpBase<elem_type,derived>::t() const
  35. {
  36. return SpOp<derived,spop_htrans>( (*this).get_ref() );
  37. }
  38. template<typename elem_type, typename derived>
  39. inline
  40. const SpOp<derived, spop_htrans>
  41. SpBase<elem_type,derived>::ht() const
  42. {
  43. return SpOp<derived, spop_htrans>( (*this).get_ref() );
  44. }
  45. template<typename elem_type, typename derived>
  46. inline
  47. const SpOp<derived, spop_strans>
  48. SpBase<elem_type,derived>::st() const
  49. {
  50. return SpOp<derived, spop_strans>( (*this).get_ref() );
  51. }
  52. template<typename elem_type, typename derived>
  53. arma_cold
  54. inline
  55. void
  56. SpBase<elem_type,derived>::print(const std::string extra_text) const
  57. {
  58. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  59. tmp.M.impl_print(extra_text);
  60. }
  61. template<typename elem_type, typename derived>
  62. arma_cold
  63. inline
  64. void
  65. SpBase<elem_type,derived>::print(std::ostream& user_stream, const std::string extra_text) const
  66. {
  67. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  68. tmp.M.impl_print(user_stream, extra_text);
  69. }
  70. template<typename elem_type, typename derived>
  71. arma_cold
  72. inline
  73. void
  74. SpBase<elem_type,derived>::raw_print(const std::string extra_text) const
  75. {
  76. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  77. tmp.M.impl_raw_print(extra_text);
  78. }
  79. template<typename elem_type, typename derived>
  80. arma_cold
  81. inline
  82. void
  83. SpBase<elem_type,derived>::raw_print(std::ostream& user_stream, const std::string extra_text) const
  84. {
  85. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  86. tmp.M.impl_raw_print(user_stream, extra_text);
  87. }
  88. template<typename elem_type, typename derived>
  89. arma_cold
  90. inline
  91. void
  92. SpBase<elem_type, derived>::print_dense(const std::string extra_text) const
  93. {
  94. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  95. tmp.M.impl_print_dense(extra_text);
  96. }
  97. template<typename elem_type, typename derived>
  98. arma_cold
  99. inline
  100. void
  101. SpBase<elem_type, derived>::print_dense(std::ostream& user_stream, const std::string extra_text) const
  102. {
  103. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  104. tmp.M.impl_print_dense(user_stream, extra_text);
  105. }
  106. template<typename elem_type, typename derived>
  107. arma_cold
  108. inline
  109. void
  110. SpBase<elem_type, derived>::raw_print_dense(const std::string extra_text) const
  111. {
  112. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  113. tmp.M.impl_raw_print_dense(extra_text);
  114. }
  115. template<typename elem_type, typename derived>
  116. arma_cold
  117. inline
  118. void
  119. SpBase<elem_type, derived>::raw_print_dense(std::ostream& user_stream, const std::string extra_text) const
  120. {
  121. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  122. tmp.M.impl_raw_print_dense(user_stream, extra_text);
  123. }
  124. //
  125. // extra functions defined in SpBase_eval_SpMat
  126. template<typename elem_type, typename derived>
  127. inline
  128. const derived&
  129. SpBase_eval_SpMat<elem_type, derived>::eval() const
  130. {
  131. arma_extra_debug_sigprint();
  132. return static_cast<const derived&>(*this);
  133. }
  134. //
  135. // extra functions defined in SpBase_eval_expr
  136. template<typename elem_type, typename derived>
  137. inline
  138. SpMat<elem_type>
  139. SpBase_eval_expr<elem_type, derived>::eval() const
  140. {
  141. arma_extra_debug_sigprint();
  142. return SpMat<elem_type>( static_cast<const derived&>(*this) );
  143. }
  144. template<typename elem_type, typename derived>
  145. inline
  146. arma_warn_unused
  147. elem_type
  148. SpBase<elem_type, derived>::min() const
  149. {
  150. return spop_min::min( (*this).get_ref() );
  151. }
  152. template<typename elem_type, typename derived>
  153. inline
  154. arma_warn_unused
  155. elem_type
  156. SpBase<elem_type, derived>::max() const
  157. {
  158. return spop_max::max( (*this).get_ref() );
  159. }
  160. template<typename elem_type, typename derived>
  161. inline
  162. elem_type
  163. SpBase<elem_type, derived>::min(uword& index_of_min_val) const
  164. {
  165. const SpProxy<derived> P( (*this).get_ref() );
  166. return spop_min::min_with_index(P, index_of_min_val);
  167. }
  168. template<typename elem_type, typename derived>
  169. inline
  170. elem_type
  171. SpBase<elem_type, derived>::max(uword& index_of_max_val) const
  172. {
  173. const SpProxy<derived> P( (*this).get_ref() );
  174. return spop_max::max_with_index(P, index_of_max_val);
  175. }
  176. template<typename elem_type, typename derived>
  177. inline
  178. elem_type
  179. SpBase<elem_type, derived>::min(uword& row_of_min_val, uword& col_of_min_val) const
  180. {
  181. const SpProxy<derived> P( (*this).get_ref() );
  182. uword index = 0;
  183. const elem_type val = spop_min::min_with_index(P, index);
  184. const uword local_n_rows = P.get_n_rows();
  185. row_of_min_val = index % local_n_rows;
  186. col_of_min_val = index / local_n_rows;
  187. return val;
  188. }
  189. template<typename elem_type, typename derived>
  190. inline
  191. elem_type
  192. SpBase<elem_type, derived>::max(uword& row_of_max_val, uword& col_of_max_val) const
  193. {
  194. const SpProxy<derived> P( (*this).get_ref() );
  195. uword index = 0;
  196. const elem_type val = spop_max::max_with_index(P, index);
  197. const uword local_n_rows = P.get_n_rows();
  198. row_of_max_val = index % local_n_rows;
  199. col_of_max_val = index / local_n_rows;
  200. return val;
  201. }
  202. template<typename elem_type, typename derived>
  203. inline
  204. arma_warn_unused
  205. uword
  206. SpBase<elem_type,derived>::index_min() const
  207. {
  208. const SpProxy<derived> P( (*this).get_ref() );
  209. uword index = 0;
  210. if(P.get_n_elem() == 0)
  211. {
  212. arma_debug_check(true, "index_min(): object has no elements");
  213. }
  214. else
  215. {
  216. spop_min::min_with_index(P, index);
  217. }
  218. return index;
  219. }
  220. template<typename elem_type, typename derived>
  221. inline
  222. arma_warn_unused
  223. uword
  224. SpBase<elem_type,derived>::index_max() const
  225. {
  226. const SpProxy<derived> P( (*this).get_ref() );
  227. uword index = 0;
  228. if(P.get_n_elem() == 0)
  229. {
  230. arma_debug_check(true, "index_max(): object has no elements");
  231. }
  232. else
  233. {
  234. spop_max::max_with_index(P, index);
  235. }
  236. return index;
  237. }
  238. template<typename elem_type, typename derived>
  239. inline
  240. arma_warn_unused
  241. bool
  242. SpBase<elem_type,derived>::is_symmetric() const
  243. {
  244. arma_extra_debug_sigprint();
  245. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  246. return tmp.M.is_symmetric();
  247. }
  248. template<typename elem_type, typename derived>
  249. inline
  250. arma_warn_unused
  251. bool
  252. SpBase<elem_type,derived>::is_symmetric(const typename get_pod_type<elem_type>::result tol) const
  253. {
  254. arma_extra_debug_sigprint();
  255. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  256. return tmp.M.is_symmetric(tol);
  257. }
  258. template<typename elem_type, typename derived>
  259. inline
  260. arma_warn_unused
  261. bool
  262. SpBase<elem_type,derived>::is_hermitian() const
  263. {
  264. arma_extra_debug_sigprint();
  265. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  266. return tmp.M.is_hermitian();
  267. }
  268. template<typename elem_type, typename derived>
  269. inline
  270. arma_warn_unused
  271. bool
  272. SpBase<elem_type,derived>::is_hermitian(const typename get_pod_type<elem_type>::result tol) const
  273. {
  274. arma_extra_debug_sigprint();
  275. const unwrap_spmat<derived> tmp( (*this).get_ref() );
  276. return tmp.M.is_hermitian(tol);
  277. }
  278. template<typename elem_type, typename derived>
  279. inline
  280. arma_warn_unused
  281. bool
  282. SpBase<elem_type,derived>::is_zero(const typename get_pod_type<elem_type>::result tol) const
  283. {
  284. arma_extra_debug_sigprint();
  285. typedef typename get_pod_type<elem_type>::result T;
  286. arma_debug_check( (tol < T(0)), "is_zero(): parameter 'tol' must be >= 0" );
  287. const SpProxy<derived> P( (*this).get_ref() );
  288. if(P.get_n_elem() == 0) { return false; }
  289. if(P.get_n_nonzero() == 0) { return true; }
  290. if(is_SpMat<typename SpProxy<derived>::stored_type>::value)
  291. {
  292. const unwrap_spmat<typename SpProxy<derived>::stored_type> U(P.Q);
  293. return arrayops::is_zero(U.M.values, U.M.n_nonzero, tol);
  294. }
  295. typename SpProxy<derived>::const_iterator_type it = P.begin();
  296. typename SpProxy<derived>::const_iterator_type it_end = P.end();
  297. if(is_cx<elem_type>::yes)
  298. {
  299. while(it != it_end)
  300. {
  301. const elem_type val = (*it);
  302. const T val_real = access::tmp_real(val);
  303. const T val_imag = access::tmp_imag(val);
  304. if(eop_aux::arma_abs(val_real) > tol) { return false; }
  305. if(eop_aux::arma_abs(val_imag) > tol) { return false; }
  306. ++it;
  307. }
  308. }
  309. else // not complex
  310. {
  311. while(it != it_end)
  312. {
  313. if(eop_aux::arma_abs(*it) > tol) { return false; }
  314. ++it;
  315. }
  316. }
  317. return true;
  318. }
  319. template<typename elem_type, typename derived>
  320. inline
  321. arma_warn_unused
  322. bool
  323. SpBase<elem_type,derived>::is_trimatu() const
  324. {
  325. arma_extra_debug_sigprint();
  326. const SpProxy<derived> P( (*this).get_ref() );
  327. if(P.get_n_rows() != P.get_n_cols()) { return false; }
  328. typename SpProxy<derived>::const_iterator_type it = P.begin();
  329. typename SpProxy<derived>::const_iterator_type it_end = P.end();
  330. while(it != it_end)
  331. {
  332. if(it.row() > it.col()) { return false; }
  333. ++it;
  334. }
  335. return true;
  336. }
  337. template<typename elem_type, typename derived>
  338. inline
  339. arma_warn_unused
  340. bool
  341. SpBase<elem_type,derived>::is_trimatl() const
  342. {
  343. arma_extra_debug_sigprint();
  344. const SpProxy<derived> P( (*this).get_ref() );
  345. if(P.get_n_rows() != P.get_n_cols()) { return false; }
  346. typename SpProxy<derived>::const_iterator_type it = P.begin();
  347. typename SpProxy<derived>::const_iterator_type it_end = P.end();
  348. while(it != it_end)
  349. {
  350. if(it.row() < it.col()) { return false; }
  351. ++it;
  352. }
  353. return true;
  354. }
  355. template<typename elem_type, typename derived>
  356. inline
  357. arma_warn_unused
  358. bool
  359. SpBase<elem_type,derived>::is_diagmat() const
  360. {
  361. arma_extra_debug_sigprint();
  362. const SpProxy<derived> P( (*this).get_ref() );
  363. typename SpProxy<derived>::const_iterator_type it = P.begin();
  364. typename SpProxy<derived>::const_iterator_type it_end = P.end();
  365. while(it != it_end)
  366. {
  367. if(it.row() != it.col()) { return false; }
  368. ++it;
  369. }
  370. return true;
  371. }
  372. template<typename elem_type, typename derived>
  373. inline
  374. arma_warn_unused
  375. bool
  376. SpBase<elem_type,derived>::is_empty() const
  377. {
  378. arma_extra_debug_sigprint();
  379. const SpProxy<derived> P( (*this).get_ref() );
  380. return (P.get_n_elem() == uword(0));
  381. }
  382. template<typename elem_type, typename derived>
  383. inline
  384. arma_warn_unused
  385. bool
  386. SpBase<elem_type,derived>::is_square() const
  387. {
  388. arma_extra_debug_sigprint();
  389. const SpProxy<derived> P( (*this).get_ref() );
  390. return (P.get_n_rows() == P.get_n_cols());
  391. }
  392. template<typename elem_type, typename derived>
  393. inline
  394. arma_warn_unused
  395. bool
  396. SpBase<elem_type,derived>::is_vec() const
  397. {
  398. arma_extra_debug_sigprint();
  399. if( (SpProxy<derived>::is_row) || (SpProxy<derived>::is_col) || (SpProxy<derived>::is_xvec) ) { return true; }
  400. const SpProxy<derived> P( (*this).get_ref() );
  401. return ( (P.get_n_rows() == uword(1)) || (P.get_n_cols() == uword(1)) );
  402. }
  403. template<typename elem_type, typename derived>
  404. inline
  405. arma_warn_unused
  406. bool
  407. SpBase<elem_type,derived>::is_colvec() const
  408. {
  409. arma_extra_debug_sigprint();
  410. if(SpProxy<derived>::is_col) { return true; }
  411. const SpProxy<derived> P( (*this).get_ref() );
  412. return (P.get_n_cols() == uword(1));
  413. }
  414. template<typename elem_type, typename derived>
  415. inline
  416. arma_warn_unused
  417. bool
  418. SpBase<elem_type,derived>::is_rowvec() const
  419. {
  420. arma_extra_debug_sigprint();
  421. if(SpProxy<derived>::is_row) { return true; }
  422. const SpProxy<derived> P( (*this).get_ref() );
  423. return (P.get_n_rows() == uword(1));
  424. }
  425. template<typename elem_type, typename derived>
  426. inline
  427. arma_warn_unused
  428. bool
  429. SpBase<elem_type,derived>::is_finite() const
  430. {
  431. arma_extra_debug_sigprint();
  432. const SpProxy<derived> P( (*this).get_ref() );
  433. if(is_SpMat<typename SpProxy<derived>::stored_type>::value)
  434. {
  435. const unwrap_spmat<typename SpProxy<derived>::stored_type> U(P.Q);
  436. return U.M.is_finite();
  437. }
  438. else
  439. {
  440. typename SpProxy<derived>::const_iterator_type it = P.begin();
  441. typename SpProxy<derived>::const_iterator_type it_end = P.end();
  442. while(it != it_end)
  443. {
  444. if(arma_isfinite(*it) == false) { return false; }
  445. ++it;
  446. }
  447. }
  448. return true;
  449. }
  450. template<typename elem_type, typename derived>
  451. inline
  452. arma_warn_unused
  453. bool
  454. SpBase<elem_type,derived>::has_inf() const
  455. {
  456. arma_extra_debug_sigprint();
  457. const SpProxy<derived> P( (*this).get_ref() );
  458. if(is_SpMat<typename SpProxy<derived>::stored_type>::value)
  459. {
  460. const unwrap_spmat<typename SpProxy<derived>::stored_type> U(P.Q);
  461. return U.M.has_inf();
  462. }
  463. else
  464. {
  465. typename SpProxy<derived>::const_iterator_type it = P.begin();
  466. typename SpProxy<derived>::const_iterator_type it_end = P.end();
  467. while(it != it_end)
  468. {
  469. if(arma_isinf(*it)) { return true; }
  470. ++it;
  471. }
  472. }
  473. return false;
  474. }
  475. template<typename elem_type, typename derived>
  476. inline
  477. arma_warn_unused
  478. bool
  479. SpBase<elem_type,derived>::has_nan() const
  480. {
  481. arma_extra_debug_sigprint();
  482. const SpProxy<derived> P( (*this).get_ref() );
  483. if(is_SpMat<typename SpProxy<derived>::stored_type>::value)
  484. {
  485. const unwrap_spmat<typename SpProxy<derived>::stored_type> U(P.Q);
  486. return U.M.has_nan();
  487. }
  488. else
  489. {
  490. typename SpProxy<derived>::const_iterator_type it = P.begin();
  491. typename SpProxy<derived>::const_iterator_type it_end = P.end();
  492. while(it != it_end)
  493. {
  494. if(arma_isnan(*it)) { return true; }
  495. ++it;
  496. }
  497. }
  498. return false;
  499. }
  500. template<typename elem_type, typename derived>
  501. inline
  502. arma_warn_unused
  503. const SpOp<derived,spop_vectorise_col>
  504. SpBase<elem_type, derived>::as_col() const
  505. {
  506. return SpOp<derived,spop_vectorise_col>( (*this).get_ref() );
  507. }
  508. template<typename elem_type, typename derived>
  509. inline
  510. arma_warn_unused
  511. const SpOp<derived,spop_vectorise_row>
  512. SpBase<elem_type, derived>::as_row() const
  513. {
  514. return SpOp<derived,spop_vectorise_row>( (*this).get_ref() );
  515. }
  516. //! @}