debug.hpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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 debug
  16. //! @{
  17. template<typename T>
  18. inline
  19. std::ostream&
  20. arma_cout_stream(std::ostream* user_stream)
  21. {
  22. static std::ostream* cout_stream = &(ARMA_COUT_STREAM);
  23. if(user_stream != NULL) { cout_stream = user_stream; }
  24. return (*cout_stream);
  25. }
  26. template<typename T>
  27. inline
  28. std::ostream&
  29. arma_cerr_stream(std::ostream* user_stream)
  30. {
  31. static std::ostream* cerr_stream = &(ARMA_CERR_STREAM);
  32. if(user_stream != NULL) { cerr_stream = user_stream; }
  33. return (*cerr_stream);
  34. }
  35. inline
  36. void
  37. set_cout_stream(std::ostream& user_stream)
  38. {
  39. arma_cout_stream<char>(&user_stream);
  40. }
  41. inline
  42. void
  43. set_cerr_stream(std::ostream& user_stream)
  44. {
  45. arma_cerr_stream<char>(&user_stream);
  46. }
  47. inline
  48. std::ostream&
  49. get_cout_stream()
  50. {
  51. return arma_cout_stream<char>(NULL);
  52. }
  53. inline
  54. std::ostream&
  55. get_cerr_stream()
  56. {
  57. return arma_cerr_stream<char>(NULL);
  58. }
  59. //! do not use this function - it's deprecated and will be removed
  60. inline
  61. arma_deprecated
  62. void
  63. set_stream_err1(std::ostream& user_stream)
  64. {
  65. set_cerr_stream(user_stream);
  66. }
  67. //! do not use this function - it's deprecated and will be removed
  68. inline
  69. arma_deprecated
  70. void
  71. set_stream_err2(std::ostream& user_stream)
  72. {
  73. set_cerr_stream(user_stream);
  74. }
  75. //! do not use this function - it's deprecated and will be removed
  76. inline
  77. arma_deprecated
  78. std::ostream&
  79. get_stream_err1()
  80. {
  81. return get_cerr_stream();
  82. }
  83. //! do not use this function - it's deprecated and will be removed
  84. inline
  85. arma_deprecated
  86. std::ostream&
  87. get_stream_err2()
  88. {
  89. return get_cerr_stream();
  90. }
  91. //! print a message to get_cerr_stream() and throw logic_error exception
  92. template<typename T1>
  93. arma_cold
  94. arma_noinline
  95. static
  96. void
  97. arma_stop_logic_error(const T1& x)
  98. {
  99. #if defined(ARMA_PRINT_ERRORS)
  100. {
  101. get_cerr_stream() << "\nerror: " << x << std::endl;
  102. }
  103. #endif
  104. throw std::logic_error( std::string(x) );
  105. }
  106. //! print a message to get_cerr_stream() and throw bad_alloc exception
  107. template<typename T1>
  108. arma_cold
  109. arma_noinline
  110. static
  111. void
  112. arma_stop_bad_alloc(const T1& x)
  113. {
  114. #if defined(ARMA_PRINT_ERRORS)
  115. {
  116. get_cerr_stream() << "\nerror: " << x << std::endl;
  117. }
  118. #else
  119. {
  120. arma_ignore(x);
  121. }
  122. #endif
  123. throw std::bad_alloc();
  124. }
  125. //! print a message to get_cerr_stream() and throw runtime_error exception
  126. template<typename T1>
  127. arma_cold
  128. arma_noinline
  129. static
  130. void
  131. arma_stop_runtime_error(const T1& x)
  132. {
  133. #if defined(ARMA_PRINT_ERRORS)
  134. {
  135. get_cerr_stream() << "\nerror: " << x << std::endl;
  136. }
  137. #endif
  138. throw std::runtime_error( std::string(x) );
  139. }
  140. //
  141. // arma_print
  142. arma_cold
  143. inline
  144. void
  145. arma_print()
  146. {
  147. get_cerr_stream() << std::endl;
  148. }
  149. template<typename T1>
  150. arma_cold
  151. arma_noinline
  152. static
  153. void
  154. arma_print(const T1& x)
  155. {
  156. get_cerr_stream() << x << std::endl;
  157. }
  158. template<typename T1, typename T2>
  159. arma_cold
  160. arma_noinline
  161. static
  162. void
  163. arma_print(const T1& x, const T2& y)
  164. {
  165. get_cerr_stream() << x << y << std::endl;
  166. }
  167. template<typename T1, typename T2, typename T3>
  168. arma_cold
  169. arma_noinline
  170. static
  171. void
  172. arma_print(const T1& x, const T2& y, const T3& z)
  173. {
  174. get_cerr_stream() << x << y << z << std::endl;
  175. }
  176. //
  177. // arma_sigprint
  178. //! print a message the the log stream with a preceding @ character.
  179. //! by default the log stream is cout.
  180. //! used for printing the signature of a function
  181. //! (see the arma_extra_debug_sigprint macro)
  182. inline
  183. void
  184. arma_sigprint(const char* x)
  185. {
  186. get_cerr_stream() << "@ " << x;
  187. }
  188. //
  189. // arma_bktprint
  190. inline
  191. void
  192. arma_bktprint()
  193. {
  194. get_cerr_stream() << std::endl;
  195. }
  196. template<typename T1>
  197. inline
  198. void
  199. arma_bktprint(const T1& x)
  200. {
  201. get_cerr_stream() << " [" << x << ']' << std::endl;
  202. }
  203. template<typename T1, typename T2>
  204. inline
  205. void
  206. arma_bktprint(const T1& x, const T2& y)
  207. {
  208. get_cerr_stream() << " [" << x << y << ']' << std::endl;
  209. }
  210. //
  211. // arma_thisprint
  212. inline
  213. void
  214. arma_thisprint(const void* this_ptr)
  215. {
  216. get_cerr_stream() << " [this = " << this_ptr << ']' << std::endl;
  217. }
  218. //
  219. // arma_warn
  220. //! print a message to the warn stream
  221. template<typename T1>
  222. arma_cold
  223. arma_noinline
  224. static
  225. void
  226. arma_warn(const T1& x)
  227. {
  228. #if defined(ARMA_PRINT_ERRORS)
  229. {
  230. get_cerr_stream() << "\nwarning: " << x << '\n';
  231. }
  232. #else
  233. {
  234. arma_ignore(x);
  235. }
  236. #endif
  237. }
  238. template<typename T1, typename T2>
  239. arma_cold
  240. arma_noinline
  241. static
  242. void
  243. arma_warn(const T1& x, const T2& y)
  244. {
  245. #if defined(ARMA_PRINT_ERRORS)
  246. {
  247. get_cerr_stream() << "\nwarning: " << x << y << '\n';
  248. }
  249. #else
  250. {
  251. arma_ignore(x);
  252. arma_ignore(y);
  253. }
  254. #endif
  255. }
  256. template<typename T1, typename T2, typename T3>
  257. arma_cold
  258. arma_noinline
  259. static
  260. void
  261. arma_warn(const T1& x, const T2& y, const T3& z)
  262. {
  263. #if defined(ARMA_PRINT_ERRORS)
  264. {
  265. get_cerr_stream() << "\nwarning: " << x << y << z << '\n';
  266. }
  267. #else
  268. {
  269. arma_ignore(x);
  270. arma_ignore(y);
  271. arma_ignore(z);
  272. }
  273. #endif
  274. }
  275. //
  276. // arma_check
  277. //! if state is true, abort program
  278. template<typename T1>
  279. arma_hot
  280. inline
  281. void
  282. arma_check(const bool state, const T1& x)
  283. {
  284. if(state) { arma_stop_logic_error(arma_str::str_wrapper(x)); }
  285. }
  286. template<typename T1, typename T2>
  287. arma_hot
  288. inline
  289. void
  290. arma_check(const bool state, const T1& x, const T2& y)
  291. {
  292. if(state) { arma_stop_logic_error( std::string(x) + std::string(y) ); }
  293. }
  294. template<typename T1>
  295. arma_hot
  296. inline
  297. void
  298. arma_check_bad_alloc(const bool state, const T1& x)
  299. {
  300. if(state) { arma_stop_bad_alloc(x); }
  301. }
  302. //
  303. // arma_set_error
  304. arma_hot
  305. arma_inline
  306. void
  307. arma_set_error(bool& err_state, char*& err_msg, const bool expression, const char* message)
  308. {
  309. if(expression)
  310. {
  311. err_state = true;
  312. err_msg = const_cast<char*>(message);
  313. }
  314. }
  315. //
  316. // functions for generating strings indicating size errors
  317. arma_cold
  318. arma_noinline
  319. static
  320. std::string
  321. arma_incompat_size_string(const uword A_n_rows, const uword A_n_cols, const uword B_n_rows, const uword B_n_cols, const char* x)
  322. {
  323. std::ostringstream tmp;
  324. tmp << x << ": incompatible matrix dimensions: " << A_n_rows << 'x' << A_n_cols << " and " << B_n_rows << 'x' << B_n_cols;
  325. return tmp.str();
  326. }
  327. arma_cold
  328. arma_noinline
  329. static
  330. std::string
  331. arma_incompat_size_string(const uword A_n_rows, const uword A_n_cols, const uword A_n_slices, const uword B_n_rows, const uword B_n_cols, const uword B_n_slices, const char* x)
  332. {
  333. std::ostringstream tmp;
  334. tmp << x << ": incompatible cube dimensions: " << A_n_rows << 'x' << A_n_cols << 'x' << A_n_slices << " and " << B_n_rows << 'x' << B_n_cols << 'x' << B_n_slices;
  335. return tmp.str();
  336. }
  337. template<typename eT>
  338. arma_cold
  339. arma_noinline
  340. static
  341. std::string
  342. arma_incompat_size_string(const subview_cube<eT>& Q, const Mat<eT>& A, const char* x)
  343. {
  344. std::ostringstream tmp;
  345. tmp << x
  346. << ": interpreting matrix as cube with dimensions: "
  347. << A.n_rows << 'x' << A.n_cols << 'x' << 1
  348. << " or "
  349. << A.n_rows << 'x' << 1 << 'x' << A.n_cols
  350. << " or "
  351. << 1 << 'x' << A.n_rows << 'x' << A.n_cols
  352. << " is incompatible with cube dimensions: "
  353. << Q.n_rows << 'x' << Q.n_cols << 'x' << Q.n_slices;
  354. return tmp.str();
  355. }
  356. //
  357. // functions for checking whether two dense matrices have the same dimensions
  358. arma_inline
  359. arma_hot
  360. void
  361. arma_assert_same_size(const uword A_n_rows, const uword A_n_cols, const uword B_n_rows, const uword B_n_cols, const char* x)
  362. {
  363. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  364. {
  365. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  366. }
  367. }
  368. //! stop if given matrices have different sizes
  369. template<typename eT1, typename eT2>
  370. arma_hot
  371. inline
  372. void
  373. arma_assert_same_size(const Mat<eT1>& A, const Mat<eT2>& B, const char* x)
  374. {
  375. const uword A_n_rows = A.n_rows;
  376. const uword A_n_cols = A.n_cols;
  377. const uword B_n_rows = B.n_rows;
  378. const uword B_n_cols = B.n_cols;
  379. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  380. {
  381. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  382. }
  383. }
  384. //! stop if given proxies have different sizes
  385. template<typename eT1, typename eT2>
  386. arma_hot
  387. inline
  388. void
  389. arma_assert_same_size(const Proxy<eT1>& A, const Proxy<eT2>& B, const char* x)
  390. {
  391. const uword A_n_rows = A.get_n_rows();
  392. const uword A_n_cols = A.get_n_cols();
  393. const uword B_n_rows = B.get_n_rows();
  394. const uword B_n_cols = B.get_n_cols();
  395. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  396. {
  397. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  398. }
  399. }
  400. template<typename eT1, typename eT2>
  401. arma_hot
  402. inline
  403. void
  404. arma_assert_same_size(const subview<eT1>& A, const subview<eT2>& B, const char* x)
  405. {
  406. const uword A_n_rows = A.n_rows;
  407. const uword A_n_cols = A.n_cols;
  408. const uword B_n_rows = B.n_rows;
  409. const uword B_n_cols = B.n_cols;
  410. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  411. {
  412. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  413. }
  414. }
  415. template<typename eT1, typename eT2>
  416. arma_hot
  417. inline
  418. void
  419. arma_assert_same_size(const Mat<eT1>& A, const subview<eT2>& B, const char* x)
  420. {
  421. const uword A_n_rows = A.n_rows;
  422. const uword A_n_cols = A.n_cols;
  423. const uword B_n_rows = B.n_rows;
  424. const uword B_n_cols = B.n_cols;
  425. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  426. {
  427. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  428. }
  429. }
  430. template<typename eT1, typename eT2>
  431. arma_hot
  432. inline
  433. void
  434. arma_assert_same_size(const subview<eT1>& A, const Mat<eT2>& B, const char* x)
  435. {
  436. const uword A_n_rows = A.n_rows;
  437. const uword A_n_cols = A.n_cols;
  438. const uword B_n_rows = B.n_rows;
  439. const uword B_n_cols = B.n_cols;
  440. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  441. {
  442. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  443. }
  444. }
  445. template<typename eT1, typename eT2>
  446. arma_hot
  447. inline
  448. void
  449. arma_assert_same_size(const Mat<eT1>& A, const Proxy<eT2>& B, const char* x)
  450. {
  451. const uword A_n_rows = A.n_rows;
  452. const uword A_n_cols = A.n_cols;
  453. const uword B_n_rows = B.get_n_rows();
  454. const uword B_n_cols = B.get_n_cols();
  455. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  456. {
  457. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  458. }
  459. }
  460. template<typename eT1, typename eT2>
  461. arma_hot
  462. inline
  463. void
  464. arma_assert_same_size(const Proxy<eT1>& A, const Mat<eT2>& B, const char* x)
  465. {
  466. const uword A_n_rows = A.get_n_rows();
  467. const uword A_n_cols = A.get_n_cols();
  468. const uword B_n_rows = B.n_rows;
  469. const uword B_n_cols = B.n_cols;
  470. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  471. {
  472. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  473. }
  474. }
  475. template<typename eT1, typename eT2>
  476. arma_hot
  477. inline
  478. void
  479. arma_assert_same_size(const Proxy<eT1>& A, const subview<eT2>& B, const char* x)
  480. {
  481. const uword A_n_rows = A.get_n_rows();
  482. const uword A_n_cols = A.get_n_cols();
  483. const uword B_n_rows = B.n_rows;
  484. const uword B_n_cols = B.n_cols;
  485. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  486. {
  487. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  488. }
  489. }
  490. template<typename eT1, typename eT2>
  491. arma_hot
  492. inline
  493. void
  494. arma_assert_same_size(const subview<eT1>& A, const Proxy<eT2>& B, const char* x)
  495. {
  496. const uword A_n_rows = A.n_rows;
  497. const uword A_n_cols = A.n_cols;
  498. const uword B_n_rows = B.get_n_rows();
  499. const uword B_n_cols = B.get_n_cols();
  500. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  501. {
  502. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  503. }
  504. }
  505. //
  506. // functions for checking whether two sparse matrices have the same dimensions
  507. template<typename eT1, typename eT2>
  508. arma_hot
  509. inline
  510. void
  511. arma_assert_same_size(const SpMat<eT1>& A, const SpMat<eT2>& B, const char* x)
  512. {
  513. const uword A_n_rows = A.n_rows;
  514. const uword A_n_cols = A.n_cols;
  515. const uword B_n_rows = B.n_rows;
  516. const uword B_n_cols = B.n_cols;
  517. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) )
  518. {
  519. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  520. }
  521. }
  522. //
  523. // functions for checking whether two cubes have the same dimensions
  524. arma_hot
  525. inline
  526. void
  527. arma_assert_same_size(const uword A_n_rows, const uword A_n_cols, const uword A_n_slices, const uword B_n_rows, const uword B_n_cols, const uword B_n_slices, const char* x)
  528. {
  529. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) || (A_n_slices != B_n_slices) )
  530. {
  531. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, A_n_slices, B_n_rows, B_n_cols, B_n_slices, x) );
  532. }
  533. }
  534. //! stop if given cubes have different sizes
  535. template<typename eT1, typename eT2>
  536. arma_hot
  537. inline
  538. void
  539. arma_assert_same_size(const Cube<eT1>& A, const Cube<eT2>& B, const char* x)
  540. {
  541. if( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) || (A.n_slices != B.n_slices) )
  542. {
  543. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, A.n_slices, B.n_rows, B.n_cols, B.n_slices, x) );
  544. }
  545. }
  546. template<typename eT1, typename eT2>
  547. arma_hot
  548. inline
  549. void
  550. arma_assert_same_size(const Cube<eT1>& A, const subview_cube<eT2>& B, const char* x)
  551. {
  552. if( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) || (A.n_slices != B.n_slices) )
  553. {
  554. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, A.n_slices, B.n_rows, B.n_cols, B.n_slices, x) );
  555. }
  556. }
  557. template<typename eT1, typename eT2>
  558. arma_hot
  559. inline
  560. void
  561. arma_assert_same_size(const subview_cube<eT1>& A, const Cube<eT2>& B, const char* x)
  562. {
  563. if( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) || (A.n_slices != B.n_slices) )
  564. {
  565. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, A.n_slices, B.n_rows, B.n_cols, B.n_slices, x) );
  566. }
  567. }
  568. template<typename eT1, typename eT2>
  569. arma_hot
  570. inline
  571. void
  572. arma_assert_same_size(const subview_cube<eT1>& A, const subview_cube<eT2>& B, const char* x)
  573. {
  574. if( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) || (A.n_slices != B.n_slices))
  575. {
  576. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, A.n_slices, B.n_rows, B.n_cols, B.n_slices, x) );
  577. }
  578. }
  579. //! stop if given cube proxies have different sizes
  580. template<typename eT1, typename eT2>
  581. arma_hot
  582. inline
  583. void
  584. arma_assert_same_size(const ProxyCube<eT1>& A, const ProxyCube<eT2>& B, const char* x)
  585. {
  586. const uword A_n_rows = A.get_n_rows();
  587. const uword A_n_cols = A.get_n_cols();
  588. const uword A_n_slices = A.get_n_slices();
  589. const uword B_n_rows = B.get_n_rows();
  590. const uword B_n_cols = B.get_n_cols();
  591. const uword B_n_slices = B.get_n_slices();
  592. if( (A_n_rows != B_n_rows) || (A_n_cols != B_n_cols) || (A_n_slices != B_n_slices))
  593. {
  594. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, A_n_slices, B_n_rows, B_n_cols, B_n_slices, x) );
  595. }
  596. }
  597. //
  598. // functions for checking whether a cube or subcube can be interpreted as a matrix (i.e. single slice)
  599. template<typename eT1, typename eT2>
  600. arma_hot
  601. inline
  602. void
  603. arma_assert_same_size(const Cube<eT1>& A, const Mat<eT2>& B, const char* x)
  604. {
  605. if( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) || (A.n_slices != 1) )
  606. {
  607. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, A.n_slices, B.n_rows, B.n_cols, 1, x) );
  608. }
  609. }
  610. template<typename eT1, typename eT2>
  611. arma_hot
  612. inline
  613. void
  614. arma_assert_same_size(const Mat<eT1>& A, const Cube<eT2>& B, const char* x)
  615. {
  616. if( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) || (1 != B.n_slices) )
  617. {
  618. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, 1, B.n_rows, B.n_cols, B.n_slices, x) );
  619. }
  620. }
  621. template<typename eT1, typename eT2>
  622. arma_hot
  623. inline
  624. void
  625. arma_assert_same_size(const subview_cube<eT1>& A, const Mat<eT2>& B, const char* x)
  626. {
  627. if( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) || (A.n_slices != 1) )
  628. {
  629. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, A.n_slices, B.n_rows, B.n_cols, 1, x) );
  630. }
  631. }
  632. template<typename eT1, typename eT2>
  633. arma_hot
  634. inline
  635. void
  636. arma_assert_same_size(const Mat<eT1>& A, const subview_cube<eT2>& B, const char* x)
  637. {
  638. if( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) || (1 != B.n_slices) )
  639. {
  640. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, 1, B.n_rows, B.n_cols, B.n_slices, x) );
  641. }
  642. }
  643. template<typename eT, typename T1>
  644. inline
  645. void
  646. arma_assert_cube_as_mat(const Mat<eT>& M, const T1& Q, const char* x, const bool check_compat_size)
  647. {
  648. const uword Q_n_rows = Q.n_rows;
  649. const uword Q_n_cols = Q.n_cols;
  650. const uword Q_n_slices = Q.n_slices;
  651. const uword M_vec_state = M.vec_state;
  652. if(M_vec_state == 0)
  653. {
  654. if( ( (Q_n_rows == 1) || (Q_n_cols == 1) || (Q_n_slices == 1) ) == false )
  655. {
  656. std::ostringstream tmp;
  657. tmp << x
  658. << ": can't interpret cube with dimensions "
  659. << Q_n_rows << 'x' << Q_n_cols << 'x' << Q_n_slices
  660. << " as a matrix; one of the dimensions must be 1";
  661. arma_stop_logic_error( tmp.str() );
  662. }
  663. }
  664. else
  665. {
  666. if(Q_n_slices == 1)
  667. {
  668. if( (M_vec_state == 1) && (Q_n_cols != 1) )
  669. {
  670. std::ostringstream tmp;
  671. tmp << x
  672. << ": can't interpret cube with dimensions "
  673. << Q_n_rows << 'x' << Q_n_cols << 'x' << Q_n_slices
  674. << " as a column vector";
  675. arma_stop_logic_error( tmp.str() );
  676. }
  677. if( (M_vec_state == 2) && (Q_n_rows != 1) )
  678. {
  679. std::ostringstream tmp;
  680. tmp << x
  681. << ": can't interpret cube with dimensions "
  682. << Q_n_rows << 'x' << Q_n_cols << 'x' << Q_n_slices
  683. << " as a row vector";
  684. arma_stop_logic_error( tmp.str() );
  685. }
  686. }
  687. else
  688. {
  689. if( (Q_n_cols != 1) && (Q_n_rows != 1) )
  690. {
  691. std::ostringstream tmp;
  692. tmp << x
  693. << ": can't interpret cube with dimensions "
  694. << Q_n_rows << 'x' << Q_n_cols << 'x' << Q_n_slices
  695. << " as a vector";
  696. arma_stop_logic_error( tmp.str() );
  697. }
  698. }
  699. }
  700. if(check_compat_size)
  701. {
  702. const uword M_n_rows = M.n_rows;
  703. const uword M_n_cols = M.n_cols;
  704. if(M_vec_state == 0)
  705. {
  706. if(
  707. (
  708. ( (Q_n_rows == M_n_rows) && (Q_n_cols == M_n_cols) )
  709. ||
  710. ( (Q_n_rows == M_n_rows) && (Q_n_slices == M_n_cols) )
  711. ||
  712. ( (Q_n_cols == M_n_rows) && (Q_n_slices == M_n_cols) )
  713. )
  714. == false
  715. )
  716. {
  717. std::ostringstream tmp;
  718. tmp << x
  719. << ": can't interpret cube with dimensions "
  720. << Q_n_rows << 'x' << Q_n_cols << 'x' << Q_n_slices
  721. << " as a matrix with dimensions "
  722. << M_n_rows << 'x' << M_n_cols;
  723. arma_stop_logic_error( tmp.str() );
  724. }
  725. }
  726. else
  727. {
  728. if(Q_n_slices == 1)
  729. {
  730. if( (M_vec_state == 1) && (Q_n_rows != M_n_rows) )
  731. {
  732. std::ostringstream tmp;
  733. tmp << x
  734. << ": can't interpret cube with dimensions "
  735. << Q_n_rows << 'x' << Q_n_cols << 'x' << Q_n_slices
  736. << " as a column vector with dimensions "
  737. << M_n_rows << 'x' << M_n_cols;
  738. arma_stop_logic_error( tmp.str() );
  739. }
  740. if( (M_vec_state == 2) && (Q_n_cols != M_n_cols) )
  741. {
  742. std::ostringstream tmp;
  743. tmp << x
  744. << ": can't interpret cube with dimensions "
  745. << Q_n_rows << 'x' << Q_n_cols << 'x' << Q_n_slices
  746. << " as a row vector with dimensions "
  747. << M_n_rows << 'x' << M_n_cols;
  748. arma_stop_logic_error( tmp.str() );
  749. }
  750. }
  751. else
  752. {
  753. if( ( (M_n_cols == Q_n_slices) || (M_n_rows == Q_n_slices) ) == false )
  754. {
  755. std::ostringstream tmp;
  756. tmp << x
  757. << ": can't interpret cube with dimensions "
  758. << Q_n_rows << 'x' << Q_n_cols << 'x' << Q_n_slices
  759. << " as a vector with dimensions "
  760. << M_n_rows << 'x' << M_n_cols;
  761. arma_stop_logic_error( tmp.str() );
  762. }
  763. }
  764. }
  765. }
  766. }
  767. //
  768. // functions for checking whether two matrices have dimensions that are compatible with the matrix multiply operation
  769. arma_hot
  770. inline
  771. void
  772. arma_assert_mul_size(const uword A_n_rows, const uword A_n_cols, const uword B_n_rows, const uword B_n_cols, const char* x)
  773. {
  774. if(A_n_cols != B_n_rows)
  775. {
  776. arma_stop_logic_error( arma_incompat_size_string(A_n_rows, A_n_cols, B_n_rows, B_n_cols, x) );
  777. }
  778. }
  779. //! stop if given matrices are incompatible for multiplication
  780. template<typename eT1, typename eT2>
  781. arma_hot
  782. inline
  783. void
  784. arma_assert_mul_size(const Mat<eT1>& A, const Mat<eT2>& B, const char* x)
  785. {
  786. const uword A_n_cols = A.n_cols;
  787. const uword B_n_rows = B.n_rows;
  788. if(A_n_cols != B_n_rows)
  789. {
  790. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A_n_cols, B_n_rows, B.n_cols, x) );
  791. }
  792. }
  793. //! stop if given matrices are incompatible for multiplication
  794. template<typename eT1, typename eT2>
  795. arma_hot
  796. inline
  797. void
  798. arma_assert_mul_size(const Mat<eT1>& A, const Mat<eT2>& B, const bool do_trans_A, const bool do_trans_B, const char* x)
  799. {
  800. const uword final_A_n_cols = (do_trans_A == false) ? A.n_cols : A.n_rows;
  801. const uword final_B_n_rows = (do_trans_B == false) ? B.n_rows : B.n_cols;
  802. if(final_A_n_cols != final_B_n_rows)
  803. {
  804. const uword final_A_n_rows = (do_trans_A == false) ? A.n_rows : A.n_cols;
  805. const uword final_B_n_cols = (do_trans_B == false) ? B.n_cols : B.n_rows;
  806. arma_stop_logic_error( arma_incompat_size_string(final_A_n_rows, final_A_n_cols, final_B_n_rows, final_B_n_cols, x) );
  807. }
  808. }
  809. template<const bool do_trans_A, const bool do_trans_B>
  810. arma_hot
  811. inline
  812. void
  813. arma_assert_trans_mul_size(const uword A_n_rows, const uword A_n_cols, const uword B_n_rows, const uword B_n_cols, const char* x)
  814. {
  815. const uword final_A_n_cols = (do_trans_A == false) ? A_n_cols : A_n_rows;
  816. const uword final_B_n_rows = (do_trans_B == false) ? B_n_rows : B_n_cols;
  817. if(final_A_n_cols != final_B_n_rows)
  818. {
  819. const uword final_A_n_rows = (do_trans_A == false) ? A_n_rows : A_n_cols;
  820. const uword final_B_n_cols = (do_trans_B == false) ? B_n_cols : B_n_rows;
  821. arma_stop_logic_error( arma_incompat_size_string(final_A_n_rows, final_A_n_cols, final_B_n_rows, final_B_n_cols, x) );
  822. }
  823. }
  824. template<typename eT1, typename eT2>
  825. arma_hot
  826. inline
  827. void
  828. arma_assert_mul_size(const Mat<eT1>& A, const subview<eT2>& B, const char* x)
  829. {
  830. if(A.n_cols != B.n_rows)
  831. {
  832. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, B.n_rows, B.n_cols, x) );
  833. }
  834. }
  835. template<typename eT1, typename eT2>
  836. arma_hot
  837. inline
  838. void
  839. arma_assert_mul_size(const subview<eT1>& A, const Mat<eT2>& B, const char* x)
  840. {
  841. if(A.n_cols != B.n_rows)
  842. {
  843. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, B.n_rows, B.n_cols, x) );
  844. }
  845. }
  846. template<typename eT1, typename eT2>
  847. arma_hot
  848. inline
  849. void
  850. arma_assert_mul_size(const subview<eT1>& A, const subview<eT2>& B, const char* x)
  851. {
  852. if(A.n_cols != B.n_rows)
  853. {
  854. arma_stop_logic_error( arma_incompat_size_string(A.n_rows, A.n_cols, B.n_rows, B.n_cols, x) );
  855. }
  856. }
  857. template<typename T1>
  858. arma_hot
  859. inline
  860. void
  861. arma_assert_blas_size(const T1& A)
  862. {
  863. if(sizeof(uword) >= sizeof(blas_int))
  864. {
  865. bool overflow;
  866. overflow = (A.n_rows > ARMA_MAX_BLAS_INT);
  867. overflow = (A.n_cols > ARMA_MAX_BLAS_INT) || overflow;
  868. if(overflow)
  869. {
  870. arma_stop_runtime_error("integer overflow: matrix dimensions are too large for integer type used by BLAS and LAPACK");
  871. }
  872. }
  873. }
  874. template<typename T1, typename T2>
  875. arma_hot
  876. inline
  877. void
  878. arma_assert_blas_size(const T1& A, const T2& B)
  879. {
  880. if(sizeof(uword) >= sizeof(blas_int))
  881. {
  882. bool overflow;
  883. overflow = (A.n_rows > ARMA_MAX_BLAS_INT);
  884. overflow = (A.n_cols > ARMA_MAX_BLAS_INT) || overflow;
  885. overflow = (B.n_rows > ARMA_MAX_BLAS_INT) || overflow;
  886. overflow = (B.n_cols > ARMA_MAX_BLAS_INT) || overflow;
  887. if(overflow)
  888. {
  889. arma_stop_runtime_error("integer overflow: matrix dimensions are too large for integer type used by BLAS and LAPACK");
  890. }
  891. }
  892. }
  893. template<typename T1>
  894. arma_hot
  895. inline
  896. void
  897. arma_assert_atlas_size(const T1& A)
  898. {
  899. if(sizeof(uword) >= sizeof(int))
  900. {
  901. bool overflow;
  902. overflow = (A.n_rows > INT_MAX);
  903. overflow = (A.n_cols > INT_MAX) || overflow;
  904. if(overflow)
  905. {
  906. arma_stop_runtime_error("integer overflow: matrix dimensions are too large for integer type used by ATLAS");
  907. }
  908. }
  909. }
  910. template<typename T1, typename T2>
  911. arma_hot
  912. inline
  913. void
  914. arma_assert_atlas_size(const T1& A, const T2& B)
  915. {
  916. if(sizeof(uword) >= sizeof(int))
  917. {
  918. bool overflow;
  919. overflow = (A.n_rows > INT_MAX);
  920. overflow = (A.n_cols > INT_MAX) || overflow;
  921. overflow = (B.n_rows > INT_MAX) || overflow;
  922. overflow = (B.n_cols > INT_MAX) || overflow;
  923. if(overflow)
  924. {
  925. arma_stop_runtime_error("integer overflow: matrix dimensions are too large for integer type used by ATLAS");
  926. }
  927. }
  928. }
  929. //
  930. // macros
  931. // #define ARMA_STRING1(x) #x
  932. // #define ARMA_STRING2(x) ARMA_STRING1(x)
  933. // #define ARMA_FILELINE __FILE__ ": " ARMA_STRING2(__LINE__)
  934. #if defined(ARMA_NO_DEBUG)
  935. #undef ARMA_EXTRA_DEBUG
  936. #define arma_debug_print true ? (void)0 : arma_print
  937. #define arma_debug_warn true ? (void)0 : arma_warn
  938. #define arma_debug_check true ? (void)0 : arma_check
  939. #define arma_debug_set_error true ? (void)0 : arma_set_error
  940. #define arma_debug_assert_same_size true ? (void)0 : arma_assert_same_size
  941. #define arma_debug_assert_mul_size true ? (void)0 : arma_assert_mul_size
  942. #define arma_debug_assert_trans_mul_size true ? (void)0 : arma_assert_trans_mul_size
  943. #define arma_debug_assert_cube_as_mat true ? (void)0 : arma_assert_cube_as_mat
  944. #define arma_debug_assert_blas_size true ? (void)0 : arma_assert_blas_size
  945. #define arma_debug_assert_atlas_size true ? (void)0 : arma_assert_atlas_size
  946. #else
  947. #define arma_debug_print arma_print
  948. #define arma_debug_warn arma_warn
  949. #define arma_debug_check arma_check
  950. #define arma_debug_set_error arma_set_error
  951. #define arma_debug_assert_same_size arma_assert_same_size
  952. #define arma_debug_assert_mul_size arma_assert_mul_size
  953. #define arma_debug_assert_trans_mul_size arma_assert_trans_mul_size
  954. #define arma_debug_assert_cube_as_mat arma_assert_cube_as_mat
  955. #define arma_debug_assert_blas_size arma_assert_blas_size
  956. #define arma_debug_assert_atlas_size arma_assert_atlas_size
  957. #endif
  958. #if defined(ARMA_EXTRA_DEBUG)
  959. #define arma_extra_debug_sigprint arma_sigprint(ARMA_FNSIG); arma_bktprint
  960. #define arma_extra_debug_sigprint_this arma_sigprint(ARMA_FNSIG); arma_thisprint
  961. #define arma_extra_debug_print arma_print
  962. #define arma_extra_debug_warn arma_warn
  963. #define arma_extra_debug_check arma_check
  964. #else
  965. #define arma_extra_debug_sigprint true ? (void)0 : arma_bktprint
  966. #define arma_extra_debug_sigprint_this true ? (void)0 : arma_thisprint
  967. #define arma_extra_debug_print true ? (void)0 : arma_print
  968. #define arma_extra_debug_warn true ? (void)0 : arma_warn
  969. #define arma_extra_debug_check true ? (void)0 : arma_check
  970. #endif
  971. #if defined(ARMA_EXTRA_DEBUG)
  972. namespace junk
  973. {
  974. class arma_first_extra_debug_message
  975. {
  976. public:
  977. inline
  978. arma_first_extra_debug_message()
  979. {
  980. union
  981. {
  982. unsigned short a;
  983. unsigned char b[sizeof(unsigned short)];
  984. } endian_test;
  985. endian_test.a = 1;
  986. const bool little_endian = (endian_test.b[0] == 1);
  987. const char* nickname = ARMA_VERSION_NAME;
  988. std::ostream& out = get_cerr_stream();
  989. out << "@ ---" << '\n';
  990. out << "@ Armadillo "
  991. << arma_version::major << '.' << arma_version::minor << '.' << arma_version::patch
  992. << " (" << nickname << ")\n";
  993. out << "@ arma_config::wrapper = " << arma_config::wrapper << '\n';
  994. out << "@ arma_config::cxx11 = " << arma_config::cxx11 << '\n';
  995. out << "@ arma_config::cxx11_mutex = " << arma_config::cxx11_mutex << '\n';
  996. out << "@ arma_config::posix = " << arma_config::posix << '\n';
  997. out << "@ arma_config::openmp = " << arma_config::openmp << '\n';
  998. out << "@ arma_config::lapack = " << arma_config::lapack << '\n';
  999. out << "@ arma_config::blas = " << arma_config::blas << '\n';
  1000. out << "@ arma_config::newarp = " << arma_config::newarp << '\n';
  1001. out << "@ arma_config::arpack = " << arma_config::arpack << '\n';
  1002. out << "@ arma_config::superlu = " << arma_config::superlu << '\n';
  1003. out << "@ arma_config::atlas = " << arma_config::atlas << '\n';
  1004. out << "@ arma_config::hdf5 = " << arma_config::hdf5 << '\n';
  1005. out << "@ arma_config::good_comp = " << arma_config::good_comp << '\n';
  1006. out << "@ arma_config::extra_code = " << arma_config::extra_code << '\n';
  1007. out << "@ arma_config::hidden_args = " << arma_config::hidden_args << '\n';
  1008. out << "@ arma_config::mat_prealloc = " << arma_config::mat_prealloc << '\n';
  1009. out << "@ arma_config::mp_threshold = " << arma_config::mp_threshold << '\n';
  1010. out << "@ arma_config::mp_threads = " << arma_config::mp_threads << '\n';
  1011. out << "@ sizeof(void*) = " << sizeof(void*) << '\n';
  1012. out << "@ sizeof(int) = " << sizeof(int) << '\n';
  1013. out << "@ sizeof(long) = " << sizeof(long) << '\n';
  1014. out << "@ sizeof(uword) = " << sizeof(uword) << '\n';
  1015. out << "@ sizeof(blas_int) = " << sizeof(blas_int) << '\n';
  1016. out << "@ little_endian = " << little_endian << '\n';
  1017. out << "@ ---" << std::endl;
  1018. }
  1019. };
  1020. static arma_first_extra_debug_message arma_first_extra_debug_message_run;
  1021. }
  1022. #endif
  1023. //! @}