spsubview.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. // Copyright 2011-2017 Ryan Curtin (http://www.ratml.org/)
  2. // Copyright 2017 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. #include <armadillo>
  16. #include "catch.hpp"
  17. using namespace arma;
  18. TEST_CASE("sp_subview_tests")
  19. {
  20. Mat<double> ref(4,4);
  21. ref.eye(4,4);
  22. SpMat<double> X(4,4);
  23. X.eye(4,4);
  24. /*
  25. * [[1,0,0,0] [[2,0,0,0]
  26. * [0,1,0,0] -> [0,2,0,0]
  27. * [0,0,1,0] [0,0,2,0]
  28. * [0,0,0,1]] [0,0,0,1]]
  29. */
  30. ref.submat(0, 0, 2, 2) *= 2;
  31. X.submat(0, 0, 2, 2) *= 2;
  32. for (uword i = 0; i < 4; i++)
  33. {
  34. for (uword j = 0; j < 4; j++)
  35. {
  36. REQUIRE( (double) ref(i, j) == Approx((double) X(i, j)) );
  37. }
  38. }
  39. /*
  40. * [[2,0,0,0] [[2,0,0,0]
  41. * [0,2,0,0] -> [0,1,0,0]
  42. * [0,0,2,0] [0,0,1,0]
  43. * [0,0,0,1]] [0,0,0,.5]]
  44. */
  45. ref.submat(1, 1, 3, 3) /= 2;
  46. X.submat(1, 1, 3, 3) /= 2;
  47. for (uword i = 0; i < 4; i++)
  48. {
  49. for (uword j = 0; j < 4; j++)
  50. {
  51. REQUIRE( (double) ref(i, j) == Approx((double) X(i, j)) );
  52. }
  53. }
  54. span s(1, 2);
  55. ref.submat(s, s) += 10;
  56. X.submat(s, s) += 10;
  57. for (uword i = 0; i < 4; i++)
  58. {
  59. for (uword j = 0; j < 4; j++)
  60. {
  61. REQUIRE( (double) ref(i, j) == Approx((double) X(i, j)) );
  62. }
  63. }
  64. }
  65. TEST_CASE("sp_subview_const_test")
  66. {
  67. Mat<double> ref(4, 4);
  68. ref.eye(4, 4);
  69. SpMat<double> X(4, 4);
  70. X.eye(4, 4);
  71. const SpSubview<double> subX = X.submat(span(0, 2), span::all);
  72. /*
  73. X.print("x");
  74. for (size_t i = 0; i < 3; ++i)
  75. {
  76. for (size_t j = 0; j < 4; ++j)
  77. {
  78. printf("%f ", subX(i, j));
  79. }
  80. printf("\n");
  81. }
  82. */
  83. }
  84. TEST_CASE("sp_subview_multiplication_test")
  85. {
  86. // Ensure matrix multiplication with subviews works correctly.
  87. SpMat<double> a(2, 5);
  88. SpMat<double> b(5, 2);
  89. a(1, 3) = 1;
  90. a(0, 0) = 2;
  91. a(1, 2) = 1.5;
  92. b(4, 1) = 3;
  93. b(3, 0) = 2;
  94. b(1, 0) = 1;
  95. b(0, 0) = 0.6;
  96. b *= a;
  97. REQUIRE( b.n_cols == 5 );
  98. REQUIRE( b.n_rows == 5 );
  99. REQUIRE( b.n_elem == 25 );
  100. REQUIRE( b.n_nonzero == 5 );
  101. REQUIRE( (double) b(0, 0) == Approx(1.2) );
  102. REQUIRE( (double) b(0, 1) == Approx(1e-5) );
  103. REQUIRE( (double) b(0, 2) == Approx(1e-5) );
  104. REQUIRE( (double) b(0, 3) == Approx(1e-5) );
  105. REQUIRE( (double) b(0, 4) == Approx(1e-5) );
  106. REQUIRE( (double) b(1, 0) == Approx(2.0) );
  107. REQUIRE( (double) b(1, 1) == Approx(1e-5) );
  108. REQUIRE( (double) b(1, 2) == Approx(1e-5) );
  109. REQUIRE( (double) b(1, 3) == Approx(1e-5) );
  110. REQUIRE( (double) b(1, 4) == Approx(1e-5) );
  111. REQUIRE( (double) b(2, 0) == Approx(1e-5) );
  112. REQUIRE( (double) b(2, 1) == Approx(1e-5) );
  113. REQUIRE( (double) b(2, 2) == Approx(1e-5) );
  114. REQUIRE( (double) b(2, 3) == Approx(1e-5) );
  115. REQUIRE( (double) b(2, 4) == Approx(1e-5) );
  116. REQUIRE( (double) b(3, 0) == Approx(4.0) );
  117. REQUIRE( (double) b(3, 1) == Approx(1e-5) );
  118. REQUIRE( (double) b(3, 2) == Approx(1e-5) );
  119. REQUIRE( (double) b(3, 3) == Approx(1e-5) );
  120. REQUIRE( (double) b(3, 4) == Approx(1e-5) );
  121. REQUIRE( (double) b(4, 0) == Approx(1e-5) );
  122. REQUIRE( (double) b(4, 1) == Approx(1e-5) );
  123. REQUIRE( (double) b(4, 2) == Approx(4.5) );
  124. REQUIRE( (double) b(4, 3) == Approx(3.0) );
  125. REQUIRE( (double) b(4, 4) == Approx(1e-5) );
  126. }
  127. TEST_CASE("sp_subview_multiplication_test_2")
  128. {
  129. // Ensure matrix multiplication with subviews works correctly.
  130. SpMat<double> a(4, 5);
  131. SpMat<double> b(5, 2);
  132. a(2, 3) = 1;
  133. a(3, 1) = 1.4;
  134. a(0, 0) = 2;
  135. a(1, 0) = 2;
  136. a(2, 2) = 1.5;
  137. b(4, 1) = 3;
  138. b(3, 0) = 2;
  139. b(1, 0) = 1;
  140. b(0, 0) = 0.6;
  141. b *= a.rows(1, 2);
  142. REQUIRE( b.n_cols == 5 );
  143. REQUIRE( b.n_rows == 5 );
  144. REQUIRE( b.n_elem == 25 );
  145. REQUIRE( b.n_nonzero == 5 );
  146. REQUIRE( (double) b(0, 0) == Approx(1.2) );
  147. REQUIRE( (double) b(0, 1) == Approx(1e-5) );
  148. REQUIRE( (double) b(0, 2) == Approx(1e-5) );
  149. REQUIRE( (double) b(0, 3) == Approx(1e-5) );
  150. REQUIRE( (double) b(0, 4) == Approx(1e-5) );
  151. REQUIRE( (double) b(1, 0) == Approx(2.0) );
  152. REQUIRE( (double) b(1, 1) == Approx(1e-5) );
  153. REQUIRE( (double) b(1, 2) == Approx(1e-5) );
  154. REQUIRE( (double) b(1, 3) == Approx(1e-5) );
  155. REQUIRE( (double) b(1, 4) == Approx(1e-5) );
  156. REQUIRE( (double) b(2, 0) == Approx(1e-5) );
  157. REQUIRE( (double) b(2, 1) == Approx(1e-5) );
  158. REQUIRE( (double) b(2, 2) == Approx(1e-5) );
  159. REQUIRE( (double) b(2, 3) == Approx(1e-5) );
  160. REQUIRE( (double) b(2, 4) == Approx(1e-5) );
  161. REQUIRE( (double) b(3, 0) == Approx(4.0) );
  162. REQUIRE( (double) b(3, 1) == Approx(1e-5) );
  163. REQUIRE( (double) b(3, 2) == Approx(1e-5) );
  164. REQUIRE( (double) b(3, 3) == Approx(1e-5) );
  165. REQUIRE( (double) b(3, 4) == Approx(1e-5) );
  166. REQUIRE( (double) b(4, 0) == Approx(1e-5) );
  167. REQUIRE( (double) b(4, 1) == Approx(1e-5) );
  168. REQUIRE( (double) b(4, 2) == Approx(4.5) );
  169. REQUIRE( (double) b(4, 3) == Approx(3.0) );
  170. REQUIRE( (double) b(4, 4) == Approx(1e-5) );
  171. }
  172. TEST_CASE("sp_subview_unary_operators_test")
  173. {
  174. SpMat<int> a(3, 3);
  175. SpMat<int> b(5, 5);
  176. a(0, 0) = 1;
  177. a(1, 2) = 4;
  178. a(2, 2) = 5;
  179. b(2, 3) = 1;
  180. b(3, 2) = 2;
  181. b(3, 4) = -4;
  182. b(4, 4) = 5;
  183. SpMat<int> c = a + b.submat(2, 2, 4, 4);
  184. REQUIRE( c.n_nonzero == 4 );
  185. REQUIRE( (double) c(0, 0) == 1 );
  186. REQUIRE( (double) c(1, 0) == 2 );
  187. REQUIRE( (double) c(2, 0) == 0 );
  188. REQUIRE( (double) c(0, 1) == 1 );
  189. REQUIRE( (double) c(1, 1) == 0 );
  190. REQUIRE( (double) c(2, 1) == 0 );
  191. REQUIRE( (double) c(0, 2) == 0 );
  192. REQUIRE( (double) c(1, 2) == 0 );
  193. REQUIRE( (double) c(2, 2) == 10 );
  194. c = a - b.submat(2, 2, 4, 4);
  195. REQUIRE( c.n_nonzero == 4 );
  196. REQUIRE( (double) c(0, 0) == 1 );
  197. REQUIRE( (double) c(1, 0) == -2 );
  198. REQUIRE( (double) c(2, 0) == 0 );
  199. REQUIRE( (double) c(0, 1) == -1 );
  200. REQUIRE( (double) c(1, 1) == 0 );
  201. REQUIRE( (double) c(2, 1) == 0 );
  202. REQUIRE( (double) c(0, 2) == 0 );
  203. REQUIRE( (double) c(1, 2) == 8 );
  204. REQUIRE( (double) c(2, 2) == 0 );
  205. c = a % b.submat(2, 2, 4, 4);
  206. REQUIRE( c.n_nonzero == 2 );
  207. REQUIRE( (double) c(0, 0) == 0 );
  208. REQUIRE( (double) c(1, 0) == 0 );
  209. REQUIRE( (double) c(2, 0) == 0 );
  210. REQUIRE( (double) c(0, 1) == 0 );
  211. REQUIRE( (double) c(1, 1) == 0 );
  212. REQUIRE( (double) c(2, 1) == 0 );
  213. REQUIRE( (double) c(0, 2) == 0 );
  214. REQUIRE( (double) c(1, 2) == -16 );
  215. REQUIRE( (double) c(2, 2) == 25 );
  216. a(0, 0) = 4;
  217. b(2, 2) = 2;
  218. /*
  219. c = a / b.submat(2, 2, 4, 4);
  220. REQUIRE( c.n_nonzero == 3 );
  221. REQUIRE( (double) c(0, 0) == 2 );
  222. REQUIRE( (double) c(1, 0) == 0 );
  223. REQUIRE( (double) c(2, 0) == 0 );
  224. REQUIRE( (double) c(0, 1) == 0 );
  225. REQUIRE( (double) c(1, 1) == 0 );
  226. REQUIRE( (double) c(2, 1) == 0 );
  227. REQUIRE( (double) c(0, 2) == 0 );
  228. REQUIRE( (double) c(1, 2) == -1 );
  229. REQUIRE( (double) c(2, 2) == 1 );
  230. */
  231. }
  232. TEST_CASE("sp_subview_mat_operator_tests")
  233. {
  234. SpMat<double> a(6, 10);
  235. a(2, 2) = 2.0;
  236. a(3, 4) = 3.5;
  237. a(4, 3) = -2.0;
  238. a(4, 4) = 4.5;
  239. a(5, 1) = 3.2;
  240. a(0, 1) = 1.3;
  241. a(1, 1) = -4.0;
  242. a(5, 3) = 5.3;
  243. mat b(3, 3);
  244. b.fill(2.0);
  245. mat c(b);
  246. c += a.submat(2, 2, 4, 4);
  247. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  248. REQUIRE( (double) c(1, 0) == Approx(2.0) );
  249. REQUIRE( (double) c(2, 0) == Approx(2.0) );
  250. REQUIRE( (double) c(0, 1) == Approx(2.0) );
  251. REQUIRE( (double) c(1, 1) == Approx(2.0) );
  252. REQUIRE( (double) c(2, 1) == Approx(1e-5) );
  253. REQUIRE( (double) c(0, 2) == Approx(2.0) );
  254. REQUIRE( (double) c(1, 2) == Approx(5.5) );
  255. REQUIRE( (double) c(2, 2) == Approx(6.5) );
  256. c = b + a.submat(2, 2, 4, 4);
  257. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  258. REQUIRE( (double) c(1, 0) == Approx(2.0) );
  259. REQUIRE( (double) c(2, 0) == Approx(2.0) );
  260. REQUIRE( (double) c(0, 1) == Approx(2.0) );
  261. REQUIRE( (double) c(1, 1) == Approx(2.0) );
  262. REQUIRE( (double) c(2, 1) == Approx(1e-5) );
  263. REQUIRE( (double) c(0, 2) == Approx(2.0) );
  264. REQUIRE( (double) c(1, 2) == Approx(5.5) );
  265. REQUIRE( (double) c(2, 2) == Approx(6.5) );
  266. c = b;
  267. c -= a.submat(2, 2, 4, 4);
  268. REQUIRE( (double) c(0, 0) == Approx(1e-5) );
  269. REQUIRE( (double) c(1, 0) == Approx(2.0) );
  270. REQUIRE( (double) c(2, 0) == Approx(2.0) );
  271. REQUIRE( (double) c(0, 1) == Approx(2.0) );
  272. REQUIRE( (double) c(1, 1) == Approx(2.0) );
  273. REQUIRE( (double) c(2, 1) == Approx(4.0) );
  274. REQUIRE( (double) c(0, 2) == Approx(2.0) );
  275. REQUIRE( (double) c(1, 2) == Approx(-1.5) );
  276. REQUIRE( (double) c(2, 2) == Approx(-2.5) );
  277. c = b - a.submat(2, 2, 4, 4);
  278. REQUIRE( (double) c(0, 0) == Approx(1e-5) );
  279. REQUIRE( (double) c(1, 0) == Approx(2.0) );
  280. REQUIRE( (double) c(2, 0) == Approx(2.0) );
  281. REQUIRE( (double) c(0, 1) == Approx(2.0) );
  282. REQUIRE( (double) c(1, 1) == Approx(2.0) );
  283. REQUIRE( (double) c(2, 1) == Approx(4.0) );
  284. REQUIRE( (double) c(0, 2) == Approx(2.0) );
  285. REQUIRE( (double) c(1, 2) == Approx(-1.5) );
  286. REQUIRE( (double) c(2, 2) == Approx(-2.5) );
  287. c = b;
  288. c *= a.submat(2, 2, 4, 4);
  289. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  290. REQUIRE( (double) c(1, 0) == Approx(4.0) );
  291. REQUIRE( (double) c(2, 0) == Approx(4.0) );
  292. REQUIRE( (double) c(0, 1) == Approx(-4.0) );
  293. REQUIRE( (double) c(1, 1) == Approx(-4.0) );
  294. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  295. REQUIRE( (double) c(0, 2) == Approx(16.0) );
  296. REQUIRE( (double) c(1, 2) == Approx(16.0) );
  297. REQUIRE( (double) c(2, 2) == Approx(16.0) );
  298. mat e = b * a.submat(2, 2, 4, 4);
  299. REQUIRE( (double) e(0, 0) == Approx(4.0) );
  300. REQUIRE( (double) e(1, 0) == Approx(4.0) );
  301. REQUIRE( (double) e(2, 0) == Approx(4.0) );
  302. REQUIRE( (double) e(0, 1) == Approx(-4.0) );
  303. REQUIRE( (double) e(1, 1) == Approx(-4.0) );
  304. REQUIRE( (double) e(2, 1) == Approx(-4.0) );
  305. REQUIRE( (double) e(0, 2) == Approx(16.0) );
  306. REQUIRE( (double) e(1, 2) == Approx(16.0) );
  307. REQUIRE( (double) e(2, 2) == Approx(16.0) );
  308. c = b;
  309. c %= a.submat(2, 2, 4, 4);
  310. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  311. REQUIRE( (double) c(1, 0) == Approx(1e-5) );
  312. REQUIRE( (double) c(2, 0) == Approx(1e-5) );
  313. REQUIRE( (double) c(0, 1) == Approx(1e-5) );
  314. REQUIRE( (double) c(1, 1) == Approx(1e-5) );
  315. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  316. REQUIRE( (double) c(0, 2) == Approx(1e-5) );
  317. REQUIRE( (double) c(1, 2) == Approx(7.0) );
  318. REQUIRE( (double) c(2, 2) == Approx(9.0) );
  319. SpMat<double> d = b % a.submat(2, 2, 4, 4);
  320. REQUIRE( d.n_nonzero == 4 );
  321. REQUIRE( (double) d(0, 0) == Approx(4.0) );
  322. REQUIRE( (double) d(2, 1) == Approx(-4.0) );
  323. REQUIRE( (double) d(1, 2) == Approx(7.0) );
  324. REQUIRE( (double) d(2, 2) == Approx(9.0) );
  325. c = b;
  326. c /= a.submat(2, 2, 4, 4);
  327. REQUIRE( c(0, 0) == Approx(1.0) );
  328. REQUIRE( std::isinf(c(1, 0)) );
  329. REQUIRE( std::isinf(c(2, 0)) );
  330. REQUIRE( std::isinf(c(0, 1)) );
  331. REQUIRE( std::isinf(c(1, 1)) );
  332. REQUIRE( c(2, 1) == Approx(-1.0) );
  333. REQUIRE( std::isinf(c(0, 2)) );
  334. REQUIRE( c(1, 2) == Approx(2.0 / 3.5) );
  335. REQUIRE( c(2, 2) == Approx(2.0 / 4.5) );
  336. }
  337. TEST_CASE("sp_subview_base_test")
  338. {
  339. SpMat<double> a(6, 10);
  340. a(2, 2) = 2.0;
  341. a(3, 4) = 3.5;
  342. a(4, 3) = -2.0;
  343. a(4, 4) = 4.5;
  344. a(5, 1) = 3.2;
  345. a(0, 1) = 1.3;
  346. a(1, 1) = -4.0;
  347. a(5, 3) = 5.3;
  348. mat b(3, 3);
  349. b.fill(2.0);
  350. SpMat<double> c = a;
  351. c.submat(2, 2, 4, 4) = b;
  352. REQUIRE( c.n_nonzero == 13 );
  353. REQUIRE( (double) c(2, 2) == Approx(2.0) );
  354. REQUIRE( (double) c(3, 2) == Approx(2.0) );
  355. REQUIRE( (double) c(4, 2) == Approx(2.0) );
  356. REQUIRE( (double) c(2, 3) == Approx(2.0) );
  357. REQUIRE( (double) c(3, 3) == Approx(2.0) );
  358. REQUIRE( (double) c(4, 3) == Approx(2.0) );
  359. REQUIRE( (double) c(2, 4) == Approx(2.0) );
  360. REQUIRE( (double) c(3, 4) == Approx(2.0) );
  361. REQUIRE( (double) c(4, 4) == Approx(2.0) );
  362. c = a;
  363. c.submat(2, 2, 4, 4) += b;
  364. REQUIRE( c.n_nonzero == 12 );
  365. REQUIRE( (double) c(2, 2) == Approx(4.0) );
  366. REQUIRE( (double) c(3, 2) == Approx(2.0) );
  367. REQUIRE( (double) c(4, 2) == Approx(2.0) );
  368. REQUIRE( (double) c(2, 3) == Approx(2.0) );
  369. REQUIRE( (double) c(3, 3) == Approx(2.0) );
  370. REQUIRE( (double) c(4, 3) == Approx(1e-5) );
  371. REQUIRE( (double) c(2, 4) == Approx(2.0) );
  372. REQUIRE( (double) c(3, 4) == Approx(5.5) );
  373. REQUIRE( (double) c(4, 4) == Approx(6.5) );
  374. Mat<double> d = a.submat(2, 2, 4, 4) + b;
  375. c = a.submat(2, 2, 4, 4) + b;
  376. REQUIRE( c.n_nonzero == 8 );
  377. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  378. REQUIRE( (double) c(1, 0) == Approx(2.0) );
  379. REQUIRE( (double) c(2, 0) == Approx(2.0) );
  380. REQUIRE( (double) c(0, 1) == Approx(2.0) );
  381. REQUIRE( (double) c(1, 1) == Approx(2.0) );
  382. REQUIRE( (double) c(2, 1) == Approx(1e-5) );
  383. REQUIRE( (double) c(0, 2) == Approx(2.0) );
  384. REQUIRE( (double) c(1, 2) == Approx(5.5) );
  385. REQUIRE( (double) c(2, 2) == Approx(6.5) );
  386. c = a;
  387. c.submat(2, 2, 4, 4) -= b;
  388. REQUIRE( c.n_nonzero == 12 );
  389. REQUIRE( (double) c(2, 2) == Approx(1e-5) );
  390. REQUIRE( (double) c(3, 2) == Approx(-2.0) );
  391. REQUIRE( (double) c(4, 2) == Approx(-2.0) );
  392. REQUIRE( (double) c(2, 3) == Approx(-2.0) );
  393. REQUIRE( (double) c(3, 3) == Approx(-2.0) );
  394. REQUIRE( (double) c(4, 3) == Approx(-4.0) );
  395. REQUIRE( (double) c(2, 4) == Approx(-2.0) );
  396. REQUIRE( (double) c(3, 4) == Approx(1.5) );
  397. REQUIRE( (double) c(4, 4) == Approx(2.5) );
  398. c = a.submat(2, 2, 4, 4) - b;
  399. REQUIRE( c.n_nonzero == 8 );
  400. REQUIRE( (double) c(0, 0) == Approx(1e-5) );
  401. REQUIRE( (double) c(1, 0) == Approx(-2.0) );
  402. REQUIRE( (double) c(2, 0) == Approx(-2.0) );
  403. REQUIRE( (double) c(0, 1) == Approx(-2.0) );
  404. REQUIRE( (double) c(1, 1) == Approx(-2.0) );
  405. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  406. REQUIRE( (double) c(0, 2) == Approx(-2.0) );
  407. REQUIRE( (double) c(1, 2) == Approx(1.5) );
  408. REQUIRE( (double) c(2, 2) == Approx(2.5) );
  409. c = a;
  410. c.submat(2, 2, 4, 4) *= b;
  411. REQUIRE( c.n_nonzero == 13 );
  412. REQUIRE( (double) c(2, 2) == Approx(4.0) );
  413. REQUIRE( (double) c(3, 2) == Approx(7.0) );
  414. REQUIRE( (double) c(4, 2) == Approx(5.0) );
  415. REQUIRE( (double) c(2, 3) == Approx(4.0) );
  416. REQUIRE( (double) c(3, 3) == Approx(7.0) );
  417. REQUIRE( (double) c(4, 3) == Approx(5.0) );
  418. REQUIRE( (double) c(2, 4) == Approx(4.0) );
  419. REQUIRE( (double) c(3, 4) == Approx(7.0) );
  420. REQUIRE( (double) c(4, 4) == Approx(5.0) );
  421. c = a.submat(2, 2, 4, 4) * b;
  422. REQUIRE( c.n_nonzero == 9 );
  423. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  424. REQUIRE( (double) c(1, 0) == Approx(7.0) );
  425. REQUIRE( (double) c(2, 0) == Approx(5.0) );
  426. REQUIRE( (double) c(0, 1) == Approx(4.0) );
  427. REQUIRE( (double) c(1, 1) == Approx(7.0) );
  428. REQUIRE( (double) c(2, 1) == Approx(5.0) );
  429. REQUIRE( (double) c(0, 2) == Approx(4.0) );
  430. REQUIRE( (double) c(1, 2) == Approx(7.0) );
  431. REQUIRE( (double) c(2, 2) == Approx(5.0) );
  432. c = a.submat(2, 2, 4, 4) % b;
  433. REQUIRE( c.n_nonzero == 4 );
  434. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  435. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  436. REQUIRE( (double) c(1, 2) == Approx(7.0) );
  437. REQUIRE( (double) c(2, 2) == Approx(9.0) );
  438. c = a;
  439. c.submat(2, 2, 4, 4) %= b;
  440. REQUIRE( c.n_nonzero == 8 );
  441. REQUIRE( (double) c(2, 2) == Approx(4.0) );
  442. REQUIRE( (double) c(4, 3) == Approx(-4.0) );
  443. REQUIRE( (double) c(3, 4) == Approx(7.0) );
  444. REQUIRE( (double) c(4, 4) == Approx(9.0) );
  445. c = a.submat(2, 2, 4, 4) / b;
  446. REQUIRE( c.n_nonzero == 4 );
  447. REQUIRE( (double) c(0, 0) == Approx(1.0) );
  448. REQUIRE( (double) c(2, 1) == Approx(-1.0) );
  449. REQUIRE( (double) c(1, 2) == Approx(3.5 / 2.0) );
  450. REQUIRE( (double) c(2, 2) == Approx(4.5 / 2.0) );
  451. c = a;
  452. c.submat(2, 2, 4, 4) /= b;
  453. REQUIRE( c.n_nonzero == 8 );
  454. REQUIRE( (double) c(2, 2) == Approx(1.0) );
  455. REQUIRE( (double) c(4, 3) == Approx(-1.0) );
  456. REQUIRE( (double) c(3, 4) == Approx(3.5 / 2.0) );
  457. REQUIRE( (double) c(4, 4) == Approx(4.5 / 2.0) );
  458. }
  459. TEST_CASE("sp_subview_sp_mat_test")
  460. {
  461. SpMat<double> a(6, 10);
  462. a(2, 2) = 2.0;
  463. a(3, 4) = 3.5;
  464. a(4, 3) = -2.0;
  465. a(4, 4) = 4.5;
  466. a(5, 1) = 3.2;
  467. a(0, 1) = 1.3;
  468. a(1, 1) = -4.0;
  469. a(5, 3) = 5.3;
  470. SpMat<double> b(3, 3);
  471. b(0, 0) = 2.0;
  472. b(1, 2) = 1.5;
  473. b(2, 1) = 2.0;
  474. SpMat<double> c = a;
  475. c.submat(2, 2, 4, 4) = b;
  476. REQUIRE( c.n_nonzero == 7 );
  477. REQUIRE( (double) c(2, 2) == Approx(2.0) );
  478. REQUIRE( (double) c(3, 4) == Approx(1.5) );
  479. REQUIRE( (double) c(4, 3) == Approx(2.0) );
  480. c = a;
  481. c.submat(2, 2, 4, 4) += b;
  482. REQUIRE( c.n_nonzero == 7 );
  483. REQUIRE( (double) c(2, 2) == Approx(4.0) );
  484. REQUIRE( (double) c(3, 4) == Approx(5.0) );
  485. REQUIRE( (double) c(4, 4) == Approx(4.5) );
  486. c = a.submat(2, 2, 4, 4) + b;
  487. REQUIRE( c.n_nonzero == 3 );
  488. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  489. REQUIRE( (double) c(1, 2) == Approx(5.0) );
  490. REQUIRE( (double) c(2, 2) == Approx(4.5) );
  491. c = a;
  492. c.submat(2, 2, 4, 4) -= b;
  493. REQUIRE( c.n_nonzero == 7 );
  494. REQUIRE( (double) c(2, 2) == Approx(1e-5) );
  495. REQUIRE( (double) c(3, 2) == Approx(1e-5) );
  496. REQUIRE( (double) c(4, 2) == Approx(1e-5) );
  497. REQUIRE( (double) c(2, 3) == Approx(1e-5) );
  498. REQUIRE( (double) c(3, 3) == Approx(1e-5) );
  499. REQUIRE( (double) c(4, 3) == Approx(-4.0) );
  500. REQUIRE( (double) c(2, 4) == Approx(1e-5) );
  501. REQUIRE( (double) c(3, 4) == Approx(2.0) );
  502. REQUIRE( (double) c(4, 4) == Approx(4.5) );
  503. c = a.submat(2, 2, 4, 4) - b;
  504. REQUIRE( c.n_nonzero == 3 );
  505. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  506. REQUIRE( (double) c(1, 2) == Approx(2.0) );
  507. REQUIRE( (double) c(2, 2) == Approx(4.5) );
  508. c = a;
  509. c.submat(2, 2, 4, 4) *= b;
  510. REQUIRE( c.n_nonzero == 8 );
  511. REQUIRE( (double) c(2, 2) == Approx(4.0) );
  512. REQUIRE( (double) c(3, 3) == Approx(7.0) );
  513. REQUIRE( (double) c(4, 3) == Approx(9.0) );
  514. REQUIRE( (double) c(4, 4) == Approx(-3.0) );
  515. c = a.submat(2, 2, 4, 4) * b;
  516. REQUIRE( c.n_nonzero == 4 );
  517. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  518. REQUIRE( (double) c(1, 1) == Approx(7.0) );
  519. REQUIRE( (double) c(2, 1) == Approx(9.0) );
  520. REQUIRE( (double) c(2, 2) == Approx(-3.0) );
  521. c = a.submat(2, 2, 4, 4) % b;
  522. REQUIRE( c.n_nonzero == 3 );
  523. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  524. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  525. REQUIRE( (double) c(1, 2) == Approx(5.25) );
  526. REQUIRE( (double) c(2, 2) == Approx(1e-5) );
  527. c = a;
  528. c.submat(2, 2, 4, 4) %= b;
  529. REQUIRE( c.n_nonzero == 7 );
  530. REQUIRE( (double) c(2, 2) == Approx(4.0) );
  531. REQUIRE( (double) c(4, 3) == Approx(-4.0) );
  532. REQUIRE( (double) c(3, 4) == Approx(5.25) );
  533. REQUIRE( (double) c(4, 4) == Approx(1e-5) );
  534. // c = a.submat(2, 2, 4, 4) / b;
  535. // REQUIRE( c.n_nonzero == 9 );
  536. // REQUIRE( (double) c(0, 0) == Approx(1.0) );
  537. // REQUIRE( (double) c(1, 0) != (double) c(1, 0) );
  538. // REQUIRE( (double) c(2, 0) != (double) c(2, 0) );
  539. // REQUIRE( (double) c(0, 1) != (double) c(0, 1) );
  540. // REQUIRE( (double) c(1, 1) != (double) c(1, 1) );
  541. // REQUIRE( (double) c(2, 1) == Approx(-1.0) );
  542. // REQUIRE( (double) c(0, 2) != (double) c(0, 2) );
  543. // REQUIRE( (double) c(1, 2) == Approx((3.5 / 1.5)) );
  544. // REQUIRE( std::isinf((double) c(2, 2)) );
  545. c = a;
  546. c.submat(2, 2, 4, 4) /= b;
  547. REQUIRE( c.n_nonzero == 13 );
  548. REQUIRE( (double) c(2, 2) == Approx(1.0) );
  549. REQUIRE( (double) c(3, 2) != (double) c(3, 2));
  550. REQUIRE( (double) c(4, 2) != (double) c(4, 2));
  551. REQUIRE( (double) c(2, 3) != (double) c(2, 3));
  552. REQUIRE( (double) c(3, 3) != (double) c(3, 3));
  553. REQUIRE( (double) c(4, 3) == Approx(-1.0) );
  554. REQUIRE( (double) c(2, 4) != (double) c(2, 4) );
  555. REQUIRE( (double) c(3, 4) == Approx((3.5 / 1.5)) );
  556. REQUIRE( std::isinf((double) c(4, 4)) );
  557. }
  558. TEST_CASE("sp_subview_sp_subview_tests")
  559. {
  560. SpMat<double> a(6, 10);
  561. a(2, 2) = 2.0;
  562. a(3, 4) = 3.5;
  563. a(4, 3) = -2.0;
  564. a(4, 4) = 4.5;
  565. a(5, 1) = 3.2;
  566. a(0, 1) = 1.3;
  567. a(1, 1) = -4.0;
  568. a(5, 3) = 5.3;
  569. SpMat<double> b(5, 5);
  570. b(0, 0) = 1.0;
  571. b(0, 1) = 1.0;
  572. b(0, 2) = 1.0;
  573. b(0, 3) = 1.0;
  574. b(0, 4) = 1.0;
  575. b(1, 0) = 1.0;
  576. b(2, 0) = 1.0;
  577. b(3, 0) = 1.0;
  578. b(4, 0) = 1.0;
  579. b(4, 1) = 1.0;
  580. b(4, 2) = 1.0;
  581. b(4, 3) = 1.0;
  582. b(4, 4) = 1.0;
  583. b(3, 4) = 1.0;
  584. b(2, 4) = 1.0;
  585. b(1, 4) = 1.0;
  586. b(1, 1) = 2.0;
  587. b(2, 3) = 1.5;
  588. b(3, 2) = 2.0;
  589. SpMat<double> c = a;
  590. c.submat(2, 2, 4, 4) = b.submat(1, 1, 3, 3);
  591. REQUIRE( c.n_nonzero == 7 );
  592. REQUIRE( (double) c(2, 2) == Approx(2.0) );
  593. REQUIRE( (double) c(3, 4) == Approx(1.5) );
  594. REQUIRE( (double) c(4, 3) == Approx(2.0) );
  595. c = a;
  596. c.submat(2, 2, 4, 4) += b.submat(1, 1, 3, 3);
  597. REQUIRE( c.n_nonzero == 7 );
  598. REQUIRE( (double) c(2, 2) == Approx(4.0) );
  599. REQUIRE( (double) c(3, 4) == Approx(5.0) );
  600. REQUIRE( (double) c(4, 4) == Approx(4.5) );
  601. c = a.submat(2, 2, 4, 4) + b.submat(1, 1, 3, 3);
  602. REQUIRE( c.n_nonzero == 3 );
  603. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  604. REQUIRE( (double) c(1, 2) == Approx(5.0) );
  605. REQUIRE( (double) c(2, 2) == Approx(4.5) );
  606. c = a;
  607. c.submat(2, 2, 4, 4) -= b.submat(1, 1, 3, 3);
  608. REQUIRE( c.n_nonzero == 7 );
  609. REQUIRE( (double) c(2, 2) == Approx(1e-5) );
  610. REQUIRE( (double) c(3, 2) == Approx(1e-5) );
  611. REQUIRE( (double) c(4, 2) == Approx(1e-5) );
  612. REQUIRE( (double) c(2, 3) == Approx(1e-5) );
  613. REQUIRE( (double) c(3, 3) == Approx(1e-5) );
  614. REQUIRE( (double) c(4, 3) == Approx(-4.0) );
  615. REQUIRE( (double) c(2, 4) == Approx(1e-5) );
  616. REQUIRE( (double) c(3, 4) == Approx(2.0) );
  617. REQUIRE( (double) c(4, 4) == Approx(4.5) );
  618. c = a.submat(2, 2, 4, 4) - b.submat(1, 1, 3, 3);
  619. REQUIRE( c.n_nonzero == 3 );
  620. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  621. REQUIRE( (double) c(1, 2) == Approx(2.0) );
  622. REQUIRE( (double) c(2, 2) == Approx(4.5) );
  623. c = a;
  624. c.submat(2, 2, 4, 4) *= b.submat(1, 1, 3, 3);
  625. REQUIRE( c.n_nonzero == 8 );
  626. REQUIRE( (double) c(2, 2) == Approx(4.0) );
  627. REQUIRE( (double) c(3, 3) == Approx(7.0) );
  628. REQUIRE( (double) c(4, 3) == Approx(9.0) );
  629. REQUIRE( (double) c(4, 4) == Approx(-3.0) );
  630. c = a.submat(2, 2, 4, 4) * b.submat(1, 1, 3, 3);
  631. REQUIRE( c.n_nonzero == 4 );
  632. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  633. REQUIRE( (double) c(1, 1) == Approx(7.0) );
  634. REQUIRE( (double) c(2, 1) == Approx(9.0) );
  635. REQUIRE( (double) c(2, 2) == Approx(-3.0) );
  636. c = a.submat(2, 2, 4, 4) % b.submat(1, 1, 3, 3);
  637. REQUIRE( c.n_nonzero == 3 );
  638. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  639. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  640. REQUIRE( (double) c(1, 2) == Approx(5.25) );
  641. REQUIRE( (double) c(2, 2) == Approx(1e-5) );
  642. c = a;
  643. c.submat(2, 2, 4, 4) %= b.submat(1, 1, 3, 3);
  644. REQUIRE( c.n_nonzero == 7 );
  645. REQUIRE( (double) c(2, 2) == Approx(4.0) );
  646. REQUIRE( (double) c(4, 3) == Approx(-4.0) );
  647. REQUIRE( (double) c(3, 4) == Approx(5.25) );
  648. REQUIRE( (double) c(4, 4) == Approx(1e-5) );
  649. // c = a.submat(2, 2, 4, 4) / b.submat(1, 1, 3, 3);
  650. // REQUIRE( c.n_nonzero == 9 );
  651. // REQUIRE( (double) c(0, 0) == Approx(1.0) );
  652. // REQUIRE( (double) c(1, 0) != (double) c(1, 0) );
  653. // REQUIRE( (double) c(2, 0) != (double) c(2, 0) );
  654. // REQUIRE( (double) c(0, 1) != (double) c(0, 1) );
  655. // REQUIRE( (double) c(1, 1) != (double) c(1, 1) );
  656. // REQUIRE( (double) c(2, 1) == Approx(-1.0) );
  657. // REQUIRE( (double) c(0, 2) != (double) c(0, 2) );
  658. // REQUIRE( (double) c(1, 2) == Approx((3.5 / 1.5)) );
  659. // REQUIRE( std::isinf((double) c(2, 2)) );
  660. c = a;
  661. c.submat(2, 2, 4, 4) /= b.submat(1, 1, 3, 3);
  662. REQUIRE( c.n_nonzero == 13 );
  663. REQUIRE( (double) c(2, 2) == Approx(1.0) );
  664. REQUIRE( (double) c(3, 2) != (double) c(3, 2) );
  665. REQUIRE( (double) c(4, 2) != (double) c(4, 2) );
  666. REQUIRE( (double) c(2, 3) != (double) c(2, 3) );
  667. REQUIRE( (double) c(3, 3) != (double) c(3, 3) );
  668. REQUIRE( (double) c(4, 3) == Approx(-1.0) );
  669. REQUIRE( (double) c(2, 4) != (double) c(2, 4) );
  670. REQUIRE( (double) c(3, 4) == Approx((3.5 / 1.5)) );
  671. REQUIRE( std::isinf((double) c(4, 4)) );
  672. }
  673. TEST_CASE("sp_subview_iterators_test")
  674. {
  675. SpMat<double> b(5, 5);
  676. b(0, 0) = 1.0;
  677. b(0, 1) = 1.0;
  678. b(0, 2) = 1.0;
  679. b(0, 3) = 1.0;
  680. b(0, 4) = 1.0;
  681. b(1, 0) = 1.0;
  682. b(2, 0) = 1.0;
  683. b(3, 0) = 1.0;
  684. b(4, 0) = 1.0;
  685. b(4, 1) = 1.0;
  686. b(4, 2) = 1.0;
  687. b(4, 3) = 1.0;
  688. b(4, 4) = 1.0;
  689. b(3, 4) = 1.0;
  690. b(2, 4) = 1.0;
  691. b(1, 4) = 1.0;
  692. b(1, 1) = 2.0;
  693. b(2, 3) = 1.5;
  694. b(3, 2) = 2.0;
  695. // [[1.0 1.0 1.0 1.0 1.0]
  696. // [1.0 2.0 0.0 0.0 1.0]
  697. // [1.0 0.0 0.0 1.5 1.0]
  698. // [1.0 0.0 2.0 0.0 1.0]
  699. // [1.0 1.0 1.0 1.0 1.0]]
  700. SpSubview<double> s = b.submat(1, 1, 3, 3);
  701. SpSubview<double>::iterator it = s.begin();
  702. REQUIRE( it.pos() == 0 );
  703. REQUIRE( it.skip_pos == 6 );
  704. REQUIRE( it.row() == 0 );
  705. REQUIRE( it.col() == 0 );
  706. REQUIRE( (double) (*it) == Approx(2.0) );
  707. ++it;
  708. REQUIRE( it.pos() == 1 );
  709. REQUIRE( it.skip_pos == 8 );
  710. REQUIRE( it.row() == 2 );
  711. REQUIRE( it.col() == 1 );
  712. REQUIRE( (double) (*it) == Approx(2.0) );
  713. ++it;
  714. REQUIRE( it.pos() == 2 );
  715. REQUIRE( it.skip_pos == 10 );
  716. REQUIRE( it.row() == 1 );
  717. REQUIRE( it.col() == 2 );
  718. REQUIRE( (double) (*it) == Approx(1.5) );
  719. *it = 4.3;
  720. REQUIRE( (double) (*it) == Approx(4.3) );
  721. ++it;
  722. REQUIRE( it.pos() == s.n_nonzero );
  723. --it;
  724. REQUIRE( it.pos() == 2 );
  725. REQUIRE( it.skip_pos == 10 );
  726. REQUIRE( it.row() == 1 );
  727. REQUIRE( it.col() == 2 );
  728. REQUIRE( (double) (*it) == Approx(4.3) );
  729. --it;
  730. REQUIRE( it.pos() == 1 );
  731. REQUIRE( it.skip_pos == 8 );
  732. REQUIRE( it.row() == 2 );
  733. REQUIRE( it.col() == 1 );
  734. REQUIRE( (double) (*it) == Approx(2.0) );
  735. --it;
  736. REQUIRE( it.pos() == 0 );
  737. REQUIRE( it.skip_pos == 6 );
  738. REQUIRE( it.row() == 0 );
  739. REQUIRE( it.col() == 0 );
  740. REQUIRE( (double) (*it) == Approx(2.0) );
  741. SpMat<double> c(5, 5);
  742. c(1, 1) = 2.0;
  743. c(2, 3) = 1.5;
  744. c(3, 2) = 2.0;
  745. SpSubview<double> ss = c.submat(1, 1, 3, 3);
  746. SpSubview<double>::iterator sit = ss.begin();
  747. REQUIRE( sit.pos() == 0 );
  748. REQUIRE( sit.skip_pos == 0 );
  749. REQUIRE( sit.row() == 0 );
  750. REQUIRE( sit.col() == 0 );
  751. REQUIRE( (double) (*sit) == Approx(2.0) );
  752. ++sit;
  753. REQUIRE( sit.pos() == 1 );
  754. REQUIRE( sit.skip_pos == 0 );
  755. REQUIRE( sit.row() == 2 );
  756. REQUIRE( sit.col() == 1 );
  757. REQUIRE( (double) (*sit) == Approx(2.0) );
  758. ++sit;
  759. REQUIRE( sit.pos() == 2 );
  760. REQUIRE( sit.skip_pos == 0 );
  761. REQUIRE( sit.row() == 1 );
  762. REQUIRE( sit.col() == 2 );
  763. REQUIRE( (double) (*sit) == Approx(1.5) );
  764. *sit = 4.2;
  765. REQUIRE( (double) (*sit) == Approx(4.2) );
  766. ++sit;
  767. REQUIRE( sit.pos() == ss.n_nonzero );
  768. --sit;
  769. REQUIRE( sit.pos() == 2 );
  770. REQUIRE( sit.skip_pos == 0 );
  771. REQUIRE( sit.row() == 1 );
  772. REQUIRE( sit.col() == 2 );
  773. REQUIRE( (double) (*sit) == Approx(4.2) );
  774. --sit;
  775. REQUIRE( sit.pos() == 1 );
  776. REQUIRE( sit.skip_pos == 0 );
  777. REQUIRE( sit.row() == 2 );
  778. REQUIRE( sit.col() == 1 );
  779. REQUIRE( (double) (*sit) == Approx(2.0) );
  780. --sit;
  781. REQUIRE( sit.pos() == 0 );
  782. REQUIRE( sit.skip_pos == 0 );
  783. REQUIRE( sit.row() == 0 );
  784. REQUIRE( sit.col() == 0 );
  785. REQUIRE( (double) (*sit) == Approx(2.0) );
  786. }
  787. TEST_CASE("sp_subview_row_iterators_test")
  788. {
  789. SpMat<double> b(5, 5);
  790. b(0, 0) = 1.0;
  791. b(0, 1) = 1.0;
  792. b(0, 2) = 1.0;
  793. b(0, 3) = 1.0;
  794. b(0, 4) = 1.0;
  795. b(1, 0) = 1.0;
  796. b(2, 0) = 1.0;
  797. b(3, 0) = 1.0;
  798. b(4, 0) = 1.0;
  799. b(4, 1) = 1.0;
  800. b(4, 2) = 1.0;
  801. b(4, 3) = 1.0;
  802. b(4, 4) = 1.0;
  803. b(3, 4) = 1.0;
  804. b(2, 4) = 1.0;
  805. b(1, 4) = 1.0;
  806. b(1, 1) = 2.0;
  807. b(2, 3) = 1.5;
  808. b(3, 2) = 2.0;
  809. // [[1.0 1.0 1.0 1.0 1.0]
  810. // [1.0 2.0 0.0 0.0 1.0]
  811. // [1.0 0.0 0.0 1.5 1.0]
  812. // [1.0 0.0 2.0 0.0 1.0]
  813. // [1.0 1.0 1.0 1.0 1.0]]
  814. SpSubview<double> s = b.submat(1, 1, 3, 3);
  815. SpSubview<double>::row_iterator it = s.begin_row();
  816. REQUIRE( it.pos() == 0 );
  817. REQUIRE( it.row() == 0 );
  818. REQUIRE( it.col() == 0 );
  819. REQUIRE( it.actual_pos == 6 );
  820. REQUIRE( (double) (*it) == Approx(2.0) );
  821. ++it;
  822. REQUIRE( it.pos() == 1 );
  823. REQUIRE( it.row() == 1 );
  824. REQUIRE( it.col() == 2 );
  825. REQUIRE( it.actual_pos == 12 );
  826. REQUIRE( (double) (*it) == Approx(1.5) );
  827. ++it;
  828. REQUIRE( it.pos() == 2 );
  829. REQUIRE( it.row() == 2 );
  830. REQUIRE( it.col() == 1 );
  831. REQUIRE( it.actual_pos == 9 );
  832. REQUIRE( (double) (*it) == Approx(2.0) );
  833. ++it;
  834. REQUIRE( it.pos() == s.n_nonzero );
  835. --it;
  836. REQUIRE( it.pos() == 2 );
  837. REQUIRE( it.row() == 2 );
  838. REQUIRE( it.col() == 1 );
  839. REQUIRE( it.actual_pos == 9 );
  840. REQUIRE( (double) (*it) == Approx(2.0) );
  841. (*it) = 4.0;
  842. REQUIRE( (double) (*it) == Approx(4.0) );
  843. --it;
  844. REQUIRE( it.pos() == 1 );
  845. REQUIRE( it.row() == 1 );
  846. REQUIRE( it.col() == 2 );
  847. REQUIRE( it.actual_pos == 12 );
  848. REQUIRE( (double) (*it) == Approx(1.5) );
  849. --it;
  850. REQUIRE( it.pos() == 0 );
  851. REQUIRE( it.row() == 0 );
  852. REQUIRE( it.col() == 0 );
  853. REQUIRE( it.actual_pos == 6 );
  854. REQUIRE( (double) (*it) == Approx(2.0) );
  855. // now a different matrix
  856. SpMat<double> c(5, 5);
  857. c(1, 1) = 2.0;
  858. c(2, 3) = 1.5;
  859. c(3, 2) = 2.0;
  860. SpSubview<double> ss = c.submat(0, 0, 3, 3);
  861. SpSubview<double>::row_iterator sit = ss.begin_row();
  862. REQUIRE( sit.pos() == 0 );
  863. REQUIRE( sit.row() == 1 );
  864. REQUIRE( sit.col() == 1 );
  865. REQUIRE( (double) (*sit) == Approx(2.0) );
  866. ++sit;
  867. REQUIRE( sit.pos() == 1 );
  868. REQUIRE( sit.row() == 2 );
  869. REQUIRE( sit.col() == 3 );
  870. REQUIRE( (double) (*sit) == Approx(1.5) );
  871. ++sit;
  872. REQUIRE( sit.pos() == 2 );
  873. REQUIRE( sit.row() == 3 );
  874. REQUIRE( sit.col() == 2 );
  875. REQUIRE( (double) (*sit) == Approx(2.0) );
  876. ++sit;
  877. REQUIRE( sit.pos() == ss.n_nonzero );
  878. --sit;
  879. REQUIRE( sit.pos() == 2 );
  880. REQUIRE( sit.row() == 3 );
  881. REQUIRE( sit.col() == 2 );
  882. REQUIRE( (double) (*sit) == Approx(2.0) );
  883. (*sit) = 4.0;
  884. REQUIRE( (double) (*sit) == Approx(4.0) );
  885. --sit;
  886. REQUIRE( sit.pos() == 1 );
  887. REQUIRE( sit.row() == 2 );
  888. REQUIRE( sit.col() == 3 );
  889. REQUIRE( (double) (*sit) == Approx(1.5) );
  890. --sit;
  891. REQUIRE( sit.pos() == 0 );
  892. REQUIRE( sit.row() == 1 );
  893. REQUIRE( sit.col() == 1 );
  894. REQUIRE( (double) (*sit) == Approx(2.0) );
  895. }
  896. TEST_CASE("sp_subview_sp_base_add_subtract_modulo")
  897. {
  898. SpMat<double> m;
  899. m.sprandu(100, 100, 0.1);
  900. SpMat<double> n;
  901. n.sprandu(50, 50, 0.1);
  902. Mat<double> x(m);
  903. Mat<double> y(n);
  904. m.submat(25, 25, 74, 74) += n;
  905. x.submat(25, 25, 74, 74) += y;
  906. for (uword c = 0; c < 100; ++c)
  907. {
  908. for (uword r = 0; r < 100; ++r)
  909. {
  910. REQUIRE( (double) m(r, c) == Approx(x(r, c)) );
  911. }
  912. }
  913. m.sprandu(100, 100, 0.1);
  914. n.sprandu(50, 50, 0.1);
  915. x = m;
  916. y = n;
  917. m.submat(25, 25, 74, 74) -= n;
  918. x.submat(25, 25, 74, 74) -= y;
  919. for (uword c = 0; c < 100; ++c)
  920. {
  921. for (uword r = 0; r < 100; ++r)
  922. {
  923. REQUIRE( (double) m(r, c) == Approx(x(r, c)) );
  924. }
  925. }
  926. m.sprandu(100, 100, 0.1);
  927. n.sprandu(50, 50, 0.1);
  928. x = m;
  929. y = n;
  930. m.submat(25, 25, 74, 74) %= n;
  931. x.submat(25, 25, 74, 74) %= y;
  932. for( uword c = 0; c < 100; ++c)
  933. {
  934. for( uword r = 0; r < 100; ++r)
  935. {
  936. REQUIRE( (double) m(r, c) == Approx(x(r, c)) );
  937. }
  938. }
  939. }
  940. TEST_CASE("sp_subview_hadamard")
  941. {
  942. SpMat<double> x;
  943. x.sprandu(100, 100, 0.1);
  944. Mat<double> d(x);
  945. SpMat<double> y;
  946. y.sprandu(200, 200, 0.1);
  947. Mat<double> dy(y);
  948. x %= y.submat(50, 50, 149, 149);
  949. d %= dy.submat(50, 50, 149, 149);
  950. for (uword c = 0; c < 100; ++c)
  951. {
  952. for (uword r = 0; r < 100; ++r)
  953. {
  954. REQUIRE( (double) x(r, c) == Approx(d(r, c)) );
  955. }
  956. }
  957. }
  958. TEST_CASE("sp_subview_subviews_test")
  959. {
  960. SpMat<double> m(20, 20);
  961. m.sprandu(20, 20, 0.3);
  962. // Get a subview.
  963. SpSubview<double> s = m.submat(1, 1, 10, 10); // 10x10
  964. const SpSubview<double> c = m.submat(1, 1, 10, 10);
  965. SpSubview<double> t = s.row(1);
  966. const SpSubview<double> d = c.row(1);
  967. REQUIRE( t.n_rows == 1 );
  968. REQUIRE( t.n_cols == 10 );
  969. REQUIRE( d.n_rows == 1 );
  970. REQUIRE( d.n_cols == 10 );
  971. REQUIRE( t.aux_row1 == 2 );
  972. REQUIRE( t.aux_col1 == 1 );
  973. for (uword i = 0; i < 10; ++i)
  974. {
  975. REQUIRE( (double) t[i] == (double) m(2, i + 1) );
  976. REQUIRE( d[i] == (double) m(2, i + 1) );
  977. }
  978. SpSubview<double> t1 = s.col(2);
  979. const SpSubview<double> d1 = c.col(2);
  980. REQUIRE( t1.n_rows == 10 );
  981. REQUIRE( t1.n_cols == 1 );
  982. REQUIRE( d1.n_rows == 10 );
  983. REQUIRE( d1.n_cols == 1 );
  984. for (uword i = 0; i < 10; ++i)
  985. {
  986. REQUIRE( (double) t1[i] == (double) m(i + 1, 3) );
  987. REQUIRE( d1[i] == (double) m(i + 1, 3) );
  988. }
  989. SpSubview<double> t2 = s.rows(3, 5);
  990. const SpSubview<double> d2 = c.rows(3, 5);
  991. REQUIRE( t2.n_rows == 3 );
  992. REQUIRE( t2.n_cols == 10 );
  993. REQUIRE( d2.n_rows == 3 );
  994. REQUIRE( d2.n_cols == 10 );
  995. for (uword j = 0; j < 3; ++j)
  996. {
  997. for (uword i = 0; i < 10; ++i)
  998. {
  999. REQUIRE( (double) t2(j, i) == (double) m(4 + j, i + 1) );
  1000. REQUIRE( d2(j, i) == (double) m(4 + j, i + 1) );
  1001. }
  1002. }
  1003. SpSubview<double> t3 = s.cols(4, 6);
  1004. const SpSubview<double> d3 = c.cols(4, 6);
  1005. REQUIRE( t3.n_rows == 10 );
  1006. REQUIRE( t3.n_cols == 3 );
  1007. REQUIRE( d3.n_rows == 10 );
  1008. REQUIRE( d3.n_cols == 3 );
  1009. for (uword j = 0; j < 3; ++j)
  1010. {
  1011. for (uword i = 0; i < 10; ++i)
  1012. {
  1013. REQUIRE( (double) t3(i, j) == (double) m(i + 1, 5 + j) );
  1014. REQUIRE( d3(i, j) == (double) m(i + 1, 5 + j) );
  1015. }
  1016. }
  1017. SpSubview<double> t4 = s.submat(1, 1, 6, 6);
  1018. const SpSubview<double> d4 = c.submat(1, 1, 6, 6);
  1019. REQUIRE( t4.n_rows == 6 );
  1020. REQUIRE( t4.n_cols == 6 );
  1021. REQUIRE( d4.n_rows == 6 );
  1022. REQUIRE( d4.n_cols == 6 );
  1023. for (uword j = 0; j < 6; ++j)
  1024. {
  1025. for (uword i = 0; i < 6; ++i)
  1026. {
  1027. REQUIRE( (double) t4(i, j) == (double) m(i + 2, 2 + j) );
  1028. REQUIRE( d4(i, j) == (double) m(i + 2, 2 + j) );
  1029. }
  1030. }
  1031. SpSubview<double> t5 = s.submat(span(2, 8), span(2, 5));
  1032. const SpSubview<double> d5 = c.submat(span(2, 8), span(2, 5));
  1033. REQUIRE( t5.n_rows == 7 );
  1034. REQUIRE( t5.n_cols == 4 );
  1035. REQUIRE( d5.n_rows == 7 );
  1036. REQUIRE( d5.n_cols == 4 );
  1037. for (uword j = 0; j < 4; ++j)
  1038. {
  1039. for (uword i = 0; i < 7; ++i)
  1040. {
  1041. REQUIRE( (double) t5(i, j) == (double) m(i + 3, 3 + j) );
  1042. REQUIRE( d5(i, j) == (double) m(i + 3, 3 + j) );
  1043. }
  1044. }
  1045. SpSubview<double> t6 = s(4, span(1, 5));
  1046. const SpSubview<double> d6 = c(4, span(1, 5));
  1047. REQUIRE( t6.n_rows == 1 );
  1048. REQUIRE( t6.n_cols == 5 );
  1049. REQUIRE( d6.n_rows == 1 );
  1050. REQUIRE( d6.n_cols == 5 );
  1051. for (uword i = 0; i < 5; ++i)
  1052. {
  1053. REQUIRE( (double) t6(i) == (double) m(5, 2 + i) );
  1054. REQUIRE( d6(i) == (double) m(5, 2 + i) );
  1055. }
  1056. SpSubview<double> t7 = s(span(1, 5), 4);
  1057. const SpSubview<double> d7 = c(span(1, 5), 4);
  1058. REQUIRE( t7.n_rows == 5 );
  1059. REQUIRE( t7.n_cols == 1 );
  1060. REQUIRE( d7.n_rows == 5 );
  1061. REQUIRE( d7.n_cols == 1 );
  1062. for (uword i = 0; i < 5; ++i)
  1063. {
  1064. REQUIRE( (double) t7(i) == (double) m(2 + i, 5) );
  1065. REQUIRE( d7(i) == (double) m(2 + i, 5) );
  1066. }
  1067. SpSubview<double> t8 = s(span(1, 9), span(7, 8));
  1068. const SpSubview<double> d8 = c(span(1, 9), span(7, 8));
  1069. REQUIRE( t8.n_rows == 9 );
  1070. REQUIRE( t8.n_cols == 2 );
  1071. REQUIRE( d8.n_rows == 9 );
  1072. REQUIRE( d8.n_cols == 2 );
  1073. for (uword j = 0; j < 2; ++j)
  1074. {
  1075. for (uword i = 0; i < 9; ++i)
  1076. {
  1077. REQUIRE( (double) t8(i, j) == (double) m(i + 2, 8 + j) );
  1078. REQUIRE( d8(i, j) == (double) m(i + 2, 8 + j) );
  1079. }
  1080. }
  1081. }
  1082. TEST_CASE("sp_subview_assignment_sp_base")
  1083. {
  1084. mat d(51, 51);
  1085. d.fill(7.0); // Why not?
  1086. mat dd(d);
  1087. sp_mat e;
  1088. e.sprandu(50, 50, 0.3);
  1089. mat ed(e); // Dense copy.
  1090. d.submat(0, 0, 49, 49) = e;
  1091. dd.submat(0, 0, 49, 49) = ed;
  1092. for (uword i = 0; i < d.n_elem; ++i)
  1093. {
  1094. REQUIRE( d[i] == Approx(dd[i]) );
  1095. }
  1096. }
  1097. TEST_CASE("sp_subview_addition_sp_base")
  1098. {
  1099. mat d(51, 51);
  1100. d.fill(7.0); // Why not?
  1101. mat dd(d);
  1102. sp_mat e;
  1103. e.sprandu(50, 50, 0.3);
  1104. mat ed(e); // Dense copy.
  1105. d.submat(0, 0, 49, 49) += e;
  1106. dd.submat(0, 0, 49, 49) += ed;
  1107. for (uword i = 0; i < d.n_elem; ++i)
  1108. {
  1109. REQUIRE( d[i] == Approx(dd[i]) );
  1110. }
  1111. }
  1112. TEST_CASE("sp_subview_subtraction_sp_base")
  1113. {
  1114. mat d(51, 51);
  1115. d.fill(7.0); // Why not?
  1116. mat dd(d);
  1117. sp_mat e;
  1118. e.sprandu(50, 50, 0.3);
  1119. mat ed(e); // Dense copy.
  1120. d.submat(0, 0, 49, 49) -= e;
  1121. dd.submat(0, 0, 49, 49) -= ed;
  1122. for (uword i = 0; i < d.n_elem; ++i)
  1123. {
  1124. REQUIRE( d[i] == Approx(dd[i]) );
  1125. }
  1126. }
  1127. TEST_CASE("sp_subview_schur_sp_base")
  1128. {
  1129. mat d(51, 51);
  1130. d.fill(7.0); // Why not?
  1131. mat dd(d);
  1132. sp_mat e;
  1133. e.sprandu(50, 50, 0.3);
  1134. mat ed(e); // Dense copy.
  1135. d.submat(0, 0, 49, 49) %= e;
  1136. dd.submat(0, 0, 49, 49) %= ed;
  1137. for (uword i = 0; i < d.n_elem; ++i)
  1138. {
  1139. REQUIRE( d[i] == Approx(dd[i]) );
  1140. }
  1141. }
  1142. TEST_CASE("sp_subview_division_sp_base")
  1143. {
  1144. mat d(51, 51);
  1145. d.fill(7.0); // Why not?
  1146. mat dd(d);
  1147. sp_mat e;
  1148. e.sprandu(50, 50, 0.3);
  1149. mat ed(e); // Dense copy.
  1150. d.submat(0, 0, 49, 49) /= e;
  1151. dd.submat(0, 0, 49, 49) /= ed;
  1152. for (uword i = 0; i < d.n_elem; ++i)
  1153. {
  1154. if (std::isinf(d[i]))
  1155. REQUIRE( std::isinf(dd[i]) );
  1156. else
  1157. REQUIRE( d[i] == Approx(dd[i]) );
  1158. }
  1159. }
  1160. TEST_CASE("sp_subview_row_iterator_constructor")
  1161. {
  1162. // Create a row iterator with an exact position.
  1163. Mat<double> tmp =
  1164. { { 1.1, 1.2, 1.3, 1.4, 1.5 },
  1165. { 2.1, 5.5, 0.0, 0.0, 3.1 },
  1166. { 2.2, 0.0, 0.0, 6.5, 3.2 },
  1167. { 2.3, 0.0, 7.5, 0.0, 3.3 },
  1168. { 2.4, 4.2, 4.3, 4.4, 3.4 } };
  1169. SpMat<double> X(tmp);
  1170. SpSubview<double> sv = X.submat(1, 1, 3, 3);
  1171. // Now create an iterator to an exact position.
  1172. SpSubview<double>::const_row_iterator cri(sv, 0, 1);
  1173. // This should end up at (1, 2) with value 6.5.
  1174. REQUIRE( cri.row() == 1 );
  1175. REQUIRE( cri.col() == 2 );
  1176. REQUIRE( (*cri) == Approx(6.5) );
  1177. cri = SpSubview<double>::const_row_iterator(sv, 0, 0);
  1178. // This should end up at (0, 0) with value 5.5.
  1179. REQUIRE( cri.row() == 0 );
  1180. REQUIRE( cri.col() == 0 );
  1181. REQUIRE( (*cri) == Approx(5.5) );
  1182. cri = SpSubview<double>::const_row_iterator(sv, 2, 1);
  1183. // This should end up at (2, 1) with value 7.5.
  1184. REQUIRE( cri.row() == 2 );
  1185. REQUIRE( cri.col() == 1 );
  1186. REQUIRE( (*cri) == Approx(7.5) );
  1187. }
  1188. TEST_CASE("sp_subview_row_iterator_test_1")
  1189. {
  1190. Mat<double> tmp =
  1191. { { 1.1, 1.2, 1.3, 1.4, 1.5 },
  1192. { 2.1, 5.5, 0.0, 0.0, 3.1 },
  1193. { 2.2, 0.0, 0.0, 6.5, 3.2 },
  1194. { 2.3, 0.0, 7.5, 0.0, 3.3 },
  1195. { 2.4, 4.2, 4.3, 4.4, 3.4 } };
  1196. SpMat<double> X(tmp);
  1197. SpSubview<double> sv = X.submat(1, 1, 3, 3);
  1198. SpSubview<double>::row_iterator it = sv.begin_row();
  1199. SpSubview<double>::row_iterator it2 = sv.begin_row();
  1200. ++it;
  1201. --it;
  1202. REQUIRE( it.row() == it2.row() );
  1203. REQUIRE( it.col() == it2.col() );
  1204. REQUIRE( (*it) == Approx(*it2) );
  1205. REQUIRE( it.pos() == it2.pos() );
  1206. }
  1207. TEST_CASE("sp_subview_row_iterator_test_2")
  1208. {
  1209. // Make sure the loop terminates.
  1210. Mat<double> tmp =
  1211. { { 1.1, 1.2, 1.3, 1.4, 1.5 },
  1212. { 2.1, 5.5, 0.0, 0.0, 3.1 },
  1213. { 2.2, 0.0, 0.0, 6.5, 3.2 },
  1214. { 2.3, 0.0, 7.5, 0.0, 3.3 },
  1215. { 2.4, 4.2, 4.3, 4.4, 3.4 } };
  1216. SpMat<double> X(tmp);
  1217. SpSubview<double> sv = X.submat(1, 1, 3, 3);
  1218. SpSubview<double>::row_iterator it = sv.end_row(sv.n_rows - 1); // points to past-the-end
  1219. SpSubview<double>::row_iterator it_end = sv.begin_row();
  1220. do {
  1221. --it;
  1222. } while(it != it_end);
  1223. REQUIRE( true );
  1224. }