You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

606 lines
28 KiB

  1. #include "gtest/gtest.h"
  2. #include "src/storage/SparseMatrix.h"
  3. #include "src/exceptions/InvalidStateException.h"
  4. #include "src/exceptions/OutOfRangeException.h"
  5. TEST(SparseMatrixBuilder, CreationWithDimensions) {
  6. storm::storage::SparseMatrixBuilder<double> matrixBuilder(3, 4, 5);
  7. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  8. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  9. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  10. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  11. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2));
  12. storm::storage::SparseMatrix<double> matrix;
  13. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  14. ASSERT_EQ(3, matrix.getRowCount());
  15. ASSERT_EQ(4, matrix.getColumnCount());
  16. ASSERT_EQ(5, matrix.getEntryCount());
  17. }
  18. TEST(SparseMatrixBuilder, CreationWithoutNumberOfEntries) {
  19. storm::storage::SparseMatrixBuilder<double> matrixBuilder(3, 4);
  20. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  21. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  22. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  23. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  24. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2));
  25. storm::storage::SparseMatrix<double> matrix;
  26. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  27. ASSERT_EQ(3, matrix.getRowCount());
  28. ASSERT_EQ(4, matrix.getColumnCount());
  29. ASSERT_EQ(5, matrix.getEntryCount());
  30. }
  31. TEST(SparseMatrixBuilder, CreationWithNumberOfRows) {
  32. storm::storage::SparseMatrixBuilder<double> matrixBuilder(3);
  33. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  34. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  35. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  36. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  37. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2));
  38. storm::storage::SparseMatrix<double> matrix;
  39. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  40. ASSERT_EQ(3, matrix.getRowCount());
  41. ASSERT_EQ(4, matrix.getColumnCount());
  42. ASSERT_EQ(5, matrix.getEntryCount());
  43. }
  44. TEST(SparseMatrixBuilder, CreationWithoutDimensions) {
  45. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  46. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  47. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 3, 1.2));
  48. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  49. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  50. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 2, 0.2));
  51. storm::storage::SparseMatrix<double> matrix;
  52. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  53. ASSERT_EQ(2, matrix.getRowCount());
  54. ASSERT_EQ(4, matrix.getColumnCount());
  55. ASSERT_EQ(5, matrix.getEntryCount());
  56. }
  57. TEST(SparseMatrixBuilder, AddNextValue) {
  58. storm::storage::SparseMatrixBuilder<double> matrixBuilder1(3, 4, 5);
  59. ASSERT_NO_THROW(matrixBuilder1.addNextValue(0, 1, 1.0));
  60. ASSERT_NO_THROW(matrixBuilder1.addNextValue(0, 2, 1.2));
  61. ASSERT_THROW(matrixBuilder1.addNextValue(0, 4, 0.5), storm::exceptions::OutOfRangeException);
  62. ASSERT_THROW(matrixBuilder1.addNextValue(3, 1, 0.5), storm::exceptions::OutOfRangeException);
  63. storm::storage::SparseMatrixBuilder<double> matrixBuilder2(3, 4);
  64. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0));
  65. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2));
  66. ASSERT_THROW(matrixBuilder2.addNextValue(0, 4, 0.5), storm::exceptions::OutOfRangeException);
  67. ASSERT_THROW(matrixBuilder2.addNextValue(3, 1, 0.5), storm::exceptions::OutOfRangeException);
  68. storm::storage::SparseMatrixBuilder<double> matrixBuilder3(3);
  69. ASSERT_NO_THROW(matrixBuilder3.addNextValue(0, 1, 1.0));
  70. ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 2, 1.2));
  71. ASSERT_NO_THROW(matrixBuilder3.addNextValue(2, 4, 0.5));
  72. ASSERT_THROW(matrixBuilder3.addNextValue(3, 1, 0.2), storm::exceptions::OutOfRangeException);
  73. storm::storage::SparseMatrixBuilder<double> matrixBuilder4;
  74. ASSERT_NO_THROW(matrixBuilder4.addNextValue(0, 1, 1.0));
  75. ASSERT_NO_THROW(matrixBuilder4.addNextValue(1, 2, 1.2));
  76. ASSERT_NO_THROW(matrixBuilder4.addNextValue(2, 4, 0.5));
  77. ASSERT_NO_THROW(matrixBuilder4.addNextValue(3, 1, 0.2));
  78. }
  79. TEST(SparseMatrix, Build) {
  80. storm::storage::SparseMatrixBuilder<double> matrixBuilder1(3, 4, 5);
  81. ASSERT_NO_THROW(matrixBuilder1.addNextValue(0, 1, 1.0));
  82. ASSERT_NO_THROW(matrixBuilder1.addNextValue(0, 2, 1.2));
  83. ASSERT_NO_THROW(matrixBuilder1.addNextValue(1, 0, 0.5));
  84. ASSERT_NO_THROW(matrixBuilder1.addNextValue(1, 1, 0.7));
  85. ASSERT_NO_THROW(matrixBuilder1.addNextValue(1, 3, 0.2));
  86. ASSERT_NO_THROW(matrixBuilder1.build());
  87. storm::storage::SparseMatrixBuilder<double> matrixBuilder2(3, 4, 5);
  88. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0));
  89. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2));
  90. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 0.5));
  91. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7));
  92. ASSERT_THROW(matrixBuilder2.build(), storm::exceptions::InvalidStateException);
  93. storm::storage::SparseMatrixBuilder<double> matrixBuilder3;
  94. ASSERT_NO_THROW(matrixBuilder3.addNextValue(0, 1, 1.0));
  95. ASSERT_NO_THROW(matrixBuilder3.addNextValue(0, 2, 1.2));
  96. ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 0, 0.5));
  97. ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 1, 0.7));
  98. ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 3, 0.2));
  99. storm::storage::SparseMatrix<double> matrix3;
  100. ASSERT_NO_THROW(matrix3 = matrixBuilder3.build());
  101. ASSERT_EQ(2, matrix3.getRowCount());
  102. ASSERT_EQ(4, matrix3.getColumnCount());
  103. ASSERT_EQ(5, matrix3.getEntryCount());
  104. storm::storage::SparseMatrixBuilder<double> matrixBuilder4;
  105. ASSERT_NO_THROW(matrixBuilder4.addNextValue(0, 1, 1.0));
  106. ASSERT_NO_THROW(matrixBuilder4.addNextValue(0, 2, 1.2));
  107. ASSERT_NO_THROW(matrixBuilder4.addNextValue(1, 0, 0.5));
  108. ASSERT_NO_THROW(matrixBuilder4.addNextValue(1, 1, 0.7));
  109. ASSERT_NO_THROW(matrixBuilder4.addNextValue(1, 3, 0.2));
  110. storm::storage::SparseMatrix<double> matrix4;
  111. ASSERT_NO_THROW(matrix4 = matrixBuilder4.build(4));
  112. ASSERT_EQ(4, matrix4.getRowCount());
  113. ASSERT_EQ(4, matrix4.getColumnCount());
  114. ASSERT_EQ(5, matrix4.getEntryCount());
  115. storm::storage::SparseMatrixBuilder<double> matrixBuilder5;
  116. ASSERT_NO_THROW(matrixBuilder5.addNextValue(0, 1, 1.0));
  117. ASSERT_NO_THROW(matrixBuilder5.addNextValue(0, 2, 1.2));
  118. ASSERT_NO_THROW(matrixBuilder5.addNextValue(1, 0, 0.5));
  119. ASSERT_NO_THROW(matrixBuilder5.addNextValue(1, 1, 0.7));
  120. ASSERT_NO_THROW(matrixBuilder5.addNextValue(1, 3, 0.2));
  121. storm::storage::SparseMatrix<double> matrix5;
  122. ASSERT_NO_THROW(matrix5 = matrixBuilder5.build(0, 6));
  123. ASSERT_EQ(2, matrix5.getRowCount());
  124. ASSERT_EQ(6, matrix5.getColumnCount());
  125. ASSERT_EQ(5, matrix5.getEntryCount());
  126. }
  127. TEST(SparseMatrix, CreationWithMovingContents) {
  128. std::vector<std::pair<uint_fast64_t, double>> columnsAndValues;
  129. columnsAndValues.emplace_back(1, 1.0);
  130. columnsAndValues.emplace_back(2, 1.2);
  131. columnsAndValues.emplace_back(0, 0.5);
  132. columnsAndValues.emplace_back(1, 0.7);
  133. columnsAndValues.emplace_back(3, 0.2);
  134. ASSERT_NO_THROW(storm::storage::SparseMatrix<double> matrix(4, {0, 2, 5, 5}, columnsAndValues));
  135. storm::storage::SparseMatrix<double> matrix(4, {0, 2, 5, 5}, columnsAndValues);
  136. ASSERT_EQ(3, matrix.getRowCount());
  137. ASSERT_EQ(4, matrix.getColumnCount());
  138. ASSERT_EQ(5, matrix.getEntryCount());
  139. }
  140. TEST(SparseMatrix, CopyConstruct) {
  141. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  142. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  143. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  144. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  145. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  146. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2));
  147. storm::storage::SparseMatrix<double> matrix;
  148. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  149. ASSERT_NO_THROW(storm::storage::SparseMatrix<double> copy(matrix));
  150. storm::storage::SparseMatrix<double> copy(matrix);
  151. ASSERT_TRUE(matrix == copy);
  152. }
  153. TEST(SparseMatrix, CopyAssign) {
  154. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  155. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  156. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  157. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  158. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  159. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2));
  160. storm::storage::SparseMatrix<double> matrix;
  161. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  162. ASSERT_NO_THROW(storm::storage::SparseMatrix<double> copy = matrix);
  163. storm::storage::SparseMatrix<double> copy = matrix;
  164. ASSERT_TRUE(matrix == copy);
  165. }
  166. TEST(SparseMatrix, MakeAbsorbing) {
  167. storm::storage::SparseMatrixBuilder<double> matrixBuilder(3, 4, 5);
  168. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  169. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  170. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  171. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  172. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2));
  173. storm::storage::SparseMatrix<double> matrix;
  174. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  175. storm::storage::BitVector absorbingRows(3);
  176. absorbingRows.set(1);
  177. ASSERT_NO_THROW(matrix.makeRowsAbsorbing(absorbingRows));
  178. storm::storage::SparseMatrixBuilder<double> matrixBuilder2(3, 4, 3);
  179. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0));
  180. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2));
  181. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 1));
  182. storm::storage::SparseMatrix<double> matrix2;
  183. ASSERT_NO_THROW(matrix2 = matrixBuilder2.build());
  184. ASSERT_TRUE(matrix == matrix2);
  185. }
  186. TEST(SparseMatrix, MakeRowGroupAbsorbing) {
  187. storm::storage::SparseMatrixBuilder<double> matrixBuilder(5, 4, 9);
  188. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  189. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  190. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  191. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  192. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5));
  193. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1));
  194. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1));
  195. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2));
  196. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3));
  197. storm::storage::SparseMatrix<double> matrix;
  198. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  199. std::vector<uint_fast64_t> rowGroupIndices = {0, 2, 4, 5};
  200. storm::storage::BitVector absorbingRowGroups(3);
  201. absorbingRowGroups.set(1);
  202. ASSERT_NO_THROW(matrix.makeRowsAbsorbing(absorbingRowGroups, rowGroupIndices));
  203. storm::storage::SparseMatrixBuilder<double> matrixBuilder2;
  204. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0));
  205. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2));
  206. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 0.5));
  207. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7));
  208. ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 1, 1));
  209. ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 1, 1));
  210. ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 0, 0.1));
  211. ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 1, 0.2));
  212. ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 3, 0.3));
  213. storm::storage::SparseMatrix<double> matrix2;
  214. ASSERT_NO_THROW(matrix2 = matrixBuilder2.build());
  215. ASSERT_TRUE(matrix == matrix2);
  216. }
  217. TEST(SparseMatrix, ConstrainedRowSumVector) {
  218. storm::storage::SparseMatrixBuilder<double> matrixBuilder(5, 4, 9);
  219. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  220. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  221. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  222. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  223. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5));
  224. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1));
  225. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1));
  226. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2));
  227. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3));
  228. storm::storage::SparseMatrix<double> matrix;
  229. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  230. storm::storage::BitVector columnConstraint(4);
  231. columnConstraint.set(1);
  232. columnConstraint.set(3);
  233. ASSERT_NO_THROW(std::vector<double> constrainedRowSum = matrix.getConstrainedRowSumVector(storm::storage::BitVector(5, true), columnConstraint));
  234. std::vector<double> constrainedRowSum = matrix.getConstrainedRowSumVector(storm::storage::BitVector(5, true), columnConstraint);
  235. ASSERT_TRUE(constrainedRowSum == std::vector<double>({1.0, 0.7, 0, 0, 0.5}));
  236. storm::storage::SparseMatrixBuilder<double> matrixBuilder2(5, 4, 9);
  237. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0));
  238. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2));
  239. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 0.5));
  240. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7));
  241. ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 0, 0.5));
  242. ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 2, 1.1));
  243. ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 3, 1.2));
  244. ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 0, 0.1));
  245. ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 3, 0.3));
  246. storm::storage::SparseMatrix<double> matrix2;
  247. ASSERT_NO_THROW(matrix2 = matrixBuilder2.build());
  248. std::vector<uint_fast64_t> rowGroupIndices = {0, 2, 4, 5};
  249. storm::storage::BitVector rowGroupConstraint(3);
  250. rowGroupConstraint.set(1);
  251. storm::storage::BitVector columnConstraint2(4);
  252. columnConstraint2.set(2);
  253. columnConstraint2.set(3);
  254. ASSERT_NO_THROW(std::vector<double> constrainedRowSum2 = matrix2.getConstrainedRowSumVector(rowGroupConstraint, rowGroupIndices, columnConstraint2));
  255. std::vector<double> constrainedRowSum2 = matrix2.getConstrainedRowSumVector(rowGroupConstraint, rowGroupIndices, columnConstraint2);
  256. ASSERT_TRUE(constrainedRowSum2 == std::vector<double>({0, 2.3}));
  257. }
  258. TEST(SparseMatrix, Submatrix) {
  259. storm::storage::SparseMatrixBuilder<double> matrixBuilder(5, 4, 9);
  260. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  261. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  262. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  263. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  264. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5));
  265. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1));
  266. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1));
  267. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2));
  268. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3));
  269. storm::storage::SparseMatrix<double> matrix;
  270. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  271. std::vector<uint_fast64_t> rowGroupIndices = {0, 1, 2, 4, 5};
  272. storm::storage::BitVector rowGroupConstraint(4);
  273. rowGroupConstraint.set(2);
  274. rowGroupConstraint.set(3);
  275. storm::storage::BitVector columnConstraint(4);
  276. columnConstraint.set(0);
  277. columnConstraint.set(3);
  278. ASSERT_NO_THROW(storm::storage::SparseMatrix<double> matrix2 = matrix.getSubmatrix(rowGroupConstraint, columnConstraint, rowGroupIndices, false));
  279. storm::storage::SparseMatrix<double> matrix2 = matrix.getSubmatrix(rowGroupConstraint, columnConstraint, rowGroupIndices, false);
  280. storm::storage::SparseMatrixBuilder<double> matrixBuilder3(3, 2, 3);
  281. ASSERT_NO_THROW(matrixBuilder3.addNextValue(0, 0, 0.5));
  282. ASSERT_NO_THROW(matrixBuilder3.addNextValue(2, 0, 0.1));
  283. ASSERT_NO_THROW(matrixBuilder3.addNextValue(2, 1, 0.3));
  284. storm::storage::SparseMatrix<double> matrix3;
  285. ASSERT_NO_THROW(matrix3 = matrixBuilder3.build());
  286. ASSERT_TRUE(matrix2 == matrix3);
  287. std::vector<uint_fast64_t> rowGroupToIndexMapping = {0, 0, 1, 0};
  288. ASSERT_NO_THROW(storm::storage::SparseMatrix<double> matrix4 = matrix.getSubmatrix(rowGroupToIndexMapping, rowGroupIndices));
  289. storm::storage::SparseMatrix<double> matrix4 = matrix.getSubmatrix(rowGroupToIndexMapping, rowGroupIndices);
  290. storm::storage::SparseMatrixBuilder<double> matrixBuilder5(4, 4, 8);
  291. ASSERT_NO_THROW(matrixBuilder5.addNextValue(0, 1, 1.0));
  292. ASSERT_NO_THROW(matrixBuilder5.addNextValue(0, 2, 1.2));
  293. ASSERT_NO_THROW(matrixBuilder5.addNextValue(1, 0, 0.5));
  294. ASSERT_NO_THROW(matrixBuilder5.addNextValue(1, 1, 0.7));
  295. ASSERT_NO_THROW(matrixBuilder5.addNextValue(2, 2, 1.1));
  296. ASSERT_NO_THROW(matrixBuilder5.addNextValue(3, 0, 0.1));
  297. ASSERT_NO_THROW(matrixBuilder5.addNextValue(3, 1, 0.2));
  298. ASSERT_NO_THROW(matrixBuilder5.addNextValue(3, 3, 0.3));
  299. storm::storage::SparseMatrix<double> matrix5;
  300. ASSERT_NO_THROW(matrix5 = matrixBuilder5.build());
  301. ASSERT_TRUE(matrix4 == matrix5);
  302. }
  303. TEST(SparseMatrix, Transpose) {
  304. storm::storage::SparseMatrixBuilder<double> matrixBuilder(5, 4, 9);
  305. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  306. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  307. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  308. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  309. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5));
  310. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1));
  311. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1));
  312. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2));
  313. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3));
  314. storm::storage::SparseMatrix<double> matrix;
  315. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  316. ASSERT_NO_THROW(storm::storage::SparseMatrix<double> transposeResult = matrix.transpose());
  317. storm::storage::SparseMatrix<double> transposeResult = matrix.transpose();
  318. storm::storage::SparseMatrixBuilder<double> matrixBuilder2(4, 5, 9);
  319. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 0.5));
  320. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 0.5));
  321. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 4, 0.1));
  322. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 1.0));
  323. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7));
  324. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 4, 0.2));
  325. ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 0, 1.2));
  326. ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 3, 1.1));
  327. ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 4, 0.3));
  328. storm::storage::SparseMatrix<double> matrix2;
  329. ASSERT_NO_THROW(matrix2 = matrixBuilder2.build());
  330. ASSERT_TRUE(transposeResult == matrix2);
  331. }
  332. TEST(SparseMatrix, EquationSystem) {
  333. storm::storage::SparseMatrixBuilder<double> matrixBuilder(4, 4, 7);
  334. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 0, 1.1));
  335. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.2));
  336. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.5));
  337. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.7));
  338. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5));
  339. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 2, 0.99));
  340. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 3, 0.11));
  341. storm::storage::SparseMatrix<double> matrix;
  342. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  343. ASSERT_NO_THROW(matrix.convertToEquationSystem());
  344. storm::storage::SparseMatrixBuilder<double> matrixBuilder2(4, 4, 7);
  345. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 0, 1 - 1.1));
  346. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, -1.2));
  347. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 1 - 0.5));
  348. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 3, -0.7));
  349. ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 0, -0.5));
  350. ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 2, 1 - 0.99));
  351. ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 3, 1 - 0.11));
  352. storm::storage::SparseMatrix<double> matrix2;
  353. ASSERT_NO_THROW(matrix2 = matrixBuilder2.build());
  354. ASSERT_TRUE(matrix == matrix2);
  355. }
  356. TEST(SparseMatrix, JacobiDecomposition) {
  357. storm::storage::SparseMatrixBuilder<double> matrixBuilder(4, 4, 7);
  358. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 0, 1.1));
  359. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.2));
  360. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.5));
  361. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.7));
  362. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5));
  363. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 2, 0.99));
  364. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 3, 0.11));
  365. storm::storage::SparseMatrix<double> matrix;
  366. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  367. ASSERT_NO_THROW(matrix.getJacobiDecomposition());
  368. std::pair<storm::storage::SparseMatrix<double>, storm::storage::SparseMatrix<double>> jacobiDecomposition = matrix.getJacobiDecomposition();
  369. storm::storage::SparseMatrixBuilder<double> luBuilder(4, 4, 3);
  370. ASSERT_NO_THROW(luBuilder.addNextValue(0, 1, 1.2));
  371. ASSERT_NO_THROW(luBuilder.addNextValue(1, 3, 0.7));
  372. ASSERT_NO_THROW(luBuilder.addNextValue(2, 0, 0.5));
  373. storm::storage::SparseMatrix<double> lu;
  374. ASSERT_NO_THROW(lu = luBuilder.build());
  375. storm::storage::SparseMatrixBuilder<double> dinvBuilder(4, 4, 4);
  376. ASSERT_NO_THROW(dinvBuilder.addNextValue(0, 0, 1 / 1.1));
  377. ASSERT_NO_THROW(dinvBuilder.addNextValue(1, 1, 1 / 0.5));
  378. ASSERT_NO_THROW(dinvBuilder.addNextValue(2, 2, 1 / 0.99));
  379. ASSERT_NO_THROW(dinvBuilder.addNextValue(3, 3, 1 / 0.11));
  380. storm::storage::SparseMatrix<double> dinv;
  381. ASSERT_NO_THROW(dinv = dinvBuilder.build());
  382. ASSERT_TRUE(lu == jacobiDecomposition.first);
  383. ASSERT_TRUE(dinv == jacobiDecomposition.second);
  384. }
  385. TEST(SparseMatrix, PointwiseMultiplicationVector) {
  386. storm::storage::SparseMatrixBuilder<double> matrixBuilder(5, 4, 9);
  387. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  388. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  389. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  390. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  391. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5));
  392. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1));
  393. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1));
  394. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2));
  395. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3));
  396. storm::storage::SparseMatrix<double> matrix;
  397. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  398. storm::storage::SparseMatrixBuilder<double> matrixBuilder2(5, 4, 9);
  399. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0));
  400. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2));
  401. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 0.5));
  402. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7));
  403. ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 0, 0.5));
  404. ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 2, 1.1));
  405. ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 0, 0.1));
  406. ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 1, 0.2));
  407. ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 3, 0.3));
  408. storm::storage::SparseMatrix<double> matrix2;
  409. ASSERT_NO_THROW(matrix2 = matrixBuilder2.build());
  410. ASSERT_NO_THROW(std::vector<double> pointwiseProductRowSums = matrix.getPointwiseProductRowSumVector(matrix2));
  411. std::vector<double> pointwiseProductRowSums = matrix.getPointwiseProductRowSumVector(matrix2);
  412. std::vector<double> correctResult = {1.0*1.0+1.2*1.2, 0.5*0.5+0.7*0.7, 0.5*0.5, 1.1*1.1, 0.1*0.1+0.2*0.2+0.3*0.3};
  413. ASSERT_TRUE(pointwiseProductRowSums == correctResult);
  414. }
  415. TEST(SparseMatrix, MatrixVectorMultiply) {
  416. storm::storage::SparseMatrixBuilder<double> matrixBuilder(5, 4, 9);
  417. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  418. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  419. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  420. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  421. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5));
  422. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1));
  423. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1));
  424. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2));
  425. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3));
  426. storm::storage::SparseMatrix<double> matrix;
  427. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  428. std::vector<double> x = {1, 0.3, 1.4, 7.1};
  429. std::vector<double> result(matrix.getRowCount());
  430. ASSERT_NO_THROW(matrix.multiplyWithVector(x, result));
  431. std::vector<double> correctResult = {1.0*0.3+1.2*1.4, 0.5*1+0.7*0.3, 0.5*1, 1.1*1.4, 0.1*1+0.2*0.3+0.3*7.1};
  432. ASSERT_TRUE(result == correctResult);
  433. }
  434. TEST(SparseMatrix, Iteration) {
  435. storm::storage::SparseMatrixBuilder<double> matrixBuilder(5, 4, 9);
  436. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  437. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  438. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  439. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  440. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5));
  441. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1));
  442. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1));
  443. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2));
  444. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3));
  445. storm::storage::SparseMatrix<double> matrix;
  446. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  447. for (auto const& entry : matrix.getRow(4)) {
  448. if (entry.first == 0) {
  449. ASSERT_EQ(0.1, entry.second);
  450. } else if (entry.first == 1) {
  451. ASSERT_EQ(0.2, entry.second);
  452. } else if (entry.first == 3) {
  453. ASSERT_EQ(0.3, entry.second);
  454. } else {
  455. ASSERT_TRUE(false);
  456. }
  457. }
  458. for (storm::storage::SparseMatrix<double>::iterator it = matrix.begin(4), ite = matrix.end(4); it != ite; ++it) {
  459. if (it->first == 0) {
  460. ASSERT_EQ(0.1, it->second);
  461. } else if (it->first == 1) {
  462. ASSERT_EQ(0.2, it->second);
  463. } else if (it->first == 3) {
  464. ASSERT_EQ(0.3, it->second);
  465. } else {
  466. ASSERT_TRUE(false);
  467. }
  468. }
  469. }
  470. TEST(SparseMatrix, RowSum) {
  471. storm::storage::SparseMatrixBuilder<double> matrixBuilder(5, 4, 8);
  472. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  473. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  474. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  475. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  476. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1));
  477. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1));
  478. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2));
  479. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3));
  480. storm::storage::SparseMatrix<double> matrix;
  481. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  482. ASSERT_EQ(0, matrix.getRowSum(2));
  483. ASSERT_EQ(0.1+0.2+0.3, matrix.getRowSum(4));
  484. }
  485. TEST(SparseMatrix, IsSubmatrix) {
  486. storm::storage::SparseMatrixBuilder<double> matrixBuilder(5, 4, 8);
  487. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0));
  488. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2));
  489. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5));
  490. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7));
  491. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1));
  492. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1));
  493. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2));
  494. ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3));
  495. storm::storage::SparseMatrix<double> matrix;
  496. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  497. storm::storage::SparseMatrixBuilder<double> matrixBuilder2(5, 4, 5);
  498. ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0));
  499. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 0.5));
  500. ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7));
  501. ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 0, 0.1));
  502. ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 1, 0.2));
  503. storm::storage::SparseMatrix<double> matrix2;
  504. ASSERT_NO_THROW(matrix2 = matrixBuilder2.build());
  505. ASSERT_TRUE(matrix2.isSubmatrixOf(matrix));
  506. storm::storage::SparseMatrixBuilder<double> matrixBuilder3(5, 4, 5);
  507. ASSERT_NO_THROW(matrixBuilder3.addNextValue(0, 3, 1.0));
  508. ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 0, 0.5));
  509. ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 1, 0.7));
  510. ASSERT_NO_THROW(matrixBuilder3.addNextValue(4, 0, 0.1));
  511. ASSERT_NO_THROW(matrixBuilder3.addNextValue(4, 1, 0.2));
  512. storm::storage::SparseMatrix<double> matrix3;
  513. ASSERT_NO_THROW(matrix3 = matrixBuilder3.build());
  514. ASSERT_FALSE(matrix3.isSubmatrixOf(matrix));
  515. ASSERT_FALSE(matrix3.isSubmatrixOf(matrix2));
  516. }